Skip to contentSkip to conversation

cpufreq: Fix synchronization with system suspend/resume

PATCH In review Rafael J. Wysocki 8 participants 17 messages Nov 3 – Nov 19

Subsystems (2)
lore
Share & tools
lore

Rafael J. Wysocki

author maintainer
#0 lore raw

v2 cover cpufreq: Fix synchronization with system suspend/resume

Hi,

This is an update of

https://lore.kernel.org/linux-pm/[email protected]/

that modifies the first two patches in the series (to address Sashiko findings) and drops the last one.

This series fixes issues related to the synchronization between cpufreq and the intel_pstate driver with system suspend/resume mentioned here:

https://lore.kernel.org/linux-pm/CAJZ5v0hT6aoX4tuJM+rO_5iequFc_7rEXCoaFnHoJBaH3JP4KA@mail.gmail.com/

The first patch actually prevents governor operations from being carried out on suspended cpufreq policies.

The second one forces the driver .target() callback to be invoked on the first frequency update after cpufreq policy resume.

The third one causes intel_pstate to invalidate the cached last P-state value during suspend.

Thanks!

author maintainer
#1 lore raw

PATCH v2 1/3 cpufreq: Improve suspend-resume synchronization

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

The suspend-resume synchronization in the cpufreq core is not perfect because the global cpufreq_suspended variable is set at the end of cpufreq_suspend() after stopping governors for all of the active policies, so the governor .limits() callback may run via cpufreq_governor_limits() after stopping the governor and before setting cpufreq_suspended which is questionable. Likewise, the governor .limits() callback may run between clearing cpufreq_suspended in cpufreq_resume() and starting the governor.

Address this by adding a per-policy "suspended" flag that is always read under the policy rwsem and is updated under it when the governor is stopped in cpufreq_suspend() and when the governor is restarted in cpufreq_resume().

Also make cpufreq_resume() use the cpus_read_lock guard to reflect what is done in cpufreq_suspend() and call cpufreq_exit_governor() for policies that were suspended during a system suspend transition and are inactive during the subsequent system resume (to avoid leaking an inactive policy with a stopped governor that has not exited yet).

Fixes 8e30444e1530 ("cpufreq: fix cpufreq suspend/resume for intel_pstate")
Fixes b1b12babe3b7 ("cpufreq: update 'cpufreq_suspended' after stopping governors")
Cc All applicable <[email protected]>

---

v1 -> v2:
* Retain all of the cpufreq_suspended checks to avoid carrying out governor operations after suspending cpufreq during system shutdown and during hibernation resume on systems with SMT (Sashiko)

* Rearrange cpufreq_resume() to walk all cpufreq policies and call cpufreq_resume_policy() for all of them

* Call cpufreq_exit_governor() in cpufreq_resume_policy() for policies that were suspended during a system suspend transition and are inactive during the subsequent system resume (Sashiko)

* Update cpufreq_suspended under cpus_read_lock in cpufreq_resume() for more symmetry with cpufreq_suspend()

* Update changelog

---
 drivers/cpufreq/cpufreq.c |   86 +++++++++++++++++++++++++++++++++-------------
 include/linux/cpufreq.h   |    3 +
 2 files changed, 66 insertions(+), 23 deletions(-)
drivers/cpufreq/cpufreq.c +63 -23
@@ -35,6 +35,9 @@
 static LIST_HEAD(cpufreq_policy_list);
 
 /* Macros to iterate over CPU policies */
+#define for_each_policy(__policy)	\
+	list_for_each_entry(__policy, &cpufreq_policy_list, policy_list)
+
 #define for_each_suitable_policy(__policy, __active)			 \
 	list_for_each_entry(__policy, &cpufreq_policy_list, policy_list) \
 		if ((__active) == !policy_is_inactive(__policy))
@@ -1956,6 +1959,14 @@ int cpufreq_generic_suspend(struct cpufr
 }
 EXPORT_SYMBOL(cpufreq_generic_suspend);
 
+static void cpufreq_suspend_policy(struct cpufreq_policy *policy)
+{
+	guard(cpufreq_policy_write)(policy);
+
+	cpufreq_stop_governor(policy);
+	policy->suspended = true;
+}
+
 /**
  * cpufreq_suspend() - Suspend CPUFreq governors.
  *
@@ -1978,11 +1989,8 @@ void cpufreq_suspend(void)
 	pr_debug("%s: Suspending Governors\n", __func__);
 
 	for_each_active_policy(policy) {
-		if (has_target()) {
-			scoped_guard(cpufreq_policy_write, policy) {
-				cpufreq_stop_governor(policy);
-			}
-		}
+		if (has_target())
+			cpufreq_suspend_policy(policy);
 
 		if (cpufreq_driver->suspend && cpufreq_driver->suspend(policy))
 			pr_err("%s: Failed to suspend driver: %s\n", __func__,
@@ -1994,6 +2002,32 @@ suspend:
 	cpus_read_unlock();
 }
 
+static void cpufreq_resume_policy(struct cpufreq_policy *policy, bool start)
+{
+	guard(cpufreq_policy_write)(policy);
+
+	if (!policy->suspended)
+		return;
+
+	policy->suspended = false;
+
+	if (policy_is_inactive(policy)) {
+		/*
+		 * The policy was active when it was suspended, so its governor
+		 * is still there.  Let the governor go.
+		 */
+		cpufreq_exit_governor(policy);
+		return;
+	}
+
+	if (!start)
+		return;
+
+	if (cpufreq_start_governor(policy))
+		pr_err("%s: Failed to start governor for CPU%u's policy\n",
+		       __func__, policy->cpu);
+}
+
 /**
  * cpufreq_resume() - Resume CPUFreq governors.
  *
@@ -2003,11 +2037,12 @@ suspend:
 void cpufreq_resume(void)
 {
 	struct cpufreq_policy *policy;
-	int ret;
 
 	if (!cpufreq_driver)
 		return;
 
+	guard(cpus_read_lock)();
+
 	if (unlikely(!cpufreq_suspended))
 		return;
 
@@ -2018,19 +2053,18 @@ void cpufreq_resume(void)
 
 	pr_debug("%s: Resuming Governors\n", __func__);
 
-	for_each_active_policy(policy) {
-		if (cpufreq_driver->resume && cpufreq_driver->resume(policy)) {
-			pr_err("%s: Failed to resume driver: %s\n", __func__,
-				cpufreq_driver->name);
-		} else if (has_target()) {
-			scoped_guard(cpufreq_policy_write, policy) {
-				ret = cpufreq_start_governor(policy);
-			}
+	for_each_policy(policy) {
+		int ret = 0;
 
+		if (cpufreq_driver->resume && !policy_is_inactive(policy)) {
+			ret = cpufreq_driver->resume(policy);
 			if (ret)
-				pr_err("%s: Failed to start governor for CPU%u's policy\n",
-				       __func__, policy->cpu);
+				pr_err("%s: Failed to resume driver: %s\n", __func__,
+				       cpufreq_driver->name);
 		}
+
+		if (has_target())
+			cpufreq_resume_policy(policy, ret == 0);
 	}
 }
 
@@ -2412,12 +2446,17 @@ __weak struct cpufreq_governor *cpufreq_
 	return NULL;
 }
 
+static bool cpufreq_is_suspended(struct cpufreq_policy *policy)
+{
+	return cpufreq_suspended || policy->suspended;
+}
+
 static int cpufreq_init_governor(struct cpufreq_policy *policy)
 {
 	int ret;
 
-	/* Don't start any governor operations if we are entering suspend */
-	if (cpufreq_suspended)
+	/* Don't start any governor operations for suspended policies. */
+	if (cpufreq_is_suspended(policy))
 		return 0;
 	/*
 	 * Governor might not be initiated here if ACPI _PPC changed
@@ -2460,7 +2499,7 @@ static int cpufreq_init_governor(struct
 
 static void cpufreq_exit_governor(struct cpufreq_policy *policy)
 {
-	if (cpufreq_suspended || !policy->governor)
+	if (cpufreq_is_suspended(policy) || !policy->governor)
 		return;
 
 	pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
@@ -2475,7 +2514,7 @@ int cpufreq_start_governor(struct cpufre
 {
 	int ret;
 
-	if (cpufreq_suspended)
+	if (cpufreq_is_suspended(policy))
 		return 0;
 
 	if (!policy->governor)
@@ -2499,7 +2538,7 @@ int cpufreq_start_governor(struct cpufre
 
 void cpufreq_stop_governor(struct cpufreq_policy *policy)
 {
-	if (cpufreq_suspended || !policy->governor)
+	if (cpufreq_is_suspended(policy) || !policy->governor)
 		return;
 
 	pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
@@ -2510,7 +2549,7 @@ void cpufreq_stop_governor(struct cpufre
 
 static void cpufreq_governor_limits(struct cpufreq_policy *policy)
 {
-	if (cpufreq_suspended || !policy->governor)
+	if (cpufreq_is_suspended(policy) || !policy->governor)
 		return;
 
 	pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
@@ -2747,7 +2786,8 @@ static void cpufreq_policy_refresh(struc
 	 * -> ask driver for current freq and notify governors about a change
 	 */
 	if (cpufreq_driver->get && has_target() &&
-	    (cpufreq_suspended || WARN_ON(!cpufreq_verify_current_freq(policy, false))))
+	    (cpufreq_is_suspended(policy) ||
+	     WARN_ON(!cpufreq_verify_current_freq(policy, false))))
 		return;
 
 	refresh_frequency_limits(policy);
include/linux/cpufreq.h +3 -0
@@ -149,6 +149,9 @@ struct cpufreq_policy {
 	/* Pending policy->min/max update for the driver */
 	bool			update_limits;
 
+	/* The policy has been suspended. */
+	bool			suspended;
+
 	 /* Cached frequency lookup from cpufreq_driver_resolve_freq. */
 	unsigned int cached_target_freq;
 	unsigned int cached_resolved_idx;
#2 lore raw

PATCH v2 1/3 cpufreq: Improve suspend-resume synchronization

Hello Rafael,

Quoted 16 lines · Rafael J. Wysocki

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

The suspend-resume synchronization in the cpufreq core is not perfect because the global cpufreq_suspended variable is set at the end of cpufreq_suspend() after stopping governors for all of the active policies, so the governor .limits() callback may run via cpufreq_governor_limits() after stopping the governor and before setting cpufreq_suspended which is questionable. Likewise, the governor .limits() callback may run between clearing cpufreq_suspended in cpufreq_resume() and starting the governor.

Address this by adding a per-policy "suspended" flag that is always read under the policy rwsem and is updated under it when the governor is stopped in cpufreq_suspend() and when the governor is restarted in cpufreq_resume().

Quoted 6 lines

Also make cpufreq_resume() use the cpus_read_lock guard to reflect what is done in cpufreq_suspend() and call cpufreq_exit_governor() for policies that were suspended during a system suspend transition and are inactive during the subsequent system resume (to avoid leaking an inactive policy with a stopped governor that has not exited yet).

This seems to be causing regression in our CI [1] (affecting dg1 and dg2). We could not revert the patch cleanly but resetting to its parent heals the issue.

<4> [510.751891] ======================================================
<4> [510.751907] WARNING: possible circular locking dependency detected
<4> [510.751925] 7.4.0-rc3-CI_DRM_19546-gfb078d17b966+ #1 Tainted: G S U L
<4> [510.751946] ------------------------------------------------------
<4> [510.751962] rtcwake/4256 is trying to acquire lock:
<4> [510.751977] ffffffff83ce0f40 (intel_pstate_driver_lock){+.+.}-{3:3}, at: set_power_ctl_ee_state+0x1a/0x90 <4> [510.752022]
but task is already holding lock:
<4> [510.752038] ffffffff83886890 (cpu_hotplug_lock){++++}-{0:0}, at: cpufreq_resume+0x23/0x250 <4> [510.752074]
which lock already depends on the new lock.
<4> [510.752094]
the existing dependency chain (in reverse order) is:
<4> [510.752112]
-> #1 (cpu_hotplug_lock){++++}-{0:0}:

<4> [510.752137]        cpus_read_lock+0x3c/0x100
<4> [510.752157]        cpufreq_register_driver+0xca/0x380
<4> [510.752175]        intel_pstate_register_driver+0x9e/0x1d0
<4> [510.752192]        intel_pstate_init+0x3e3/0xb30
<4> [510.752211]        do_one_initcall+0x59/0x3d0
<4> [510.752229]        kernel_init_freeable+0x3dc/0x6b0
<4> [510.752249]        kernel_init+0x20/0x210
<4> [510.752264]        ret_from_fork+0x3bf/0x470
<4> [510.752282]        ret_from_fork_asm+0x1a/0x30
<4> [510.752299]
-> #0 (intel_pstate_driver_lock){+.+.}-{3:3}:
<4> [510.752325]        __lock_acquire+0x1700/0x29c0
<4> [510.752341]        lock_acquire+0xd9/0x2f0
<4> [510.752355]        __mutex_lock+0xae/0x1030
<4> [510.752372]        mutex_lock_nested+0x1b/0x30
<4> [510.752390]        set_power_ctl_ee_state+0x1a/0x90
<4> [510.752409]        intel_pstate_resume+0x109/0x110
<4> [510.752428]        cpufreq_resume+0x153/0x250
<4> [510.752445]        dpm_resume+0x1f1/0x390
<4> [510.752459]        dpm_resume_end+0x11/0x30
<4> [510.752474]        suspend_devices_and_enter+0x372/0xcb0
<4> [510.752491]        pm_suspend+0x372/0x840
<4> [510.752506]        state_store+0x6e/0xe0
<4> [510.752523]        kobj_attr_store+0x12/0x40
<4> [510.752541]        sysfs_kf_write+0x4d/0x80
<4> [510.752559]        kernfs_fop_write_iter+0x193/0x240
<4> [510.752576]        vfs_write+0x285/0x530
<4> [510.752591]        ksys_write+0x6f/0xf0
<4> [510.752606]        __x64_sys_write+0x19/0x30
<4> [510.752622]        x64_sys_call+0x1285/0x27e0
<4> [510.752637]        do_syscall_64+0xec/0x680
<4> [510.752654]        entry_SYSCALL_64_after_hwframe+0x76/0x7e

Could you please check why the patch causes this regression and provide a fix if necessary?

Regards
Chaitanya

[1] https://intel-gfx-ci.01.org/tree/drm-tip/CI_DRM_19546/shard-dg1-14/igt@[email protected]

--Bisect Logs--
git bisect start
# status: waiting for both good and bad commits
# bad: [cfe67bbcfc37474cbb4482d750d4fba34bbc5a24] drm-tip: 2026y-11m-17d-05h-12m-00s UTC integration manifest
git bisect bad cfe67bbcfc37474cbb4482d750d4fba34bbc5a24
# status: waiting for good commit(s), bad commit known
# good: [39e407b553ec15d4a233b651531ad431ed7542be] drm/sun4i: dw-hdmi: Deinit PHY in bind error path
git bisect good 39e407b553ec15d4a233b651531ad431ed7542be
# bad: [715f145f6204363a50853eb15719dc860db0bc01] drm/i915/dsb: Realign GOSUB only for QW slots 6 and 7
git bisect bad 715f145f6204363a50853eb15719dc860db0bc01
# bad: [7e1392a42ea288d70a266f6553fe06f57f8647ea] Merge tag 'net-7.4-rc3' of git://git.kernel.org/pub/scm/linux/kernel/git/netdev/net
git bisect bad 7e1392a42ea288d70a266f6553fe06f57f8647ea
# good: [0359338fd8cf1a2dc001d0cd69c331c17decee88] af_unix: Inherit SO_INQ in unix_stream_connect()
git bisect good 0359338fd8cf1a2dc001d0cd69c331c17decee88
# good: [79eb6679ef6a0d5ea68da53a8321fb1dc72299b9] Merge tag 'loongarch-fixes-7.4-1' of git://git.kernel.org/pub/scm/linux/kernel/git/chenhuacai/linux-loongson
git bisect good 79eb6679ef6a0d5ea68da53a8321fb1dc72299b9
# bad: [baa6744dc939a927e2c94e5f20aff01ac538aef1] Merge tag 'acpi-7.4-rc3' of git://git.kernel.org/pub/scm/linux/kernel/git/rafael/linux-pm
git bisect bad baa6744dc939a927e2c94e5f20aff01ac538aef1
# good: [2cca8a6f6ae690755862f5b633125119dcf6bd0c] Merge patch series "ns: fixes identifier fixes"
git bisect good 2cca8a6f6ae690755862f5b633125119dcf6bd0c
# bad: [df42809bd599109660bd3f95e2e22330c80993df] Merge branches 'pm-cpufreq' and 'pm-runtime'
git bisect bad df42809bd599109660bd3f95e2e22330c80993df
# bad: [6bc092a0fc4d331a4adbbb664078af149c580834] cpufreq: acpi-cpufreq: fix P-state index mismatch in extract_io()
git bisect bad 6bc092a0fc4d331a4adbbb664078af149c580834
# bad: [02e77af824d5be3a94fdd885835a352f90d24c5c] cpufreq: intel_pstate: Clear last written P-state value on suspend
git bisect bad 02e77af824d5be3a94fdd885835a352f90d24c5c
# bad: [eee2a44e4e2b41c0699919a550d41cc38daa4f42] cpufreq: Improve suspend-resume synchronization
git bisect bad eee2a44e4e2b41c0699919a550d41cc38daa4f42
# good: [e6f50e602e373583423654ec000cad8b8d08f7bc] cpufreq: Hold the policy write lock when changing boost state
git bisect good e6f50e602e373583423654ec000cad8b8d08f7bc

# first bad commit: [eee2a44e4e2b41c0699919a550d41cc38daa4f42] cpufreq: Improve suspend-resume synchronization
Quoted 223 lines
Fixes 8e30444e1530 ("cpufreq: fix cpufreq suspend/resume for intel_pstate")
Fixes b1b12babe3b7 ("cpufreq: update 'cpufreq_suspended' after stopping governors")
Cc All applicable <[email protected]>

---

v1 -> v2:
* Retain all of the cpufreq_suspended checks to avoid carrying out governor operations after suspending cpufreq during system shutdown and during hibernation resume on systems with SMT (Sashiko)

* Rearrange cpufreq_resume() to walk all cpufreq policies and call cpufreq_resume_policy() for all of them

* Call cpufreq_exit_governor() in cpufreq_resume_policy() for policies that were suspended during a system suspend transition and are inactive during the subsequent system resume (Sashiko)

* Update cpufreq_suspended under cpus_read_lock in cpufreq_resume() for more symmetry with cpufreq_suspend()

* Update changelog

---
  drivers/cpufreq/cpufreq.c |   86 +++++++++++++++++++++++++++++++++-------------
  include/linux/cpufreq.h   |    3 +
  2 files changed, 66 insertions(+), 23 deletions(-)
drivers/cpufreq/cpufreq.c +63 -23
@@ -35,6 +35,9 @@
  static LIST_HEAD(cpufreq_policy_list);
 
  /* Macros to iterate over CPU policies */
+#define for_each_policy(__policy)	\
+	list_for_each_entry(__policy, &cpufreq_policy_list, policy_list)
+
  #define for_each_suitable_policy(__policy, __active)			 \
  	list_for_each_entry(__policy, &cpufreq_policy_list, policy_list) \
  		if ((__active) == !policy_is_inactive(__policy))
@@ -1956,6 +1959,14 @@ int cpufreq_generic_suspend(struct cpufr
  }
  EXPORT_SYMBOL(cpufreq_generic_suspend);
 
+static void cpufreq_suspend_policy(struct cpufreq_policy *policy)
+{
+	guard(cpufreq_policy_write)(policy);
+
+	cpufreq_stop_governor(policy);
+	policy->suspended = true;
+}
+
  /**
   * cpufreq_suspend() - Suspend CPUFreq governors.
   *
@@ -1978,11 +1989,8 @@ void cpufreq_suspend(void)
  	pr_debug("%s: Suspending Governors\n", __func__);
 
  	for_each_active_policy(policy) {
-		if (has_target()) {
-			scoped_guard(cpufreq_policy_write, policy) {
-				cpufreq_stop_governor(policy);
-			}
-		}
+		if (has_target())
+			cpufreq_suspend_policy(policy);
 
  		if (cpufreq_driver->suspend && cpufreq_driver->suspend(policy))
  			pr_err("%s: Failed to suspend driver: %s\n", __func__,
@@ -1994,6 +2002,32 @@ suspend:
  	cpus_read_unlock();
  }
 
+static void cpufreq_resume_policy(struct cpufreq_policy *policy, bool start)
+{
+	guard(cpufreq_policy_write)(policy);
+
+	if (!policy->suspended)
+		return;
+
+	policy->suspended = false;
+
+	if (policy_is_inactive(policy)) {
+		/*
+		 * The policy was active when it was suspended, so its governor
+		 * is still there.  Let the governor go.
+		 */
+		cpufreq_exit_governor(policy);
+		return;
+	}
+
+	if (!start)
+		return;
+
+	if (cpufreq_start_governor(policy))
+		pr_err("%s: Failed to start governor for CPU%u's policy\n",
+		       __func__, policy->cpu);
+}
+
  /**
   * cpufreq_resume() - Resume CPUFreq governors.
   *
@@ -2003,11 +2037,12 @@ suspend:
  void cpufreq_resume(void)
  {
  	struct cpufreq_policy *policy;
-	int ret;
 
  	if (!cpufreq_driver)
  		return;
 
+	guard(cpus_read_lock)();
+
  	if (unlikely(!cpufreq_suspended))
  		return;
 
@@ -2018,19 +2053,18 @@ void cpufreq_resume(void)
 
  	pr_debug("%s: Resuming Governors\n", __func__);
 
-	for_each_active_policy(policy) {
-		if (cpufreq_driver->resume && cpufreq_driver->resume(policy)) {
-			pr_err("%s: Failed to resume driver: %s\n", __func__,
-				cpufreq_driver->name);
-		} else if (has_target()) {
-			scoped_guard(cpufreq_policy_write, policy) {
-				ret = cpufreq_start_governor(policy);
-			}
+	for_each_policy(policy) {
+		int ret = 0;
 
+		if (cpufreq_driver->resume && !policy_is_inactive(policy)) {
+			ret = cpufreq_driver->resume(policy);
  			if (ret)
-				pr_err("%s: Failed to start governor for CPU%u's policy\n",
-				       __func__, policy->cpu);
+				pr_err("%s: Failed to resume driver: %s\n", __func__,
+				       cpufreq_driver->name);
  		}
+
+		if (has_target())
+			cpufreq_resume_policy(policy, ret == 0);
  	}
  }
 
@@ -2412,12 +2446,17 @@ __weak struct cpufreq_governor *cpufreq_
  	return NULL;
  }
 
+static bool cpufreq_is_suspended(struct cpufreq_policy *policy)
+{
+	return cpufreq_suspended || policy->suspended;
+}
+
  static int cpufreq_init_governor(struct cpufreq_policy *policy)
  {
  	int ret;
 
-	/* Don't start any governor operations if we are entering suspend */
-	if (cpufreq_suspended)
+	/* Don't start any governor operations for suspended policies. */
+	if (cpufreq_is_suspended(policy))
  		return 0;
  	/*
  	 * Governor might not be initiated here if ACPI _PPC changed
@@ -2460,7 +2499,7 @@ static int cpufreq_init_governor(struct
 
  static void cpufreq_exit_governor(struct cpufreq_policy *policy)
  {
-	if (cpufreq_suspended || !policy->governor)
+	if (cpufreq_is_suspended(policy) || !policy->governor)
  		return;
 
  	pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
@@ -2475,7 +2514,7 @@ int cpufreq_start_governor(struct cpufre
  {
  	int ret;
 
-	if (cpufreq_suspended)
+	if (cpufreq_is_suspended(policy))
  		return 0;
 
  	if (!policy->governor)
@@ -2499,7 +2538,7 @@ int cpufreq_start_governor(struct cpufre
 
  void cpufreq_stop_governor(struct cpufreq_policy *policy)
  {
-	if (cpufreq_suspended || !policy->governor)
+	if (cpufreq_is_suspended(policy) || !policy->governor)
  		return;
 
  	pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
@@ -2510,7 +2549,7 @@ void cpufreq_stop_governor(struct cpufre
 
  static void cpufreq_governor_limits(struct cpufreq_policy *policy)
  {
-	if (cpufreq_suspended || !policy->governor)
+	if (cpufreq_is_suspended(policy) || !policy->governor)
  		return;
 
  	pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
@@ -2747,7 +2786,8 @@ static void cpufreq_policy_refresh(struc
  	 * -> ask driver for current freq and notify governors about a change
  	 */
  	if (cpufreq_driver->get && has_target() &&
-	    (cpufreq_suspended || WARN_ON(!cpufreq_verify_current_freq(policy, false))))
+	    (cpufreq_is_suspended(policy) ||
+	     WARN_ON(!cpufreq_verify_current_freq(policy, false))))
  		return;
 
  	refresh_frequency_limits(policy);
include/linux/cpufreq.h +3 -0
@@ -149,6 +149,9 @@ struct cpufreq_policy {
  	/* Pending policy->min/max update for the driver */
  	bool			update_limits;
 
+	/* The policy has been suspended. */
+	bool			suspended;
+
  	 /* Cached frequency lookup from cpufreq_driver_resolve_freq. */
  	unsigned int cached_target_freq;
  	unsigned int cached_resolved_idx;
author maintainer
#3 lore raw

Re: [REGRESSION: [PATCH v2 1/3] cpufreq: Improve suspend-resume synchronization

Quoted 83 lines · Chaitanya Kumar

Hello Rafael,

Quoted 16 lines · Rafael J. Wysocki

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

The suspend-resume synchronization in the cpufreq core is not perfect because the global cpufreq_suspended variable is set at the end of cpufreq_suspend() after stopping governors for all of the active policies, so the governor .limits() callback may run via cpufreq_governor_limits() after stopping the governor and before setting cpufreq_suspended which is questionable. Likewise, the governor .limits() callback may run between clearing cpufreq_suspended in cpufreq_resume() and starting the governor.

Address this by adding a per-policy "suspended" flag that is always read under the policy rwsem and is updated under it when the governor is stopped in cpufreq_suspend() and when the governor is restarted in cpufreq_resume().

Quoted 6 lines

Also make cpufreq_resume() use the cpus_read_lock guard to reflect what is done in cpufreq_suspend() and call cpufreq_exit_governor() for policies that were suspended during a system suspend transition and are inactive during the subsequent system resume (to avoid leaking an inactive policy with a stopped governor that has not exited yet).

This seems to be causing regression in our CI [1] (affecting dg1 and dg2). We could not revert the patch cleanly but resetting to its parent heals the issue.

<4> [510.751891] ====================================================== <4> [510.751907] WARNING: possible circular locking dependency detected <4> [510.751925] 7.4.0-rc3-CI_DRM_19546-gfb078d17b966+ #1 Tainted: G S
U L
<4> [510.751946] ------------------------------------------------------ <4> [510.751962] rtcwake/4256 is trying to acquire lock:
<4> [510.751977] ffffffff83ce0f40 (intel_pstate_driver_lock){+.+.}-{3:3}, at: set_power_ctl_ee_state+0x1a/0x90 <4> [510.752022]
but task is already holding lock:
<4> [510.752038] ffffffff83886890 (cpu_hotplug_lock){++++}-{0:0}, at:
cpufreq_resume+0x23/0x250
<4> [510.752074]
which lock already depends on the new lock.
<4> [510.752094]
the existing dependency chain (in reverse order) is:
<4> [510.752112]
-> #1 (cpu_hotplug_lock){++++}-{0:0}:

<4> [510.752137]        cpus_read_lock+0x3c/0x100
<4> [510.752157]        cpufreq_register_driver+0xca/0x380
<4> [510.752175]        intel_pstate_register_driver+0x9e/0x1d0
<4> [510.752192]        intel_pstate_init+0x3e3/0xb30
<4> [510.752211]        do_one_initcall+0x59/0x3d0
<4> [510.752229]        kernel_init_freeable+0x3dc/0x6b0
<4> [510.752249]        kernel_init+0x20/0x210
<4> [510.752264]        ret_from_fork+0x3bf/0x470
<4> [510.752282]        ret_from_fork_asm+0x1a/0x30
<4> [510.752299]
-> #0 (intel_pstate_driver_lock){+.+.}-{3:3}:
<4> [510.752325]        __lock_acquire+0x1700/0x29c0
<4> [510.752341]        lock_acquire+0xd9/0x2f0
<4> [510.752355]        __mutex_lock+0xae/0x1030
<4> [510.752372]        mutex_lock_nested+0x1b/0x30
<4> [510.752390]        set_power_ctl_ee_state+0x1a/0x90
<4> [510.752409]        intel_pstate_resume+0x109/0x110
<4> [510.752428]        cpufreq_resume+0x153/0x250
<4> [510.752445]        dpm_resume+0x1f1/0x390
<4> [510.752459]        dpm_resume_end+0x11/0x30
<4> [510.752474]        suspend_devices_and_enter+0x372/0xcb0
<4> [510.752491]        pm_suspend+0x372/0x840
<4> [510.752506]        state_store+0x6e/0xe0
<4> [510.752523]        kobj_attr_store+0x12/0x40
<4> [510.752541]        sysfs_kf_write+0x4d/0x80
<4> [510.752559]        kernfs_fop_write_iter+0x193/0x240
<4> [510.752576]        vfs_write+0x285/0x530
<4> [510.752591]        ksys_write+0x6f/0xf0
<4> [510.752606]        __x64_sys_write+0x19/0x30
<4> [510.752622]        x64_sys_call+0x1285/0x27e0
<4> [510.752637]        do_syscall_64+0xec/0x680
<4> [510.752654]        entry_SYSCALL_64_after_hwframe+0x76/0x7e
Quoted 2 lines

Could you please check why the patch causes this regression and provide a fix if necessary?

That's because set_power_ctl_ee_state() acquires intel_pstate_driver_lock which is held around cpu_hotplug_lock during driver mode changes, so adding cpu_hotplug_lock around intel_pstate_resume(), which calls set_power_ctl_ee_state(), causes lockdep to complain. Note that the deadlock is not real, but set_power_ctl_ee_state() need not use that particular lock either AFAICS.

The attached patch should address this.

Quoted 268 lines

[1]
https://intel-gfx-ci.01.org/tree/drm-tip/CI_DRM_19546/shard-dg1-14/igt@[email protected]

--Bisect Logs--
git bisect start
# status: waiting for both good and bad commits
# bad: [cfe67bbcfc37474cbb4482d750d4fba34bbc5a24] drm-tip:
2026y-11m-17d-05h-12m-00s UTC integration manifest
git bisect bad cfe67bbcfc37474cbb4482d750d4fba34bbc5a24
# status: waiting for good commit(s), bad commit known
# good: [39e407b553ec15d4a233b651531ad431ed7542be] drm/sun4i: dw-hdmi:
Deinit PHY in bind error path
git bisect good 39e407b553ec15d4a233b651531ad431ed7542be
# bad: [715f145f6204363a50853eb15719dc860db0bc01] drm/i915/dsb: Realign GOSUB only for QW slots 6 and 7
git bisect bad 715f145f6204363a50853eb15719dc860db0bc01
# bad: [7e1392a42ea288d70a266f6553fe06f57f8647ea] Merge tag 'net-7.4-rc3' of git://git.kernel.org/pub/scm/linux/kernel/git/netdev/net
git bisect bad 7e1392a42ea288d70a266f6553fe06f57f8647ea
# good: [0359338fd8cf1a2dc001d0cd69c331c17decee88] af_unix: Inherit SO_INQ in unix_stream_connect()
git bisect good 0359338fd8cf1a2dc001d0cd69c331c17decee88
# good: [79eb6679ef6a0d5ea68da53a8321fb1dc72299b9] Merge tag 'loongarch-fixes-7.4-1' of
git://git.kernel.org/pub/scm/linux/kernel/git/chenhuacai/linux-loongson
git bisect good 79eb6679ef6a0d5ea68da53a8321fb1dc72299b9
# bad: [baa6744dc939a927e2c94e5f20aff01ac538aef1] Merge tag 'acpi-7.4-rc3' of
git://git.kernel.org/pub/scm/linux/kernel/git/rafael/linux-pm
git bisect bad baa6744dc939a927e2c94e5f20aff01ac538aef1
# good: [2cca8a6f6ae690755862f5b633125119dcf6bd0c] Merge patch series "ns: fixes identifier fixes"
git bisect good 2cca8a6f6ae690755862f5b633125119dcf6bd0c
# bad: [df42809bd599109660bd3f95e2e22330c80993df] Merge branches 'pm-cpufreq' and 'pm-runtime'
git bisect bad df42809bd599109660bd3f95e2e22330c80993df
# bad: [6bc092a0fc4d331a4adbbb664078af149c580834] cpufreq: acpi-cpufreq:
fix P-state index mismatch in extract_io()
git bisect bad 6bc092a0fc4d331a4adbbb664078af149c580834
# bad: [02e77af824d5be3a94fdd885835a352f90d24c5c] cpufreq: intel_pstate:
Clear last written P-state value on suspend
git bisect bad 02e77af824d5be3a94fdd885835a352f90d24c5c
# bad: [eee2a44e4e2b41c0699919a550d41cc38daa4f42] cpufreq: Improve suspend-resume synchronization
git bisect bad eee2a44e4e2b41c0699919a550d41cc38daa4f42
# good: [e6f50e602e373583423654ec000cad8b8d08f7bc] cpufreq: Hold the policy write lock when changing boost state
git bisect good e6f50e602e373583423654ec000cad8b8d08f7bc

# first bad commit: [eee2a44e4e2b41c0699919a550d41cc38daa4f42] cpufreq:
Improve suspend-resume synchronization
Quoted 217 lines
Fixes 8e30444e1530 ("cpufreq: fix cpufreq suspend/resume for intel_pstate")
Fixes b1b12babe3b7 ("cpufreq: update 'cpufreq_suspended' after stopping governors")
Cc All applicable <[email protected]>

---

v1 -> v2:
* Retain all of the cpufreq_suspended checks to avoid carrying out governor operations after suspending cpufreq during system shutdown and during hibernation resume on systems with SMT (Sashiko)

* Rearrange cpufreq_resume() to walk all cpufreq policies and call cpufreq_resume_policy() for all of them

* Call cpufreq_exit_governor() in cpufreq_resume_policy() for policies that were suspended during a system suspend transition and are inactive during the subsequent system resume (Sashiko)

* Update cpufreq_suspended under cpus_read_lock in cpufreq_resume() for more symmetry with cpufreq_suspend()

* Update changelog

---
  drivers/cpufreq/cpufreq.c |   86 +++++++++++++++++++++++++++++++++-------------
  include/linux/cpufreq.h   |    3 +
  2 files changed, 66 insertions(+), 23 deletions(-)
drivers/cpufreq/cpufreq.c +63 -23
@@ -35,6 +35,9 @@
  static LIST_HEAD(cpufreq_policy_list);
 
  /* Macros to iterate over CPU policies */
+#define for_each_policy(__policy)    \
+     list_for_each_entry(__policy, &cpufreq_policy_list, policy_list)
+
  #define for_each_suitable_policy(__policy, __active)                         \
      list_for_each_entry(__policy, &cpufreq_policy_list, policy_list) \
              if ((__active) == !policy_is_inactive(__policy))
@@ -1956,6 +1959,14 @@ int cpufreq_generic_suspend(struct cpufr
  }
  EXPORT_SYMBOL(cpufreq_generic_suspend);
 
+static void cpufreq_suspend_policy(struct cpufreq_policy *policy)
+{
+     guard(cpufreq_policy_write)(policy);
+
+     cpufreq_stop_governor(policy);
+     policy->suspended = true;
+}
+
  /**
   * cpufreq_suspend() - Suspend CPUFreq governors.
   *
@@ -1978,11 +1989,8 @@ void cpufreq_suspend(void)
      pr_debug("%s: Suspending Governors\n", __func__);
 
      for_each_active_policy(policy) {
-             if (has_target()) {
-                     scoped_guard(cpufreq_policy_write, policy) {
-                             cpufreq_stop_governor(policy);
-                     }
-             }
+             if (has_target())
+                     cpufreq_suspend_policy(policy);
 
              if (cpufreq_driver->suspend && cpufreq_driver->suspend(policy))
                      pr_err("%s: Failed to suspend driver: %s\n", __func__,
@@ -1994,6 +2002,32 @@ suspend:
      cpus_read_unlock();
  }
 
+static void cpufreq_resume_policy(struct cpufreq_policy *policy, bool start)
+{
+     guard(cpufreq_policy_write)(policy);
+
+     if (!policy->suspended)
+             return;
+
+     policy->suspended = false;
+
+     if (policy_is_inactive(policy)) {
+             /*
+              * The policy was active when it was suspended, so its governor
+              * is still there.  Let the governor go.
+              */
+             cpufreq_exit_governor(policy);
+             return;
+     }
+
+     if (!start)
+             return;
+
+     if (cpufreq_start_governor(policy))
+             pr_err("%s: Failed to start governor for CPU%u's policy\n",
+                    __func__, policy->cpu);
+}
+
  /**
   * cpufreq_resume() - Resume CPUFreq governors.
   *
@@ -2003,11 +2037,12 @@ suspend:
  void cpufreq_resume(void)
  {
      struct cpufreq_policy *policy;
-     int ret;
 
      if (!cpufreq_driver)
              return;
 
+     guard(cpus_read_lock)();
+
      if (unlikely(!cpufreq_suspended))
              return;
 
@@ -2018,19 +2053,18 @@ void cpufreq_resume(void)
 
      pr_debug("%s: Resuming Governors\n", __func__);
 
-     for_each_active_policy(policy) {
-             if (cpufreq_driver->resume && cpufreq_driver->resume(policy)) {
-                     pr_err("%s: Failed to resume driver: %s\n", __func__,
-                             cpufreq_driver->name);
-             } else if (has_target()) {
-                     scoped_guard(cpufreq_policy_write, policy) {
-                             ret = cpufreq_start_governor(policy);
-                     }
+     for_each_policy(policy) {
+             int ret = 0;
 
+             if (cpufreq_driver->resume && !policy_is_inactive(policy)) {
+                     ret = cpufreq_driver->resume(policy);
                      if (ret)
-                             pr_err("%s: Failed to start governor for CPU%u's policy\n",
-                                    __func__, policy->cpu);
+                             pr_err("%s: Failed to resume driver: %s\n", __func__,
+                                    cpufreq_driver->name);
              }
+
+             if (has_target())
+                     cpufreq_resume_policy(policy, ret == 0);
      }
  }
 
@@ -2412,12 +2446,17 @@ __weak struct cpufreq_governor *cpufreq_
      return NULL;
  }
 
+static bool cpufreq_is_suspended(struct cpufreq_policy *policy)
+{
+     return cpufreq_suspended || policy->suspended;
+}
+
  static int cpufreq_init_governor(struct cpufreq_policy *policy)
  {
      int ret;
 
-     /* Don't start any governor operations if we are entering suspend */
-     if (cpufreq_suspended)
+     /* Don't start any governor operations for suspended policies. */
+     if (cpufreq_is_suspended(policy))
              return 0;
      /*
       * Governor might not be initiated here if ACPI _PPC changed
@@ -2460,7 +2499,7 @@ static int cpufreq_init_governor(struct
 
  static void cpufreq_exit_governor(struct cpufreq_policy *policy)
  {
-     if (cpufreq_suspended || !policy->governor)
+     if (cpufreq_is_suspended(policy) || !policy->governor)
              return;
 
      pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
@@ -2475,7 +2514,7 @@ int cpufreq_start_governor(struct cpufre
  {
      int ret;
 
-     if (cpufreq_suspended)
+     if (cpufreq_is_suspended(policy))
              return 0;
 
      if (!policy->governor)
@@ -2499,7 +2538,7 @@ int cpufreq_start_governor(struct cpufre
 
  void cpufreq_stop_governor(struct cpufreq_policy *policy)
  {
-     if (cpufreq_suspended || !policy->governor)
+     if (cpufreq_is_suspended(policy) || !policy->governor)
              return;
 
      pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
@@ -2510,7 +2549,7 @@ void cpufreq_stop_governor(struct cpufre
 
  static void cpufreq_governor_limits(struct cpufreq_policy *policy)
  {
-     if (cpufreq_suspended || !policy->governor)
+     if (cpufreq_is_suspended(policy) || !policy->governor)
              return;
 
      pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
@@ -2747,7 +2786,8 @@ static void cpufreq_policy_refresh(struc
       * -> ask driver for current freq and notify governors about a change
       */
      if (cpufreq_driver->get && has_target() &&
-         (cpufreq_suspended || WARN_ON(!cpufreq_verify_current_freq(policy, false))))
+         (cpufreq_is_suspended(policy) ||
+          WARN_ON(!cpufreq_verify_current_freq(policy, false))))
              return;
 
      refresh_frequency_limits(policy);
include/linux/cpufreq.h +0 -0
@@ -149,6 +149,9 @@ struct cpufreq_policy {
      /* Pending policy->min/max update for the driver */
      bool                    update_limits;
 
Quoted 8 lines

include/linux/cpufreq.h struct cpufreq_policy { · line 157

Quoted 6 lines
include/linux/cpufreq.h +3 -0
+     /* The policy has been suspended. */
+     bool                    suspended;
+
       /* Cached frequency lookup from cpufreq_driver_resolve_freq. */
      unsigned int cached_target_freq;
      unsigned int cached_resolved_idx;
---
 drivers/cpufreq/intel_pstate.c |   24 ++++++++++++++++--------
 1 file changed, 16 insertions(+), 8 deletions(-)
drivers/cpufreq/intel_pstate.c +16 -8
@@ -1352,13 +1352,12 @@ static void intel_pstate_hwp_offline(str
 #define POWER_CTL_DEC_ENABLE	27
 
 static int power_ctl_ee_state;
+static DEFINE_MUTEX(power_ctl_ee_state_lock);
 
-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);
-
 	rdmsrq(MSR_IA32_POWER_CTL, power_ctl);
 	if (input) {
 		power_ctl &= ~BIT(MSR_IA32_POWER_CTL_BIT_EE);
@@ -1370,6 +1369,13 @@ static void set_power_ctl_ee_state(bool
 	wrmsrq(MSR_IA32_POWER_CTL, power_ctl);
 }
 
+static void set_power_ctl_ee_state(bool enable)
+{
+	guard(mutex)(&power_ctl_ee_state_lock);
+
+	__set_power_ctl_ee_state(enable);
+}
+
 static void intel_pstate_hwp_enable(struct cpudata *cpudata);
 
 static void intel_pstate_hwp_reenable(struct cpudata *cpu)
@@ -1398,11 +1404,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_ee_state_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);
#4 lore raw

Re: [REGRESSION: [PATCH v2 1/3] cpufreq: Improve suspend-resume synchronization

Quoted 96 lines · Rafael J. Wysocki (Intel)
Quoted 83 lines · Chaitanya Kumar

Hello Rafael,

Quoted 16 lines · Rafael J. Wysocki

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

The suspend-resume synchronization in the cpufreq core is not perfect because the global cpufreq_suspended variable is set at the end of cpufreq_suspend() after stopping governors for all of the active policies, so the governor .limits() callback may run via cpufreq_governor_limits() after stopping the governor and before setting cpufreq_suspended which is questionable. Likewise, the governor .limits() callback may run between clearing cpufreq_suspended in cpufreq_resume() and starting the governor.

Address this by adding a per-policy "suspended" flag that is always read under the policy rwsem and is updated under it when the governor is stopped in cpufreq_suspend() and when the governor is restarted in cpufreq_resume().

Quoted 6 lines

Also make cpufreq_resume() use the cpus_read_lock guard to reflect what is done in cpufreq_suspend() and call cpufreq_exit_governor() for policies that were suspended during a system suspend transition and are inactive during the subsequent system resume (to avoid leaking an inactive policy with a stopped governor that has not exited yet).

This seems to be causing regression in our CI [1] (affecting dg1 and dg2). We could not revert the patch cleanly but resetting to its parent heals the issue.

<4> [510.751891] ====================================================== <4> [510.751907] WARNING: possible circular locking dependency detected <4> [510.751925] 7.4.0-rc3-CI_DRM_19546-gfb078d17b966+ #1 Tainted: G S
U L
<4> [510.751946] ------------------------------------------------------ <4> [510.751962] rtcwake/4256 is trying to acquire lock:
<4> [510.751977] ffffffff83ce0f40 (intel_pstate_driver_lock){+.+.}-{3:3}, at: set_power_ctl_ee_state+0x1a/0x90 <4> [510.752022]
but task is already holding lock:
<4> [510.752038] ffffffff83886890 (cpu_hotplug_lock){++++}-{0:0}, at:
cpufreq_resume+0x23/0x250
<4> [510.752074]
which lock already depends on the new lock.
<4> [510.752094]
the existing dependency chain (in reverse order) is:
<4> [510.752112]
-> #1 (cpu_hotplug_lock){++++}-{0:0}:

<4> [510.752137]        cpus_read_lock+0x3c/0x100
<4> [510.752157]        cpufreq_register_driver+0xca/0x380
<4> [510.752175]        intel_pstate_register_driver+0x9e/0x1d0
<4> [510.752192]        intel_pstate_init+0x3e3/0xb30
<4> [510.752211]        do_one_initcall+0x59/0x3d0
<4> [510.752229]        kernel_init_freeable+0x3dc/0x6b0
<4> [510.752249]        kernel_init+0x20/0x210
<4> [510.752264]        ret_from_fork+0x3bf/0x470
<4> [510.752282]        ret_from_fork_asm+0x1a/0x30
<4> [510.752299]
-> #0 (intel_pstate_driver_lock){+.+.}-{3:3}:
<4> [510.752325]        __lock_acquire+0x1700/0x29c0
<4> [510.752341]        lock_acquire+0xd9/0x2f0
<4> [510.752355]        __mutex_lock+0xae/0x1030
<4> [510.752372]        mutex_lock_nested+0x1b/0x30
<4> [510.752390]        set_power_ctl_ee_state+0x1a/0x90
<4> [510.752409]        intel_pstate_resume+0x109/0x110
<4> [510.752428]        cpufreq_resume+0x153/0x250
<4> [510.752445]        dpm_resume+0x1f1/0x390
<4> [510.752459]        dpm_resume_end+0x11/0x30
<4> [510.752474]        suspend_devices_and_enter+0x372/0xcb0
<4> [510.752491]        pm_suspend+0x372/0x840
<4> [510.752506]        state_store+0x6e/0xe0
<4> [510.752523]        kobj_attr_store+0x12/0x40
<4> [510.752541]        sysfs_kf_write+0x4d/0x80
<4> [510.752559]        kernfs_fop_write_iter+0x193/0x240
<4> [510.752576]        vfs_write+0x285/0x530
<4> [510.752591]        ksys_write+0x6f/0xf0
<4> [510.752606]        __x64_sys_write+0x19/0x30
<4> [510.752622]        x64_sys_call+0x1285/0x27e0
<4> [510.752637]        do_syscall_64+0xec/0x680
<4> [510.752654]        entry_SYSCALL_64_after_hwframe+0x76/0x7e
Quoted 2 lines

Could you please check why the patch causes this regression and provide a fix if necessary?

That's because set_power_ctl_ee_state() acquires intel_pstate_driver_lock which is held around cpu_hotplug_lock during driver mode changes, so adding cpu_hotplug_lock around intel_pstate_resume(), which calls set_power_ctl_ee_state(), causes lockdep to complain. Note that the deadlock is not real, but set_power_ctl_ee_state() need not use that particular lock either AFAICS.

Quoted 1 line

The attached patch should address this.

This works. Thank you for the patch.
I hope you would be floating it soon.

==
Chaitanya

Quoted 277 lines
Quoted 277 lines

[1]
https://intel-gfx-ci.01.org/tree/drm-tip/CI_DRM_19546/shard-dg1-14/igt@[email protected]

--Bisect Logs--
git bisect start
# status: waiting for both good and bad commits
# bad: [cfe67bbcfc37474cbb4482d750d4fba34bbc5a24] drm-tip:
2026y-11m-17d-05h-12m-00s UTC integration manifest
git bisect bad cfe67bbcfc37474cbb4482d750d4fba34bbc5a24
# status: waiting for good commit(s), bad commit known
# good: [39e407b553ec15d4a233b651531ad431ed7542be] drm/sun4i: dw-hdmi:
Deinit PHY in bind error path
git bisect good 39e407b553ec15d4a233b651531ad431ed7542be
# bad: [715f145f6204363a50853eb15719dc860db0bc01] drm/i915/dsb: Realign GOSUB only for QW slots 6 and 7
git bisect bad 715f145f6204363a50853eb15719dc860db0bc01
# bad: [7e1392a42ea288d70a266f6553fe06f57f8647ea] Merge tag 'net-7.4-rc3' of git://git.kernel.org/pub/scm/linux/kernel/git/netdev/net
git bisect bad 7e1392a42ea288d70a266f6553fe06f57f8647ea
# good: [0359338fd8cf1a2dc001d0cd69c331c17decee88] af_unix: Inherit SO_INQ in unix_stream_connect()
git bisect good 0359338fd8cf1a2dc001d0cd69c331c17decee88
# good: [79eb6679ef6a0d5ea68da53a8321fb1dc72299b9] Merge tag 'loongarch-fixes-7.4-1' of
git://git.kernel.org/pub/scm/linux/kernel/git/chenhuacai/linux-loongson
git bisect good 79eb6679ef6a0d5ea68da53a8321fb1dc72299b9
# bad: [baa6744dc939a927e2c94e5f20aff01ac538aef1] Merge tag 'acpi-7.4-rc3' of
git://git.kernel.org/pub/scm/linux/kernel/git/rafael/linux-pm
git bisect bad baa6744dc939a927e2c94e5f20aff01ac538aef1
# good: [2cca8a6f6ae690755862f5b633125119dcf6bd0c] Merge patch series "ns: fixes identifier fixes"
git bisect good 2cca8a6f6ae690755862f5b633125119dcf6bd0c
# bad: [df42809bd599109660bd3f95e2e22330c80993df] Merge branches 'pm-cpufreq' and 'pm-runtime'
git bisect bad df42809bd599109660bd3f95e2e22330c80993df
# bad: [6bc092a0fc4d331a4adbbb664078af149c580834] cpufreq: acpi-cpufreq:
fix P-state index mismatch in extract_io()
git bisect bad 6bc092a0fc4d331a4adbbb664078af149c580834
# bad: [02e77af824d5be3a94fdd885835a352f90d24c5c] cpufreq: intel_pstate:
Clear last written P-state value on suspend
git bisect bad 02e77af824d5be3a94fdd885835a352f90d24c5c
# bad: [eee2a44e4e2b41c0699919a550d41cc38daa4f42] cpufreq: Improve suspend-resume synchronization
git bisect bad eee2a44e4e2b41c0699919a550d41cc38daa4f42
# good: [e6f50e602e373583423654ec000cad8b8d08f7bc] cpufreq: Hold the policy write lock when changing boost state
git bisect good e6f50e602e373583423654ec000cad8b8d08f7bc

# first bad commit: [eee2a44e4e2b41c0699919a550d41cc38daa4f42] cpufreq:
Improve suspend-resume synchronization
Quoted 223 lines
Fixes 8e30444e1530 ("cpufreq: fix cpufreq suspend/resume for intel_pstate")
Fixes b1b12babe3b7 ("cpufreq: update 'cpufreq_suspended' after stopping governors")
Cc All applicable <[email protected]>

---

v1 -> v2:
* Retain all of the cpufreq_suspended checks to avoid carrying out governor operations after suspending cpufreq during system shutdown and during hibernation resume on systems with SMT (Sashiko)

* Rearrange cpufreq_resume() to walk all cpufreq policies and call cpufreq_resume_policy() for all of them

* Call cpufreq_exit_governor() in cpufreq_resume_policy() for policies that were suspended during a system suspend transition and are inactive during the subsequent system resume (Sashiko)

* Update cpufreq_suspended under cpus_read_lock in cpufreq_resume() for more symmetry with cpufreq_suspend()

* Update changelog

---
   drivers/cpufreq/cpufreq.c |   86 +++++++++++++++++++++++++++++++++-------------
   include/linux/cpufreq.h   |    3 +
   2 files changed, 66 insertions(+), 23 deletions(-)
drivers/cpufreq/cpufreq.c +63 -23
@@ -35,6 +35,9 @@
   static LIST_HEAD(cpufreq_policy_list);
 
   /* Macros to iterate over CPU policies */
+#define for_each_policy(__policy)    \
+     list_for_each_entry(__policy, &cpufreq_policy_list, policy_list)
+
   #define for_each_suitable_policy(__policy, __active)                         \
       list_for_each_entry(__policy, &cpufreq_policy_list, policy_list) \
               if ((__active) == !policy_is_inactive(__policy))
@@ -1956,6 +1959,14 @@ int cpufreq_generic_suspend(struct cpufr
   }
   EXPORT_SYMBOL(cpufreq_generic_suspend);
 
+static void cpufreq_suspend_policy(struct cpufreq_policy *policy)
+{
+     guard(cpufreq_policy_write)(policy);
+
+     cpufreq_stop_governor(policy);
+     policy->suspended = true;
+}
+
   /**
    * cpufreq_suspend() - Suspend CPUFreq governors.
    *
@@ -1978,11 +1989,8 @@ void cpufreq_suspend(void)
       pr_debug("%s: Suspending Governors\n", __func__);
 
       for_each_active_policy(policy) {
-             if (has_target()) {
-                     scoped_guard(cpufreq_policy_write, policy) {
-                             cpufreq_stop_governor(policy);
-                     }
-             }
+             if (has_target())
+                     cpufreq_suspend_policy(policy);
 
               if (cpufreq_driver->suspend && cpufreq_driver->suspend(policy))
                       pr_err("%s: Failed to suspend driver: %s\n", __func__,
@@ -1994,6 +2002,32 @@ suspend:
       cpus_read_unlock();
   }
 
+static void cpufreq_resume_policy(struct cpufreq_policy *policy, bool start)
+{
+     guard(cpufreq_policy_write)(policy);
+
+     if (!policy->suspended)
+             return;
+
+     policy->suspended = false;
+
+     if (policy_is_inactive(policy)) {
+             /*
+              * The policy was active when it was suspended, so its governor
+              * is still there.  Let the governor go.
+              */
+             cpufreq_exit_governor(policy);
+             return;
+     }
+
+     if (!start)
+             return;
+
+     if (cpufreq_start_governor(policy))
+             pr_err("%s: Failed to start governor for CPU%u's policy\n",
+                    __func__, policy->cpu);
+}
+
   /**
    * cpufreq_resume() - Resume CPUFreq governors.
    *
@@ -2003,11 +2037,12 @@ suspend:
   void cpufreq_resume(void)
   {
       struct cpufreq_policy *policy;
-     int ret;
 
       if (!cpufreq_driver)
               return;
 
+     guard(cpus_read_lock)();
+
       if (unlikely(!cpufreq_suspended))
               return;
 
@@ -2018,19 +2053,18 @@ void cpufreq_resume(void)
 
       pr_debug("%s: Resuming Governors\n", __func__);
 
-     for_each_active_policy(policy) {
-             if (cpufreq_driver->resume && cpufreq_driver->resume(policy)) {
-                     pr_err("%s: Failed to resume driver: %s\n", __func__,
-                             cpufreq_driver->name);
-             } else if (has_target()) {
-                     scoped_guard(cpufreq_policy_write, policy) {
-                             ret = cpufreq_start_governor(policy);
-                     }
+     for_each_policy(policy) {
+             int ret = 0;
 
+             if (cpufreq_driver->resume && !policy_is_inactive(policy)) {
+                     ret = cpufreq_driver->resume(policy);
                       if (ret)
-                             pr_err("%s: Failed to start governor for CPU%u's policy\n",
-                                    __func__, policy->cpu);
+                             pr_err("%s: Failed to resume driver: %s\n", __func__,
+                                    cpufreq_driver->name);
               }
+
+             if (has_target())
+                     cpufreq_resume_policy(policy, ret == 0);
       }
   }
 
@@ -2412,12 +2446,17 @@ __weak struct cpufreq_governor *cpufreq_
       return NULL;
   }
 
+static bool cpufreq_is_suspended(struct cpufreq_policy *policy)
+{
+     return cpufreq_suspended || policy->suspended;
+}
+
   static int cpufreq_init_governor(struct cpufreq_policy *policy)
   {
       int ret;
 
-     /* Don't start any governor operations if we are entering suspend */
-     if (cpufreq_suspended)
+     /* Don't start any governor operations for suspended policies. */
+     if (cpufreq_is_suspended(policy))
               return 0;
       /*
        * Governor might not be initiated here if ACPI _PPC changed
@@ -2460,7 +2499,7 @@ static int cpufreq_init_governor(struct
 
   static void cpufreq_exit_governor(struct cpufreq_policy *policy)
   {
-     if (cpufreq_suspended || !policy->governor)
+     if (cpufreq_is_suspended(policy) || !policy->governor)
               return;
 
       pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
@@ -2475,7 +2514,7 @@ int cpufreq_start_governor(struct cpufre
   {
       int ret;
 
-     if (cpufreq_suspended)
+     if (cpufreq_is_suspended(policy))
               return 0;
 
       if (!policy->governor)
@@ -2499,7 +2538,7 @@ int cpufreq_start_governor(struct cpufre
 
   void cpufreq_stop_governor(struct cpufreq_policy *policy)
   {
-     if (cpufreq_suspended || !policy->governor)
+     if (cpufreq_is_suspended(policy) || !policy->governor)
               return;
 
       pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
@@ -2510,7 +2549,7 @@ void cpufreq_stop_governor(struct cpufre
 
   static void cpufreq_governor_limits(struct cpufreq_policy *policy)
   {
-     if (cpufreq_suspended || !policy->governor)
+     if (cpufreq_is_suspended(policy) || !policy->governor)
               return;
 
       pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
@@ -2747,7 +2786,8 @@ static void cpufreq_policy_refresh(struc
        * -> ask driver for current freq and notify governors about a change
        */
       if (cpufreq_driver->get && has_target() &&
-         (cpufreq_suspended || WARN_ON(!cpufreq_verify_current_freq(policy, false))))
+         (cpufreq_is_suspended(policy) ||
+          WARN_ON(!cpufreq_verify_current_freq(policy, false))))
               return;
 
       refresh_frequency_limits(policy);
include/linux/cpufreq.h +3 -0
@@ -149,6 +149,9 @@ struct cpufreq_policy {
       /* Pending policy->min/max update for the driver */
       bool                    update_limits;
 
+     /* The policy has been suspended. */
+     bool                    suspended;
+
        /* Cached frequency lookup from cpufreq_driver_resolve_freq. */
       unsigned int cached_target_freq;
       unsigned int cached_resolved_idx;
author maintainer
#5 lore raw

PATCH v2 2/3 cpufreq: Force passing target frequency to driver during resume

In reply to Rafael J. Wysocki

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

The policy->cur value preserved across a system suspend-resume cycle may become stale in some cases and it may prevent the CPU performance scaling logic from being updated due to the target_freq == policy->cur check in __cpufreq_driver_target() [1].

To address this, adjust the existing mechanism to force the invocation of the driver .target() callback in __cpufreq_driver_target() and make cpufreq_resume_policy() use it.

This involves renaming update_limits in struct cpufreq_policy to force_target and moving the driver CPUFREQ_NEED_UPDATE_LIMITS flag check from __cpufreq_driver_target() to cpufreq_set_policy(), where it is used as a condition for setting policy->force_target.

In cpufreq_resume_policy(), policy->force_target is set for drivers that implement the .target() callback, but don't implement the .get() one, which is the case when the driver callback needs to run if policy->cur becomes stale while suspended.

Fixes 2f0aea936360 ("cpufreq: suspend governors on system suspend/hibernate")
Cc All applicable <[email protected]>

---

v1 -> v2:
* Always clear policy->force_target if set in __cpufreq_driver_target() to avoid forcing the driver callback next time unnecessarily (Sashiko)

* Replace an outdated comment in __cpufreq_driver_target()

* Set policy->force_target only if .target() is present and .get() is not present because that is the only case in which it may matter

* Update changelog

---
 drivers/cpufreq/cpufreq.c |   33 +++++++++++++++++++++------------
 include/linux/cpufreq.h   |    4 ++--
 2 files changed, 23 insertions(+), 14 deletions(-)
drivers/cpufreq/cpufreq.c +21 -12
@@ -2023,6 +2023,15 @@ static void cpufreq_resume_policy(struct
 	if (!start)
 		return;
 
+	/*
+	 * Force executing the driver .target() callback, if present, in the
+	 * next invocation of __cpufreq_driver_target() in case policy->cur has
+	 * become stale while suspended, but do it only for drivers without a
+	 * .get() callback for which cpufreq_verify_current_freq() is a NOP.
+	 */
+	if (cpufreq_driver->target && !cpufreq_driver->get)
+		policy->force_target = true;
+
 	if (cpufreq_start_governor(policy))
 		pr_err("%s: Failed to start governor for CPU%u's policy\n",
 		       __func__, policy->cpu);
@@ -2400,17 +2409,14 @@ int __cpufreq_driver_target(struct cpufr
 		policy->cpu, policy->cur, target_freq, old_target_freq, relation);
 
 	/*
-	 * This might look like a redundant call as we are checking it again
-	 * after finding index. But it is left intentionally for cases where
-	 * exactly same freq is called again and so we can save on few function
-	 * calls.
+	 * Unless invoking the target callback is forced, skip the update if the
+	 * frequency is not really changing.
 	 */
-	if (target_freq == policy->cur) {
-		if (!(cpufreq_driver->flags & CPUFREQ_NEED_UPDATE_LIMITS) ||
-		    !policy->update_limits)
-			return 0;
-
-		policy->update_limits = false;
+	if (policy->force_target) {
+		/* Avoid forcing the driver callback unnecessarily next time. */
+		policy->force_target = false;
+	} else if (target_freq == policy->cur) {
+		return 0;
 	}
 
 	if (cpufreq_driver->target) {
@@ -2669,6 +2675,7 @@ static int cpufreq_set_policy(struct cpu
 			      struct cpufreq_governor *new_gov,
 			      unsigned int new_pol)
 {
+	bool need_update_limits = cpufreq_driver->flags & CPUFREQ_NEED_UPDATE_LIMITS;
 	struct cpufreq_policy_data new_data;
 	struct cpufreq_governor *old_gov;
 	unsigned int freq;
@@ -2708,7 +2715,8 @@ static int cpufreq_set_policy(struct cpu
 			      CPUFREQ_RELATION_H);
 	if (freq != policy->max) {
 		WRITE_ONCE(policy->max, freq);
-		policy->update_limits = true;
+		if (need_update_limits)
+			policy->force_target = true;
 	}
 
 	freq = __resolve_freq(policy, new_data.min, new_data.min, new_data.max,
@@ -2716,7 +2724,8 @@ static int cpufreq_set_policy(struct cpu
 	freq = min(freq, policy->max);
 	if (freq != policy->min) {
 		WRITE_ONCE(policy->min, freq);
-		policy->update_limits = true;
+		if (need_update_limits)
+			policy->force_target = true;
 	}
 
 	trace_cpu_frequency_limits(policy);
include/linux/cpufreq.h +2 -2
@@ -146,8 +146,8 @@ struct cpufreq_policy {
 	/* Per policy boost supported flag. */
 	bool			boost_supported;
 
-	/* Pending policy->min/max update for the driver */
-	bool			update_limits;
+	/* Force passing target frequency to the driver. */
+	bool			force_target;
 
 	/* The policy has been suspended. */
 	bool			suspended;
#6 lore raw

Re: [PATCH v2 2/3] cpufreq: Force passing target frequency to driver during resume

Quoted 76 lines · Rafael J. Wysocki

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

The policy->cur value preserved across a system suspend-resume cycle may become stale in some cases and it may prevent the CPU performance scaling logic from being updated due to the target_freq == policy->cur check in __cpufreq_driver_target() [1].

To address this, adjust the existing mechanism to force the invocation of the driver .target() callback in __cpufreq_driver_target() and make cpufreq_resume_policy() use it.

This involves renaming update_limits in struct cpufreq_policy to force_target and moving the driver CPUFREQ_NEED_UPDATE_LIMITS flag check from __cpufreq_driver_target() to cpufreq_set_policy(), where it is used as a condition for setting policy->force_target.

In cpufreq_resume_policy(), policy->force_target is set for drivers that implement the .target() callback, but don't implement the .get() one, which is the case when the driver callback needs to run if policy->cur becomes stale while suspended.

Fixes 2f0aea936360 ("cpufreq: suspend governors on system suspend/hibernate")
Cc All applicable <[email protected]>

---

v1 -> v2:
* Always clear policy->force_target if set in __cpufreq_driver_target() to avoid forcing the driver callback next time unnecessarily (Sashiko)

* Replace an outdated comment in __cpufreq_driver_target()

* Set policy->force_target only if .target() is present and .get() is not present because that is the only case in which it may matter

* Update changelog

---
 drivers/cpufreq/cpufreq.c |   33 +++++++++++++++++++++------------
 include/linux/cpufreq.h   |    4 ++--
 2 files changed, 23 insertions(+), 14 deletions(-)
drivers/cpufreq/cpufreq.c +11 -6
@@ -2023,6 +2023,15 @@ static void cpufreq_resume_policy(struct
 	if (!start)
 		return;
 
+	/*
+	 * Force executing the driver .target() callback, if present, in the
+	 * next invocation of __cpufreq_driver_target() in case policy->cur has
+	 * become stale while suspended, but do it only for drivers without a
+	 * .get() callback for which cpufreq_verify_current_freq() is a NOP.
+	 */
+	if (cpufreq_driver->target && !cpufreq_driver->get)
+		policy->force_target = true;
+
 	if (cpufreq_start_governor(policy))
 		pr_err("%s: Failed to start governor for CPU%u's policy\n",
 		       __func__, policy->cpu);
@@ -2400,17 +2409,14 @@ int __cpufreq_driver_target(struct cpufr
 		policy->cpu, policy->cur, target_freq, old_target_freq, relation);
 
 	/*
-	 * This might look like a redundant call as we are checking it again
-	 * after finding index. But it is left intentionally for cases where
-	 * exactly same freq is called again and so we can save on few function
-	 * calls.
+	 * Unless invoking the target callback is forced, skip the update if the
+	 * frequency is not really changing.
 	 */
-	if (target_freq == policy->cur) {
-		if (!(cpufreq_driver->flags & CPUFREQ_NEED_UPDATE_LIMITS) ||

drivers/cpufreq/cpufreq.c int __cpufreq_driver_target(struct cpufr · line 2419

Quoted 10 lines
drivers/cpufreq/cpufreq.c +5 -4
-		    !policy->update_limits)
-			return 0;
-
-		policy->update_limits = false;
+	if (policy->force_target) {
+		/* Avoid forcing the driver callback unnecessarily next time. */
+		policy->force_target = false;
+	} else if (target_freq == policy->cur) {
+		return 0;
[snip]

This is too early though? Should be something like:

---->8-----

drivers/cpufreq/cpufreq.c +11 -9
index 5f0ddb8823cb..66bc53f13eef 100644
@@ -2398,6 +2398,7 @@ int __cpufreq_driver_target(struct cpufreq_policy *policy,
                            unsigned int relation)
 {
        unsigned int old_target_freq = target_freq;
+       int ret;
 
        if (cpufreq_disabled())
                return -ENODEV;
@@ -2412,12 +2413,8 @@ int __cpufreq_driver_target(struct cpufreq_policy *policy,
         * Unless invoking the target callback is forced, skip the update if the
         * frequency is not really changing.
         */
-       if (policy->force_target) {
-               /* Avoid forcing the driver callback unnecessarily next time. */
-               policy->force_target = false;
-       } else if (target_freq == policy->cur) {
+       if (!policy->force_target && target_freq == policy->cur)
                return 0;
-       }
 
        if (cpufreq_driver->target) {
                /*
@@ -2427,13 +2424,18 @@ int __cpufreq_driver_target(struct cpufreq_policy *policy,
                if (!policy->efficiencies_available)
                        relation &= ~CPUFREQ_RELATION_E;
 
-               return cpufreq_driver->target(policy, target_freq, relation);
+               ret = cpufreq_driver->target(policy, target_freq, relation);
+       } else if (cpufreq_driver->target_index) {
+               ret = __target_index(policy, policy->cached_resolved_idx);
+       } else {
+               return -EINVAL;
        }
 
-       if (!cpufreq_driver->target_index)
-               return -EINVAL;
+       /* Failed updates still need to be passed to the driver next time. */
+       if (!ret)
+               policy->force_target = false;
 
-       return __target_index(policy, policy->cached_resolved_idx);
+       return ret;
 
 
author maintainer
#7 lore raw

Re: [PATCH v2 2/3] cpufreq: Force passing target frequency to driver during resume

Quoted 82 lines · Christian Loehle
Quoted 80 lines · Rafael J. Wysocki

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

The policy->cur value preserved across a system suspend-resume cycle may become stale in some cases and it may prevent the CPU performance scaling logic from being updated due to the target_freq == policy->cur check in __cpufreq_driver_target() [1].

To address this, adjust the existing mechanism to force the invocation of the driver .target() callback in __cpufreq_driver_target() and make cpufreq_resume_policy() use it.

This involves renaming update_limits in struct cpufreq_policy to force_target and moving the driver CPUFREQ_NEED_UPDATE_LIMITS flag check from __cpufreq_driver_target() to cpufreq_set_policy(), where it is used as a condition for setting policy->force_target.

In cpufreq_resume_policy(), policy->force_target is set for drivers that implement the .target() callback, but don't implement the .get() one, which is the case when the driver callback needs to run if policy->cur becomes stale while suspended.

Fixes 2f0aea936360 ("cpufreq: suspend governors on system suspend/hibernate")
Cc All applicable <[email protected]>

---

v1 -> v2:
* Always clear policy->force_target if set in __cpufreq_driver_target() to avoid forcing the driver callback next time unnecessarily (Sashiko)

* Replace an outdated comment in __cpufreq_driver_target()

* Set policy->force_target only if .target() is present and .get() is not present because that is the only case in which it may matter

* Update changelog

---
 drivers/cpufreq/cpufreq.c |   33 +++++++++++++++++++++------------
 include/linux/cpufreq.h   |    4 ++--
 2 files changed, 23 insertions(+), 14 deletions(-)
drivers/cpufreq/cpufreq.c +11 -10
@@ -2023,6 +2023,15 @@ static void cpufreq_resume_policy(struct
      if (!start)
              return;
 
+     /*
+      * Force executing the driver .target() callback, if present, in the
+      * next invocation of __cpufreq_driver_target() in case policy->cur has
+      * become stale while suspended, but do it only for drivers without a
+      * .get() callback for which cpufreq_verify_current_freq() is a NOP.
+      */
+     if (cpufreq_driver->target && !cpufreq_driver->get)
+             policy->force_target = true;
+
      if (cpufreq_start_governor(policy))
              pr_err("%s: Failed to start governor for CPU%u's policy\n",
                     __func__, policy->cpu);
@@ -2400,17 +2409,14 @@ int __cpufreq_driver_target(struct cpufr
              policy->cpu, policy->cur, target_freq, old_target_freq, relation);
 
      /*
-      * This might look like a redundant call as we are checking it again
-      * after finding index. But it is left intentionally for cases where
-      * exactly same freq is called again and so we can save on few function
-      * calls.
+      * Unless invoking the target callback is forced, skip the update if the
+      * frequency is not really changing.
       */
-     if (target_freq == policy->cur) {
-             if (!(cpufreq_driver->flags & CPUFREQ_NEED_UPDATE_LIMITS) ||
-                 !policy->update_limits)
-                     return 0;
-
-             policy->update_limits = false;
Quoted 8 lines

drivers/cpufreq/cpufreq.c int __cpufreq_driver_target(struct cpufr · line 2419

Quoted 6 lines
drivers/cpufreq/cpufreq.c +5 -0
+     if (policy->force_target) {
+             /* Avoid forcing the driver callback unnecessarily next time. */
+             policy->force_target = false;
+     } else if (target_freq == policy->cur) {
+             return 0;
[snip]

This is too early though? Should be something like:

I don't think so.

If the driver has a problem, there is not much the core can do about it.

Do you have any specific drivers in mind here?

Quoted 49 lines

---->8-----

drivers/cpufreq/cpufreq.c +11 -9
index 5f0ddb8823cb..66bc53f13eef 100644
@@ -2398,6 +2398,7 @@ int __cpufreq_driver_target(struct cpufreq_policy *policy,
                            unsigned int relation)
 {
        unsigned int old_target_freq = target_freq;
+       int ret;
 
        if (cpufreq_disabled())
                return -ENODEV;
@@ -2412,12 +2413,8 @@ int __cpufreq_driver_target(struct cpufreq_policy *policy,
         * Unless invoking the target callback is forced, skip the update if the
         * frequency is not really changing.
         */
-       if (policy->force_target) {
-               /* Avoid forcing the driver callback unnecessarily next time. */
-               policy->force_target = false;
-       } else if (target_freq == policy->cur) {
+       if (!policy->force_target && target_freq == policy->cur)
                return 0;
-       }
 
        if (cpufreq_driver->target) {
                /*
@@ -2427,13 +2424,18 @@ int __cpufreq_driver_target(struct cpufreq_policy *policy,
                if (!policy->efficiencies_available)
                        relation &= ~CPUFREQ_RELATION_E;
 
-               return cpufreq_driver->target(policy, target_freq, relation);
+               ret = cpufreq_driver->target(policy, target_freq, relation);
+       } else if (cpufreq_driver->target_index) {
+               ret = __target_index(policy, policy->cached_resolved_idx);
+       } else {
+               return -EINVAL;
        }
 
-       if (!cpufreq_driver->target_index)
-               return -EINVAL;
+       /* Failed updates still need to be passed to the driver next time. */
+       if (!ret)
+               policy->force_target = false;
 
-       return __target_index(policy, policy->cached_resolved_idx);
+       return ret;
 
 
#8 lore raw

Re: [PATCH v2 2/3] cpufreq: Force passing target frequency to driver during resume

Quoted 93 lines · Rafael J. Wysocki (Intel)
Quoted 82 lines · Christian Loehle
Quoted 80 lines · Rafael J. Wysocki

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

The policy->cur value preserved across a system suspend-resume cycle may become stale in some cases and it may prevent the CPU performance scaling logic from being updated due to the target_freq == policy->cur check in __cpufreq_driver_target() [1].

To address this, adjust the existing mechanism to force the invocation of the driver .target() callback in __cpufreq_driver_target() and make cpufreq_resume_policy() use it.

This involves renaming update_limits in struct cpufreq_policy to force_target and moving the driver CPUFREQ_NEED_UPDATE_LIMITS flag check from __cpufreq_driver_target() to cpufreq_set_policy(), where it is used as a condition for setting policy->force_target.

In cpufreq_resume_policy(), policy->force_target is set for drivers that implement the .target() callback, but don't implement the .get() one, which is the case when the driver callback needs to run if policy->cur becomes stale while suspended.

Fixes 2f0aea936360 ("cpufreq: suspend governors on system suspend/hibernate")
Cc All applicable <[email protected]>

---

v1 -> v2:
* Always clear policy->force_target if set in __cpufreq_driver_target() to avoid forcing the driver callback next time unnecessarily (Sashiko)

* Replace an outdated comment in __cpufreq_driver_target()

* Set policy->force_target only if .target() is present and .get() is not present because that is the only case in which it may matter

* Update changelog

---
 drivers/cpufreq/cpufreq.c |   33 +++++++++++++++++++++------------
 include/linux/cpufreq.h   |    4 ++--
 2 files changed, 23 insertions(+), 14 deletions(-)
drivers/cpufreq/cpufreq.c +11 -10
@@ -2023,6 +2023,15 @@ static void cpufreq_resume_policy(struct
      if (!start)
              return;
 
+     /*
+      * Force executing the driver .target() callback, if present, in the
+      * next invocation of __cpufreq_driver_target() in case policy->cur has
+      * become stale while suspended, but do it only for drivers without a
+      * .get() callback for which cpufreq_verify_current_freq() is a NOP.
+      */
+     if (cpufreq_driver->target && !cpufreq_driver->get)
+             policy->force_target = true;
+
      if (cpufreq_start_governor(policy))
              pr_err("%s: Failed to start governor for CPU%u's policy\n",
                     __func__, policy->cpu);
@@ -2400,17 +2409,14 @@ int __cpufreq_driver_target(struct cpufr
              policy->cpu, policy->cur, target_freq, old_target_freq, relation);
 
      /*
-      * This might look like a redundant call as we are checking it again
-      * after finding index. But it is left intentionally for cases where
-      * exactly same freq is called again and so we can save on few function
-      * calls.
+      * Unless invoking the target callback is forced, skip the update if the
+      * frequency is not really changing.
       */
-     if (target_freq == policy->cur) {
-             if (!(cpufreq_driver->flags & CPUFREQ_NEED_UPDATE_LIMITS) ||
-                 !policy->update_limits)
-                     return 0;
-
-             policy->update_limits = false;
Quoted 8 lines

drivers/cpufreq/cpufreq.c int __cpufreq_driver_target(struct cpufr · line 2419

Quoted 6 lines
drivers/cpufreq/cpufreq.c +5 -0
+     if (policy->force_target) {
+             /* Avoid forcing the driver callback unnecessarily next time. */
+             policy->force_target = false;
+     } else if (target_freq == policy->cur) {
+             return 0;
[snip]

This is too early though? Should be something like:

Quoted 5 lines

I don't think so.

If the driver has a problem, there is not much the core can do about it.

Do you have any specific drivers in mind here?

Well drivers cppc_cpufreq is certainly possible with PCC.
I don't have a platform where this would likely be an issue, but MPAR limits is an obvious one. For resume specifically, interestingly enough, MPAR limits uses ktime_get() instead of _boottime, so it might not be entirely unreasonable that we would hit this.
That being said cppc_cpufreq implements .get() making the above moot for resume. Nonetheless the old update_limits path retained the update_limits flag on the write failure and now we drop force_target?

Quoted 1 line

[snip]

author maintainer
#9 lore raw

Re: [PATCH v2 2/3] cpufreq: Force passing target frequency to driver during resume

Quoted 108 lines · Christian Loehle
Quoted 93 lines · Rafael J. Wysocki (Intel)
Quoted 82 lines · Christian Loehle
Quoted 80 lines · Rafael J. Wysocki

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

The policy->cur value preserved across a system suspend-resume cycle may become stale in some cases and it may prevent the CPU performance scaling logic from being updated due to the target_freq == policy->cur check in __cpufreq_driver_target() [1].

To address this, adjust the existing mechanism to force the invocation of the driver .target() callback in __cpufreq_driver_target() and make cpufreq_resume_policy() use it.

This involves renaming update_limits in struct cpufreq_policy to force_target and moving the driver CPUFREQ_NEED_UPDATE_LIMITS flag check from __cpufreq_driver_target() to cpufreq_set_policy(), where it is used as a condition for setting policy->force_target.

In cpufreq_resume_policy(), policy->force_target is set for drivers that implement the .target() callback, but don't implement the .get() one, which is the case when the driver callback needs to run if policy->cur becomes stale while suspended.

Fixes 2f0aea936360 ("cpufreq: suspend governors on system suspend/hibernate")
Cc All applicable <[email protected]>

---

v1 -> v2:
* Always clear policy->force_target if set in __cpufreq_driver_target() to avoid forcing the driver callback next time unnecessarily (Sashiko)

* Replace an outdated comment in __cpufreq_driver_target()

* Set policy->force_target only if .target() is present and .get() is not present because that is the only case in which it may matter

* Update changelog

---
 drivers/cpufreq/cpufreq.c |   33 +++++++++++++++++++++------------
 include/linux/cpufreq.h   |    4 ++--
 2 files changed, 23 insertions(+), 14 deletions(-)
drivers/cpufreq/cpufreq.c +11 -10
@@ -2023,6 +2023,15 @@ static void cpufreq_resume_policy(struct
      if (!start)
              return;
 
+     /*
+      * Force executing the driver .target() callback, if present, in the
+      * next invocation of __cpufreq_driver_target() in case policy->cur has
+      * become stale while suspended, but do it only for drivers without a
+      * .get() callback for which cpufreq_verify_current_freq() is a NOP.
+      */
+     if (cpufreq_driver->target && !cpufreq_driver->get)
+             policy->force_target = true;
+
      if (cpufreq_start_governor(policy))
              pr_err("%s: Failed to start governor for CPU%u's policy\n",
                     __func__, policy->cpu);
@@ -2400,17 +2409,14 @@ int __cpufreq_driver_target(struct cpufr
              policy->cpu, policy->cur, target_freq, old_target_freq, relation);
 
      /*
-      * This might look like a redundant call as we are checking it again
-      * after finding index. But it is left intentionally for cases where
-      * exactly same freq is called again and so we can save on few function
-      * calls.
+      * Unless invoking the target callback is forced, skip the update if the
+      * frequency is not really changing.
       */
-     if (target_freq == policy->cur) {
-             if (!(cpufreq_driver->flags & CPUFREQ_NEED_UPDATE_LIMITS) ||
-                 !policy->update_limits)
-                     return 0;
-
-             policy->update_limits = false;
Quoted 8 lines

drivers/cpufreq/cpufreq.c int __cpufreq_driver_target(struct cpufr · line 2419

Quoted 6 lines
drivers/cpufreq/cpufreq.c +5 -0
+     if (policy->force_target) {
+             /* Avoid forcing the driver callback unnecessarily next time. */
+             policy->force_target = false;
+     } else if (target_freq == policy->cur) {
+             return 0;
[snip]

This is too early though? Should be something like:

Quoted 5 lines

I don't think so.

If the driver has a problem, there is not much the core can do about it.

Do you have any specific drivers in mind here?

Well drivers cppc_cpufreq is certainly possible with PCC.
I don't have a platform where this would likely be an issue, but MPAR limits is an obvious one. For resume specifically, interestingly enough, MPAR limits uses ktime_get() instead of _boottime, so it might not be entirely unreasonable that we would hit this.
That being said cppc_cpufreq implements .get() making the above moot for resume.

Quoted 2 lines

Nonetheless the old update_limits path retained the update_limits flag on the write failure and now we drop force_target?

The relevant code before the change is this:

if (target_freq == policy->cur) {
    if (!(cpufreq_driver->flags & CPUFREQ_NEED_UPDATE_LIMITS) ||
        !policy->update_limits)
            return 0;

    policy->update_limits = false;
}

so strictly speaking it only cleared policy->update_limits when target_freq == policy->cur (which was arguably kind of a mistake), but when that happened, the flag was cleared regardless of driver errors.

So yes, the behavior is different, but it makes more logical sense to me now.

author maintainer
#10 lore raw

PATCH v2 3/3 cpufreq: intel_pstate: Clear last written P-state value on suspend

In reply to Rafael J. Wysocki

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

Preserving cpu->pstate.current_pstate across system suspend-resume cycles, when running in the passive mode with HWP disabled, is a mistake because its value from before system suspend may become stale after the subsequent system resume and in some cases it prevents MSR_IA32_PERF_CTL from being updated properly after system resume [1].

Address this by clearing cpu->pstate.current_pstate after the governor has been stopped during system suspend.

Fixes 001c76f05b01 ("cpufreq: intel_pstate: Generic governors support")
Fixes 4adcf2e5829f ("cpufreq: intel_pstate: Add ->offline and ->online callbacks")
Cc All applicable <[email protected]>

---

v1 -> v2: No changes

---
 drivers/cpufreq/intel_pstate.c |   14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)
drivers/cpufreq/intel_pstate.c +13 -1
@@ -3389,10 +3389,11 @@ static void intel_cpufreq_cpu_exit(struc
 
 static int intel_cpufreq_suspend(struct cpufreq_policy *policy)
 {
+	struct cpudata *cpu = all_cpu_data[policy->cpu];
+
 	intel_pstate_suspend(policy);
 
 	if (hwp_active) {
-		struct cpudata *cpu = all_cpu_data[policy->cpu];
 		u64 value = READ_ONCE(cpu->hwp_req_cached);
 
 		/*
@@ -3403,6 +3404,17 @@ static int intel_cpufreq_suspend(struct
 		value &= ~HWP_DESIRED_PERF(~0L);
 		wrmsrq_on_cpu(cpu->cpu, MSR_HWP_REQUEST, value);
 		WRITE_ONCE(cpu->hwp_req_cached, value);
+	} else {
+		/*
+		 * Invalidate the cached P-state value that may be stale when
+		 * the governor is started again during resume and may prevent
+		 * MSR_IA32_PERF_CTL from being updated.
+		 *
+		 * This does not require synchronization because the core
+		 * prevents frequency updates for suspended policies from
+		 * occurring.
+		 */
+		cpu->pstate.current_pstate = 0;
 	}
 
 	return 0;
Reviewed-by on the v2 series
#11 lore raw
In reply to Rafael J. Wysocki
Quoted 17 lines · Rafael J. Wysocki

Hi,

This is an update of

https://lore.kernel.org/linux-pm/[email protected]/

that modifies the first two patches in the series (to address Sashiko findings) and drops the last one.

This series fixes issues related to the synchronization between cpufreq and the intel_pstate driver with system suspend/resume mentioned here:

https://lore.kernel.org/linux-pm/CAJZ5v0hT6aoX4tuJM+rO_5iequFc_7rEXCoaFnHoJBaH3JP4KA@mail.gmail.com/

The first patch actually prevents governor operations from being carried out on suspended cpufreq policies.

Quoted 7 lines

The second one forces the driver .target() callback to be invoked on the first frequency update after cpufreq policy resume.

The third one causes intel_pstate to invalidate the cached last P-state value during suspend.

Thanks!

Tested-by on the v2 series
#12 lore raw
In reply to Rafael J. Wysocki
Quoted 7 lines · Rafael J. Wysocki

This is an update of

https://lore.kernel.org/linux-pm/[email protected]/

that modifies the first two patches in the series (to address Sashiko findings) and drops the last one.

Same tests as for v1 on the T520 (7.4-rc1 + v2, lockdep), same results:
the P-state is right after resume in every passive mode case, active mode is unchanged, and there are no lockdep reports.

I also made CPU3 fail to come back during resume (hotplug/fail set to irq/affinity:online, passive + schedutil), then onlined it by hand. On 7.2.15, CPU3 comes back without a cpufreq policy ("Failed to initialize policy for cpu: 3 (-16)"); with v2 it's fine. That test also gives a WARNING in rebuild_sched_domains_locked() (cpuset.c:1059): the rebuild sugov_exit() asks for runs while the cpusets still include CPU3, since cpuset_cpu_active() only updates them once num_cpus_frozen is back to
0. On 7.2.15, switching CPU0's governor in that state gives the same warning, so it doesn't come from the series. Onlining CPU3 then also gives intel_epb's "duplicate filename ... energy_perf_bias" sysfs warning, on both kernels.

I used Claude Code to script and run the tests.

Reviewed-by on the v2 series
#13 lore raw
In reply to Rafael J. Wysocki
Quoted 17 lines · Rafael J. Wysocki

Hi,

This is an update of

https://lore.kernel.org/linux-pm/[email protected]/

that modifies the first two patches in the series (to address Sashiko findings) and drops the last one.

This series fixes issues related to the synchronization between cpufreq and the intel_pstate driver with system suspend/resume mentioned here:

https://lore.kernel.org/linux-pm/CAJZ5v0hT6aoX4tuJM+rO_5iequFc_7rEXCoaFnHoJBaH3JP4KA@mail.gmail.com/

The first patch actually prevents governor operations from being carried out on suspended cpufreq policies.

Quoted 9 lines

The second one forces the driver .target() callback to be invoked on the first frequency update after cpufreq policy resume.

The third one causes intel_pstate to invalidate the cached last P-state value during suspend.

Thanks!

Quoted 2 lines
Signature · 2 lines
Thx and BRs,
Zhongqiu Han
maintainer Acked-by on the v2 series
#14 lore raw
In reply to Rafael J. Wysocki
Quoted 14 lines · Rafael J. Wysocki

Hi,

This is an update of

https://lore.kernel.org/linux-pm/[email protected]/

that modifies the first two patches in the series (to address Sashiko findings) and drops the last one.

This series fixes issues related to the synchronization between cpufreq and the intel_pstate driver with system suspend/resume mentioned here:

https://lore.kernel.org/linux-pm/CAJZ5v0hT6aoX4tuJM+rO_5iequFc_7rEXCoaFnHoJBaH3JP4KA@mail.gmail.com/

Quoted 8 lines

The first patch actually prevents governor operations from being carried out on suspended cpufreq policies.

The second one forces the driver .target() callback to be invoked on the first frequency update after cpufreq policy resume.

The third one causes intel_pstate to invalidate the cached last P-state value during suspend.

Signature · 1 line
viresh
Tested-by on the v2 series
#15 lore raw
In reply to Rafael J. Wysocki
Quoted 17 lines · On 2026.11.03 12:42 : Rafael wrote:

Hi,

This is an update of

https://lore.kernel.org/linux-pm/[email protected]/

that modifies the first two patches in the series (to address Sashiko findings) and drops the last one.

This series fixes issues related to the synchronization between cpufreq and the intel_pstate driver with system suspend/resume mentioned here:

https://lore.kernel.org/linux-pm/CAJZ5v0hT6aoX4tuJM+rO_5iequFc_7rEXCoaFnHoJBaH3JP4KA@mail.gmail.com/

The first patch actually prevents governor operations from being carried out on suspended cpufreq policies.

Quoted 7 lines

The second one forces the driver .target() callback to be invoked on the first frequency update after cpufreq policy resume.

The third one causes intel_pstate to invalidate the cached last P-state value during suspend.

Thanks!

Test computer: Test computer: i5-10600K, 6 cores, 12 CPUs, HWP disabled, passive mode.

Tested-by on the v2 series
#16 lore raw
In reply to Rafael J. Wysocki
Quoted 17 lines · Rafael J. Wysocki

Hi,

This is an update of

https://lore.kernel.org/linux-pm/[email protected]/

that modifies the first two patches in the series (to address Sashiko findings) and drops the last one.

This series fixes issues related to the synchronization between cpufreq and the intel_pstate driver with system suspend/resume mentioned here:

https://lore.kernel.org/linux-pm/CAJZ5v0hT6aoX4tuJM+rO_5iequFc_7rEXCoaFnHoJBaH3JP4KA@mail.gmail.com/

The first patch actually prevents governor operations from being carried out on suspended cpufreq policies.

Quoted 10 lines

The second one forces the driver .target() callback to be invoked on the first frequency update after cpufreq policy resume.

The third one causes intel_pstate to invalidate the cached last P-state value during suspend.

Thanks!

AmpereOne: cppc_cpufreq, s2idle
AMD Ryzen 5 7430U, amd-pstate active/passive/guided, S3 AMD Ryzen 3 3300U: acpi-cpufreq, S3
Radxa Orion O6: cppc_cpufreq, PM test stages only