From nobody Fri Sep 25 05:30:16 2026 Received: from va-1-112.ptr.blmpb.com (va-1-112.ptr.blmpb.com [209.127.230.112]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 18EFB51FCBE for ; Wed, 16 Sep 2026 14:27:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.127.230.112 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789568829; cv=none; b=UzRFzhaUIK6qC1ty6dgaVhZlaVIx5zhF3IMVn2c2l61NVACf11Pnj38pB4Om1Yb1aen1GyaM2Ke7q1LaPI7Db6z4aDk2nTSV+u6tc9l1tO87snrEtVG4ByKkapWCXrNYlCI3JeEGfiBqdHeAhk88oPifnuNvycWfIC1rSCw3tk4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789568829; c=relaxed/simple; bh=5gx3hFy8w0hJVCf6w05eXatJkfbVEQiR6oApXJvvce8=; h=Message-Id:Cc:Mime-Version:Content-Type:To:Date:References: In-Reply-To:From:Subject; b=bNArdxRzy8Lm83rELyYxhATa8/B0rA5TRUOjcCtgSMErdeLuYEliSBZyEBaS4NJbfmmefc2EVpzLFwoAYMvgVczP9yg1ufE2+2JXhCXgtrO4nVPOmOnjLlI29TviYPi1yMfTnMB6y6XYodGOoRa+DYFEUlpa3YWg5ewFYcwpy5s= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=bytedance.com; spf=pass smtp.mailfrom=bytedance.com; dkim=pass (2048-bit key) header.d=bytedance.com header.i=@bytedance.com header.b=QOo3YAg9; arc=none smtp.client-ip=209.127.230.112 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=bytedance.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bytedance.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bytedance.com header.i=@bytedance.com header.b="QOo3YAg9" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; s=2212171451; d=bytedance.com; t=1789568816; h=from:subject: mime-version:from:date:message-id:subject:to:cc:reply-to:content-type: mime-version:in-reply-to:message-id; bh=tudkbI/Wzi+fx+PxY1Tz+PO+3o83pqkwPsAyJdML/jU=; b=QOo3YAg9pd3gCVNj5U1y+HjZNJFRhlxdXMEcjD0QWbte5vqan78PgFrulZGAMZGuFsGJ5v K/xSo5T0EDyNnvXEaftM/j+hkwix6GrdPcuv97eGxLe/BQSJObbmYdRYM9khEHigY303ZV g6aP0zdZsCEJErBwLTw95r6onJCN5Y3AILcsZ4ARpEZB74la1hjNUUj+OjhanD9VmdSpk7 jplN93hv9VqETTog3c0rDbFZ6bMEiXxN1R9Qrb2pB7Ft1nMAwdWXke5lVinBXBdEqpD1Be m3IvShozKl2y90VfQKV5ORnfi+h9aJnuReDjexC0zjrvfsBcdGDJsM04T6c/Fw== Message-Id: <20260916142623.359336-2-zhouchuyi@bytedance.com> X-Mailer: git-send-email 2.20.1 Cc: , "Chuyi Zhou" Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 X-Lms-Return-Path: To: , , , , , , , , Date: Wed, 16 Sep 2026 22:26:21 +0800 References: <20260916142623.359336-1-zhouchuyi@bytedance.com> X-Original-From: Chuyi Zhou In-Reply-To: <20260916142623.359336-1-zhouchuyi@bytedance.com> From: "Chuyi Zhou" Subject: [PATCH 1/3] x86/aperfmperf: Separate hardware feedback from fallback reporting Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" arch_freq_get_on_cpu() reads cached APERF/MPERF samples and falls back to cpufreq_quick_get() or cpu_khz when no usable sample is available. Its callers cannot distinguish that fallback from a measured frequency. This prevents a hardware feedback consumer from reporting missing samples while preserving the fallback used by existing interfaces. Extract the sample lookup and calculation into arch_freq_get_avg(). Return -EOPNOTSUPP when APERF/MPERF is unavailable and -EAGAIN when the sample is too old or has a zero MPERF delta. Keep the existing seqcount read, age limit and frequency calculation. No functional change intended. This prepares cpuinfo_avg_freq to use hardware feedback without fallback values. Signed-off-by: Chuyi Zhou --- arch/x86/kernel/cpu/aperfmperf.c | 25 ++++++++++++++++++++----- include/linux/cpufreq.h | 1 + 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/arch/x86/kernel/cpu/aperfmperf.c b/arch/x86/kernel/cpu/aperfmp= erf.c index 7ffc78d5ebf2..de6297b1284b 100644 --- a/arch/x86/kernel/cpu/aperfmperf.c +++ b/arch/x86/kernel/cpu/aperfmperf.c @@ -503,15 +503,22 @@ void arch_scale_freq_tick(void) */ #define MAX_SAMPLE_AGE ((unsigned long)HZ / 50) =20 -int arch_freq_get_on_cpu(int cpu) +/** + * arch_freq_get_avg() - Read cached APERF/MPERF frequency feedback + * @cpu: CPU to read. + * + * Return: Frequency in kHz, -EAGAIN if no usable sample is available, or + * -EOPNOTSUPP if APERF/MPERF is unsupported. + */ +int arch_freq_get_avg(int cpu) { struct aperfmperf *s =3D per_cpu_ptr(&cpu_samples, cpu); - unsigned int seq, freq; unsigned long last; + unsigned int seq; u64 acnt, mcnt; =20 if (!cpu_feature_enabled(X86_FEATURE_APERFMPERF)) - goto fallback; + return -EOPNOTSUPP; =20 do { seq =3D raw_read_seqcount_begin(&s->seq); @@ -525,11 +532,19 @@ int arch_freq_get_on_cpu(int cpu) * which covers idle and NOHZ full CPUs. */ if (!mcnt || (jiffies - last) > MAX_SAMPLE_AGE) - goto fallback; + return -EAGAIN; =20 return div64_u64((cpu_khz * acnt), mcnt); +} + +int arch_freq_get_on_cpu(int cpu) +{ + int freq =3D arch_freq_get_avg(cpu); + + if (freq >=3D 0) + return freq; =20 -fallback: + /* Preserve the fallback used by scaling_cur_freq and /proc/cpuinfo. */ freq =3D cpufreq_quick_get(cpu); return freq ? freq : cpu_khz; } diff --git a/include/linux/cpufreq.h b/include/linux/cpufreq.h index 35ce665edfd8..dab1d921183d 100644 --- a/include/linux/cpufreq.h +++ b/include/linux/cpufreq.h @@ -1224,6 +1224,7 @@ static inline int of_perf_domain_get_sharing_cpumask(= int pcpu, const char *list_ } #endif =20 +extern int arch_freq_get_avg(int cpu); extern int arch_freq_get_on_cpu(int cpu); =20 #ifndef arch_set_freq_scale --=20 2.20.1 From nobody Fri Sep 25 05:30:16 2026 Received: from va-1-111.ptr.blmpb.com (va-1-111.ptr.blmpb.com [209.127.230.111]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 332AE538D94 for ; Wed, 16 Sep 2026 14:27:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.127.230.111 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789568841; cv=none; b=S0gTlpfQW/m3RYhj8+SPKSf2RbEOREqe/hwfeLNjCd9jCMIkhdqSg8sltHDv3MsLW8vEL9lR6HlNV2QsUUC6Td2PDyz4M6J+QlXPszpTrlAmgrAr6BfTDr1qmiRYdW4UBijTllIa+5Lxk2Ja5l3wMDCc8XMvYvr0JsD8HpI88Ds= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789568841; c=relaxed/simple; bh=GaTQ9iTfa2fiJ8vtF/8FEnHCAP5RxQ/mXmTN4K28/Ts=; h=From:Date:Message-Id:To:References:In-Reply-To:Mime-Version: Content-Type:Cc:Subject; b=Qvj0K538zDfhmYMOJw53p59CfPzc8IrMxkfnuDJjIWPg/6rMmwPT6YIs+42MQYEs9K50ExMUSEo2zlQHpg8FIOFAFhD1SHDNp48HWU0bV5ZIIApyBcDUj8747/NNeIKQINR6Jp7Fu+Exx+cfXlO04qZyW6xFyZjyRvXRzn5gWkY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=bytedance.com; spf=pass smtp.mailfrom=bytedance.com; dkim=pass (2048-bit key) header.d=bytedance.com header.i=@bytedance.com header.b=JAK5lPm2; arc=none smtp.client-ip=209.127.230.111 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=bytedance.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bytedance.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bytedance.com header.i=@bytedance.com header.b="JAK5lPm2" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; s=2212171451; d=bytedance.com; t=1789568828; h=from:subject: mime-version:from:date:message-id:subject:to:cc:reply-to:content-type: mime-version:in-reply-to:message-id; bh=YarYGRxch6A9B+7ga7PZoJ3GuLBezPPbSeknKvqJqAo=; b=JAK5lPm2qfyR26bEhkJuW/SrmCU0rj4ZHWeyBPrhxdolKXJ7nv0OfAW4HKQNDDQRJ6YNGx XgGgDMciFFRsZ/Sgrq6LolJyGt5OiCDMz2J4JDQl7bhwtaoMPJHANGQ+yrOk1YJP6UDSEe j7TM3WZDK6c4RLX6hyrEbnb7aMT6xjLAFrP2+AohNKQDy7SW8fHnm3UKTAPSLPioBFLtzI WZOL3qyjQBd6bqzTWe2459TmppweSEnDG8wXiQDxXtbLSaZr8WmpNMDj7vepGXvU0lOUTF +6NXDBgzqoz1SKu02aZqL9o2sce9yYZw79//T1bLOzrKJ448FITqi6wMBDIV+g== From: "Chuyi Zhou" Date: Wed, 16 Sep 2026 22:26:22 +0800 Message-Id: <20260916142623.359336-3-zhouchuyi@bytedance.com> Content-Transfer-Encoding: quoted-printable To: , , , , , , , , References: <20260916142623.359336-1-zhouchuyi@bytedance.com> In-Reply-To: <20260916142623.359336-1-zhouchuyi@bytedance.com> X-Lms-Return-Path: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 X-Mailer: git-send-email 2.20.1 Cc: , "Chuyi Zhou" Subject: [PATCH 2/3] cpufreq: Use hardware feedback for cpuinfo_avg_freq X-Original-From: Chuyi Zhou Content-Type: text/plain; charset="utf-8" cpuinfo_avg_freq is documented as a frequency derived from hardware feedback, but it uses arch_freq_get_on_cpu(), which supplies a fallback on x86 when the cached sample is unavailable. Userspace cannot distinguish that value from a measurement. With amd-pstate-epp, for example, the fallback is policy->min even under the performance policy. A fallback based on the requested frequency also does not establish the frequency at which the hardware actually ran. Use arch_freq_get_avg() for cpuinfo_avg_freq reads and support detection. On x86, unavailable samples then return -EAGAIN instead of a fallback frequency, and the attribute is omitted when APERF/MPERF is unsupported. Provide a weak default returning -EOPNOTSUPP and reuse the existing ARM64 AMU implementation. Preserve the behavior of ARM64, scaling_cur_freq and /proc/cpuinfo. Signed-off-by: Chuyi Zhou --- Documentation/admin-guide/pm/cpufreq.rst | 11 +++++++++-- arch/arm64/kernel/topology.c | 7 ++++++- drivers/cpufreq/cpufreq.c | 19 +++++++++++++++++-- 3 files changed, 32 insertions(+), 5 deletions(-) diff --git a/Documentation/admin-guide/pm/cpufreq.rst b/Documentation/admin= -guide/pm/cpufreq.rst index 34baf20cc202..3057829b01bd 100644 --- a/Documentation/admin-guide/pm/cpufreq.rst +++ b/Documentation/admin-guide/pm/cpufreq.rst @@ -255,12 +255,19 @@ are the following: =20 This is expected to be based on the frequency the hardware actuall= y runs at and, as such, might require specialised hardware support (such = as AMU - extension on ARM). If one cannot be determined, this attribute sho= uld - not be present. + extension on ARM or APERF/MPERF on x86). This attribute is not pre= sent + when hardware feedback is unsupported. =20 Note that failed attempt to retrieve current frequency for a given CPU(s) will result in an appropriate error, i.e.: EAGAIN for CPU t= hat remains idle (raised on ARM). + The attribute remains present during temporary sampling gaps. + + On x86, reads use cached APERF/MPERF samples without waking the ta= rget + CPU to collect new samples. An expired sample or a zero MPERF delta + results in ``EAGAIN`` instead of a fallback to a policy or referen= ce + frequency. A CPU that has just entered idle can still have a usable + sample, while a busy CPU excluded from periodic sampling can lack = one. =20 ``cpuinfo_max_freq`` Maximum possible operating frequency the CPUs belonging to this policy diff --git a/arch/arm64/kernel/topology.c b/arch/arm64/kernel/topology.c index d28438f8b83f..939d3e4ce5d2 100644 --- a/arch/arm64/kernel/topology.c +++ b/arch/arm64/kernel/topology.c @@ -181,7 +181,7 @@ void arch_cpu_idle_enter(void) =20 #define AMU_SAMPLE_EXP_MS 20 =20 -int arch_freq_get_on_cpu(int cpu) +int arch_freq_get_avg(int cpu) { struct amu_cntr_sample *amu_sample; unsigned int start_cpu =3D cpu; @@ -250,6 +250,11 @@ int arch_freq_get_on_cpu(int cpu) return freq; } =20 +int arch_freq_get_on_cpu(int cpu) +{ + return arch_freq_get_avg(cpu); +} + static void amu_fie_setup(const struct cpumask *cpus) { int cpu; diff --git a/drivers/cpufreq/cpufreq.c b/drivers/cpufreq/cpufreq.c index 0d0df986fa3d..21bec6b7f538 100644 --- a/drivers/cpufreq/cpufreq.c +++ b/drivers/cpufreq/cpufreq.c @@ -705,9 +705,24 @@ __weak int arch_freq_get_on_cpu(int cpu) return -EOPNOTSUPP; } =20 +/** + * arch_freq_get_avg() - Get an average frequency from hardware feedback + * @cpu: CPU to read. + * + * Provide cpuinfo_avg_freq with an average operating frequency derived + * from recent hardware feedback for @cpu or its frequency domain. + * + * Return: Frequency in kHz, -EOPNOTSUPP if feedback is unsupported for the + * CPU or policy under the current configuration. + */ +__weak int arch_freq_get_avg(int cpu) +{ + return -EOPNOTSUPP; +} + static inline bool cpufreq_avg_freq_supported(struct cpufreq_policy *polic= y) { - return arch_freq_get_on_cpu(policy->cpu) !=3D -EOPNOTSUPP; + return arch_freq_get_avg(policy->cpu) !=3D -EOPNOTSUPP; } =20 static ssize_t show_scaling_cur_freq(struct cpufreq_policy *policy, char *= buf) @@ -769,7 +784,7 @@ static ssize_t show_cpuinfo_cur_freq(struct cpufreq_pol= icy *policy, static ssize_t show_cpuinfo_avg_freq(struct cpufreq_policy *policy, char *buf) { - int avg_freq =3D arch_freq_get_on_cpu(policy->cpu); + int avg_freq =3D arch_freq_get_avg(policy->cpu); =20 if (avg_freq > 0) return sysfs_emit(buf, "%u\n", avg_freq); --=20 2.20.1 From nobody Fri Sep 25 05:30:16 2026 Received: from va-1-115.ptr.blmpb.com (va-1-115.ptr.blmpb.com [209.127.230.115]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0D6A053FD40 for ; Wed, 16 Sep 2026 14:27:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.127.230.115 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789568852; cv=none; b=HFQtGiNCUo4k9bkBQbYkpHfnqL1eJFfPr3khPQr+S4QhS7CeRyzNL5c3Vs4wjggLDSEHKgAumvARGZ9jjfj5XnuGlEgkWoM/hY733PyEBuJAYtY5n7bN5nmMctnmCjtlyRsrNDGkil/t+yzSCvB0JmkVv6lgr0/4nmEs479YufQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789568852; c=relaxed/simple; bh=Z4jzHhEkvT75dgKmXLBZDSx6Kwuwt1KpudwXMyg5eEM=; h=References:To:From:Content-Type:Date:Message-Id:In-Reply-To:Cc: Subject:Mime-Version; b=WjVr2AFQANmKnv67DCg1nPDYpKkgHfrK6xu/N1FCy4sMcrGdDAci7ugfr5GZWuflKV9Zyvbom9+u3qnytY17MPVH5uesmYWJmpmJwbOq8wzoUFf6/b/lLhSeb1LqAk1wcTLW613P3rLr7WYxVWawLLNZGUULdRjeua/pyW/Z7KI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=bytedance.com; spf=pass smtp.mailfrom=bytedance.com; dkim=pass (2048-bit key) header.d=bytedance.com header.i=@bytedance.com header.b=Nr/9opxA; arc=none smtp.client-ip=209.127.230.115 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=bytedance.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bytedance.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bytedance.com header.i=@bytedance.com header.b="Nr/9opxA" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; s=2212171451; d=bytedance.com; t=1789568839; h=from:subject: mime-version:from:date:message-id:subject:to:cc:reply-to:content-type: mime-version:in-reply-to:message-id; bh=E9Oz00dh7xaWWymhv+e2YK9j5EQ5QPEt135OYIOJYtA=; b=Nr/9opxAIcMhG1dr0U10DydcrijuIx55l4IY39FCOGNwEaVQL4FwkAlyuJqVQTqq94Wstj qe0nKl8hJXnfowXboYOLn7rzjG+F1mns9p2FL+SSR1YxT5Kp2F41SZy0lmK/uEKu31aCoe bElLo+brev9Y826lDwG3YaA1mEZz3rBlOTHjyAqvFMk2ZggYukxxWw3e8kuu4wM+XUWEPr /VC1Opwex7uIjfxSZtStN59nNT94B6Lt+tKklosNdLsYO9CdA9uwWka8PW3626EQ6zofEB aGzaCPmEDJQcRrYffqxgYVJTWIKHi2ABqgK09JKRFkW2mrrOWghqyENOzzHgbw== References: <20260916142623.359336-1-zhouchuyi@bytedance.com> To: , , , , , , , , From: "Chuyi Zhou" X-Original-From: Chuyi Zhou X-Mailer: git-send-email 2.20.1 Date: Wed, 16 Sep 2026 22:26:23 +0800 Message-Id: <20260916142623.359336-4-zhouchuyi@bytedance.com> X-Lms-Return-Path: In-Reply-To: <20260916142623.359336-1-zhouchuyi@bytedance.com> Content-Transfer-Encoding: quoted-printable Cc: , "Chuyi Zhou" Subject: [PATCH 3/3] x86/aperfmperf: Clean up includes after removing IPI sampling Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 Content-Type: text/plain; charset="utf-8" Commit f3eca381bd49 ("x86/aperfmperf: Replace arch_freq_get_on_cpu()") removed the ad hoc sampling code, including its delays, ktime timestamps, RCU idle checks, housekeeping checks and remote function calls. The corresponding includes remain even though the file no longer uses those interfaces directly. Remove the obsolete includes and explicitly include the headers for jiffies, sequence counters and workqueues used by the current code. Signed-off-by: Chuyi Zhou --- arch/x86/kernel/cpu/aperfmperf.c | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/arch/x86/kernel/cpu/aperfmperf.c b/arch/x86/kernel/cpu/aperfmp= erf.c index de6297b1284b..8cf9decea07c 100644 --- a/arch/x86/kernel/cpu/aperfmperf.c +++ b/arch/x86/kernel/cpu/aperfmperf.c @@ -7,15 +7,13 @@ * Author: Len Brown */ #include -#include -#include +#include #include #include -#include -#include #include -#include +#include #include +#include =20 #include #include --=20 2.20.1