Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

arm64: use activity monitors for frequency invariance

The Frequency Invariance Engine (FIE) is providing a frequency
scaling correction factor that helps achieve more accurate
load-tracking.

So far, for arm and arm64 platforms, this scale factor has been
obtained based on the ratio between the current frequency and the
maximum supported frequency recorded by the cpufreq policy. The
setting of this scale factor is triggered from cpufreq drivers by
calling arch_set_freq_scale. The current frequency used in computation
is the frequency requested by a governor, but it may not be the
frequency that was implemented by the platform.

This correction factor can also be obtained using a core counter and a
constant counter to get information on the performance (frequency based
only) obtained in a period of time. This will more accurately reflect
the actual current frequency of the CPU, compared with the alternative
implementation that reflects the request of a performance level from
the OS.

Therefore, implement arch_scale_freq_tick to use activity monitors, if
present, for the computation of the frequency scale factor.

The use of AMU counters depends on:
- CONFIG_ARM64_AMU_EXTN - depents on the AMU extension being present
- CONFIG_CPU_FREQ - the current frequency obtained using counter
information is divided by the maximum frequency obtained from the
cpufreq policy.

While it is possible to have a combination of CPUs in the system with
and without support for activity monitors, the use of counters for
frequency invariance is only enabled for a CPU if all related CPUs
(CPUs in the same frequency domain) support and have enabled the core
and constant activity monitor counters. In this way, there is a clear
separation between the policies for which arch_set_freq_scale (cpufreq
based FIE) is used, and the policies for which arch_scale_freq_tick
(counter based FIE) is used to set the frequency scale factor. For
this purpose, a late_initcall_sync is registered to trigger validation
work for policies that will enable or disable the use of AMU counters
for frequency invariance. If CONFIG_CPU_FREQ is not defined, the use
of counters is enabled on all CPUs only if all possible CPUs correctly
support the necessary counters.

Signed-off-by: Ionela Voinescu <ionela.voinescu@arm.com>
Reviewed-by: Lukasz Luba <lukasz.luba@arm.com>
Acked-by: Sudeep Holla <sudeep.holla@arm.com>
Cc: Sudeep Holla <sudeep.holla@arm.com>
Cc: Will Deacon <will@kernel.org>
Cc: Catalin Marinas <catalin.marinas@arm.com>
Signed-off-by: Catalin Marinas <catalin.marinas@arm.com>

authored by

Ionela Voinescu and committed by
Catalin Marinas
cd0ed03a bbce8eaa

+207
+9
arch/arm64/include/asm/topology.h
··· 16 16 17 17 #include <linux/arch_topology.h> 18 18 19 + #ifdef CONFIG_ARM64_AMU_EXTN 20 + /* 21 + * Replace task scheduler's default counter-based 22 + * frequency-invariance scale factor setting. 23 + */ 24 + void topology_scale_freq_tick(void); 25 + #define arch_scale_freq_tick topology_scale_freq_tick 26 + #endif /* CONFIG_ARM64_AMU_EXTN */ 27 + 19 28 /* Replace task scheduler's default frequency-invariant accounting */ 20 29 #define arch_scale_freq_capacity topology_get_freq_scale 21 30
+4
arch/arm64/kernel/cpufeature.c
··· 1241 1241 return cpumask_test_cpu(cpu, &amu_cpus); 1242 1242 } 1243 1243 1244 + /* Initialize the use of AMU counters for frequency invariance */ 1245 + extern void init_cpu_freq_invariance_counters(void); 1246 + 1244 1247 static void cpu_amu_enable(struct arm64_cpu_capabilities const *cap) 1245 1248 { 1246 1249 if (has_cpuid_feature(cap, SCOPE_LOCAL_CPU)) { 1247 1250 pr_info("detected CPU%d: Activity Monitors Unit (AMU)\n", 1248 1251 smp_processor_id()); 1249 1252 cpumask_set_cpu(smp_processor_id(), &amu_cpus); 1253 + init_cpu_freq_invariance_counters(); 1250 1254 } 1251 1255 } 1252 1256
+180
arch/arm64/kernel/topology.c
··· 14 14 #include <linux/acpi.h> 15 15 #include <linux/arch_topology.h> 16 16 #include <linux/cacheinfo.h> 17 + #include <linux/cpufreq.h> 17 18 #include <linux/init.h> 18 19 #include <linux/percpu.h> 19 20 ··· 121 120 } 122 121 #endif 123 122 123 + #ifdef CONFIG_ARM64_AMU_EXTN 124 124 125 + #undef pr_fmt 126 + #define pr_fmt(fmt) "AMU: " fmt 127 + 128 + static DEFINE_PER_CPU_READ_MOSTLY(unsigned long, arch_max_freq_scale); 129 + static DEFINE_PER_CPU(u64, arch_const_cycles_prev); 130 + static DEFINE_PER_CPU(u64, arch_core_cycles_prev); 131 + static cpumask_var_t amu_fie_cpus; 132 + 133 + /* Initialize counter reference per-cpu variables for the current CPU */ 134 + void init_cpu_freq_invariance_counters(void) 135 + { 136 + this_cpu_write(arch_core_cycles_prev, 137 + read_sysreg_s(SYS_AMEVCNTR0_CORE_EL0)); 138 + this_cpu_write(arch_const_cycles_prev, 139 + read_sysreg_s(SYS_AMEVCNTR0_CONST_EL0)); 140 + } 141 + 142 + static int validate_cpu_freq_invariance_counters(int cpu) 143 + { 144 + u64 max_freq_hz, ratio; 145 + 146 + if (!cpu_has_amu_feat(cpu)) { 147 + pr_debug("CPU%d: counters are not supported.\n", cpu); 148 + return -EINVAL; 149 + } 150 + 151 + if (unlikely(!per_cpu(arch_const_cycles_prev, cpu) || 152 + !per_cpu(arch_core_cycles_prev, cpu))) { 153 + pr_debug("CPU%d: cycle counters are not enabled.\n", cpu); 154 + return -EINVAL; 155 + } 156 + 157 + /* Convert maximum frequency from KHz to Hz and validate */ 158 + max_freq_hz = cpufreq_get_hw_max_freq(cpu) * 1000; 159 + if (unlikely(!max_freq_hz)) { 160 + pr_debug("CPU%d: invalid maximum frequency.\n", cpu); 161 + return -EINVAL; 162 + } 163 + 164 + /* 165 + * Pre-compute the fixed ratio between the frequency of the constant 166 + * counter and the maximum frequency of the CPU. 167 + * 168 + * const_freq 169 + * arch_max_freq_scale = ---------------- * SCHED_CAPACITY_SCALE² 170 + * cpuinfo_max_freq 171 + * 172 + * We use a factor of 2 * SCHED_CAPACITY_SHIFT -> SCHED_CAPACITY_SCALE² 173 + * in order to ensure a good resolution for arch_max_freq_scale for 174 + * very low arch timer frequencies (down to the KHz range which should 175 + * be unlikely). 176 + */ 177 + ratio = (u64)arch_timer_get_rate() << (2 * SCHED_CAPACITY_SHIFT); 178 + ratio = div64_u64(ratio, max_freq_hz); 179 + if (!ratio) { 180 + WARN_ONCE(1, "System timer frequency too low.\n"); 181 + return -EINVAL; 182 + } 183 + 184 + per_cpu(arch_max_freq_scale, cpu) = (unsigned long)ratio; 185 + 186 + return 0; 187 + } 188 + 189 + static inline bool 190 + enable_policy_freq_counters(int cpu, cpumask_var_t valid_cpus) 191 + { 192 + struct cpufreq_policy *policy = cpufreq_cpu_get(cpu); 193 + 194 + if (!policy) { 195 + pr_debug("CPU%d: No cpufreq policy found.\n", cpu); 196 + return false; 197 + } 198 + 199 + if (cpumask_subset(policy->related_cpus, valid_cpus)) 200 + cpumask_or(amu_fie_cpus, policy->related_cpus, 201 + amu_fie_cpus); 202 + 203 + cpufreq_cpu_put(policy); 204 + 205 + return true; 206 + } 207 + 208 + static DEFINE_STATIC_KEY_FALSE(amu_fie_key); 209 + #define amu_freq_invariant() static_branch_unlikely(&amu_fie_key) 210 + 211 + static int __init init_amu_fie(void) 212 + { 213 + cpumask_var_t valid_cpus; 214 + bool have_policy = false; 215 + int ret = 0; 216 + int cpu; 217 + 218 + if (!zalloc_cpumask_var(&valid_cpus, GFP_KERNEL)) 219 + return -ENOMEM; 220 + 221 + if (!zalloc_cpumask_var(&amu_fie_cpus, GFP_KERNEL)) { 222 + ret = -ENOMEM; 223 + goto free_valid_mask; 224 + } 225 + 226 + for_each_present_cpu(cpu) { 227 + if (validate_cpu_freq_invariance_counters(cpu)) 228 + continue; 229 + cpumask_set_cpu(cpu, valid_cpus); 230 + have_policy |= enable_policy_freq_counters(cpu, valid_cpus); 231 + } 232 + 233 + /* 234 + * If we are not restricted by cpufreq policies, we only enable 235 + * the use of the AMU feature for FIE if all CPUs support AMU. 236 + * Otherwise, enable_policy_freq_counters has already enabled 237 + * policy cpus. 238 + */ 239 + if (!have_policy && cpumask_equal(valid_cpus, cpu_present_mask)) 240 + cpumask_or(amu_fie_cpus, amu_fie_cpus, valid_cpus); 241 + 242 + if (!cpumask_empty(amu_fie_cpus)) { 243 + pr_info("CPUs[%*pbl]: counters will be used for FIE.", 244 + cpumask_pr_args(amu_fie_cpus)); 245 + static_branch_enable(&amu_fie_key); 246 + } 247 + 248 + free_valid_mask: 249 + free_cpumask_var(valid_cpus); 250 + 251 + return ret; 252 + } 253 + late_initcall_sync(init_amu_fie); 254 + 255 + bool arch_freq_counters_available(struct cpumask *cpus) 256 + { 257 + return amu_freq_invariant() && 258 + cpumask_subset(cpus, amu_fie_cpus); 259 + } 260 + 261 + void topology_scale_freq_tick(void) 262 + { 263 + u64 prev_core_cnt, prev_const_cnt; 264 + u64 core_cnt, const_cnt, scale; 265 + int cpu = smp_processor_id(); 266 + 267 + if (!amu_freq_invariant()) 268 + return; 269 + 270 + if (!cpumask_test_cpu(cpu, amu_fie_cpus)) 271 + return; 272 + 273 + const_cnt = read_sysreg_s(SYS_AMEVCNTR0_CONST_EL0); 274 + core_cnt = read_sysreg_s(SYS_AMEVCNTR0_CORE_EL0); 275 + prev_const_cnt = this_cpu_read(arch_const_cycles_prev); 276 + prev_core_cnt = this_cpu_read(arch_core_cycles_prev); 277 + 278 + if (unlikely(core_cnt <= prev_core_cnt || 279 + const_cnt <= prev_const_cnt)) 280 + goto store_and_exit; 281 + 282 + /* 283 + * /\core arch_max_freq_scale 284 + * scale = ------- * -------------------- 285 + * /\const SCHED_CAPACITY_SCALE 286 + * 287 + * See validate_cpu_freq_invariance_counters() for details on 288 + * arch_max_freq_scale and the use of SCHED_CAPACITY_SHIFT. 289 + */ 290 + scale = core_cnt - prev_core_cnt; 291 + scale *= this_cpu_read(arch_max_freq_scale); 292 + scale = div64_u64(scale >> SCHED_CAPACITY_SHIFT, 293 + const_cnt - prev_const_cnt); 294 + 295 + scale = min_t(unsigned long, scale, SCHED_CAPACITY_SCALE); 296 + this_cpu_write(freq_scale, (unsigned long)scale); 297 + 298 + store_and_exit: 299 + this_cpu_write(arch_core_cycles_prev, core_cnt); 300 + this_cpu_write(arch_const_cycles_prev, const_cnt); 301 + } 302 + #endif /* CONFIG_ARM64_AMU_EXTN */
+12
drivers/base/arch_topology.c
··· 21 21 #include <linux/sched.h> 22 22 #include <linux/smp.h> 23 23 24 + __weak bool arch_freq_counters_available(struct cpumask *cpus) 25 + { 26 + return false; 27 + } 24 28 DEFINE_PER_CPU(unsigned long, freq_scale) = SCHED_CAPACITY_SCALE; 25 29 26 30 void arch_set_freq_scale(struct cpumask *cpus, unsigned long cur_freq, ··· 32 28 { 33 29 unsigned long scale; 34 30 int i; 31 + 32 + /* 33 + * If the use of counters for FIE is enabled, just return as we don't 34 + * want to update the scale factor with information from CPUFREQ. 35 + * Instead the scale factor will be updated from arch_scale_freq_tick. 36 + */ 37 + if (arch_freq_counters_available(cpus)) 38 + return; 35 39 36 40 scale = (cur_freq << SCHED_CAPACITY_SHIFT) / max_freq; 37 41
+2
include/linux/arch_topology.h
··· 33 33 return per_cpu(freq_scale, cpu); 34 34 } 35 35 36 + bool arch_freq_counters_available(struct cpumask *cpus); 37 + 36 38 struct cpu_topology { 37 39 int thread_id; 38 40 int core_id;