From: Mario Limonciello <mario.limonciello@amd.com>
To: "Gautham R. Shenoy" <gautham.shenoy@amd.com>,
"Rafael J . Wysocki" <rafael@kernel.org>,
Viresh Kumar <viresh.kumar@linaro.org>,
K Prateek Nayak <kprateek.nayak@amd.com>
Cc: linux-kernel@vger.kernel.org, linux-pm@vger.kernel.org
Subject: Re: [PATCH v4 09/12] amd-pstate-ut: Add a testcase to validate the visibility of driver attributes
Date: Thu, 26 Mar 2026 14:34:52 -0500 [thread overview]
Message-ID: <6ff31c1b-8d97-4e63-9397-c2e04ae997ed@amd.com> (raw)
In-Reply-To: <20260326114756.20374-10-gautham.shenoy@amd.com>
On 3/26/26 06:47, Gautham R. Shenoy wrote:
> amd-pstate driver has per-attribute visibility functions to
> dynamically control which sysfs freq_attrs are exposed based on the
> platform capabilities and the current amd_pstate mode. However, there
> is no test coverage to validate that the driver's live attribute list
> matches the expected visibility for each mode.
>
> Add amd_pstate_ut_check_freq_attrs() to the amd-pstate unit test
> module. For each enabled mode (passive, active, guided), the test
> independently derives the expected visibility of each attribute:
> - Core attributes (max_freq, lowest_nonlinear_freq, highest_perf)
> are always expected.
> - Prefcore attributes (prefcore_ranking, hw_prefcore) are expected
> only when cpudata->hw_prefcore indicates platform support.
> - EPP attributes (energy_performance_preference,
> energy_performance_available_preferences) are expected only in
> active mode.
> - Floor frequency attributes (floor_freq, floor_count) are expected
> only when X86_FEATURE_CPPC_PERF_PRIO is present.
>
> Compare these independent expectations against the live driver's attr
> array, catching bugs such as attributes leaking into wrong modes or
> visibility functions checking incorrect conditions.
>
> Signed-off-by: Gautham R. Shenoy <gautham.shenoy@amd.com>
Reviewed-by: Mario Limonciello (AMD) <superm1@kernel.org>
> ---
> drivers/cpufreq/amd-pstate-ut.c | 139 ++++++++++++++++++++++++++++++--
> drivers/cpufreq/amd-pstate.c | 8 ++
> drivers/cpufreq/amd-pstate.h | 4 +
> 3 files changed, 146 insertions(+), 5 deletions(-)
>
> diff --git a/drivers/cpufreq/amd-pstate-ut.c b/drivers/cpufreq/amd-pstate-ut.c
> index 3dcdf56883a6..1f62ab6438b4 100644
> --- a/drivers/cpufreq/amd-pstate-ut.c
> +++ b/drivers/cpufreq/amd-pstate-ut.c
> @@ -23,6 +23,8 @@
> #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
>
> #include <linux/bitfield.h>
> +#include <linux/cpufeature.h>
> +#include <linux/cpufreq.h>
> #include <linux/kernel.h>
> #include <linux/module.h>
> #include <linux/moduleparam.h>
> @@ -53,13 +55,15 @@ static int amd_pstate_ut_check_enabled(u32 index);
> static int amd_pstate_ut_check_perf(u32 index);
> static int amd_pstate_ut_check_freq(u32 index);
> static int amd_pstate_ut_check_driver(u32 index);
> +static int amd_pstate_ut_check_freq_attrs(u32 index);
>
> static struct amd_pstate_ut_struct amd_pstate_ut_cases[] = {
> - {"amd_pstate_ut_acpi_cpc_valid", amd_pstate_ut_acpi_cpc_valid },
> - {"amd_pstate_ut_check_enabled", amd_pstate_ut_check_enabled },
> - {"amd_pstate_ut_check_perf", amd_pstate_ut_check_perf },
> - {"amd_pstate_ut_check_freq", amd_pstate_ut_check_freq },
> - {"amd_pstate_ut_check_driver", amd_pstate_ut_check_driver }
> + {"amd_pstate_ut_acpi_cpc_valid", amd_pstate_ut_acpi_cpc_valid },
> + {"amd_pstate_ut_check_enabled", amd_pstate_ut_check_enabled },
> + {"amd_pstate_ut_check_perf", amd_pstate_ut_check_perf },
> + {"amd_pstate_ut_check_freq", amd_pstate_ut_check_freq },
> + {"amd_pstate_ut_check_driver", amd_pstate_ut_check_driver },
> + {"amd_pstate_ut_check_freq_attrs", amd_pstate_ut_check_freq_attrs },
> };
>
> static bool test_in_list(const char *list, const char *name)
> @@ -293,6 +297,131 @@ static int amd_pstate_ut_check_driver(u32 index)
> return ret;
> }
>
> +enum attr_category {
> + ATTR_ALWAYS,
> + ATTR_PREFCORE,
> + ATTR_EPP,
> + ATTR_FLOOR_FREQ,
> +};
> +
> +static const struct {
> + const char *name;
> + enum attr_category category;
> +} expected_freq_attrs[] = {
> + {"amd_pstate_max_freq", ATTR_ALWAYS},
> + {"amd_pstate_lowest_nonlinear_freq", ATTR_ALWAYS},
> + {"amd_pstate_highest_perf", ATTR_ALWAYS},
> + {"amd_pstate_prefcore_ranking", ATTR_PREFCORE},
> + {"amd_pstate_hw_prefcore", ATTR_PREFCORE},
> + {"energy_performance_preference", ATTR_EPP},
> + {"energy_performance_available_preferences", ATTR_EPP},
> + {"amd_pstate_floor_freq", ATTR_FLOOR_FREQ},
> + {"amd_pstate_floor_count", ATTR_FLOOR_FREQ},
> +};
> +
> +static bool attr_in_driver(struct freq_attr **driver_attrs, const char *name)
> +{
> + int j;
> +
> + for (j = 0; driver_attrs[j]; j++) {
> + if (!strcmp(driver_attrs[j]->attr.name, name))
> + return true;
> + }
> + return false;
> +}
> +
> +/*
> + * Verify that for each mode the driver's live ->attr array contains exactly
> + * the attributes that should be visible. Expected visibility is derived
> + * independently from hw_prefcore, cpu features, and the current mode —
> + * not from the driver's own visibility functions.
> + */
> +static int amd_pstate_ut_check_freq_attrs(u32 index)
> +{
> + enum amd_pstate_mode orig_mode = amd_pstate_get_status();
> + static const enum amd_pstate_mode modes[] = {
> + AMD_PSTATE_PASSIVE, AMD_PSTATE_ACTIVE, AMD_PSTATE_GUIDED,
> + };
> + bool has_prefcore, has_floor_freq;
> + int m, i, ret;
> +
> + has_floor_freq = cpu_feature_enabled(X86_FEATURE_CPPC_PERF_PRIO);
> +
> + /*
> + * Determine prefcore support from any online CPU's cpudata.
> + * hw_prefcore reflects the platform-wide decision made at init.
> + */
> + has_prefcore = false;
> + for_each_online_cpu(i) {
> + struct cpufreq_policy *policy __free(put_cpufreq_policy) = NULL;
> + struct amd_cpudata *cpudata;
> +
> + policy = cpufreq_cpu_get(i);
> + if (!policy)
> + continue;
> + cpudata = policy->driver_data;
> + has_prefcore = cpudata->hw_prefcore;
> + break;
> + }
> +
> + for (m = 0; m < ARRAY_SIZE(modes); m++) {
> + struct freq_attr **driver_attrs;
> +
> + ret = amd_pstate_set_mode(modes[m]);
> + if (ret)
> + goto out;
> +
> + driver_attrs = amd_pstate_get_current_attrs();
> + if (!driver_attrs) {
> + pr_err("%s: no driver attrs in mode %s\n",
> + __func__, amd_pstate_get_mode_string(modes[m]));
> + ret = -EINVAL;
> + goto out;
> + }
> +
> + for (i = 0; i < ARRAY_SIZE(expected_freq_attrs); i++) {
> + bool expected, found;
> +
> + switch (expected_freq_attrs[i].category) {
> + case ATTR_ALWAYS:
> + expected = true;
> + break;
> + case ATTR_PREFCORE:
> + expected = has_prefcore;
> + break;
> + case ATTR_EPP:
> + expected = (modes[m] == AMD_PSTATE_ACTIVE);
> + break;
> + case ATTR_FLOOR_FREQ:
> + expected = has_floor_freq;
> + break;
> + default:
> + expected = false;
> + break;
> + }
> +
> + found = attr_in_driver(driver_attrs,
> + expected_freq_attrs[i].name);
> +
> + if (expected != found) {
> + pr_err("%s: mode %s: attr %s expected %s but is %s\n",
> + __func__,
> + amd_pstate_get_mode_string(modes[m]),
> + expected_freq_attrs[i].name,
> + expected ? "visible" : "hidden",
> + found ? "visible" : "hidden");
> + ret = -EINVAL;
> + goto out;
> + }
> + }
> + }
> +
> + ret = 0;
> +out:
> + amd_pstate_set_mode(orig_mode);
> + return ret;
> +}
> +
> static int __init amd_pstate_ut_init(void)
> {
> u32 i = 0, arr_size = ARRAY_SIZE(amd_pstate_ut_cases);
> diff --git a/drivers/cpufreq/amd-pstate.c b/drivers/cpufreq/amd-pstate.c
> index 5eae74a67aeb..ed9fd4155a25 100644
> --- a/drivers/cpufreq/amd-pstate.c
> +++ b/drivers/cpufreq/amd-pstate.c
> @@ -1390,6 +1390,14 @@ static struct freq_attr_visibility amd_pstate_attr_visibility[] = {
> {&amd_pstate_floor_count, floor_freq_visibility},
> };
>
> +struct freq_attr **amd_pstate_get_current_attrs(void)
> +{
> + if (!current_pstate_driver)
> + return NULL;
> + return current_pstate_driver->attr;
> +}
> +EXPORT_SYMBOL_GPL(amd_pstate_get_current_attrs);
> +
> static struct freq_attr **get_freq_attrs(void)
> {
> bool attr_visible[ARRAY_SIZE(amd_pstate_attr_visibility)];
> diff --git a/drivers/cpufreq/amd-pstate.h b/drivers/cpufreq/amd-pstate.h
> index 453adfb445f8..faead0b19a8a 100644
> --- a/drivers/cpufreq/amd-pstate.h
> +++ b/drivers/cpufreq/amd-pstate.h
> @@ -134,4 +134,8 @@ const char *amd_pstate_get_mode_string(enum amd_pstate_mode mode);
> int amd_pstate_get_status(void);
> int amd_pstate_update_status(const char *buf, size_t size);
>
> +struct freq_attr;
> +
> +struct freq_attr **amd_pstate_get_current_attrs(void);
> +
> #endif /* _LINUX_AMD_PSTATE_H */
next prev parent reply other threads:[~2026-03-26 19:34 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-03-26 11:47 [PATCH v4 00/12] amd-pstate: Introduce AMD CPPC Performance Priority Gautham R. Shenoy
2026-03-26 11:47 ` [PATCH v4 01/12] amd-pstate: Fix memory leak in amd_pstate_epp_cpu_init() Gautham R. Shenoy
2026-03-26 11:47 ` [PATCH v4 02/12] amd-pstate: Update cppc_req_cached in fast_switch case Gautham R. Shenoy
2026-03-26 11:47 ` [PATCH v4 03/12] amd-pstate: Make certain freq_attrs conditionally visible Gautham R. Shenoy
2026-03-26 11:47 ` [PATCH v4 04/12] x86/cpufeatures: Add AMD CPPC Performance Priority feature Gautham R. Shenoy
2026-03-26 11:47 ` [PATCH v4 05/12] amd-pstate: Add support for CPPC_REQ2 and FLOOR_PERF Gautham R. Shenoy
2026-03-26 11:47 ` [PATCH v4 06/12] amd-pstate: Add sysfs support for floor_freq and floor_count Gautham R. Shenoy
2026-03-26 11:47 ` [PATCH v4 07/12] amd-pstate: Introduce a tracepoint trace_amd_pstate_cppc_req2() Gautham R. Shenoy
2026-03-26 11:47 ` [PATCH v4 08/12] amd-pstate-ut: Add module parameter to select testcases Gautham R. Shenoy
2026-03-26 18:46 ` Mario Limonciello
2026-03-26 11:47 ` [PATCH v4 09/12] amd-pstate-ut: Add a testcase to validate the visibility of driver attributes Gautham R. Shenoy
2026-03-26 19:34 ` Mario Limonciello [this message]
2026-03-26 11:47 ` [PATCH v4 10/12] Documentation/amd-pstate: List amd_pstate_hw_prefcore sysfs file Gautham R. Shenoy
2026-03-26 11:47 ` [PATCH v4 11/12] Documentation/amd-pstate: List amd_pstate_prefcore_ranking " Gautham R. Shenoy
2026-03-26 11:47 ` [PATCH v4 12/12] Documentation/amd-pstate: Add documentation for amd_pstate_floor_{freq,count} Gautham R. Shenoy
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=6ff31c1b-8d97-4e63-9397-c2e04ae997ed@amd.com \
--to=mario.limonciello@amd.com \
--cc=gautham.shenoy@amd.com \
--cc=kprateek.nayak@amd.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-pm@vger.kernel.org \
--cc=rafael@kernel.org \
--cc=viresh.kumar@linaro.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox