From nobody Sat Oct 4 08:06:48 2025 Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B58C42848A4; Tue, 19 Aug 2025 07:30:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=45.249.212.187 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755588611; cv=none; b=DMyAJdWYLClZspVkpNiMhs+mN6sQHKSR14DWX4/W34FYk+CfBn/gV+ON0WjFgs8tCrVX3y0idt0B37UVMJl52ZHrt5PKQt+k4XFDwZmwKPHrsb/XtsXgVUaAjrkOfLf30Abz5fruINr4+ttHu6Q5UmAYNQJGmwiTy3eIo0MNPyc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755588611; c=relaxed/simple; bh=XQ4eBg80a96FV2MZDPzFeGhY4E98depsJi9EaPWmjxM=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=s9+6QGCq9hh/bi5S3SVaWN+NHeJeKv3s2gFaE+czo8l6s6Zy7VPDaaMZR9KNqDXV218xFm+oUjiL/YoS2uULO+foOCo6NwvQY0hGcJM1YlgUHeFh9NKpzTjpJFv96IW30XDkgIP3g7Ii6qVgrXF5hxnTrzvDqGRRDy/sMBXxIho= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com; spf=pass smtp.mailfrom=huawei.com; arc=none smtp.client-ip=45.249.212.187 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huawei.com Received: from mail.maildlp.com (unknown [172.19.88.194]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4c5h383K2cz13Mgm; Tue, 19 Aug 2025 15:26:28 +0800 (CST) Received: from kwepemh100008.china.huawei.com (unknown [7.202.181.93]) by mail.maildlp.com (Postfix) with ESMTPS id 4F6A6140275; Tue, 19 Aug 2025 15:30:00 +0800 (CST) Received: from localhost.localdomain (10.50.165.33) by kwepemh100008.china.huawei.com (7.202.181.93) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.11; Tue, 19 Aug 2025 15:29:59 +0800 From: Lifeng Zheng To: , , , , , CC: , , , , , , , , , , , , Subject: [PATCH v5 1/3] arm64: topology: Set scale freq source only for the CPUs that have not been set before Date: Tue, 19 Aug 2025 15:29:29 +0800 Message-ID: <20250819072931.1647431-2-zhenglifeng1@huawei.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20250819072931.1647431-1-zhenglifeng1@huawei.com> References: <20250819072931.1647431-1-zhenglifeng1@huawei.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: kwepems200002.china.huawei.com (7.221.188.68) To kwepemh100008.china.huawei.com (7.202.181.93) Content-Type: text/plain; charset="utf-8" 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 Reviewed-by: Beata Michalska Reviewed-by: Jie Zhan --- 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) =20 cpumask_or(amu_fie_cpus, amu_fie_cpus, cpus); =20 - topology_set_scale_freq_source(&amu_sfd, amu_fie_cpus); + topology_set_scale_freq_source(&amu_sfd, cpus); =20 pr_debug("CPUs[%*pbl]: counters will be used for FIE.", cpumask_pr_args(cpus)); --=20 2.33.0 From nobody Sat Oct 4 08:06:48 2025 Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B58532848A2; Tue, 19 Aug 2025 07:30:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=45.249.212.187 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755588611; cv=none; b=LP4Mc1SLR8Lc340BOte7Nc3AtEWIhXRG5Oli8vs6BMB4N4194PVDwk4V7ui1YXcWEuyuL9WhidU01eMvkjIjjgCYR+yMQBEKp8saLZk3+x6xyvKi9x5wnUhTMRnXZR0S/7bPcwfUquc/HAVgMNIBk7jXiHN1nzdzvHVoWmX1HhY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755588611; c=relaxed/simple; bh=B7cxUmOPIsERLccuPdLVQ0iR/od4JFn901Rk8cBMZAw=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=ilVNZwSQ2dtxk6rFsP81JAQMyxainu+QrlKN+xjb0ksNQFJhWe634x1uAb7pUDkCtD9X/deNDoD6cTunfHkZ6kUJXG7qIBy9LEb6jr4YSJZ4zldDRZpRwDbYlWc5DAiRCCYSIhknosHJ7j2aIcL1Lrir9A798pHShaCnCgSexZw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com; spf=pass smtp.mailfrom=huawei.com; arc=none smtp.client-ip=45.249.212.187 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huawei.com Received: from mail.maildlp.com (unknown [172.19.163.174]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4c5h390yk0z13NF3; Tue, 19 Aug 2025 15:26:29 +0800 (CST) Received: from kwepemh100008.china.huawei.com (unknown [7.202.181.93]) by mail.maildlp.com (Postfix) with ESMTPS id F1917140158; Tue, 19 Aug 2025 15:30:00 +0800 (CST) Received: from localhost.localdomain (10.50.165.33) by kwepemh100008.china.huawei.com (7.202.181.93) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.11; Tue, 19 Aug 2025 15:30:00 +0800 From: Lifeng Zheng To: , , , , , CC: , , , , , , , , , , , , Subject: [PATCH v5 2/3] cpufreq: Add a new function to get cpufreq policy without checking if the CPU is online Date: Tue, 19 Aug 2025 15:29:30 +0800 Message-ID: <20250819072931.1647431-3-zhenglifeng1@huawei.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20250819072931.1647431-1-zhenglifeng1@huawei.com> References: <20250819072931.1647431-1-zhenglifeng1@huawei.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: kwepems200002.china.huawei.com (7.221.188.68) To kwepemh100008.china.huawei.com (7.202.181.93) Content-Type: text/plain; charset="utf-8" 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 Reviewed-by: Jie Zhan Reviewed-by: Rafael J. Wysocki (Intel) --- drivers/cpufreq/cpufreq.c | 6 ++++++ include/linux/cpufreq.h | 5 +++++ 2 files changed, 11 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 in= t cpu) } EXPORT_SYMBOL_GPL(cpufreq_cpu_get_raw); =20 +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 =3D cpufreq_cpu_get_raw(cpu); diff --git a/include/linux/cpufreq.h b/include/linux/cpufreq.h index 95f3807c8c55..26b3c3310d5b 100644 --- a/include/linux/cpufreq.h +++ b/include/linux/cpufreq.h @@ -205,6 +205,7 @@ struct cpufreq_freqs { =20 #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 @@ -212,6 +213,10 @@ static inline struct cpufreq_policy *cpufreq_cpu_get_r= aw(unsigned int cpu) { return NULL; } +static inline struct cpufreq_policy *cpufreq_cpu_policy(unsigned int cpu) +{ + return NULL; +} static inline struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu) { return NULL; --=20 2.33.0 From nobody Sat Oct 4 08:06:48 2025 Received: from szxga03-in.huawei.com (szxga03-in.huawei.com [45.249.212.189]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C97F32848BF; Tue, 19 Aug 2025 07:30:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=45.249.212.189 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755588612; cv=none; b=nTEqZ8o8uDehah5v31kJByW64mwi628wQJbIzD9NxXMbmwMmzNs96Ku6E13iniYKujecBQvTCvK5cTEPkA7ocTcpU5xdC3U//xhjNaWf9KkZfIXz4XWBGYGshPP8WsFYzEPNJa9f2T0btfswgTuZE69X2cJNeXgHM0dHG0y/uyE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755588612; c=relaxed/simple; bh=MdGDFOp63pOxhwYv/QqVxRQ/ITwQrV9I93hd7KuGVNs=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=iqFti2/oKd6nF3jJDnoizz//8lRsA/Ur9Gk2737wf2JEltLJHWj9Dok2zmfjl9Q3f9DsDwcmS+9HtMjvMab6MNX92h3LRQoljwdfo+qF023OA6iEn+F0F6dvxYok4OwvYAapadd8XXFi37VTQTyvC/NLTDy+MNhzlMhp3FcDviA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com; spf=pass smtp.mailfrom=huawei.com; arc=none smtp.client-ip=45.249.212.189 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huawei.com Received: from mail.maildlp.com (unknown [172.19.88.194]) by szxga03-in.huawei.com (SkyGuard) with ESMTP id 4c5h2C0TtbzdcPD; Tue, 19 Aug 2025 15:25:39 +0800 (CST) Received: from kwepemh100008.china.huawei.com (unknown [7.202.181.93]) by mail.maildlp.com (Postfix) with ESMTPS id 8E515140275; Tue, 19 Aug 2025 15:30:01 +0800 (CST) Received: from localhost.localdomain (10.50.165.33) by kwepemh100008.china.huawei.com (7.202.181.93) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.11; Tue, 19 Aug 2025 15:30:00 +0800 From: Lifeng Zheng To: , , , , , CC: , , , , , , , , , , , , Subject: [PATCH v5 3/3] arm64: topology: Setup AMU FIE for online CPUs only Date: Tue, 19 Aug 2025 15:29:31 +0800 Message-ID: <20250819072931.1647431-4-zhenglifeng1@huawei.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20250819072931.1647431-1-zhenglifeng1@huawei.com> References: <20250819072931.1647431-1-zhenglifeng1@huawei.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: kwepems200002.china.huawei.com (7.221.188.68) To kwepemh100008.china.huawei.com (7.202.181.93) Content-Type: text/plain; charset="utf-8" When boot with maxcpu=3D1 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 --- 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 =3D data; =20 if (val =3D=3D CPUFREQ_CREATE_POLICY) - amu_fie_setup(policy->related_cpus); + amu_fie_setup(policy->cpus); =20 /* * 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 = =3D { .notifier_call =3D init_amu_fie_callback, }; =20 +static int cpuhp_topology_online(unsigned int cpu) +{ + struct cpufreq_policy *policy =3D 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 =3D cpufreq_register_notifier(&init_amu_fie_notifier, CPUFREQ_POLICY_NOTIFIER); + if (ret) + return ret; + + ret =3D 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); =20 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, =20 cpufreq_notify_post_transition(policy, freqs, transition_failed); =20 - arch_set_freq_scale(policy->related_cpus, + arch_set_freq_scale(policy->cpus, policy->cur, arch_scale_freq_ref(policy->cpu)); =20 @@ -2219,7 +2219,7 @@ unsigned int cpufreq_driver_fast_switch(struct cpufre= q_policy *policy, return 0; =20 policy->cur =3D 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); =20 --=20 2.33.0