Daniel Lezcano | 108c35a | 2018-12-03 11:29:29 +0100 | [diff] [blame^] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 2 | /* |
| 3 | * CPUFreq governor based on scheduler-provided CPU utilization data. |
| 4 | * |
| 5 | * Copyright (C) 2016, Intel Corporation |
| 6 | * Author: Rafael J. Wysocki <rafael.j.wysocki@intel.com> |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 7 | */ |
| 8 | |
Viresh Kumar | 60f05e8 | 2016-05-18 17:55:28 +0530 | [diff] [blame] | 9 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 10 | |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 11 | #include "sched.h" |
| 12 | |
Ingo Molnar | 325ea10 | 2018-03-03 12:20:47 +0100 | [diff] [blame] | 13 | #include <trace/events/power.h> |
| 14 | |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 15 | struct sugov_tunables { |
Ingo Molnar | 97fb7a0 | 2018-03-03 14:01:12 +0100 | [diff] [blame] | 16 | struct gov_attr_set attr_set; |
| 17 | unsigned int rate_limit_us; |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 18 | }; |
| 19 | |
| 20 | struct sugov_policy { |
Ingo Molnar | 97fb7a0 | 2018-03-03 14:01:12 +0100 | [diff] [blame] | 21 | struct cpufreq_policy *policy; |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 22 | |
Ingo Molnar | 97fb7a0 | 2018-03-03 14:01:12 +0100 | [diff] [blame] | 23 | struct sugov_tunables *tunables; |
| 24 | struct list_head tunables_hook; |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 25 | |
Ingo Molnar | 97fb7a0 | 2018-03-03 14:01:12 +0100 | [diff] [blame] | 26 | raw_spinlock_t update_lock; /* For shared policies */ |
| 27 | u64 last_freq_update_time; |
| 28 | s64 freq_update_delay_ns; |
| 29 | unsigned int next_freq; |
| 30 | unsigned int cached_raw_freq; |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 31 | |
Ingo Molnar | 97fb7a0 | 2018-03-03 14:01:12 +0100 | [diff] [blame] | 32 | /* The next fields are only needed if fast switch cannot be used: */ |
| 33 | struct irq_work irq_work; |
| 34 | struct kthread_work work; |
| 35 | struct mutex work_lock; |
| 36 | struct kthread_worker worker; |
| 37 | struct task_struct *thread; |
| 38 | bool work_in_progress; |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 39 | |
Ingo Molnar | 97fb7a0 | 2018-03-03 14:01:12 +0100 | [diff] [blame] | 40 | bool need_freq_update; |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 41 | }; |
| 42 | |
| 43 | struct sugov_cpu { |
Ingo Molnar | 97fb7a0 | 2018-03-03 14:01:12 +0100 | [diff] [blame] | 44 | struct update_util_data update_util; |
| 45 | struct sugov_policy *sg_policy; |
| 46 | unsigned int cpu; |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 47 | |
Ingo Molnar | 97fb7a0 | 2018-03-03 14:01:12 +0100 | [diff] [blame] | 48 | bool iowait_boost_pending; |
| 49 | unsigned int iowait_boost; |
| 50 | unsigned int iowait_boost_max; |
Patrick Bellasi | fd7d528 | 2018-05-22 12:07:54 +0100 | [diff] [blame] | 51 | u64 last_update; |
Steve Muckle | 5cbea46 | 2016-07-13 13:25:26 -0700 | [diff] [blame] | 52 | |
Vincent Guittot | 8cc9051 | 2018-06-28 17:45:08 +0200 | [diff] [blame] | 53 | unsigned long bw_dl; |
Ingo Molnar | 97fb7a0 | 2018-03-03 14:01:12 +0100 | [diff] [blame] | 54 | unsigned long max; |
Rafael J. Wysocki | b7eaf1a | 2017-03-22 00:08:50 +0100 | [diff] [blame] | 55 | |
Ingo Molnar | 97fb7a0 | 2018-03-03 14:01:12 +0100 | [diff] [blame] | 56 | /* The field below is for single-CPU policies only: */ |
Rafael J. Wysocki | b7eaf1a | 2017-03-22 00:08:50 +0100 | [diff] [blame] | 57 | #ifdef CONFIG_NO_HZ_COMMON |
Ingo Molnar | 97fb7a0 | 2018-03-03 14:01:12 +0100 | [diff] [blame] | 58 | unsigned long saved_idle_calls; |
Rafael J. Wysocki | b7eaf1a | 2017-03-22 00:08:50 +0100 | [diff] [blame] | 59 | #endif |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 60 | }; |
| 61 | |
| 62 | static DEFINE_PER_CPU(struct sugov_cpu, sugov_cpu); |
| 63 | |
| 64 | /************************ Governor internals ***********************/ |
| 65 | |
| 66 | static bool sugov_should_update_freq(struct sugov_policy *sg_policy, u64 time) |
| 67 | { |
| 68 | s64 delta_ns; |
| 69 | |
Viresh Kumar | 674e754 | 2017-07-28 12:16:38 +0530 | [diff] [blame] | 70 | /* |
| 71 | * Since cpufreq_update_util() is called with rq->lock held for |
Ingo Molnar | 97fb7a0 | 2018-03-03 14:01:12 +0100 | [diff] [blame] | 72 | * the @target_cpu, our per-CPU data is fully serialized. |
Viresh Kumar | 674e754 | 2017-07-28 12:16:38 +0530 | [diff] [blame] | 73 | * |
Ingo Molnar | 97fb7a0 | 2018-03-03 14:01:12 +0100 | [diff] [blame] | 74 | * However, drivers cannot in general deal with cross-CPU |
Viresh Kumar | 674e754 | 2017-07-28 12:16:38 +0530 | [diff] [blame] | 75 | * requests, so while get_next_freq() will work, our |
Viresh Kumar | c49cbc1 | 2017-08-14 14:50:16 +0530 | [diff] [blame] | 76 | * sugov_update_commit() call may not for the fast switching platforms. |
Viresh Kumar | 674e754 | 2017-07-28 12:16:38 +0530 | [diff] [blame] | 77 | * |
| 78 | * Hence stop here for remote requests if they aren't supported |
| 79 | * by the hardware, as calculating the frequency is pointless if |
| 80 | * we cannot in fact act on it. |
Viresh Kumar | c49cbc1 | 2017-08-14 14:50:16 +0530 | [diff] [blame] | 81 | * |
| 82 | * For the slow switching platforms, the kthread is always scheduled on |
| 83 | * the right set of CPUs and any CPU can find the next frequency and |
| 84 | * schedule the kthread. |
Viresh Kumar | 674e754 | 2017-07-28 12:16:38 +0530 | [diff] [blame] | 85 | */ |
Viresh Kumar | c49cbc1 | 2017-08-14 14:50:16 +0530 | [diff] [blame] | 86 | if (sg_policy->policy->fast_switch_enabled && |
Viresh Kumar | 0363997 | 2018-05-22 15:31:30 +0530 | [diff] [blame] | 87 | !cpufreq_this_cpu_can_update(sg_policy->policy)) |
Viresh Kumar | 674e754 | 2017-07-28 12:16:38 +0530 | [diff] [blame] | 88 | return false; |
| 89 | |
Viresh Kumar | ecd2884 | 2018-05-09 16:05:24 +0530 | [diff] [blame] | 90 | if (unlikely(sg_policy->need_freq_update)) |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 91 | return true; |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 92 | |
| 93 | delta_ns = time - sg_policy->last_freq_update_time; |
Ingo Molnar | 97fb7a0 | 2018-03-03 14:01:12 +0100 | [diff] [blame] | 94 | |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 95 | return delta_ns >= sg_policy->freq_update_delay_ns; |
| 96 | } |
| 97 | |
Rafael J. Wysocki | a61dec7 | 2018-05-23 11:47:45 +0200 | [diff] [blame] | 98 | static bool sugov_update_next_freq(struct sugov_policy *sg_policy, u64 time, |
| 99 | unsigned int next_freq) |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 100 | { |
Rafael J. Wysocki | 38d4ea2 | 2017-03-22 18:32:47 +0100 | [diff] [blame] | 101 | if (sg_policy->next_freq == next_freq) |
Rafael J. Wysocki | a61dec7 | 2018-05-23 11:47:45 +0200 | [diff] [blame] | 102 | return false; |
Rafael J. Wysocki | 38d4ea2 | 2017-03-22 18:32:47 +0100 | [diff] [blame] | 103 | |
| 104 | sg_policy->next_freq = next_freq; |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 105 | sg_policy->last_freq_update_time = time; |
| 106 | |
Rafael J. Wysocki | a61dec7 | 2018-05-23 11:47:45 +0200 | [diff] [blame] | 107 | return true; |
| 108 | } |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 109 | |
Rafael J. Wysocki | a61dec7 | 2018-05-23 11:47:45 +0200 | [diff] [blame] | 110 | static void sugov_fast_switch(struct sugov_policy *sg_policy, u64 time, |
| 111 | unsigned int next_freq) |
| 112 | { |
| 113 | struct cpufreq_policy *policy = sg_policy->policy; |
| 114 | |
| 115 | if (!sugov_update_next_freq(sg_policy, time, next_freq)) |
| 116 | return; |
| 117 | |
| 118 | next_freq = cpufreq_driver_fast_switch(policy, next_freq); |
| 119 | if (!next_freq) |
| 120 | return; |
| 121 | |
| 122 | policy->cur = next_freq; |
| 123 | trace_cpu_frequency(next_freq, smp_processor_id()); |
| 124 | } |
| 125 | |
| 126 | static void sugov_deferred_update(struct sugov_policy *sg_policy, u64 time, |
| 127 | unsigned int next_freq) |
| 128 | { |
| 129 | if (!sugov_update_next_freq(sg_policy, time, next_freq)) |
| 130 | return; |
| 131 | |
| 132 | if (!sg_policy->work_in_progress) { |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 133 | sg_policy->work_in_progress = true; |
| 134 | irq_work_queue(&sg_policy->irq_work); |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * get_next_freq - Compute a new frequency for a given cpufreq policy. |
Viresh Kumar | 655cb1e | 2017-03-02 14:03:21 +0530 | [diff] [blame] | 140 | * @sg_policy: schedutil policy object to compute the new frequency for. |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 141 | * @util: Current CPU utilization. |
| 142 | * @max: CPU capacity. |
| 143 | * |
| 144 | * If the utilization is frequency-invariant, choose the new frequency to be |
| 145 | * proportional to it, that is |
| 146 | * |
| 147 | * next_freq = C * max_freq * util / max |
| 148 | * |
| 149 | * Otherwise, approximate the would-be frequency-invariant utilization by |
| 150 | * util_raw * (curr_freq / max_freq) which leads to |
| 151 | * |
| 152 | * next_freq = C * curr_freq * util_raw / max |
| 153 | * |
| 154 | * Take C = 1.25 for the frequency tipping point at (util / max) = 0.8. |
Steve Muckle | 5cbea46 | 2016-07-13 13:25:26 -0700 | [diff] [blame] | 155 | * |
| 156 | * The lowest driver-supported frequency which is equal or greater than the raw |
| 157 | * next_freq (as calculated above) is returned, subject to policy min/max and |
| 158 | * cpufreq driver limitations. |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 159 | */ |
Viresh Kumar | 655cb1e | 2017-03-02 14:03:21 +0530 | [diff] [blame] | 160 | static unsigned int get_next_freq(struct sugov_policy *sg_policy, |
| 161 | unsigned long util, unsigned long max) |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 162 | { |
Steve Muckle | 5cbea46 | 2016-07-13 13:25:26 -0700 | [diff] [blame] | 163 | struct cpufreq_policy *policy = sg_policy->policy; |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 164 | unsigned int freq = arch_scale_freq_invariant() ? |
| 165 | policy->cpuinfo.max_freq : policy->cur; |
| 166 | |
Steve Muckle | 5cbea46 | 2016-07-13 13:25:26 -0700 | [diff] [blame] | 167 | freq = (freq + (freq >> 2)) * util / max; |
| 168 | |
Viresh Kumar | ecd2884 | 2018-05-09 16:05:24 +0530 | [diff] [blame] | 169 | if (freq == sg_policy->cached_raw_freq && !sg_policy->need_freq_update) |
Steve Muckle | 5cbea46 | 2016-07-13 13:25:26 -0700 | [diff] [blame] | 170 | return sg_policy->next_freq; |
Viresh Kumar | ecd2884 | 2018-05-09 16:05:24 +0530 | [diff] [blame] | 171 | |
| 172 | sg_policy->need_freq_update = false; |
Viresh Kumar | 6c4f0fa | 2017-03-02 14:03:20 +0530 | [diff] [blame] | 173 | sg_policy->cached_raw_freq = freq; |
Steve Muckle | 5cbea46 | 2016-07-13 13:25:26 -0700 | [diff] [blame] | 174 | return cpufreq_driver_resolve_freq(policy, freq); |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 175 | } |
| 176 | |
Peter Zijlstra | 45f5519 | 2018-07-05 14:36:17 +0200 | [diff] [blame] | 177 | /* |
| 178 | * This function computes an effective utilization for the given CPU, to be |
| 179 | * used for frequency selection given the linear relation: f = u * f_max. |
| 180 | * |
| 181 | * The scheduler tracks the following metrics: |
| 182 | * |
| 183 | * cpu_util_{cfs,rt,dl,irq}() |
| 184 | * cpu_bw_dl() |
| 185 | * |
| 186 | * Where the cfs,rt and dl util numbers are tracked with the same metric and |
| 187 | * synchronized windows and are thus directly comparable. |
| 188 | * |
| 189 | * The cfs,rt,dl utilization are the running times measured with rq->clock_task |
| 190 | * which excludes things like IRQ and steal-time. These latter are then accrued |
| 191 | * in the irq utilization. |
| 192 | * |
| 193 | * The DL bandwidth number otoh is not a measured metric but a value computed |
| 194 | * based on the task model parameters and gives the minimal utilization |
| 195 | * required to meet deadlines. |
| 196 | */ |
Vincent Guittot | dfa444d | 2018-06-28 17:45:11 +0200 | [diff] [blame] | 197 | static unsigned long sugov_get_util(struct sugov_cpu *sg_cpu) |
Rafael J. Wysocki | 58919e8 | 2016-08-16 22:14:55 +0200 | [diff] [blame] | 198 | { |
Juri Lelli | d18be45 | 2017-12-04 11:23:21 +0100 | [diff] [blame] | 199 | struct rq *rq = cpu_rq(sg_cpu->cpu); |
Vincent Guittot | dfa444d | 2018-06-28 17:45:11 +0200 | [diff] [blame] | 200 | unsigned long util, irq, max; |
Steve Muckle | 8314bc8 | 2016-08-26 11:40:47 -0700 | [diff] [blame] | 201 | |
Vincent Guittot | dfa444d | 2018-06-28 17:45:11 +0200 | [diff] [blame] | 202 | sg_cpu->max = max = arch_scale_cpu_capacity(NULL, sg_cpu->cpu); |
| 203 | sg_cpu->bw_dl = cpu_bw_dl(rq); |
Peter Zijlstra | 8f111bc | 2017-12-20 16:26:12 +0100 | [diff] [blame] | 204 | |
Vincent Guittot | 296b2ff | 2018-06-26 15:53:22 +0200 | [diff] [blame] | 205 | if (rt_rq_is_runnable(&rq->rt)) |
Vincent Guittot | dfa444d | 2018-06-28 17:45:11 +0200 | [diff] [blame] | 206 | return max; |
Peter Zijlstra | 8f111bc | 2017-12-20 16:26:12 +0100 | [diff] [blame] | 207 | |
Peter Zijlstra | 45f5519 | 2018-07-05 14:36:17 +0200 | [diff] [blame] | 208 | /* |
| 209 | * Early check to see if IRQ/steal time saturates the CPU, can be |
| 210 | * because of inaccuracies in how we track these -- see |
| 211 | * update_irq_load_avg(). |
| 212 | */ |
Vincent Guittot | dfa444d | 2018-06-28 17:45:11 +0200 | [diff] [blame] | 213 | irq = cpu_util_irq(rq); |
Vincent Guittot | dfa444d | 2018-06-28 17:45:11 +0200 | [diff] [blame] | 214 | if (unlikely(irq >= max)) |
Vincent Guittot | 9033ea1 | 2018-06-28 17:45:10 +0200 | [diff] [blame] | 215 | return max; |
| 216 | |
Peter Zijlstra | 45f5519 | 2018-07-05 14:36:17 +0200 | [diff] [blame] | 217 | /* |
| 218 | * Because the time spend on RT/DL tasks is visible as 'lost' time to |
| 219 | * CFS tasks and we use the same metric to track the effective |
| 220 | * utilization (PELT windows are synchronized) we can directly add them |
| 221 | * to obtain the CPU's actual utilization. |
| 222 | */ |
Vincent Guittot | dfa444d | 2018-06-28 17:45:11 +0200 | [diff] [blame] | 223 | util = cpu_util_cfs(rq); |
| 224 | util += cpu_util_rt(rq); |
Vincent Guittot | 3ae117c | 2018-06-28 17:45:06 +0200 | [diff] [blame] | 225 | |
Vincent Guittot | 9033ea1 | 2018-06-28 17:45:10 +0200 | [diff] [blame] | 226 | /* |
Peter Zijlstra | 45f5519 | 2018-07-05 14:36:17 +0200 | [diff] [blame] | 227 | * We do not make cpu_util_dl() a permanent part of this sum because we |
| 228 | * want to use cpu_bw_dl() later on, but we need to check if the |
| 229 | * CFS+RT+DL sum is saturated (ie. no idle time) such that we select |
| 230 | * f_max when there is no idle time. |
| 231 | * |
| 232 | * NOTE: numerical errors or stop class might cause us to not quite hit |
| 233 | * saturation when we should -- something for later. |
Vincent Guittot | 9033ea1 | 2018-06-28 17:45:10 +0200 | [diff] [blame] | 234 | */ |
Vincent Guittot | dfa444d | 2018-06-28 17:45:11 +0200 | [diff] [blame] | 235 | if ((util + cpu_util_dl(rq)) >= max) |
Vincent Guittot | 9033ea1 | 2018-06-28 17:45:10 +0200 | [diff] [blame] | 236 | return max; |
Vincent Guittot | 8cc9051 | 2018-06-28 17:45:08 +0200 | [diff] [blame] | 237 | |
Juri Lelli | d4edd66 | 2017-12-04 11:23:18 +0100 | [diff] [blame] | 238 | /* |
Peter Zijlstra | 45f5519 | 2018-07-05 14:36:17 +0200 | [diff] [blame] | 239 | * There is still idle time; further improve the number by using the |
| 240 | * irq metric. Because IRQ/steal time is hidden from the task clock we |
| 241 | * need to scale the task numbers: |
Vincent Guittot | 8cc9051 | 2018-06-28 17:45:08 +0200 | [diff] [blame] | 242 | * |
Peter Zijlstra | 45f5519 | 2018-07-05 14:36:17 +0200 | [diff] [blame] | 243 | * 1 - irq |
| 244 | * U' = irq + ------- * U |
| 245 | * max |
| 246 | */ |
Vincent Guittot | 2e62c47 | 2018-07-19 14:00:06 +0200 | [diff] [blame] | 247 | util = scale_irq_capacity(util, irq, max); |
Peter Zijlstra | 45f5519 | 2018-07-05 14:36:17 +0200 | [diff] [blame] | 248 | util += irq; |
| 249 | |
| 250 | /* |
Vincent Guittot | 8cc9051 | 2018-06-28 17:45:08 +0200 | [diff] [blame] | 251 | * Bandwidth required by DEADLINE must always be granted while, for |
| 252 | * FAIR and RT, we use blocked utilization of IDLE CPUs as a mechanism |
| 253 | * to gracefully reduce the frequency when no tasks show up for longer |
Patrick Bellasi | 8ecf04e | 2018-05-24 15:10:22 +0100 | [diff] [blame] | 254 | * periods of time. |
| 255 | * |
Peter Zijlstra | 45f5519 | 2018-07-05 14:36:17 +0200 | [diff] [blame] | 256 | * Ideally we would like to set bw_dl as min/guaranteed freq and util + |
| 257 | * bw_dl as requested freq. However, cpufreq is not yet ready for such |
| 258 | * an interface. So, we only do the latter for now. |
Juri Lelli | d4edd66 | 2017-12-04 11:23:18 +0100 | [diff] [blame] | 259 | */ |
Peter Zijlstra | 45f5519 | 2018-07-05 14:36:17 +0200 | [diff] [blame] | 260 | return min(max, util + sg_cpu->bw_dl); |
Rafael J. Wysocki | 58919e8 | 2016-08-16 22:14:55 +0200 | [diff] [blame] | 261 | } |
| 262 | |
Patrick Bellasi | fd7d528 | 2018-05-22 12:07:54 +0100 | [diff] [blame] | 263 | /** |
| 264 | * sugov_iowait_reset() - Reset the IO boost status of a CPU. |
| 265 | * @sg_cpu: the sugov data for the CPU to boost |
| 266 | * @time: the update time from the caller |
| 267 | * @set_iowait_boost: true if an IO boost has been requested |
| 268 | * |
| 269 | * The IO wait boost of a task is disabled after a tick since the last update |
| 270 | * of a CPU. If a new IO wait boost is requested after more then a tick, then |
| 271 | * we enable the boost starting from the minimum frequency, which improves |
| 272 | * energy efficiency by ignoring sporadic wakeups from IO. |
| 273 | */ |
| 274 | static bool sugov_iowait_reset(struct sugov_cpu *sg_cpu, u64 time, |
| 275 | bool set_iowait_boost) |
Rafael J. Wysocki | 21ca6d2 | 2016-09-10 00:00:31 +0200 | [diff] [blame] | 276 | { |
Patrick Bellasi | fd7d528 | 2018-05-22 12:07:54 +0100 | [diff] [blame] | 277 | s64 delta_ns = time - sg_cpu->last_update; |
Joel Fernandes | a5a0809 | 2017-07-23 08:54:25 -0700 | [diff] [blame] | 278 | |
Patrick Bellasi | fd7d528 | 2018-05-22 12:07:54 +0100 | [diff] [blame] | 279 | /* Reset boost only if a tick has elapsed since last request */ |
| 280 | if (delta_ns <= TICK_NSEC) |
| 281 | return false; |
Joel Fernandes | a5a0809 | 2017-07-23 08:54:25 -0700 | [diff] [blame] | 282 | |
Patrick Bellasi | fd7d528 | 2018-05-22 12:07:54 +0100 | [diff] [blame] | 283 | sg_cpu->iowait_boost = set_iowait_boost |
| 284 | ? sg_cpu->sg_policy->policy->min : 0; |
| 285 | sg_cpu->iowait_boost_pending = set_iowait_boost; |
Rafael J. Wysocki | 21ca6d2 | 2016-09-10 00:00:31 +0200 | [diff] [blame] | 286 | |
Patrick Bellasi | fd7d528 | 2018-05-22 12:07:54 +0100 | [diff] [blame] | 287 | return true; |
Rafael J. Wysocki | 21ca6d2 | 2016-09-10 00:00:31 +0200 | [diff] [blame] | 288 | } |
| 289 | |
Patrick Bellasi | fd7d528 | 2018-05-22 12:07:54 +0100 | [diff] [blame] | 290 | /** |
| 291 | * sugov_iowait_boost() - Updates the IO boost status of a CPU. |
| 292 | * @sg_cpu: the sugov data for the CPU to boost |
| 293 | * @time: the update time from the caller |
| 294 | * @flags: SCHED_CPUFREQ_IOWAIT if the task is waking up after an IO wait |
| 295 | * |
| 296 | * Each time a task wakes up after an IO operation, the CPU utilization can be |
| 297 | * boosted to a certain utilization which doubles at each "frequent and |
| 298 | * successive" wakeup from IO, ranging from the utilization of the minimum |
| 299 | * OPP to the utilization of the maximum OPP. |
| 300 | * To keep doubling, an IO boost has to be requested at least once per tick, |
| 301 | * otherwise we restart from the utilization of the minimum OPP. |
| 302 | */ |
| 303 | static void sugov_iowait_boost(struct sugov_cpu *sg_cpu, u64 time, |
| 304 | unsigned int flags) |
| 305 | { |
| 306 | bool set_iowait_boost = flags & SCHED_CPUFREQ_IOWAIT; |
| 307 | |
| 308 | /* Reset boost if the CPU appears to have been idle enough */ |
| 309 | if (sg_cpu->iowait_boost && |
| 310 | sugov_iowait_reset(sg_cpu, time, set_iowait_boost)) |
| 311 | return; |
| 312 | |
| 313 | /* Boost only tasks waking up after IO */ |
| 314 | if (!set_iowait_boost) |
| 315 | return; |
| 316 | |
| 317 | /* Ensure boost doubles only one time at each request */ |
| 318 | if (sg_cpu->iowait_boost_pending) |
| 319 | return; |
| 320 | sg_cpu->iowait_boost_pending = true; |
| 321 | |
| 322 | /* Double the boost at each request */ |
| 323 | if (sg_cpu->iowait_boost) { |
| 324 | sg_cpu->iowait_boost <<= 1; |
| 325 | if (sg_cpu->iowait_boost > sg_cpu->iowait_boost_max) |
| 326 | sg_cpu->iowait_boost = sg_cpu->iowait_boost_max; |
| 327 | return; |
| 328 | } |
| 329 | |
| 330 | /* First wakeup after IO: start with minimum boost */ |
| 331 | sg_cpu->iowait_boost = sg_cpu->sg_policy->policy->min; |
| 332 | } |
| 333 | |
| 334 | /** |
| 335 | * sugov_iowait_apply() - Apply the IO boost to a CPU. |
| 336 | * @sg_cpu: the sugov data for the cpu to boost |
| 337 | * @time: the update time from the caller |
| 338 | * @util: the utilization to (eventually) boost |
| 339 | * @max: the maximum value the utilization can be boosted to |
| 340 | * |
| 341 | * A CPU running a task which woken up after an IO operation can have its |
| 342 | * utilization boosted to speed up the completion of those IO operations. |
| 343 | * The IO boost value is increased each time a task wakes up from IO, in |
| 344 | * sugov_iowait_apply(), and it's instead decreased by this function, |
| 345 | * each time an increase has not been requested (!iowait_boost_pending). |
| 346 | * |
| 347 | * A CPU which also appears to have been idle for at least one tick has also |
| 348 | * its IO boost utilization reset. |
| 349 | * |
| 350 | * This mechanism is designed to boost high frequently IO waiting tasks, while |
| 351 | * being more conservative on tasks which does sporadic IO operations. |
| 352 | */ |
| 353 | static void sugov_iowait_apply(struct sugov_cpu *sg_cpu, u64 time, |
| 354 | unsigned long *util, unsigned long *max) |
Rafael J. Wysocki | 21ca6d2 | 2016-09-10 00:00:31 +0200 | [diff] [blame] | 355 | { |
Joel Fernandes | 251accf | 2017-07-23 08:54:26 -0700 | [diff] [blame] | 356 | unsigned int boost_util, boost_max; |
Rafael J. Wysocki | 21ca6d2 | 2016-09-10 00:00:31 +0200 | [diff] [blame] | 357 | |
Patrick Bellasi | fd7d528 | 2018-05-22 12:07:54 +0100 | [diff] [blame] | 358 | /* No boost currently required */ |
Joel Fernandes | a5a0809 | 2017-07-23 08:54:25 -0700 | [diff] [blame] | 359 | if (!sg_cpu->iowait_boost) |
Rafael J. Wysocki | 21ca6d2 | 2016-09-10 00:00:31 +0200 | [diff] [blame] | 360 | return; |
| 361 | |
Patrick Bellasi | fd7d528 | 2018-05-22 12:07:54 +0100 | [diff] [blame] | 362 | /* Reset boost if the CPU appears to have been idle enough */ |
| 363 | if (sugov_iowait_reset(sg_cpu, time, false)) |
| 364 | return; |
| 365 | |
| 366 | /* |
| 367 | * An IO waiting task has just woken up: |
| 368 | * allow to further double the boost value |
| 369 | */ |
Joel Fernandes | a5a0809 | 2017-07-23 08:54:25 -0700 | [diff] [blame] | 370 | if (sg_cpu->iowait_boost_pending) { |
| 371 | sg_cpu->iowait_boost_pending = false; |
| 372 | } else { |
Patrick Bellasi | fd7d528 | 2018-05-22 12:07:54 +0100 | [diff] [blame] | 373 | /* |
| 374 | * Otherwise: reduce the boost value and disable it when we |
| 375 | * reach the minimum. |
| 376 | */ |
Joel Fernandes | a5a0809 | 2017-07-23 08:54:25 -0700 | [diff] [blame] | 377 | sg_cpu->iowait_boost >>= 1; |
| 378 | if (sg_cpu->iowait_boost < sg_cpu->sg_policy->policy->min) { |
| 379 | sg_cpu->iowait_boost = 0; |
| 380 | return; |
| 381 | } |
| 382 | } |
| 383 | |
Patrick Bellasi | fd7d528 | 2018-05-22 12:07:54 +0100 | [diff] [blame] | 384 | /* |
| 385 | * Apply the current boost value: a CPU is boosted only if its current |
| 386 | * utilization is smaller then the current IO boost level. |
| 387 | */ |
Joel Fernandes | a5a0809 | 2017-07-23 08:54:25 -0700 | [diff] [blame] | 388 | boost_util = sg_cpu->iowait_boost; |
| 389 | boost_max = sg_cpu->iowait_boost_max; |
Rafael J. Wysocki | 21ca6d2 | 2016-09-10 00:00:31 +0200 | [diff] [blame] | 390 | if (*util * boost_max < *max * boost_util) { |
| 391 | *util = boost_util; |
| 392 | *max = boost_max; |
| 393 | } |
Rafael J. Wysocki | 21ca6d2 | 2016-09-10 00:00:31 +0200 | [diff] [blame] | 394 | } |
| 395 | |
Rafael J. Wysocki | b7eaf1a | 2017-03-22 00:08:50 +0100 | [diff] [blame] | 396 | #ifdef CONFIG_NO_HZ_COMMON |
| 397 | static bool sugov_cpu_is_busy(struct sugov_cpu *sg_cpu) |
| 398 | { |
Joel Fernandes | 466a2b4 | 2017-12-21 02:22:45 +0100 | [diff] [blame] | 399 | unsigned long idle_calls = tick_nohz_get_idle_calls_cpu(sg_cpu->cpu); |
Rafael J. Wysocki | b7eaf1a | 2017-03-22 00:08:50 +0100 | [diff] [blame] | 400 | bool ret = idle_calls == sg_cpu->saved_idle_calls; |
| 401 | |
| 402 | sg_cpu->saved_idle_calls = idle_calls; |
| 403 | return ret; |
| 404 | } |
| 405 | #else |
| 406 | static inline bool sugov_cpu_is_busy(struct sugov_cpu *sg_cpu) { return false; } |
| 407 | #endif /* CONFIG_NO_HZ_COMMON */ |
| 408 | |
Claudio Scordino | e97a90f | 2018-03-13 11:35:40 +0100 | [diff] [blame] | 409 | /* |
| 410 | * Make sugov_should_update_freq() ignore the rate limit when DL |
| 411 | * has increased the utilization. |
| 412 | */ |
| 413 | static inline void ignore_dl_rate_limit(struct sugov_cpu *sg_cpu, struct sugov_policy *sg_policy) |
| 414 | { |
Vincent Guittot | 8cc9051 | 2018-06-28 17:45:08 +0200 | [diff] [blame] | 415 | if (cpu_bw_dl(cpu_rq(sg_cpu->cpu)) > sg_cpu->bw_dl) |
Claudio Scordino | e97a90f | 2018-03-13 11:35:40 +0100 | [diff] [blame] | 416 | sg_policy->need_freq_update = true; |
| 417 | } |
| 418 | |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 419 | static void sugov_update_single(struct update_util_data *hook, u64 time, |
Rafael J. Wysocki | 58919e8 | 2016-08-16 22:14:55 +0200 | [diff] [blame] | 420 | unsigned int flags) |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 421 | { |
| 422 | struct sugov_cpu *sg_cpu = container_of(hook, struct sugov_cpu, update_util); |
| 423 | struct sugov_policy *sg_policy = sg_cpu->sg_policy; |
Rafael J. Wysocki | 58919e8 | 2016-08-16 22:14:55 +0200 | [diff] [blame] | 424 | unsigned long util, max; |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 425 | unsigned int next_f; |
Rafael J. Wysocki | b7eaf1a | 2017-03-22 00:08:50 +0100 | [diff] [blame] | 426 | bool busy; |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 427 | |
Patrick Bellasi | fd7d528 | 2018-05-22 12:07:54 +0100 | [diff] [blame] | 428 | sugov_iowait_boost(sg_cpu, time, flags); |
Rafael J. Wysocki | 21ca6d2 | 2016-09-10 00:00:31 +0200 | [diff] [blame] | 429 | sg_cpu->last_update = time; |
| 430 | |
Claudio Scordino | e97a90f | 2018-03-13 11:35:40 +0100 | [diff] [blame] | 431 | ignore_dl_rate_limit(sg_cpu, sg_policy); |
| 432 | |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 433 | if (!sugov_should_update_freq(sg_policy, time)) |
| 434 | return; |
| 435 | |
Rafael J. Wysocki | b7eaf1a | 2017-03-22 00:08:50 +0100 | [diff] [blame] | 436 | busy = sugov_cpu_is_busy(sg_cpu); |
| 437 | |
Vincent Guittot | dfa444d | 2018-06-28 17:45:11 +0200 | [diff] [blame] | 438 | util = sugov_get_util(sg_cpu); |
Peter Zijlstra | 8f111bc | 2017-12-20 16:26:12 +0100 | [diff] [blame] | 439 | max = sg_cpu->max; |
Patrick Bellasi | fd7d528 | 2018-05-22 12:07:54 +0100 | [diff] [blame] | 440 | sugov_iowait_apply(sg_cpu, time, &util, &max); |
Peter Zijlstra | 8f111bc | 2017-12-20 16:26:12 +0100 | [diff] [blame] | 441 | next_f = get_next_freq(sg_policy, util, max); |
| 442 | /* |
| 443 | * Do not reduce the frequency if the CPU has not been idle |
| 444 | * recently, as the reduction is likely to be premature then. |
| 445 | */ |
Viresh Kumar | ecd2884 | 2018-05-09 16:05:24 +0530 | [diff] [blame] | 446 | if (busy && next_f < sg_policy->next_freq) { |
Peter Zijlstra | 8f111bc | 2017-12-20 16:26:12 +0100 | [diff] [blame] | 447 | next_f = sg_policy->next_freq; |
Viresh Kumar | 07458f6 | 2017-11-08 20:23:55 +0530 | [diff] [blame] | 448 | |
Peter Zijlstra | 8f111bc | 2017-12-20 16:26:12 +0100 | [diff] [blame] | 449 | /* Reset cached freq as next_freq has changed */ |
| 450 | sg_policy->cached_raw_freq = 0; |
Rafael J. Wysocki | 58919e8 | 2016-08-16 22:14:55 +0200 | [diff] [blame] | 451 | } |
Peter Zijlstra | 8f111bc | 2017-12-20 16:26:12 +0100 | [diff] [blame] | 452 | |
Rafael J. Wysocki | a61dec7 | 2018-05-23 11:47:45 +0200 | [diff] [blame] | 453 | /* |
| 454 | * This code runs under rq->lock for the target CPU, so it won't run |
| 455 | * concurrently on two different CPUs for the same target and it is not |
| 456 | * necessary to acquire the lock in the fast switch case. |
| 457 | */ |
| 458 | if (sg_policy->policy->fast_switch_enabled) { |
| 459 | sugov_fast_switch(sg_policy, time, next_f); |
| 460 | } else { |
| 461 | raw_spin_lock(&sg_policy->update_lock); |
| 462 | sugov_deferred_update(sg_policy, time, next_f); |
| 463 | raw_spin_unlock(&sg_policy->update_lock); |
| 464 | } |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 465 | } |
| 466 | |
Juri Lelli | d86ab9c | 2017-05-03 14:30:48 +0100 | [diff] [blame] | 467 | static unsigned int sugov_next_freq_shared(struct sugov_cpu *sg_cpu, u64 time) |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 468 | { |
Steve Muckle | 5cbea46 | 2016-07-13 13:25:26 -0700 | [diff] [blame] | 469 | struct sugov_policy *sg_policy = sg_cpu->sg_policy; |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 470 | struct cpufreq_policy *policy = sg_policy->policy; |
Viresh Kumar | cba1dfb | 2017-03-09 09:34:54 +0530 | [diff] [blame] | 471 | unsigned long util = 0, max = 1; |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 472 | unsigned int j; |
| 473 | |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 474 | for_each_cpu(j, policy->cpus) { |
Viresh Kumar | cba1dfb | 2017-03-09 09:34:54 +0530 | [diff] [blame] | 475 | struct sugov_cpu *j_sg_cpu = &per_cpu(sugov_cpu, j); |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 476 | unsigned long j_util, j_max; |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 477 | |
Vincent Guittot | dfa444d | 2018-06-28 17:45:11 +0200 | [diff] [blame] | 478 | j_util = sugov_get_util(j_sg_cpu); |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 479 | j_max = j_sg_cpu->max; |
Patrick Bellasi | fd7d528 | 2018-05-22 12:07:54 +0100 | [diff] [blame] | 480 | sugov_iowait_apply(j_sg_cpu, time, &j_util, &j_max); |
| 481 | |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 482 | if (j_util * max > j_max * util) { |
| 483 | util = j_util; |
| 484 | max = j_max; |
| 485 | } |
| 486 | } |
| 487 | |
Viresh Kumar | 655cb1e | 2017-03-02 14:03:21 +0530 | [diff] [blame] | 488 | return get_next_freq(sg_policy, util, max); |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 489 | } |
| 490 | |
Ingo Molnar | 97fb7a0 | 2018-03-03 14:01:12 +0100 | [diff] [blame] | 491 | static void |
| 492 | sugov_update_shared(struct update_util_data *hook, u64 time, unsigned int flags) |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 493 | { |
| 494 | struct sugov_cpu *sg_cpu = container_of(hook, struct sugov_cpu, update_util); |
| 495 | struct sugov_policy *sg_policy = sg_cpu->sg_policy; |
| 496 | unsigned int next_f; |
| 497 | |
| 498 | raw_spin_lock(&sg_policy->update_lock); |
| 499 | |
Patrick Bellasi | fd7d528 | 2018-05-22 12:07:54 +0100 | [diff] [blame] | 500 | sugov_iowait_boost(sg_cpu, time, flags); |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 501 | sg_cpu->last_update = time; |
| 502 | |
Claudio Scordino | e97a90f | 2018-03-13 11:35:40 +0100 | [diff] [blame] | 503 | ignore_dl_rate_limit(sg_cpu, sg_policy); |
Viresh Kumar | cba1dfb | 2017-03-09 09:34:54 +0530 | [diff] [blame] | 504 | |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 505 | if (sugov_should_update_freq(sg_policy, time)) { |
Peter Zijlstra | 8f111bc | 2017-12-20 16:26:12 +0100 | [diff] [blame] | 506 | next_f = sugov_next_freq_shared(sg_cpu, time); |
Rafael J. Wysocki | a61dec7 | 2018-05-23 11:47:45 +0200 | [diff] [blame] | 507 | |
| 508 | if (sg_policy->policy->fast_switch_enabled) |
| 509 | sugov_fast_switch(sg_policy, time, next_f); |
| 510 | else |
| 511 | sugov_deferred_update(sg_policy, time, next_f); |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 512 | } |
| 513 | |
| 514 | raw_spin_unlock(&sg_policy->update_lock); |
| 515 | } |
| 516 | |
| 517 | static void sugov_work(struct kthread_work *work) |
| 518 | { |
| 519 | struct sugov_policy *sg_policy = container_of(work, struct sugov_policy, work); |
Joel Fernandes (Google) | 152db03 | 2018-05-22 15:55:53 -0700 | [diff] [blame] | 520 | unsigned int freq; |
| 521 | unsigned long flags; |
| 522 | |
| 523 | /* |
| 524 | * Hold sg_policy->update_lock shortly to handle the case where: |
| 525 | * incase sg_policy->next_freq is read here, and then updated by |
Rafael J. Wysocki | a61dec7 | 2018-05-23 11:47:45 +0200 | [diff] [blame] | 526 | * sugov_deferred_update() just before work_in_progress is set to false |
Joel Fernandes (Google) | 152db03 | 2018-05-22 15:55:53 -0700 | [diff] [blame] | 527 | * here, we may miss queueing the new update. |
| 528 | * |
| 529 | * Note: If a work was queued after the update_lock is released, |
Rafael J. Wysocki | a61dec7 | 2018-05-23 11:47:45 +0200 | [diff] [blame] | 530 | * sugov_work() will just be called again by kthread_work code; and the |
Joel Fernandes (Google) | 152db03 | 2018-05-22 15:55:53 -0700 | [diff] [blame] | 531 | * request will be proceed before the sugov thread sleeps. |
| 532 | */ |
| 533 | raw_spin_lock_irqsave(&sg_policy->update_lock, flags); |
| 534 | freq = sg_policy->next_freq; |
| 535 | sg_policy->work_in_progress = false; |
| 536 | raw_spin_unlock_irqrestore(&sg_policy->update_lock, flags); |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 537 | |
| 538 | mutex_lock(&sg_policy->work_lock); |
Joel Fernandes (Google) | 152db03 | 2018-05-22 15:55:53 -0700 | [diff] [blame] | 539 | __cpufreq_driver_target(sg_policy->policy, freq, CPUFREQ_RELATION_L); |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 540 | mutex_unlock(&sg_policy->work_lock); |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 541 | } |
| 542 | |
| 543 | static void sugov_irq_work(struct irq_work *irq_work) |
| 544 | { |
| 545 | struct sugov_policy *sg_policy; |
| 546 | |
| 547 | sg_policy = container_of(irq_work, struct sugov_policy, irq_work); |
Viresh Kumar | 02a7b1e | 2016-11-15 13:53:22 +0530 | [diff] [blame] | 548 | |
Viresh Kumar | 02a7b1e | 2016-11-15 13:53:22 +0530 | [diff] [blame] | 549 | kthread_queue_work(&sg_policy->worker, &sg_policy->work); |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 550 | } |
| 551 | |
| 552 | /************************** sysfs interface ************************/ |
| 553 | |
| 554 | static struct sugov_tunables *global_tunables; |
| 555 | static DEFINE_MUTEX(global_tunables_lock); |
| 556 | |
| 557 | static inline struct sugov_tunables *to_sugov_tunables(struct gov_attr_set *attr_set) |
| 558 | { |
| 559 | return container_of(attr_set, struct sugov_tunables, attr_set); |
| 560 | } |
| 561 | |
| 562 | static ssize_t rate_limit_us_show(struct gov_attr_set *attr_set, char *buf) |
| 563 | { |
| 564 | struct sugov_tunables *tunables = to_sugov_tunables(attr_set); |
| 565 | |
| 566 | return sprintf(buf, "%u\n", tunables->rate_limit_us); |
| 567 | } |
| 568 | |
Ingo Molnar | 97fb7a0 | 2018-03-03 14:01:12 +0100 | [diff] [blame] | 569 | static ssize_t |
| 570 | rate_limit_us_store(struct gov_attr_set *attr_set, const char *buf, size_t count) |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 571 | { |
| 572 | struct sugov_tunables *tunables = to_sugov_tunables(attr_set); |
| 573 | struct sugov_policy *sg_policy; |
| 574 | unsigned int rate_limit_us; |
| 575 | |
| 576 | if (kstrtouint(buf, 10, &rate_limit_us)) |
| 577 | return -EINVAL; |
| 578 | |
| 579 | tunables->rate_limit_us = rate_limit_us; |
| 580 | |
| 581 | list_for_each_entry(sg_policy, &attr_set->policy_list, tunables_hook) |
| 582 | sg_policy->freq_update_delay_ns = rate_limit_us * NSEC_PER_USEC; |
| 583 | |
| 584 | return count; |
| 585 | } |
| 586 | |
| 587 | static struct governor_attr rate_limit_us = __ATTR_RW(rate_limit_us); |
| 588 | |
| 589 | static struct attribute *sugov_attributes[] = { |
| 590 | &rate_limit_us.attr, |
| 591 | NULL |
| 592 | }; |
| 593 | |
| 594 | static struct kobj_type sugov_tunables_ktype = { |
| 595 | .default_attrs = sugov_attributes, |
| 596 | .sysfs_ops = &governor_sysfs_ops, |
| 597 | }; |
| 598 | |
| 599 | /********************** cpufreq governor interface *********************/ |
| 600 | |
| 601 | static struct cpufreq_governor schedutil_gov; |
| 602 | |
| 603 | static struct sugov_policy *sugov_policy_alloc(struct cpufreq_policy *policy) |
| 604 | { |
| 605 | struct sugov_policy *sg_policy; |
| 606 | |
| 607 | sg_policy = kzalloc(sizeof(*sg_policy), GFP_KERNEL); |
| 608 | if (!sg_policy) |
| 609 | return NULL; |
| 610 | |
| 611 | sg_policy->policy = policy; |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 612 | raw_spin_lock_init(&sg_policy->update_lock); |
| 613 | return sg_policy; |
| 614 | } |
| 615 | |
| 616 | static void sugov_policy_free(struct sugov_policy *sg_policy) |
| 617 | { |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 618 | kfree(sg_policy); |
| 619 | } |
| 620 | |
Viresh Kumar | 02a7b1e | 2016-11-15 13:53:22 +0530 | [diff] [blame] | 621 | static int sugov_kthread_create(struct sugov_policy *sg_policy) |
| 622 | { |
| 623 | struct task_struct *thread; |
Juri Lelli | 794a56e | 2017-12-04 11:23:20 +0100 | [diff] [blame] | 624 | struct sched_attr attr = { |
Ingo Molnar | 97fb7a0 | 2018-03-03 14:01:12 +0100 | [diff] [blame] | 625 | .size = sizeof(struct sched_attr), |
| 626 | .sched_policy = SCHED_DEADLINE, |
| 627 | .sched_flags = SCHED_FLAG_SUGOV, |
| 628 | .sched_nice = 0, |
| 629 | .sched_priority = 0, |
Juri Lelli | 794a56e | 2017-12-04 11:23:20 +0100 | [diff] [blame] | 630 | /* |
| 631 | * Fake (unused) bandwidth; workaround to "fix" |
| 632 | * priority inheritance. |
| 633 | */ |
| 634 | .sched_runtime = 1000000, |
| 635 | .sched_deadline = 10000000, |
| 636 | .sched_period = 10000000, |
| 637 | }; |
Viresh Kumar | 02a7b1e | 2016-11-15 13:53:22 +0530 | [diff] [blame] | 638 | struct cpufreq_policy *policy = sg_policy->policy; |
| 639 | int ret; |
| 640 | |
| 641 | /* kthread only required for slow path */ |
| 642 | if (policy->fast_switch_enabled) |
| 643 | return 0; |
| 644 | |
| 645 | kthread_init_work(&sg_policy->work, sugov_work); |
| 646 | kthread_init_worker(&sg_policy->worker); |
| 647 | thread = kthread_create(kthread_worker_fn, &sg_policy->worker, |
| 648 | "sugov:%d", |
| 649 | cpumask_first(policy->related_cpus)); |
| 650 | if (IS_ERR(thread)) { |
| 651 | pr_err("failed to create sugov thread: %ld\n", PTR_ERR(thread)); |
| 652 | return PTR_ERR(thread); |
| 653 | } |
| 654 | |
Juri Lelli | 794a56e | 2017-12-04 11:23:20 +0100 | [diff] [blame] | 655 | ret = sched_setattr_nocheck(thread, &attr); |
Viresh Kumar | 02a7b1e | 2016-11-15 13:53:22 +0530 | [diff] [blame] | 656 | if (ret) { |
| 657 | kthread_stop(thread); |
Juri Lelli | 794a56e | 2017-12-04 11:23:20 +0100 | [diff] [blame] | 658 | pr_warn("%s: failed to set SCHED_DEADLINE\n", __func__); |
Viresh Kumar | 02a7b1e | 2016-11-15 13:53:22 +0530 | [diff] [blame] | 659 | return ret; |
| 660 | } |
| 661 | |
| 662 | sg_policy->thread = thread; |
Dietmar Eggemann | 1b04722 | 2018-05-08 08:33:40 +0100 | [diff] [blame] | 663 | kthread_bind_mask(thread, policy->related_cpus); |
Viresh Kumar | 21ef572 | 2016-11-15 13:53:23 +0530 | [diff] [blame] | 664 | init_irq_work(&sg_policy->irq_work, sugov_irq_work); |
| 665 | mutex_init(&sg_policy->work_lock); |
| 666 | |
Viresh Kumar | 02a7b1e | 2016-11-15 13:53:22 +0530 | [diff] [blame] | 667 | wake_up_process(thread); |
| 668 | |
| 669 | return 0; |
| 670 | } |
| 671 | |
| 672 | static void sugov_kthread_stop(struct sugov_policy *sg_policy) |
| 673 | { |
| 674 | /* kthread only required for slow path */ |
| 675 | if (sg_policy->policy->fast_switch_enabled) |
| 676 | return; |
| 677 | |
| 678 | kthread_flush_worker(&sg_policy->worker); |
| 679 | kthread_stop(sg_policy->thread); |
Viresh Kumar | 21ef572 | 2016-11-15 13:53:23 +0530 | [diff] [blame] | 680 | mutex_destroy(&sg_policy->work_lock); |
Viresh Kumar | 02a7b1e | 2016-11-15 13:53:22 +0530 | [diff] [blame] | 681 | } |
| 682 | |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 683 | static struct sugov_tunables *sugov_tunables_alloc(struct sugov_policy *sg_policy) |
| 684 | { |
| 685 | struct sugov_tunables *tunables; |
| 686 | |
| 687 | tunables = kzalloc(sizeof(*tunables), GFP_KERNEL); |
| 688 | if (tunables) { |
| 689 | gov_attr_set_init(&tunables->attr_set, &sg_policy->tunables_hook); |
| 690 | if (!have_governor_per_policy()) |
| 691 | global_tunables = tunables; |
| 692 | } |
| 693 | return tunables; |
| 694 | } |
| 695 | |
| 696 | static void sugov_tunables_free(struct sugov_tunables *tunables) |
| 697 | { |
| 698 | if (!have_governor_per_policy()) |
| 699 | global_tunables = NULL; |
| 700 | |
| 701 | kfree(tunables); |
| 702 | } |
| 703 | |
| 704 | static int sugov_init(struct cpufreq_policy *policy) |
| 705 | { |
| 706 | struct sugov_policy *sg_policy; |
| 707 | struct sugov_tunables *tunables; |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 708 | int ret = 0; |
| 709 | |
| 710 | /* State should be equivalent to EXIT */ |
| 711 | if (policy->governor_data) |
| 712 | return -EBUSY; |
| 713 | |
Viresh Kumar | 4a71ce4 | 2016-11-15 13:53:21 +0530 | [diff] [blame] | 714 | cpufreq_enable_fast_switch(policy); |
| 715 | |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 716 | sg_policy = sugov_policy_alloc(policy); |
Viresh Kumar | 4a71ce4 | 2016-11-15 13:53:21 +0530 | [diff] [blame] | 717 | if (!sg_policy) { |
| 718 | ret = -ENOMEM; |
| 719 | goto disable_fast_switch; |
| 720 | } |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 721 | |
Viresh Kumar | 02a7b1e | 2016-11-15 13:53:22 +0530 | [diff] [blame] | 722 | ret = sugov_kthread_create(sg_policy); |
| 723 | if (ret) |
| 724 | goto free_sg_policy; |
| 725 | |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 726 | mutex_lock(&global_tunables_lock); |
| 727 | |
| 728 | if (global_tunables) { |
| 729 | if (WARN_ON(have_governor_per_policy())) { |
| 730 | ret = -EINVAL; |
Viresh Kumar | 02a7b1e | 2016-11-15 13:53:22 +0530 | [diff] [blame] | 731 | goto stop_kthread; |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 732 | } |
| 733 | policy->governor_data = sg_policy; |
| 734 | sg_policy->tunables = global_tunables; |
| 735 | |
| 736 | gov_attr_set_get(&global_tunables->attr_set, &sg_policy->tunables_hook); |
| 737 | goto out; |
| 738 | } |
| 739 | |
| 740 | tunables = sugov_tunables_alloc(sg_policy); |
| 741 | if (!tunables) { |
| 742 | ret = -ENOMEM; |
Viresh Kumar | 02a7b1e | 2016-11-15 13:53:22 +0530 | [diff] [blame] | 743 | goto stop_kthread; |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 744 | } |
| 745 | |
Viresh Kumar | aa7519a | 2017-07-19 15:42:42 +0530 | [diff] [blame] | 746 | tunables->rate_limit_us = cpufreq_policy_transition_delay_us(policy); |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 747 | |
| 748 | policy->governor_data = sg_policy; |
| 749 | sg_policy->tunables = tunables; |
| 750 | |
| 751 | ret = kobject_init_and_add(&tunables->attr_set.kobj, &sugov_tunables_ktype, |
| 752 | get_governor_parent_kobj(policy), "%s", |
| 753 | schedutil_gov.name); |
| 754 | if (ret) |
| 755 | goto fail; |
| 756 | |
Viresh Kumar | 8e2ddb0 | 2016-11-15 13:53:20 +0530 | [diff] [blame] | 757 | out: |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 758 | mutex_unlock(&global_tunables_lock); |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 759 | return 0; |
| 760 | |
Viresh Kumar | 8e2ddb0 | 2016-11-15 13:53:20 +0530 | [diff] [blame] | 761 | fail: |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 762 | policy->governor_data = NULL; |
| 763 | sugov_tunables_free(tunables); |
| 764 | |
Viresh Kumar | 02a7b1e | 2016-11-15 13:53:22 +0530 | [diff] [blame] | 765 | stop_kthread: |
| 766 | sugov_kthread_stop(sg_policy); |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 767 | mutex_unlock(&global_tunables_lock); |
| 768 | |
Jules Maselbas | 1b5d43cf | 2018-03-29 15:43:01 +0100 | [diff] [blame] | 769 | free_sg_policy: |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 770 | sugov_policy_free(sg_policy); |
Viresh Kumar | 4a71ce4 | 2016-11-15 13:53:21 +0530 | [diff] [blame] | 771 | |
| 772 | disable_fast_switch: |
| 773 | cpufreq_disable_fast_switch(policy); |
| 774 | |
Viresh Kumar | 60f05e8 | 2016-05-18 17:55:28 +0530 | [diff] [blame] | 775 | pr_err("initialization failed (error %d)\n", ret); |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 776 | return ret; |
| 777 | } |
| 778 | |
Rafael J. Wysocki | e788892 | 2016-06-02 23:24:15 +0200 | [diff] [blame] | 779 | static void sugov_exit(struct cpufreq_policy *policy) |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 780 | { |
| 781 | struct sugov_policy *sg_policy = policy->governor_data; |
| 782 | struct sugov_tunables *tunables = sg_policy->tunables; |
| 783 | unsigned int count; |
| 784 | |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 785 | mutex_lock(&global_tunables_lock); |
| 786 | |
| 787 | count = gov_attr_set_put(&tunables->attr_set, &sg_policy->tunables_hook); |
| 788 | policy->governor_data = NULL; |
| 789 | if (!count) |
| 790 | sugov_tunables_free(tunables); |
| 791 | |
| 792 | mutex_unlock(&global_tunables_lock); |
| 793 | |
Viresh Kumar | 02a7b1e | 2016-11-15 13:53:22 +0530 | [diff] [blame] | 794 | sugov_kthread_stop(sg_policy); |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 795 | sugov_policy_free(sg_policy); |
Viresh Kumar | 4a71ce4 | 2016-11-15 13:53:21 +0530 | [diff] [blame] | 796 | cpufreq_disable_fast_switch(policy); |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 797 | } |
| 798 | |
| 799 | static int sugov_start(struct cpufreq_policy *policy) |
| 800 | { |
| 801 | struct sugov_policy *sg_policy = policy->governor_data; |
| 802 | unsigned int cpu; |
| 803 | |
Ingo Molnar | 97fb7a0 | 2018-03-03 14:01:12 +0100 | [diff] [blame] | 804 | sg_policy->freq_update_delay_ns = sg_policy->tunables->rate_limit_us * NSEC_PER_USEC; |
| 805 | sg_policy->last_freq_update_time = 0; |
Viresh Kumar | ecd2884 | 2018-05-09 16:05:24 +0530 | [diff] [blame] | 806 | sg_policy->next_freq = 0; |
Ingo Molnar | 97fb7a0 | 2018-03-03 14:01:12 +0100 | [diff] [blame] | 807 | sg_policy->work_in_progress = false; |
| 808 | sg_policy->need_freq_update = false; |
| 809 | sg_policy->cached_raw_freq = 0; |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 810 | |
| 811 | for_each_cpu(cpu, policy->cpus) { |
| 812 | struct sugov_cpu *sg_cpu = &per_cpu(sugov_cpu, cpu); |
| 813 | |
Rafael J. Wysocki | 4296f23 | 2017-03-19 14:30:02 +0100 | [diff] [blame] | 814 | memset(sg_cpu, 0, sizeof(*sg_cpu)); |
Ingo Molnar | 97fb7a0 | 2018-03-03 14:01:12 +0100 | [diff] [blame] | 815 | sg_cpu->cpu = cpu; |
| 816 | sg_cpu->sg_policy = sg_policy; |
Ingo Molnar | 97fb7a0 | 2018-03-03 14:01:12 +0100 | [diff] [blame] | 817 | sg_cpu->iowait_boost_max = policy->cpuinfo.max_freq; |
Vikram Mulukutla | ab2f7cf | 2017-07-06 10:53:20 -0700 | [diff] [blame] | 818 | } |
| 819 | |
| 820 | for_each_cpu(cpu, policy->cpus) { |
| 821 | struct sugov_cpu *sg_cpu = &per_cpu(sugov_cpu, cpu); |
| 822 | |
Rafael J. Wysocki | 4296f23 | 2017-03-19 14:30:02 +0100 | [diff] [blame] | 823 | cpufreq_add_update_util_hook(cpu, &sg_cpu->update_util, |
| 824 | policy_is_shared(policy) ? |
| 825 | sugov_update_shared : |
| 826 | sugov_update_single); |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 827 | } |
| 828 | return 0; |
| 829 | } |
| 830 | |
Rafael J. Wysocki | e788892 | 2016-06-02 23:24:15 +0200 | [diff] [blame] | 831 | static void sugov_stop(struct cpufreq_policy *policy) |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 832 | { |
| 833 | struct sugov_policy *sg_policy = policy->governor_data; |
| 834 | unsigned int cpu; |
| 835 | |
| 836 | for_each_cpu(cpu, policy->cpus) |
| 837 | cpufreq_remove_update_util_hook(cpu); |
| 838 | |
| 839 | synchronize_sched(); |
| 840 | |
Viresh Kumar | 21ef572 | 2016-11-15 13:53:23 +0530 | [diff] [blame] | 841 | if (!policy->fast_switch_enabled) { |
| 842 | irq_work_sync(&sg_policy->irq_work); |
| 843 | kthread_cancel_work_sync(&sg_policy->work); |
| 844 | } |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 845 | } |
| 846 | |
Rafael J. Wysocki | e788892 | 2016-06-02 23:24:15 +0200 | [diff] [blame] | 847 | static void sugov_limits(struct cpufreq_policy *policy) |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 848 | { |
| 849 | struct sugov_policy *sg_policy = policy->governor_data; |
| 850 | |
| 851 | if (!policy->fast_switch_enabled) { |
| 852 | mutex_lock(&sg_policy->work_lock); |
Viresh Kumar | bf2be2d | 2016-05-18 17:55:31 +0530 | [diff] [blame] | 853 | cpufreq_policy_apply_limits(policy); |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 854 | mutex_unlock(&sg_policy->work_lock); |
| 855 | } |
| 856 | |
| 857 | sg_policy->need_freq_update = true; |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 858 | } |
| 859 | |
| 860 | static struct cpufreq_governor schedutil_gov = { |
Ingo Molnar | 97fb7a0 | 2018-03-03 14:01:12 +0100 | [diff] [blame] | 861 | .name = "schedutil", |
| 862 | .owner = THIS_MODULE, |
| 863 | .dynamic_switching = true, |
| 864 | .init = sugov_init, |
| 865 | .exit = sugov_exit, |
| 866 | .start = sugov_start, |
| 867 | .stop = sugov_stop, |
| 868 | .limits = sugov_limits, |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 869 | }; |
| 870 | |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 871 | #ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_SCHEDUTIL |
| 872 | struct cpufreq_governor *cpufreq_default_governor(void) |
| 873 | { |
| 874 | return &schedutil_gov; |
| 875 | } |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 876 | #endif |
Rafael J. Wysocki | 58919e8 | 2016-08-16 22:14:55 +0200 | [diff] [blame] | 877 | |
| 878 | static int __init sugov_register(void) |
| 879 | { |
| 880 | return cpufreq_register_governor(&schedutil_gov); |
| 881 | } |
| 882 | fs_initcall(sugov_register); |