From nobody Sun Feb 8 23:42:48 2026 Received: from canpmsgout08.his.huawei.com (canpmsgout08.his.huawei.com [113.46.200.223]) (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 D38E030AAB8 for ; Tue, 4 Nov 2025 07:55:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=113.46.200.223 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762242951; cv=none; b=qxeuvffHUkWKLkvsd/6Gc64jPC53J4muI/7OHcC5xGlTqnUueM/vWd1XqPAcVW6U6w3vbRzoziRHLj93GuI0gjngRGN/49AoUze2rvNal2EBgRRc6VCbYxaF1B1mT8ltXFa4C1X8Nns1t62+rWTc5j1F52oUeBk33yMyMvAfsGo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762242951; c=relaxed/simple; bh=LeOCGfywwihtrz0pnZKuzKShPA8snZlvMlYADmne93A=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=UwI7b8begDwFmgcisOsBvYQesbOU2s8yOTiuWeTD/GVjmX9MmFvMNd49ceKsNoTDQ3+e6riYPg1dVBPlXXNgiKxiqPZ8addoJ8mO/57oqp5LSOtcX+0vnmwF4Tyb+EeJkcqDIkoBETma5D/qX1fAw4xs1ZGj/Wx0OKEcffgh7FE= 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; dkim=pass (1024-bit key) header.d=huawei.com header.i=@huawei.com header.b=hENdKQ21; arc=none smtp.client-ip=113.46.200.223 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=huawei.com header.i=@huawei.com header.b="hENdKQ21" dkim-signature: v=1; a=rsa-sha256; d=huawei.com; s=dkim; c=relaxed/relaxed; q=dns/txt; h=From; bh=cidHgw/xNGXgeS5+lrmSBrk6DyPCnvca9eh+9hpl+R8=; b=hENdKQ21rju/zb8MhykSFu6NqS1dTitxvcgSSFh+6qRW46PkB6uVMoqpkV0UatHtAVtprwMXc M3X0r4upH4dyMSQVCI8u2AM5d/FtlpGDReGiB0SX/XXsy43t+xgaFwhnctwVSvTuMHKSVcliS0c dPI2sWcUC0K4VAysceTtyNg= Received: from mail.maildlp.com (unknown [172.19.163.44]) by canpmsgout08.his.huawei.com (SkyGuard) with ESMTPS id 4d111b5RD9zmV8q; Tue, 4 Nov 2025 15:54:11 +0800 (CST) Received: from kwepemk500012.china.huawei.com (unknown [7.202.194.97]) by mail.maildlp.com (Postfix) with ESMTPS id 77B75140158; Tue, 4 Nov 2025 15:55:46 +0800 (CST) Received: from localhost.localdomain (10.50.163.32) by kwepemk500012.china.huawei.com (7.202.194.97) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.11; Tue, 4 Nov 2025 15:55:45 +0800 From: Bowen Yu To: , , , , , , , CC: , , , , , , Subject: [PATCH 1/3] arm64: topology: Improve AMU-based frequency calculation Date: Tue, 4 Nov 2025 15:55:42 +0800 Message-ID: <20251104075544.3243606-2-yubowen8@huawei.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20251104075544.3243606-1-yubowen8@huawei.com> References: <20251104075544.3243606-1-yubowen8@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: kwepems100002.china.huawei.com (7.221.188.206) To kwepemk500012.china.huawei.com (7.202.194.97) Content-Type: text/plain; charset="utf-8" The current approach of reverse-calculating CPU frequency from capacity values introduces quantization errors due to intermediate scaling of arch_scale_freq_capacity, which results in the calculated frequency having only 1/1024 resolution. This patch: 1. Directly computes frequency using AMU counters in amu_scale_freq_tick(): freq =3D (core_cycles_delta * timer_freq) / (const_cycles_delta * 1000) - core_cycles_delta: Measured CPU cycles - timer_freq: Architectural timer frequency - const_cycles_delta: Reference cycles from fixed-frequency timer 2. Returns pre-computed avgfreq in arch_freq_get_on_cpu() examples: Before change [root@localhost ~]# cat /sys/devices/system/cpu/cpufreq/policy*/cpuinfo_avg= _freq 2297851 2297851 2295312 2297851 2297851 2295312 2297851 2295312 2297851 2297851 2297851 2295312 2295312 2297851 2297851 2297851 2297851 2300390 2297851 2297851 2297851 After change [root@localhost ~]# cat /sys/devices/system/cpu/cpufreq/policy*/cpuinfo_avg= _freq 2299177 2298117 2299188 2297330 2296530 2298817 2298434 2298986 2298596 2299395 2299560 2298446 2299108 2299294 2298707 2298453 2298632 2299218 2297962 Signed-off-by: Bowen Yu --- arch/arm64/kernel/topology.c | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/arch/arm64/kernel/topology.c b/arch/arm64/kernel/topology.c index 5d07ee85bdae..c0dbc27289ea 100644 --- a/arch/arm64/kernel/topology.c +++ b/arch/arm64/kernel/topology.c @@ -20,6 +20,7 @@ #include #include #include +#include =20 #include #include @@ -144,6 +145,8 @@ int __init parse_acpi_topology(void) */ static DEFINE_PER_CPU_READ_MOSTLY(unsigned long, arch_max_freq_scale) =3D = 1UL << (2 * SCHED_CAPACITY_SHIFT); static cpumask_var_t amu_fie_cpus; +static DEFINE_PER_CPU(unsigned long, core_delta); +static DEFINE_PER_CPU(unsigned long, const_delta); =20 struct amu_cntr_sample { u64 arch_const_cycles_prev; @@ -246,6 +249,7 @@ static void amu_scale_freq_tick(void) * arch_max_freq_scale and the use of SCHED_CAPACITY_SHIFT. */ scale =3D core_cnt - prev_core_cnt; + this_cpu_write(core_delta, scale); scale *=3D this_cpu_read(arch_max_freq_scale); scale =3D div64_u64(scale >> SCHED_CAPACITY_SHIFT, const_cnt - prev_const_cnt); @@ -253,6 +257,7 @@ static void amu_scale_freq_tick(void) scale =3D min_t(unsigned long, scale, SCHED_CAPACITY_SCALE); this_cpu_write(arch_freq_scale, (unsigned long)scale); =20 + this_cpu_write(const_delta, const_cnt - prev_const_cnt); amu_sample->last_scale_update =3D jiffies; } =20 @@ -288,7 +293,7 @@ int arch_freq_get_on_cpu(int cpu) unsigned int start_cpu =3D cpu; unsigned long last_update; unsigned int freq =3D 0; - u64 scale; + u64 delta_core_kHz; =20 if (!amu_fie_cpu_supported(cpu) || !arch_scale_freq_ref(cpu)) return -EOPNOTSUPP; @@ -340,14 +345,11 @@ int arch_freq_get_on_cpu(int cpu) break; } } - /* - * Reversed computation to the one used to determine - * the arch_freq_scale value - * (see amu_scale_freq_tick for details) - */ - scale =3D arch_scale_freq_capacity(cpu); - freq =3D scale * arch_scale_freq_ref(cpu); - freq >>=3D SCHED_CAPACITY_SHIFT; + + if (check_mul_overflow(per_cpu(core_delta, cpu), arch_timer_get_cntfrq(),= &delta_core_kHz)) + return -EINVAL; + + freq =3D div_u64(delta_core_kHz, per_cpu(const_delta, cpu) * HZ_PER_KHZ); return freq; } =20 --=20 2.33.0 From nobody Sun Feb 8 23:42:48 2026 Received: from canpmsgout09.his.huawei.com (canpmsgout09.his.huawei.com [113.46.200.224]) (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 341B930FC3E for ; Tue, 4 Nov 2025 07:55:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=113.46.200.224 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762242951; cv=none; b=pTubfXs/SKM6CLKPaNfjwzeFI6brSoiGF/VklARWPx7gzHckbKZ52kdXhxvWbtqae4+Eb+Px5j9FEvVt5NY2HnQTrn5dtOa0Kegv76HmOLtUrGaBtIjunk1NzxXGmBQ0WRz3s8WBIFp8xs0PYWCy8wwKlvNMEAMLgGxmrkQJ/tY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762242951; c=relaxed/simple; bh=DDcZE0+BW/R3G5J4jP3uwnZcXOu76HW9eD5SdfvX2uE=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=iQhMsaK+l4uD6GJlnoWOWOL+GxszWtSKY1NQ4dwnfe++nGIJ4P+WpWuQ6/XEcG95y/F2MIcpFLnFES3LLsiaGLtX5tJBq+VfB4YEEHGSPaJ+u+skdz2A715oEcvFMrSz8Ae/rRcJUH4aPGUIJe4aO88osjgzJOwKq8Vk0GZ5BWQ= 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; dkim=pass (1024-bit key) header.d=huawei.com header.i=@huawei.com header.b=xw+/AePo; arc=none smtp.client-ip=113.46.200.224 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=huawei.com header.i=@huawei.com header.b="xw+/AePo" dkim-signature: v=1; a=rsa-sha256; d=huawei.com; s=dkim; c=relaxed/relaxed; q=dns/txt; h=From; bh=h0Cz8r26uiUEfgHyGdbGpMReEJmd9fW3ZKjrNjIlhTI=; b=xw+/AePoCAV2kjUx8a71rOuNkh59Pv1Ifc+IRZntxldEiRKLR8neCRtojLlPKyMKkGYl1CBa7 2IRwRD5wnA4eJGozEH6oxY7gmAQ5ZkRbGWzEl3WSOHF9ERF77hfbS2zpPL0dZFRn55pBSyVk96V un6Tsfk7a1WcLmtirwq0L7o= Received: from mail.maildlp.com (unknown [172.19.88.234]) by canpmsgout09.his.huawei.com (SkyGuard) with ESMTPS id 4d111c0MTSz1cyV7; Tue, 4 Nov 2025 15:54:12 +0800 (CST) Received: from kwepemk500012.china.huawei.com (unknown [7.202.194.97]) by mail.maildlp.com (Postfix) with ESMTPS id EE1211402C3; Tue, 4 Nov 2025 15:55:46 +0800 (CST) Received: from localhost.localdomain (10.50.163.32) by kwepemk500012.china.huawei.com (7.202.194.97) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.11; Tue, 4 Nov 2025 15:55:46 +0800 From: Bowen Yu To: , , , , , , , CC: , , , , , , Subject: [PATCH 2/3] arm64: topology: Use current freq in governor for idle cpus in cpuinfo_avg_freq Date: Tue, 4 Nov 2025 15:55:43 +0800 Message-ID: <20251104075544.3243606-3-yubowen8@huawei.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20251104075544.3243606-1-yubowen8@huawei.com> References: <20251104075544.3243606-1-yubowen8@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: kwepems100002.china.huawei.com (7.221.188.206) To kwepemk500012.china.huawei.com (7.202.194.97) Content-Type: text/plain; charset="utf-8" The current cpuinfo_avg_freq interface returns an error when all CPUs under a policy are idle, which is relatively common. To address this, it is better to use the current frequency stored in the governor. This implementation is also used on x86 architecture. Since the current frequency in the governor is the last known frequency, it should be more user-friendly. This patch also removes redundant !housekeeping_cpu() check since it is inherently done when checking jiffies. Original output when all cpus under a policy are idle: [root@localhost home]# cat /sys/devices/system/cpu/cpufreq/policy0/ cpuinfo_avg_freq cat: /sys/devices/system/cpu/cpufreq/policy0/cpuinfo_avg_freq: Resource temporarily unavailable Output after changes: [root@localhost home]# cat /sys/devices/system/cpu/cpufreq/policy0 /cpuinfo_avg_freq 1200000 Signed-off-by: Bowen Yu --- arch/arm64/kernel/topology.c | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/arch/arm64/kernel/topology.c b/arch/arm64/kernel/topology.c index c0dbc27289ea..f1370a4a4df9 100644 --- a/arch/arm64/kernel/topology.c +++ b/arch/arm64/kernel/topology.c @@ -333,14 +333,13 @@ int arch_freq_get_on_cpu(int cpu) if (!idle_cpu(ref_cpu)) break; } + + if (ref_cpu >=3D nr_cpu_ids) { + cpufreq_cpu_put(policy); + return cpufreq_quick_get(start_cpu); + } =20 cpufreq_cpu_put(policy); - - if (ref_cpu >=3D nr_cpu_ids) - /* No alternative to pull info from */ - return -EAGAIN; - - cpu =3D ref_cpu; } else { break; } --=20 2.33.0 From nobody Sun Feb 8 23:42:48 2026 Received: from canpmsgout04.his.huawei.com (canpmsgout04.his.huawei.com [113.46.200.219]) (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 EB5D630FF06 for ; Tue, 4 Nov 2025 07:55:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=113.46.200.219 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762242952; cv=none; b=ASImKpE/t9N07bJQt0LUXBv2wVwxUB8AzWOilIZuI+bC0K5Isu12wWnjlbjEtPt05pEZdVLzwdFy6gNeDPIg2aJk0oxVEJFpzn8OYQDIVZybHW2xuOZAqpSS6PlBbHKg0n/ZV2kE7nr/Rdtil6z6V+Mh6+KyfcaA2/ytKbUVJqo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762242952; c=relaxed/simple; bh=GXyfCYePFPYrVDG2hlmggVwu4LLFR9AXyvTD7zhjKgM=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=pqHZvaElkxWC1IIAR2alcMKF5sVqLXmYyj9h1p9KFAdeQVjZGnJT+qnYzUwlPESnKj8vVlcUxfqLz+CpkLHRLdp7C3lK+YNaLgIXrbvwvpryWs6aaPeZzLkxYwsSKmRmPHof6WXw8p4dtrD+WKGRSMauNLroiJmIyYFZZCzD0HY= 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; dkim=pass (1024-bit key) header.d=huawei.com header.i=@huawei.com header.b=uw1k6Ufc; arc=none smtp.client-ip=113.46.200.219 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=huawei.com header.i=@huawei.com header.b="uw1k6Ufc" dkim-signature: v=1; a=rsa-sha256; d=huawei.com; s=dkim; c=relaxed/relaxed; q=dns/txt; h=From; bh=uOZYkNr7I7kzYLHzotIZXC0+std2hhngeKFpxYz3X8A=; b=uw1k6UfcxwHujPA5dNgdzZMMlP2fBu1GS9CVCNRspIRcsl0LRiHurN/5lWPiON3Ry49oQqsoW 6yyKFnf5QB84WsYvaJNk4dUIdrTcfvrY3lsQkoEhbwapwmblu+taf1bjkiITZ11l6IoLyzpSon6 yJk83TiqcB9NXNQ4LF5W3KI= Received: from mail.maildlp.com (unknown [172.19.162.254]) by canpmsgout04.his.huawei.com (SkyGuard) with ESMTPS id 4d111c27Yvz1prKB; Tue, 4 Nov 2025 15:54:12 +0800 (CST) Received: from kwepemk500012.china.huawei.com (unknown [7.202.194.97]) by mail.maildlp.com (Postfix) with ESMTPS id 73392180493; Tue, 4 Nov 2025 15:55:47 +0800 (CST) Received: from localhost.localdomain (10.50.163.32) by kwepemk500012.china.huawei.com (7.202.194.97) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.11; Tue, 4 Nov 2025 15:55:46 +0800 From: Bowen Yu To: , , , , , , , CC: , , , , , , Subject: [PATCH 3/3] arm64: topology: Remove redundant housekeeping_cpu() checks in arch_freq_get_on_cpu Date: Tue, 4 Nov 2025 15:55:44 +0800 Message-ID: <20251104075544.3243606-4-yubowen8@huawei.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20251104075544.3243606-1-yubowen8@huawei.com> References: <20251104075544.3243606-1-yubowen8@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: kwepems100002.china.huawei.com (7.221.188.206) To kwepemk500012.china.huawei.com (7.202.194.97) Content-Type: text/plain; charset="utf-8" This patch removes redundant !housekeeping_cpu() check since it is inherently done when checking jiffies. Signed-off-by: Bowen Yu --- arch/arm64/kernel/topology.c | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/arch/arm64/kernel/topology.c b/arch/arm64/kernel/topology.c index f1370a4a4df9..6981ef3019d3 100644 --- a/arch/arm64/kernel/topology.c +++ b/arch/arm64/kernel/topology.c @@ -310,20 +310,13 @@ int arch_freq_get_on_cpu(int cpu) * (and thus freq scale), if available, for given policy: this boils * down to identifying an active cpu within the same freq domain, if any. */ - if (!housekeeping_cpu(cpu, HK_TYPE_TICK) || - time_is_before_jiffies(last_update + msecs_to_jiffies(AMU_SAMPLE_EXP= _MS))) { + if (time_is_before_jiffies(last_update + msecs_to_jiffies(AMU_SAMPLE_EXP= _MS))) { struct cpufreq_policy *policy =3D cpufreq_cpu_get(cpu); int ref_cpu; =20 if (!policy) return -EINVAL; =20 - if (!cpumask_intersects(policy->related_cpus, - housekeeping_cpumask(HK_TYPE_TICK))) { - cpufreq_cpu_put(policy); - return -EOPNOTSUPP; - } - for_each_cpu_wrap(ref_cpu, policy->cpus, cpu + 1) { if (ref_cpu =3D=3D start_cpu) { /* Prevent verifying same CPU twice */ --=20 2.33.0