Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 2 | |
| 3 | #include <linux/sched.h> |
Ingo Molnar | dfc3401 | 2017-02-03 23:15:21 +0100 | [diff] [blame] | 4 | #include <linux/sched/autogroup.h> |
Clark Williams | cf4aebc2 | 2013-02-07 09:46:59 -0600 | [diff] [blame] | 5 | #include <linux/sched/sysctl.h> |
Ingo Molnar | 105ab3d | 2017-02-01 16:36:40 +0100 | [diff] [blame] | 6 | #include <linux/sched/topology.h> |
Clark Williams | 8bd75c7 | 2013-02-07 09:47:07 -0600 | [diff] [blame] | 7 | #include <linux/sched/rt.h> |
Ingo Molnar | ef8bd77 | 2017-02-08 18:51:36 +0100 | [diff] [blame] | 8 | #include <linux/sched/deadline.h> |
Ingo Molnar | e601757 | 2017-02-01 16:36:40 +0100 | [diff] [blame] | 9 | #include <linux/sched/clock.h> |
Ingo Molnar | 84f001e | 2017-02-01 16:36:40 +0100 | [diff] [blame] | 10 | #include <linux/sched/wake_q.h> |
Ingo Molnar | 3f07c01 | 2017-02-08 18:51:30 +0100 | [diff] [blame] | 11 | #include <linux/sched/signal.h> |
Ingo Molnar | 6a3827d | 2017-02-08 18:51:31 +0100 | [diff] [blame] | 12 | #include <linux/sched/numa_balancing.h> |
Ingo Molnar | 6e84f31 | 2017-02-08 18:51:29 +0100 | [diff] [blame] | 13 | #include <linux/sched/mm.h> |
Ingo Molnar | 55687da | 2017-02-08 18:51:31 +0100 | [diff] [blame] | 14 | #include <linux/sched/cpufreq.h> |
Ingo Molnar | 03441a3 | 2017-02-08 18:51:35 +0100 | [diff] [blame] | 15 | #include <linux/sched/stat.h> |
Ingo Molnar | 370c913 | 2017-02-08 18:51:35 +0100 | [diff] [blame] | 16 | #include <linux/sched/nohz.h> |
Ingo Molnar | b17b015 | 2017-02-08 18:51:35 +0100 | [diff] [blame] | 17 | #include <linux/sched/debug.h> |
Ingo Molnar | ef8bd77 | 2017-02-08 18:51:36 +0100 | [diff] [blame] | 18 | #include <linux/sched/hotplug.h> |
Ingo Molnar | 2993002 | 2017-02-08 18:51:36 +0100 | [diff] [blame] | 19 | #include <linux/sched/task.h> |
Ingo Molnar | 68db0cf | 2017-02-08 18:51:37 +0100 | [diff] [blame] | 20 | #include <linux/sched/task_stack.h> |
Ingo Molnar | 32ef551 | 2017-02-05 11:48:36 +0100 | [diff] [blame] | 21 | #include <linux/sched/cputime.h> |
Ingo Molnar | 1777e46 | 2017-02-05 14:47:12 +0100 | [diff] [blame] | 22 | #include <linux/sched/init.h> |
Ingo Molnar | ef8bd77 | 2017-02-08 18:51:36 +0100 | [diff] [blame] | 23 | |
Frederic Weisbecker | 19d23dbf | 2016-09-26 02:29:20 +0200 | [diff] [blame] | 24 | #include <linux/u64_stats_sync.h> |
Frederic Weisbecker | a499a5a | 2017-01-31 04:09:32 +0100 | [diff] [blame] | 25 | #include <linux/kernel_stat.h> |
Steven Rostedt (Red Hat) | 3866e84 | 2016-02-22 16:26:51 -0500 | [diff] [blame] | 26 | #include <linux/binfmts.h> |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 27 | #include <linux/mutex.h> |
| 28 | #include <linux/spinlock.h> |
| 29 | #include <linux/stop_machine.h> |
Steven Rostedt | b6366f0 | 2015-03-18 14:49:46 -0400 | [diff] [blame] | 30 | #include <linux/irq_work.h> |
Frederic Weisbecker | 9f3660c | 2013-04-20 14:35:09 +0200 | [diff] [blame] | 31 | #include <linux/tick.h> |
Mel Gorman | f809ca9 | 2013-10-07 11:28:57 +0100 | [diff] [blame] | 32 | #include <linux/slab.h> |
Tejun Heo | d2cc5ed | 2017-09-25 08:12:04 -0700 | [diff] [blame] | 33 | #include <linux/cgroup.h> |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 34 | |
Ingo Molnar | 7fce777 | 2017-02-02 14:47:27 +0100 | [diff] [blame] | 35 | #ifdef CONFIG_PARAVIRT |
| 36 | #include <asm/paravirt.h> |
| 37 | #endif |
| 38 | |
Peter Zijlstra | 391e43d | 2011-11-15 17:14:39 +0100 | [diff] [blame] | 39 | #include "cpupri.h" |
Juri Lelli | 6bfd6d7 | 2013-11-07 14:43:47 +0100 | [diff] [blame] | 40 | #include "cpudeadline.h" |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 41 | |
Peter Zijlstra | 9148a3a | 2016-09-20 22:34:51 +0200 | [diff] [blame] | 42 | #ifdef CONFIG_SCHED_DEBUG |
Ingo Molnar | 6d3aed3 | 2017-06-20 12:24:42 +0200 | [diff] [blame] | 43 | # define SCHED_WARN_ON(x) WARN_ONCE(x, #x) |
Peter Zijlstra | 9148a3a | 2016-09-20 22:34:51 +0200 | [diff] [blame] | 44 | #else |
Ingo Molnar | 6d3aed3 | 2017-06-20 12:24:42 +0200 | [diff] [blame] | 45 | # define SCHED_WARN_ON(x) ({ (void)(x), 0; }) |
Peter Zijlstra | 9148a3a | 2016-09-20 22:34:51 +0200 | [diff] [blame] | 46 | #endif |
| 47 | |
Paul Gortmaker | 45ceebf | 2013-04-19 15:10:49 -0400 | [diff] [blame] | 48 | struct rq; |
Daniel Lezcano | 442bf3a | 2014-09-04 11:32:09 -0400 | [diff] [blame] | 49 | struct cpuidle_state; |
Paul Gortmaker | 45ceebf | 2013-04-19 15:10:49 -0400 | [diff] [blame] | 50 | |
Kirill Tkhai | da0c1e6 | 2014-08-20 13:47:32 +0400 | [diff] [blame] | 51 | /* task_struct::on_rq states: */ |
| 52 | #define TASK_ON_RQ_QUEUED 1 |
Kirill Tkhai | cca26e8 | 2014-08-20 13:47:42 +0400 | [diff] [blame] | 53 | #define TASK_ON_RQ_MIGRATING 2 |
Kirill Tkhai | da0c1e6 | 2014-08-20 13:47:32 +0400 | [diff] [blame] | 54 | |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 55 | extern __read_mostly int scheduler_running; |
| 56 | |
Paul Gortmaker | 45ceebf | 2013-04-19 15:10:49 -0400 | [diff] [blame] | 57 | extern unsigned long calc_load_update; |
| 58 | extern atomic_long_t calc_load_tasks; |
| 59 | |
Peter Zijlstra | 3289bdb | 2015-04-14 13:19:42 +0200 | [diff] [blame] | 60 | extern void calc_global_load_tick(struct rq *this_rq); |
Thomas Gleixner | d60585c | 2016-07-12 18:33:56 +0200 | [diff] [blame] | 61 | extern long calc_load_fold_active(struct rq *this_rq, long adjust); |
Peter Zijlstra | 3289bdb | 2015-04-14 13:19:42 +0200 | [diff] [blame] | 62 | |
| 63 | #ifdef CONFIG_SMP |
Frederic Weisbecker | cee1afc | 2016-04-13 15:56:50 +0200 | [diff] [blame] | 64 | extern void cpu_load_update_active(struct rq *this_rq); |
Peter Zijlstra | 3289bdb | 2015-04-14 13:19:42 +0200 | [diff] [blame] | 65 | #else |
Frederic Weisbecker | cee1afc | 2016-04-13 15:56:50 +0200 | [diff] [blame] | 66 | static inline void cpu_load_update_active(struct rq *this_rq) { } |
Peter Zijlstra | 3289bdb | 2015-04-14 13:19:42 +0200 | [diff] [blame] | 67 | #endif |
Paul Gortmaker | 45ceebf | 2013-04-19 15:10:49 -0400 | [diff] [blame] | 68 | |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 69 | /* |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 70 | * Helpers for converting nanosecond timing to jiffy resolution |
| 71 | */ |
| 72 | #define NS_TO_JIFFIES(TIME) ((unsigned long)(TIME) / (NSEC_PER_SEC / HZ)) |
| 73 | |
Li Zefan | cc1f4b1 | 2013-03-05 16:06:09 +0800 | [diff] [blame] | 74 | /* |
| 75 | * Increase resolution of nice-level calculations for 64-bit architectures. |
| 76 | * The extra resolution improves shares distribution and load balancing of |
| 77 | * low-weight task groups (eg. nice +19 on an autogroup), deeper taskgroup |
| 78 | * hierarchies, especially on larger systems. This is not a user-visible change |
| 79 | * and does not change the user-interface for setting shares/weights. |
| 80 | * |
| 81 | * We increase resolution only if we have enough bits to allow this increased |
Peter Zijlstra | 2159197 | 2016-04-28 12:49:38 +0200 | [diff] [blame] | 82 | * resolution (i.e. 64bit). The costs for increasing resolution when 32bit are |
| 83 | * pretty high and the returns do not justify the increased costs. |
| 84 | * |
| 85 | * Really only required when CONFIG_FAIR_GROUP_SCHED is also set, but to |
| 86 | * increase coverage and consistency always enable it on 64bit platforms. |
Li Zefan | cc1f4b1 | 2013-03-05 16:06:09 +0800 | [diff] [blame] | 87 | */ |
Peter Zijlstra | 2159197 | 2016-04-28 12:49:38 +0200 | [diff] [blame] | 88 | #ifdef CONFIG_64BIT |
Yuyang Du | 172895e | 2016-04-05 12:12:27 +0800 | [diff] [blame] | 89 | # define NICE_0_LOAD_SHIFT (SCHED_FIXEDPOINT_SHIFT + SCHED_FIXEDPOINT_SHIFT) |
Yuyang Du | 6ecdd74 | 2016-04-05 12:12:26 +0800 | [diff] [blame] | 90 | # define scale_load(w) ((w) << SCHED_FIXEDPOINT_SHIFT) |
| 91 | # define scale_load_down(w) ((w) >> SCHED_FIXEDPOINT_SHIFT) |
Li Zefan | cc1f4b1 | 2013-03-05 16:06:09 +0800 | [diff] [blame] | 92 | #else |
Yuyang Du | 172895e | 2016-04-05 12:12:27 +0800 | [diff] [blame] | 93 | # define NICE_0_LOAD_SHIFT (SCHED_FIXEDPOINT_SHIFT) |
Li Zefan | cc1f4b1 | 2013-03-05 16:06:09 +0800 | [diff] [blame] | 94 | # define scale_load(w) (w) |
| 95 | # define scale_load_down(w) (w) |
| 96 | #endif |
| 97 | |
Yuyang Du | 6ecdd74 | 2016-04-05 12:12:26 +0800 | [diff] [blame] | 98 | /* |
Yuyang Du | 172895e | 2016-04-05 12:12:27 +0800 | [diff] [blame] | 99 | * Task weight (visible to users) and its load (invisible to users) have |
| 100 | * independent resolution, but they should be well calibrated. We use |
| 101 | * scale_load() and scale_load_down(w) to convert between them. The |
| 102 | * following must be true: |
| 103 | * |
| 104 | * scale_load(sched_prio_to_weight[USER_PRIO(NICE_TO_PRIO(0))]) == NICE_0_LOAD |
| 105 | * |
Yuyang Du | 6ecdd74 | 2016-04-05 12:12:26 +0800 | [diff] [blame] | 106 | */ |
Yuyang Du | 172895e | 2016-04-05 12:12:27 +0800 | [diff] [blame] | 107 | #define NICE_0_LOAD (1L << NICE_0_LOAD_SHIFT) |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 108 | |
| 109 | /* |
Dario Faggioli | 332ac17 | 2013-11-07 14:43:45 +0100 | [diff] [blame] | 110 | * Single value that decides SCHED_DEADLINE internal math precision. |
| 111 | * 10 -> just above 1us |
| 112 | * 9 -> just above 0.5us |
| 113 | */ |
| 114 | #define DL_SCALE (10) |
| 115 | |
| 116 | /* |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 117 | * These are the 'tuning knobs' of the scheduler: |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 118 | */ |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 119 | |
| 120 | /* |
| 121 | * single value that denotes runtime == period, ie unlimited time. |
| 122 | */ |
| 123 | #define RUNTIME_INF ((u64)~0ULL) |
| 124 | |
Henrik Austad | 20f9cd2 | 2015-09-09 17:00:41 +0200 | [diff] [blame] | 125 | static inline int idle_policy(int policy) |
| 126 | { |
| 127 | return policy == SCHED_IDLE; |
| 128 | } |
Dario Faggioli | d50dde5 | 2013-11-07 14:43:36 +0100 | [diff] [blame] | 129 | static inline int fair_policy(int policy) |
| 130 | { |
| 131 | return policy == SCHED_NORMAL || policy == SCHED_BATCH; |
| 132 | } |
| 133 | |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 134 | static inline int rt_policy(int policy) |
| 135 | { |
Dario Faggioli | d50dde5 | 2013-11-07 14:43:36 +0100 | [diff] [blame] | 136 | return policy == SCHED_FIFO || policy == SCHED_RR; |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 137 | } |
| 138 | |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 139 | static inline int dl_policy(int policy) |
| 140 | { |
| 141 | return policy == SCHED_DEADLINE; |
| 142 | } |
Henrik Austad | 20f9cd2 | 2015-09-09 17:00:41 +0200 | [diff] [blame] | 143 | static inline bool valid_policy(int policy) |
| 144 | { |
| 145 | return idle_policy(policy) || fair_policy(policy) || |
| 146 | rt_policy(policy) || dl_policy(policy); |
| 147 | } |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 148 | |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 149 | static inline int task_has_rt_policy(struct task_struct *p) |
| 150 | { |
| 151 | return rt_policy(p->policy); |
| 152 | } |
| 153 | |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 154 | static inline int task_has_dl_policy(struct task_struct *p) |
| 155 | { |
| 156 | return dl_policy(p->policy); |
| 157 | } |
| 158 | |
Juri Lelli | 0788116 | 2017-12-04 11:23:25 +0100 | [diff] [blame] | 159 | #define cap_scale(v, s) ((v)*(s) >> SCHED_CAPACITY_SHIFT) |
| 160 | |
Dario Faggioli | 2d3d891 | 2013-11-07 14:43:44 +0100 | [diff] [blame] | 161 | /* |
Juri Lelli | 794a56e | 2017-12-04 11:23:20 +0100 | [diff] [blame] | 162 | * !! For sched_setattr_nocheck() (kernel) only !! |
| 163 | * |
| 164 | * This is actually gross. :( |
| 165 | * |
| 166 | * It is used to make schedutil kworker(s) higher priority than SCHED_DEADLINE |
| 167 | * tasks, but still be able to sleep. We need this on platforms that cannot |
| 168 | * atomically change clock frequency. Remove once fast switching will be |
| 169 | * available on such platforms. |
| 170 | * |
| 171 | * SUGOV stands for SchedUtil GOVernor. |
| 172 | */ |
| 173 | #define SCHED_FLAG_SUGOV 0x10000000 |
| 174 | |
| 175 | static inline bool dl_entity_is_special(struct sched_dl_entity *dl_se) |
| 176 | { |
| 177 | #ifdef CONFIG_CPU_FREQ_GOV_SCHEDUTIL |
| 178 | return unlikely(dl_se->flags & SCHED_FLAG_SUGOV); |
| 179 | #else |
| 180 | return false; |
| 181 | #endif |
| 182 | } |
| 183 | |
| 184 | /* |
Dario Faggioli | 2d3d891 | 2013-11-07 14:43:44 +0100 | [diff] [blame] | 185 | * Tells if entity @a should preempt entity @b. |
| 186 | */ |
Dario Faggioli | 332ac17 | 2013-11-07 14:43:45 +0100 | [diff] [blame] | 187 | static inline bool |
| 188 | dl_entity_preempt(struct sched_dl_entity *a, struct sched_dl_entity *b) |
Dario Faggioli | 2d3d891 | 2013-11-07 14:43:44 +0100 | [diff] [blame] | 189 | { |
Juri Lelli | 794a56e | 2017-12-04 11:23:20 +0100 | [diff] [blame] | 190 | return dl_entity_is_special(a) || |
| 191 | dl_time_before(a->deadline, b->deadline); |
Dario Faggioli | 2d3d891 | 2013-11-07 14:43:44 +0100 | [diff] [blame] | 192 | } |
| 193 | |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 194 | /* |
| 195 | * This is the priority-queue data structure of the RT scheduling class: |
| 196 | */ |
| 197 | struct rt_prio_array { |
| 198 | DECLARE_BITMAP(bitmap, MAX_RT_PRIO+1); /* include 1 bit for delimiter */ |
| 199 | struct list_head queue[MAX_RT_PRIO]; |
| 200 | }; |
| 201 | |
| 202 | struct rt_bandwidth { |
| 203 | /* nests inside the rq lock: */ |
| 204 | raw_spinlock_t rt_runtime_lock; |
| 205 | ktime_t rt_period; |
| 206 | u64 rt_runtime; |
| 207 | struct hrtimer rt_period_timer; |
Peter Zijlstra | 4cfafd3 | 2015-05-14 12:23:11 +0200 | [diff] [blame] | 208 | unsigned int rt_period_active; |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 209 | }; |
Juri Lelli | a5e7be3 | 2014-09-19 10:22:39 +0100 | [diff] [blame] | 210 | |
| 211 | void __dl_clear_params(struct task_struct *p); |
| 212 | |
Dario Faggioli | 332ac17 | 2013-11-07 14:43:45 +0100 | [diff] [blame] | 213 | /* |
| 214 | * To keep the bandwidth of -deadline tasks and groups under control |
| 215 | * we need some place where: |
| 216 | * - store the maximum -deadline bandwidth of the system (the group); |
| 217 | * - cache the fraction of that bandwidth that is currently allocated. |
| 218 | * |
| 219 | * This is all done in the data structure below. It is similar to the |
| 220 | * one used for RT-throttling (rt_bandwidth), with the main difference |
| 221 | * that, since here we are only interested in admission control, we |
| 222 | * do not decrease any runtime while the group "executes", neither we |
| 223 | * need a timer to replenish it. |
| 224 | * |
| 225 | * With respect to SMP, the bandwidth is given on a per-CPU basis, |
| 226 | * meaning that: |
| 227 | * - dl_bw (< 100%) is the bandwidth of the system (group) on each CPU; |
| 228 | * - dl_total_bw array contains, in the i-eth element, the currently |
| 229 | * allocated bandwidth on the i-eth CPU. |
| 230 | * Moreover, groups consume bandwidth on each CPU, while tasks only |
| 231 | * consume bandwidth on the CPU they're running on. |
| 232 | * Finally, dl_total_bw_cpu is used to cache the index of dl_total_bw |
| 233 | * that will be shown the next time the proc or cgroup controls will |
| 234 | * be red. It on its turn can be changed by writing on its own |
| 235 | * control. |
| 236 | */ |
| 237 | struct dl_bandwidth { |
| 238 | raw_spinlock_t dl_runtime_lock; |
| 239 | u64 dl_runtime; |
| 240 | u64 dl_period; |
| 241 | }; |
| 242 | |
| 243 | static inline int dl_bandwidth_enabled(void) |
| 244 | { |
Peter Zijlstra | 1724813 | 2013-12-17 12:44:49 +0100 | [diff] [blame] | 245 | return sysctl_sched_rt_runtime >= 0; |
Dario Faggioli | 332ac17 | 2013-11-07 14:43:45 +0100 | [diff] [blame] | 246 | } |
| 247 | |
Dario Faggioli | 332ac17 | 2013-11-07 14:43:45 +0100 | [diff] [blame] | 248 | struct dl_bw { |
| 249 | raw_spinlock_t lock; |
| 250 | u64 bw, total_bw; |
| 251 | }; |
| 252 | |
Luca Abeni | daec579 | 2017-05-18 22:13:36 +0200 | [diff] [blame] | 253 | static inline void __dl_update(struct dl_bw *dl_b, s64 bw); |
| 254 | |
Juri Lelli | 7f51412 | 2014-09-19 10:22:40 +0100 | [diff] [blame] | 255 | static inline |
Peter Zijlstra | 8c0944ce | 2017-09-07 12:09:30 +0200 | [diff] [blame] | 256 | void __dl_sub(struct dl_bw *dl_b, u64 tsk_bw, int cpus) |
Juri Lelli | 7f51412 | 2014-09-19 10:22:40 +0100 | [diff] [blame] | 257 | { |
| 258 | dl_b->total_bw -= tsk_bw; |
Luca Abeni | daec579 | 2017-05-18 22:13:36 +0200 | [diff] [blame] | 259 | __dl_update(dl_b, (s32)tsk_bw / cpus); |
Juri Lelli | 7f51412 | 2014-09-19 10:22:40 +0100 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | static inline |
Luca Abeni | daec579 | 2017-05-18 22:13:36 +0200 | [diff] [blame] | 263 | void __dl_add(struct dl_bw *dl_b, u64 tsk_bw, int cpus) |
Juri Lelli | 7f51412 | 2014-09-19 10:22:40 +0100 | [diff] [blame] | 264 | { |
| 265 | dl_b->total_bw += tsk_bw; |
Luca Abeni | daec579 | 2017-05-18 22:13:36 +0200 | [diff] [blame] | 266 | __dl_update(dl_b, -((s32)tsk_bw / cpus)); |
Juri Lelli | 7f51412 | 2014-09-19 10:22:40 +0100 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | static inline |
| 270 | bool __dl_overflow(struct dl_bw *dl_b, int cpus, u64 old_bw, u64 new_bw) |
| 271 | { |
| 272 | return dl_b->bw != -1 && |
| 273 | dl_b->bw * cpus < dl_b->total_bw - old_bw + new_bw; |
| 274 | } |
| 275 | |
Luca Abeni | 209a0cb | 2017-05-18 22:13:29 +0200 | [diff] [blame] | 276 | void dl_change_utilization(struct task_struct *p, u64 new_bw); |
Ingo Molnar | f2cb136 | 2017-02-01 13:10:18 +0100 | [diff] [blame] | 277 | extern void init_dl_bw(struct dl_bw *dl_b); |
Nicolas Pitre | 06a76fe | 2017-06-21 14:22:01 -0400 | [diff] [blame] | 278 | extern int sched_dl_global_validate(void); |
| 279 | extern void sched_dl_do_global(void); |
| 280 | extern int sched_dl_overflow(struct task_struct *p, int policy, |
| 281 | const struct sched_attr *attr); |
| 282 | extern void __setparam_dl(struct task_struct *p, const struct sched_attr *attr); |
| 283 | extern void __getparam_dl(struct task_struct *p, struct sched_attr *attr); |
| 284 | extern bool __checkparam_dl(const struct sched_attr *attr); |
Nicolas Pitre | 06a76fe | 2017-06-21 14:22:01 -0400 | [diff] [blame] | 285 | extern bool dl_param_changed(struct task_struct *p, const struct sched_attr *attr); |
| 286 | extern int dl_task_can_attach(struct task_struct *p, |
| 287 | const struct cpumask *cs_cpus_allowed); |
| 288 | extern int dl_cpuset_cpumask_can_shrink(const struct cpumask *cur, |
| 289 | const struct cpumask *trial); |
| 290 | extern bool dl_cpu_busy(unsigned int cpu); |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 291 | |
| 292 | #ifdef CONFIG_CGROUP_SCHED |
| 293 | |
| 294 | #include <linux/cgroup.h> |
| 295 | |
| 296 | struct cfs_rq; |
| 297 | struct rt_rq; |
| 298 | |
Mike Galbraith | 35cf4e5 | 2012-08-07 05:00:13 +0200 | [diff] [blame] | 299 | extern struct list_head task_groups; |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 300 | |
| 301 | struct cfs_bandwidth { |
| 302 | #ifdef CONFIG_CFS_BANDWIDTH |
| 303 | raw_spinlock_t lock; |
| 304 | ktime_t period; |
| 305 | u64 quota, runtime; |
Zhihui Zhang | 9c58c79 | 2014-09-20 21:24:36 -0400 | [diff] [blame] | 306 | s64 hierarchical_quota; |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 307 | u64 runtime_expires; |
| 308 | |
Peter Zijlstra | 4cfafd3 | 2015-05-14 12:23:11 +0200 | [diff] [blame] | 309 | int idle, period_active; |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 310 | struct hrtimer period_timer, slack_timer; |
| 311 | struct list_head throttled_cfs_rq; |
| 312 | |
| 313 | /* statistics */ |
| 314 | int nr_periods, nr_throttled; |
| 315 | u64 throttled_time; |
| 316 | #endif |
| 317 | }; |
| 318 | |
| 319 | /* task group related information */ |
| 320 | struct task_group { |
| 321 | struct cgroup_subsys_state css; |
| 322 | |
| 323 | #ifdef CONFIG_FAIR_GROUP_SCHED |
| 324 | /* schedulable entities of this group on each cpu */ |
| 325 | struct sched_entity **se; |
| 326 | /* runqueue "owned" by this group on each cpu */ |
| 327 | struct cfs_rq **cfs_rq; |
| 328 | unsigned long shares; |
| 329 | |
Alex Shi | fa6bdde | 2013-06-20 10:18:46 +0800 | [diff] [blame] | 330 | #ifdef CONFIG_SMP |
Waiman Long | b036762 | 2015-12-02 13:41:49 -0500 | [diff] [blame] | 331 | /* |
| 332 | * load_avg can be heavily contended at clock tick time, so put |
| 333 | * it in its own cacheline separated from the fields above which |
| 334 | * will also be accessed at each tick. |
| 335 | */ |
| 336 | atomic_long_t load_avg ____cacheline_aligned; |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 337 | #endif |
Alex Shi | fa6bdde | 2013-06-20 10:18:46 +0800 | [diff] [blame] | 338 | #endif |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 339 | |
| 340 | #ifdef CONFIG_RT_GROUP_SCHED |
| 341 | struct sched_rt_entity **rt_se; |
| 342 | struct rt_rq **rt_rq; |
| 343 | |
| 344 | struct rt_bandwidth rt_bandwidth; |
| 345 | #endif |
| 346 | |
| 347 | struct rcu_head rcu; |
| 348 | struct list_head list; |
| 349 | |
| 350 | struct task_group *parent; |
| 351 | struct list_head siblings; |
| 352 | struct list_head children; |
| 353 | |
| 354 | #ifdef CONFIG_SCHED_AUTOGROUP |
| 355 | struct autogroup *autogroup; |
| 356 | #endif |
| 357 | |
| 358 | struct cfs_bandwidth cfs_bandwidth; |
| 359 | }; |
| 360 | |
| 361 | #ifdef CONFIG_FAIR_GROUP_SCHED |
| 362 | #define ROOT_TASK_GROUP_LOAD NICE_0_LOAD |
| 363 | |
| 364 | /* |
| 365 | * A weight of 0 or 1 can cause arithmetics problems. |
| 366 | * A weight of a cfs_rq is the sum of weights of which entities |
| 367 | * are queued on this cfs_rq, so a weight of a entity should not be |
| 368 | * too large, so as the shares value of a task group. |
| 369 | * (The default weight is 1024 - so there's no practical |
| 370 | * limitation from this.) |
| 371 | */ |
| 372 | #define MIN_SHARES (1UL << 1) |
| 373 | #define MAX_SHARES (1UL << 18) |
| 374 | #endif |
| 375 | |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 376 | typedef int (*tg_visitor)(struct task_group *, void *); |
| 377 | |
| 378 | extern int walk_tg_tree_from(struct task_group *from, |
| 379 | tg_visitor down, tg_visitor up, void *data); |
| 380 | |
| 381 | /* |
| 382 | * Iterate the full tree, calling @down when first entering a node and @up when |
| 383 | * leaving it for the final time. |
| 384 | * |
| 385 | * Caller must hold rcu_lock or sufficient equivalent. |
| 386 | */ |
| 387 | static inline int walk_tg_tree(tg_visitor down, tg_visitor up, void *data) |
| 388 | { |
| 389 | return walk_tg_tree_from(&root_task_group, down, up, data); |
| 390 | } |
| 391 | |
| 392 | extern int tg_nop(struct task_group *tg, void *data); |
| 393 | |
| 394 | extern void free_fair_sched_group(struct task_group *tg); |
| 395 | extern int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent); |
Peter Zijlstra | 8663e24 | 2016-06-22 14:58:02 +0200 | [diff] [blame] | 396 | extern void online_fair_sched_group(struct task_group *tg); |
Peter Zijlstra | 6fe1f34 | 2016-01-21 22:24:16 +0100 | [diff] [blame] | 397 | extern void unregister_fair_sched_group(struct task_group *tg); |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 398 | extern void init_tg_cfs_entry(struct task_group *tg, struct cfs_rq *cfs_rq, |
| 399 | struct sched_entity *se, int cpu, |
| 400 | struct sched_entity *parent); |
| 401 | extern void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b); |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 402 | |
| 403 | extern void __refill_cfs_bandwidth_runtime(struct cfs_bandwidth *cfs_b); |
Peter Zijlstra | 77a4d1a | 2015-04-15 11:41:57 +0200 | [diff] [blame] | 404 | extern void start_cfs_bandwidth(struct cfs_bandwidth *cfs_b); |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 405 | extern void unthrottle_cfs_rq(struct cfs_rq *cfs_rq); |
| 406 | |
| 407 | extern void free_rt_sched_group(struct task_group *tg); |
| 408 | extern int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent); |
| 409 | extern void init_tg_rt_entry(struct task_group *tg, struct rt_rq *rt_rq, |
| 410 | struct sched_rt_entity *rt_se, int cpu, |
| 411 | struct sched_rt_entity *parent); |
Nicolas Pitre | 8887cd9 | 2017-06-21 14:22:02 -0400 | [diff] [blame] | 412 | extern int sched_group_set_rt_runtime(struct task_group *tg, long rt_runtime_us); |
| 413 | extern int sched_group_set_rt_period(struct task_group *tg, u64 rt_period_us); |
| 414 | extern long sched_group_rt_runtime(struct task_group *tg); |
| 415 | extern long sched_group_rt_period(struct task_group *tg); |
| 416 | extern int sched_rt_can_attach(struct task_group *tg, struct task_struct *tsk); |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 417 | |
Li Zefan | 25cc7da | 2013-03-05 16:07:33 +0800 | [diff] [blame] | 418 | extern struct task_group *sched_create_group(struct task_group *parent); |
| 419 | extern void sched_online_group(struct task_group *tg, |
| 420 | struct task_group *parent); |
| 421 | extern void sched_destroy_group(struct task_group *tg); |
| 422 | extern void sched_offline_group(struct task_group *tg); |
| 423 | |
| 424 | extern void sched_move_task(struct task_struct *tsk); |
| 425 | |
| 426 | #ifdef CONFIG_FAIR_GROUP_SCHED |
| 427 | extern int sched_group_set_shares(struct task_group *tg, unsigned long shares); |
Byungchul Park | ad936d8 | 2015-10-24 01:16:19 +0900 | [diff] [blame] | 428 | |
| 429 | #ifdef CONFIG_SMP |
| 430 | extern void set_task_rq_fair(struct sched_entity *se, |
| 431 | struct cfs_rq *prev, struct cfs_rq *next); |
| 432 | #else /* !CONFIG_SMP */ |
| 433 | static inline void set_task_rq_fair(struct sched_entity *se, |
| 434 | struct cfs_rq *prev, struct cfs_rq *next) { } |
| 435 | #endif /* CONFIG_SMP */ |
| 436 | #endif /* CONFIG_FAIR_GROUP_SCHED */ |
Li Zefan | 25cc7da | 2013-03-05 16:07:33 +0800 | [diff] [blame] | 437 | |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 438 | #else /* CONFIG_CGROUP_SCHED */ |
| 439 | |
| 440 | struct cfs_bandwidth { }; |
| 441 | |
| 442 | #endif /* CONFIG_CGROUP_SCHED */ |
| 443 | |
| 444 | /* CFS-related fields in a runqueue */ |
| 445 | struct cfs_rq { |
| 446 | struct load_weight load; |
Peter Zijlstra | 1ea6c46 | 2017-05-06 15:59:54 +0200 | [diff] [blame] | 447 | unsigned long runnable_weight; |
Peter Zijlstra | c82513e | 2012-04-26 13:12:27 +0200 | [diff] [blame] | 448 | unsigned int nr_running, h_nr_running; |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 449 | |
| 450 | u64 exec_clock; |
| 451 | u64 min_vruntime; |
| 452 | #ifndef CONFIG_64BIT |
| 453 | u64 min_vruntime_copy; |
| 454 | #endif |
| 455 | |
Davidlohr Bueso | bfb0688 | 2017-09-08 16:14:55 -0700 | [diff] [blame] | 456 | struct rb_root_cached tasks_timeline; |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 457 | |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 458 | /* |
| 459 | * 'curr' points to currently running entity on this cfs_rq. |
| 460 | * It is set to NULL otherwise (i.e when none are currently running). |
| 461 | */ |
| 462 | struct sched_entity *curr, *next, *last, *skip; |
| 463 | |
| 464 | #ifdef CONFIG_SCHED_DEBUG |
| 465 | unsigned int nr_spread_over; |
| 466 | #endif |
| 467 | |
Paul Turner | 2dac754 | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 468 | #ifdef CONFIG_SMP |
| 469 | /* |
Yuyang Du | 9d89c25 | 2015-07-15 08:04:37 +0800 | [diff] [blame] | 470 | * CFS load tracking |
Paul Turner | 2dac754 | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 471 | */ |
Yuyang Du | 9d89c25 | 2015-07-15 08:04:37 +0800 | [diff] [blame] | 472 | struct sched_avg avg; |
Peter Zijlstra | 2a2f5d4e | 2017-05-08 16:51:41 +0200 | [diff] [blame] | 473 | #ifndef CONFIG_64BIT |
| 474 | u64 load_last_update_time_copy; |
| 475 | #endif |
Peter Zijlstra | 2a2f5d4e | 2017-05-08 16:51:41 +0200 | [diff] [blame] | 476 | struct { |
| 477 | raw_spinlock_t lock ____cacheline_aligned; |
| 478 | int nr; |
| 479 | unsigned long load_avg; |
| 480 | unsigned long util_avg; |
Peter Zijlstra | 0e2d2aa | 2017-05-08 17:30:46 +0200 | [diff] [blame] | 481 | unsigned long runnable_sum; |
Peter Zijlstra | 2a2f5d4e | 2017-05-08 16:51:41 +0200 | [diff] [blame] | 482 | } removed; |
Alex Shi | 141965c | 2013-06-26 13:05:39 +0800 | [diff] [blame] | 483 | |
Paul Turner | c566e8e | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 484 | #ifdef CONFIG_FAIR_GROUP_SCHED |
Peter Zijlstra | 0e2d2aa | 2017-05-08 17:30:46 +0200 | [diff] [blame] | 485 | unsigned long tg_load_avg_contrib; |
| 486 | long propagate; |
| 487 | long prop_runnable_sum; |
| 488 | |
Paul Turner | 8295836 | 2012-10-04 13:18:31 +0200 | [diff] [blame] | 489 | /* |
| 490 | * h_load = weight * f(tg) |
| 491 | * |
| 492 | * Where f(tg) is the recursive weight fraction assigned to |
| 493 | * this group. |
| 494 | */ |
| 495 | unsigned long h_load; |
Vladimir Davydov | 6852079 | 2013-07-15 17:49:19 +0400 | [diff] [blame] | 496 | u64 last_h_load_update; |
| 497 | struct sched_entity *h_load_next; |
| 498 | #endif /* CONFIG_FAIR_GROUP_SCHED */ |
Paul Turner | 8295836 | 2012-10-04 13:18:31 +0200 | [diff] [blame] | 499 | #endif /* CONFIG_SMP */ |
| 500 | |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 501 | #ifdef CONFIG_FAIR_GROUP_SCHED |
| 502 | struct rq *rq; /* cpu runqueue to which this cfs_rq is attached */ |
| 503 | |
| 504 | /* |
| 505 | * leaf cfs_rqs are those that hold tasks (lowest schedulable entity in |
| 506 | * a hierarchy). Non-leaf lrqs hold other higher schedulable entities |
| 507 | * (like users, containers etc.) |
| 508 | * |
| 509 | * leaf_cfs_rq_list ties together list of leaf cfs_rq's in a cpu. This |
| 510 | * list is used during load balance. |
| 511 | */ |
| 512 | int on_list; |
| 513 | struct list_head leaf_cfs_rq_list; |
| 514 | struct task_group *tg; /* group that "owns" this runqueue */ |
| 515 | |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 516 | #ifdef CONFIG_CFS_BANDWIDTH |
| 517 | int runtime_enabled; |
| 518 | u64 runtime_expires; |
| 519 | s64 runtime_remaining; |
| 520 | |
Paul Turner | f1b1728 | 2012-10-04 13:18:31 +0200 | [diff] [blame] | 521 | u64 throttled_clock, throttled_clock_task; |
| 522 | u64 throttled_clock_task_time; |
Peter Zijlstra | 55e16d3 | 2016-06-22 15:14:26 +0200 | [diff] [blame] | 523 | int throttled, throttle_count; |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 524 | struct list_head throttled_list; |
| 525 | #endif /* CONFIG_CFS_BANDWIDTH */ |
| 526 | #endif /* CONFIG_FAIR_GROUP_SCHED */ |
| 527 | }; |
| 528 | |
| 529 | static inline int rt_bandwidth_enabled(void) |
| 530 | { |
| 531 | return sysctl_sched_rt_runtime >= 0; |
| 532 | } |
| 533 | |
Steven Rostedt | b6366f0 | 2015-03-18 14:49:46 -0400 | [diff] [blame] | 534 | /* RT IPI pull logic requires IRQ_WORK */ |
Steven Rostedt (Red Hat) | 4bdced5 | 2017-10-06 14:05:04 -0400 | [diff] [blame] | 535 | #if defined(CONFIG_IRQ_WORK) && defined(CONFIG_SMP) |
Steven Rostedt | b6366f0 | 2015-03-18 14:49:46 -0400 | [diff] [blame] | 536 | # define HAVE_RT_PUSH_IPI |
| 537 | #endif |
| 538 | |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 539 | /* Real-Time classes' related field in a runqueue: */ |
| 540 | struct rt_rq { |
| 541 | struct rt_prio_array active; |
Peter Zijlstra | c82513e | 2012-04-26 13:12:27 +0200 | [diff] [blame] | 542 | unsigned int rt_nr_running; |
Frederic Weisbecker | 01d36d0 | 2015-11-04 18:17:10 +0100 | [diff] [blame] | 543 | unsigned int rr_nr_running; |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 544 | #if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED |
| 545 | struct { |
| 546 | int curr; /* highest queued rt task prio */ |
| 547 | #ifdef CONFIG_SMP |
| 548 | int next; /* next highest */ |
| 549 | #endif |
| 550 | } highest_prio; |
| 551 | #endif |
| 552 | #ifdef CONFIG_SMP |
| 553 | unsigned long rt_nr_migratory; |
| 554 | unsigned long rt_nr_total; |
| 555 | int overloaded; |
| 556 | struct plist_head pushable_tasks; |
Steven Rostedt | b6366f0 | 2015-03-18 14:49:46 -0400 | [diff] [blame] | 557 | #endif /* CONFIG_SMP */ |
Kirill Tkhai | f4ebcbc | 2014-03-15 02:15:00 +0400 | [diff] [blame] | 558 | int rt_queued; |
| 559 | |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 560 | int rt_throttled; |
| 561 | u64 rt_time; |
| 562 | u64 rt_runtime; |
| 563 | /* Nests inside the rq lock: */ |
| 564 | raw_spinlock_t rt_runtime_lock; |
| 565 | |
| 566 | #ifdef CONFIG_RT_GROUP_SCHED |
| 567 | unsigned long rt_nr_boosted; |
| 568 | |
| 569 | struct rq *rq; |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 570 | struct task_group *tg; |
| 571 | #endif |
| 572 | }; |
| 573 | |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 574 | /* Deadline class' related fields in a runqueue */ |
| 575 | struct dl_rq { |
| 576 | /* runqueue is an rbtree, ordered by deadline */ |
Davidlohr Bueso | 2161573 | 2017-09-08 16:14:58 -0700 | [diff] [blame] | 577 | struct rb_root_cached root; |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 578 | |
| 579 | unsigned long dl_nr_running; |
Juri Lelli | 1baca4c | 2013-11-07 14:43:38 +0100 | [diff] [blame] | 580 | |
| 581 | #ifdef CONFIG_SMP |
| 582 | /* |
| 583 | * Deadline values of the currently executing and the |
| 584 | * earliest ready task on this rq. Caching these facilitates |
| 585 | * the decision wether or not a ready but not running task |
| 586 | * should migrate somewhere else. |
| 587 | */ |
| 588 | struct { |
| 589 | u64 curr; |
| 590 | u64 next; |
| 591 | } earliest_dl; |
| 592 | |
| 593 | unsigned long dl_nr_migratory; |
Juri Lelli | 1baca4c | 2013-11-07 14:43:38 +0100 | [diff] [blame] | 594 | int overloaded; |
| 595 | |
| 596 | /* |
| 597 | * Tasks on this rq that can be pushed away. They are kept in |
| 598 | * an rb-tree, ordered by tasks' deadlines, with caching |
| 599 | * of the leftmost (earliest deadline) element. |
| 600 | */ |
Davidlohr Bueso | 2161573 | 2017-09-08 16:14:58 -0700 | [diff] [blame] | 601 | struct rb_root_cached pushable_dl_tasks_root; |
Dario Faggioli | 332ac17 | 2013-11-07 14:43:45 +0100 | [diff] [blame] | 602 | #else |
| 603 | struct dl_bw dl_bw; |
Juri Lelli | 1baca4c | 2013-11-07 14:43:38 +0100 | [diff] [blame] | 604 | #endif |
Luca Abeni | e36d867 | 2017-05-18 22:13:28 +0200 | [diff] [blame] | 605 | /* |
| 606 | * "Active utilization" for this runqueue: increased when a |
| 607 | * task wakes up (becomes TASK_RUNNING) and decreased when a |
| 608 | * task blocks |
| 609 | */ |
| 610 | u64 running_bw; |
Luca Abeni | 4da3abc | 2017-05-18 22:13:32 +0200 | [diff] [blame] | 611 | |
| 612 | /* |
Luca Abeni | 8fd2723 | 2017-05-18 22:13:34 +0200 | [diff] [blame] | 613 | * Utilization of the tasks "assigned" to this runqueue (including |
| 614 | * the tasks that are in runqueue and the tasks that executed on this |
| 615 | * CPU and blocked). Increased when a task moves to this runqueue, and |
| 616 | * decreased when the task moves away (migrates, changes scheduling |
| 617 | * policy, or terminates). |
| 618 | * This is needed to compute the "inactive utilization" for the |
| 619 | * runqueue (inactive utilization = this_bw - running_bw). |
| 620 | */ |
| 621 | u64 this_bw; |
Luca Abeni | daec579 | 2017-05-18 22:13:36 +0200 | [diff] [blame] | 622 | u64 extra_bw; |
Luca Abeni | 8fd2723 | 2017-05-18 22:13:34 +0200 | [diff] [blame] | 623 | |
| 624 | /* |
Luca Abeni | 4da3abc | 2017-05-18 22:13:32 +0200 | [diff] [blame] | 625 | * Inverse of the fraction of CPU utilization that can be reclaimed |
| 626 | * by the GRUB algorithm. |
| 627 | */ |
| 628 | u64 bw_ratio; |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 629 | }; |
| 630 | |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 631 | #ifdef CONFIG_SMP |
| 632 | |
Tim Chen | afe06ef | 2016-11-22 12:23:53 -0800 | [diff] [blame] | 633 | static inline bool sched_asym_prefer(int a, int b) |
| 634 | { |
| 635 | return arch_asym_cpu_priority(a) > arch_asym_cpu_priority(b); |
| 636 | } |
| 637 | |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 638 | /* |
| 639 | * We add the notion of a root-domain which will be used to define per-domain |
| 640 | * variables. Each exclusive cpuset essentially defines an island domain by |
| 641 | * fully partitioning the member cpus from any other cpuset. Whenever a new |
| 642 | * exclusive cpuset is created, we also create and attach a new root-domain |
| 643 | * object. |
| 644 | * |
| 645 | */ |
| 646 | struct root_domain { |
| 647 | atomic_t refcount; |
| 648 | atomic_t rto_count; |
| 649 | struct rcu_head rcu; |
| 650 | cpumask_var_t span; |
| 651 | cpumask_var_t online; |
| 652 | |
Tim Chen | 4486edd | 2014-06-23 12:16:49 -0700 | [diff] [blame] | 653 | /* Indicate more than one runnable task for any CPU */ |
| 654 | bool overload; |
| 655 | |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 656 | /* |
Juri Lelli | 1baca4c | 2013-11-07 14:43:38 +0100 | [diff] [blame] | 657 | * The bit corresponding to a CPU gets set here if such CPU has more |
| 658 | * than one runnable -deadline task (as it is below for RT tasks). |
| 659 | */ |
| 660 | cpumask_var_t dlo_mask; |
| 661 | atomic_t dlo_count; |
Dario Faggioli | 332ac17 | 2013-11-07 14:43:45 +0100 | [diff] [blame] | 662 | struct dl_bw dl_bw; |
Juri Lelli | 6bfd6d7 | 2013-11-07 14:43:47 +0100 | [diff] [blame] | 663 | struct cpudl cpudl; |
Juri Lelli | 1baca4c | 2013-11-07 14:43:38 +0100 | [diff] [blame] | 664 | |
Steven Rostedt (Red Hat) | 4bdced5 | 2017-10-06 14:05:04 -0400 | [diff] [blame] | 665 | #ifdef HAVE_RT_PUSH_IPI |
| 666 | /* |
| 667 | * For IPI pull requests, loop across the rto_mask. |
| 668 | */ |
| 669 | struct irq_work rto_push_work; |
| 670 | raw_spinlock_t rto_lock; |
| 671 | /* These are only updated and read within rto_lock */ |
| 672 | int rto_loop; |
| 673 | int rto_cpu; |
| 674 | /* These atomics are updated outside of a lock */ |
| 675 | atomic_t rto_loop_next; |
| 676 | atomic_t rto_loop_start; |
| 677 | #endif |
Juri Lelli | 1baca4c | 2013-11-07 14:43:38 +0100 | [diff] [blame] | 678 | /* |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 679 | * The "RT overload" flag: it gets set if a CPU has more than |
| 680 | * one runnable RT task. |
| 681 | */ |
| 682 | cpumask_var_t rto_mask; |
| 683 | struct cpupri cpupri; |
Dietmar Eggemann | cd92bfd | 2016-08-01 19:53:35 +0100 | [diff] [blame] | 684 | |
| 685 | unsigned long max_cpu_capacity; |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 686 | }; |
| 687 | |
| 688 | extern struct root_domain def_root_domain; |
Ingo Molnar | f2cb136 | 2017-02-01 13:10:18 +0100 | [diff] [blame] | 689 | extern struct mutex sched_domains_mutex; |
Ingo Molnar | f2cb136 | 2017-02-01 13:10:18 +0100 | [diff] [blame] | 690 | |
| 691 | extern void init_defrootdomain(void); |
Peter Zijlstra | 8d5dc51 | 2017-04-25 15:29:40 +0200 | [diff] [blame] | 692 | extern int sched_init_domains(const struct cpumask *cpu_map); |
Ingo Molnar | f2cb136 | 2017-02-01 13:10:18 +0100 | [diff] [blame] | 693 | extern void rq_attach_root(struct rq *rq, struct root_domain *rd); |
Steven Rostedt (VMware) | 364f566 | 2018-01-23 20:45:38 -0500 | [diff] [blame] | 694 | extern void sched_get_rd(struct root_domain *rd); |
| 695 | extern void sched_put_rd(struct root_domain *rd); |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 696 | |
Steven Rostedt (Red Hat) | 4bdced5 | 2017-10-06 14:05:04 -0400 | [diff] [blame] | 697 | #ifdef HAVE_RT_PUSH_IPI |
| 698 | extern void rto_push_irq_work_func(struct irq_work *work); |
| 699 | #endif |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 700 | #endif /* CONFIG_SMP */ |
| 701 | |
| 702 | /* |
| 703 | * This is the main, per-CPU runqueue data structure. |
| 704 | * |
| 705 | * Locking rule: those places that want to lock multiple runqueues |
| 706 | * (such as the load balancing or the thread migration code), lock |
| 707 | * acquire operations must be ordered by ascending &runqueue. |
| 708 | */ |
| 709 | struct rq { |
| 710 | /* runqueue lock: */ |
| 711 | raw_spinlock_t lock; |
| 712 | |
| 713 | /* |
| 714 | * nr_running and cpu_load should be in the same cacheline because |
| 715 | * remote CPUs use both these fields when doing load calculation. |
| 716 | */ |
Peter Zijlstra | c82513e | 2012-04-26 13:12:27 +0200 | [diff] [blame] | 717 | unsigned int nr_running; |
Peter Zijlstra | 0ec8aa0 | 2013-10-07 11:29:33 +0100 | [diff] [blame] | 718 | #ifdef CONFIG_NUMA_BALANCING |
| 719 | unsigned int nr_numa_running; |
| 720 | unsigned int nr_preferred_running; |
| 721 | #endif |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 722 | #define CPU_LOAD_IDX_MAX 5 |
| 723 | unsigned long cpu_load[CPU_LOAD_IDX_MAX]; |
Frederic Weisbecker | 3451d02 | 2011-08-10 23:21:01 +0200 | [diff] [blame] | 724 | #ifdef CONFIG_NO_HZ_COMMON |
Frederic Weisbecker | 9fd81dd | 2016-04-19 17:36:51 +0200 | [diff] [blame] | 725 | #ifdef CONFIG_SMP |
| 726 | unsigned long last_load_update_tick; |
| 727 | #endif /* CONFIG_SMP */ |
Suresh Siddha | 1c792db | 2011-12-01 17:07:32 -0800 | [diff] [blame] | 728 | unsigned long nohz_flags; |
Frederic Weisbecker | 9fd81dd | 2016-04-19 17:36:51 +0200 | [diff] [blame] | 729 | #endif /* CONFIG_NO_HZ_COMMON */ |
Frederic Weisbecker | dcdedb2 | 2018-02-21 05:17:28 +0100 | [diff] [blame^] | 730 | |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 731 | /* capture load from *all* tasks on this cpu: */ |
| 732 | struct load_weight load; |
| 733 | unsigned long nr_load_updates; |
| 734 | u64 nr_switches; |
| 735 | |
| 736 | struct cfs_rq cfs; |
| 737 | struct rt_rq rt; |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 738 | struct dl_rq dl; |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 739 | |
| 740 | #ifdef CONFIG_FAIR_GROUP_SCHED |
| 741 | /* list of leaf cfs_rq on this cpu: */ |
| 742 | struct list_head leaf_cfs_rq_list; |
Vincent Guittot | 9c2791f | 2016-11-08 10:53:43 +0100 | [diff] [blame] | 743 | struct list_head *tmp_alone_branch; |
Peter Zijlstra | a35b646 | 2012-08-08 21:46:40 +0200 | [diff] [blame] | 744 | #endif /* CONFIG_FAIR_GROUP_SCHED */ |
| 745 | |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 746 | /* |
| 747 | * This is part of a global counter where only the total sum |
| 748 | * over all CPUs matters. A task can increase this counter on |
| 749 | * one CPU and if it got migrated afterwards it may decrease |
| 750 | * it on another CPU. Always updated under the runqueue lock: |
| 751 | */ |
| 752 | unsigned long nr_uninterruptible; |
| 753 | |
| 754 | struct task_struct *curr, *idle, *stop; |
| 755 | unsigned long next_balance; |
| 756 | struct mm_struct *prev_mm; |
| 757 | |
Matt Fleming | cb42c9a | 2016-09-21 14:38:13 +0100 | [diff] [blame] | 758 | unsigned int clock_update_flags; |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 759 | u64 clock; |
| 760 | u64 clock_task; |
| 761 | |
| 762 | atomic_t nr_iowait; |
| 763 | |
| 764 | #ifdef CONFIG_SMP |
| 765 | struct root_domain *rd; |
| 766 | struct sched_domain *sd; |
| 767 | |
Nicolas Pitre | ced549f | 2014-05-26 18:19:38 -0400 | [diff] [blame] | 768 | unsigned long cpu_capacity; |
Vincent Guittot | ca6d75e | 2015-02-27 16:54:09 +0100 | [diff] [blame] | 769 | unsigned long cpu_capacity_orig; |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 770 | |
Peter Zijlstra | e3fca9e | 2015-06-11 14:46:37 +0200 | [diff] [blame] | 771 | struct callback_head *balance_callback; |
| 772 | |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 773 | unsigned char idle_balance; |
| 774 | /* For active balancing */ |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 775 | int active_balance; |
| 776 | int push_cpu; |
| 777 | struct cpu_stop_work active_balance_work; |
| 778 | /* cpu of this runqueue: */ |
| 779 | int cpu; |
| 780 | int online; |
| 781 | |
Peter Zijlstra | 367456c | 2012-02-20 21:49:09 +0100 | [diff] [blame] | 782 | struct list_head cfs_tasks; |
| 783 | |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 784 | u64 rt_avg; |
| 785 | u64 age_stamp; |
| 786 | u64 idle_stamp; |
| 787 | u64 avg_idle; |
Jason Low | 9bd721c | 2013-09-13 11:26:52 -0700 | [diff] [blame] | 788 | |
| 789 | /* This is used to determine avg_idle's max value */ |
| 790 | u64 max_idle_balance_cost; |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 791 | #endif |
| 792 | |
| 793 | #ifdef CONFIG_IRQ_TIME_ACCOUNTING |
| 794 | u64 prev_irq_time; |
| 795 | #endif |
| 796 | #ifdef CONFIG_PARAVIRT |
| 797 | u64 prev_steal_time; |
| 798 | #endif |
| 799 | #ifdef CONFIG_PARAVIRT_TIME_ACCOUNTING |
| 800 | u64 prev_steal_time_rq; |
| 801 | #endif |
| 802 | |
| 803 | /* calc_load related fields */ |
| 804 | unsigned long calc_load_update; |
| 805 | long calc_load_active; |
| 806 | |
| 807 | #ifdef CONFIG_SCHED_HRTICK |
| 808 | #ifdef CONFIG_SMP |
| 809 | int hrtick_csd_pending; |
Ying Huang | 966a967 | 2017-08-08 12:30:00 +0800 | [diff] [blame] | 810 | call_single_data_t hrtick_csd; |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 811 | #endif |
| 812 | struct hrtimer hrtick_timer; |
| 813 | #endif |
| 814 | |
| 815 | #ifdef CONFIG_SCHEDSTATS |
| 816 | /* latency stats */ |
| 817 | struct sched_info rq_sched_info; |
| 818 | unsigned long long rq_cpu_time; |
| 819 | /* could above be rq->cfs_rq.exec_clock + rq->rt_rq.rt_runtime ? */ |
| 820 | |
| 821 | /* sys_sched_yield() stats */ |
| 822 | unsigned int yld_count; |
| 823 | |
| 824 | /* schedule() stats */ |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 825 | unsigned int sched_count; |
| 826 | unsigned int sched_goidle; |
| 827 | |
| 828 | /* try_to_wake_up() stats */ |
| 829 | unsigned int ttwu_count; |
| 830 | unsigned int ttwu_local; |
| 831 | #endif |
| 832 | |
| 833 | #ifdef CONFIG_SMP |
| 834 | struct llist_head wake_list; |
| 835 | #endif |
Daniel Lezcano | 442bf3a | 2014-09-04 11:32:09 -0400 | [diff] [blame] | 836 | |
| 837 | #ifdef CONFIG_CPU_IDLE |
| 838 | /* Must be inspected within a rcu lock section */ |
| 839 | struct cpuidle_state *idle_state; |
| 840 | #endif |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 841 | }; |
| 842 | |
| 843 | static inline int cpu_of(struct rq *rq) |
| 844 | { |
| 845 | #ifdef CONFIG_SMP |
| 846 | return rq->cpu; |
| 847 | #else |
| 848 | return 0; |
| 849 | #endif |
| 850 | } |
| 851 | |
Peter Zijlstra | 1b568f0 | 2016-05-09 10:38:41 +0200 | [diff] [blame] | 852 | |
| 853 | #ifdef CONFIG_SCHED_SMT |
| 854 | |
| 855 | extern struct static_key_false sched_smt_present; |
| 856 | |
| 857 | extern void __update_idle_core(struct rq *rq); |
| 858 | |
| 859 | static inline void update_idle_core(struct rq *rq) |
| 860 | { |
| 861 | if (static_branch_unlikely(&sched_smt_present)) |
| 862 | __update_idle_core(rq); |
| 863 | } |
| 864 | |
| 865 | #else |
| 866 | static inline void update_idle_core(struct rq *rq) { } |
| 867 | #endif |
| 868 | |
Pranith Kumar | 8b06c55 | 2014-08-13 13:28:12 -0400 | [diff] [blame] | 869 | DECLARE_PER_CPU_SHARED_ALIGNED(struct rq, runqueues); |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 870 | |
Peter Zijlstra | 518cd62 | 2011-12-07 15:07:31 +0100 | [diff] [blame] | 871 | #define cpu_rq(cpu) (&per_cpu(runqueues, (cpu))) |
Christoph Lameter | 4a32fea | 2014-08-17 12:30:27 -0500 | [diff] [blame] | 872 | #define this_rq() this_cpu_ptr(&runqueues) |
Peter Zijlstra | 518cd62 | 2011-12-07 15:07:31 +0100 | [diff] [blame] | 873 | #define task_rq(p) cpu_rq(task_cpu(p)) |
| 874 | #define cpu_curr(cpu) (cpu_rq(cpu)->curr) |
Christoph Lameter | 4a32fea | 2014-08-17 12:30:27 -0500 | [diff] [blame] | 875 | #define raw_rq() raw_cpu_ptr(&runqueues) |
Peter Zijlstra | 518cd62 | 2011-12-07 15:07:31 +0100 | [diff] [blame] | 876 | |
Peter Zijlstra | cebde6d | 2015-01-05 11:18:10 +0100 | [diff] [blame] | 877 | static inline u64 __rq_clock_broken(struct rq *rq) |
| 878 | { |
Jason Low | 316c1608d | 2015-04-28 13:00:20 -0700 | [diff] [blame] | 879 | return READ_ONCE(rq->clock); |
Peter Zijlstra | cebde6d | 2015-01-05 11:18:10 +0100 | [diff] [blame] | 880 | } |
| 881 | |
Matt Fleming | cb42c9a | 2016-09-21 14:38:13 +0100 | [diff] [blame] | 882 | /* |
| 883 | * rq::clock_update_flags bits |
| 884 | * |
| 885 | * %RQCF_REQ_SKIP - will request skipping of clock update on the next |
| 886 | * call to __schedule(). This is an optimisation to avoid |
| 887 | * neighbouring rq clock updates. |
| 888 | * |
| 889 | * %RQCF_ACT_SKIP - is set from inside of __schedule() when skipping is |
| 890 | * in effect and calls to update_rq_clock() are being ignored. |
| 891 | * |
| 892 | * %RQCF_UPDATED - is a debug flag that indicates whether a call has been |
| 893 | * made to update_rq_clock() since the last time rq::lock was pinned. |
| 894 | * |
| 895 | * If inside of __schedule(), clock_update_flags will have been |
| 896 | * shifted left (a left shift is a cheap operation for the fast path |
| 897 | * to promote %RQCF_REQ_SKIP to %RQCF_ACT_SKIP), so you must use, |
| 898 | * |
| 899 | * if (rq-clock_update_flags >= RQCF_UPDATED) |
| 900 | * |
| 901 | * to check if %RQCF_UPADTED is set. It'll never be shifted more than |
| 902 | * one position though, because the next rq_unpin_lock() will shift it |
| 903 | * back. |
| 904 | */ |
| 905 | #define RQCF_REQ_SKIP 0x01 |
| 906 | #define RQCF_ACT_SKIP 0x02 |
| 907 | #define RQCF_UPDATED 0x04 |
| 908 | |
| 909 | static inline void assert_clock_updated(struct rq *rq) |
| 910 | { |
| 911 | /* |
| 912 | * The only reason for not seeing a clock update since the |
| 913 | * last rq_pin_lock() is if we're currently skipping updates. |
| 914 | */ |
| 915 | SCHED_WARN_ON(rq->clock_update_flags < RQCF_ACT_SKIP); |
| 916 | } |
| 917 | |
Frederic Weisbecker | 78becc2 | 2013-04-12 01:51:02 +0200 | [diff] [blame] | 918 | static inline u64 rq_clock(struct rq *rq) |
| 919 | { |
Peter Zijlstra | cebde6d | 2015-01-05 11:18:10 +0100 | [diff] [blame] | 920 | lockdep_assert_held(&rq->lock); |
Matt Fleming | cb42c9a | 2016-09-21 14:38:13 +0100 | [diff] [blame] | 921 | assert_clock_updated(rq); |
| 922 | |
Frederic Weisbecker | 78becc2 | 2013-04-12 01:51:02 +0200 | [diff] [blame] | 923 | return rq->clock; |
| 924 | } |
| 925 | |
| 926 | static inline u64 rq_clock_task(struct rq *rq) |
| 927 | { |
Peter Zijlstra | cebde6d | 2015-01-05 11:18:10 +0100 | [diff] [blame] | 928 | lockdep_assert_held(&rq->lock); |
Matt Fleming | cb42c9a | 2016-09-21 14:38:13 +0100 | [diff] [blame] | 929 | assert_clock_updated(rq); |
| 930 | |
Frederic Weisbecker | 78becc2 | 2013-04-12 01:51:02 +0200 | [diff] [blame] | 931 | return rq->clock_task; |
| 932 | } |
| 933 | |
Peter Zijlstra | 9edfbfe | 2015-01-05 11:18:11 +0100 | [diff] [blame] | 934 | static inline void rq_clock_skip_update(struct rq *rq, bool skip) |
| 935 | { |
| 936 | lockdep_assert_held(&rq->lock); |
| 937 | if (skip) |
Matt Fleming | cb42c9a | 2016-09-21 14:38:13 +0100 | [diff] [blame] | 938 | rq->clock_update_flags |= RQCF_REQ_SKIP; |
Peter Zijlstra | 9edfbfe | 2015-01-05 11:18:11 +0100 | [diff] [blame] | 939 | else |
Matt Fleming | cb42c9a | 2016-09-21 14:38:13 +0100 | [diff] [blame] | 940 | rq->clock_update_flags &= ~RQCF_REQ_SKIP; |
Peter Zijlstra | 9edfbfe | 2015-01-05 11:18:11 +0100 | [diff] [blame] | 941 | } |
| 942 | |
Matt Fleming | d8ac897 | 2016-09-21 14:38:10 +0100 | [diff] [blame] | 943 | struct rq_flags { |
| 944 | unsigned long flags; |
| 945 | struct pin_cookie cookie; |
Matt Fleming | cb42c9a | 2016-09-21 14:38:13 +0100 | [diff] [blame] | 946 | #ifdef CONFIG_SCHED_DEBUG |
| 947 | /* |
| 948 | * A copy of (rq::clock_update_flags & RQCF_UPDATED) for the |
| 949 | * current pin context is stashed here in case it needs to be |
| 950 | * restored in rq_repin_lock(). |
| 951 | */ |
| 952 | unsigned int clock_update_flags; |
| 953 | #endif |
Matt Fleming | d8ac897 | 2016-09-21 14:38:10 +0100 | [diff] [blame] | 954 | }; |
| 955 | |
| 956 | static inline void rq_pin_lock(struct rq *rq, struct rq_flags *rf) |
| 957 | { |
| 958 | rf->cookie = lockdep_pin_lock(&rq->lock); |
Matt Fleming | cb42c9a | 2016-09-21 14:38:13 +0100 | [diff] [blame] | 959 | |
| 960 | #ifdef CONFIG_SCHED_DEBUG |
| 961 | rq->clock_update_flags &= (RQCF_REQ_SKIP|RQCF_ACT_SKIP); |
| 962 | rf->clock_update_flags = 0; |
| 963 | #endif |
Matt Fleming | d8ac897 | 2016-09-21 14:38:10 +0100 | [diff] [blame] | 964 | } |
| 965 | |
| 966 | static inline void rq_unpin_lock(struct rq *rq, struct rq_flags *rf) |
| 967 | { |
Matt Fleming | cb42c9a | 2016-09-21 14:38:13 +0100 | [diff] [blame] | 968 | #ifdef CONFIG_SCHED_DEBUG |
| 969 | if (rq->clock_update_flags > RQCF_ACT_SKIP) |
| 970 | rf->clock_update_flags = RQCF_UPDATED; |
| 971 | #endif |
| 972 | |
Matt Fleming | d8ac897 | 2016-09-21 14:38:10 +0100 | [diff] [blame] | 973 | lockdep_unpin_lock(&rq->lock, rf->cookie); |
| 974 | } |
| 975 | |
| 976 | static inline void rq_repin_lock(struct rq *rq, struct rq_flags *rf) |
| 977 | { |
| 978 | lockdep_repin_lock(&rq->lock, rf->cookie); |
Matt Fleming | cb42c9a | 2016-09-21 14:38:13 +0100 | [diff] [blame] | 979 | |
| 980 | #ifdef CONFIG_SCHED_DEBUG |
| 981 | /* |
| 982 | * Restore the value we stashed in @rf for this pin context. |
| 983 | */ |
| 984 | rq->clock_update_flags |= rf->clock_update_flags; |
| 985 | #endif |
Matt Fleming | d8ac897 | 2016-09-21 14:38:10 +0100 | [diff] [blame] | 986 | } |
| 987 | |
Rik van Riel | 9942f79 | 2014-10-17 03:29:49 -0400 | [diff] [blame] | 988 | #ifdef CONFIG_NUMA |
Rik van Riel | e3fe70b | 2014-10-17 03:29:50 -0400 | [diff] [blame] | 989 | enum numa_topology_type { |
| 990 | NUMA_DIRECT, |
| 991 | NUMA_GLUELESS_MESH, |
| 992 | NUMA_BACKPLANE, |
| 993 | }; |
| 994 | extern enum numa_topology_type sched_numa_topology_type; |
Rik van Riel | 9942f79 | 2014-10-17 03:29:49 -0400 | [diff] [blame] | 995 | extern int sched_max_numa_distance; |
| 996 | extern bool find_numa_distance(int distance); |
| 997 | #endif |
| 998 | |
Ingo Molnar | f2cb136 | 2017-02-01 13:10:18 +0100 | [diff] [blame] | 999 | #ifdef CONFIG_NUMA |
| 1000 | extern void sched_init_numa(void); |
| 1001 | extern void sched_domains_numa_masks_set(unsigned int cpu); |
| 1002 | extern void sched_domains_numa_masks_clear(unsigned int cpu); |
| 1003 | #else |
| 1004 | static inline void sched_init_numa(void) { } |
| 1005 | static inline void sched_domains_numa_masks_set(unsigned int cpu) { } |
| 1006 | static inline void sched_domains_numa_masks_clear(unsigned int cpu) { } |
| 1007 | #endif |
| 1008 | |
Mel Gorman | f809ca9 | 2013-10-07 11:28:57 +0100 | [diff] [blame] | 1009 | #ifdef CONFIG_NUMA_BALANCING |
Iulia Manda | 44dba3d | 2014-10-31 02:13:31 +0200 | [diff] [blame] | 1010 | /* The regions in numa_faults array from task_struct */ |
| 1011 | enum numa_faults_stats { |
| 1012 | NUMA_MEM = 0, |
| 1013 | NUMA_CPU, |
| 1014 | NUMA_MEMBUF, |
| 1015 | NUMA_CPUBUF |
| 1016 | }; |
Peter Zijlstra | 0ec8aa0 | 2013-10-07 11:29:33 +0100 | [diff] [blame] | 1017 | extern void sched_setnuma(struct task_struct *p, int node); |
Mel Gorman | e6628d5 | 2013-10-07 11:29:02 +0100 | [diff] [blame] | 1018 | extern int migrate_task_to(struct task_struct *p, int cpu); |
Peter Zijlstra | ac66f54 | 2013-10-07 11:29:16 +0100 | [diff] [blame] | 1019 | extern int migrate_swap(struct task_struct *, struct task_struct *); |
Mel Gorman | f809ca9 | 2013-10-07 11:28:57 +0100 | [diff] [blame] | 1020 | #endif /* CONFIG_NUMA_BALANCING */ |
| 1021 | |
Peter Zijlstra | 518cd62 | 2011-12-07 15:07:31 +0100 | [diff] [blame] | 1022 | #ifdef CONFIG_SMP |
| 1023 | |
Peter Zijlstra | e3fca9e | 2015-06-11 14:46:37 +0200 | [diff] [blame] | 1024 | static inline void |
| 1025 | queue_balance_callback(struct rq *rq, |
| 1026 | struct callback_head *head, |
| 1027 | void (*func)(struct rq *rq)) |
| 1028 | { |
| 1029 | lockdep_assert_held(&rq->lock); |
| 1030 | |
| 1031 | if (unlikely(head->next)) |
| 1032 | return; |
| 1033 | |
| 1034 | head->func = (void (*)(struct callback_head *))func; |
| 1035 | head->next = rq->balance_callback; |
| 1036 | rq->balance_callback = head; |
| 1037 | } |
| 1038 | |
Peter Zijlstra | e3baac4 | 2014-06-04 10:31:18 -0700 | [diff] [blame] | 1039 | extern void sched_ttwu_pending(void); |
| 1040 | |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 1041 | #define rcu_dereference_check_sched_domain(p) \ |
| 1042 | rcu_dereference_check((p), \ |
| 1043 | lockdep_is_held(&sched_domains_mutex)) |
| 1044 | |
| 1045 | /* |
| 1046 | * The domain tree (rq->sd) is protected by RCU's quiescent state transition. |
| 1047 | * See detach_destroy_domains: synchronize_sched for details. |
| 1048 | * |
| 1049 | * The domain tree of any CPU may only be accessed from within |
| 1050 | * preempt-disabled sections. |
| 1051 | */ |
| 1052 | #define for_each_domain(cpu, __sd) \ |
Peter Zijlstra | 518cd62 | 2011-12-07 15:07:31 +0100 | [diff] [blame] | 1053 | for (__sd = rcu_dereference_check_sched_domain(cpu_rq(cpu)->sd); \ |
| 1054 | __sd; __sd = __sd->parent) |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 1055 | |
Suresh Siddha | 77e8136 | 2011-11-17 11:08:23 -0800 | [diff] [blame] | 1056 | #define for_each_lower_domain(sd) for (; sd; sd = sd->child) |
| 1057 | |
Peter Zijlstra | 518cd62 | 2011-12-07 15:07:31 +0100 | [diff] [blame] | 1058 | /** |
| 1059 | * highest_flag_domain - Return highest sched_domain containing flag. |
| 1060 | * @cpu: The cpu whose highest level of sched domain is to |
| 1061 | * be returned. |
| 1062 | * @flag: The flag to check for the highest sched_domain |
| 1063 | * for the given cpu. |
| 1064 | * |
| 1065 | * Returns the highest sched_domain of a cpu which contains the given flag. |
| 1066 | */ |
| 1067 | static inline struct sched_domain *highest_flag_domain(int cpu, int flag) |
| 1068 | { |
| 1069 | struct sched_domain *sd, *hsd = NULL; |
| 1070 | |
| 1071 | for_each_domain(cpu, sd) { |
| 1072 | if (!(sd->flags & flag)) |
| 1073 | break; |
| 1074 | hsd = sd; |
| 1075 | } |
| 1076 | |
| 1077 | return hsd; |
| 1078 | } |
| 1079 | |
Mel Gorman | fb13c7e | 2013-10-07 11:29:17 +0100 | [diff] [blame] | 1080 | static inline struct sched_domain *lowest_flag_domain(int cpu, int flag) |
| 1081 | { |
| 1082 | struct sched_domain *sd; |
| 1083 | |
| 1084 | for_each_domain(cpu, sd) { |
| 1085 | if (sd->flags & flag) |
| 1086 | break; |
| 1087 | } |
| 1088 | |
| 1089 | return sd; |
| 1090 | } |
| 1091 | |
Peter Zijlstra | 518cd62 | 2011-12-07 15:07:31 +0100 | [diff] [blame] | 1092 | DECLARE_PER_CPU(struct sched_domain *, sd_llc); |
Peter Zijlstra | 7d9ffa8 | 2013-07-04 12:56:46 +0800 | [diff] [blame] | 1093 | DECLARE_PER_CPU(int, sd_llc_size); |
Peter Zijlstra | 518cd62 | 2011-12-07 15:07:31 +0100 | [diff] [blame] | 1094 | DECLARE_PER_CPU(int, sd_llc_id); |
Peter Zijlstra | 0e369d7 | 2016-05-09 10:38:01 +0200 | [diff] [blame] | 1095 | DECLARE_PER_CPU(struct sched_domain_shared *, sd_llc_shared); |
Mel Gorman | fb13c7e | 2013-10-07 11:29:17 +0100 | [diff] [blame] | 1096 | DECLARE_PER_CPU(struct sched_domain *, sd_numa); |
Preeti U Murthy | 37dc6b5 | 2013-10-30 08:42:52 +0530 | [diff] [blame] | 1097 | DECLARE_PER_CPU(struct sched_domain *, sd_asym); |
Peter Zijlstra | 518cd62 | 2011-12-07 15:07:31 +0100 | [diff] [blame] | 1098 | |
Nicolas Pitre | 63b2ca3 | 2014-05-26 18:19:37 -0400 | [diff] [blame] | 1099 | struct sched_group_capacity { |
Li Zefan | 5e6521e | 2013-03-05 16:06:23 +0800 | [diff] [blame] | 1100 | atomic_t ref; |
| 1101 | /* |
Yuyang Du | 172895e | 2016-04-05 12:12:27 +0800 | [diff] [blame] | 1102 | * CPU capacity of this group, SCHED_CAPACITY_SCALE being max capacity |
Nicolas Pitre | 63b2ca3 | 2014-05-26 18:19:37 -0400 | [diff] [blame] | 1103 | * for a single CPU. |
Li Zefan | 5e6521e | 2013-03-05 16:06:23 +0800 | [diff] [blame] | 1104 | */ |
Morten Rasmussen | bf475ce | 2016-10-14 14:41:09 +0100 | [diff] [blame] | 1105 | unsigned long capacity; |
| 1106 | unsigned long min_capacity; /* Min per-CPU capacity in group */ |
Li Zefan | 5e6521e | 2013-03-05 16:06:23 +0800 | [diff] [blame] | 1107 | unsigned long next_update; |
Nicolas Pitre | 63b2ca3 | 2014-05-26 18:19:37 -0400 | [diff] [blame] | 1108 | int imbalance; /* XXX unrelated to capacity but shared group state */ |
Li Zefan | 5e6521e | 2013-03-05 16:06:23 +0800 | [diff] [blame] | 1109 | |
Peter Zijlstra | 005f874 | 2017-04-26 17:35:35 +0200 | [diff] [blame] | 1110 | #ifdef CONFIG_SCHED_DEBUG |
| 1111 | int id; |
| 1112 | #endif |
| 1113 | |
Peter Zijlstra | e5c14b1 | 2017-05-01 10:47:02 +0200 | [diff] [blame] | 1114 | unsigned long cpumask[0]; /* balance mask */ |
Li Zefan | 5e6521e | 2013-03-05 16:06:23 +0800 | [diff] [blame] | 1115 | }; |
| 1116 | |
| 1117 | struct sched_group { |
| 1118 | struct sched_group *next; /* Must be a circular list */ |
| 1119 | atomic_t ref; |
| 1120 | |
| 1121 | unsigned int group_weight; |
Nicolas Pitre | 63b2ca3 | 2014-05-26 18:19:37 -0400 | [diff] [blame] | 1122 | struct sched_group_capacity *sgc; |
Tim Chen | afe06ef | 2016-11-22 12:23:53 -0800 | [diff] [blame] | 1123 | int asym_prefer_cpu; /* cpu of highest priority in group */ |
Li Zefan | 5e6521e | 2013-03-05 16:06:23 +0800 | [diff] [blame] | 1124 | |
| 1125 | /* |
| 1126 | * The CPUs this group covers. |
| 1127 | * |
| 1128 | * NOTE: this field is variable length. (Allocated dynamically |
| 1129 | * by attaching extra space to the end of the structure, |
| 1130 | * depending on how many CPUs the kernel has booted up with) |
| 1131 | */ |
| 1132 | unsigned long cpumask[0]; |
| 1133 | }; |
| 1134 | |
Peter Zijlstra | ae4df9d | 2017-05-01 11:03:12 +0200 | [diff] [blame] | 1135 | static inline struct cpumask *sched_group_span(struct sched_group *sg) |
Li Zefan | 5e6521e | 2013-03-05 16:06:23 +0800 | [diff] [blame] | 1136 | { |
| 1137 | return to_cpumask(sg->cpumask); |
| 1138 | } |
| 1139 | |
| 1140 | /* |
Peter Zijlstra | e5c14b1 | 2017-05-01 10:47:02 +0200 | [diff] [blame] | 1141 | * See build_balance_mask(). |
Li Zefan | 5e6521e | 2013-03-05 16:06:23 +0800 | [diff] [blame] | 1142 | */ |
Peter Zijlstra | e5c14b1 | 2017-05-01 10:47:02 +0200 | [diff] [blame] | 1143 | static inline struct cpumask *group_balance_mask(struct sched_group *sg) |
Li Zefan | 5e6521e | 2013-03-05 16:06:23 +0800 | [diff] [blame] | 1144 | { |
Nicolas Pitre | 63b2ca3 | 2014-05-26 18:19:37 -0400 | [diff] [blame] | 1145 | return to_cpumask(sg->sgc->cpumask); |
Li Zefan | 5e6521e | 2013-03-05 16:06:23 +0800 | [diff] [blame] | 1146 | } |
| 1147 | |
| 1148 | /** |
| 1149 | * group_first_cpu - Returns the first cpu in the cpumask of a sched_group. |
| 1150 | * @group: The group whose first cpu is to be returned. |
| 1151 | */ |
| 1152 | static inline unsigned int group_first_cpu(struct sched_group *group) |
| 1153 | { |
Peter Zijlstra | ae4df9d | 2017-05-01 11:03:12 +0200 | [diff] [blame] | 1154 | return cpumask_first(sched_group_span(group)); |
Li Zefan | 5e6521e | 2013-03-05 16:06:23 +0800 | [diff] [blame] | 1155 | } |
| 1156 | |
Peter Zijlstra | c117487 | 2012-05-31 14:47:33 +0200 | [diff] [blame] | 1157 | extern int group_balance_cpu(struct sched_group *sg); |
| 1158 | |
Steven Rostedt (Red Hat) | 3866e84 | 2016-02-22 16:26:51 -0500 | [diff] [blame] | 1159 | #if defined(CONFIG_SCHED_DEBUG) && defined(CONFIG_SYSCTL) |
| 1160 | void register_sched_domain_sysctl(void); |
Peter Zijlstra | bbdacdf | 2017-08-10 17:10:26 +0200 | [diff] [blame] | 1161 | void dirty_sched_domain_sysctl(int cpu); |
Steven Rostedt (Red Hat) | 3866e84 | 2016-02-22 16:26:51 -0500 | [diff] [blame] | 1162 | void unregister_sched_domain_sysctl(void); |
| 1163 | #else |
| 1164 | static inline void register_sched_domain_sysctl(void) |
| 1165 | { |
| 1166 | } |
Peter Zijlstra | bbdacdf | 2017-08-10 17:10:26 +0200 | [diff] [blame] | 1167 | static inline void dirty_sched_domain_sysctl(int cpu) |
| 1168 | { |
| 1169 | } |
Steven Rostedt (Red Hat) | 3866e84 | 2016-02-22 16:26:51 -0500 | [diff] [blame] | 1170 | static inline void unregister_sched_domain_sysctl(void) |
| 1171 | { |
| 1172 | } |
| 1173 | #endif |
| 1174 | |
Peter Zijlstra | e3baac4 | 2014-06-04 10:31:18 -0700 | [diff] [blame] | 1175 | #else |
| 1176 | |
| 1177 | static inline void sched_ttwu_pending(void) { } |
| 1178 | |
Peter Zijlstra | 518cd62 | 2011-12-07 15:07:31 +0100 | [diff] [blame] | 1179 | #endif /* CONFIG_SMP */ |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 1180 | |
Peter Zijlstra | 391e43d | 2011-11-15 17:14:39 +0100 | [diff] [blame] | 1181 | #include "stats.h" |
Ingo Molnar | 1051408 | 2017-02-01 18:42:41 +0100 | [diff] [blame] | 1182 | #include "autogroup.h" |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 1183 | |
| 1184 | #ifdef CONFIG_CGROUP_SCHED |
| 1185 | |
| 1186 | /* |
| 1187 | * Return the group to which this tasks belongs. |
| 1188 | * |
Tejun Heo | 8af01f5 | 2013-08-08 20:11:22 -0400 | [diff] [blame] | 1189 | * We cannot use task_css() and friends because the cgroup subsystem |
| 1190 | * changes that value before the cgroup_subsys::attach() method is called, |
| 1191 | * therefore we cannot pin it and might observe the wrong value. |
Peter Zijlstra | 8323f26 | 2012-06-22 13:36:05 +0200 | [diff] [blame] | 1192 | * |
| 1193 | * The same is true for autogroup's p->signal->autogroup->tg, the autogroup |
| 1194 | * core changes this before calling sched_move_task(). |
| 1195 | * |
| 1196 | * Instead we use a 'copy' which is updated from sched_move_task() while |
| 1197 | * holding both task_struct::pi_lock and rq::lock. |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 1198 | */ |
| 1199 | static inline struct task_group *task_group(struct task_struct *p) |
| 1200 | { |
Peter Zijlstra | 8323f26 | 2012-06-22 13:36:05 +0200 | [diff] [blame] | 1201 | return p->sched_task_group; |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 1202 | } |
| 1203 | |
| 1204 | /* Change a task's cfs_rq and parent entity if it moves across CPUs/groups */ |
| 1205 | static inline void set_task_rq(struct task_struct *p, unsigned int cpu) |
| 1206 | { |
| 1207 | #if defined(CONFIG_FAIR_GROUP_SCHED) || defined(CONFIG_RT_GROUP_SCHED) |
| 1208 | struct task_group *tg = task_group(p); |
| 1209 | #endif |
| 1210 | |
| 1211 | #ifdef CONFIG_FAIR_GROUP_SCHED |
Byungchul Park | ad936d8 | 2015-10-24 01:16:19 +0900 | [diff] [blame] | 1212 | set_task_rq_fair(&p->se, p->se.cfs_rq, tg->cfs_rq[cpu]); |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 1213 | p->se.cfs_rq = tg->cfs_rq[cpu]; |
| 1214 | p->se.parent = tg->se[cpu]; |
| 1215 | #endif |
| 1216 | |
| 1217 | #ifdef CONFIG_RT_GROUP_SCHED |
| 1218 | p->rt.rt_rq = tg->rt_rq[cpu]; |
| 1219 | p->rt.parent = tg->rt_se[cpu]; |
| 1220 | #endif |
| 1221 | } |
| 1222 | |
| 1223 | #else /* CONFIG_CGROUP_SCHED */ |
| 1224 | |
| 1225 | static inline void set_task_rq(struct task_struct *p, unsigned int cpu) { } |
| 1226 | static inline struct task_group *task_group(struct task_struct *p) |
| 1227 | { |
| 1228 | return NULL; |
| 1229 | } |
| 1230 | |
| 1231 | #endif /* CONFIG_CGROUP_SCHED */ |
| 1232 | |
| 1233 | static inline void __set_task_cpu(struct task_struct *p, unsigned int cpu) |
| 1234 | { |
| 1235 | set_task_rq(p, cpu); |
| 1236 | #ifdef CONFIG_SMP |
| 1237 | /* |
| 1238 | * After ->cpu is set up to a new value, task_rq_lock(p, ...) can be |
| 1239 | * successfuly executed on another CPU. We must ensure that updates of |
| 1240 | * per-task data have been completed by this moment. |
| 1241 | */ |
| 1242 | smp_wmb(); |
Andy Lutomirski | c65eacb | 2016-09-13 14:29:24 -0700 | [diff] [blame] | 1243 | #ifdef CONFIG_THREAD_INFO_IN_TASK |
| 1244 | p->cpu = cpu; |
| 1245 | #else |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 1246 | task_thread_info(p)->cpu = cpu; |
Andy Lutomirski | c65eacb | 2016-09-13 14:29:24 -0700 | [diff] [blame] | 1247 | #endif |
Peter Zijlstra | ac66f54 | 2013-10-07 11:29:16 +0100 | [diff] [blame] | 1248 | p->wake_cpu = cpu; |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 1249 | #endif |
| 1250 | } |
| 1251 | |
| 1252 | /* |
| 1253 | * Tunables that become constants when CONFIG_SCHED_DEBUG is off: |
| 1254 | */ |
| 1255 | #ifdef CONFIG_SCHED_DEBUG |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 1256 | # include <linux/static_key.h> |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 1257 | # define const_debug __read_mostly |
| 1258 | #else |
| 1259 | # define const_debug const |
| 1260 | #endif |
| 1261 | |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 1262 | #define SCHED_FEAT(name, enabled) \ |
| 1263 | __SCHED_FEAT_##name , |
| 1264 | |
| 1265 | enum { |
Peter Zijlstra | 391e43d | 2011-11-15 17:14:39 +0100 | [diff] [blame] | 1266 | #include "features.h" |
Peter Zijlstra | f8b6d1c | 2011-07-06 14:20:14 +0200 | [diff] [blame] | 1267 | __SCHED_FEAT_NR, |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 1268 | }; |
| 1269 | |
| 1270 | #undef SCHED_FEAT |
| 1271 | |
Peter Zijlstra | f8b6d1c | 2011-07-06 14:20:14 +0200 | [diff] [blame] | 1272 | #if defined(CONFIG_SCHED_DEBUG) && defined(HAVE_JUMP_LABEL) |
Patrick Bellasi | 765cc3a | 2017-11-08 18:41:01 +0000 | [diff] [blame] | 1273 | |
| 1274 | /* |
| 1275 | * To support run-time toggling of sched features, all the translation units |
| 1276 | * (but core.c) reference the sysctl_sched_features defined in core.c. |
| 1277 | */ |
| 1278 | extern const_debug unsigned int sysctl_sched_features; |
| 1279 | |
Peter Zijlstra | f8b6d1c | 2011-07-06 14:20:14 +0200 | [diff] [blame] | 1280 | #define SCHED_FEAT(name, enabled) \ |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 1281 | static __always_inline bool static_branch_##name(struct static_key *key) \ |
Peter Zijlstra | f8b6d1c | 2011-07-06 14:20:14 +0200 | [diff] [blame] | 1282 | { \ |
Jason Baron | 6e76ea8 | 2014-07-02 15:52:41 +0000 | [diff] [blame] | 1283 | return static_key_##enabled(key); \ |
Peter Zijlstra | f8b6d1c | 2011-07-06 14:20:14 +0200 | [diff] [blame] | 1284 | } |
| 1285 | |
| 1286 | #include "features.h" |
Peter Zijlstra | f8b6d1c | 2011-07-06 14:20:14 +0200 | [diff] [blame] | 1287 | #undef SCHED_FEAT |
| 1288 | |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 1289 | extern struct static_key sched_feat_keys[__SCHED_FEAT_NR]; |
Peter Zijlstra | f8b6d1c | 2011-07-06 14:20:14 +0200 | [diff] [blame] | 1290 | #define sched_feat(x) (static_branch_##x(&sched_feat_keys[__SCHED_FEAT_##x])) |
Patrick Bellasi | 765cc3a | 2017-11-08 18:41:01 +0000 | [diff] [blame] | 1291 | |
Peter Zijlstra | f8b6d1c | 2011-07-06 14:20:14 +0200 | [diff] [blame] | 1292 | #else /* !(SCHED_DEBUG && HAVE_JUMP_LABEL) */ |
Patrick Bellasi | 765cc3a | 2017-11-08 18:41:01 +0000 | [diff] [blame] | 1293 | |
| 1294 | /* |
| 1295 | * Each translation unit has its own copy of sysctl_sched_features to allow |
| 1296 | * constants propagation at compile time and compiler optimization based on |
| 1297 | * features default. |
| 1298 | */ |
| 1299 | #define SCHED_FEAT(name, enabled) \ |
| 1300 | (1UL << __SCHED_FEAT_##name) * enabled | |
| 1301 | static const_debug __maybe_unused unsigned int sysctl_sched_features = |
| 1302 | #include "features.h" |
| 1303 | 0; |
| 1304 | #undef SCHED_FEAT |
| 1305 | |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 1306 | #define sched_feat(x) (sysctl_sched_features & (1UL << __SCHED_FEAT_##x)) |
Patrick Bellasi | 765cc3a | 2017-11-08 18:41:01 +0000 | [diff] [blame] | 1307 | |
Peter Zijlstra | f8b6d1c | 2011-07-06 14:20:14 +0200 | [diff] [blame] | 1308 | #endif /* SCHED_DEBUG && HAVE_JUMP_LABEL */ |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 1309 | |
Srikar Dronamraju | 2a59572 | 2015-08-11 21:54:21 +0530 | [diff] [blame] | 1310 | extern struct static_key_false sched_numa_balancing; |
Mel Gorman | cb25176 | 2016-02-05 09:08:36 +0000 | [diff] [blame] | 1311 | extern struct static_key_false sched_schedstats; |
Peter Zijlstra | cbee9f8 | 2012-10-25 14:16:43 +0200 | [diff] [blame] | 1312 | |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 1313 | static inline u64 global_rt_period(void) |
| 1314 | { |
| 1315 | return (u64)sysctl_sched_rt_period * NSEC_PER_USEC; |
| 1316 | } |
| 1317 | |
| 1318 | static inline u64 global_rt_runtime(void) |
| 1319 | { |
| 1320 | if (sysctl_sched_rt_runtime < 0) |
| 1321 | return RUNTIME_INF; |
| 1322 | |
| 1323 | return (u64)sysctl_sched_rt_runtime * NSEC_PER_USEC; |
| 1324 | } |
| 1325 | |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 1326 | static inline int task_current(struct rq *rq, struct task_struct *p) |
| 1327 | { |
| 1328 | return rq->curr == p; |
| 1329 | } |
| 1330 | |
| 1331 | static inline int task_running(struct rq *rq, struct task_struct *p) |
| 1332 | { |
| 1333 | #ifdef CONFIG_SMP |
| 1334 | return p->on_cpu; |
| 1335 | #else |
| 1336 | return task_current(rq, p); |
| 1337 | #endif |
| 1338 | } |
| 1339 | |
Kirill Tkhai | da0c1e6 | 2014-08-20 13:47:32 +0400 | [diff] [blame] | 1340 | static inline int task_on_rq_queued(struct task_struct *p) |
| 1341 | { |
| 1342 | return p->on_rq == TASK_ON_RQ_QUEUED; |
| 1343 | } |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 1344 | |
Kirill Tkhai | cca26e8 | 2014-08-20 13:47:42 +0400 | [diff] [blame] | 1345 | static inline int task_on_rq_migrating(struct task_struct *p) |
| 1346 | { |
| 1347 | return p->on_rq == TASK_ON_RQ_MIGRATING; |
| 1348 | } |
| 1349 | |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 1350 | #ifndef prepare_arch_switch |
| 1351 | # define prepare_arch_switch(next) do { } while (0) |
| 1352 | #endif |
Catalin Marinas | 01f23e1 | 2011-11-27 21:43:10 +0000 | [diff] [blame] | 1353 | #ifndef finish_arch_post_lock_switch |
| 1354 | # define finish_arch_post_lock_switch() do { } while (0) |
| 1355 | #endif |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 1356 | |
Li Zefan | b13095f | 2013-03-05 16:06:38 +0800 | [diff] [blame] | 1357 | /* |
| 1358 | * wake flags |
| 1359 | */ |
| 1360 | #define WF_SYNC 0x01 /* waker goes to sleep after wakeup */ |
| 1361 | #define WF_FORK 0x02 /* child wakeup after fork */ |
| 1362 | #define WF_MIGRATED 0x4 /* internal use, task got migrated */ |
| 1363 | |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 1364 | /* |
| 1365 | * To aid in avoiding the subversion of "niceness" due to uneven distribution |
| 1366 | * of tasks with abnormal "nice" values across CPUs the contribution that |
| 1367 | * each task makes to its run queue's load is weighted according to its |
| 1368 | * scheduling class and "nice" value. For SCHED_NORMAL tasks this is just a |
| 1369 | * scaled version of the new time slice allocation that they receive on time |
| 1370 | * slice expiry etc. |
| 1371 | */ |
| 1372 | |
| 1373 | #define WEIGHT_IDLEPRIO 3 |
| 1374 | #define WMULT_IDLEPRIO 1431655765 |
| 1375 | |
Andi Kleen | ed82b8a | 2015-11-29 20:59:43 -0800 | [diff] [blame] | 1376 | extern const int sched_prio_to_weight[40]; |
| 1377 | extern const u32 sched_prio_to_wmult[40]; |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 1378 | |
Peter Zijlstra | ff77e46 | 2016-01-18 15:27:07 +0100 | [diff] [blame] | 1379 | /* |
| 1380 | * {de,en}queue flags: |
| 1381 | * |
| 1382 | * DEQUEUE_SLEEP - task is no longer runnable |
| 1383 | * ENQUEUE_WAKEUP - task just became runnable |
| 1384 | * |
| 1385 | * SAVE/RESTORE - an otherwise spurious dequeue/enqueue, done to ensure tasks |
| 1386 | * are in a known state which allows modification. Such pairs |
| 1387 | * should preserve as much state as possible. |
| 1388 | * |
| 1389 | * MOVE - paired with SAVE/RESTORE, explicitly does not preserve the location |
| 1390 | * in the runqueue. |
| 1391 | * |
| 1392 | * ENQUEUE_HEAD - place at front of runqueue (tail if not specified) |
| 1393 | * ENQUEUE_REPLENISH - CBS (replenish runtime and postpone deadline) |
Peter Zijlstra | 59efa0b | 2016-05-10 18:24:37 +0200 | [diff] [blame] | 1394 | * ENQUEUE_MIGRATED - the task was migrated during wakeup |
Peter Zijlstra | ff77e46 | 2016-01-18 15:27:07 +0100 | [diff] [blame] | 1395 | * |
| 1396 | */ |
| 1397 | |
| 1398 | #define DEQUEUE_SLEEP 0x01 |
| 1399 | #define DEQUEUE_SAVE 0x02 /* matches ENQUEUE_RESTORE */ |
| 1400 | #define DEQUEUE_MOVE 0x04 /* matches ENQUEUE_MOVE */ |
Peter Zijlstra | 0a67d1e | 2016-10-04 16:29:45 +0200 | [diff] [blame] | 1401 | #define DEQUEUE_NOCLOCK 0x08 /* matches ENQUEUE_NOCLOCK */ |
Peter Zijlstra | ff77e46 | 2016-01-18 15:27:07 +0100 | [diff] [blame] | 1402 | |
Peter Zijlstra | 1de6444 | 2015-09-30 17:44:13 +0200 | [diff] [blame] | 1403 | #define ENQUEUE_WAKEUP 0x01 |
Peter Zijlstra | ff77e46 | 2016-01-18 15:27:07 +0100 | [diff] [blame] | 1404 | #define ENQUEUE_RESTORE 0x02 |
| 1405 | #define ENQUEUE_MOVE 0x04 |
Peter Zijlstra | 0a67d1e | 2016-10-04 16:29:45 +0200 | [diff] [blame] | 1406 | #define ENQUEUE_NOCLOCK 0x08 |
Peter Zijlstra | ff77e46 | 2016-01-18 15:27:07 +0100 | [diff] [blame] | 1407 | |
Peter Zijlstra | 0a67d1e | 2016-10-04 16:29:45 +0200 | [diff] [blame] | 1408 | #define ENQUEUE_HEAD 0x10 |
| 1409 | #define ENQUEUE_REPLENISH 0x20 |
Li Zefan | c82ba9f | 2013-03-05 16:06:55 +0800 | [diff] [blame] | 1410 | #ifdef CONFIG_SMP |
Peter Zijlstra | 0a67d1e | 2016-10-04 16:29:45 +0200 | [diff] [blame] | 1411 | #define ENQUEUE_MIGRATED 0x40 |
Li Zefan | c82ba9f | 2013-03-05 16:06:55 +0800 | [diff] [blame] | 1412 | #else |
Peter Zijlstra | 59efa0b | 2016-05-10 18:24:37 +0200 | [diff] [blame] | 1413 | #define ENQUEUE_MIGRATED 0x00 |
Li Zefan | c82ba9f | 2013-03-05 16:06:55 +0800 | [diff] [blame] | 1414 | #endif |
Li Zefan | c82ba9f | 2013-03-05 16:06:55 +0800 | [diff] [blame] | 1415 | |
Peter Zijlstra | 37e117c | 2014-02-14 12:25:08 +0100 | [diff] [blame] | 1416 | #define RETRY_TASK ((void *)-1UL) |
| 1417 | |
Li Zefan | c82ba9f | 2013-03-05 16:06:55 +0800 | [diff] [blame] | 1418 | struct sched_class { |
| 1419 | const struct sched_class *next; |
| 1420 | |
| 1421 | void (*enqueue_task) (struct rq *rq, struct task_struct *p, int flags); |
| 1422 | void (*dequeue_task) (struct rq *rq, struct task_struct *p, int flags); |
| 1423 | void (*yield_task) (struct rq *rq); |
| 1424 | bool (*yield_to_task) (struct rq *rq, struct task_struct *p, bool preempt); |
| 1425 | |
| 1426 | void (*check_preempt_curr) (struct rq *rq, struct task_struct *p, int flags); |
| 1427 | |
Peter Zijlstra | 606dba2 | 2012-02-11 06:05:00 +0100 | [diff] [blame] | 1428 | /* |
| 1429 | * It is the responsibility of the pick_next_task() method that will |
| 1430 | * return the next task to call put_prev_task() on the @prev task or |
| 1431 | * something equivalent. |
Peter Zijlstra | 37e117c | 2014-02-14 12:25:08 +0100 | [diff] [blame] | 1432 | * |
| 1433 | * May return RETRY_TASK when it finds a higher prio class has runnable |
| 1434 | * tasks. |
Peter Zijlstra | 606dba2 | 2012-02-11 06:05:00 +0100 | [diff] [blame] | 1435 | */ |
| 1436 | struct task_struct * (*pick_next_task) (struct rq *rq, |
Peter Zijlstra | e7904a2 | 2015-08-01 19:25:08 +0200 | [diff] [blame] | 1437 | struct task_struct *prev, |
Matt Fleming | d8ac897 | 2016-09-21 14:38:10 +0100 | [diff] [blame] | 1438 | struct rq_flags *rf); |
Li Zefan | c82ba9f | 2013-03-05 16:06:55 +0800 | [diff] [blame] | 1439 | void (*put_prev_task) (struct rq *rq, struct task_struct *p); |
| 1440 | |
| 1441 | #ifdef CONFIG_SMP |
Peter Zijlstra | ac66f54 | 2013-10-07 11:29:16 +0100 | [diff] [blame] | 1442 | int (*select_task_rq)(struct task_struct *p, int task_cpu, int sd_flag, int flags); |
xiaofeng.yan | 5a4fd03 | 2015-09-23 14:55:59 +0800 | [diff] [blame] | 1443 | void (*migrate_task_rq)(struct task_struct *p); |
Li Zefan | c82ba9f | 2013-03-05 16:06:55 +0800 | [diff] [blame] | 1444 | |
Li Zefan | c82ba9f | 2013-03-05 16:06:55 +0800 | [diff] [blame] | 1445 | void (*task_woken) (struct rq *this_rq, struct task_struct *task); |
| 1446 | |
| 1447 | void (*set_cpus_allowed)(struct task_struct *p, |
| 1448 | const struct cpumask *newmask); |
| 1449 | |
| 1450 | void (*rq_online)(struct rq *rq); |
| 1451 | void (*rq_offline)(struct rq *rq); |
| 1452 | #endif |
| 1453 | |
| 1454 | void (*set_curr_task) (struct rq *rq); |
| 1455 | void (*task_tick) (struct rq *rq, struct task_struct *p, int queued); |
| 1456 | void (*task_fork) (struct task_struct *p); |
Dario Faggioli | e6c390f | 2013-11-07 14:43:35 +0100 | [diff] [blame] | 1457 | void (*task_dead) (struct task_struct *p); |
Li Zefan | c82ba9f | 2013-03-05 16:06:55 +0800 | [diff] [blame] | 1458 | |
Kirill Tkhai | 67dfa1b | 2014-10-27 17:40:52 +0300 | [diff] [blame] | 1459 | /* |
| 1460 | * The switched_from() call is allowed to drop rq->lock, therefore we |
| 1461 | * cannot assume the switched_from/switched_to pair is serliazed by |
| 1462 | * rq->lock. They are however serialized by p->pi_lock. |
| 1463 | */ |
Li Zefan | c82ba9f | 2013-03-05 16:06:55 +0800 | [diff] [blame] | 1464 | void (*switched_from) (struct rq *this_rq, struct task_struct *task); |
| 1465 | void (*switched_to) (struct rq *this_rq, struct task_struct *task); |
| 1466 | void (*prio_changed) (struct rq *this_rq, struct task_struct *task, |
| 1467 | int oldprio); |
| 1468 | |
| 1469 | unsigned int (*get_rr_interval) (struct rq *rq, |
| 1470 | struct task_struct *task); |
| 1471 | |
Stanislaw Gruszka | 6e99891 | 2014-11-12 16:58:44 +0100 | [diff] [blame] | 1472 | void (*update_curr) (struct rq *rq); |
| 1473 | |
Vincent Guittot | ea86cb4 | 2016-06-17 13:38:55 +0200 | [diff] [blame] | 1474 | #define TASK_SET_GROUP 0 |
| 1475 | #define TASK_MOVE_GROUP 1 |
| 1476 | |
Li Zefan | c82ba9f | 2013-03-05 16:06:55 +0800 | [diff] [blame] | 1477 | #ifdef CONFIG_FAIR_GROUP_SCHED |
Vincent Guittot | ea86cb4 | 2016-06-17 13:38:55 +0200 | [diff] [blame] | 1478 | void (*task_change_group) (struct task_struct *p, int type); |
Li Zefan | c82ba9f | 2013-03-05 16:06:55 +0800 | [diff] [blame] | 1479 | #endif |
| 1480 | }; |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 1481 | |
Peter Zijlstra | 3f1d2a3 | 2014-02-12 10:49:30 +0100 | [diff] [blame] | 1482 | static inline void put_prev_task(struct rq *rq, struct task_struct *prev) |
| 1483 | { |
| 1484 | prev->sched_class->put_prev_task(rq, prev); |
| 1485 | } |
| 1486 | |
Peter Zijlstra | b2bf6c3 | 2016-09-20 22:00:38 +0200 | [diff] [blame] | 1487 | static inline void set_curr_task(struct rq *rq, struct task_struct *curr) |
| 1488 | { |
| 1489 | curr->sched_class->set_curr_task(rq); |
| 1490 | } |
| 1491 | |
Nicolas Pitre | f5832c1 | 2017-05-29 17:02:57 -0400 | [diff] [blame] | 1492 | #ifdef CONFIG_SMP |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 1493 | #define sched_class_highest (&stop_sched_class) |
Nicolas Pitre | f5832c1 | 2017-05-29 17:02:57 -0400 | [diff] [blame] | 1494 | #else |
| 1495 | #define sched_class_highest (&dl_sched_class) |
| 1496 | #endif |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 1497 | #define for_each_class(class) \ |
| 1498 | for (class = sched_class_highest; class; class = class->next) |
| 1499 | |
| 1500 | extern const struct sched_class stop_sched_class; |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 1501 | extern const struct sched_class dl_sched_class; |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 1502 | extern const struct sched_class rt_sched_class; |
| 1503 | extern const struct sched_class fair_sched_class; |
| 1504 | extern const struct sched_class idle_sched_class; |
| 1505 | |
| 1506 | |
| 1507 | #ifdef CONFIG_SMP |
| 1508 | |
Nicolas Pitre | 63b2ca3 | 2014-05-26 18:19:37 -0400 | [diff] [blame] | 1509 | extern void update_group_capacity(struct sched_domain *sd, int cpu); |
Li Zefan | b719203 | 2013-03-07 10:00:26 +0800 | [diff] [blame] | 1510 | |
Daniel Lezcano | 7caff66 | 2014-01-06 12:34:38 +0100 | [diff] [blame] | 1511 | extern void trigger_load_balance(struct rq *rq); |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 1512 | |
Peter Zijlstra | c5b2803 | 2015-05-15 17:43:35 +0200 | [diff] [blame] | 1513 | extern void set_cpus_allowed_common(struct task_struct *p, const struct cpumask *new_mask); |
| 1514 | |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 1515 | #endif |
| 1516 | |
Daniel Lezcano | 442bf3a | 2014-09-04 11:32:09 -0400 | [diff] [blame] | 1517 | #ifdef CONFIG_CPU_IDLE |
| 1518 | static inline void idle_set_state(struct rq *rq, |
| 1519 | struct cpuidle_state *idle_state) |
| 1520 | { |
| 1521 | rq->idle_state = idle_state; |
| 1522 | } |
| 1523 | |
| 1524 | static inline struct cpuidle_state *idle_get_state(struct rq *rq) |
| 1525 | { |
Peter Zijlstra | 9148a3a | 2016-09-20 22:34:51 +0200 | [diff] [blame] | 1526 | SCHED_WARN_ON(!rcu_read_lock_held()); |
Daniel Lezcano | 442bf3a | 2014-09-04 11:32:09 -0400 | [diff] [blame] | 1527 | return rq->idle_state; |
| 1528 | } |
| 1529 | #else |
| 1530 | static inline void idle_set_state(struct rq *rq, |
| 1531 | struct cpuidle_state *idle_state) |
| 1532 | { |
| 1533 | } |
| 1534 | |
| 1535 | static inline struct cpuidle_state *idle_get_state(struct rq *rq) |
| 1536 | { |
| 1537 | return NULL; |
| 1538 | } |
| 1539 | #endif |
| 1540 | |
Steven Rostedt (VMware) | 8663eff | 2017-04-14 08:48:09 -0400 | [diff] [blame] | 1541 | extern void schedule_idle(void); |
| 1542 | |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 1543 | extern void sysrq_sched_debug_show(void); |
| 1544 | extern void sched_init_granularity(void); |
| 1545 | extern void update_max_interval(void); |
Juri Lelli | 1baca4c | 2013-11-07 14:43:38 +0100 | [diff] [blame] | 1546 | |
| 1547 | extern void init_sched_dl_class(void); |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 1548 | extern void init_sched_rt_class(void); |
| 1549 | extern void init_sched_fair_class(void); |
| 1550 | |
Vincent Guittot | 9059393 | 2017-05-17 11:50:45 +0200 | [diff] [blame] | 1551 | extern void reweight_task(struct task_struct *p, int prio); |
| 1552 | |
Kirill Tkhai | 8875125 | 2014-06-29 00:03:57 +0400 | [diff] [blame] | 1553 | extern void resched_curr(struct rq *rq); |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 1554 | extern void resched_cpu(int cpu); |
| 1555 | |
| 1556 | extern struct rt_bandwidth def_rt_bandwidth; |
| 1557 | extern void init_rt_bandwidth(struct rt_bandwidth *rt_b, u64 period, u64 runtime); |
| 1558 | |
Dario Faggioli | 332ac17 | 2013-11-07 14:43:45 +0100 | [diff] [blame] | 1559 | extern struct dl_bandwidth def_dl_bandwidth; |
| 1560 | extern void init_dl_bandwidth(struct dl_bandwidth *dl_b, u64 period, u64 runtime); |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 1561 | extern void init_dl_task_timer(struct sched_dl_entity *dl_se); |
Luca Abeni | 209a0cb | 2017-05-18 22:13:29 +0200 | [diff] [blame] | 1562 | extern void init_dl_inactive_task_timer(struct sched_dl_entity *dl_se); |
Luca Abeni | 4da3abc | 2017-05-18 22:13:32 +0200 | [diff] [blame] | 1563 | extern void init_dl_rq_bw_ratio(struct dl_rq *dl_rq); |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 1564 | |
Luca Abeni | c52f14d | 2017-05-18 22:13:31 +0200 | [diff] [blame] | 1565 | #define BW_SHIFT 20 |
| 1566 | #define BW_UNIT (1 << BW_SHIFT) |
Luca Abeni | 4da3abc | 2017-05-18 22:13:32 +0200 | [diff] [blame] | 1567 | #define RATIO_SHIFT 8 |
Dario Faggioli | 332ac17 | 2013-11-07 14:43:45 +0100 | [diff] [blame] | 1568 | unsigned long to_ratio(u64 period, u64 runtime); |
| 1569 | |
Yuyang Du | 540247f | 2015-07-15 08:04:39 +0800 | [diff] [blame] | 1570 | extern void init_entity_runnable_average(struct sched_entity *se); |
Yuyang Du | 2b8c41d | 2016-03-30 04:30:56 +0800 | [diff] [blame] | 1571 | extern void post_init_entity_util_avg(struct sched_entity *se); |
Alex Shi | a75cdaa | 2013-06-20 10:18:47 +0800 | [diff] [blame] | 1572 | |
Frederic Weisbecker | 76d92ac | 2015-07-17 22:25:49 +0200 | [diff] [blame] | 1573 | #ifdef CONFIG_NO_HZ_FULL |
| 1574 | extern bool sched_can_stop_tick(struct rq *rq); |
Frederic Weisbecker | d84b313 | 2018-02-21 05:17:27 +0100 | [diff] [blame] | 1575 | extern int __init sched_tick_offload_init(void); |
Frederic Weisbecker | 76d92ac | 2015-07-17 22:25:49 +0200 | [diff] [blame] | 1576 | |
| 1577 | /* |
| 1578 | * Tick may be needed by tasks in the runqueue depending on their policy and |
| 1579 | * requirements. If tick is needed, lets send the target an IPI to kick it out of |
| 1580 | * nohz mode if necessary. |
| 1581 | */ |
| 1582 | static inline void sched_update_tick_dependency(struct rq *rq) |
| 1583 | { |
| 1584 | int cpu; |
| 1585 | |
| 1586 | if (!tick_nohz_full_enabled()) |
| 1587 | return; |
| 1588 | |
| 1589 | cpu = cpu_of(rq); |
| 1590 | |
| 1591 | if (!tick_nohz_full_cpu(cpu)) |
| 1592 | return; |
| 1593 | |
| 1594 | if (sched_can_stop_tick(rq)) |
| 1595 | tick_nohz_dep_clear_cpu(cpu, TICK_DEP_BIT_SCHED); |
| 1596 | else |
| 1597 | tick_nohz_dep_set_cpu(cpu, TICK_DEP_BIT_SCHED); |
| 1598 | } |
| 1599 | #else |
Frederic Weisbecker | d84b313 | 2018-02-21 05:17:27 +0100 | [diff] [blame] | 1600 | static inline int sched_tick_offload_init(void) { return 0; } |
Frederic Weisbecker | 76d92ac | 2015-07-17 22:25:49 +0200 | [diff] [blame] | 1601 | static inline void sched_update_tick_dependency(struct rq *rq) { } |
| 1602 | #endif |
| 1603 | |
Kirill Tkhai | 7246544 | 2014-05-09 03:00:14 +0400 | [diff] [blame] | 1604 | static inline void add_nr_running(struct rq *rq, unsigned count) |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 1605 | { |
Kirill Tkhai | 7246544 | 2014-05-09 03:00:14 +0400 | [diff] [blame] | 1606 | unsigned prev_nr = rq->nr_running; |
| 1607 | |
| 1608 | rq->nr_running = prev_nr + count; |
Frederic Weisbecker | 9f3660c | 2013-04-20 14:35:09 +0200 | [diff] [blame] | 1609 | |
Kirill Tkhai | 7246544 | 2014-05-09 03:00:14 +0400 | [diff] [blame] | 1610 | if (prev_nr < 2 && rq->nr_running >= 2) { |
Tim Chen | 4486edd | 2014-06-23 12:16:49 -0700 | [diff] [blame] | 1611 | #ifdef CONFIG_SMP |
| 1612 | if (!rq->rd->overload) |
| 1613 | rq->rd->overload = true; |
| 1614 | #endif |
Tim Chen | 4486edd | 2014-06-23 12:16:49 -0700 | [diff] [blame] | 1615 | } |
Frederic Weisbecker | 76d92ac | 2015-07-17 22:25:49 +0200 | [diff] [blame] | 1616 | |
| 1617 | sched_update_tick_dependency(rq); |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 1618 | } |
| 1619 | |
Kirill Tkhai | 7246544 | 2014-05-09 03:00:14 +0400 | [diff] [blame] | 1620 | static inline void sub_nr_running(struct rq *rq, unsigned count) |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 1621 | { |
Kirill Tkhai | 7246544 | 2014-05-09 03:00:14 +0400 | [diff] [blame] | 1622 | rq->nr_running -= count; |
Frederic Weisbecker | 76d92ac | 2015-07-17 22:25:49 +0200 | [diff] [blame] | 1623 | /* Check if we still need preemption */ |
| 1624 | sched_update_tick_dependency(rq); |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 1625 | } |
| 1626 | |
| 1627 | extern void update_rq_clock(struct rq *rq); |
| 1628 | |
| 1629 | extern void activate_task(struct rq *rq, struct task_struct *p, int flags); |
| 1630 | extern void deactivate_task(struct rq *rq, struct task_struct *p, int flags); |
| 1631 | |
| 1632 | extern void check_preempt_curr(struct rq *rq, struct task_struct *p, int flags); |
| 1633 | |
| 1634 | extern const_debug unsigned int sysctl_sched_time_avg; |
| 1635 | extern const_debug unsigned int sysctl_sched_nr_migrate; |
| 1636 | extern const_debug unsigned int sysctl_sched_migration_cost; |
| 1637 | |
| 1638 | static inline u64 sched_avg_period(void) |
| 1639 | { |
| 1640 | return (u64)sysctl_sched_time_avg * NSEC_PER_MSEC / 2; |
| 1641 | } |
| 1642 | |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 1643 | #ifdef CONFIG_SCHED_HRTICK |
| 1644 | |
| 1645 | /* |
| 1646 | * Use hrtick when: |
| 1647 | * - enabled by features |
| 1648 | * - hrtimer is actually high res |
| 1649 | */ |
| 1650 | static inline int hrtick_enabled(struct rq *rq) |
| 1651 | { |
| 1652 | if (!sched_feat(HRTICK)) |
| 1653 | return 0; |
| 1654 | if (!cpu_active(cpu_of(rq))) |
| 1655 | return 0; |
| 1656 | return hrtimer_is_hres_active(&rq->hrtick_timer); |
| 1657 | } |
| 1658 | |
| 1659 | void hrtick_start(struct rq *rq, u64 delay); |
| 1660 | |
Mike Galbraith | b39e66e | 2011-11-22 15:20:07 +0100 | [diff] [blame] | 1661 | #else |
| 1662 | |
| 1663 | static inline int hrtick_enabled(struct rq *rq) |
| 1664 | { |
| 1665 | return 0; |
| 1666 | } |
| 1667 | |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 1668 | #endif /* CONFIG_SCHED_HRTICK */ |
| 1669 | |
Peter Zijlstra | dfbca41 | 2015-03-23 14:19:05 +0100 | [diff] [blame] | 1670 | #ifndef arch_scale_freq_capacity |
| 1671 | static __always_inline |
Juri Lelli | 7673c8a | 2017-12-04 11:23:23 +0100 | [diff] [blame] | 1672 | unsigned long arch_scale_freq_capacity(int cpu) |
Peter Zijlstra | dfbca41 | 2015-03-23 14:19:05 +0100 | [diff] [blame] | 1673 | { |
| 1674 | return SCHED_CAPACITY_SCALE; |
| 1675 | } |
| 1676 | #endif |
Vincent Guittot | b5b4860 | 2015-02-27 16:54:08 +0100 | [diff] [blame] | 1677 | |
Juri Lelli | 7e1a920 | 2017-12-04 11:23:24 +0100 | [diff] [blame] | 1678 | #ifdef CONFIG_SMP |
| 1679 | extern void sched_avg_update(struct rq *rq); |
| 1680 | |
Morten Rasmussen | 8cd5601 | 2015-08-14 17:23:10 +0100 | [diff] [blame] | 1681 | #ifndef arch_scale_cpu_capacity |
| 1682 | static __always_inline |
| 1683 | unsigned long arch_scale_cpu_capacity(struct sched_domain *sd, int cpu) |
| 1684 | { |
Dietmar Eggemann | e3279a2 | 2015-08-15 00:04:41 +0100 | [diff] [blame] | 1685 | if (sd && (sd->flags & SD_SHARE_CPUCAPACITY) && (sd->span_weight > 1)) |
Morten Rasmussen | 8cd5601 | 2015-08-14 17:23:10 +0100 | [diff] [blame] | 1686 | return sd->smt_gain / sd->span_weight; |
| 1687 | |
| 1688 | return SCHED_CAPACITY_SCALE; |
| 1689 | } |
| 1690 | #endif |
| 1691 | |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 1692 | static inline void sched_rt_avg_update(struct rq *rq, u64 rt_delta) |
| 1693 | { |
Juri Lelli | 7673c8a | 2017-12-04 11:23:23 +0100 | [diff] [blame] | 1694 | rq->rt_avg += rt_delta * arch_scale_freq_capacity(cpu_of(rq)); |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 1695 | sched_avg_update(rq); |
| 1696 | } |
| 1697 | #else |
Juri Lelli | 7e1a920 | 2017-12-04 11:23:24 +0100 | [diff] [blame] | 1698 | #ifndef arch_scale_cpu_capacity |
| 1699 | static __always_inline |
| 1700 | unsigned long arch_scale_cpu_capacity(void __always_unused *sd, int cpu) |
| 1701 | { |
| 1702 | return SCHED_CAPACITY_SCALE; |
| 1703 | } |
| 1704 | #endif |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 1705 | static inline void sched_rt_avg_update(struct rq *rq, u64 rt_delta) { } |
| 1706 | static inline void sched_avg_update(struct rq *rq) { } |
| 1707 | #endif |
| 1708 | |
Peter Zijlstra | eb58075 | 2015-07-31 21:28:18 +0200 | [diff] [blame] | 1709 | struct rq *__task_rq_lock(struct task_struct *p, struct rq_flags *rf) |
Peter Zijlstra | 3e71a46 | 2016-04-28 16:16:33 +0200 | [diff] [blame] | 1710 | __acquires(rq->lock); |
Peter Zijlstra | 8a8c69c | 2016-10-04 16:04:35 +0200 | [diff] [blame] | 1711 | |
Peter Zijlstra | eb58075 | 2015-07-31 21:28:18 +0200 | [diff] [blame] | 1712 | struct rq *task_rq_lock(struct task_struct *p, struct rq_flags *rf) |
Peter Zijlstra | 3960c8c | 2015-02-17 13:22:25 +0100 | [diff] [blame] | 1713 | __acquires(p->pi_lock) |
Peter Zijlstra | 3e71a46 | 2016-04-28 16:16:33 +0200 | [diff] [blame] | 1714 | __acquires(rq->lock); |
Peter Zijlstra | 3960c8c | 2015-02-17 13:22:25 +0100 | [diff] [blame] | 1715 | |
Peter Zijlstra | eb58075 | 2015-07-31 21:28:18 +0200 | [diff] [blame] | 1716 | static inline void __task_rq_unlock(struct rq *rq, struct rq_flags *rf) |
Peter Zijlstra | 3960c8c | 2015-02-17 13:22:25 +0100 | [diff] [blame] | 1717 | __releases(rq->lock) |
| 1718 | { |
Matt Fleming | d8ac897 | 2016-09-21 14:38:10 +0100 | [diff] [blame] | 1719 | rq_unpin_lock(rq, rf); |
Peter Zijlstra | 3960c8c | 2015-02-17 13:22:25 +0100 | [diff] [blame] | 1720 | raw_spin_unlock(&rq->lock); |
| 1721 | } |
| 1722 | |
| 1723 | static inline void |
Peter Zijlstra | eb58075 | 2015-07-31 21:28:18 +0200 | [diff] [blame] | 1724 | task_rq_unlock(struct rq *rq, struct task_struct *p, struct rq_flags *rf) |
Peter Zijlstra | 3960c8c | 2015-02-17 13:22:25 +0100 | [diff] [blame] | 1725 | __releases(rq->lock) |
| 1726 | __releases(p->pi_lock) |
| 1727 | { |
Matt Fleming | d8ac897 | 2016-09-21 14:38:10 +0100 | [diff] [blame] | 1728 | rq_unpin_lock(rq, rf); |
Peter Zijlstra | 3960c8c | 2015-02-17 13:22:25 +0100 | [diff] [blame] | 1729 | raw_spin_unlock(&rq->lock); |
Peter Zijlstra | eb58075 | 2015-07-31 21:28:18 +0200 | [diff] [blame] | 1730 | raw_spin_unlock_irqrestore(&p->pi_lock, rf->flags); |
Peter Zijlstra | 3960c8c | 2015-02-17 13:22:25 +0100 | [diff] [blame] | 1731 | } |
| 1732 | |
Peter Zijlstra | 8a8c69c | 2016-10-04 16:04:35 +0200 | [diff] [blame] | 1733 | static inline void |
| 1734 | rq_lock_irqsave(struct rq *rq, struct rq_flags *rf) |
| 1735 | __acquires(rq->lock) |
| 1736 | { |
| 1737 | raw_spin_lock_irqsave(&rq->lock, rf->flags); |
| 1738 | rq_pin_lock(rq, rf); |
| 1739 | } |
| 1740 | |
| 1741 | static inline void |
| 1742 | rq_lock_irq(struct rq *rq, struct rq_flags *rf) |
| 1743 | __acquires(rq->lock) |
| 1744 | { |
| 1745 | raw_spin_lock_irq(&rq->lock); |
| 1746 | rq_pin_lock(rq, rf); |
| 1747 | } |
| 1748 | |
| 1749 | static inline void |
| 1750 | rq_lock(struct rq *rq, struct rq_flags *rf) |
| 1751 | __acquires(rq->lock) |
| 1752 | { |
| 1753 | raw_spin_lock(&rq->lock); |
| 1754 | rq_pin_lock(rq, rf); |
| 1755 | } |
| 1756 | |
| 1757 | static inline void |
| 1758 | rq_relock(struct rq *rq, struct rq_flags *rf) |
| 1759 | __acquires(rq->lock) |
| 1760 | { |
| 1761 | raw_spin_lock(&rq->lock); |
| 1762 | rq_repin_lock(rq, rf); |
| 1763 | } |
| 1764 | |
| 1765 | static inline void |
| 1766 | rq_unlock_irqrestore(struct rq *rq, struct rq_flags *rf) |
| 1767 | __releases(rq->lock) |
| 1768 | { |
| 1769 | rq_unpin_lock(rq, rf); |
| 1770 | raw_spin_unlock_irqrestore(&rq->lock, rf->flags); |
| 1771 | } |
| 1772 | |
| 1773 | static inline void |
| 1774 | rq_unlock_irq(struct rq *rq, struct rq_flags *rf) |
| 1775 | __releases(rq->lock) |
| 1776 | { |
| 1777 | rq_unpin_lock(rq, rf); |
| 1778 | raw_spin_unlock_irq(&rq->lock); |
| 1779 | } |
| 1780 | |
| 1781 | static inline void |
| 1782 | rq_unlock(struct rq *rq, struct rq_flags *rf) |
| 1783 | __releases(rq->lock) |
| 1784 | { |
| 1785 | rq_unpin_lock(rq, rf); |
| 1786 | raw_spin_unlock(&rq->lock); |
| 1787 | } |
| 1788 | |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 1789 | #ifdef CONFIG_SMP |
| 1790 | #ifdef CONFIG_PREEMPT |
| 1791 | |
| 1792 | static inline void double_rq_lock(struct rq *rq1, struct rq *rq2); |
| 1793 | |
| 1794 | /* |
| 1795 | * fair double_lock_balance: Safely acquires both rq->locks in a fair |
| 1796 | * way at the expense of forcing extra atomic operations in all |
| 1797 | * invocations. This assures that the double_lock is acquired using the |
| 1798 | * same underlying policy as the spinlock_t on this architecture, which |
| 1799 | * reduces latency compared to the unfair variant below. However, it |
| 1800 | * also adds more overhead and therefore may reduce throughput. |
| 1801 | */ |
| 1802 | static inline int _double_lock_balance(struct rq *this_rq, struct rq *busiest) |
| 1803 | __releases(this_rq->lock) |
| 1804 | __acquires(busiest->lock) |
| 1805 | __acquires(this_rq->lock) |
| 1806 | { |
| 1807 | raw_spin_unlock(&this_rq->lock); |
| 1808 | double_rq_lock(this_rq, busiest); |
| 1809 | |
| 1810 | return 1; |
| 1811 | } |
| 1812 | |
| 1813 | #else |
| 1814 | /* |
| 1815 | * Unfair double_lock_balance: Optimizes throughput at the expense of |
| 1816 | * latency by eliminating extra atomic operations when the locks are |
| 1817 | * already in proper order on entry. This favors lower cpu-ids and will |
| 1818 | * grant the double lock to lower cpus over higher ids under contention, |
| 1819 | * regardless of entry order into the function. |
| 1820 | */ |
| 1821 | static inline int _double_lock_balance(struct rq *this_rq, struct rq *busiest) |
| 1822 | __releases(this_rq->lock) |
| 1823 | __acquires(busiest->lock) |
| 1824 | __acquires(this_rq->lock) |
| 1825 | { |
| 1826 | int ret = 0; |
| 1827 | |
| 1828 | if (unlikely(!raw_spin_trylock(&busiest->lock))) { |
| 1829 | if (busiest < this_rq) { |
| 1830 | raw_spin_unlock(&this_rq->lock); |
| 1831 | raw_spin_lock(&busiest->lock); |
| 1832 | raw_spin_lock_nested(&this_rq->lock, |
| 1833 | SINGLE_DEPTH_NESTING); |
| 1834 | ret = 1; |
| 1835 | } else |
| 1836 | raw_spin_lock_nested(&busiest->lock, |
| 1837 | SINGLE_DEPTH_NESTING); |
| 1838 | } |
| 1839 | return ret; |
| 1840 | } |
| 1841 | |
| 1842 | #endif /* CONFIG_PREEMPT */ |
| 1843 | |
| 1844 | /* |
| 1845 | * double_lock_balance - lock the busiest runqueue, this_rq is locked already. |
| 1846 | */ |
| 1847 | static inline int double_lock_balance(struct rq *this_rq, struct rq *busiest) |
| 1848 | { |
| 1849 | if (unlikely(!irqs_disabled())) { |
| 1850 | /* printk() doesn't work good under rq->lock */ |
| 1851 | raw_spin_unlock(&this_rq->lock); |
| 1852 | BUG_ON(1); |
| 1853 | } |
| 1854 | |
| 1855 | return _double_lock_balance(this_rq, busiest); |
| 1856 | } |
| 1857 | |
| 1858 | static inline void double_unlock_balance(struct rq *this_rq, struct rq *busiest) |
| 1859 | __releases(busiest->lock) |
| 1860 | { |
| 1861 | raw_spin_unlock(&busiest->lock); |
| 1862 | lock_set_subclass(&this_rq->lock.dep_map, 0, _RET_IP_); |
| 1863 | } |
| 1864 | |
Peter Zijlstra | 7460231 | 2013-10-10 20:17:22 +0200 | [diff] [blame] | 1865 | static inline void double_lock(spinlock_t *l1, spinlock_t *l2) |
| 1866 | { |
| 1867 | if (l1 > l2) |
| 1868 | swap(l1, l2); |
| 1869 | |
| 1870 | spin_lock(l1); |
| 1871 | spin_lock_nested(l2, SINGLE_DEPTH_NESTING); |
| 1872 | } |
| 1873 | |
Mike Galbraith | 60e69ee | 2014-04-07 10:55:15 +0200 | [diff] [blame] | 1874 | static inline void double_lock_irq(spinlock_t *l1, spinlock_t *l2) |
| 1875 | { |
| 1876 | if (l1 > l2) |
| 1877 | swap(l1, l2); |
| 1878 | |
| 1879 | spin_lock_irq(l1); |
| 1880 | spin_lock_nested(l2, SINGLE_DEPTH_NESTING); |
| 1881 | } |
| 1882 | |
Peter Zijlstra | 7460231 | 2013-10-10 20:17:22 +0200 | [diff] [blame] | 1883 | static inline void double_raw_lock(raw_spinlock_t *l1, raw_spinlock_t *l2) |
| 1884 | { |
| 1885 | if (l1 > l2) |
| 1886 | swap(l1, l2); |
| 1887 | |
| 1888 | raw_spin_lock(l1); |
| 1889 | raw_spin_lock_nested(l2, SINGLE_DEPTH_NESTING); |
| 1890 | } |
| 1891 | |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 1892 | /* |
| 1893 | * double_rq_lock - safely lock two runqueues |
| 1894 | * |
| 1895 | * Note this does not disable interrupts like task_rq_lock, |
| 1896 | * you need to do so manually before calling. |
| 1897 | */ |
| 1898 | static inline void double_rq_lock(struct rq *rq1, struct rq *rq2) |
| 1899 | __acquires(rq1->lock) |
| 1900 | __acquires(rq2->lock) |
| 1901 | { |
| 1902 | BUG_ON(!irqs_disabled()); |
| 1903 | if (rq1 == rq2) { |
| 1904 | raw_spin_lock(&rq1->lock); |
| 1905 | __acquire(rq2->lock); /* Fake it out ;) */ |
| 1906 | } else { |
| 1907 | if (rq1 < rq2) { |
| 1908 | raw_spin_lock(&rq1->lock); |
| 1909 | raw_spin_lock_nested(&rq2->lock, SINGLE_DEPTH_NESTING); |
| 1910 | } else { |
| 1911 | raw_spin_lock(&rq2->lock); |
| 1912 | raw_spin_lock_nested(&rq1->lock, SINGLE_DEPTH_NESTING); |
| 1913 | } |
| 1914 | } |
| 1915 | } |
| 1916 | |
| 1917 | /* |
| 1918 | * double_rq_unlock - safely unlock two runqueues |
| 1919 | * |
| 1920 | * Note this does not restore interrupts like task_rq_unlock, |
| 1921 | * you need to do so manually after calling. |
| 1922 | */ |
| 1923 | static inline void double_rq_unlock(struct rq *rq1, struct rq *rq2) |
| 1924 | __releases(rq1->lock) |
| 1925 | __releases(rq2->lock) |
| 1926 | { |
| 1927 | raw_spin_unlock(&rq1->lock); |
| 1928 | if (rq1 != rq2) |
| 1929 | raw_spin_unlock(&rq2->lock); |
| 1930 | else |
| 1931 | __release(rq2->lock); |
| 1932 | } |
| 1933 | |
Ingo Molnar | f2cb136 | 2017-02-01 13:10:18 +0100 | [diff] [blame] | 1934 | extern void set_rq_online (struct rq *rq); |
| 1935 | extern void set_rq_offline(struct rq *rq); |
| 1936 | extern bool sched_smp_initialized; |
| 1937 | |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 1938 | #else /* CONFIG_SMP */ |
| 1939 | |
| 1940 | /* |
| 1941 | * double_rq_lock - safely lock two runqueues |
| 1942 | * |
| 1943 | * Note this does not disable interrupts like task_rq_lock, |
| 1944 | * you need to do so manually before calling. |
| 1945 | */ |
| 1946 | static inline void double_rq_lock(struct rq *rq1, struct rq *rq2) |
| 1947 | __acquires(rq1->lock) |
| 1948 | __acquires(rq2->lock) |
| 1949 | { |
| 1950 | BUG_ON(!irqs_disabled()); |
| 1951 | BUG_ON(rq1 != rq2); |
| 1952 | raw_spin_lock(&rq1->lock); |
| 1953 | __acquire(rq2->lock); /* Fake it out ;) */ |
| 1954 | } |
| 1955 | |
| 1956 | /* |
| 1957 | * double_rq_unlock - safely unlock two runqueues |
| 1958 | * |
| 1959 | * Note this does not restore interrupts like task_rq_unlock, |
| 1960 | * you need to do so manually after calling. |
| 1961 | */ |
| 1962 | static inline void double_rq_unlock(struct rq *rq1, struct rq *rq2) |
| 1963 | __releases(rq1->lock) |
| 1964 | __releases(rq2->lock) |
| 1965 | { |
| 1966 | BUG_ON(rq1 != rq2); |
| 1967 | raw_spin_unlock(&rq1->lock); |
| 1968 | __release(rq2->lock); |
| 1969 | } |
| 1970 | |
| 1971 | #endif |
| 1972 | |
| 1973 | extern struct sched_entity *__pick_first_entity(struct cfs_rq *cfs_rq); |
| 1974 | extern struct sched_entity *__pick_last_entity(struct cfs_rq *cfs_rq); |
Srikar Dronamraju | 6b55c96 | 2015-06-25 22:51:41 +0530 | [diff] [blame] | 1975 | |
| 1976 | #ifdef CONFIG_SCHED_DEBUG |
Peter Zijlstra | 9469eb0 | 2017-09-07 17:03:53 +0200 | [diff] [blame] | 1977 | extern bool sched_debug_enabled; |
| 1978 | |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 1979 | extern void print_cfs_stats(struct seq_file *m, int cpu); |
| 1980 | extern void print_rt_stats(struct seq_file *m, int cpu); |
Wanpeng Li | acb3213 | 2014-10-31 06:39:33 +0800 | [diff] [blame] | 1981 | extern void print_dl_stats(struct seq_file *m, int cpu); |
Srikar Dronamraju | 6b55c96 | 2015-06-25 22:51:41 +0530 | [diff] [blame] | 1982 | extern void |
| 1983 | print_cfs_rq(struct seq_file *m, int cpu, struct cfs_rq *cfs_rq); |
Srikar Dronamraju | 397f237 | 2015-06-25 22:51:43 +0530 | [diff] [blame] | 1984 | #ifdef CONFIG_NUMA_BALANCING |
| 1985 | extern void |
| 1986 | show_numa_stats(struct task_struct *p, struct seq_file *m); |
| 1987 | extern void |
| 1988 | print_numa_stats(struct seq_file *m, int node, unsigned long tsf, |
| 1989 | unsigned long tpf, unsigned long gsf, unsigned long gpf); |
| 1990 | #endif /* CONFIG_NUMA_BALANCING */ |
| 1991 | #endif /* CONFIG_SCHED_DEBUG */ |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 1992 | |
| 1993 | extern void init_cfs_rq(struct cfs_rq *cfs_rq); |
Abel Vesa | 07c54f7 | 2015-03-03 13:50:27 +0200 | [diff] [blame] | 1994 | extern void init_rt_rq(struct rt_rq *rt_rq); |
| 1995 | extern void init_dl_rq(struct dl_rq *dl_rq); |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 1996 | |
Ben Segall | 1ee14e6 | 2013-10-16 11:16:12 -0700 | [diff] [blame] | 1997 | extern void cfs_bandwidth_usage_inc(void); |
| 1998 | extern void cfs_bandwidth_usage_dec(void); |
Suresh Siddha | 1c792db | 2011-12-01 17:07:32 -0800 | [diff] [blame] | 1999 | |
Frederic Weisbecker | 3451d02 | 2011-08-10 23:21:01 +0200 | [diff] [blame] | 2000 | #ifdef CONFIG_NO_HZ_COMMON |
Suresh Siddha | 1c792db | 2011-12-01 17:07:32 -0800 | [diff] [blame] | 2001 | enum rq_nohz_flag_bits { |
| 2002 | NOHZ_TICK_STOPPED, |
| 2003 | NOHZ_BALANCE_KICK, |
| 2004 | }; |
| 2005 | |
| 2006 | #define nohz_flags(cpu) (&cpu_rq(cpu)->nohz_flags) |
Thomas Gleixner | 20a5c8c | 2016-03-10 12:54:20 +0100 | [diff] [blame] | 2007 | |
| 2008 | extern void nohz_balance_exit_idle(unsigned int cpu); |
| 2009 | #else |
| 2010 | static inline void nohz_balance_exit_idle(unsigned int cpu) { } |
Suresh Siddha | 1c792db | 2011-12-01 17:07:32 -0800 | [diff] [blame] | 2011 | #endif |
Frederic Weisbecker | 73fbec6 | 2012-06-16 15:57:37 +0200 | [diff] [blame] | 2012 | |
Luca Abeni | daec579 | 2017-05-18 22:13:36 +0200 | [diff] [blame] | 2013 | |
| 2014 | #ifdef CONFIG_SMP |
| 2015 | static inline |
| 2016 | void __dl_update(struct dl_bw *dl_b, s64 bw) |
| 2017 | { |
| 2018 | struct root_domain *rd = container_of(dl_b, struct root_domain, dl_bw); |
| 2019 | int i; |
| 2020 | |
| 2021 | RCU_LOCKDEP_WARN(!rcu_read_lock_sched_held(), |
| 2022 | "sched RCU must be held"); |
| 2023 | for_each_cpu_and(i, rd->span, cpu_active_mask) { |
| 2024 | struct rq *rq = cpu_rq(i); |
| 2025 | |
| 2026 | rq->dl.extra_bw += bw; |
| 2027 | } |
| 2028 | } |
| 2029 | #else |
| 2030 | static inline |
| 2031 | void __dl_update(struct dl_bw *dl_b, s64 bw) |
| 2032 | { |
| 2033 | struct dl_rq *dl = container_of(dl_b, struct dl_rq, dl_bw); |
| 2034 | |
| 2035 | dl->extra_bw += bw; |
| 2036 | } |
| 2037 | #endif |
| 2038 | |
| 2039 | |
Frederic Weisbecker | 73fbec6 | 2012-06-16 15:57:37 +0200 | [diff] [blame] | 2040 | #ifdef CONFIG_IRQ_TIME_ACCOUNTING |
Frederic Weisbecker | 19d23dbf | 2016-09-26 02:29:20 +0200 | [diff] [blame] | 2041 | struct irqtime { |
Frederic Weisbecker | 25e2d8c | 2017-04-25 16:10:48 +0200 | [diff] [blame] | 2042 | u64 total; |
Frederic Weisbecker | a499a5a | 2017-01-31 04:09:32 +0100 | [diff] [blame] | 2043 | u64 tick_delta; |
Frederic Weisbecker | 19d23dbf | 2016-09-26 02:29:20 +0200 | [diff] [blame] | 2044 | u64 irq_start_time; |
| 2045 | struct u64_stats_sync sync; |
| 2046 | }; |
Frederic Weisbecker | 73fbec6 | 2012-06-16 15:57:37 +0200 | [diff] [blame] | 2047 | |
Frederic Weisbecker | 19d23dbf | 2016-09-26 02:29:20 +0200 | [diff] [blame] | 2048 | DECLARE_PER_CPU(struct irqtime, cpu_irqtime); |
Frederic Weisbecker | 73fbec6 | 2012-06-16 15:57:37 +0200 | [diff] [blame] | 2049 | |
Frederic Weisbecker | 25e2d8c | 2017-04-25 16:10:48 +0200 | [diff] [blame] | 2050 | /* |
| 2051 | * Returns the irqtime minus the softirq time computed by ksoftirqd. |
| 2052 | * Otherwise ksoftirqd's sum_exec_runtime is substracted its own runtime |
| 2053 | * and never move forward. |
| 2054 | */ |
Frederic Weisbecker | 73fbec6 | 2012-06-16 15:57:37 +0200 | [diff] [blame] | 2055 | static inline u64 irq_time_read(int cpu) |
| 2056 | { |
Frederic Weisbecker | 19d23dbf | 2016-09-26 02:29:20 +0200 | [diff] [blame] | 2057 | struct irqtime *irqtime = &per_cpu(cpu_irqtime, cpu); |
| 2058 | unsigned int seq; |
| 2059 | u64 total; |
Frederic Weisbecker | 73fbec6 | 2012-06-16 15:57:37 +0200 | [diff] [blame] | 2060 | |
| 2061 | do { |
Frederic Weisbecker | 19d23dbf | 2016-09-26 02:29:20 +0200 | [diff] [blame] | 2062 | seq = __u64_stats_fetch_begin(&irqtime->sync); |
Frederic Weisbecker | 25e2d8c | 2017-04-25 16:10:48 +0200 | [diff] [blame] | 2063 | total = irqtime->total; |
Frederic Weisbecker | 19d23dbf | 2016-09-26 02:29:20 +0200 | [diff] [blame] | 2064 | } while (__u64_stats_fetch_retry(&irqtime->sync, seq)); |
Frederic Weisbecker | 73fbec6 | 2012-06-16 15:57:37 +0200 | [diff] [blame] | 2065 | |
Frederic Weisbecker | 19d23dbf | 2016-09-26 02:29:20 +0200 | [diff] [blame] | 2066 | return total; |
Frederic Weisbecker | 73fbec6 | 2012-06-16 15:57:37 +0200 | [diff] [blame] | 2067 | } |
Frederic Weisbecker | 73fbec6 | 2012-06-16 15:57:37 +0200 | [diff] [blame] | 2068 | #endif /* CONFIG_IRQ_TIME_ACCOUNTING */ |
Rafael J. Wysocki | adaf9fc | 2016-03-10 20:44:47 +0100 | [diff] [blame] | 2069 | |
| 2070 | #ifdef CONFIG_CPU_FREQ |
| 2071 | DECLARE_PER_CPU(struct update_util_data *, cpufreq_update_util_data); |
| 2072 | |
| 2073 | /** |
| 2074 | * cpufreq_update_util - Take a note about CPU utilization changes. |
Rafael J. Wysocki | 12bde33 | 2016-08-10 03:11:17 +0200 | [diff] [blame] | 2075 | * @rq: Runqueue to carry out the update for. |
Rafael J. Wysocki | 58919e8 | 2016-08-16 22:14:55 +0200 | [diff] [blame] | 2076 | * @flags: Update reason flags. |
Rafael J. Wysocki | adaf9fc | 2016-03-10 20:44:47 +0100 | [diff] [blame] | 2077 | * |
Rafael J. Wysocki | 58919e8 | 2016-08-16 22:14:55 +0200 | [diff] [blame] | 2078 | * This function is called by the scheduler on the CPU whose utilization is |
| 2079 | * being updated. |
Rafael J. Wysocki | adaf9fc | 2016-03-10 20:44:47 +0100 | [diff] [blame] | 2080 | * |
| 2081 | * It can only be called from RCU-sched read-side critical sections. |
Rafael J. Wysocki | adaf9fc | 2016-03-10 20:44:47 +0100 | [diff] [blame] | 2082 | * |
| 2083 | * The way cpufreq is currently arranged requires it to evaluate the CPU |
| 2084 | * performance state (frequency/voltage) on a regular basis to prevent it from |
| 2085 | * being stuck in a completely inadequate performance level for too long. |
Juri Lelli | e0367b1 | 2017-12-04 11:23:19 +0100 | [diff] [blame] | 2086 | * That is not guaranteed to happen if the updates are only triggered from CFS |
| 2087 | * and DL, though, because they may not be coming in if only RT tasks are |
| 2088 | * active all the time (or there are RT tasks only). |
Rafael J. Wysocki | adaf9fc | 2016-03-10 20:44:47 +0100 | [diff] [blame] | 2089 | * |
Juri Lelli | e0367b1 | 2017-12-04 11:23:19 +0100 | [diff] [blame] | 2090 | * As a workaround for that issue, this function is called periodically by the |
| 2091 | * RT sched class to trigger extra cpufreq updates to prevent it from stalling, |
Rafael J. Wysocki | adaf9fc | 2016-03-10 20:44:47 +0100 | [diff] [blame] | 2092 | * but that really is a band-aid. Going forward it should be replaced with |
Juri Lelli | e0367b1 | 2017-12-04 11:23:19 +0100 | [diff] [blame] | 2093 | * solutions targeted more specifically at RT tasks. |
Rafael J. Wysocki | adaf9fc | 2016-03-10 20:44:47 +0100 | [diff] [blame] | 2094 | */ |
Rafael J. Wysocki | 12bde33 | 2016-08-10 03:11:17 +0200 | [diff] [blame] | 2095 | static inline void cpufreq_update_util(struct rq *rq, unsigned int flags) |
Rafael J. Wysocki | adaf9fc | 2016-03-10 20:44:47 +0100 | [diff] [blame] | 2096 | { |
Rafael J. Wysocki | 58919e8 | 2016-08-16 22:14:55 +0200 | [diff] [blame] | 2097 | struct update_util_data *data; |
| 2098 | |
Viresh Kumar | 674e754 | 2017-07-28 12:16:38 +0530 | [diff] [blame] | 2099 | data = rcu_dereference_sched(*per_cpu_ptr(&cpufreq_update_util_data, |
| 2100 | cpu_of(rq))); |
Rafael J. Wysocki | 58919e8 | 2016-08-16 22:14:55 +0200 | [diff] [blame] | 2101 | if (data) |
Rafael J. Wysocki | 12bde33 | 2016-08-10 03:11:17 +0200 | [diff] [blame] | 2102 | data->func(data, rq_clock(rq), flags); |
| 2103 | } |
Rafael J. Wysocki | adaf9fc | 2016-03-10 20:44:47 +0100 | [diff] [blame] | 2104 | #else |
Rafael J. Wysocki | 12bde33 | 2016-08-10 03:11:17 +0200 | [diff] [blame] | 2105 | static inline void cpufreq_update_util(struct rq *rq, unsigned int flags) {} |
Rafael J. Wysocki | adaf9fc | 2016-03-10 20:44:47 +0100 | [diff] [blame] | 2106 | #endif /* CONFIG_CPU_FREQ */ |
Linus Torvalds | be53f58 | 2016-03-24 09:42:50 -0700 | [diff] [blame] | 2107 | |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 2108 | #ifdef arch_scale_freq_capacity |
| 2109 | #ifndef arch_scale_freq_invariant |
| 2110 | #define arch_scale_freq_invariant() (true) |
| 2111 | #endif |
| 2112 | #else /* arch_scale_freq_capacity */ |
| 2113 | #define arch_scale_freq_invariant() (false) |
| 2114 | #endif |
Juri Lelli | d4edd66 | 2017-12-04 11:23:18 +0100 | [diff] [blame] | 2115 | |
Juri Lelli | 794a56e | 2017-12-04 11:23:20 +0100 | [diff] [blame] | 2116 | #ifdef CONFIG_CPU_FREQ_GOV_SCHEDUTIL |
| 2117 | |
Juri Lelli | d4edd66 | 2017-12-04 11:23:18 +0100 | [diff] [blame] | 2118 | static inline unsigned long cpu_util_dl(struct rq *rq) |
| 2119 | { |
| 2120 | return (rq->dl.running_bw * SCHED_CAPACITY_SCALE) >> BW_SHIFT; |
| 2121 | } |
| 2122 | |
| 2123 | static inline unsigned long cpu_util_cfs(struct rq *rq) |
| 2124 | { |
| 2125 | return rq->cfs.avg.util_avg; |
| 2126 | } |
Juri Lelli | 794a56e | 2017-12-04 11:23:20 +0100 | [diff] [blame] | 2127 | |
| 2128 | #endif |