Skip to content

Commit 3b90e5a

Browse files
gautshensuperm1
authored andcommitted
amd-pstate-ut: Add a testcase to validate the visibility of driver attributes
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> Signed-off-by: Mario Limonciello (AMD) <superm1@kernel.org>
1 parent c6a2b75 commit 3b90e5a

3 files changed

Lines changed: 146 additions & 5 deletions

File tree

drivers/cpufreq/amd-pstate-ut.c

Lines changed: 134 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
2424

2525
#include <linux/bitfield.h>
26+
#include <linux/cpufeature.h>
27+
#include <linux/cpufreq.h>
2628
#include <linux/kernel.h>
2729
#include <linux/module.h>
2830
#include <linux/moduleparam.h>
@@ -53,13 +55,15 @@ static int amd_pstate_ut_check_enabled(u32 index);
5355
static int amd_pstate_ut_check_perf(u32 index);
5456
static int amd_pstate_ut_check_freq(u32 index);
5557
static int amd_pstate_ut_check_driver(u32 index);
58+
static int amd_pstate_ut_check_freq_attrs(u32 index);
5659

5760
static struct amd_pstate_ut_struct amd_pstate_ut_cases[] = {
58-
{"amd_pstate_ut_acpi_cpc_valid", amd_pstate_ut_acpi_cpc_valid },
59-
{"amd_pstate_ut_check_enabled", amd_pstate_ut_check_enabled },
60-
{"amd_pstate_ut_check_perf", amd_pstate_ut_check_perf },
61-
{"amd_pstate_ut_check_freq", amd_pstate_ut_check_freq },
62-
{"amd_pstate_ut_check_driver", amd_pstate_ut_check_driver }
61+
{"amd_pstate_ut_acpi_cpc_valid", amd_pstate_ut_acpi_cpc_valid },
62+
{"amd_pstate_ut_check_enabled", amd_pstate_ut_check_enabled },
63+
{"amd_pstate_ut_check_perf", amd_pstate_ut_check_perf },
64+
{"amd_pstate_ut_check_freq", amd_pstate_ut_check_freq },
65+
{"amd_pstate_ut_check_driver", amd_pstate_ut_check_driver },
66+
{"amd_pstate_ut_check_freq_attrs", amd_pstate_ut_check_freq_attrs },
6367
};
6468

6569
static bool test_in_list(const char *list, const char *name)
@@ -293,6 +297,131 @@ static int amd_pstate_ut_check_driver(u32 index)
293297
return ret;
294298
}
295299

300+
enum attr_category {
301+
ATTR_ALWAYS,
302+
ATTR_PREFCORE,
303+
ATTR_EPP,
304+
ATTR_FLOOR_FREQ,
305+
};
306+
307+
static const struct {
308+
const char *name;
309+
enum attr_category category;
310+
} expected_freq_attrs[] = {
311+
{"amd_pstate_max_freq", ATTR_ALWAYS},
312+
{"amd_pstate_lowest_nonlinear_freq", ATTR_ALWAYS},
313+
{"amd_pstate_highest_perf", ATTR_ALWAYS},
314+
{"amd_pstate_prefcore_ranking", ATTR_PREFCORE},
315+
{"amd_pstate_hw_prefcore", ATTR_PREFCORE},
316+
{"energy_performance_preference", ATTR_EPP},
317+
{"energy_performance_available_preferences", ATTR_EPP},
318+
{"amd_pstate_floor_freq", ATTR_FLOOR_FREQ},
319+
{"amd_pstate_floor_count", ATTR_FLOOR_FREQ},
320+
};
321+
322+
static bool attr_in_driver(struct freq_attr **driver_attrs, const char *name)
323+
{
324+
int j;
325+
326+
for (j = 0; driver_attrs[j]; j++) {
327+
if (!strcmp(driver_attrs[j]->attr.name, name))
328+
return true;
329+
}
330+
return false;
331+
}
332+
333+
/*
334+
* Verify that for each mode the driver's live ->attr array contains exactly
335+
* the attributes that should be visible. Expected visibility is derived
336+
* independently from hw_prefcore, cpu features, and the current mode —
337+
* not from the driver's own visibility functions.
338+
*/
339+
static int amd_pstate_ut_check_freq_attrs(u32 index)
340+
{
341+
enum amd_pstate_mode orig_mode = amd_pstate_get_status();
342+
static const enum amd_pstate_mode modes[] = {
343+
AMD_PSTATE_PASSIVE, AMD_PSTATE_ACTIVE, AMD_PSTATE_GUIDED,
344+
};
345+
bool has_prefcore, has_floor_freq;
346+
int m, i, ret;
347+
348+
has_floor_freq = cpu_feature_enabled(X86_FEATURE_CPPC_PERF_PRIO);
349+
350+
/*
351+
* Determine prefcore support from any online CPU's cpudata.
352+
* hw_prefcore reflects the platform-wide decision made at init.
353+
*/
354+
has_prefcore = false;
355+
for_each_online_cpu(i) {
356+
struct cpufreq_policy *policy __free(put_cpufreq_policy) = NULL;
357+
struct amd_cpudata *cpudata;
358+
359+
policy = cpufreq_cpu_get(i);
360+
if (!policy)
361+
continue;
362+
cpudata = policy->driver_data;
363+
has_prefcore = cpudata->hw_prefcore;
364+
break;
365+
}
366+
367+
for (m = 0; m < ARRAY_SIZE(modes); m++) {
368+
struct freq_attr **driver_attrs;
369+
370+
ret = amd_pstate_set_mode(modes[m]);
371+
if (ret)
372+
goto out;
373+
374+
driver_attrs = amd_pstate_get_current_attrs();
375+
if (!driver_attrs) {
376+
pr_err("%s: no driver attrs in mode %s\n",
377+
__func__, amd_pstate_get_mode_string(modes[m]));
378+
ret = -EINVAL;
379+
goto out;
380+
}
381+
382+
for (i = 0; i < ARRAY_SIZE(expected_freq_attrs); i++) {
383+
bool expected, found;
384+
385+
switch (expected_freq_attrs[i].category) {
386+
case ATTR_ALWAYS:
387+
expected = true;
388+
break;
389+
case ATTR_PREFCORE:
390+
expected = has_prefcore;
391+
break;
392+
case ATTR_EPP:
393+
expected = (modes[m] == AMD_PSTATE_ACTIVE);
394+
break;
395+
case ATTR_FLOOR_FREQ:
396+
expected = has_floor_freq;
397+
break;
398+
default:
399+
expected = false;
400+
break;
401+
}
402+
403+
found = attr_in_driver(driver_attrs,
404+
expected_freq_attrs[i].name);
405+
406+
if (expected != found) {
407+
pr_err("%s: mode %s: attr %s expected %s but is %s\n",
408+
__func__,
409+
amd_pstate_get_mode_string(modes[m]),
410+
expected_freq_attrs[i].name,
411+
expected ? "visible" : "hidden",
412+
found ? "visible" : "hidden");
413+
ret = -EINVAL;
414+
goto out;
415+
}
416+
}
417+
}
418+
419+
ret = 0;
420+
out:
421+
amd_pstate_set_mode(orig_mode);
422+
return ret;
423+
}
424+
296425
static int __init amd_pstate_ut_init(void)
297426
{
298427
u32 i = 0, arr_size = ARRAY_SIZE(amd_pstate_ut_cases);

drivers/cpufreq/amd-pstate.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1390,6 +1390,14 @@ static struct freq_attr_visibility amd_pstate_attr_visibility[] = {
13901390
{&amd_pstate_floor_count, floor_freq_visibility},
13911391
};
13921392

1393+
struct freq_attr **amd_pstate_get_current_attrs(void)
1394+
{
1395+
if (!current_pstate_driver)
1396+
return NULL;
1397+
return current_pstate_driver->attr;
1398+
}
1399+
EXPORT_SYMBOL_GPL(amd_pstate_get_current_attrs);
1400+
13931401
static struct freq_attr **get_freq_attrs(void)
13941402
{
13951403
bool attr_visible[ARRAY_SIZE(amd_pstate_attr_visibility)];

drivers/cpufreq/amd-pstate.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,4 +134,8 @@ const char *amd_pstate_get_mode_string(enum amd_pstate_mode mode);
134134
int amd_pstate_get_status(void);
135135
int amd_pstate_update_status(const char *buf, size_t size);
136136

137+
struct freq_attr;
138+
139+
struct freq_attr **amd_pstate_get_current_attrs(void);
140+
137141
#endif /* _LINUX_AMD_PSTATE_H */

0 commit comments

Comments
 (0)