Commit f9f16835 authored by K Prateek Nayak's avatar K Prateek Nayak Committed by Mario Limonciello (AMD)
Browse files

cpufreq/amd-pstate-ut: Drop policy reference before driver switch



Recent changes to the EPP unit test tries to perform a driver switch
with a cpufreq_policy reference held when the driver is loaded into
anything but the active mode which leads to a circular dependency and
the unit test hanging indefinitely.

Drop the reference before driver switch and grab it back once the driver
mode is stabilized for the test.

The EPP writes are only possible with CPUFREQ_POLICY_POWERSAVE policy.
Temporarily switch the cpudata->policy (while holding the write end of
the policy->rwsem) to CPUFREQ_POLICY_POWERSAVE and restore the original
policy once tests are done. To ensure the final EPP is correct in case
the driver started with CPUFREQ_POLICY_PERFORMANCE, EPP performance is
tested last.

The __free() based cleanup for cpufreq_policy is lost in the process.

Reported-by: default avatarKalpana Shetty <kalpana.shetty@amd.com>
Fixes: 7e173bc3 ("cpufreq/amd-pstate-ut: Add a unit test for raw EPP")
Reviewed-by: default avatarMario Limonciello <mario.limonciello@amd.com>
Signed-off-by: default avatarK Prateek Nayak <kprateek.nayak@amd.com>
Link: https://lore.kernel.org/r/20260508051748.10484-7-kprateek.nayak@amd.com


Signed-off-by: default avatarMario Limonciello (AMD) <superm1@kernel.org>
parent caa822d3
Loading
Loading
Loading
Loading
+29 −7
Original line number Diff line number Diff line
@@ -274,20 +274,21 @@ static int amd_pstate_set_mode(enum amd_pstate_mode mode)

static int amd_pstate_ut_epp(u32 index)
{
	struct cpufreq_policy *policy __free(put_cpufreq_policy) = NULL;
	char *buf __free(cleanup_page) = NULL;
	static const char * const epp_strings[] = {
		"performance",
		"balance_performance",
		"balance_power",
		"power",
		"balance_power",
		"balance_performance",
		"performance",
	};
	struct amd_cpudata *cpudata;
	char *buf __free(cleanup_page) = NULL;
	struct cpufreq_policy *policy = NULL;
	enum amd_pstate_mode orig_mode;
	struct amd_cpudata *cpudata;
	unsigned long orig_policy;
	bool orig_dynamic_epp;
	int ret, cpu = 0;
	int i;
	u16 epp;
	int i;

	policy = cpufreq_cpu_get(cpu);
	if (!policy)
@@ -297,6 +298,10 @@ static int amd_pstate_ut_epp(u32 index)
	orig_mode = amd_pstate_get_status();
	orig_dynamic_epp = cpudata->dynamic_epp;

	/* Drop reference before potential driver change. */
	cpufreq_cpu_put(policy);
	policy = NULL;

	/* disable dynamic EPP before running test */
	if (cpudata->dynamic_epp) {
		pr_debug("Dynamic EPP is enabled, disabling it\n");
@@ -311,6 +316,17 @@ static int amd_pstate_ut_epp(u32 index)
	if (ret)
		goto out;

	policy = cpufreq_cpu_get(cpu);
	if (!policy) {
		ret = -ENODEV;
		goto out;
	}

	down_write(&policy->rwsem);
	cpudata = policy->driver_data;
	orig_policy = cpudata->policy;
	cpudata->policy = CPUFREQ_POLICY_POWERSAVE;

	for (epp = 0; epp <= U8_MAX; epp++) {
		u8 val;

@@ -358,6 +374,12 @@ static int amd_pstate_ut_epp(u32 index)
	ret = 0;

out:
	if (policy) {
		cpudata->policy = orig_policy;
		up_write(&policy->rwsem);
		cpufreq_cpu_put(policy);
	}

	if (orig_dynamic_epp) {
		int ret2;