Skip to contentSkip to conversation

cpufreq: intel_pstate: Use dedicated lock in set_power_ctl_ee_state()

PATCH New Rafael J. Wysocki 1 participant 1 message Nov 19

Subsystems (2)
lore
Share & tools
lore

Rafael J. Wysocki

author maintainer
#0 lore raw

PATCH 1/1 cpufreq: intel_pstate: Use dedicated lock in set_power_ctl_ee_state()

From: "Rafael J. Wysocki" <[email protected]>

Commit eee2a44e4e2b ("cpufreq: Improve suspend-resume synchronization") caused lockdep to complain about a possible circular locking dependency because intel_pstate_resume() is now called under cpus_read_lock, and it calls set_power_ctl_ee_state() that acquires intel_pstate_driver_lock which can be held around CPU hotplug locking.

Fortunately, this cannot lead to a real deadlock because intel_pstate_resume() does not run in parallel with the other holders of intel_pstate_driver_lock, but in fact set_power_ctl_ee_state() need not use that particular lock either. Moreover, the original purpose of that lock is different, so using it in set_power_ctl_ee_state() is questionable.

Accordingly, introduce a dedicated mutex for protecting MSR_IA32_POWER_CTL updates and power_ctl_ee_state accesses and make set_power_ctl_ee_state() use it instead of intel_pstate_driver_lock. Also take it directly in intel_pstate_resume() around the EE state restore.

Fixes eee2a44e4e2b ("cpufreq: Improve suspend-resume synchronization")
---
 drivers/cpufreq/intel_pstate.c |   24 +++++++++++++++++-------
 1 file changed, 17 insertions(+), 7 deletions(-)
drivers/cpufreq/intel_pstate.c +17 -7
@@ -1351,13 +1351,14 @@ static void intel_pstate_hwp_offline(str
 /* Enable bit for Dynamic Efficiency Control (DEC) */
 #define POWER_CTL_DEC_ENABLE	27
 
+static DEFINE_MUTEX(power_ctl_update_lock);
 static int power_ctl_ee_state;
 
-static void set_power_ctl_ee_state(bool input)
+static void __set_power_ctl_ee_state(bool input)
 {
 	u64 power_ctl;
 
-	guard(mutex)(&intel_pstate_driver_lock);
+	lockdep_assert_held(&power_ctl_update_lock);
 
 	rdmsrq(MSR_IA32_POWER_CTL, power_ctl);
 	if (input) {
@@ -1370,6 +1371,13 @@ static void set_power_ctl_ee_state(bool
 	wrmsrq(MSR_IA32_POWER_CTL, power_ctl);
 }
 
+static void set_power_ctl_ee_state(bool input)
+{
+	guard(mutex)(&power_ctl_update_lock);
+
+	__set_power_ctl_ee_state(input);
+}
+
 static void intel_pstate_hwp_enable(struct cpudata *cpudata);
 
 static void intel_pstate_hwp_reenable(struct cpudata *cpu)
@@ -1398,11 +1406,13 @@ static int intel_pstate_resume(struct cp
 
 	pr_debug("CPU %d resuming\n", cpu->cpu);
 
-	/* Only restore if the system default is changed */
-	if (power_ctl_ee_state == POWER_CTL_EE_ENABLE)
-		set_power_ctl_ee_state(true);
-	else if (power_ctl_ee_state == POWER_CTL_EE_DISABLE)
-		set_power_ctl_ee_state(false);
+	scoped_guard(mutex, &power_ctl_update_lock) {
+		/* Only restore if the system default has changed */
+		if (power_ctl_ee_state == POWER_CTL_EE_ENABLE)
+			__set_power_ctl_ee_state(true);
+		else if (power_ctl_ee_state == POWER_CTL_EE_DISABLE)
+			__set_power_ctl_ee_state(false);
+	}
 
 	if (cpu->suspended && hwp_active) {
 		mutex_lock(&intel_pstate_limits_lock);