blob: 6adffd3aa828b61e38a81e43120a7298a30e6569 [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>
Frederic Weisbecker19d23dbf2016-09-26 02:29:20 +02005#include <linux/u64_stats_sync.h>
Dario Faggioliaab03e02013-11-28 11:14:43 +01006#include <linux/sched/deadline.h>
Steven Rostedt (Red Hat)3866e842016-02-22 16:26:51 -05007#include <linux/binfmts.h>
Peter Zijlstra029632f2011-10-25 10:00:11 +02008#include <linux/mutex.h>
9#include <linux/spinlock.h>
10#include <linux/stop_machine.h>
Steven Rostedtb6366f02015-03-18 14:49:46 -040011#include <linux/irq_work.h>
Frederic Weisbecker9f3660c2013-04-20 14:35:09 +020012#include <linux/tick.h>
Mel Gormanf809ca92013-10-07 11:28:57 +010013#include <linux/slab.h>
Peter Zijlstra029632f2011-10-25 10:00:11 +020014
Peter Zijlstra391e43d2011-11-15 17:14:39 +010015#include "cpupri.h"
Juri Lelli6bfd6d72013-11-07 14:43:47 +010016#include "cpudeadline.h"
Li Zefan60fed782013-03-29 14:36:43 +080017#include "cpuacct.h"
Peter Zijlstra029632f2011-10-25 10:00:11 +020018
Peter Zijlstra9148a3a2016-09-20 22:34:51 +020019#ifdef CONFIG_SCHED_DEBUG
20#define SCHED_WARN_ON(x) WARN_ONCE(x, #x)
21#else
22#define SCHED_WARN_ON(x) ((void)(x))
23#endif
24
Paul Gortmaker45ceebf2013-04-19 15:10:49 -040025struct rq;
Daniel Lezcano442bf3a2014-09-04 11:32:09 -040026struct cpuidle_state;
Paul Gortmaker45ceebf2013-04-19 15:10:49 -040027
Joonwoo Parkf7d6cd42017-01-17 15:19:43 -080028#ifdef CONFIG_SCHED_WALT
29extern unsigned int sched_ravg_window;
Channagoud Kadabi8810e5f2017-02-17 16:01:05 -080030
Pavankumar Kondeti84f72d72017-07-20 11:00:45 +053031struct walt_sched_stats {
Vikram Mulukutlad056dbc2017-02-07 18:58:07 -080032 int nr_big_tasks;
33 u64 cumulative_runnable_avg;
34 u64 pred_demands_sum;
35};
36
Joonwoo Parkf7d6cd42017-01-17 15:19:43 -080037struct cpu_cycle {
38 u64 cycles;
39 u64 time;
Vikram Mulukutlad056dbc2017-02-07 18:58:07 -080040};
41
42struct group_cpu_time {
43 u64 curr_runnable_sum;
44 u64 prev_runnable_sum;
45 u64 nt_curr_runnable_sum;
46 u64 nt_prev_runnable_sum;
47};
48
Joonwoo Parkf7d6cd42017-01-17 15:19:43 -080049struct load_subtractions {
50 u64 window_start;
51 u64 subs;
52 u64 new_subs;
53};
54
55#define NUM_TRACKED_WINDOWS 2
56#define NUM_LOAD_INDICES 1000
57
Vikram Mulukutlad056dbc2017-02-07 18:58:07 -080058struct sched_cluster {
59 raw_spinlock_t load_lock;
60 struct list_head list;
61 struct cpumask cpus;
62 int id;
63 int max_power_cost;
64 int min_power_cost;
65 int max_possible_capacity;
66 int capacity;
67 int efficiency; /* Differentiate cpus with different IPC capability */
68 int load_scale_factor;
69 unsigned int exec_scale_factor;
70 /*
71 * max_freq = user maximum
72 * max_mitigated_freq = thermal defined maximum
73 * max_possible_freq = maximum supported by hardware
74 */
75 unsigned int cur_freq, max_freq, max_mitigated_freq, min_freq;
76 unsigned int max_possible_freq;
77 bool freq_init_done;
78 int dstate, dstate_wakeup_latency, dstate_wakeup_energy;
79 unsigned int static_cluster_pwr_cost;
80 int notifier_sent;
81 bool wake_up_idle;
Syed Rameez Mustafa20acfe72017-01-30 09:35:46 +053082 u64 aggr_grp_load;
Vikram Mulukutlad056dbc2017-02-07 18:58:07 -080083};
84
Vikram Mulukutla4b54aae2017-03-20 13:41:37 -070085extern unsigned int sched_disable_window_stats;
Syed Rameez Mustafa20acfe72017-01-30 09:35:46 +053086
87extern struct timer_list sched_grp_timer;
Joonwoo Parkf7d6cd42017-01-17 15:19:43 -080088#endif /* CONFIG_SCHED_WALT */
Vikram Mulukutlad056dbc2017-02-07 18:58:07 -080089
90
Kirill Tkhaida0c1e62014-08-20 13:47:32 +040091/* task_struct::on_rq states: */
92#define TASK_ON_RQ_QUEUED 1
Kirill Tkhaicca26e82014-08-20 13:47:42 +040093#define TASK_ON_RQ_MIGRATING 2
Kirill Tkhaida0c1e62014-08-20 13:47:32 +040094
Peter Zijlstra029632f2011-10-25 10:00:11 +020095extern __read_mostly int scheduler_running;
96
Paul Gortmaker45ceebf2013-04-19 15:10:49 -040097extern unsigned long calc_load_update;
98extern atomic_long_t calc_load_tasks;
99
Peter Zijlstra3289bdb2015-04-14 13:19:42 +0200100extern void calc_global_load_tick(struct rq *this_rq);
Thomas Gleixnerd60585c2016-07-12 18:33:56 +0200101extern long calc_load_fold_active(struct rq *this_rq, long adjust);
Peter Zijlstra3289bdb2015-04-14 13:19:42 +0200102
103#ifdef CONFIG_SMP
Frederic Weisbeckercee1afc2016-04-13 15:56:50 +0200104extern void cpu_load_update_active(struct rq *this_rq);
Peter Zijlstra3289bdb2015-04-14 13:19:42 +0200105#else
Frederic Weisbeckercee1afc2016-04-13 15:56:50 +0200106static inline void cpu_load_update_active(struct rq *this_rq) { }
Peter Zijlstra3289bdb2015-04-14 13:19:42 +0200107#endif
Paul Gortmaker45ceebf2013-04-19 15:10:49 -0400108
Peter Zijlstra029632f2011-10-25 10:00:11 +0200109/*
Peter Zijlstra029632f2011-10-25 10:00:11 +0200110 * Helpers for converting nanosecond timing to jiffy resolution
111 */
112#define NS_TO_JIFFIES(TIME) ((unsigned long)(TIME) / (NSEC_PER_SEC / HZ))
113
Li Zefancc1f4b12013-03-05 16:06:09 +0800114/*
115 * Increase resolution of nice-level calculations for 64-bit architectures.
116 * The extra resolution improves shares distribution and load balancing of
117 * low-weight task groups (eg. nice +19 on an autogroup), deeper taskgroup
118 * hierarchies, especially on larger systems. This is not a user-visible change
119 * and does not change the user-interface for setting shares/weights.
120 *
121 * We increase resolution only if we have enough bits to allow this increased
Peter Zijlstra21591972016-04-28 12:49:38 +0200122 * resolution (i.e. 64bit). The costs for increasing resolution when 32bit are
123 * pretty high and the returns do not justify the increased costs.
124 *
125 * Really only required when CONFIG_FAIR_GROUP_SCHED is also set, but to
126 * increase coverage and consistency always enable it on 64bit platforms.
Li Zefancc1f4b12013-03-05 16:06:09 +0800127 */
Peter Zijlstra21591972016-04-28 12:49:38 +0200128#ifdef CONFIG_64BIT
Yuyang Du172895e2016-04-05 12:12:27 +0800129# define NICE_0_LOAD_SHIFT (SCHED_FIXEDPOINT_SHIFT + SCHED_FIXEDPOINT_SHIFT)
Yuyang Du6ecdd742016-04-05 12:12:26 +0800130# define scale_load(w) ((w) << SCHED_FIXEDPOINT_SHIFT)
131# define scale_load_down(w) ((w) >> SCHED_FIXEDPOINT_SHIFT)
Li Zefancc1f4b12013-03-05 16:06:09 +0800132#else
Yuyang Du172895e2016-04-05 12:12:27 +0800133# define NICE_0_LOAD_SHIFT (SCHED_FIXEDPOINT_SHIFT)
Li Zefancc1f4b12013-03-05 16:06:09 +0800134# define scale_load(w) (w)
135# define scale_load_down(w) (w)
136#endif
137
Yuyang Du6ecdd742016-04-05 12:12:26 +0800138/*
Yuyang Du172895e2016-04-05 12:12:27 +0800139 * Task weight (visible to users) and its load (invisible to users) have
140 * independent resolution, but they should be well calibrated. We use
141 * scale_load() and scale_load_down(w) to convert between them. The
142 * following must be true:
143 *
144 * scale_load(sched_prio_to_weight[USER_PRIO(NICE_TO_PRIO(0))]) == NICE_0_LOAD
145 *
Yuyang Du6ecdd742016-04-05 12:12:26 +0800146 */
Yuyang Du172895e2016-04-05 12:12:27 +0800147#define NICE_0_LOAD (1L << NICE_0_LOAD_SHIFT)
Peter Zijlstra029632f2011-10-25 10:00:11 +0200148
149/*
Dario Faggioli332ac172013-11-07 14:43:45 +0100150 * Single value that decides SCHED_DEADLINE internal math precision.
151 * 10 -> just above 1us
152 * 9 -> just above 0.5us
153 */
154#define DL_SCALE (10)
155
156/*
Peter Zijlstra029632f2011-10-25 10:00:11 +0200157 * These are the 'tuning knobs' of the scheduler:
Peter Zijlstra029632f2011-10-25 10:00:11 +0200158 */
Peter Zijlstra029632f2011-10-25 10:00:11 +0200159
160/*
161 * single value that denotes runtime == period, ie unlimited time.
162 */
163#define RUNTIME_INF ((u64)~0ULL)
164
Henrik Austad20f9cd22015-09-09 17:00:41 +0200165static inline int idle_policy(int policy)
166{
167 return policy == SCHED_IDLE;
168}
Dario Faggiolid50dde52013-11-07 14:43:36 +0100169static inline int fair_policy(int policy)
170{
171 return policy == SCHED_NORMAL || policy == SCHED_BATCH;
172}
173
Peter Zijlstra029632f2011-10-25 10:00:11 +0200174static inline int rt_policy(int policy)
175{
Dario Faggiolid50dde52013-11-07 14:43:36 +0100176 return policy == SCHED_FIFO || policy == SCHED_RR;
Peter Zijlstra029632f2011-10-25 10:00:11 +0200177}
178
Dario Faggioliaab03e02013-11-28 11:14:43 +0100179static inline int dl_policy(int policy)
180{
181 return policy == SCHED_DEADLINE;
182}
Henrik Austad20f9cd22015-09-09 17:00:41 +0200183static inline bool valid_policy(int policy)
184{
185 return idle_policy(policy) || fair_policy(policy) ||
186 rt_policy(policy) || dl_policy(policy);
187}
Dario Faggioliaab03e02013-11-28 11:14:43 +0100188
Peter Zijlstra029632f2011-10-25 10:00:11 +0200189static inline int task_has_rt_policy(struct task_struct *p)
190{
191 return rt_policy(p->policy);
192}
193
Dario Faggioliaab03e02013-11-28 11:14:43 +0100194static inline int task_has_dl_policy(struct task_struct *p)
195{
196 return dl_policy(p->policy);
197}
198
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100199/*
200 * Tells if entity @a should preempt entity @b.
201 */
Dario Faggioli332ac172013-11-07 14:43:45 +0100202static inline bool
203dl_entity_preempt(struct sched_dl_entity *a, struct sched_dl_entity *b)
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100204{
205 return dl_time_before(a->deadline, b->deadline);
206}
207
Peter Zijlstra029632f2011-10-25 10:00:11 +0200208/*
209 * This is the priority-queue data structure of the RT scheduling class:
210 */
211struct rt_prio_array {
212 DECLARE_BITMAP(bitmap, MAX_RT_PRIO+1); /* include 1 bit for delimiter */
213 struct list_head queue[MAX_RT_PRIO];
214};
215
216struct rt_bandwidth {
217 /* nests inside the rq lock: */
218 raw_spinlock_t rt_runtime_lock;
219 ktime_t rt_period;
220 u64 rt_runtime;
221 struct hrtimer rt_period_timer;
Peter Zijlstra4cfafd32015-05-14 12:23:11 +0200222 unsigned int rt_period_active;
Peter Zijlstra029632f2011-10-25 10:00:11 +0200223};
Juri Lellia5e7be32014-09-19 10:22:39 +0100224
225void __dl_clear_params(struct task_struct *p);
226
Dario Faggioli332ac172013-11-07 14:43:45 +0100227/*
228 * To keep the bandwidth of -deadline tasks and groups under control
229 * we need some place where:
230 * - store the maximum -deadline bandwidth of the system (the group);
231 * - cache the fraction of that bandwidth that is currently allocated.
232 *
233 * This is all done in the data structure below. It is similar to the
234 * one used for RT-throttling (rt_bandwidth), with the main difference
235 * that, since here we are only interested in admission control, we
236 * do not decrease any runtime while the group "executes", neither we
237 * need a timer to replenish it.
238 *
239 * With respect to SMP, the bandwidth is given on a per-CPU basis,
240 * meaning that:
241 * - dl_bw (< 100%) is the bandwidth of the system (group) on each CPU;
242 * - dl_total_bw array contains, in the i-eth element, the currently
243 * allocated bandwidth on the i-eth CPU.
244 * Moreover, groups consume bandwidth on each CPU, while tasks only
245 * consume bandwidth on the CPU they're running on.
246 * Finally, dl_total_bw_cpu is used to cache the index of dl_total_bw
247 * that will be shown the next time the proc or cgroup controls will
248 * be red. It on its turn can be changed by writing on its own
249 * control.
250 */
251struct dl_bandwidth {
252 raw_spinlock_t dl_runtime_lock;
253 u64 dl_runtime;
254 u64 dl_period;
255};
256
257static inline int dl_bandwidth_enabled(void)
258{
Peter Zijlstra17248132013-12-17 12:44:49 +0100259 return sysctl_sched_rt_runtime >= 0;
Dario Faggioli332ac172013-11-07 14:43:45 +0100260}
261
262extern struct dl_bw *dl_bw_of(int i);
263
264struct dl_bw {
265 raw_spinlock_t lock;
266 u64 bw, total_bw;
267};
268
Juri Lelli7f514122014-09-19 10:22:40 +0100269static inline
270void __dl_clear(struct dl_bw *dl_b, u64 tsk_bw)
271{
272 dl_b->total_bw -= tsk_bw;
273}
274
275static inline
276void __dl_add(struct dl_bw *dl_b, u64 tsk_bw)
277{
278 dl_b->total_bw += tsk_bw;
279}
280
281static inline
282bool __dl_overflow(struct dl_bw *dl_b, int cpus, u64 old_bw, u64 new_bw)
283{
284 return dl_b->bw != -1 &&
285 dl_b->bw * cpus < dl_b->total_bw - old_bw + new_bw;
286}
287
Peter Zijlstra029632f2011-10-25 10:00:11 +0200288extern struct mutex sched_domains_mutex;
289
290#ifdef CONFIG_CGROUP_SCHED
291
292#include <linux/cgroup.h>
293
294struct cfs_rq;
295struct rt_rq;
296
Mike Galbraith35cf4e52012-08-07 05:00:13 +0200297extern struct list_head task_groups;
Peter Zijlstra029632f2011-10-25 10:00:11 +0200298
299struct cfs_bandwidth {
300#ifdef CONFIG_CFS_BANDWIDTH
301 raw_spinlock_t lock;
302 ktime_t period;
303 u64 quota, runtime;
Zhihui Zhang9c58c792014-09-20 21:24:36 -0400304 s64 hierarchical_quota;
Peter Zijlstra029632f2011-10-25 10:00:11 +0200305 u64 runtime_expires;
306
Peter Zijlstra4cfafd32015-05-14 12:23:11 +0200307 int idle, period_active;
Peter Zijlstra029632f2011-10-25 10:00:11 +0200308 struct hrtimer period_timer, slack_timer;
309 struct list_head throttled_cfs_rq;
310
311 /* statistics */
312 int nr_periods, nr_throttled;
313 u64 throttled_time;
314#endif
315};
316
317/* task group related information */
318struct task_group {
319 struct cgroup_subsys_state css;
320
321#ifdef CONFIG_FAIR_GROUP_SCHED
322 /* schedulable entities of this group on each cpu */
323 struct sched_entity **se;
324 /* runqueue "owned" by this group on each cpu */
325 struct cfs_rq **cfs_rq;
326 unsigned long shares;
327
Alex Shifa6bdde2013-06-20 10:18:46 +0800328#ifdef CONFIG_SMP
Waiman Longb0367622015-12-02 13:41:49 -0500329 /*
330 * load_avg can be heavily contended at clock tick time, so put
331 * it in its own cacheline separated from the fields above which
332 * will also be accessed at each tick.
333 */
334 atomic_long_t load_avg ____cacheline_aligned;
Peter Zijlstra029632f2011-10-25 10:00:11 +0200335#endif
Alex Shifa6bdde2013-06-20 10:18:46 +0800336#endif
Peter Zijlstra029632f2011-10-25 10:00:11 +0200337
338#ifdef CONFIG_RT_GROUP_SCHED
339 struct sched_rt_entity **rt_se;
340 struct rt_rq **rt_rq;
341
342 struct rt_bandwidth rt_bandwidth;
343#endif
344
345 struct rcu_head rcu;
346 struct list_head list;
347
348 struct task_group *parent;
349 struct list_head siblings;
350 struct list_head children;
351
352#ifdef CONFIG_SCHED_AUTOGROUP
353 struct autogroup *autogroup;
354#endif
355
356 struct cfs_bandwidth cfs_bandwidth;
357};
358
359#ifdef CONFIG_FAIR_GROUP_SCHED
360#define ROOT_TASK_GROUP_LOAD NICE_0_LOAD
361
362/*
363 * A weight of 0 or 1 can cause arithmetics problems.
364 * A weight of a cfs_rq is the sum of weights of which entities
365 * are queued on this cfs_rq, so a weight of a entity should not be
366 * too large, so as the shares value of a task group.
367 * (The default weight is 1024 - so there's no practical
368 * limitation from this.)
369 */
370#define MIN_SHARES (1UL << 1)
371#define MAX_SHARES (1UL << 18)
372#endif
373
Peter Zijlstra029632f2011-10-25 10:00:11 +0200374typedef int (*tg_visitor)(struct task_group *, void *);
375
376extern int walk_tg_tree_from(struct task_group *from,
377 tg_visitor down, tg_visitor up, void *data);
378
379/*
380 * Iterate the full tree, calling @down when first entering a node and @up when
381 * leaving it for the final time.
382 *
383 * Caller must hold rcu_lock or sufficient equivalent.
384 */
385static inline int walk_tg_tree(tg_visitor down, tg_visitor up, void *data)
386{
387 return walk_tg_tree_from(&root_task_group, down, up, data);
388}
389
390extern int tg_nop(struct task_group *tg, void *data);
391
392extern void free_fair_sched_group(struct task_group *tg);
393extern int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent);
Peter Zijlstra8663e242016-06-22 14:58:02 +0200394extern void online_fair_sched_group(struct task_group *tg);
Peter Zijlstra6fe1f342016-01-21 22:24:16 +0100395extern void unregister_fair_sched_group(struct task_group *tg);
Peter Zijlstra029632f2011-10-25 10:00:11 +0200396extern void init_tg_cfs_entry(struct task_group *tg, struct cfs_rq *cfs_rq,
397 struct sched_entity *se, int cpu,
398 struct sched_entity *parent);
399extern void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b);
Peter Zijlstra029632f2011-10-25 10:00:11 +0200400
401extern void __refill_cfs_bandwidth_runtime(struct cfs_bandwidth *cfs_b);
Peter Zijlstra77a4d1a2015-04-15 11:41:57 +0200402extern void start_cfs_bandwidth(struct cfs_bandwidth *cfs_b);
Peter Zijlstra029632f2011-10-25 10:00:11 +0200403extern void unthrottle_cfs_rq(struct cfs_rq *cfs_rq);
404
405extern void free_rt_sched_group(struct task_group *tg);
406extern int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent);
407extern void init_tg_rt_entry(struct task_group *tg, struct rt_rq *rt_rq,
408 struct sched_rt_entity *rt_se, int cpu,
409 struct sched_rt_entity *parent);
410
Li Zefan25cc7da2013-03-05 16:07:33 +0800411extern struct task_group *sched_create_group(struct task_group *parent);
412extern void sched_online_group(struct task_group *tg,
413 struct task_group *parent);
414extern void sched_destroy_group(struct task_group *tg);
415extern void sched_offline_group(struct task_group *tg);
416
417extern void sched_move_task(struct task_struct *tsk);
418
419#ifdef CONFIG_FAIR_GROUP_SCHED
420extern int sched_group_set_shares(struct task_group *tg, unsigned long shares);
Byungchul Parkad936d82015-10-24 01:16:19 +0900421
422#ifdef CONFIG_SMP
423extern void set_task_rq_fair(struct sched_entity *se,
424 struct cfs_rq *prev, struct cfs_rq *next);
425#else /* !CONFIG_SMP */
426static inline void set_task_rq_fair(struct sched_entity *se,
427 struct cfs_rq *prev, struct cfs_rq *next) { }
428#endif /* CONFIG_SMP */
429#endif /* CONFIG_FAIR_GROUP_SCHED */
Li Zefan25cc7da2013-03-05 16:07:33 +0800430
Syed Rameez Mustafadddcab72016-09-07 16:18:27 -0700431extern struct task_group *css_tg(struct cgroup_subsys_state *css);
432
Peter Zijlstra029632f2011-10-25 10:00:11 +0200433#else /* CONFIG_CGROUP_SCHED */
434
435struct cfs_bandwidth { };
436
437#endif /* CONFIG_CGROUP_SCHED */
438
439/* CFS-related fields in a runqueue */
440struct cfs_rq {
441 struct load_weight load;
Peter Zijlstrac82513e2012-04-26 13:12:27 +0200442 unsigned int nr_running, h_nr_running;
Peter Zijlstra029632f2011-10-25 10:00:11 +0200443
444 u64 exec_clock;
445 u64 min_vruntime;
446#ifndef CONFIG_64BIT
447 u64 min_vruntime_copy;
448#endif
449
450 struct rb_root tasks_timeline;
451 struct rb_node *rb_leftmost;
452
Peter Zijlstra029632f2011-10-25 10:00:11 +0200453 /*
454 * 'curr' points to currently running entity on this cfs_rq.
455 * It is set to NULL otherwise (i.e when none are currently running).
456 */
457 struct sched_entity *curr, *next, *last, *skip;
458
459#ifdef CONFIG_SCHED_DEBUG
460 unsigned int nr_spread_over;
461#endif
462
Paul Turner2dac7542012-10-04 13:18:30 +0200463#ifdef CONFIG_SMP
464 /*
Yuyang Du9d89c252015-07-15 08:04:37 +0800465 * CFS load tracking
Paul Turner2dac7542012-10-04 13:18:30 +0200466 */
Yuyang Du9d89c252015-07-15 08:04:37 +0800467 struct sched_avg avg;
Yuyang Du13962232015-07-15 08:04:41 +0800468 u64 runnable_load_sum;
469 unsigned long runnable_load_avg;
Yuyang Du9d89c252015-07-15 08:04:37 +0800470#ifdef CONFIG_FAIR_GROUP_SCHED
471 unsigned long tg_load_avg_contrib;
472#endif
473 atomic_long_t removed_load_avg, removed_util_avg;
474#ifndef CONFIG_64BIT
475 u64 load_last_update_time_copy;
476#endif
Alex Shi141965c2013-06-26 13:05:39 +0800477
Paul Turnerc566e8e2012-10-04 13:18:30 +0200478#ifdef CONFIG_FAIR_GROUP_SCHED
Paul Turner82958362012-10-04 13:18:31 +0200479 /*
480 * h_load = weight * f(tg)
481 *
482 * Where f(tg) is the recursive weight fraction assigned to
483 * this group.
484 */
485 unsigned long h_load;
Vladimir Davydov68520792013-07-15 17:49:19 +0400486 u64 last_h_load_update;
487 struct sched_entity *h_load_next;
488#endif /* CONFIG_FAIR_GROUP_SCHED */
Paul Turner82958362012-10-04 13:18:31 +0200489#endif /* CONFIG_SMP */
490
Peter Zijlstra029632f2011-10-25 10:00:11 +0200491#ifdef CONFIG_FAIR_GROUP_SCHED
492 struct rq *rq; /* cpu runqueue to which this cfs_rq is attached */
493
494 /*
495 * leaf cfs_rqs are those that hold tasks (lowest schedulable entity in
496 * a hierarchy). Non-leaf lrqs hold other higher schedulable entities
497 * (like users, containers etc.)
498 *
499 * leaf_cfs_rq_list ties together list of leaf cfs_rq's in a cpu. This
500 * list is used during load balance.
501 */
502 int on_list;
503 struct list_head leaf_cfs_rq_list;
504 struct task_group *tg; /* group that "owns" this runqueue */
505
Peter Zijlstra029632f2011-10-25 10:00:11 +0200506#ifdef CONFIG_CFS_BANDWIDTH
507 int runtime_enabled;
508 u64 runtime_expires;
509 s64 runtime_remaining;
510
Paul Turnerf1b17282012-10-04 13:18:31 +0200511 u64 throttled_clock, throttled_clock_task;
512 u64 throttled_clock_task_time;
Peter Zijlstra55e16d32016-06-22 15:14:26 +0200513 int throttled, throttle_count;
Peter Zijlstra029632f2011-10-25 10:00:11 +0200514 struct list_head throttled_list;
515#endif /* CONFIG_CFS_BANDWIDTH */
516#endif /* CONFIG_FAIR_GROUP_SCHED */
517};
518
519static inline int rt_bandwidth_enabled(void)
520{
521 return sysctl_sched_rt_runtime >= 0;
522}
523
Steven Rostedtb6366f02015-03-18 14:49:46 -0400524/* RT IPI pull logic requires IRQ_WORK */
525#ifdef CONFIG_IRQ_WORK
526# define HAVE_RT_PUSH_IPI
527#endif
528
Peter Zijlstra029632f2011-10-25 10:00:11 +0200529/* Real-Time classes' related field in a runqueue: */
530struct rt_rq {
531 struct rt_prio_array active;
Peter Zijlstrac82513e2012-04-26 13:12:27 +0200532 unsigned int rt_nr_running;
Frederic Weisbecker01d36d02015-11-04 18:17:10 +0100533 unsigned int rr_nr_running;
Peter Zijlstra029632f2011-10-25 10:00:11 +0200534#if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
535 struct {
536 int curr; /* highest queued rt task prio */
537#ifdef CONFIG_SMP
538 int next; /* next highest */
539#endif
540 } highest_prio;
541#endif
542#ifdef CONFIG_SMP
543 unsigned long rt_nr_migratory;
544 unsigned long rt_nr_total;
545 int overloaded;
546 struct plist_head pushable_tasks;
Steven Rostedtb6366f02015-03-18 14:49:46 -0400547#ifdef HAVE_RT_PUSH_IPI
548 int push_flags;
549 int push_cpu;
550 struct irq_work push_work;
551 raw_spinlock_t push_lock;
Peter Zijlstra029632f2011-10-25 10:00:11 +0200552#endif
Steven Rostedtb6366f02015-03-18 14:49:46 -0400553#endif /* CONFIG_SMP */
Kirill Tkhaif4ebcbc2014-03-15 02:15:00 +0400554 int rt_queued;
555
Peter Zijlstra029632f2011-10-25 10:00:11 +0200556 int rt_throttled;
557 u64 rt_time;
558 u64 rt_runtime;
559 /* Nests inside the rq lock: */
560 raw_spinlock_t rt_runtime_lock;
561
562#ifdef CONFIG_RT_GROUP_SCHED
563 unsigned long rt_nr_boosted;
564
565 struct rq *rq;
Peter Zijlstra029632f2011-10-25 10:00:11 +0200566 struct task_group *tg;
567#endif
568};
569
Dario Faggioliaab03e02013-11-28 11:14:43 +0100570/* Deadline class' related fields in a runqueue */
571struct dl_rq {
572 /* runqueue is an rbtree, ordered by deadline */
573 struct rb_root rb_root;
574 struct rb_node *rb_leftmost;
575
576 unsigned long dl_nr_running;
Juri Lelli1baca4c2013-11-07 14:43:38 +0100577
578#ifdef CONFIG_SMP
579 /*
580 * Deadline values of the currently executing and the
581 * earliest ready task on this rq. Caching these facilitates
582 * the decision wether or not a ready but not running task
583 * should migrate somewhere else.
584 */
585 struct {
586 u64 curr;
587 u64 next;
588 } earliest_dl;
589
590 unsigned long dl_nr_migratory;
Juri Lelli1baca4c2013-11-07 14:43:38 +0100591 int overloaded;
592
593 /*
594 * Tasks on this rq that can be pushed away. They are kept in
595 * an rb-tree, ordered by tasks' deadlines, with caching
596 * of the leftmost (earliest deadline) element.
597 */
598 struct rb_root pushable_dl_tasks_root;
599 struct rb_node *pushable_dl_tasks_leftmost;
Dario Faggioli332ac172013-11-07 14:43:45 +0100600#else
601 struct dl_bw dl_bw;
Juri Lelli1baca4c2013-11-07 14:43:38 +0100602#endif
Dario Faggioliaab03e02013-11-28 11:14:43 +0100603};
604
Peter Zijlstra029632f2011-10-25 10:00:11 +0200605#ifdef CONFIG_SMP
606
Dietmar Eggemannbbb138b2015-09-26 18:19:54 +0100607struct max_cpu_capacity {
608 raw_spinlock_t lock;
609 unsigned long val;
610 int cpu;
611};
612
Peter Zijlstra029632f2011-10-25 10:00:11 +0200613/*
614 * We add the notion of a root-domain which will be used to define per-domain
615 * variables. Each exclusive cpuset essentially defines an island domain by
616 * fully partitioning the member cpus from any other cpuset. Whenever a new
617 * exclusive cpuset is created, we also create and attach a new root-domain
618 * object.
619 *
620 */
621struct root_domain {
622 atomic_t refcount;
623 atomic_t rto_count;
624 struct rcu_head rcu;
625 cpumask_var_t span;
626 cpumask_var_t online;
627
Tim Chen4486edd2014-06-23 12:16:49 -0700628 /* Indicate more than one runnable task for any CPU */
629 bool overload;
630
Morten Rasmussena562dfc2015-05-09 16:49:57 +0100631 /* Indicate one or more cpus over-utilized (tipping point) */
632 bool overutilized;
633
Peter Zijlstra029632f2011-10-25 10:00:11 +0200634 /*
Juri Lelli1baca4c2013-11-07 14:43:38 +0100635 * The bit corresponding to a CPU gets set here if such CPU has more
636 * than one runnable -deadline task (as it is below for RT tasks).
637 */
638 cpumask_var_t dlo_mask;
639 atomic_t dlo_count;
Dario Faggioli332ac172013-11-07 14:43:45 +0100640 struct dl_bw dl_bw;
Juri Lelli6bfd6d72013-11-07 14:43:47 +0100641 struct cpudl cpudl;
Juri Lelli1baca4c2013-11-07 14:43:38 +0100642
643 /*
Peter Zijlstra029632f2011-10-25 10:00:11 +0200644 * The "RT overload" flag: it gets set if a CPU has more than
645 * one runnable RT task.
646 */
647 cpumask_var_t rto_mask;
648 struct cpupri cpupri;
Dietmar Eggemanncd92bfd2016-08-01 19:53:35 +0100649
Dietmar Eggemannbbb138b2015-09-26 18:19:54 +0100650 /* Maximum cpu capacity in the system. */
651 struct max_cpu_capacity max_cpu_capacity;
Peter Zijlstra029632f2011-10-25 10:00:11 +0200652};
653
654extern struct root_domain def_root_domain;
655
656#endif /* CONFIG_SMP */
657
658/*
659 * This is the main, per-CPU runqueue data structure.
660 *
661 * Locking rule: those places that want to lock multiple runqueues
662 * (such as the load balancing or the thread migration code), lock
663 * acquire operations must be ordered by ascending &runqueue.
664 */
665struct rq {
666 /* runqueue lock: */
667 raw_spinlock_t lock;
668
669 /*
670 * nr_running and cpu_load should be in the same cacheline because
671 * remote CPUs use both these fields when doing load calculation.
672 */
Peter Zijlstrac82513e2012-04-26 13:12:27 +0200673 unsigned int nr_running;
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +0100674#ifdef CONFIG_NUMA_BALANCING
675 unsigned int nr_numa_running;
676 unsigned int nr_preferred_running;
677#endif
Peter Zijlstra029632f2011-10-25 10:00:11 +0200678 #define CPU_LOAD_IDX_MAX 5
679 unsigned long cpu_load[CPU_LOAD_IDX_MAX];
Morten Rasmussen4c6a8242016-02-25 12:47:54 +0000680 unsigned int misfit_task;
Frederic Weisbecker3451d022011-08-10 23:21:01 +0200681#ifdef CONFIG_NO_HZ_COMMON
Frederic Weisbecker9fd81dd2016-04-19 17:36:51 +0200682#ifdef CONFIG_SMP
683 unsigned long last_load_update_tick;
684#endif /* CONFIG_SMP */
Suresh Siddha1c792db2011-12-01 17:07:32 -0800685 unsigned long nohz_flags;
Frederic Weisbecker9fd81dd2016-04-19 17:36:51 +0200686#endif /* CONFIG_NO_HZ_COMMON */
Frederic Weisbecker265f22a2013-05-03 03:39:05 +0200687#ifdef CONFIG_NO_HZ_FULL
688 unsigned long last_sched_tick;
689#endif
Joseph Lo77501862013-04-22 14:39:18 +0800690
691#ifdef CONFIG_CPU_QUIET
692 /* time-based average load */
693 u64 nr_last_stamp;
694 u64 nr_running_integral;
695 seqcount_t ave_seqcnt;
696#endif
697
Peter Zijlstra029632f2011-10-25 10:00:11 +0200698 /* capture load from *all* tasks on this cpu: */
699 struct load_weight load;
700 unsigned long nr_load_updates;
701 u64 nr_switches;
702
703 struct cfs_rq cfs;
704 struct rt_rq rt;
Dario Faggioliaab03e02013-11-28 11:14:43 +0100705 struct dl_rq dl;
Peter Zijlstra029632f2011-10-25 10:00:11 +0200706
707#ifdef CONFIG_FAIR_GROUP_SCHED
708 /* list of leaf cfs_rq on this cpu: */
709 struct list_head leaf_cfs_rq_list;
Peter Zijlstraa35b6462012-08-08 21:46:40 +0200710#endif /* CONFIG_FAIR_GROUP_SCHED */
711
Peter Zijlstra029632f2011-10-25 10:00:11 +0200712 /*
713 * This is part of a global counter where only the total sum
714 * over all CPUs matters. A task can increase this counter on
715 * one CPU and if it got migrated afterwards it may decrease
716 * it on another CPU. Always updated under the runqueue lock:
717 */
718 unsigned long nr_uninterruptible;
719
720 struct task_struct *curr, *idle, *stop;
721 unsigned long next_balance;
722 struct mm_struct *prev_mm;
723
Peter Zijlstra9edfbfe2015-01-05 11:18:11 +0100724 unsigned int clock_skip_update;
Peter Zijlstra029632f2011-10-25 10:00:11 +0200725 u64 clock;
726 u64 clock_task;
727
728 atomic_t nr_iowait;
729
730#ifdef CONFIG_SMP
731 struct root_domain *rd;
732 struct sched_domain *sd;
733
Nicolas Pitreced549f2014-05-26 18:19:38 -0400734 unsigned long cpu_capacity;
Vincent Guittotca6d75e2015-02-27 16:54:09 +0100735 unsigned long cpu_capacity_orig;
Peter Zijlstra029632f2011-10-25 10:00:11 +0200736
Peter Zijlstrae3fca9e2015-06-11 14:46:37 +0200737 struct callback_head *balance_callback;
738
Peter Zijlstra029632f2011-10-25 10:00:11 +0200739 unsigned char idle_balance;
740 /* For active balancing */
Peter Zijlstra029632f2011-10-25 10:00:11 +0200741 int active_balance;
742 int push_cpu;
Syed Rameez Mustafadddcab72016-09-07 16:18:27 -0700743 struct task_struct *push_task;
Peter Zijlstra029632f2011-10-25 10:00:11 +0200744 struct cpu_stop_work active_balance_work;
745 /* cpu of this runqueue: */
746 int cpu;
747 int online;
748
Peter Zijlstra367456c2012-02-20 21:49:09 +0100749 struct list_head cfs_tasks;
750
Peter Zijlstra029632f2011-10-25 10:00:11 +0200751 u64 rt_avg;
752 u64 age_stamp;
753 u64 idle_stamp;
754 u64 avg_idle;
Jason Low9bd721c2013-09-13 11:26:52 -0700755
756 /* This is used to determine avg_idle's max value */
757 u64 max_idle_balance_cost;
Peter Zijlstra029632f2011-10-25 10:00:11 +0200758#endif
759
Joonwoo Parkf7d6cd42017-01-17 15:19:43 -0800760#ifdef CONFIG_SCHED_WALT
Syed Rameez Mustafadddcab72016-09-07 16:18:27 -0700761 struct sched_cluster *cluster;
762 struct cpumask freq_domain_cpumask;
Pavankumar Kondeti84f72d72017-07-20 11:00:45 +0530763 struct walt_sched_stats walt_stats;
Syed Rameez Mustafadddcab72016-09-07 16:18:27 -0700764
765 int cstate, wakeup_latency, wakeup_energy;
766 u64 window_start;
Joonwoo Park84a80882017-02-03 11:15:31 -0800767 s64 cum_window_start;
Vikram Mulukutla4b54aae2017-03-20 13:41:37 -0700768 u64 load_reported_window;
Pavankumar Kondeti84f72d72017-07-20 11:00:45 +0530769 unsigned long walt_flags;
Syed Rameez Mustafadddcab72016-09-07 16:18:27 -0700770
771 u64 cur_irqload;
772 u64 avg_irqload;
773 u64 irqload_ts;
774 unsigned int static_cpu_pwr_cost;
775 struct task_struct *ed_task;
776 struct cpu_cycle cc;
777 u64 old_busy_time, old_busy_time_group;
778 u64 old_estimated_time;
779 u64 curr_runnable_sum;
780 u64 prev_runnable_sum;
781 u64 nt_curr_runnable_sum;
782 u64 nt_prev_runnable_sum;
Joonwoo Park84a80882017-02-03 11:15:31 -0800783 u64 cum_window_demand;
Pavankumar Kondeti6deb2c42017-01-09 13:56:33 +0530784 struct group_cpu_time grp_time;
Syed Rameez Mustafa59b5fb72016-05-31 16:40:45 -0700785 struct load_subtractions load_subs[NUM_TRACKED_WINDOWS];
Syed Rameez Mustafa7ac74fc2016-06-07 15:18:37 -0700786 DECLARE_BITMAP_ARRAY(top_tasks_bitmap,
787 NUM_TRACKED_WINDOWS, NUM_LOAD_INDICES);
Syed Rameez Mustafa59b5fb72016-05-31 16:40:45 -0700788 u8 *top_tasks[NUM_TRACKED_WINDOWS];
789 u8 curr_table;
790 int prev_top;
791 int curr_top;
Syed Rameez Mustafadddcab72016-09-07 16:18:27 -0700792#endif
793
Peter Zijlstra029632f2011-10-25 10:00:11 +0200794#ifdef CONFIG_IRQ_TIME_ACCOUNTING
795 u64 prev_irq_time;
796#endif
797#ifdef CONFIG_PARAVIRT
798 u64 prev_steal_time;
799#endif
800#ifdef CONFIG_PARAVIRT_TIME_ACCOUNTING
801 u64 prev_steal_time_rq;
802#endif
803
804 /* calc_load related fields */
805 unsigned long calc_load_update;
806 long calc_load_active;
807
808#ifdef CONFIG_SCHED_HRTICK
809#ifdef CONFIG_SMP
810 int hrtick_csd_pending;
811 struct call_single_data hrtick_csd;
812#endif
813 struct hrtimer hrtick_timer;
814#endif
815
816#ifdef CONFIG_SCHEDSTATS
817 /* latency stats */
818 struct sched_info rq_sched_info;
819 unsigned long long rq_cpu_time;
820 /* could above be rq->cfs_rq.exec_clock + rq->rt_rq.rt_runtime ? */
821
822 /* sys_sched_yield() stats */
823 unsigned int yld_count;
824
825 /* schedule() stats */
Peter Zijlstra029632f2011-10-25 10:00:11 +0200826 unsigned int sched_count;
827 unsigned int sched_goidle;
828
829 /* try_to_wake_up() stats */
830 unsigned int ttwu_count;
831 unsigned int ttwu_local;
832#endif
833
834#ifdef CONFIG_SMP
835 struct llist_head wake_list;
836#endif
Daniel Lezcano442bf3a2014-09-04 11:32:09 -0400837
838#ifdef CONFIG_CPU_IDLE
839 /* Must be inspected within a rcu lock section */
840 struct cpuidle_state *idle_state;
Morten Rasmussen06910642015-01-27 13:48:07 +0000841 int idle_state_idx;
Daniel Lezcano442bf3a2014-09-04 11:32:09 -0400842#endif
Peter Zijlstra029632f2011-10-25 10:00:11 +0200843};
844
845static inline int cpu_of(struct rq *rq)
846{
847#ifdef CONFIG_SMP
848 return rq->cpu;
849#else
850 return 0;
851#endif
852}
853
Peter Zijlstra1b568f02016-05-09 10:38:41 +0200854
855#ifdef CONFIG_SCHED_SMT
856
857extern struct static_key_false sched_smt_present;
858
859extern void __update_idle_core(struct rq *rq);
860
861static inline void update_idle_core(struct rq *rq)
862{
863 if (static_branch_unlikely(&sched_smt_present))
864 __update_idle_core(rq);
865}
866
867#else
868static inline void update_idle_core(struct rq *rq) { }
869#endif
870
Pranith Kumar8b06c552014-08-13 13:28:12 -0400871DECLARE_PER_CPU_SHARED_ALIGNED(struct rq, runqueues);
Peter Zijlstra029632f2011-10-25 10:00:11 +0200872
Peter Zijlstra518cd622011-12-07 15:07:31 +0100873#define cpu_rq(cpu) (&per_cpu(runqueues, (cpu)))
Christoph Lameter4a32fea2014-08-17 12:30:27 -0500874#define this_rq() this_cpu_ptr(&runqueues)
Peter Zijlstra518cd622011-12-07 15:07:31 +0100875#define task_rq(p) cpu_rq(task_cpu(p))
876#define cpu_curr(cpu) (cpu_rq(cpu)->curr)
Christoph Lameter4a32fea2014-08-17 12:30:27 -0500877#define raw_rq() raw_cpu_ptr(&runqueues)
Peter Zijlstra518cd622011-12-07 15:07:31 +0100878
Peter Zijlstracebde6d2015-01-05 11:18:10 +0100879static inline u64 __rq_clock_broken(struct rq *rq)
880{
Jason Low316c1608d2015-04-28 13:00:20 -0700881 return READ_ONCE(rq->clock);
Peter Zijlstracebde6d2015-01-05 11:18:10 +0100882}
883
Frederic Weisbecker78becc22013-04-12 01:51:02 +0200884static inline u64 rq_clock(struct rq *rq)
885{
Peter Zijlstracebde6d2015-01-05 11:18:10 +0100886 lockdep_assert_held(&rq->lock);
Frederic Weisbecker78becc22013-04-12 01:51:02 +0200887 return rq->clock;
888}
889
890static inline u64 rq_clock_task(struct rq *rq)
891{
Peter Zijlstracebde6d2015-01-05 11:18:10 +0100892 lockdep_assert_held(&rq->lock);
Frederic Weisbecker78becc22013-04-12 01:51:02 +0200893 return rq->clock_task;
894}
895
Peter Zijlstra9edfbfe2015-01-05 11:18:11 +0100896#define RQCF_REQ_SKIP 0x01
897#define RQCF_ACT_SKIP 0x02
898
899static inline void rq_clock_skip_update(struct rq *rq, bool skip)
900{
901 lockdep_assert_held(&rq->lock);
902 if (skip)
903 rq->clock_skip_update |= RQCF_REQ_SKIP;
904 else
905 rq->clock_skip_update &= ~RQCF_REQ_SKIP;
906}
907
Rik van Riel9942f792014-10-17 03:29:49 -0400908#ifdef CONFIG_NUMA
Rik van Riele3fe70b2014-10-17 03:29:50 -0400909enum numa_topology_type {
910 NUMA_DIRECT,
911 NUMA_GLUELESS_MESH,
912 NUMA_BACKPLANE,
913};
914extern enum numa_topology_type sched_numa_topology_type;
Rik van Riel9942f792014-10-17 03:29:49 -0400915extern int sched_max_numa_distance;
916extern bool find_numa_distance(int distance);
917#endif
918
Mel Gormanf809ca92013-10-07 11:28:57 +0100919#ifdef CONFIG_NUMA_BALANCING
Iulia Manda44dba3d2014-10-31 02:13:31 +0200920/* The regions in numa_faults array from task_struct */
921enum numa_faults_stats {
922 NUMA_MEM = 0,
923 NUMA_CPU,
924 NUMA_MEMBUF,
925 NUMA_CPUBUF
926};
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +0100927extern void sched_setnuma(struct task_struct *p, int node);
Mel Gormane6628d52013-10-07 11:29:02 +0100928extern int migrate_task_to(struct task_struct *p, int cpu);
Peter Zijlstraac66f542013-10-07 11:29:16 +0100929extern int migrate_swap(struct task_struct *, struct task_struct *);
Mel Gormanf809ca92013-10-07 11:28:57 +0100930#endif /* CONFIG_NUMA_BALANCING */
931
Peter Zijlstra518cd622011-12-07 15:07:31 +0100932#ifdef CONFIG_SMP
933
Peter Zijlstrae3fca9e2015-06-11 14:46:37 +0200934static inline void
935queue_balance_callback(struct rq *rq,
936 struct callback_head *head,
937 void (*func)(struct rq *rq))
938{
939 lockdep_assert_held(&rq->lock);
940
941 if (unlikely(head->next))
942 return;
943
944 head->func = (void (*)(struct callback_head *))func;
945 head->next = rq->balance_callback;
946 rq->balance_callback = head;
947}
948
Peter Zijlstrae3baac42014-06-04 10:31:18 -0700949extern void sched_ttwu_pending(void);
950
Peter Zijlstra029632f2011-10-25 10:00:11 +0200951#define rcu_dereference_check_sched_domain(p) \
952 rcu_dereference_check((p), \
953 lockdep_is_held(&sched_domains_mutex))
954
955/*
956 * The domain tree (rq->sd) is protected by RCU's quiescent state transition.
957 * See detach_destroy_domains: synchronize_sched for details.
958 *
959 * The domain tree of any CPU may only be accessed from within
960 * preempt-disabled sections.
961 */
962#define for_each_domain(cpu, __sd) \
Peter Zijlstra518cd622011-12-07 15:07:31 +0100963 for (__sd = rcu_dereference_check_sched_domain(cpu_rq(cpu)->sd); \
964 __sd; __sd = __sd->parent)
Peter Zijlstra029632f2011-10-25 10:00:11 +0200965
Suresh Siddha77e81362011-11-17 11:08:23 -0800966#define for_each_lower_domain(sd) for (; sd; sd = sd->child)
967
Peter Zijlstra518cd622011-12-07 15:07:31 +0100968/**
969 * highest_flag_domain - Return highest sched_domain containing flag.
970 * @cpu: The cpu whose highest level of sched domain is to
971 * be returned.
972 * @flag: The flag to check for the highest sched_domain
973 * for the given cpu.
974 *
975 * Returns the highest sched_domain of a cpu which contains the given flag.
976 */
977static inline struct sched_domain *highest_flag_domain(int cpu, int flag)
978{
979 struct sched_domain *sd, *hsd = NULL;
980
981 for_each_domain(cpu, sd) {
982 if (!(sd->flags & flag))
983 break;
984 hsd = sd;
985 }
986
987 return hsd;
988}
989
Mel Gormanfb13c7e2013-10-07 11:29:17 +0100990static inline struct sched_domain *lowest_flag_domain(int cpu, int flag)
991{
992 struct sched_domain *sd;
993
994 for_each_domain(cpu, sd) {
995 if (sd->flags & flag)
996 break;
997 }
998
999 return sd;
1000}
1001
Peter Zijlstra518cd622011-12-07 15:07:31 +01001002DECLARE_PER_CPU(struct sched_domain *, sd_llc);
Peter Zijlstra7d9ffa82013-07-04 12:56:46 +08001003DECLARE_PER_CPU(int, sd_llc_size);
Peter Zijlstra518cd622011-12-07 15:07:31 +01001004DECLARE_PER_CPU(int, sd_llc_id);
Peter Zijlstra0e369d72016-05-09 10:38:01 +02001005DECLARE_PER_CPU(struct sched_domain_shared *, sd_llc_shared);
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001006DECLARE_PER_CPU(struct sched_domain *, sd_numa);
Preeti U Murthy37dc6b52013-10-30 08:42:52 +05301007DECLARE_PER_CPU(struct sched_domain *, sd_asym);
Morten Rasmussen30786a02015-01-02 17:08:52 +00001008DECLARE_PER_CPU(struct sched_domain *, sd_ea);
Morten Rasmussen61bf6252014-12-18 14:47:18 +00001009DECLARE_PER_CPU(struct sched_domain *, sd_scs);
Peter Zijlstra518cd622011-12-07 15:07:31 +01001010
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04001011struct sched_group_capacity {
Li Zefan5e6521e2013-03-05 16:06:23 +08001012 atomic_t ref;
1013 /*
Yuyang Du172895e2016-04-05 12:12:27 +08001014 * CPU capacity of this group, SCHED_CAPACITY_SCALE being max capacity
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04001015 * for a single CPU.
Li Zefan5e6521e2013-03-05 16:06:23 +08001016 */
Morten Rasmussen5cdeb5f2016-02-25 12:43:49 +00001017 unsigned long capacity;
1018 unsigned long max_capacity; /* Max per-cpu capacity in group */
Li Zefan5e6521e2013-03-05 16:06:23 +08001019 unsigned long next_update;
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04001020 int imbalance; /* XXX unrelated to capacity but shared group state */
Li Zefan5e6521e2013-03-05 16:06:23 +08001021
1022 unsigned long cpumask[0]; /* iteration mask */
1023};
1024
1025struct sched_group {
1026 struct sched_group *next; /* Must be a circular list */
1027 atomic_t ref;
1028
1029 unsigned int group_weight;
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04001030 struct sched_group_capacity *sgc;
Greg Hackmann2a3c6e62017-03-07 10:37:56 -08001031 const struct sched_group_energy *sge;
Li Zefan5e6521e2013-03-05 16:06:23 +08001032
1033 /*
1034 * The CPUs this group covers.
1035 *
1036 * NOTE: this field is variable length. (Allocated dynamically
1037 * by attaching extra space to the end of the structure,
1038 * depending on how many CPUs the kernel has booted up with)
1039 */
1040 unsigned long cpumask[0];
1041};
1042
1043static inline struct cpumask *sched_group_cpus(struct sched_group *sg)
1044{
1045 return to_cpumask(sg->cpumask);
1046}
1047
1048/*
1049 * cpumask masking which cpus in the group are allowed to iterate up the domain
1050 * tree.
1051 */
1052static inline struct cpumask *sched_group_mask(struct sched_group *sg)
1053{
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04001054 return to_cpumask(sg->sgc->cpumask);
Li Zefan5e6521e2013-03-05 16:06:23 +08001055}
1056
1057/**
1058 * group_first_cpu - Returns the first cpu in the cpumask of a sched_group.
1059 * @group: The group whose first cpu is to be returned.
1060 */
1061static inline unsigned int group_first_cpu(struct sched_group *group)
1062{
1063 return cpumask_first(sched_group_cpus(group));
1064}
1065
Peter Zijlstrac1174872012-05-31 14:47:33 +02001066extern int group_balance_cpu(struct sched_group *sg);
1067
Steven Rostedt (Red Hat)3866e842016-02-22 16:26:51 -05001068#if defined(CONFIG_SCHED_DEBUG) && defined(CONFIG_SYSCTL)
1069void register_sched_domain_sysctl(void);
1070void unregister_sched_domain_sysctl(void);
1071#else
1072static inline void register_sched_domain_sysctl(void)
1073{
1074}
1075static inline void unregister_sched_domain_sysctl(void)
1076{
1077}
1078#endif
1079
Peter Zijlstrae3baac42014-06-04 10:31:18 -07001080#else
1081
1082static inline void sched_ttwu_pending(void) { }
1083
Peter Zijlstra518cd622011-12-07 15:07:31 +01001084#endif /* CONFIG_SMP */
Peter Zijlstra029632f2011-10-25 10:00:11 +02001085
Peter Zijlstra391e43d2011-11-15 17:14:39 +01001086#include "stats.h"
1087#include "auto_group.h"
Peter Zijlstra029632f2011-10-25 10:00:11 +02001088
Pavankumar Kondeticbf7ea02017-01-11 15:11:23 +05301089enum sched_boost_policy {
1090 SCHED_BOOST_NONE,
1091 SCHED_BOOST_ON_BIG,
1092 SCHED_BOOST_ON_ALL,
1093};
1094
Syed Rameez Mustafadddcab72016-09-07 16:18:27 -07001095/*
1096 * Returns the rq capacity of any rq in a group. This does not play
1097 * well with groups where rq capacity can change independently.
1098 */
1099#define group_rq_capacity(group) cpu_capacity(group_first_cpu(group))
1100
Peter Zijlstra029632f2011-10-25 10:00:11 +02001101#ifdef CONFIG_CGROUP_SCHED
1102
1103/*
1104 * Return the group to which this tasks belongs.
1105 *
Tejun Heo8af01f52013-08-08 20:11:22 -04001106 * We cannot use task_css() and friends because the cgroup subsystem
1107 * changes that value before the cgroup_subsys::attach() method is called,
1108 * therefore we cannot pin it and might observe the wrong value.
Peter Zijlstra8323f262012-06-22 13:36:05 +02001109 *
1110 * The same is true for autogroup's p->signal->autogroup->tg, the autogroup
1111 * core changes this before calling sched_move_task().
1112 *
1113 * Instead we use a 'copy' which is updated from sched_move_task() while
1114 * holding both task_struct::pi_lock and rq::lock.
Peter Zijlstra029632f2011-10-25 10:00:11 +02001115 */
1116static inline struct task_group *task_group(struct task_struct *p)
1117{
Peter Zijlstra8323f262012-06-22 13:36:05 +02001118 return p->sched_task_group;
Peter Zijlstra029632f2011-10-25 10:00:11 +02001119}
1120
1121/* Change a task's cfs_rq and parent entity if it moves across CPUs/groups */
1122static inline void set_task_rq(struct task_struct *p, unsigned int cpu)
1123{
1124#if defined(CONFIG_FAIR_GROUP_SCHED) || defined(CONFIG_RT_GROUP_SCHED)
1125 struct task_group *tg = task_group(p);
1126#endif
1127
1128#ifdef CONFIG_FAIR_GROUP_SCHED
Byungchul Parkad936d82015-10-24 01:16:19 +09001129 set_task_rq_fair(&p->se, p->se.cfs_rq, tg->cfs_rq[cpu]);
Peter Zijlstra029632f2011-10-25 10:00:11 +02001130 p->se.cfs_rq = tg->cfs_rq[cpu];
1131 p->se.parent = tg->se[cpu];
1132#endif
1133
1134#ifdef CONFIG_RT_GROUP_SCHED
1135 p->rt.rt_rq = tg->rt_rq[cpu];
1136 p->rt.parent = tg->rt_se[cpu];
1137#endif
1138}
1139
1140#else /* CONFIG_CGROUP_SCHED */
1141
1142static inline void set_task_rq(struct task_struct *p, unsigned int cpu) { }
1143static inline struct task_group *task_group(struct task_struct *p)
1144{
1145 return NULL;
1146}
1147
1148#endif /* CONFIG_CGROUP_SCHED */
1149
1150static inline void __set_task_cpu(struct task_struct *p, unsigned int cpu)
1151{
1152 set_task_rq(p, cpu);
1153#ifdef CONFIG_SMP
1154 /*
1155 * After ->cpu is set up to a new value, task_rq_lock(p, ...) can be
1156 * successfuly executed on another CPU. We must ensure that updates of
1157 * per-task data have been completed by this moment.
1158 */
1159 smp_wmb();
Andy Lutomirskic65eacb2016-09-13 14:29:24 -07001160#ifdef CONFIG_THREAD_INFO_IN_TASK
1161 p->cpu = cpu;
1162#else
Peter Zijlstra029632f2011-10-25 10:00:11 +02001163 task_thread_info(p)->cpu = cpu;
Andy Lutomirskic65eacb2016-09-13 14:29:24 -07001164#endif
Peter Zijlstraac66f542013-10-07 11:29:16 +01001165 p->wake_cpu = cpu;
Peter Zijlstra029632f2011-10-25 10:00:11 +02001166#endif
1167}
1168
1169/*
1170 * Tunables that become constants when CONFIG_SCHED_DEBUG is off:
1171 */
1172#ifdef CONFIG_SCHED_DEBUG
Ingo Molnarc5905af2012-02-24 08:31:31 +01001173# include <linux/static_key.h>
Peter Zijlstra029632f2011-10-25 10:00:11 +02001174# define const_debug __read_mostly
1175#else
1176# define const_debug const
1177#endif
1178
1179extern const_debug unsigned int sysctl_sched_features;
1180
1181#define SCHED_FEAT(name, enabled) \
1182 __SCHED_FEAT_##name ,
1183
1184enum {
Peter Zijlstra391e43d2011-11-15 17:14:39 +01001185#include "features.h"
Peter Zijlstraf8b6d1c2011-07-06 14:20:14 +02001186 __SCHED_FEAT_NR,
Peter Zijlstra029632f2011-10-25 10:00:11 +02001187};
1188
1189#undef SCHED_FEAT
1190
Peter Zijlstraf8b6d1c2011-07-06 14:20:14 +02001191#if defined(CONFIG_SCHED_DEBUG) && defined(HAVE_JUMP_LABEL)
Peter Zijlstraf8b6d1c2011-07-06 14:20:14 +02001192#define SCHED_FEAT(name, enabled) \
Ingo Molnarc5905af2012-02-24 08:31:31 +01001193static __always_inline bool static_branch_##name(struct static_key *key) \
Peter Zijlstraf8b6d1c2011-07-06 14:20:14 +02001194{ \
Jason Baron6e76ea82014-07-02 15:52:41 +00001195 return static_key_##enabled(key); \
Peter Zijlstraf8b6d1c2011-07-06 14:20:14 +02001196}
1197
1198#include "features.h"
1199
1200#undef SCHED_FEAT
1201
Ingo Molnarc5905af2012-02-24 08:31:31 +01001202extern struct static_key sched_feat_keys[__SCHED_FEAT_NR];
Peter Zijlstraf8b6d1c2011-07-06 14:20:14 +02001203#define sched_feat(x) (static_branch_##x(&sched_feat_keys[__SCHED_FEAT_##x]))
1204#else /* !(SCHED_DEBUG && HAVE_JUMP_LABEL) */
Peter Zijlstra029632f2011-10-25 10:00:11 +02001205#define sched_feat(x) (sysctl_sched_features & (1UL << __SCHED_FEAT_##x))
Peter Zijlstraf8b6d1c2011-07-06 14:20:14 +02001206#endif /* SCHED_DEBUG && HAVE_JUMP_LABEL */
Peter Zijlstra029632f2011-10-25 10:00:11 +02001207
Srikar Dronamraju2a595722015-08-11 21:54:21 +05301208extern struct static_key_false sched_numa_balancing;
Mel Gormancb251762016-02-05 09:08:36 +00001209extern struct static_key_false sched_schedstats;
Peter Zijlstracbee9f82012-10-25 14:16:43 +02001210
Peter Zijlstra029632f2011-10-25 10:00:11 +02001211static inline u64 global_rt_period(void)
1212{
1213 return (u64)sysctl_sched_rt_period * NSEC_PER_USEC;
1214}
1215
1216static inline u64 global_rt_runtime(void)
1217{
1218 if (sysctl_sched_rt_runtime < 0)
1219 return RUNTIME_INF;
1220
1221 return (u64)sysctl_sched_rt_runtime * NSEC_PER_USEC;
1222}
1223
Peter Zijlstra029632f2011-10-25 10:00:11 +02001224static inline int task_current(struct rq *rq, struct task_struct *p)
1225{
1226 return rq->curr == p;
1227}
1228
1229static inline int task_running(struct rq *rq, struct task_struct *p)
1230{
1231#ifdef CONFIG_SMP
1232 return p->on_cpu;
1233#else
1234 return task_current(rq, p);
1235#endif
1236}
1237
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04001238static inline int task_on_rq_queued(struct task_struct *p)
1239{
1240 return p->on_rq == TASK_ON_RQ_QUEUED;
1241}
Peter Zijlstra029632f2011-10-25 10:00:11 +02001242
Kirill Tkhaicca26e82014-08-20 13:47:42 +04001243static inline int task_on_rq_migrating(struct task_struct *p)
1244{
1245 return p->on_rq == TASK_ON_RQ_MIGRATING;
1246}
1247
Peter Zijlstra029632f2011-10-25 10:00:11 +02001248#ifndef prepare_arch_switch
1249# define prepare_arch_switch(next) do { } while (0)
1250#endif
Catalin Marinas01f23e12011-11-27 21:43:10 +00001251#ifndef finish_arch_post_lock_switch
1252# define finish_arch_post_lock_switch() do { } while (0)
1253#endif
Peter Zijlstra029632f2011-10-25 10:00:11 +02001254
Peter Zijlstra029632f2011-10-25 10:00:11 +02001255static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next)
1256{
1257#ifdef CONFIG_SMP
1258 /*
1259 * We can optimise this out completely for !SMP, because the
1260 * SMP rebalancing from interrupt is the only thing that cares
1261 * here.
1262 */
1263 next->on_cpu = 1;
1264#endif
1265}
1266
1267static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
1268{
1269#ifdef CONFIG_SMP
1270 /*
1271 * After ->on_cpu is cleared, the task can be moved to a different CPU.
1272 * We must ensure this doesn't happen until the switch is completely
1273 * finished.
Peter Zijlstra95913d92015-09-29 14:45:09 +02001274 *
Peter Zijlstrab75a2252015-10-06 14:36:17 +02001275 * In particular, the load of prev->state in finish_task_switch() must
1276 * happen before this.
1277 *
Peter Zijlstra1f03e8d2016-04-04 10:57:12 +02001278 * Pairs with the smp_cond_load_acquire() in try_to_wake_up().
Peter Zijlstra029632f2011-10-25 10:00:11 +02001279 */
Peter Zijlstra95913d92015-09-29 14:45:09 +02001280 smp_store_release(&prev->on_cpu, 0);
Peter Zijlstra029632f2011-10-25 10:00:11 +02001281#endif
1282#ifdef CONFIG_DEBUG_SPINLOCK
1283 /* this is a valid case when another task releases the spinlock */
1284 rq->lock.owner = current;
1285#endif
1286 /*
1287 * If we are tracking spinlock dependencies then we have to
1288 * fix up the runqueue lock - which gets 'carried over' from
1289 * prev into current:
1290 */
1291 spin_acquire(&rq->lock.dep_map, 0, 0, _THIS_IP_);
1292
1293 raw_spin_unlock_irq(&rq->lock);
1294}
1295
Li Zefanb13095f2013-03-05 16:06:38 +08001296/*
1297 * wake flags
1298 */
1299#define WF_SYNC 0x01 /* waker goes to sleep after wakeup */
1300#define WF_FORK 0x02 /* child wakeup after fork */
1301#define WF_MIGRATED 0x4 /* internal use, task got migrated */
1302
Peter Zijlstra029632f2011-10-25 10:00:11 +02001303/*
1304 * To aid in avoiding the subversion of "niceness" due to uneven distribution
1305 * of tasks with abnormal "nice" values across CPUs the contribution that
1306 * each task makes to its run queue's load is weighted according to its
1307 * scheduling class and "nice" value. For SCHED_NORMAL tasks this is just a
1308 * scaled version of the new time slice allocation that they receive on time
1309 * slice expiry etc.
1310 */
1311
1312#define WEIGHT_IDLEPRIO 3
1313#define WMULT_IDLEPRIO 1431655765
1314
Andi Kleened82b8a2015-11-29 20:59:43 -08001315extern const int sched_prio_to_weight[40];
1316extern const u32 sched_prio_to_wmult[40];
Peter Zijlstra029632f2011-10-25 10:00:11 +02001317
Peter Zijlstraff77e462016-01-18 15:27:07 +01001318/*
1319 * {de,en}queue flags:
1320 *
1321 * DEQUEUE_SLEEP - task is no longer runnable
1322 * ENQUEUE_WAKEUP - task just became runnable
1323 *
1324 * SAVE/RESTORE - an otherwise spurious dequeue/enqueue, done to ensure tasks
1325 * are in a known state which allows modification. Such pairs
1326 * should preserve as much state as possible.
1327 *
1328 * MOVE - paired with SAVE/RESTORE, explicitly does not preserve the location
1329 * in the runqueue.
1330 *
1331 * ENQUEUE_HEAD - place at front of runqueue (tail if not specified)
1332 * ENQUEUE_REPLENISH - CBS (replenish runtime and postpone deadline)
Peter Zijlstra59efa0b2016-05-10 18:24:37 +02001333 * ENQUEUE_MIGRATED - the task was migrated during wakeup
Peter Zijlstraff77e462016-01-18 15:27:07 +01001334 *
1335 */
1336
1337#define DEQUEUE_SLEEP 0x01
1338#define DEQUEUE_SAVE 0x02 /* matches ENQUEUE_RESTORE */
1339#define DEQUEUE_MOVE 0x04 /* matches ENQUEUE_MOVE */
1340
Peter Zijlstra1de64442015-09-30 17:44:13 +02001341#define ENQUEUE_WAKEUP 0x01
Peter Zijlstraff77e462016-01-18 15:27:07 +01001342#define ENQUEUE_RESTORE 0x02
1343#define ENQUEUE_MOVE 0x04
1344
1345#define ENQUEUE_HEAD 0x08
1346#define ENQUEUE_REPLENISH 0x10
Li Zefanc82ba9f2013-03-05 16:06:55 +08001347#ifdef CONFIG_SMP
Peter Zijlstra59efa0b2016-05-10 18:24:37 +02001348#define ENQUEUE_MIGRATED 0x20
Li Zefanc82ba9f2013-03-05 16:06:55 +08001349#else
Peter Zijlstra59efa0b2016-05-10 18:24:37 +02001350#define ENQUEUE_MIGRATED 0x00
Li Zefanc82ba9f2013-03-05 16:06:55 +08001351#endif
Juri Lelli43aac892015-06-26 12:14:23 +01001352#define ENQUEUE_WAKEUP_NEW 0x40
Li Zefanc82ba9f2013-03-05 16:06:55 +08001353
Peter Zijlstra37e117c2014-02-14 12:25:08 +01001354#define RETRY_TASK ((void *)-1UL)
1355
Li Zefanc82ba9f2013-03-05 16:06:55 +08001356struct sched_class {
1357 const struct sched_class *next;
1358
1359 void (*enqueue_task) (struct rq *rq, struct task_struct *p, int flags);
1360 void (*dequeue_task) (struct rq *rq, struct task_struct *p, int flags);
1361 void (*yield_task) (struct rq *rq);
1362 bool (*yield_to_task) (struct rq *rq, struct task_struct *p, bool preempt);
1363
1364 void (*check_preempt_curr) (struct rq *rq, struct task_struct *p, int flags);
1365
Peter Zijlstra606dba22012-02-11 06:05:00 +01001366 /*
1367 * It is the responsibility of the pick_next_task() method that will
1368 * return the next task to call put_prev_task() on the @prev task or
1369 * something equivalent.
Peter Zijlstra37e117c2014-02-14 12:25:08 +01001370 *
1371 * May return RETRY_TASK when it finds a higher prio class has runnable
1372 * tasks.
Peter Zijlstra606dba22012-02-11 06:05:00 +01001373 */
1374 struct task_struct * (*pick_next_task) (struct rq *rq,
Peter Zijlstrae7904a22015-08-01 19:25:08 +02001375 struct task_struct *prev,
1376 struct pin_cookie cookie);
Li Zefanc82ba9f2013-03-05 16:06:55 +08001377 void (*put_prev_task) (struct rq *rq, struct task_struct *p);
1378
1379#ifdef CONFIG_SMP
Peter Zijlstraac66f542013-10-07 11:29:16 +01001380 int (*select_task_rq)(struct task_struct *p, int task_cpu, int sd_flag, int flags);
xiaofeng.yan5a4fd032015-09-23 14:55:59 +08001381 void (*migrate_task_rq)(struct task_struct *p);
Li Zefanc82ba9f2013-03-05 16:06:55 +08001382
Li Zefanc82ba9f2013-03-05 16:06:55 +08001383 void (*task_woken) (struct rq *this_rq, struct task_struct *task);
1384
1385 void (*set_cpus_allowed)(struct task_struct *p,
1386 const struct cpumask *newmask);
1387
1388 void (*rq_online)(struct rq *rq);
1389 void (*rq_offline)(struct rq *rq);
1390#endif
1391
1392 void (*set_curr_task) (struct rq *rq);
1393 void (*task_tick) (struct rq *rq, struct task_struct *p, int queued);
1394 void (*task_fork) (struct task_struct *p);
Dario Faggiolie6c390f2013-11-07 14:43:35 +01001395 void (*task_dead) (struct task_struct *p);
Li Zefanc82ba9f2013-03-05 16:06:55 +08001396
Kirill Tkhai67dfa1b2014-10-27 17:40:52 +03001397 /*
1398 * The switched_from() call is allowed to drop rq->lock, therefore we
1399 * cannot assume the switched_from/switched_to pair is serliazed by
1400 * rq->lock. They are however serialized by p->pi_lock.
1401 */
Li Zefanc82ba9f2013-03-05 16:06:55 +08001402 void (*switched_from) (struct rq *this_rq, struct task_struct *task);
1403 void (*switched_to) (struct rq *this_rq, struct task_struct *task);
1404 void (*prio_changed) (struct rq *this_rq, struct task_struct *task,
1405 int oldprio);
1406
1407 unsigned int (*get_rr_interval) (struct rq *rq,
1408 struct task_struct *task);
1409
Stanislaw Gruszka6e998912014-11-12 16:58:44 +01001410 void (*update_curr) (struct rq *rq);
1411
Vincent Guittotea86cb42016-06-17 13:38:55 +02001412#define TASK_SET_GROUP 0
1413#define TASK_MOVE_GROUP 1
1414
Li Zefanc82ba9f2013-03-05 16:06:55 +08001415#ifdef CONFIG_FAIR_GROUP_SCHED
Vincent Guittotea86cb42016-06-17 13:38:55 +02001416 void (*task_change_group) (struct task_struct *p, int type);
Li Zefanc82ba9f2013-03-05 16:06:55 +08001417#endif
Joonwoo Parkf7d6cd42017-01-17 15:19:43 -08001418#ifdef CONFIG_SCHED_WALT
Pavankumar Kondeti84f72d72017-07-20 11:00:45 +05301419 void (*fixup_walt_sched_stats)(struct rq *rq, struct task_struct *p,
Syed Rameez Mustafadddcab72016-09-07 16:18:27 -07001420 u32 new_task_load, u32 new_pred_demand);
1421#endif
Li Zefanc82ba9f2013-03-05 16:06:55 +08001422};
Peter Zijlstra029632f2011-10-25 10:00:11 +02001423
Peter Zijlstra3f1d2a32014-02-12 10:49:30 +01001424static inline void put_prev_task(struct rq *rq, struct task_struct *prev)
1425{
1426 prev->sched_class->put_prev_task(rq, prev);
1427}
1428
Peter Zijlstrab2bf6c32016-09-20 22:00:38 +02001429static inline void set_curr_task(struct rq *rq, struct task_struct *curr)
1430{
1431 curr->sched_class->set_curr_task(rq);
1432}
1433
Peter Zijlstra029632f2011-10-25 10:00:11 +02001434#define sched_class_highest (&stop_sched_class)
1435#define for_each_class(class) \
1436 for (class = sched_class_highest; class; class = class->next)
1437
1438extern const struct sched_class stop_sched_class;
Dario Faggioliaab03e02013-11-28 11:14:43 +01001439extern const struct sched_class dl_sched_class;
Peter Zijlstra029632f2011-10-25 10:00:11 +02001440extern const struct sched_class rt_sched_class;
1441extern const struct sched_class fair_sched_class;
1442extern const struct sched_class idle_sched_class;
1443
1444
1445#ifdef CONFIG_SMP
1446
Patrick Bellasi2178e842016-07-22 11:35:59 +01001447extern void init_max_cpu_capacity(struct max_cpu_capacity *mcc);
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04001448extern void update_group_capacity(struct sched_domain *sd, int cpu);
Li Zefanb7192032013-03-07 10:00:26 +08001449
Daniel Lezcano7caff662014-01-06 12:34:38 +01001450extern void trigger_load_balance(struct rq *rq);
Olav Haugan3f2cb302016-05-31 14:34:46 -07001451extern void nohz_balance_clear_nohz_mask(int cpu);
Peter Zijlstra029632f2011-10-25 10:00:11 +02001452
Peter Zijlstrac5b28032015-05-15 17:43:35 +02001453extern void set_cpus_allowed_common(struct task_struct *p, const struct cpumask *new_mask);
1454
Joonwoo Parkdc3420d2017-01-31 11:14:43 -08001455bool cpu_overutilized(int cpu);
1456
Peter Zijlstra029632f2011-10-25 10:00:11 +02001457#endif
1458
Daniel Lezcano442bf3a2014-09-04 11:32:09 -04001459#ifdef CONFIG_CPU_IDLE
1460static inline void idle_set_state(struct rq *rq,
1461 struct cpuidle_state *idle_state)
1462{
1463 rq->idle_state = idle_state;
1464}
1465
1466static inline struct cpuidle_state *idle_get_state(struct rq *rq)
1467{
Peter Zijlstra9148a3a2016-09-20 22:34:51 +02001468 SCHED_WARN_ON(!rcu_read_lock_held());
Daniel Lezcano442bf3a2014-09-04 11:32:09 -04001469 return rq->idle_state;
1470}
Morten Rasmussen06910642015-01-27 13:48:07 +00001471
1472static inline void idle_set_state_idx(struct rq *rq, int idle_state_idx)
1473{
1474 rq->idle_state_idx = idle_state_idx;
1475}
1476
1477static inline int idle_get_state_idx(struct rq *rq)
1478{
1479 WARN_ON(!rcu_read_lock_held());
Pavankumar Kondetic3dae852017-06-19 15:30:11 +05301480
1481 if (rq->nr_running || cpu_of(rq) == raw_smp_processor_id())
1482 return -1;
1483
Morten Rasmussen06910642015-01-27 13:48:07 +00001484 return rq->idle_state_idx;
1485}
Daniel Lezcano442bf3a2014-09-04 11:32:09 -04001486#else
1487static inline void idle_set_state(struct rq *rq,
1488 struct cpuidle_state *idle_state)
1489{
1490}
1491
1492static inline struct cpuidle_state *idle_get_state(struct rq *rq)
1493{
1494 return NULL;
1495}
Morten Rasmussen06910642015-01-27 13:48:07 +00001496
1497static inline void idle_set_state_idx(struct rq *rq, int idle_state_idx)
1498{
1499}
1500
1501static inline int idle_get_state_idx(struct rq *rq)
1502{
1503 return -1;
1504}
Daniel Lezcano442bf3a2014-09-04 11:32:09 -04001505#endif
1506
Peter Zijlstra029632f2011-10-25 10:00:11 +02001507extern void sysrq_sched_debug_show(void);
1508extern void sched_init_granularity(void);
1509extern void update_max_interval(void);
Juri Lelli1baca4c2013-11-07 14:43:38 +01001510
1511extern void init_sched_dl_class(void);
Peter Zijlstra029632f2011-10-25 10:00:11 +02001512extern void init_sched_rt_class(void);
1513extern void init_sched_fair_class(void);
1514
Kirill Tkhai88751252014-06-29 00:03:57 +04001515extern void resched_curr(struct rq *rq);
Peter Zijlstra029632f2011-10-25 10:00:11 +02001516extern void resched_cpu(int cpu);
1517
1518extern struct rt_bandwidth def_rt_bandwidth;
1519extern void init_rt_bandwidth(struct rt_bandwidth *rt_b, u64 period, u64 runtime);
1520
Dario Faggioli332ac172013-11-07 14:43:45 +01001521extern struct dl_bandwidth def_dl_bandwidth;
1522extern void init_dl_bandwidth(struct dl_bandwidth *dl_b, u64 period, u64 runtime);
Dario Faggioliaab03e02013-11-28 11:14:43 +01001523extern void init_dl_task_timer(struct sched_dl_entity *dl_se);
1524
Dario Faggioli332ac172013-11-07 14:43:45 +01001525unsigned long to_ratio(u64 period, u64 runtime);
1526
Yuyang Du540247f2015-07-15 08:04:39 +08001527extern void init_entity_runnable_average(struct sched_entity *se);
Yuyang Du2b8c41d2016-03-30 04:30:56 +08001528extern void post_init_entity_util_avg(struct sched_entity *se);
Alex Shia75cdaa2013-06-20 10:18:47 +08001529
Frederic Weisbecker76d92ac2015-07-17 22:25:49 +02001530#ifdef CONFIG_NO_HZ_FULL
1531extern bool sched_can_stop_tick(struct rq *rq);
1532
1533/*
1534 * Tick may be needed by tasks in the runqueue depending on their policy and
1535 * requirements. If tick is needed, lets send the target an IPI to kick it out of
1536 * nohz mode if necessary.
1537 */
1538static inline void sched_update_tick_dependency(struct rq *rq)
1539{
1540 int cpu;
1541
1542 if (!tick_nohz_full_enabled())
1543 return;
1544
1545 cpu = cpu_of(rq);
1546
1547 if (!tick_nohz_full_cpu(cpu))
1548 return;
1549
1550 if (sched_can_stop_tick(rq))
1551 tick_nohz_dep_clear_cpu(cpu, TICK_DEP_BIT_SCHED);
1552 else
1553 tick_nohz_dep_set_cpu(cpu, TICK_DEP_BIT_SCHED);
1554}
1555#else
1556static inline void sched_update_tick_dependency(struct rq *rq) { }
1557#endif
1558
Joseph Lo77501862013-04-22 14:39:18 +08001559static inline void __add_nr_running(struct rq *rq, unsigned count)
Peter Zijlstra029632f2011-10-25 10:00:11 +02001560{
Kirill Tkhai72465442014-05-09 03:00:14 +04001561 unsigned prev_nr = rq->nr_running;
1562
Syed Rameez Mustafadddcab72016-09-07 16:18:27 -07001563 sched_update_nr_prod(cpu_of(rq), count, true);
Kirill Tkhai72465442014-05-09 03:00:14 +04001564 rq->nr_running = prev_nr + count;
Frederic Weisbecker9f3660c2013-04-20 14:35:09 +02001565
Kirill Tkhai72465442014-05-09 03:00:14 +04001566 if (prev_nr < 2 && rq->nr_running >= 2) {
Tim Chen4486edd2014-06-23 12:16:49 -07001567#ifdef CONFIG_SMP
1568 if (!rq->rd->overload)
1569 rq->rd->overload = true;
1570#endif
Tim Chen4486edd2014-06-23 12:16:49 -07001571 }
Frederic Weisbecker76d92ac2015-07-17 22:25:49 +02001572
1573 sched_update_tick_dependency(rq);
Peter Zijlstra029632f2011-10-25 10:00:11 +02001574}
1575
Joseph Lo77501862013-04-22 14:39:18 +08001576static inline void __sub_nr_running(struct rq *rq, unsigned count)
Peter Zijlstra029632f2011-10-25 10:00:11 +02001577{
Syed Rameez Mustafadddcab72016-09-07 16:18:27 -07001578 sched_update_nr_prod(cpu_of(rq), count, false);
Kirill Tkhai72465442014-05-09 03:00:14 +04001579 rq->nr_running -= count;
Frederic Weisbecker76d92ac2015-07-17 22:25:49 +02001580 /* Check if we still need preemption */
1581 sched_update_tick_dependency(rq);
Peter Zijlstra029632f2011-10-25 10:00:11 +02001582}
1583
Joseph Lo77501862013-04-22 14:39:18 +08001584#ifdef CONFIG_CPU_QUIET
1585#define NR_AVE_SCALE(x) ((x) << FSHIFT)
1586static inline u64 do_nr_running_integral(struct rq *rq)
1587{
1588 s64 nr, deltax;
1589 u64 nr_running_integral = rq->nr_running_integral;
1590
1591 deltax = rq->clock_task - rq->nr_last_stamp;
1592 nr = NR_AVE_SCALE(rq->nr_running);
1593
1594 nr_running_integral += nr * deltax;
1595
1596 return nr_running_integral;
1597}
1598
1599static inline void add_nr_running(struct rq *rq, unsigned count)
1600{
1601 write_seqcount_begin(&rq->ave_seqcnt);
1602 rq->nr_running_integral = do_nr_running_integral(rq);
1603 rq->nr_last_stamp = rq->clock_task;
1604 __add_nr_running(rq, count);
1605 write_seqcount_end(&rq->ave_seqcnt);
1606}
1607
1608static inline void sub_nr_running(struct rq *rq, unsigned count)
1609{
1610 write_seqcount_begin(&rq->ave_seqcnt);
1611 rq->nr_running_integral = do_nr_running_integral(rq);
1612 rq->nr_last_stamp = rq->clock_task;
1613 __sub_nr_running(rq, count);
1614 write_seqcount_end(&rq->ave_seqcnt);
1615}
1616#else
1617#define add_nr_running __add_nr_running
1618#define sub_nr_running __sub_nr_running
1619#endif
1620
Frederic Weisbecker265f22a2013-05-03 03:39:05 +02001621static inline void rq_last_tick_reset(struct rq *rq)
1622{
1623#ifdef CONFIG_NO_HZ_FULL
1624 rq->last_sched_tick = jiffies;
1625#endif
1626}
1627
Peter Zijlstra029632f2011-10-25 10:00:11 +02001628extern void update_rq_clock(struct rq *rq);
1629
1630extern void activate_task(struct rq *rq, struct task_struct *p, int flags);
1631extern void deactivate_task(struct rq *rq, struct task_struct *p, int flags);
1632
1633extern void check_preempt_curr(struct rq *rq, struct task_struct *p, int flags);
1634
1635extern const_debug unsigned int sysctl_sched_time_avg;
1636extern const_debug unsigned int sysctl_sched_nr_migrate;
1637extern const_debug unsigned int sysctl_sched_migration_cost;
1638
1639static inline u64 sched_avg_period(void)
1640{
1641 return (u64)sysctl_sched_time_avg * NSEC_PER_MSEC / 2;
1642}
1643
Peter Zijlstra029632f2011-10-25 10:00:11 +02001644#ifdef CONFIG_SCHED_HRTICK
1645
1646/*
1647 * Use hrtick when:
1648 * - enabled by features
1649 * - hrtimer is actually high res
1650 */
1651static inline int hrtick_enabled(struct rq *rq)
1652{
1653 if (!sched_feat(HRTICK))
1654 return 0;
1655 if (!cpu_active(cpu_of(rq)))
1656 return 0;
1657 return hrtimer_is_hres_active(&rq->hrtick_timer);
1658}
1659
1660void hrtick_start(struct rq *rq, u64 delay);
1661
Mike Galbraithb39e66e2011-11-22 15:20:07 +01001662#else
1663
1664static inline int hrtick_enabled(struct rq *rq)
1665{
1666 return 0;
1667}
1668
Peter Zijlstra029632f2011-10-25 10:00:11 +02001669#endif /* CONFIG_SCHED_HRTICK */
1670
1671#ifdef CONFIG_SMP
1672extern void sched_avg_update(struct rq *rq);
Peter Zijlstradfbca412015-03-23 14:19:05 +01001673
1674#ifndef arch_scale_freq_capacity
1675static __always_inline
1676unsigned long arch_scale_freq_capacity(struct sched_domain *sd, int cpu)
1677{
1678 return SCHED_CAPACITY_SCALE;
1679}
1680#endif
Vincent Guittotb5b48602015-02-27 16:54:08 +01001681
Morten Rasmussen8cd56012015-08-14 17:23:10 +01001682#ifndef arch_scale_cpu_capacity
1683static __always_inline
1684unsigned long arch_scale_cpu_capacity(struct sched_domain *sd, int cpu)
1685{
Dietmar Eggemanne3279a22015-08-15 00:04:41 +01001686 if (sd && (sd->flags & SD_SHARE_CPUCAPACITY) && (sd->span_weight > 1))
Morten Rasmussen8cd56012015-08-14 17:23:10 +01001687 return sd->smt_gain / sd->span_weight;
1688
1689 return SCHED_CAPACITY_SCALE;
1690}
1691#endif
1692
Joonwoo Park28c51212017-06-09 14:06:54 -07001693#ifndef arch_update_cpu_capacity
1694static __always_inline
1695void arch_update_cpu_capacity(int cpu)
1696{
1697}
1698#endif
1699
Steve Muckle608d4942015-06-25 14:12:33 +01001700#ifdef CONFIG_SMP
1701static inline unsigned long capacity_of(int cpu)
1702{
1703 return cpu_rq(cpu)->cpu_capacity;
1704}
1705
1706static inline unsigned long capacity_orig_of(int cpu)
1707{
1708 return cpu_rq(cpu)->cpu_capacity_orig;
1709}
1710
Srivatsa Vaddagiri26c21542016-05-31 09:08:38 -07001711extern unsigned int sysctl_sched_use_walt_cpu_util;
Srivatsa Vaddagiri26c21542016-05-31 09:08:38 -07001712extern unsigned int walt_disabled;
1713
Steve Muckle608d4942015-06-25 14:12:33 +01001714/*
1715 * cpu_util returns the amount of capacity of a CPU that is used by CFS
1716 * tasks. The unit of the return value must be the one of capacity so we can
1717 * compare the utilization with the capacity of the CPU that is available for
1718 * CFS task (ie cpu_capacity).
1719 *
1720 * cfs_rq.avg.util_avg is the sum of running time of runnable tasks plus the
1721 * recent utilization of currently non-runnable tasks on a CPU. It represents
1722 * the amount of utilization of a CPU in the range [0..capacity_orig] where
1723 * capacity_orig is the cpu_capacity available at the highest frequency
1724 * (arch_scale_freq_capacity()).
1725 * The utilization of a CPU converges towards a sum equal to or less than the
1726 * current capacity (capacity_curr <= capacity_orig) of the CPU because it is
1727 * the running time on this CPU scaled by capacity_curr.
1728 *
1729 * Nevertheless, cfs_rq.avg.util_avg can be higher than capacity_curr or even
1730 * higher than capacity_orig because of unfortunate rounding in
1731 * cfs.avg.util_avg or just after migrating tasks and new task wakeups until
1732 * the average stabilizes with the new running time. We need to check that the
1733 * utilization stays within the range of [0..capacity_orig] and cap it if
1734 * necessary. Without utilization capping, a group could be seen as overloaded
1735 * (CPU0 utilization at 121% + CPU1 utilization at 80%) whereas CPU1 has 20% of
1736 * available capacity. We allow utilization to overshoot capacity_curr (but not
1737 * capacity_orig) as it useful for predicting the capacity required after task
1738 * migrations (scheduler-driven DVFS).
1739 */
1740static inline unsigned long __cpu_util(int cpu, int delta)
1741{
Joonwoo Park93a51bf2017-01-20 11:10:15 -08001742 u64 util = cpu_rq(cpu)->cfs.avg.util_avg;
Steve Muckle608d4942015-06-25 14:12:33 +01001743 unsigned long capacity = capacity_orig_of(cpu);
1744
Srivatsa Vaddagiri26c21542016-05-31 09:08:38 -07001745#ifdef CONFIG_SCHED_WALT
Amit Pundir102f7f42016-08-24 11:52:17 +05301746 if (!walt_disabled && sysctl_sched_use_walt_cpu_util) {
Pavankumar Kondeti84f72d72017-07-20 11:00:45 +05301747 util = cpu_rq(cpu)->walt_stats.cumulative_runnable_avg;
Joonwoo Park93a51bf2017-01-20 11:10:15 -08001748 util = div64_u64(util,
1749 sched_ravg_window >> SCHED_CAPACITY_SHIFT);
Amit Pundir102f7f42016-08-24 11:52:17 +05301750 }
Srivatsa Vaddagiri26c21542016-05-31 09:08:38 -07001751#endif
Steve Muckle608d4942015-06-25 14:12:33 +01001752 delta += util;
1753 if (delta < 0)
1754 return 0;
1755
1756 return (delta >= capacity) ? capacity : delta;
1757}
1758
1759static inline unsigned long cpu_util(int cpu)
1760{
1761 return __cpu_util(cpu, 0);
1762}
1763
Joonwoo Park19c00752017-04-27 14:37:35 -07001764struct sched_walt_cpu_load {
1765 unsigned long prev_window_util;
1766 unsigned long nl;
1767 unsigned long pl;
Vikram Mulukutlaa65aafe2017-06-05 13:37:45 -07001768 u64 ws;
Joonwoo Park19c00752017-04-27 14:37:35 -07001769};
1770
Joonwoo Parke19cd6f2017-02-03 14:32:49 -08001771static inline unsigned long cpu_util_cum(int cpu, int delta)
1772{
1773 u64 util = cpu_rq(cpu)->cfs.avg.util_avg;
1774 unsigned long capacity = capacity_orig_of(cpu);
1775
1776#ifdef CONFIG_SCHED_WALT
1777 if (!walt_disabled && sysctl_sched_use_walt_cpu_util) {
1778 util = cpu_rq(cpu)->cum_window_demand;
1779 util = div64_u64(util,
1780 sched_ravg_window >> SCHED_CAPACITY_SHIFT);
1781 }
1782#endif
1783 delta += util;
1784 if (delta < 0)
1785 return 0;
1786
1787 return (delta >= capacity) ? capacity : delta;
1788}
1789
Syed Rameez Mustafa20acfe72017-01-30 09:35:46 +05301790#ifdef CONFIG_SCHED_WALT
1791u64 freq_policy_load(struct rq *rq);
1792#endif
1793
Joonwoo Park19c00752017-04-27 14:37:35 -07001794static inline unsigned long
1795cpu_util_freq(int cpu, struct sched_walt_cpu_load *walt_load)
1796{
Syed Rameez Mustafa20acfe72017-01-30 09:35:46 +05301797 struct rq *rq = cpu_rq(cpu);
1798 u64 util = rq->cfs.avg.util_avg;
Joonwoo Park4266ccd2016-12-08 16:12:12 -08001799 unsigned long capacity = capacity_orig_of(cpu);
Joonwoo Park19c00752017-04-27 14:37:35 -07001800
1801#ifdef CONFIG_SCHED_WALT
Joonwoo Park4266ccd2016-12-08 16:12:12 -08001802 if (!walt_disabled && sysctl_sched_use_walt_cpu_util) {
Syed Rameez Mustafa20acfe72017-01-30 09:35:46 +05301803
1804 util = freq_policy_load(rq);
Joonwoo Park93a51bf2017-01-20 11:10:15 -08001805 util = div64_u64(util,
1806 sched_ravg_window >> SCHED_CAPACITY_SHIFT);
Joonwoo Park19c00752017-04-27 14:37:35 -07001807
Syed Rameez Mustafa20acfe72017-01-30 09:35:46 +05301808 if (walt_load) {
1809 u64 nl = cpu_rq(cpu)->nt_prev_runnable_sum +
1810 rq->grp_time.nt_prev_runnable_sum;
1811
1812 nl = div64_u64(nl, sched_ravg_window >>
1813 SCHED_CAPACITY_SHIFT);
1814
Joonwoo Park4266ccd2016-12-08 16:12:12 -08001815 walt_load->prev_window_util = util;
Syed Rameez Mustafa20acfe72017-01-30 09:35:46 +05301816 walt_load->nl = nl;
Joonwoo Park81e7e572017-05-19 15:45:04 -07001817 walt_load->pl = 0;
Vikram Mulukutlaa65aafe2017-06-05 13:37:45 -07001818 walt_load->ws = rq->window_start;
Syed Rameez Mustafa20acfe72017-01-30 09:35:46 +05301819 }
Joonwoo Park4266ccd2016-12-08 16:12:12 -08001820 }
1821#endif
1822 return (util >= capacity) ? capacity : util;
Joonwoo Park19c00752017-04-27 14:37:35 -07001823}
Steve Muckle608d4942015-06-25 14:12:33 +01001824#endif
1825
Joonwoo Park2ae888b2017-01-12 17:46:00 -08001826extern unsigned int capacity_margin_freq;
1827
1828static inline unsigned long add_capacity_margin(unsigned long cpu_capacity)
1829{
1830 cpu_capacity = cpu_capacity * capacity_margin_freq;
1831 cpu_capacity /= SCHED_CAPACITY_SCALE;
1832 return cpu_capacity;
1833}
1834
Michael Turquette3b6188e2015-06-30 12:45:48 +01001835#ifdef CONFIG_CPU_FREQ_GOV_SCHED
Steve Muckle608d4942015-06-25 14:12:33 +01001836#define capacity_max SCHED_CAPACITY_SCALE
Michael Turquette3b6188e2015-06-30 12:45:48 +01001837extern struct static_key __sched_freq;
1838
1839static inline bool sched_freq(void)
1840{
1841 return static_key_false(&__sched_freq);
1842}
1843
1844DECLARE_PER_CPU(struct sched_capacity_reqs, cpu_sched_capacity_reqs);
1845void update_cpu_capacity_request(int cpu, bool request);
1846
1847static inline void set_cfs_cpu_capacity(int cpu, bool request,
1848 unsigned long capacity)
1849{
Patrick Bellasic4eef1f2016-06-30 15:00:41 +01001850 struct sched_capacity_reqs *scr = &per_cpu(cpu_sched_capacity_reqs, cpu);
1851
1852#ifdef CONFIG_SCHED_WALT
1853 if (!walt_disabled && sysctl_sched_use_walt_cpu_util) {
1854 int rtdl = scr->rt + scr->dl;
1855 /*
1856 * WALT tracks the utilization of a CPU considering the load
1857 * generated by all the scheduling classes.
1858 * Since the following call to:
1859 * update_cpu_capacity
1860 * is already adding the RT and DL utilizations let's remove
1861 * these contributions from the WALT signal.
1862 */
1863 if (capacity > rtdl)
1864 capacity -= rtdl;
1865 else
1866 capacity = 0;
1867 }
1868#endif
1869 if (scr->cfs != capacity) {
1870 scr->cfs = capacity;
Michael Turquette3b6188e2015-06-30 12:45:48 +01001871 update_cpu_capacity_request(cpu, request);
1872 }
1873}
1874
1875static inline void set_rt_cpu_capacity(int cpu, bool request,
1876 unsigned long capacity)
1877{
1878 if (per_cpu(cpu_sched_capacity_reqs, cpu).rt != capacity) {
1879 per_cpu(cpu_sched_capacity_reqs, cpu).rt = capacity;
1880 update_cpu_capacity_request(cpu, request);
1881 }
1882}
1883
1884static inline void set_dl_cpu_capacity(int cpu, bool request,
1885 unsigned long capacity)
1886{
1887 if (per_cpu(cpu_sched_capacity_reqs, cpu).dl != capacity) {
1888 per_cpu(cpu_sched_capacity_reqs, cpu).dl = capacity;
1889 update_cpu_capacity_request(cpu, request);
1890 }
1891}
1892#else
1893static inline bool sched_freq(void) { return false; }
1894static inline void set_cfs_cpu_capacity(int cpu, bool request,
1895 unsigned long capacity)
1896{ }
1897static inline void set_rt_cpu_capacity(int cpu, bool request,
1898 unsigned long capacity)
1899{ }
1900static inline void set_dl_cpu_capacity(int cpu, bool request,
1901 unsigned long capacity)
1902{ }
1903#endif
1904
Peter Zijlstra029632f2011-10-25 10:00:11 +02001905static inline void sched_rt_avg_update(struct rq *rq, u64 rt_delta)
1906{
Vincent Guittotb5b48602015-02-27 16:54:08 +01001907 rq->rt_avg += rt_delta * arch_scale_freq_capacity(NULL, cpu_of(rq));
Peter Zijlstra029632f2011-10-25 10:00:11 +02001908}
1909#else
1910static inline void sched_rt_avg_update(struct rq *rq, u64 rt_delta) { }
1911static inline void sched_avg_update(struct rq *rq) { }
1912#endif
1913
Peter Zijlstraeb580752015-07-31 21:28:18 +02001914struct rq_flags {
1915 unsigned long flags;
Peter Zijlstrae7904a22015-08-01 19:25:08 +02001916 struct pin_cookie cookie;
Peter Zijlstraeb580752015-07-31 21:28:18 +02001917};
1918
1919struct rq *__task_rq_lock(struct task_struct *p, struct rq_flags *rf)
Peter Zijlstra3e71a462016-04-28 16:16:33 +02001920 __acquires(rq->lock);
Peter Zijlstraeb580752015-07-31 21:28:18 +02001921struct rq *task_rq_lock(struct task_struct *p, struct rq_flags *rf)
Peter Zijlstra3960c8c2015-02-17 13:22:25 +01001922 __acquires(p->pi_lock)
Peter Zijlstra3e71a462016-04-28 16:16:33 +02001923 __acquires(rq->lock);
Peter Zijlstra3960c8c2015-02-17 13:22:25 +01001924
Peter Zijlstraeb580752015-07-31 21:28:18 +02001925static inline void __task_rq_unlock(struct rq *rq, struct rq_flags *rf)
Peter Zijlstra3960c8c2015-02-17 13:22:25 +01001926 __releases(rq->lock)
1927{
Peter Zijlstrae7904a22015-08-01 19:25:08 +02001928 lockdep_unpin_lock(&rq->lock, rf->cookie);
Peter Zijlstra3960c8c2015-02-17 13:22:25 +01001929 raw_spin_unlock(&rq->lock);
1930}
1931
1932static inline void
Peter Zijlstraeb580752015-07-31 21:28:18 +02001933task_rq_unlock(struct rq *rq, struct task_struct *p, struct rq_flags *rf)
Peter Zijlstra3960c8c2015-02-17 13:22:25 +01001934 __releases(rq->lock)
1935 __releases(p->pi_lock)
1936{
Peter Zijlstrae7904a22015-08-01 19:25:08 +02001937 lockdep_unpin_lock(&rq->lock, rf->cookie);
Peter Zijlstra3960c8c2015-02-17 13:22:25 +01001938 raw_spin_unlock(&rq->lock);
Peter Zijlstraeb580752015-07-31 21:28:18 +02001939 raw_spin_unlock_irqrestore(&p->pi_lock, rf->flags);
Peter Zijlstra3960c8c2015-02-17 13:22:25 +01001940}
1941
Patrick Bellasid2489002016-07-28 18:44:40 +01001942extern struct rq *lock_rq_of(struct task_struct *p, struct rq_flags *flags);
1943extern void unlock_rq_of(struct rq *rq, struct task_struct *p, struct rq_flags *flags);
1944
Peter Zijlstra029632f2011-10-25 10:00:11 +02001945#ifdef CONFIG_SMP
1946#ifdef CONFIG_PREEMPT
1947
1948static inline void double_rq_lock(struct rq *rq1, struct rq *rq2);
1949
1950/*
1951 * fair double_lock_balance: Safely acquires both rq->locks in a fair
1952 * way at the expense of forcing extra atomic operations in all
1953 * invocations. This assures that the double_lock is acquired using the
1954 * same underlying policy as the spinlock_t on this architecture, which
1955 * reduces latency compared to the unfair variant below. However, it
1956 * also adds more overhead and therefore may reduce throughput.
1957 */
1958static inline int _double_lock_balance(struct rq *this_rq, struct rq *busiest)
1959 __releases(this_rq->lock)
1960 __acquires(busiest->lock)
1961 __acquires(this_rq->lock)
1962{
1963 raw_spin_unlock(&this_rq->lock);
1964 double_rq_lock(this_rq, busiest);
1965
1966 return 1;
1967}
1968
1969#else
1970/*
1971 * Unfair double_lock_balance: Optimizes throughput at the expense of
1972 * latency by eliminating extra atomic operations when the locks are
1973 * already in proper order on entry. This favors lower cpu-ids and will
1974 * grant the double lock to lower cpus over higher ids under contention,
1975 * regardless of entry order into the function.
1976 */
1977static inline int _double_lock_balance(struct rq *this_rq, struct rq *busiest)
1978 __releases(this_rq->lock)
1979 __acquires(busiest->lock)
1980 __acquires(this_rq->lock)
1981{
1982 int ret = 0;
1983
1984 if (unlikely(!raw_spin_trylock(&busiest->lock))) {
1985 if (busiest < this_rq) {
1986 raw_spin_unlock(&this_rq->lock);
1987 raw_spin_lock(&busiest->lock);
1988 raw_spin_lock_nested(&this_rq->lock,
1989 SINGLE_DEPTH_NESTING);
1990 ret = 1;
1991 } else
1992 raw_spin_lock_nested(&busiest->lock,
1993 SINGLE_DEPTH_NESTING);
1994 }
1995 return ret;
1996}
1997
1998#endif /* CONFIG_PREEMPT */
1999
2000/*
2001 * double_lock_balance - lock the busiest runqueue, this_rq is locked already.
2002 */
2003static inline int double_lock_balance(struct rq *this_rq, struct rq *busiest)
2004{
2005 if (unlikely(!irqs_disabled())) {
2006 /* printk() doesn't work good under rq->lock */
2007 raw_spin_unlock(&this_rq->lock);
2008 BUG_ON(1);
2009 }
2010
2011 return _double_lock_balance(this_rq, busiest);
2012}
2013
2014static inline void double_unlock_balance(struct rq *this_rq, struct rq *busiest)
2015 __releases(busiest->lock)
2016{
Todd Kjosa31778a2016-07-04 15:04:45 +01002017 if (this_rq != busiest)
2018 raw_spin_unlock(&busiest->lock);
Peter Zijlstra029632f2011-10-25 10:00:11 +02002019 lock_set_subclass(&this_rq->lock.dep_map, 0, _RET_IP_);
2020}
2021
Peter Zijlstra74602312013-10-10 20:17:22 +02002022static inline void double_lock(spinlock_t *l1, spinlock_t *l2)
2023{
2024 if (l1 > l2)
2025 swap(l1, l2);
2026
2027 spin_lock(l1);
2028 spin_lock_nested(l2, SINGLE_DEPTH_NESTING);
2029}
2030
Mike Galbraith60e69ee2014-04-07 10:55:15 +02002031static inline void double_lock_irq(spinlock_t *l1, spinlock_t *l2)
2032{
2033 if (l1 > l2)
2034 swap(l1, l2);
2035
2036 spin_lock_irq(l1);
2037 spin_lock_nested(l2, SINGLE_DEPTH_NESTING);
2038}
2039
Peter Zijlstra74602312013-10-10 20:17:22 +02002040static inline void double_raw_lock(raw_spinlock_t *l1, raw_spinlock_t *l2)
2041{
2042 if (l1 > l2)
2043 swap(l1, l2);
2044
2045 raw_spin_lock(l1);
2046 raw_spin_lock_nested(l2, SINGLE_DEPTH_NESTING);
2047}
2048
Peter Zijlstra029632f2011-10-25 10:00:11 +02002049/*
2050 * double_rq_lock - safely lock two runqueues
2051 *
2052 * Note this does not disable interrupts like task_rq_lock,
2053 * you need to do so manually before calling.
2054 */
2055static inline void double_rq_lock(struct rq *rq1, struct rq *rq2)
2056 __acquires(rq1->lock)
2057 __acquires(rq2->lock)
2058{
2059 BUG_ON(!irqs_disabled());
2060 if (rq1 == rq2) {
2061 raw_spin_lock(&rq1->lock);
2062 __acquire(rq2->lock); /* Fake it out ;) */
2063 } else {
2064 if (rq1 < rq2) {
2065 raw_spin_lock(&rq1->lock);
2066 raw_spin_lock_nested(&rq2->lock, SINGLE_DEPTH_NESTING);
2067 } else {
2068 raw_spin_lock(&rq2->lock);
2069 raw_spin_lock_nested(&rq1->lock, SINGLE_DEPTH_NESTING);
2070 }
2071 }
2072}
2073
2074/*
2075 * double_rq_unlock - safely unlock two runqueues
2076 *
2077 * Note this does not restore interrupts like task_rq_unlock,
2078 * you need to do so manually after calling.
2079 */
2080static inline void double_rq_unlock(struct rq *rq1, struct rq *rq2)
2081 __releases(rq1->lock)
2082 __releases(rq2->lock)
2083{
2084 raw_spin_unlock(&rq1->lock);
2085 if (rq1 != rq2)
2086 raw_spin_unlock(&rq2->lock);
2087 else
2088 __release(rq2->lock);
2089}
2090
John Diase2c5c982016-09-15 08:52:27 -07002091/*
2092 * task_may_not_preempt - check whether a task may not be preemptible soon
2093 */
2094extern bool task_may_not_preempt(struct task_struct *task, int cpu);
2095
Peter Zijlstra029632f2011-10-25 10:00:11 +02002096#else /* CONFIG_SMP */
2097
2098/*
2099 * double_rq_lock - safely lock two runqueues
2100 *
2101 * Note this does not disable interrupts like task_rq_lock,
2102 * you need to do so manually before calling.
2103 */
2104static inline void double_rq_lock(struct rq *rq1, struct rq *rq2)
2105 __acquires(rq1->lock)
2106 __acquires(rq2->lock)
2107{
2108 BUG_ON(!irqs_disabled());
2109 BUG_ON(rq1 != rq2);
2110 raw_spin_lock(&rq1->lock);
2111 __acquire(rq2->lock); /* Fake it out ;) */
2112}
2113
2114/*
2115 * double_rq_unlock - safely unlock two runqueues
2116 *
2117 * Note this does not restore interrupts like task_rq_unlock,
2118 * you need to do so manually after calling.
2119 */
2120static inline void double_rq_unlock(struct rq *rq1, struct rq *rq2)
2121 __releases(rq1->lock)
2122 __releases(rq2->lock)
2123{
2124 BUG_ON(rq1 != rq2);
2125 raw_spin_unlock(&rq1->lock);
2126 __release(rq2->lock);
2127}
2128
2129#endif
2130
2131extern struct sched_entity *__pick_first_entity(struct cfs_rq *cfs_rq);
2132extern struct sched_entity *__pick_last_entity(struct cfs_rq *cfs_rq);
Srikar Dronamraju6b55c962015-06-25 22:51:41 +05302133
2134#ifdef CONFIG_SCHED_DEBUG
Peter Zijlstra029632f2011-10-25 10:00:11 +02002135extern void print_cfs_stats(struct seq_file *m, int cpu);
2136extern void print_rt_stats(struct seq_file *m, int cpu);
Wanpeng Liacb32132014-10-31 06:39:33 +08002137extern void print_dl_stats(struct seq_file *m, int cpu);
Srikar Dronamraju6b55c962015-06-25 22:51:41 +05302138extern void
2139print_cfs_rq(struct seq_file *m, int cpu, struct cfs_rq *cfs_rq);
Srikar Dronamraju397f2372015-06-25 22:51:43 +05302140
2141#ifdef CONFIG_NUMA_BALANCING
2142extern void
2143show_numa_stats(struct task_struct *p, struct seq_file *m);
2144extern void
2145print_numa_stats(struct seq_file *m, int node, unsigned long tsf,
2146 unsigned long tpf, unsigned long gsf, unsigned long gpf);
2147#endif /* CONFIG_NUMA_BALANCING */
2148#endif /* CONFIG_SCHED_DEBUG */
Peter Zijlstra029632f2011-10-25 10:00:11 +02002149
2150extern void init_cfs_rq(struct cfs_rq *cfs_rq);
Abel Vesa07c54f72015-03-03 13:50:27 +02002151extern void init_rt_rq(struct rt_rq *rt_rq);
2152extern void init_dl_rq(struct dl_rq *dl_rq);
Peter Zijlstra029632f2011-10-25 10:00:11 +02002153
Ben Segall1ee14e62013-10-16 11:16:12 -07002154extern void cfs_bandwidth_usage_inc(void);
2155extern void cfs_bandwidth_usage_dec(void);
Suresh Siddha1c792db2011-12-01 17:07:32 -08002156
Frederic Weisbecker3451d022011-08-10 23:21:01 +02002157#ifdef CONFIG_NO_HZ_COMMON
Suresh Siddha1c792db2011-12-01 17:07:32 -08002158enum rq_nohz_flag_bits {
2159 NOHZ_TICK_STOPPED,
2160 NOHZ_BALANCE_KICK,
2161};
2162
Syed Rameez Mustafadddcab72016-09-07 16:18:27 -07002163#define NOHZ_KICK_ANY 0
2164#define NOHZ_KICK_RESTRICT 1
2165
Suresh Siddha1c792db2011-12-01 17:07:32 -08002166#define nohz_flags(cpu) (&cpu_rq(cpu)->nohz_flags)
Thomas Gleixner20a5c8c2016-03-10 12:54:20 +01002167
2168extern void nohz_balance_exit_idle(unsigned int cpu);
2169#else
2170static inline void nohz_balance_exit_idle(unsigned int cpu) { }
Suresh Siddha1c792db2011-12-01 17:07:32 -08002171#endif
Frederic Weisbecker73fbec62012-06-16 15:57:37 +02002172
2173#ifdef CONFIG_IRQ_TIME_ACCOUNTING
Frederic Weisbecker19d23dbf2016-09-26 02:29:20 +02002174struct irqtime {
2175 u64 hardirq_time;
2176 u64 softirq_time;
2177 u64 irq_start_time;
2178 struct u64_stats_sync sync;
2179};
Frederic Weisbecker73fbec62012-06-16 15:57:37 +02002180
Frederic Weisbecker19d23dbf2016-09-26 02:29:20 +02002181DECLARE_PER_CPU(struct irqtime, cpu_irqtime);
Frederic Weisbecker73fbec62012-06-16 15:57:37 +02002182
2183static inline u64 irq_time_read(int cpu)
2184{
Frederic Weisbecker19d23dbf2016-09-26 02:29:20 +02002185 struct irqtime *irqtime = &per_cpu(cpu_irqtime, cpu);
2186 unsigned int seq;
2187 u64 total;
Frederic Weisbecker73fbec62012-06-16 15:57:37 +02002188
2189 do {
Frederic Weisbecker19d23dbf2016-09-26 02:29:20 +02002190 seq = __u64_stats_fetch_begin(&irqtime->sync);
2191 total = irqtime->softirq_time + irqtime->hardirq_time;
2192 } while (__u64_stats_fetch_retry(&irqtime->sync, seq));
Frederic Weisbecker73fbec62012-06-16 15:57:37 +02002193
Frederic Weisbecker19d23dbf2016-09-26 02:29:20 +02002194 return total;
Frederic Weisbecker73fbec62012-06-16 15:57:37 +02002195}
Frederic Weisbecker73fbec62012-06-16 15:57:37 +02002196#endif /* CONFIG_IRQ_TIME_ACCOUNTING */
Rafael J. Wysockiadaf9fc2016-03-10 20:44:47 +01002197
Vikram Mulukutlaa65aafe2017-06-05 13:37:45 -07002198#ifdef CONFIG_SCHED_WALT
2199u64 sched_ktime_clock(void);
Vikram Mulukutlac7b54b82017-07-12 11:34:54 -07002200void note_task_waking(struct task_struct *p, u64 wallclock);
Vikram Mulukutlaa65aafe2017-06-05 13:37:45 -07002201#else /* CONFIG_SCHED_WALT */
2202static inline u64 sched_ktime_clock(void)
2203{
2204 return 0;
2205}
Vikram Mulukutlac7b54b82017-07-12 11:34:54 -07002206static inline void note_task_waking(struct task_struct *p, u64 wallclock) { }
Vikram Mulukutlaa65aafe2017-06-05 13:37:45 -07002207#endif /* CONFIG_SCHED_WALT */
2208
Rafael J. Wysockiadaf9fc2016-03-10 20:44:47 +01002209#ifdef CONFIG_CPU_FREQ
2210DECLARE_PER_CPU(struct update_util_data *, cpufreq_update_util_data);
2211
2212/**
2213 * cpufreq_update_util - Take a note about CPU utilization changes.
Rafael J. Wysocki12bde332016-08-10 03:11:17 +02002214 * @rq: Runqueue to carry out the update for.
Rafael J. Wysocki58919e82016-08-16 22:14:55 +02002215 * @flags: Update reason flags.
Rafael J. Wysockiadaf9fc2016-03-10 20:44:47 +01002216 *
Rafael J. Wysocki58919e82016-08-16 22:14:55 +02002217 * This function is called by the scheduler on the CPU whose utilization is
2218 * being updated.
Rafael J. Wysockiadaf9fc2016-03-10 20:44:47 +01002219 *
2220 * It can only be called from RCU-sched read-side critical sections.
Rafael J. Wysockiadaf9fc2016-03-10 20:44:47 +01002221 *
2222 * The way cpufreq is currently arranged requires it to evaluate the CPU
2223 * performance state (frequency/voltage) on a regular basis to prevent it from
2224 * being stuck in a completely inadequate performance level for too long.
2225 * That is not guaranteed to happen if the updates are only triggered from CFS,
2226 * though, because they may not be coming in if RT or deadline tasks are active
2227 * all the time (or there are RT and DL tasks only).
2228 *
2229 * As a workaround for that issue, this function is called by the RT and DL
2230 * sched classes to trigger extra cpufreq updates to prevent it from stalling,
2231 * but that really is a band-aid. Going forward it should be replaced with
2232 * solutions targeted more specifically at RT and DL tasks.
2233 */
Rafael J. Wysocki12bde332016-08-10 03:11:17 +02002234static inline void cpufreq_update_util(struct rq *rq, unsigned int flags)
Rafael J. Wysockiadaf9fc2016-03-10 20:44:47 +01002235{
Rafael J. Wysocki58919e82016-08-16 22:14:55 +02002236 struct update_util_data *data;
2237
Vikram Mulukutlaab968a42017-05-08 19:18:22 -07002238#ifdef CONFIG_SCHED_WALT
Vikram Mulukutla4b54aae2017-03-20 13:41:37 -07002239 /*
2240 * Skip if we've already reported, but not if this is an inter-cluster
Vikram Mulukutlaf6686692017-06-06 11:58:27 -07002241 * migration. Also only allow WALT update sites.
Vikram Mulukutla4b54aae2017-03-20 13:41:37 -07002242 */
Vikram Mulukutlaf6686692017-06-06 11:58:27 -07002243 if (!(flags & SCHED_CPUFREQ_WALT))
2244 return;
Vikram Mulukutla4b54aae2017-03-20 13:41:37 -07002245 if (!sched_disable_window_stats &&
2246 (rq->load_reported_window == rq->window_start) &&
2247 !(flags & SCHED_CPUFREQ_INTERCLUSTER_MIG))
2248 return;
2249 rq->load_reported_window = rq->window_start;
2250#endif
2251
Vikram Mulukutlabab41882017-05-09 17:49:47 -07002252 data = rcu_dereference_sched(*per_cpu_ptr(&cpufreq_update_util_data,
2253 cpu_of(rq)));
Rafael J. Wysocki58919e82016-08-16 22:14:55 +02002254 if (data)
Vikram Mulukutlaa65aafe2017-06-05 13:37:45 -07002255 data->func(data, sched_ktime_clock(), flags);
Rafael J. Wysocki12bde332016-08-10 03:11:17 +02002256}
2257
2258static inline void cpufreq_update_this_cpu(struct rq *rq, unsigned int flags)
2259{
2260 if (cpu_of(rq) == smp_processor_id())
2261 cpufreq_update_util(rq, flags);
Rafael J. Wysockiadaf9fc2016-03-10 20:44:47 +01002262}
2263#else
Rafael J. Wysocki12bde332016-08-10 03:11:17 +02002264static inline void cpufreq_update_util(struct rq *rq, unsigned int flags) {}
2265static inline void cpufreq_update_this_cpu(struct rq *rq, unsigned int flags) {}
Rafael J. Wysockiadaf9fc2016-03-10 20:44:47 +01002266#endif /* CONFIG_CPU_FREQ */
Linus Torvaldsbe53f582016-03-24 09:42:50 -07002267
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +02002268#ifdef arch_scale_freq_capacity
2269#ifndef arch_scale_freq_invariant
2270#define arch_scale_freq_invariant() (true)
2271#endif
2272#else /* arch_scale_freq_capacity */
2273#define arch_scale_freq_invariant() (false)
2274#endif
Vikram Mulukutlad056dbc2017-02-07 18:58:07 -08002275
Joonwoo Parkf7d6cd42017-01-17 15:19:43 -08002276#ifdef CONFIG_SCHED_WALT
Vikram Mulukutlad056dbc2017-02-07 18:58:07 -08002277
2278static inline int cluster_first_cpu(struct sched_cluster *cluster)
2279{
2280 return cpumask_first(&cluster->cpus);
2281}
2282
2283struct related_thread_group {
2284 int id;
2285 raw_spinlock_t lock;
2286 struct list_head tasks;
2287 struct list_head list;
2288 struct sched_cluster *preferred_cluster;
2289 struct rcu_head rcu;
2290 u64 last_update;
2291};
2292
2293extern struct list_head cluster_head;
2294extern int num_clusters;
2295extern struct sched_cluster *sched_cluster[NR_CPUS];
2296
2297#define for_each_sched_cluster(cluster) \
2298 list_for_each_entry_rcu(cluster, &cluster_head, list)
2299
2300#define WINDOW_STATS_RECENT 0
2301#define WINDOW_STATS_MAX 1
2302#define WINDOW_STATS_MAX_RECENT_AVG 2
2303#define WINDOW_STATS_AVG 3
2304#define WINDOW_STATS_INVALID_POLICY 4
2305
2306#define SCHED_UPMIGRATE_MIN_NICE 15
2307#define EXITING_TASK_MARKER 0xdeaddead
2308
2309#define UP_MIGRATION 1
2310#define DOWN_MIGRATION 2
2311#define IRQLOAD_MIGRATION 3
2312
2313extern struct mutex policy_mutex;
Vikram Mulukutlad056dbc2017-02-07 18:58:07 -08002314extern unsigned int sched_disable_window_stats;
2315extern unsigned int max_possible_freq;
2316extern unsigned int min_max_freq;
Vikram Mulukutlad056dbc2017-02-07 18:58:07 -08002317extern unsigned int max_possible_efficiency;
2318extern unsigned int min_possible_efficiency;
2319extern unsigned int max_capacity;
2320extern unsigned int min_capacity;
2321extern unsigned int max_load_scale_factor;
2322extern unsigned int max_possible_capacity;
2323extern unsigned int min_max_possible_capacity;
2324extern unsigned int max_power_cost;
2325extern unsigned int sched_init_task_load_windows;
2326extern unsigned int up_down_migrate_scale_factor;
2327extern unsigned int sysctl_sched_restrict_cluster_spill;
2328extern unsigned int sched_pred_alert_load;
2329extern struct sched_cluster init_cluster;
2330extern unsigned int __read_mostly sched_short_sleep_task_threshold;
2331extern unsigned int __read_mostly sched_long_cpu_selection_threshold;
2332extern unsigned int __read_mostly sched_big_waker_task_load;
2333extern unsigned int __read_mostly sched_small_wakee_task_load;
2334extern unsigned int __read_mostly sched_spill_load;
2335extern unsigned int __read_mostly sched_upmigrate;
2336extern unsigned int __read_mostly sched_downmigrate;
2337extern unsigned int __read_mostly sysctl_sched_spill_nr_run;
2338extern unsigned int __read_mostly sched_load_granule;
2339
Vikram Mulukutlad056dbc2017-02-07 18:58:07 -08002340extern int register_cpu_cycle_counter_cb(struct cpu_cycle_counter_cb *cb);
Vikram Mulukutlad056dbc2017-02-07 18:58:07 -08002341extern int update_preferred_cluster(struct related_thread_group *grp,
2342 struct task_struct *p, u32 old_load);
2343extern void set_preferred_cluster(struct related_thread_group *grp);
2344extern void add_new_task_to_grp(struct task_struct *new);
2345extern unsigned int update_freq_aggregate_threshold(unsigned int threshold);
Vikram Mulukutlad056dbc2017-02-07 18:58:07 -08002346
2347#define NO_BOOST 0
2348#define FULL_THROTTLE_BOOST 1
2349#define CONSERVATIVE_BOOST 2
2350#define RESTRAINED_BOOST 3
2351
Vikram Mulukutlad056dbc2017-02-07 18:58:07 -08002352static inline int cpu_capacity(int cpu)
2353{
2354 return cpu_rq(cpu)->cluster->capacity;
2355}
2356
2357static inline int cpu_max_possible_capacity(int cpu)
2358{
2359 return cpu_rq(cpu)->cluster->max_possible_capacity;
2360}
2361
2362static inline int cpu_load_scale_factor(int cpu)
2363{
2364 return cpu_rq(cpu)->cluster->load_scale_factor;
2365}
2366
2367static inline int cpu_efficiency(int cpu)
2368{
2369 return cpu_rq(cpu)->cluster->efficiency;
2370}
2371
Vikram Mulukutlad056dbc2017-02-07 18:58:07 -08002372static inline unsigned int cpu_min_freq(int cpu)
2373{
2374 return cpu_rq(cpu)->cluster->min_freq;
2375}
2376
2377static inline unsigned int cluster_max_freq(struct sched_cluster *cluster)
2378{
2379 /*
2380 * Governor and thermal driver don't know the other party's mitigation
2381 * voting. So struct cluster saves both and return min() for current
2382 * cluster fmax.
2383 */
2384 return min(cluster->max_mitigated_freq, cluster->max_freq);
2385}
2386
2387static inline unsigned int cpu_max_freq(int cpu)
2388{
2389 return cluster_max_freq(cpu_rq(cpu)->cluster);
2390}
2391
2392static inline unsigned int cpu_max_possible_freq(int cpu)
2393{
2394 return cpu_rq(cpu)->cluster->max_possible_freq;
2395}
2396
Joonwoo Parkf7d6cd42017-01-17 15:19:43 -08002397/* Keep track of max/min capacity possible across CPUs "currently" */
2398static inline void __update_min_max_capacity(void)
Vikram Mulukutlad056dbc2017-02-07 18:58:07 -08002399{
Joonwoo Parkf7d6cd42017-01-17 15:19:43 -08002400 int i;
2401 int max_cap = 0, min_cap = INT_MAX;
2402
2403 for_each_online_cpu(i) {
2404 max_cap = max(max_cap, cpu_capacity(i));
2405 min_cap = min(min_cap, cpu_capacity(i));
2406 }
2407
2408 max_capacity = max_cap;
2409 min_capacity = min_cap;
2410}
2411
2412/*
2413 * Return load_scale_factor of a cpu in reference to "most" efficient cpu, so
2414 * that "most" efficient cpu gets a load_scale_factor of 1
2415 */
2416static inline unsigned long
2417load_scale_cpu_efficiency(struct sched_cluster *cluster)
2418{
2419 return DIV_ROUND_UP(1024 * max_possible_efficiency,
2420 cluster->efficiency);
2421}
2422
2423/*
2424 * Return load_scale_factor of a cpu in reference to cpu with best max_freq
2425 * (max_possible_freq), so that one with best max_freq gets a load_scale_factor
2426 * of 1.
2427 */
2428static inline unsigned long load_scale_cpu_freq(struct sched_cluster *cluster)
2429{
2430 return DIV_ROUND_UP(1024 * max_possible_freq,
2431 cluster_max_freq(cluster));
2432}
2433
2434static inline int compute_load_scale_factor(struct sched_cluster *cluster)
2435{
2436 int load_scale = 1024;
2437
2438 /*
2439 * load_scale_factor accounts for the fact that task load
2440 * is in reference to "best" performing cpu. Task's load will need to be
2441 * scaled (up) by a factor to determine suitability to be placed on a
2442 * (little) cpu.
2443 */
2444 load_scale *= load_scale_cpu_efficiency(cluster);
2445 load_scale >>= 10;
2446
2447 load_scale *= load_scale_cpu_freq(cluster);
2448 load_scale >>= 10;
2449
2450 return load_scale;
Vikram Mulukutlad056dbc2017-02-07 18:58:07 -08002451}
2452
2453static inline int cpu_max_power_cost(int cpu)
2454{
2455 return cpu_rq(cpu)->cluster->max_power_cost;
2456}
2457
2458static inline int cpu_min_power_cost(int cpu)
2459{
2460 return cpu_rq(cpu)->cluster->min_power_cost;
2461}
2462
Vikram Mulukutlad056dbc2017-02-07 18:58:07 -08002463static inline bool hmp_capable(void)
2464{
2465 return max_possible_capacity != min_max_possible_capacity;
2466}
2467
Pavankumar Kondeti005309282017-05-10 15:43:29 +05302468static inline bool is_max_capacity_cpu(int cpu)
2469{
2470 return cpu_max_possible_capacity(cpu) == max_possible_capacity;
2471}
2472
Vikram Mulukutlad056dbc2017-02-07 18:58:07 -08002473/*
2474 * 'load' is in reference to "best cpu" at its best frequency.
2475 * Scale that in reference to a given cpu, accounting for how bad it is
2476 * in reference to "best cpu".
2477 */
2478static inline u64 scale_load_to_cpu(u64 task_load, int cpu)
2479{
2480 u64 lsf = cpu_load_scale_factor(cpu);
2481
2482 if (lsf != 1024) {
2483 task_load *= lsf;
2484 task_load /= 1024;
2485 }
2486
2487 return task_load;
2488}
2489
Joonwoo Parkf7d6cd42017-01-17 15:19:43 -08002490/*
2491 * Return 'capacity' of a cpu in reference to "least" efficient cpu, such that
2492 * least efficient cpu gets capacity of 1024
2493 */
2494static unsigned long
2495capacity_scale_cpu_efficiency(struct sched_cluster *cluster)
2496{
2497 return (1024 * cluster->efficiency) / min_possible_efficiency;
2498}
2499
2500/*
2501 * Return 'capacity' of a cpu in reference to cpu with lowest max_freq
2502 * (min_max_freq), such that one with lowest max_freq gets capacity of 1024.
2503 */
2504static unsigned long capacity_scale_cpu_freq(struct sched_cluster *cluster)
2505{
2506 return (1024 * cluster_max_freq(cluster)) / min_max_freq;
2507}
2508
2509static inline int compute_capacity(struct sched_cluster *cluster)
2510{
2511 int capacity = 1024;
2512
2513 capacity *= capacity_scale_cpu_efficiency(cluster);
2514 capacity >>= 10;
2515
2516 capacity *= capacity_scale_cpu_freq(cluster);
2517 capacity >>= 10;
2518
2519 return capacity;
2520}
2521
Vikram Mulukutlad056dbc2017-02-07 18:58:07 -08002522static inline unsigned int task_load(struct task_struct *p)
2523{
2524 return p->ravg.demand;
2525}
2526
Pavankumar Kondetife1a6962017-07-25 11:08:17 +05302527static inline unsigned int task_pl(struct task_struct *p)
2528{
2529 return p->ravg.pred_demand;
2530}
2531
Vikram Mulukutlad056dbc2017-02-07 18:58:07 -08002532#define pct_to_real(tunable) \
2533 (div64_u64((u64)tunable * (u64)max_task_load(), 100))
2534
2535#define real_to_pct(tunable) \
2536 (div64_u64((u64)tunable * (u64)100, (u64)max_task_load()))
2537
Vikram Mulukutlad056dbc2017-02-07 18:58:07 -08002538static inline bool task_in_related_thread_group(struct task_struct *p)
2539{
2540 return !!(rcu_access_pointer(p->grp) != NULL);
2541}
2542
2543static inline
2544struct related_thread_group *task_related_thread_group(struct task_struct *p)
2545{
2546 return rcu_dereference(p->grp);
2547}
2548
2549#define PRED_DEMAND_DELTA ((s64)new_pred_demand - p->ravg.pred_demand)
2550
Vikram Mulukutlad056dbc2017-02-07 18:58:07 -08002551/* Is frequency of two cpus synchronized with each other? */
2552static inline int same_freq_domain(int src_cpu, int dst_cpu)
2553{
2554 struct rq *rq = cpu_rq(src_cpu);
2555
2556 if (src_cpu == dst_cpu)
2557 return 1;
2558
2559 return cpumask_test_cpu(dst_cpu, &rq->freq_domain_cpumask);
2560}
2561
2562#define BOOST_KICK 0
2563#define CPU_RESERVED 1
2564
Vikram Mulukutlad056dbc2017-02-07 18:58:07 -08002565extern int sched_boost(void);
Vikram Mulukutlad056dbc2017-02-07 18:58:07 -08002566extern int preferred_cluster(struct sched_cluster *cluster,
2567 struct task_struct *p);
Pavankumar Kondeti84f72d72017-07-20 11:00:45 +05302568extern void inc_rq_walt_stats(struct rq *rq,
Vikram Mulukutlad056dbc2017-02-07 18:58:07 -08002569 struct task_struct *p, int change_cra);
Pavankumar Kondeti84f72d72017-07-20 11:00:45 +05302570extern void dec_rq_walt_stats(struct rq *rq,
Vikram Mulukutlad056dbc2017-02-07 18:58:07 -08002571 struct task_struct *p, int change_cra);
Vikram Mulukutlad056dbc2017-02-07 18:58:07 -08002572extern struct sched_cluster *rq_cluster(struct rq *rq);
Vikram Mulukutlad056dbc2017-02-07 18:58:07 -08002573extern void reset_task_stats(struct task_struct *p);
Vikram Mulukutlad056dbc2017-02-07 18:58:07 -08002574extern void clear_top_tasks_bitmap(unsigned long *bitmap);
2575
2576#if defined(CONFIG_SCHED_TUNE) && defined(CONFIG_CGROUP_SCHEDTUNE)
2577extern bool task_sched_boost(struct task_struct *p);
2578extern int sync_cgroup_colocation(struct task_struct *p, bool insert);
2579extern bool same_schedtune(struct task_struct *tsk1, struct task_struct *tsk2);
2580extern void update_cgroup_boost_settings(void);
2581extern void restore_cgroup_boost_settings(void);
2582
2583#else
2584static inline bool
2585same_schedtune(struct task_struct *tsk1, struct task_struct *tsk2)
2586{
2587 return true;
2588}
2589
2590static inline bool task_sched_boost(struct task_struct *p)
2591{
2592 return true;
2593}
2594
2595static inline void update_cgroup_boost_settings(void) { }
2596static inline void restore_cgroup_boost_settings(void) { }
2597#endif
2598
2599extern int alloc_related_thread_groups(void);
2600
2601extern unsigned long all_cluster_ids[];
2602
Joonwoo Parke77a2012016-12-06 18:12:43 -08002603extern void check_for_migration(struct rq *rq, struct task_struct *p);
2604
2605static inline int is_reserved(int cpu)
2606{
2607 struct rq *rq = cpu_rq(cpu);
2608
Pavankumar Kondeti84f72d72017-07-20 11:00:45 +05302609 return test_bit(CPU_RESERVED, &rq->walt_flags);
Joonwoo Parke77a2012016-12-06 18:12:43 -08002610}
2611
2612static inline int mark_reserved(int cpu)
2613{
2614 struct rq *rq = cpu_rq(cpu);
2615
Pavankumar Kondeti84f72d72017-07-20 11:00:45 +05302616 return test_and_set_bit(CPU_RESERVED, &rq->walt_flags);
Joonwoo Parke77a2012016-12-06 18:12:43 -08002617}
2618
2619static inline void clear_reserved(int cpu)
2620{
2621 struct rq *rq = cpu_rq(cpu);
2622
Pavankumar Kondeti84f72d72017-07-20 11:00:45 +05302623 clear_bit(CPU_RESERVED, &rq->walt_flags);
Joonwoo Parke77a2012016-12-06 18:12:43 -08002624}
2625
Joonwoo Park84a80882017-02-03 11:15:31 -08002626static inline bool
2627__task_in_cum_window_demand(struct rq *rq, struct task_struct *p)
2628{
2629 return (p->on_rq || p->last_sleep_ts >= rq->window_start);
2630}
2631
2632static inline bool
2633task_in_cum_window_demand(struct rq *rq, struct task_struct *p)
2634{
2635 return cpu_of(rq) == task_cpu(p) && __task_in_cum_window_demand(rq, p);
2636}
2637
2638static inline void
2639dec_cum_window_demand(struct rq *rq, struct task_struct *p)
2640{
2641 rq->cum_window_demand -= p->ravg.demand;
2642 WARN_ON_ONCE(rq->cum_window_demand < 0);
2643}
2644
2645static inline void
2646inc_cum_window_demand(struct rq *rq, struct task_struct *p, s64 delta)
2647{
2648 rq->cum_window_demand += delta;
2649}
2650
Vikram Mulukutlad0ba1882017-02-03 12:56:26 -08002651extern void update_cpu_cluster_capacity(const cpumask_t *cpus);
2652
2653extern unsigned long thermal_cap(int cpu);
2654
Pavankumar Kondeti84f72d72017-07-20 11:00:45 +05302655extern void clear_walt_request(int cpu);
Syed Rameez Mustafa20acfe72017-01-30 09:35:46 +05302656
2657extern int got_boost_kick(void);
2658extern void clear_boost_kick(int cpu);
2659extern enum sched_boost_policy sched_boost_policy(void);
2660extern void sched_boost_parse_dt(void);
Syed Rameez Mustafa25de0112017-05-10 12:09:15 -07002661extern void clear_ed_task(struct task_struct *p, struct rq *rq);
2662extern bool early_detection_notify(struct rq *rq, u64 wallclock);
Syed Rameez Mustafa20acfe72017-01-30 09:35:46 +05302663
Pavankumar Kondetif9026cd2017-06-07 15:03:32 +05302664static inline unsigned int power_cost(int cpu, u64 demand)
2665{
2666 return cpu_max_possible_capacity(cpu);
2667}
Pavankumar Kondetif9026cd2017-06-07 15:03:32 +05302668
Joonwoo Parkf7d6cd42017-01-17 15:19:43 -08002669#else /* CONFIG_SCHED_WALT */
Vikram Mulukutlad056dbc2017-02-07 18:58:07 -08002670
Pavankumar Kondeti84f72d72017-07-20 11:00:45 +05302671struct walt_sched_stats;
Vikram Mulukutlad056dbc2017-02-07 18:58:07 -08002672struct related_thread_group;
2673struct sched_cluster;
2674
Vikram Mulukutlad056dbc2017-02-07 18:58:07 -08002675static inline bool task_sched_boost(struct task_struct *p)
2676{
2677 return true;
2678}
2679
Joonwoo Parke77a2012016-12-06 18:12:43 -08002680static inline void check_for_migration(struct rq *rq, struct task_struct *p) { }
2681
Vikram Mulukutlad056dbc2017-02-07 18:58:07 -08002682static inline int sched_boost(void)
2683{
2684 return 0;
2685}
2686
Pavankumar Kondeti005309282017-05-10 15:43:29 +05302687static inline bool is_max_capacity_cpu(int cpu) { return true; }
2688
Vikram Mulukutlad056dbc2017-02-07 18:58:07 -08002689static inline void
Pavankumar Kondeti84f72d72017-07-20 11:00:45 +05302690inc_rq_walt_stats(struct rq *rq, struct task_struct *p, int change_cra) { }
Vikram Mulukutlad056dbc2017-02-07 18:58:07 -08002691
2692static inline void
Pavankumar Kondeti84f72d72017-07-20 11:00:45 +05302693dec_rq_walt_stats(struct rq *rq, struct task_struct *p, int change_cra) { }
Vikram Mulukutlad056dbc2017-02-07 18:58:07 -08002694
2695static inline int
2696preferred_cluster(struct sched_cluster *cluster, struct task_struct *p)
2697{
2698 return 1;
2699}
2700
2701static inline struct sched_cluster *rq_cluster(struct rq *rq)
2702{
2703 return NULL;
2704}
2705
Vikram Mulukutlad056dbc2017-02-07 18:58:07 -08002706static inline u64 scale_load_to_cpu(u64 load, int cpu)
2707{
2708 return load;
2709}
2710
Vikram Mulukutlad056dbc2017-02-07 18:58:07 -08002711static inline int cpu_capacity(int cpu)
2712{
2713 return SCHED_CAPACITY_SCALE;
2714}
2715
Vikram Mulukutlad056dbc2017-02-07 18:58:07 -08002716static inline void set_preferred_cluster(struct related_thread_group *grp) { }
2717
2718static inline bool task_in_related_thread_group(struct task_struct *p)
2719{
2720 return false;
2721}
2722
2723static inline
2724struct related_thread_group *task_related_thread_group(struct task_struct *p)
2725{
2726 return NULL;
2727}
2728
2729static inline u32 task_load(struct task_struct *p) { return 0; }
Pavankumar Kondetife1a6962017-07-25 11:08:17 +05302730static inline u32 task_pl(struct task_struct *p) { return 0; }
Vikram Mulukutlad056dbc2017-02-07 18:58:07 -08002731
2732static inline int update_preferred_cluster(struct related_thread_group *grp,
2733 struct task_struct *p, u32 old_load)
2734{
2735 return 0;
2736}
2737
2738static inline void add_new_task_to_grp(struct task_struct *new) {}
2739
2740#define PRED_DEMAND_DELTA (0)
2741
Vikram Mulukutlad056dbc2017-02-07 18:58:07 -08002742static inline int same_freq_domain(int src_cpu, int dst_cpu)
2743{
2744 return 1;
2745}
2746
Vikram Mulukutlad056dbc2017-02-07 18:58:07 -08002747static inline void clear_reserved(int cpu) { }
Vikram Mulukutlad056dbc2017-02-07 18:58:07 -08002748static inline int alloc_related_thread_groups(void) { return 0; }
2749
2750#define trace_sched_cpu_load(...)
2751#define trace_sched_cpu_load_lb(...)
2752#define trace_sched_cpu_load_cgroup(...)
2753#define trace_sched_cpu_load_wakeup(...)
2754
Joonwoo Park84a80882017-02-03 11:15:31 -08002755static inline bool
2756__task_in_cum_window_demand(struct rq *rq, struct task_struct *p)
2757{
2758 return 0;
2759}
2760
2761static inline void
2762dec_cum_window_demand(struct rq *rq, struct task_struct *p) { }
2763
2764static inline void
2765inc_cum_window_demand(struct rq *rq, struct task_struct *p, s64 delta) { }
2766
Vikram Mulukutlad0ba1882017-02-03 12:56:26 -08002767static inline void update_cpu_cluster_capacity(const cpumask_t *cpus) { }
2768
2769#ifdef CONFIG_SMP
2770static inline unsigned long thermal_cap(int cpu)
2771{
2772 return cpu_rq(cpu)->cpu_capacity_orig;
2773}
2774#endif
2775
Pavankumar Kondeti84f72d72017-07-20 11:00:45 +05302776static inline void clear_walt_request(int cpu) { }
Syed Rameez Mustafa20acfe72017-01-30 09:35:46 +05302777
2778static inline int got_boost_kick(void)
2779{
2780 return 0;
2781}
2782
2783static inline void clear_boost_kick(int cpu) { }
2784
2785static inline enum sched_boost_policy sched_boost_policy(void)
2786{
2787 return SCHED_BOOST_NONE;
2788}
2789
2790static inline void sched_boost_parse_dt(void) { }
2791
Syed Rameez Mustafa25de0112017-05-10 12:09:15 -07002792static inline void clear_ed_task(struct task_struct *p, struct rq *rq) { }
2793
2794static inline bool early_detection_notify(struct rq *rq, u64 wallclock)
2795{
2796 return 0;
2797}
2798
Pavankumar Kondetif9026cd2017-06-07 15:03:32 +05302799static inline unsigned int power_cost(int cpu, u64 demand)
2800{
2801 return SCHED_CAPACITY_SCALE;
2802}
2803
Joonwoo Parkf7d6cd42017-01-17 15:19:43 -08002804#endif /* CONFIG_SCHED_WALT */
Vikram Mulukutlad056dbc2017-02-07 18:58:07 -08002805
Joonwoo Parkc5ddd4a2017-01-12 17:56:46 -08002806static inline bool energy_aware(void)
2807{
2808 return sched_feat(ENERGY_AWARE);
2809}