Skip to contentSkip to conversation

ACPI throttling: Save/restore tstate for each CPUs across suspend/resume

RFC PATCH In review Chen Yu 2 participants 4 messages Nov 14, 2016 – Nov 29, 2016 ACPI

lore
Share & tools
lore

Chen Yu

author
#0 lore raw

RFC PATCH 1/1 ACPI throttling: Save/restore tstate for each CPUs across suspend/resume

This is a trial version and any comments are appreciated.

Previously a bug was reported that on certain Broadwell platforms, after resuming from S3, the CPU is running at an anomalously low speed, due to BIOS has enabled the throttling across S3. The solution to this is to introduce a quirk framework to save/restore tstate MSR register around suspend/resume, in Commit 7a9c2dd08ead ("x86/pm:
Introduce quirk framework to save/restore extra MSR registers around suspend/resume").

However more and more reports show that other platforms also experienced the same issue, because some BIOSes would like to adjust the tstate if he thinks the temperature is too high. To deal with this situation, the Linux uses a compensation strategy that, the thermal management leverages thermal_pm_notify() upon resume to check if the Processors inside the thermal zone should be throttled or not, thus tstate would be re-evaluated. Unfortunately on these bogus platforms, none of the Processors are inside any thermal zones due to BIOS's implementation. Thus tstate for Processors never has a chance to be brought back to normal.

This patch tries to save/restore tstate on receiving the PM_SUSPEND_PREPARE and PM_POST_SUSPEND, to be more specific, the tstate is saved after thermal_pm_notify(PM_SUSPEND_PREPARE) is called, while it's restored before thermal_pm_notify(PM_POST_SUSPEND), in this way the thermal zone would adjust the tstate eventually and also help adjust the tstate for Processors which do not have thermal zone bound. Thus it does not imapct the old semantics.

Another concern is that, each CPU should take care of the save/restore operation, thus this patch uses percpu workqueue to achieve this.

Bugzilla: https://bugzilla.kernel.org/show_bug.cgi?id=90041

Cc Len Brown <[email protected]>
Cc "Rafael J. Wysocki" <[email protected]>
Cc Pavel Machek <[email protected]>
Cc Matthew Garrett <[email protected]>
Cc Rui Zhang <[email protected]>
---
 drivers/acpi/processor_throttling.c | 70 +++++++++++++++++++++++++++++++++++++
 1 file changed, 70 insertions(+)
drivers/acpi/processor_throttling.c +70 -0
index d51ca1c..8ddc7d6 100644
@@ -29,6 +29,7 @@
 #include <linux/sched.h>
 #include <linux/cpufreq.h>
 #include <linux/acpi.h>
+#include <linux/suspend.h>
 #include <acpi/processor.h>
 #include <asm/io.h>
 #include <asm/uaccess.h>
@@ -758,6 +759,75 @@ static int acpi_throttling_wrmsr(u64 value)
 	}
 	return ret;
 }
+
+#ifdef CONFIG_PM_SLEEP
+static DEFINE_PER_CPU(u64, tstate_msr);
+
+static long tstate_pm_fn(void *data)
+{
+	u64 value;
+	bool save = *(bool *)data;
+
+	if (save) {
+		acpi_throttling_rdmsr(&value);
+		this_cpu_write(tstate_msr, value);
+	} else {
+		value = this_cpu_read(tstate_msr);
+		if (value)
+			acpi_throttling_wrmsr(value);
+	}
+	return 0;
+}
+
+static void tstate_check(unsigned long mode, bool suspend)
+{
+	int cpu;
+	bool save;
+
+	if (suspend && mode == PM_SUSPEND_PREPARE)
+		save = true;
+	else if (!suspend && mode == PM_POST_SUSPEND)
+		save = false;
+	else
+		return;
+
+	get_online_cpus();
+	for_each_online_cpu(cpu)
+		work_on_cpu(cpu, tstate_pm_fn, &save);
+	put_online_cpus();
+}
+
+static int tstate_suspend(struct notifier_block *nb,
+				unsigned long mode, void *_unused)
+{
+	tstate_check(mode, true);
+	return 0;
+}
+
+static int tstate_resume(struct notifier_block *nb,
+				unsigned long mode, void *_unused)
+{
+	tstate_check(mode, false);
+	return 0;
+}
+
+static int __init tstate_pm_init(void)
+{
+	/*
+	 * tstate_suspend should save tstate after
+	 * thermal zone's update in thermal_pm_notify,
+	 * vice versa tstate_resume restore tstate before
+	 * thermal_pm_notify, thus the thermal framework
+	 * has a chance to re-adjust tstate according to the
+	 * temperature trend.
+	 */
+	pm_notifier(tstate_suspend, -1);
+	pm_notifier(tstate_resume, 1);
+	return 0;
+}
+
+core_initcall(tstate_pm_init);
+#endif
 #else
 static int acpi_throttling_rdmsr(u64 *value)
 {
Signature · 1 line
2.7.4
#1 lore raw
Quoted 61 lines · Chen Yu

This is a trial version and any comments are appreciated.

Previously a bug was reported that on certain Broadwell platforms, after resuming from S3, the CPU is running at an anomalously low speed, due to BIOS has enabled the throttling across S3. The solution to this is to introduce a quirk framework to save/restore tstate MSR register around suspend/resume, in Commit 7a9c2dd08ead ("x86/pm:
Introduce quirk framework to save/restore extra MSR registers around suspend/resume").

However more and more reports show that other platforms also experienced the same issue, because some BIOSes would like to adjust the tstate if he thinks the temperature is too high. To deal with this situation, the Linux uses a compensation strategy that, the thermal management leverages thermal_pm_notify() upon resume to check if the Processors inside the thermal zone should be throttled or not, thus tstate would be re-evaluated. Unfortunately on these bogus platforms, none of the Processors are inside any thermal zones due to BIOS's implementation. Thus tstate for Processors never has a chance to be brought back to normal.

This patch tries to save/restore tstate on receiving the PM_SUSPEND_PREPARE and PM_POST_SUSPEND, to be more specific, the tstate is saved after thermal_pm_notify(PM_SUSPEND_PREPARE) is called, while it's restored before thermal_pm_notify(PM_POST_SUSPEND), in this way the thermal zone would adjust the tstate eventually and also help adjust the tstate for Processors which do not have thermal zone bound. Thus it does not imapct the old semantics.

Another concern is that, each CPU should take care of the save/restore operation, thus this patch uses percpu workqueue to achieve this.

Bugzilla: https://bugzilla.kernel.org/show_bug.cgi?id=90041

Cc Len Brown <[email protected]>
Cc "Rafael J. Wysocki" <[email protected]>
Cc Pavel Machek <[email protected]>
Cc Matthew Garrett <[email protected]>
Cc Rui Zhang <[email protected]>
---
 drivers/acpi/processor_throttling.c | 70 +++++++++++++++++++++++++++++++++++++
 1 file changed, 70 insertions(+)
drivers/acpi/processor_throttling.c +1 -0
index d51ca1c..8ddc7d6 100644
@@ -29,6 +29,7 @@
 #include <linux/sched.h>
 #include <linux/cpufreq.h>
 #include <linux/acpi.h>
+#include <linux/suspend.h>
 #include <acpi/processor.h>
 #include <asm/io.h>
 #include <asm/uaccess.h>

drivers/acpi/processor_throttling.c static int acpi_throttling_wrmsr(u64 value) · line 764

Quoted 6 lines
drivers/acpi/processor_throttling.c +3 -0
@@ -758,6 +759,75 @@ static int acpi_throttling_wrmsr(u64 value)
        }
        return ret;
 }
+
+#ifdef CONFIG_PM_SLEEP
+static DEFINE_PER_CPU(u64, tstate_msr);

Call it saved_tstate_msr maybe?

Quoted 6 lines
drivers/acpi/processor_throttling.c +6 -0
+
+static long tstate_pm_fn(void *data)
+{
+       u64 value;
+       bool save = *(bool *)data;
+

drivers/acpi/processor_throttling.c static int acpi_throttling_wrmsr(u64 value) · line 780

Quoted 10 lines
drivers/acpi/processor_throttling.c +10 -0
+       if (save) {
+               acpi_throttling_rdmsr(&value);
+               this_cpu_write(tstate_msr, value);
+       } else {
+               value = this_cpu_read(tstate_msr);
+               if (value)
+                       acpi_throttling_wrmsr(value);
+       }
+       return 0;
+}

I would split the above into two functions, one for saving and one for restoring ->

Quoted 5 lines
drivers/acpi/processor_throttling.c +5 -0
+
+static void tstate_check(unsigned long mode, bool suspend)
+{
+       int cpu;
+       bool save;

drivers/acpi/processor_throttling.c static int acpi_throttling_wrmsr(u64 value) · line 795

Quoted 10 lines
drivers/acpi/processor_throttling.c +10 -0
+
+       if (suspend && mode == PM_SUSPEND_PREPARE)
+               save = true;
+       else if (!suspend && mode == PM_POST_SUSPEND)
+               save = false;
+       else
+               return;
+
+       get_online_cpus();
+       for_each_online_cpu(cpu)

-> and decide here which one to invoke.

drivers/acpi/processor_throttling.c static int acpi_throttling_wrmsr(u64 value) · line 796

Quoted 1 line
drivers/acpi/processor_throttling.c +1 -0
+               work_on_cpu(cpu, tstate_pm_fn, &save);

Does work_on_cpu() wait for the work to complete?

Quoted 19 lines
drivers/acpi/processor_throttling.c +19 -0
+       put_online_cpus();
+}
+
+static int tstate_suspend(struct notifier_block *nb,
+                               unsigned long mode, void *_unused)
+{
+       tstate_check(mode, true);
+       return 0;
+}
+
+static int tstate_resume(struct notifier_block *nb,
+                               unsigned long mode, void *_unused)
+{
+       tstate_check(mode, false);
+       return 0;
+}
+
+static int __init tstate_pm_init(void)
+{

drivers/acpi/processor_throttling.c static int acpi_throttling_wrmsr(u64 value) · line 825

Quoted 10 lines
drivers/acpi/processor_throttling.c +10 -0
+       /*
+        * tstate_suspend should save tstate after
+        * thermal zone's update in thermal_pm_notify,
+        * vice versa tstate_resume restore tstate before
+        * thermal_pm_notify, thus the thermal framework
+        * has a chance to re-adjust tstate according to the
+        * temperature trend.
+        */
+       pm_notifier(tstate_suspend, -1);
+       pm_notifier(tstate_resume, 1);

I don't think this is going to do what you really want.

Each of these notifiers is going to be invoked during both suspend and resume, so I guess you only need one notifier?

drivers/acpi/processor_throttling.c static int acpi_throttling_wrmsr(u64 value)

Quoted 9 lines
drivers/acpi/processor_throttling.c +5 -1
+       return 0;
+}
+
+core_initcall(tstate_pm_init);
+#endif
 #else
 static int acpi_throttling_rdmsr(u64 *value)
 {
--

Thanks,
Rafael

author
#2 lore raw

Hi,

Quoted 73 lines

-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of Rafael J. Wysocki
Sent: Wednesday, November 23, 2016 7:03 AM
To: Chen, Yu C
Cc: ACPI Devel Maling List; Rui Wang; Linux Kernel Mailing List; Len Brown; Rafael J. Wysocki; Pavel Machek; Matthew Garrett; Zhang, Rui; Linux PM
Subject: Re: [PATCH][RFC] ACPI throttling: Save/restore tstate for each CPUs across suspend/resume

Quoted 62 lines · Chen Yu

This is a trial version and any comments are appreciated.

Previously a bug was reported that on certain Broadwell platforms, after resuming from S3, the CPU is running at an anomalously low speed, due to BIOS has enabled the throttling across S3. The solution to this is to introduce a quirk framework to save/restore tstate MSR register around suspend/resume, in Commit 7a9c2dd08ead ("x86/pm:
Introduce quirk framework to save/restore extra MSR registers around suspend/resume").

However more and more reports show that other platforms also experienced the same issue, because some BIOSes would like to adjust the tstate if he thinks the temperature is too high.
To deal with this situation, the Linux uses a compensation strategy that, the thermal management leverages thermal_pm_notify() upon resume to check if the Processors inside the thermal zone should be throttled or not, thus tstate would be re-evaluated. Unfortunately on these bogus platforms, none of the Processors are inside any thermal zones due to BIOS's implementation. Thus tstate for Processors never has a chance to be brought back to normal.

This patch tries to save/restore tstate on receiving the PM_SUSPEND_PREPARE and PM_POST_SUSPEND, to be more specific, the tstate is saved after thermal_pm_notify(PM_SUSPEND_PREPARE) is called, while it's restored before thermal_pm_notify(PM_POST_SUSPEND),
in this way the thermal zone would adjust the tstate eventually and also help adjust the tstate for Processors which do not have thermal zone bound. Thus it does not imapct the old semantics.

Another concern is that, each CPU should take care of the save/restore operation, thus this patch uses percpu workqueue to achieve this.

Bugzilla: https://bugzilla.kernel.org/show_bug.cgi?id=90041

Cc Len Brown <[email protected]>
Cc "Rafael J. Wysocki" <[email protected]>
Cc Pavel Machek <[email protected]>
Cc Matthew Garrett <[email protected]>
Cc Rui Zhang <[email protected]>
---
 drivers/acpi/processor_throttling.c | 70
+++++++++++++++++++++++++++++++++++++
 1 file changed, 70 insertions(+)
drivers/acpi/processor_throttling.c +1 -0
index d51ca1c..8ddc7d6 100644
@@ -29,6 +29,7 @@
 #include <linux/sched.h>
 #include <linux/cpufreq.h>
 #include <linux/acpi.h>
+#include <linux/suspend.h>
 #include <acpi/processor.h>
 #include <asm/io.h>
 #include <asm/uaccess.h>
@@ -758,6 +759,75 @@ static int acpi_throttling_wrmsr(u64 value)
Quoted 8 lines

drivers/acpi/processor_throttling.c static int acpi_throttling_wrmsr(u64 value) · line 764

Quoted 6 lines
drivers/acpi/processor_throttling.c +3 -0
        }
        return ret;
 }
+
+#ifdef CONFIG_PM_SLEEP
+static DEFINE_PER_CPU(u64, tstate_msr);

Call it saved_tstate_msr maybe?

OK.

Quoted 11 lines
Quoted 11 lines
drivers/acpi/processor_throttling.c +10 -0
+
+static long tstate_pm_fn(void *data)
+{
+       u64 value;
+       bool save = *(bool *)data;
+
+       if (save) {
+               acpi_throttling_rdmsr(&value);
+               this_cpu_write(tstate_msr, value);
+       } else {
Quoted 9 lines

drivers/acpi/processor_throttling.c static int acpi_throttling_wrmsr(u64 value) · line 780

Quoted 6 lines
drivers/acpi/processor_throttling.c +6 -0
+               value = this_cpu_read(tstate_msr);
+               if (value)
+                       acpi_throttling_wrmsr(value);
+       }
+       return 0;
+}

I would split the above into two functions, one for saving and one for restoring ->

OK.

Quoted 8 lines
Quoted 8 lines
drivers/acpi/processor_throttling.c +8 -0
+
+static void tstate_check(unsigned long mode, bool suspend) {
+       int cpu;
+       bool save;
+
+       if (suspend && mode == PM_SUSPEND_PREPARE)
+               save = true;
+       else if (!suspend && mode == PM_POST_SUSPEND)
Quoted 8 lines

drivers/acpi/processor_throttling.c static int acpi_throttling_wrmsr(u64 value) · line 795

Quoted 6 lines
drivers/acpi/processor_throttling.c +6 -0
+               save = false;
+       else
+               return;
+
+       get_online_cpus();
+       for_each_online_cpu(cpu)

-> and decide here which one to invoke.

OK.

Quoted 5 lines

drivers/acpi/processor_throttling.c static int acpi_throttling_wrmsr(u64 value) · line 796

Quoted 2 lines
drivers/acpi/processor_throttling.c +1 -0
+               work_on_cpu(cpu, tstate_pm_fn, &save);

Does work_on_cpu() wait for the work to complete?

Yes, it might increase the suspend/resume time, a 'queue_work_on' might be better?

Quoted 29 lines
Quoted 16 lines
drivers/acpi/processor_throttling.c +16 -0
+       put_online_cpus();
+}
+
+static int tstate_suspend(struct notifier_block *nb,
+                               unsigned long mode, void *_unused) {
+       tstate_check(mode, true);
+       return 0;
+}
+
+static int tstate_resume(struct notifier_block *nb,
+                               unsigned long mode, void *_unused) {
+       tstate_check(mode, false);
+       return 0;
+}
+
+static int __init tstate_pm_init(void) {

drivers/acpi/processor_throttling.c static int acpi_throttling_wrmsr(u64 value) · line 825

Quoted 10 lines
drivers/acpi/processor_throttling.c +10 -0
+       /*
+        * tstate_suspend should save tstate after
+        * thermal zone's update in thermal_pm_notify,
+        * vice versa tstate_resume restore tstate before
+        * thermal_pm_notify, thus the thermal framework
+        * has a chance to re-adjust tstate according to the
+        * temperature trend.
+        */
+       pm_notifier(tstate_suspend, -1);
+       pm_notifier(tstate_resume, 1);

I don't think this is going to do what you really want.

Quoted 1 line

Each of these notifiers is going to be invoked during both suspend and resume,

Yes,

Quoted 1 line

so I guess you only need one notifier?

Here's my original thought: tstate_suspend needs to be invoked after
thermal_pm_notify, which has a priority of '0',
so the notifier of tstate_suspend should be lower than '0', thus '-1'. And the same for tstate_resume,
it should be invoked before thermal_pm_notify,
thus priority is '1' ?

Quoted 12 lines

drivers/acpi/processor_throttling.c static int acpi_throttling_wrmsr(u64 value)

Quoted 9 lines
drivers/acpi/processor_throttling.c +5 -1
+       return 0;
+}
+
+core_initcall(tstate_pm_init);
+#endif
 #else
 static int acpi_throttling_rdmsr(u64 *value)  {
--

Thanks,
Rafael

Thanks,
Yu

#3 lore raw
Quoted 5 lines · Yu C

Hi,

Quoted 4 lines

-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of Rafael J. Wysocki
Sent: Wednesday, November 23, 2016 7:03 AM

[cut]

Quoted 6 lines
Quoted 5 lines
Quoted 2 lines

+ work_on_cpu(cpu, tstate_pm_fn, &save);

Does work_on_cpu() wait for the work to complete?

Yes, it might increase the suspend/resume time, a 'queue_work_on' might be better?

Not really, because you'd need to wait before doing the put_online_cpus() anyway.

Quoted 32 lines
Quoted 29 lines
Quoted 16 lines
drivers/acpi/processor_throttling.c +16 -0
+       put_online_cpus();
+}
+
+static int tstate_suspend(struct notifier_block *nb,
+                               unsigned long mode, void *_unused) {
+       tstate_check(mode, true);
+       return 0;
+}
+
+static int tstate_resume(struct notifier_block *nb,
+                               unsigned long mode, void *_unused) {
+       tstate_check(mode, false);
+       return 0;
+}
+
+static int __init tstate_pm_init(void) {

drivers/acpi/processor_throttling.c static int acpi_throttling_wrmsr(u64 value) · line 825

Quoted 10 lines
drivers/acpi/processor_throttling.c +10 -0
+       /*
+        * tstate_suspend should save tstate after
+        * thermal zone's update in thermal_pm_notify,
+        * vice versa tstate_resume restore tstate before
+        * thermal_pm_notify, thus the thermal framework
+        * has a chance to re-adjust tstate according to the
+        * temperature trend.
+        */
+       pm_notifier(tstate_suspend, -1);
+       pm_notifier(tstate_resume, 1);

I don't think this is going to do what you really want.

Quoted 1 line

Each of these notifiers is going to be invoked during both suspend and resume,

Yes,

Quoted 8 lines
Quoted 1 line

so I guess you only need one notifier?

Here's my original thought: tstate_suspend needs to be invoked after
thermal_pm_notify, which has a priority of '0',
so the notifier of tstate_suspend should be lower than '0', thus '-1'. And the same for tstate_resume,
it should be invoked before thermal_pm_notify,
thus priority is '1' ?

If there's a dependency like that, it needs to be explicit. That is, thermal_pm_notify() needs to invoke your new code in the right order with respect to what it does already.

Thanks,
Rafael