* [PATCH v4 1/3] arm64: topology: Set scale freq source only for the CPUs that have not been set before
2025-08-14 7:28 [PATCH v4 0/3] arm64: topology: Setup AMU FIE for online CPUs only Lifeng Zheng
@ 2025-08-14 7:28 ` Lifeng Zheng
2025-08-14 7:28 ` [PATCH v4 2/3] cpufreq: Add a new function to get cpufreq policy without checking if the CPU is online Lifeng Zheng
2025-08-14 7:28 ` [PATCH v4 3/3] arm64: topology: Setup AMU FIE for online CPUs only Lifeng Zheng
2 siblings, 0 replies; 5+ messages in thread
From: Lifeng Zheng @ 2025-08-14 7:28 UTC (permalink / raw)
To: catalin.marinas, will, rafael, viresh.kumar, beata.michalska,
sudeep.holla
Cc: linux-arm-kernel, linux-pm, linux-kernel, linuxarm,
jonathan.cameron, vincent.guittot, yangyicong, zhanjie9,
lihuisong, yubowen8, linhongye, zhenglifeng1
The scale freq source of the CPUs in 'amu_fie_cpus' mask are already set to
AMU tick before, so in amu_fie_setup(), only the CPUs in the 'cpus' mask
should be set.
Signed-off-by: Lifeng Zheng <zhenglifeng1@huawei.com>
---
arch/arm64/kernel/topology.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/arch/arm64/kernel/topology.c b/arch/arm64/kernel/topology.c
index 5d07ee85bdae..9317a618bb87 100644
--- a/arch/arm64/kernel/topology.c
+++ b/arch/arm64/kernel/topology.c
@@ -373,7 +373,7 @@ static void amu_fie_setup(const struct cpumask *cpus)
cpumask_or(amu_fie_cpus, amu_fie_cpus, cpus);
- topology_set_scale_freq_source(&amu_sfd, amu_fie_cpus);
+ topology_set_scale_freq_source(&amu_sfd, cpus);
pr_debug("CPUs[%*pbl]: counters will be used for FIE.",
cpumask_pr_args(cpus));
--
2.33.0
^ permalink raw reply [flat|nested] 5+ messages in thread* [PATCH v4 2/3] cpufreq: Add a new function to get cpufreq policy without checking if the CPU is online
2025-08-14 7:28 [PATCH v4 0/3] arm64: topology: Setup AMU FIE for online CPUs only Lifeng Zheng
2025-08-14 7:28 ` [PATCH v4 1/3] arm64: topology: Set scale freq source only for the CPUs that have not been set before Lifeng Zheng
@ 2025-08-14 7:28 ` Lifeng Zheng
2025-08-14 7:28 ` [PATCH v4 3/3] arm64: topology: Setup AMU FIE for online CPUs only Lifeng Zheng
2 siblings, 0 replies; 5+ messages in thread
From: Lifeng Zheng @ 2025-08-14 7:28 UTC (permalink / raw)
To: catalin.marinas, will, rafael, viresh.kumar, beata.michalska,
sudeep.holla
Cc: linux-arm-kernel, linux-pm, linux-kernel, linuxarm,
jonathan.cameron, vincent.guittot, yangyicong, zhanjie9,
lihuisong, yubowen8, linhongye, zhenglifeng1
cpufreq_cpu_get_raw() gets cpufreq policy only if the CPU is in
policy->cpus mask, which means the CPU is already online. But in some
cases, the policy is needed before the CPU is added to cpus mask. Add a
function to get the policy in these cases.
Signed-off-by: Lifeng Zheng <zhenglifeng1@huawei.com>
---
drivers/cpufreq/cpufreq.c | 6 ++++++
include/linux/cpufreq.h | 1 +
2 files changed, 7 insertions(+)
diff --git a/drivers/cpufreq/cpufreq.c b/drivers/cpufreq/cpufreq.c
index fc7eace8b65b..78ca68ea754d 100644
--- a/drivers/cpufreq/cpufreq.c
+++ b/drivers/cpufreq/cpufreq.c
@@ -198,6 +198,12 @@ struct cpufreq_policy *cpufreq_cpu_get_raw(unsigned int cpu)
}
EXPORT_SYMBOL_GPL(cpufreq_cpu_get_raw);
+struct cpufreq_policy *cpufreq_cpu_policy(unsigned int cpu)
+{
+ return per_cpu(cpufreq_cpu_data, cpu);
+}
+EXPORT_SYMBOL_GPL(cpufreq_cpu_policy);
+
unsigned int cpufreq_generic_get(unsigned int cpu)
{
struct cpufreq_policy *policy = cpufreq_cpu_get_raw(cpu);
diff --git a/include/linux/cpufreq.h b/include/linux/cpufreq.h
index 95f3807c8c55..f135edeef925 100644
--- a/include/linux/cpufreq.h
+++ b/include/linux/cpufreq.h
@@ -205,6 +205,7 @@ struct cpufreq_freqs {
#ifdef CONFIG_CPU_FREQ
struct cpufreq_policy *cpufreq_cpu_get_raw(unsigned int cpu);
+struct cpufreq_policy *cpufreq_cpu_policy(unsigned int cpu);
struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu);
void cpufreq_cpu_put(struct cpufreq_policy *policy);
#else
--
2.33.0
^ permalink raw reply [flat|nested] 5+ messages in thread* [PATCH v4 3/3] arm64: topology: Setup AMU FIE for online CPUs only
2025-08-14 7:28 [PATCH v4 0/3] arm64: topology: Setup AMU FIE for online CPUs only Lifeng Zheng
2025-08-14 7:28 ` [PATCH v4 1/3] arm64: topology: Set scale freq source only for the CPUs that have not been set before Lifeng Zheng
2025-08-14 7:28 ` [PATCH v4 2/3] cpufreq: Add a new function to get cpufreq policy without checking if the CPU is online Lifeng Zheng
@ 2025-08-14 7:28 ` Lifeng Zheng
2025-08-15 3:47 ` kernel test robot
2 siblings, 1 reply; 5+ messages in thread
From: Lifeng Zheng @ 2025-08-14 7:28 UTC (permalink / raw)
To: catalin.marinas, will, rafael, viresh.kumar, beata.michalska,
sudeep.holla
Cc: linux-arm-kernel, linux-pm, linux-kernel, linuxarm,
jonathan.cameron, vincent.guittot, yangyicong, zhanjie9,
lihuisong, yubowen8, linhongye, zhenglifeng1
When boot with maxcpu=1 restrict, and LPI(Low Power Idle States) is on,
only CPU0 will go online. The support AMU flag of CPU0 will be set but the
flags of other CPUs will not. This will cause AMU FIE set up fail for CPU0
when it shares a cpufreq policy with other CPU(s). After that, when other
CPUs are finally online and the support AMU flags of them are set, they'll
never have a chance to set up AMU FIE, even though they're eligible.
To solve this problem, the process of setting up AMU FIE needs to be
modified as follows:
1. Set up AMU FIE only for the online CPUs.
2. Try to set up AMU FIE each time a CPU goes online and do the
freq_counters_valid() check. If this check fails, clear scale freq source
of all the CPUs related to the same policy, in case they use different
source of the freq scale.
At the same time, this change also be applied to cpufreq when calling
arch_set_freq_scale.
Signed-off-by: Lifeng Zheng <zhenglifeng1@huawei.com>
---
arch/arm64/kernel/topology.c | 54 ++++++++++++++++++++++++++++++++++--
drivers/cpufreq/cpufreq.c | 4 +--
2 files changed, 54 insertions(+), 4 deletions(-)
diff --git a/arch/arm64/kernel/topology.c b/arch/arm64/kernel/topology.c
index 9317a618bb87..a9d9e9969cea 100644
--- a/arch/arm64/kernel/topology.c
+++ b/arch/arm64/kernel/topology.c
@@ -385,7 +385,7 @@ static int init_amu_fie_callback(struct notifier_block *nb, unsigned long val,
struct cpufreq_policy *policy = data;
if (val == CPUFREQ_CREATE_POLICY)
- amu_fie_setup(policy->related_cpus);
+ amu_fie_setup(policy->cpus);
/*
* We don't need to handle CPUFREQ_REMOVE_POLICY event as the AMU
@@ -404,10 +404,60 @@ static struct notifier_block init_amu_fie_notifier = {
.notifier_call = init_amu_fie_callback,
};
+static int cpuhp_topology_online(unsigned int cpu)
+{
+ struct cpufreq_policy *policy = cpufreq_cpu_policy(cpu);
+
+ /*
+ * If the online CPUs are not all AMU FIE CPUs or the new one is already
+ * an AMU FIE one, no need to set it.
+ */
+ if (!policy || !cpumask_available(amu_fie_cpus) ||
+ !cpumask_subset(policy->cpus, amu_fie_cpus) ||
+ cpumask_test_cpu(cpu, amu_fie_cpus))
+ return 0;
+
+ /*
+ * If the new online CPU cannot pass this check, all the CPUs related to
+ * the same policy should be clear from amu_fie_cpus mask, otherwise they
+ * may use different source of the freq scale.
+ */
+ if (WARN_ON(!freq_counters_valid(cpu))) {
+ topology_clear_scale_freq_source(SCALE_FREQ_SOURCE_ARCH,
+ policy->related_cpus);
+ cpumask_andnot(amu_fie_cpus, amu_fie_cpus, policy->related_cpus);
+ return 0;
+ }
+
+ cpumask_set_cpu(cpu, amu_fie_cpus);
+
+ topology_set_scale_freq_source(&amu_sfd, cpumask_of(cpu));
+
+ pr_debug("CPU[%u]: counter will be used for FIE.", cpu);
+
+ return 0;
+}
+
static int __init init_amu_fie(void)
{
- return cpufreq_register_notifier(&init_amu_fie_notifier,
+ int ret;
+
+ ret = cpufreq_register_notifier(&init_amu_fie_notifier,
CPUFREQ_POLICY_NOTIFIER);
+ if (ret)
+ return ret;
+
+ ret = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN,
+ "arm64/topology:online",
+ cpuhp_topology_online,
+ NULL);
+ if (ret < 0) {
+ cpufreq_unregister_notifier(&init_amu_fie_notifier,
+ CPUFREQ_POLICY_NOTIFIER);
+ return ret;
+ }
+
+ return 0;
}
core_initcall(init_amu_fie);
diff --git a/drivers/cpufreq/cpufreq.c b/drivers/cpufreq/cpufreq.c
index 78ca68ea754d..d1890a2af1af 100644
--- a/drivers/cpufreq/cpufreq.c
+++ b/drivers/cpufreq/cpufreq.c
@@ -417,7 +417,7 @@ void cpufreq_freq_transition_end(struct cpufreq_policy *policy,
cpufreq_notify_post_transition(policy, freqs, transition_failed);
- arch_set_freq_scale(policy->related_cpus,
+ arch_set_freq_scale(policy->cpus,
policy->cur,
arch_scale_freq_ref(policy->cpu));
@@ -2219,7 +2219,7 @@ unsigned int cpufreq_driver_fast_switch(struct cpufreq_policy *policy,
return 0;
policy->cur = freq;
- arch_set_freq_scale(policy->related_cpus, freq,
+ arch_set_freq_scale(policy->cpus, freq,
arch_scale_freq_ref(policy->cpu));
cpufreq_stats_record_transition(policy, freq);
--
2.33.0
^ permalink raw reply [flat|nested] 5+ messages in thread