* [PATCH v2 0/7] cpufreq: amd-pstate: Fix a batch of correctness bugs
@ 2026-11-18 3:27 Mario Limonciello
2026-11-18 3:27 ` [PATCH v2 1/7] cpufreq: amd-pstate: Fix NULL dereference in amd_prefcore_param() Mario Limonciello
` (8 more replies)
0 siblings, 9 replies; 11+ messages in thread
From: Mario Limonciello @ 2026-11-18 3:27 UTC (permalink / raw)
To: K Prateek Nayak
Cc: linux-pm, Mario Limonciello, Sashiko, Mario Limonciello (AMD)
This series fixes six independent bugs in the amd-pstate cpufreq driver
and its unit test module. They were reported by automated code review
and each is fixed in its own patch:
- amd_prefcore_param() dereferenced a NULL string when "amd_prefcore"
was passed on the kernel command line without a value, panicking
early in boot.
- shmem_get_epp() applied the bits 31:24 mask to the unshifted 8-bit
value returned by cppc_get_epp_perf(), so it always returned 0 on
shared-memory CPPC systems and the firmware default EPP was lost.
- amd_pstate_init_freq() passed the quirk lowest_freq (MHz) to
freq_to_perf() together with a nominal_freq in kHz, which produced a
bogus lowest_perf.
- get_mode_idx_from_str() matched any prefix of a mode name, so writing
e.g. "undefi" to the status sysfs file was silently accepted instead
of returning -EINVAL.
- amd_pstate_ut_epp() leaked a platform profile device and left the
EPP state modified when dynamic EPP was originally disabled.
- amd_pstate_update_min_max_limit() read policy->min/max multiple times
without holding policy->rwsem, so the programmed perf limits could
disagree with the cached limit frequencies.
Signed-off-by: Mario Limonciello <mario.limonciello@amd.com>
---
Changes in v2:
- Fold in suggestions for string handling into 5/6 (Prateek)
- Pull in changes from EPP aliasing in
https://lore.kernel.org/linux-pm/20261012205559.3421297-1-mario.limonciello@amd.com/
- Link to v1: https://patch.msgid.link/20261116-superm1-linux-fabc6bcb-984e-4ba7-bb57-02cc3af08fa7-v1-0-682aefd4fed6@amd.com
---
Mario Limonciello (7):
cpufreq: amd-pstate: Fix NULL dereference in amd_prefcore_param()
cpufreq/amd-pstate: Fix EPP extraction in shmem_get_epp()
cpufreq/amd-pstate: Fix unit mismatch in amd_pstate_init_freq()
cpufreq: amd-pstate: Require exact mode string match
cpufreq/amd-pstate-ut: Restore EPP state at end of amd_pstate_ut_epp()
cpufreq/amd-pstate: Snapshot policy limits in amd_pstate_update_min_max_limit()
cpufreq: amd-pstate: Preserve names for aliased EPP values
drivers/cpufreq/amd-pstate-ut.c | 63 +++++++++++++++++++++--------------------
drivers/cpufreq/amd-pstate.c | 55 +++++++++++++++++++++++------------
drivers/cpufreq/amd-pstate.h | 4 +--
3 files changed, 71 insertions(+), 51 deletions(-)
---
base-commit: 141b164bbe186423e295b139e40e8e2d11cf1341
change-id: 20261116-superm1-linux-fabc6bcb-984e-4ba7-bb57-02cc3af08fa7-9d324aa04dd5
Best regards,
--
Mario Limonciello <mario.limonciello@amd.com>
^ permalink raw reply [flat|nested] 11+ messages in thread
* [PATCH v2 1/7] cpufreq: amd-pstate: Fix NULL dereference in amd_prefcore_param()
2026-11-18 3:27 [PATCH v2 0/7] cpufreq: amd-pstate: Fix a batch of correctness bugs Mario Limonciello
@ 2026-11-18 3:27 ` Mario Limonciello
2026-11-18 3:27 ` [PATCH v2 2/7] cpufreq/amd-pstate: Fix EPP extraction in shmem_get_epp() Mario Limonciello
` (7 subsequent siblings)
8 siblings, 0 replies; 11+ messages in thread
From: Mario Limonciello @ 2026-11-18 3:27 UTC (permalink / raw)
To: K Prateek Nayak; +Cc: linux-pm, Mario Limonciello, Sashiko
amd_prefcore_param() is registered with early_param() and passes its
argument straight to strcmp(). When "amd_prefcore" is given on the kernel
command line without a value, do_early_param() calls the handler with a
NULL string, causing a NULL pointer dereference and a panic during early
boot.
Return -EINVAL for a NULL argument, as amd_pstate_param() already does.
Fixes: f3a052391822 ("cpufreq: amd-pstate: Enable amd-pstate preferred core support")
Reported-by: Sashiko <sashiko-bot@kernel.org>
Link: https://sashiko.dev/#/bug/linux-fabc6bcb-984e-4ba7-bb57-02cc3af08fa7
Signed-off-by: Mario Limonciello <mario.limonciello@amd.com>
---
drivers/cpufreq/amd-pstate.c | 3 +++
1 file changed, 3 insertions(+)
diff --git a/drivers/cpufreq/amd-pstate.c b/drivers/cpufreq/amd-pstate.c
index 4bedfbfc18b66..eedc5fcd9c542 100644
--- a/drivers/cpufreq/amd-pstate.c
+++ b/drivers/cpufreq/amd-pstate.c
@@ -2478,6 +2478,9 @@ static int __init amd_pstate_param(char *str)
static int __init amd_prefcore_param(char *str)
{
+ if (!str)
+ return -EINVAL;
+
if (!strcmp(str, "disable"))
amd_pstate_prefcore = false;
--
2.43.0
^ permalink raw reply related [flat|nested] 11+ messages in thread
* [PATCH v2 2/7] cpufreq/amd-pstate: Fix EPP extraction in shmem_get_epp()
2026-11-18 3:27 [PATCH v2 0/7] cpufreq: amd-pstate: Fix a batch of correctness bugs Mario Limonciello
2026-11-18 3:27 ` [PATCH v2 1/7] cpufreq: amd-pstate: Fix NULL dereference in amd_prefcore_param() Mario Limonciello
@ 2026-11-18 3:27 ` Mario Limonciello
2026-11-18 3:27 ` [PATCH v2 3/7] cpufreq/amd-pstate: Fix unit mismatch in amd_pstate_init_freq() Mario Limonciello
` (6 subsequent siblings)
8 siblings, 0 replies; 11+ messages in thread
From: Mario Limonciello @ 2026-11-18 3:27 UTC (permalink / raw)
To: K Prateek Nayak; +Cc: linux-pm, Mario Limonciello, Sashiko
On shared-memory CPPC systems, cppc_get_epp_perf() returns the EPP value
unshifted in bits 7:0. shmem_get_epp() applies
FIELD_GET(AMD_CPPC_EPP_PERF_MASK, ...), which extracts bits 31:24, so it
always returns 0.
If firmware initializes the ENERGY_PERF register to a non-zero value,
amd_pstate_epp_cpu_init() then caches 0 in cpudata->cppc_req_cached. A
later shmem_set_epp() call with EPP 0 (performance) sees
epp == epp_cached, skips the write, and leaves the hardware at the
firmware-programmed EPP.
Return the 8-bit register value directly.
Fixes: 555bbe67a622 ("cpufreq/amd-pstate: Convert all perf values to u8")
Reported-by: Sashiko <sashiko-bot@kernel.org>
Link: https://sashiko.dev/#/bug/linux-bfeec24a-9e2b-4c78-bad6-a4f5dc0273ab
Signed-off-by: Mario Limonciello <mario.limonciello@amd.com>
---
drivers/cpufreq/amd-pstate.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/drivers/cpufreq/amd-pstate.c b/drivers/cpufreq/amd-pstate.c
index eedc5fcd9c542..6136437562545 100644
--- a/drivers/cpufreq/amd-pstate.c
+++ b/drivers/cpufreq/amd-pstate.c
@@ -278,7 +278,7 @@ static int shmem_get_epp(struct amd_cpudata *cpudata)
return ret;
}
- return FIELD_GET(AMD_CPPC_EPP_PERF_MASK, epp);
+ return (u8)epp;
}
static int msr_update_perf(struct cpufreq_policy *policy, u8 min_perf,
--
2.43.0
^ permalink raw reply related [flat|nested] 11+ messages in thread
* [PATCH v2 3/7] cpufreq/amd-pstate: Fix unit mismatch in amd_pstate_init_freq()
2026-11-18 3:27 [PATCH v2 0/7] cpufreq: amd-pstate: Fix a batch of correctness bugs Mario Limonciello
2026-11-18 3:27 ` [PATCH v2 1/7] cpufreq: amd-pstate: Fix NULL dereference in amd_prefcore_param() Mario Limonciello
2026-11-18 3:27 ` [PATCH v2 2/7] cpufreq/amd-pstate: Fix EPP extraction in shmem_get_epp() Mario Limonciello
@ 2026-11-18 3:27 ` Mario Limonciello
2026-11-18 3:27 ` [PATCH v2 4/7] cpufreq: amd-pstate: Require exact mode string match Mario Limonciello
` (5 subsequent siblings)
8 siblings, 0 replies; 11+ messages in thread
From: Mario Limonciello @ 2026-11-18 3:27 UTC (permalink / raw)
To: K Prateek Nayak; +Cc: linux-pm, Mario Limonciello, Sashiko
When cpufreq quirks are present, amd_pstate_init_freq() loads
quirks->lowest_freq in MHz and passes it to freq_to_perf() together with
nominal_freq, which has already been converted to kHz. The conversion of
min_freq to kHz only happens after the call.
freq_to_perf() divides the requested frequency by nominal_freq, so the
off-by-1000 mismatch yields a near-zero value that gets clamped up to 1
and overwrites perf.lowest_perf. This defeats the purpose of the lowest
frequency quirk and exposes an invalid minimum to the cpufreq core.
Convert min_freq to kHz before calling freq_to_perf() in the quirk path,
and do the same for the non-quirk path so both end up in kHz.
Fixes: 009d1c29a451 ("cpufreq/amd-pstate: Move perf values into a union")
Reported-by: Sashiko <sashiko-bot@kernel.org>
Link: https://sashiko.dev/#/bug/linux-515ed05d-eed1-45b5-82cd-41b86ca2ee88
Signed-off-by: Mario Limonciello <mario.limonciello@amd.com>
---
drivers/cpufreq/amd-pstate.c | 6 ++----
1 file changed, 2 insertions(+), 4 deletions(-)
diff --git a/drivers/cpufreq/amd-pstate.c b/drivers/cpufreq/amd-pstate.c
index 6136437562545..e73f30b6fb30f 100644
--- a/drivers/cpufreq/amd-pstate.c
+++ b/drivers/cpufreq/amd-pstate.c
@@ -1057,13 +1057,11 @@ static int amd_pstate_init_freq(struct amd_cpudata *cpudata)
nominal_freq *= 1000;
if (quirks && quirks->lowest_freq) {
- min_freq = quirks->lowest_freq;
+ min_freq = quirks->lowest_freq * 1000;
perf.lowest_perf = freq_to_perf(perf, nominal_freq, min_freq);
WRITE_ONCE(cpudata->perf, perf);
} else
- min_freq = cppc_perf.lowest_freq;
-
- min_freq *= 1000;
+ min_freq = cppc_perf.lowest_freq * 1000;
WRITE_ONCE(cpudata->nominal_freq, nominal_freq);
--
2.43.0
^ permalink raw reply related [flat|nested] 11+ messages in thread
* [PATCH v2 4/7] cpufreq: amd-pstate: Require exact mode string match
2026-11-18 3:27 [PATCH v2 0/7] cpufreq: amd-pstate: Fix a batch of correctness bugs Mario Limonciello
` (2 preceding siblings ...)
2026-11-18 3:27 ` [PATCH v2 3/7] cpufreq/amd-pstate: Fix unit mismatch in amd_pstate_init_freq() Mario Limonciello
@ 2026-11-18 3:27 ` Mario Limonciello
2026-11-18 3:27 ` [PATCH v2 5/7] cpufreq/amd-pstate-ut: Restore EPP state at end of amd_pstate_ut_epp() Mario Limonciello
` (4 subsequent siblings)
8 siblings, 0 replies; 11+ messages in thread
From: Mario Limonciello @ 2026-11-18 3:27 UTC (permalink / raw)
To: K Prateek Nayak; +Cc: linux-pm, Mario Limonciello, Sashiko
get_mode_idx_from_str() compares with strncmp() bounded by the caller's
input length, so any prefix of a mode name matches. Writing a 6 or 7
byte prefix such as "undefi", "passiv" or "disabl" to the status sysfs
file passes the size check in amd_pstate_update_status() and resolves to
a valid mode index. For "undefi" that is AMD_PSTATE_UNDEFINED, which has
no entry in mode_state_machine, so the store is silently accepted with
no state change instead of failing with -EINVAL.
Require the input length to equal the mode string length as well as
matching its contents.
Fixes: abd61c08ef34 ("cpufreq: amd-pstate: add driver working mode switch support")
Reported-by: Sashiko <sashiko-bot@kernel.org>
Link: https://sashiko.dev/#/bug/linux-f75b8476-e226-4056-92fc-96a7b92126f5
Signed-off-by: Mario Limonciello <mario.limonciello@amd.com>
---
drivers/cpufreq/amd-pstate.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/drivers/cpufreq/amd-pstate.c b/drivers/cpufreq/amd-pstate.c
index e73f30b6fb30f..574dfffc18f05 100644
--- a/drivers/cpufreq/amd-pstate.c
+++ b/drivers/cpufreq/amd-pstate.c
@@ -238,7 +238,8 @@ static inline int get_mode_idx_from_str(const char *str, size_t size)
int i;
for (i = 0; i < AMD_PSTATE_MAX; i++) {
- if (!strncmp(str, amd_pstate_mode_string[i], size))
+ if (size == strlen(amd_pstate_mode_string[i]) &&
+ !strncmp(str, amd_pstate_mode_string[i], size))
return i;
}
return -EINVAL;
--
2.43.0
^ permalink raw reply related [flat|nested] 11+ messages in thread
* [PATCH v2 5/7] cpufreq/amd-pstate-ut: Restore EPP state at end of amd_pstate_ut_epp()
2026-11-18 3:27 [PATCH v2 0/7] cpufreq: amd-pstate: Fix a batch of correctness bugs Mario Limonciello
` (3 preceding siblings ...)
2026-11-18 3:27 ` [PATCH v2 4/7] cpufreq: amd-pstate: Require exact mode string match Mario Limonciello
@ 2026-11-18 3:27 ` Mario Limonciello
2026-11-18 3:27 ` [PATCH v2 6/7] cpufreq/amd-pstate: Snapshot policy limits in amd_pstate_update_min_max_limit() Mario Limonciello
` (3 subsequent siblings)
8 siblings, 0 replies; 11+ messages in thread
From: Mario Limonciello @ 2026-11-18 3:27 UTC (permalink / raw)
To: K Prateek Nayak; +Cc: linux-pm, Mario Limonciello, Sashiko
amd_pstate_ut_epp() records whether dynamic EPP was originally enabled,
but nothing about the original static EPP, and only restores state when
dynamic EPP was originally on.
If the system started with dynamic EPP disabled, the test's "dynamic"
write allocates a platform profile device and enables dynamic EPP. The
exit path then skips restoration entirely, leaking the platform profile
device and leaving the CPU in whichever mode was written last (dynamic
on early failure, or the last tested static preference).
Capture the original EPP preference string via
show_energy_performance_preference() while holding the policy rwsem for
read, so it can't change underneath. If dynamic EPP was enabled, trim the
"(profile:...)" suffix so that "dynamic" is written back. On exit, when
the driver was originally in active mode, write the saved string back
with store_energy_performance_preference(). This restores dynamic EPP,
or a static value (tearing down any dynamic EPP state) and raw_epp.
Fixes: d61c1ad39026 ("cpufreq/amd-pstate-ut: Add unit test for "dynamic" EPP mode")
Reported-by: Sashiko <sashiko-bot@kernel.org>
Link: https://sashiko.dev/#/bug/linux-e7023df1-8e55-46ee-a161-01320e6a4082
Suggested-by: K Prateek Nayak <kprateek.nayak@amd.com>
Signed-off-by: Mario Limonciello <mario.limonciello@amd.com>
---
drivers/cpufreq/amd-pstate-ut.c | 46 +++++++++++++++++++++++++++--------------
1 file changed, 31 insertions(+), 15 deletions(-)
diff --git a/drivers/cpufreq/amd-pstate-ut.c b/drivers/cpufreq/amd-pstate-ut.c
index 506d7bb2e6609..bc696f452749d 100644
--- a/drivers/cpufreq/amd-pstate-ut.c
+++ b/drivers/cpufreq/amd-pstate-ut.c
@@ -311,13 +311,13 @@ static int amd_pstate_ut_epp(u32 index)
EPP_INDEX_BALANCE_PERFORMANCE,
EPP_INDEX_PERFORMANCE,
};
+ char *orig_epp __free(cleanup_page) = NULL;
char *buf __free(cleanup_page) = NULL;
struct cpufreq_policy *policy = NULL;
- unsigned long orig_dynamic_epp = 0;
enum amd_pstate_mode orig_mode;
struct amd_cpudata *cpudata;
unsigned long orig_policy;
- int ret, cpu = 0;
+ int ret = 0, cpu = 0;
u16 epp;
int i;
@@ -325,14 +325,36 @@ static int amd_pstate_ut_epp(u32 index)
if (!policy)
return -ENODEV;
+ /* Prevent any changes. */
+ down_read(&policy->rwsem);
+
orig_mode = amd_pstate_get_status();
if (policy->driver_data) {
cpudata = policy->driver_data;
- orig_dynamic_epp = cpudata->dynamic_epp;
- }
+ if (orig_mode == AMD_PSTATE_ACTIVE) {
+ orig_epp = (char *)__get_free_page(GFP_KERNEL);
+ if (!orig_epp) {
+ ret = -ENOMEM;
+ goto early_fail;
+ }
+
+ ret = show_energy_performance_preference(policy, orig_epp);
+ if (ret < 0)
+ goto early_fail;
+
+ if (cpudata->dynamic_epp)
+ strreplace(orig_epp, '(', '\0');
+ }
+ }
+early_fail:
/* Drop reference before potential driver change. */
+ up_read(&policy->rwsem);
cpufreq_cpu_put(policy);
+
+ if (ret < 0)
+ return ret;
+
policy = NULL;
buf = (char *)__get_free_page(GFP_KERNEL);
@@ -424,17 +446,11 @@ static int amd_pstate_ut_epp(u32 index)
out:
if (policy) {
cpudata->policy = orig_policy;
- /*
- * If the driver had enabled dynamic_epp to begin with,
- * restore it here before dropping policy reference.
- */
- if (orig_dynamic_epp) {
- const char * const dynamic_string = energy_perf_strings[epp_modes[0]];
- int ret2;
-
- ret2 = store_energy_performance_preference(policy,
- dynamic_string,
- strlen(dynamic_string));
+ /* Restore the original EPP */
+ if (orig_epp) {
+ int ret2 = store_energy_performance_preference(policy,
+ orig_epp,
+ strlen(orig_epp));
if (!ret && (ret2 < 0))
ret = ret2;
}
--
2.43.0
^ permalink raw reply related [flat|nested] 11+ messages in thread
* [PATCH v2 6/7] cpufreq/amd-pstate: Snapshot policy limits in amd_pstate_update_min_max_limit()
2026-11-18 3:27 [PATCH v2 0/7] cpufreq: amd-pstate: Fix a batch of correctness bugs Mario Limonciello
` (4 preceding siblings ...)
2026-11-18 3:27 ` [PATCH v2 5/7] cpufreq/amd-pstate-ut: Restore EPP state at end of amd_pstate_ut_epp() Mario Limonciello
@ 2026-11-18 3:27 ` Mario Limonciello
2026-11-18 3:27 ` [PATCH v2 7/7] cpufreq: amd-pstate: Preserve names for aliased EPP values Mario Limonciello
` (2 subsequent siblings)
8 siblings, 0 replies; 11+ messages in thread
From: Mario Limonciello @ 2026-11-18 3:27 UTC (permalink / raw)
To: K Prateek Nayak; +Cc: linux-pm, Mario Limonciello, Sashiko
amd_pstate_update_min_max_limit() reads policy->max and policy->min
several times. It can run from the scheduler fast path without
policy->rwsem held, so cpufreq_set_policy() can update the limits midway
through the function.
The perf limits can then be computed from the old policy->max while
cpudata->max_limit_freq caches the new one. The cached value then matches
the policy, so later limit updates are skipped even though the perf
values programmed are stale, decoupling the hardware state from the user
policy.
Read policy->min and policy->max once into locals with READ_ONCE() and
use them for all computations and cached values, including the
min_limit_freq cap.
Fixes: 009d1c29a451 ("cpufreq/amd-pstate: Move perf values into a union")
Reported-by: Sashiko <sashiko-bot@kernel.org>
Link: https://sashiko.dev/#/bug/linux-f43c6bc5-cbcc-4959-af2b-f4e5afa1e3a3
Signed-off-by: Mario Limonciello <mario.limonciello@amd.com>
---
drivers/cpufreq/amd-pstate.c | 18 ++++++++++++------
1 file changed, 12 insertions(+), 6 deletions(-)
diff --git a/drivers/cpufreq/amd-pstate.c b/drivers/cpufreq/amd-pstate.c
index 574dfffc18f05..0acb545a5f4fa 100644
--- a/drivers/cpufreq/amd-pstate.c
+++ b/drivers/cpufreq/amd-pstate.c
@@ -738,12 +738,18 @@ static void amd_pstate_update_min_max_limit(struct cpufreq_policy *policy)
{
struct amd_cpudata *cpudata = policy->driver_data;
union perf_cached perf = READ_ONCE(cpudata->perf);
+ /*
+ * This can run without policy->rwsem held; snapshot the limits so
+ * the programmed perf values and the cached frequencies agree.
+ */
+ unsigned int policy_max = READ_ONCE(policy->max);
+ unsigned int policy_min = READ_ONCE(policy->min);
- if (policy->max >= READ_ONCE(cpudata->max_freq))
+ if (policy_max >= READ_ONCE(cpudata->max_freq))
perf.max_limit_perf = perf.highest_perf;
else
- perf.max_limit_perf = freq_to_perf(perf, cpudata->nominal_freq, policy->max);
- WRITE_ONCE(cpudata->max_limit_freq, policy->max);
+ perf.max_limit_perf = freq_to_perf(perf, cpudata->nominal_freq, policy_max);
+ WRITE_ONCE(cpudata->max_limit_freq, policy_max);
if (cpudata->policy == CPUFREQ_POLICY_PERFORMANCE) {
u8 min_limit_perf = perf.bios_min_perf ?: perf.nominal_perf;
@@ -770,10 +776,10 @@ static void amd_pstate_update_min_max_limit(struct cpufreq_policy *policy)
min_limit_freq = perf_to_freq(perf, cpudata->nominal_freq, min_limit_perf);
perf.min_limit_perf = min_limit_perf;
- WRITE_ONCE(cpudata->min_limit_freq, min(min_limit_freq, cpudata->max_limit_freq));
+ WRITE_ONCE(cpudata->min_limit_freq, min(min_limit_freq, policy_max));
} else {
- perf.min_limit_perf = freq_to_perf(perf, cpudata->nominal_freq, policy->min);
- WRITE_ONCE(cpudata->min_limit_freq, policy->min);
+ perf.min_limit_perf = freq_to_perf(perf, cpudata->nominal_freq, policy_min);
+ WRITE_ONCE(cpudata->min_limit_freq, policy_min);
}
WRITE_ONCE(cpudata->perf, perf);
--
2.43.0
^ permalink raw reply related [flat|nested] 11+ messages in thread
* [PATCH v2 7/7] cpufreq: amd-pstate: Preserve names for aliased EPP values
2026-11-18 3:27 [PATCH v2 0/7] cpufreq: amd-pstate: Fix a batch of correctness bugs Mario Limonciello
` (5 preceding siblings ...)
2026-11-18 3:27 ` [PATCH v2 6/7] cpufreq/amd-pstate: Snapshot policy limits in amd_pstate_update_min_max_limit() Mario Limonciello
@ 2026-11-18 3:27 ` Mario Limonciello
2026-11-30 3:56 ` [PATCH v2 0/7] cpufreq: amd-pstate: Fix a batch of correctness bugs K Prateek Nayak
2026-11-30 4:21 ` K Prateek Nayak
8 siblings, 0 replies; 11+ messages in thread
From: Mario Limonciello @ 2026-11-18 3:27 UTC (permalink / raw)
To: K Prateek Nayak; +Cc: linux-pm, Mario Limonciello, Mario Limonciello (AMD)
Zen6 low-power cores use the same numeric EPP value for the
balance_performance, balance_power, and power preferences. The show
callback currently scans the numeric values and returns the first matching
name, so writing balance_power or power reads back as
balance_performance.
Instead of initializing epp_index to EPP_INDEX_MAX and checking a range
plus value equality in the show callback, initialize epp_index to the
correct index during CPU init. The store callback sets the index on all
successful paths via a common success: label. The show callback then
simply checks if epp_index != EPP_INDEX_MAX to derive the string.
This approach is cleaner and more direct: the index is set to the
correct value at boot time based on the ACPI power profile, and updated
whenever the user writes a named preference via sysfs. Raw EPP writes
leave epp_index at EPP_INDEX_MAX, falling back to the existing numeric
lookup.
Make the unit test require exact string round-tripping again.
Fixes: 52763c00945a ("cpufreq: amd-pstate: Update Zen6 client EPP tuning values")
Signed-off-by: Mario Limonciello <mario.limonciello@amd.com>
Reviewed-by: K Prateek Nayak <kprateek.nayak@amd.com>
Tested-by: K Prateek Nayak <kprateek.nayak@amd.com>
Link: https://patch.msgid.link/20261012205559.3421297-1-mario.limonciello@amd.com
Signed-off-by: Mario Limonciello (AMD) <superm1@kernel.org>
---
drivers/cpufreq/amd-pstate-ut.c | 17 +----------------
drivers/cpufreq/amd-pstate.c | 23 +++++++++++++++++------
drivers/cpufreq/amd-pstate.h | 4 ++--
3 files changed, 20 insertions(+), 24 deletions(-)
diff --git a/drivers/cpufreq/amd-pstate-ut.c b/drivers/cpufreq/amd-pstate-ut.c
index bc696f452749d..cbb09c78c94c2 100644
--- a/drivers/cpufreq/amd-pstate-ut.c
+++ b/drivers/cpufreq/amd-pstate-ut.c
@@ -414,26 +414,11 @@ static int amd_pstate_ut_epp(u32 index)
strreplace(buf, '\n', '\0');
/*
* "dynamic" mode reports the EPP as "dynamic(profile:X)"
- * Trim at "(" and just compare tie the epp string.
+ * Trim at "(" and just compare the EPP string.
*/
strreplace(buf, '(', '\0');
if (strcmp(buf, energy_perf_strings[epp_modes[i]])) {
- u8 epp = FIELD_GET(AMD_CPPC_EPP_PERF_MASK, cpudata->cppc_req_cached);
-
- /*
- * The preference read back may legitimately differ from the one
- * written: some platforms (e.g. Zen6) program the same raw EPP
- * value for more than one named preference, so show() reports the
- * first name that matches that value.
- *
- * If strings mismatch, confirm the CPU is programmed with the correct
- * EPP value for the mode. This verifies hardware programming while
- * tolerating such aliasing in driver.
- */
- if (epp == amd_pstate_cpu_epp_values(cpudata->cpu_type)[epp_modes[i]])
- continue;
-
pr_err("String EPP value mismatch: %s != %s\n",
buf, energy_perf_strings[epp_modes[i]]);
ret = -EINVAL;
diff --git a/drivers/cpufreq/amd-pstate.c b/drivers/cpufreq/amd-pstate.c
index 0acb545a5f4fa..c49414f8ca962 100644
--- a/drivers/cpufreq/amd-pstate.c
+++ b/drivers/cpufreq/amd-pstate.c
@@ -118,7 +118,7 @@ static_assert(ARRAY_SIZE(epp_values) == TOPO_CPU_TYPE_LOW_POWER + 1,
* Non-hybrid systems use TOPO_CPU_TYPE_ANY; hybrid systems use the CPU's
* actual type (PERFORMANCE/EFFICIENCY/LOW_POWER).
*/
-u8 *amd_pstate_cpu_epp_values(enum x86_topology_cpu_type cpu_type)
+static inline u8 *amd_pstate_cpu_epp_values(enum x86_topology_cpu_type cpu_type)
{
switch (cpu_type) {
case TOPO_CPU_TYPE_PERFORMANCE:
@@ -129,7 +129,6 @@ u8 *amd_pstate_cpu_epp_values(enum x86_topology_cpu_type cpu_type)
return epp_values[TOPO_CPU_TYPE_ANY];
}
}
-EXPORT_SYMBOL_FOR_PSTATE_UT(amd_pstate_cpu_epp_values);
/**
* struct amd_pstate_epp_values - EPP values for the four named preferences
@@ -1479,6 +1478,7 @@ ssize_t store_energy_performance_preference(struct cpufreq_policy *policy,
{
struct amd_cpudata *cpudata = policy->driver_data;
u8 *values = amd_pstate_cpu_epp_values(cpudata->cpu_type);
+ u8 epp_index = EPP_INDEX_MAX;
ssize_t ret;
bool raw_epp = false;
u8 epp;
@@ -1502,20 +1502,22 @@ ssize_t store_energy_performance_preference(struct cpufreq_policy *policy,
* Nothing to do.
*/
if (cpudata->dynamic_epp)
- return count;
+ goto success;
cpudata->current_profile = PLATFORM_PROFILE_BALANCED;
ret = amd_pstate_set_dynamic_epp(policy);
if (ret)
return ret;
- return count;
+ goto success;
}
- if (ret)
+ if (ret) {
epp = values[ret];
- else
+ epp_index = ret;
+ } else {
epp = cpudata->epp_default_dc;
+ }
}
if (epp > 0 && epp != values[EPP_INDEX_PERFORMANCE] &&
@@ -1536,6 +1538,8 @@ ssize_t store_energy_performance_preference(struct cpufreq_policy *policy,
return ret;
cpudata->raw_epp = raw_epp;
+success:
+ cpudata->epp_index = epp_index;
return count;
}
@@ -1553,6 +1557,11 @@ ssize_t show_energy_performance_preference(struct cpufreq_policy *policy, char *
if (!cpudata->dynamic_epp && cpudata->raw_epp)
return sysfs_emit(buf, "%u\n", epp);
+ /* Several named preferences may share the same numeric EPP value. */
+ if (cpudata->epp_index != EPP_INDEX_MAX)
+ return sysfs_emit(buf, "%s\n",
+ energy_perf_strings[cpudata->epp_index]);
+
/*
* Map the cached EPP value back to a named preference. Skip the
* "default" slot (index 0) so an EPP of 0 reports as "performance".
@@ -2034,6 +2043,7 @@ static int amd_pstate_epp_cpu_init(struct cpufreq_policy *policy)
policy->policy = CPUFREQ_POLICY_PERFORMANCE;
cpudata->epp_default_ac = cpudata->epp_default_dc = default_epp;
cpudata->current_profile = PLATFORM_PROFILE_PERFORMANCE;
+ cpudata->epp_index = EPP_INDEX_PERFORMANCE;
} else {
u8 *values = amd_pstate_cpu_epp_values(cpudata->cpu_type);
@@ -2041,6 +2051,7 @@ static int amd_pstate_epp_cpu_init(struct cpufreq_policy *policy)
cpudata->epp_default_ac = values[EPP_INDEX_PERFORMANCE];
cpudata->epp_default_dc = values[EPP_INDEX_BALANCE_PERFORMANCE];
cpudata->current_profile = PLATFORM_PROFILE_BALANCED;
+ cpudata->epp_index = EPP_INDEX_BALANCE_PERFORMANCE;
}
ret = amd_pstate_set_epp(policy, cpudata->epp_default_dc);
diff --git a/drivers/cpufreq/amd-pstate.h b/drivers/cpufreq/amd-pstate.h
index 422188c639b1e..e42ea811d581e 100644
--- a/drivers/cpufreq/amd-pstate.h
+++ b/drivers/cpufreq/amd-pstate.h
@@ -101,6 +101,7 @@ struct amd_aperf_mperf {
* @dynamic_epp: Whether dynamic EPP is enabled
* @raw_epp: Whether the last EPP write was a raw numeric value rather than a
* named preference
+ * @epp_index: Index of the last static named EPP preference
* @power_nb: Notifier block for power events
* @current_profile: Currently selected platform profile option
* @ppdev: Device registered with the platform profile handler
@@ -142,6 +143,7 @@ struct amd_cpudata {
u8 epp_default_dc;
bool dynamic_epp;
bool raw_epp;
+ u8 epp_index;
struct notifier_block power_nb;
/* platform profile */
@@ -204,8 +206,6 @@ static const char * const energy_perf_strings[] = {
};
static_assert(ARRAY_SIZE(energy_perf_strings) == EPP_INDEX_MAX);
-u8 *amd_pstate_cpu_epp_values(enum x86_topology_cpu_type cpu_type);
-
static inline u8 freq_to_perf(union perf_cached perf, u32 nominal_freq, unsigned int freq_val)
{
u32 perf_val = DIV_ROUND_UP_ULL((u64)freq_val * perf.nominal_perf, nominal_freq);
--
2.43.0
^ permalink raw reply related [flat|nested] 11+ messages in thread
* Re: [PATCH v2 0/7] cpufreq: amd-pstate: Fix a batch of correctness bugs
2026-11-18 3:27 [PATCH v2 0/7] cpufreq: amd-pstate: Fix a batch of correctness bugs Mario Limonciello
` (6 preceding siblings ...)
2026-11-18 3:27 ` [PATCH v2 7/7] cpufreq: amd-pstate: Preserve names for aliased EPP values Mario Limonciello
@ 2026-11-30 3:56 ` K Prateek Nayak
2026-11-30 4:21 ` K Prateek Nayak
8 siblings, 0 replies; 11+ messages in thread
From: K Prateek Nayak @ 2026-11-30 3:56 UTC (permalink / raw)
To: Mario Limonciello; +Cc: linux-pm, Sashiko, Mario Limonciello (AMD)
Hello Mario,
On 11/18/2026 8:57 AM, Mario Limonciello wrote:
> This series fixes six independent bugs in the amd-pstate cpufreq driver
> and its unit test module. They were reported by automated code review
> and each is fixed in its own patch:
>
> - amd_prefcore_param() dereferenced a NULL string when "amd_prefcore"
> was passed on the kernel command line without a value, panicking
> early in boot.
> - shmem_get_epp() applied the bits 31:24 mask to the unshifted 8-bit
> value returned by cppc_get_epp_perf(), so it always returned 0 on
> shared-memory CPPC systems and the firmware default EPP was lost.
> - amd_pstate_init_freq() passed the quirk lowest_freq (MHz) to
> freq_to_perf() together with a nominal_freq in kHz, which produced a
> bogus lowest_perf.
> - get_mode_idx_from_str() matched any prefix of a mode name, so writing
> e.g. "undefi" to the status sysfs file was silently accepted instead
> of returning -EINVAL.
> - amd_pstate_ut_epp() leaked a platform profile device and left the
> EPP state modified when dynamic EPP was originally disabled.
> - amd_pstate_update_min_max_limit() read policy->min/max multiple times
> without holding policy->rwsem, so the programmed perf limits could
> disagree with the cached limit frequencies.
>
> Signed-off-by: Mario Limonciello <mario.limonciello@amd.com>
I gave this a spin when it came out but forgot to update! Feel free to
include:
Reviewed-by: K Prateek Nayak <kprateek.nayak@amd.com>
Tested-by: K Prateek Nayak <kprateek.nayak@amd.com>
--
Thanks and Regards,
Prateek
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH v2 0/7] cpufreq: amd-pstate: Fix a batch of correctness bugs
2026-11-18 3:27 [PATCH v2 0/7] cpufreq: amd-pstate: Fix a batch of correctness bugs Mario Limonciello
` (7 preceding siblings ...)
2026-11-30 3:56 ` [PATCH v2 0/7] cpufreq: amd-pstate: Fix a batch of correctness bugs K Prateek Nayak
@ 2026-11-30 4:21 ` K Prateek Nayak
2026-11-30 14:04 ` Mario Limonciello
8 siblings, 1 reply; 11+ messages in thread
From: K Prateek Nayak @ 2026-11-30 4:21 UTC (permalink / raw)
To: Mario Limonciello; +Cc: linux-pm, Sashiko, Mario Limonciello (AMD)
Hello Mario,
On 11/18/2026 8:57 AM, Mario Limonciello wrote:
> This series fixes six independent bugs in the amd-pstate cpufreq driver
> and its unit test module. They were reported by automated code review
> and each is fixed in its own patch:
>
> - amd_prefcore_param() dereferenced a NULL string when "amd_prefcore"
> was passed on the kernel command line without a value, panicking
> early in boot.
> - shmem_get_epp() applied the bits 31:24 mask to the unshifted 8-bit
> value returned by cppc_get_epp_perf(), so it always returned 0 on
> shared-memory CPPC systems and the firmware default EPP was lost.
> - amd_pstate_init_freq() passed the quirk lowest_freq (MHz) to
> freq_to_perf() together with a nominal_freq in kHz, which produced a
> bogus lowest_perf.
> - get_mode_idx_from_str() matched any prefix of a mode name, so writing
> e.g. "undefi" to the status sysfs file was silently accepted instead
> of returning -EINVAL.
> - amd_pstate_ut_epp() leaked a platform profile device and left the
> EPP state modified when dynamic EPP was originally disabled.
> - amd_pstate_update_min_max_limit() read policy->min/max multiple times
> without holding policy->rwsem, so the programmed perf limits could
> disagree with the cached limit frequencies.
>
> Signed-off-by: Mario Limonciello <mario.limonciello@amd.com>
I gave this a spin when it came out but forgot to update! Feel free to
include:
Reviewed-by: K Prateek Nayak <kprateek.nayak@amd.com>
Tested-by: K Prateek Nayak <kprateek.nayak@amd.com>
--
Thanks and Regards,
Prateek
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH v2 0/7] cpufreq: amd-pstate: Fix a batch of correctness bugs
2026-11-30 4:21 ` K Prateek Nayak
@ 2026-11-30 14:04 ` Mario Limonciello
0 siblings, 0 replies; 11+ messages in thread
From: Mario Limonciello @ 2026-11-30 14:04 UTC (permalink / raw)
To: K Prateek Nayak; +Cc: linux-pm, Sashiko, Mario Limonciello (AMD)
On 11/29/26 22:21, K Prateek Nayak wrote:
> Hello Mario,
>
> On 11/18/2026 8:57 AM, Mario Limonciello wrote:
>> This series fixes six independent bugs in the amd-pstate cpufreq driver
>> and its unit test module. They were reported by automated code review
>> and each is fixed in its own patch:
>>
>> - amd_prefcore_param() dereferenced a NULL string when "amd_prefcore"
>> was passed on the kernel command line without a value, panicking
>> early in boot.
>> - shmem_get_epp() applied the bits 31:24 mask to the unshifted 8-bit
>> value returned by cppc_get_epp_perf(), so it always returned 0 on
>> shared-memory CPPC systems and the firmware default EPP was lost.
>> - amd_pstate_init_freq() passed the quirk lowest_freq (MHz) to
>> freq_to_perf() together with a nominal_freq in kHz, which produced a
>> bogus lowest_perf.
>> - get_mode_idx_from_str() matched any prefix of a mode name, so writing
>> e.g. "undefi" to the status sysfs file was silently accepted instead
>> of returning -EINVAL.
>> - amd_pstate_ut_epp() leaked a platform profile device and left the
>> EPP state modified when dynamic EPP was originally disabled.
>> - amd_pstate_update_min_max_limit() read policy->min/max multiple times
>> without holding policy->rwsem, so the programmed perf limits could
>> disagree with the cached limit frequencies.
>>
>> Signed-off-by: Mario Limonciello <mario.limonciello@amd.com>
>
> I gave this a spin when it came out but forgot to update! Feel free to
> include:
>
> Reviewed-by: K Prateek Nayak <kprateek.nayak@amd.com>
> Tested-by: K Prateek Nayak <kprateek.nayak@amd.com>
>
Appreciated, thanks.
^ permalink raw reply [flat|nested] 11+ messages in thread
end of thread, other threads:[~2026-11-30 14:04 UTC | newest]
Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-18 3:27 [PATCH v2 0/7] cpufreq: amd-pstate: Fix a batch of correctness bugs Mario Limonciello
2026-11-18 3:27 ` [PATCH v2 1/7] cpufreq: amd-pstate: Fix NULL dereference in amd_prefcore_param() Mario Limonciello
2026-11-18 3:27 ` [PATCH v2 2/7] cpufreq/amd-pstate: Fix EPP extraction in shmem_get_epp() Mario Limonciello
2026-11-18 3:27 ` [PATCH v2 3/7] cpufreq/amd-pstate: Fix unit mismatch in amd_pstate_init_freq() Mario Limonciello
2026-11-18 3:27 ` [PATCH v2 4/7] cpufreq: amd-pstate: Require exact mode string match Mario Limonciello
2026-11-18 3:27 ` [PATCH v2 5/7] cpufreq/amd-pstate-ut: Restore EPP state at end of amd_pstate_ut_epp() Mario Limonciello
2026-11-18 3:27 ` [PATCH v2 6/7] cpufreq/amd-pstate: Snapshot policy limits in amd_pstate_update_min_max_limit() Mario Limonciello
2026-11-18 3:27 ` [PATCH v2 7/7] cpufreq: amd-pstate: Preserve names for aliased EPP values Mario Limonciello
2026-11-30 3:56 ` [PATCH v2 0/7] cpufreq: amd-pstate: Fix a batch of correctness bugs K Prateek Nayak
2026-11-30 4:21 ` K Prateek Nayak
2026-11-30 14:04 ` Mario Limonciello
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox