Skip to contentSkip to conversation

SRF: Fix offline CPU preventing pc6 entry

PATCH In review Patryk Wlazlyn 4 participants 13 messages Jan 10, 2025 – Feb 4, 2025

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

v9 cover SRF: Fix offline CPU preventing pc6 entry

Code for determining the mwait hint for the deepest C-state by inspecting CPUID leaf 0x5 in mwait_play_dead() assumes that, if the number of sub-states for a given major C-state is nonzero, those sub-states are always represented by consecutive numbers starting from
0. This assumption is not based on the documented platform behavior and in fact it is not met on recent Intel platforms.

Changes since v8:
* Revert the deletion of cpuidle_state_table check for enter and s2idle handlers in intel_idle in 3/4:
- if (!cpuidle_state_table[cstate].enter &&
- !cpuidle_state_table[cstate].enter_s2idle)
+ if (!cpuidle_state_table[cstate].enter)

* Apply the changelog wording suggested by Rafael in v8 review.

Patryk Wlazlyn (4):
x86/smp: Allow calling mwait_play_dead with an arbitrary hint
ACPI: processor_idle: Add FFH state handling
intel_idle: Provide the default enter_dead() handler x86/smp: Eliminate mwait_play_dead_cpuid_hint()

 arch/x86/include/asm/smp.h    |  3 +++
 arch/x86/kernel/acpi/cstate.c | 10 ++++++++
 arch/x86/kernel/smpboot.c     | 46 ++++-------------------------------
 drivers/acpi/processor_idle.c |  2 ++
 drivers/idle/intel_idle.c     | 15 ++++++++++++
 include/acpi/processor.h      |  5 ++++
 6 files changed, 40 insertions(+), 41 deletions(-)
Signature · 1 line
2.47.1
#1 lore raw

PATCH v9 1/4 x86/smp: Allow calling mwait_play_dead with an arbitrary hint

Introduce a helper function to allow offlined CPUs to enter FFh idle states with a specific MWAIT hint. The new helper will be used in subsequent patches by the acpi_idle and intel_idle drivers.

No functional change intended.

---
 arch/x86/include/asm/smp.h |  3 ++
 arch/x86/kernel/smpboot.c  | 90 ++++++++++++++++++++------------------
 2 files changed, 51 insertions(+), 42 deletions(-)
arch/x86/include/asm/smp.h +3 -0
index ca073f40698f..dfd09a1e09bf 100644
@@ -114,6 +114,7 @@ void wbinvd_on_cpu(int cpu);
 int wbinvd_on_all_cpus(void);
 
 void smp_kick_mwait_play_dead(void);
+void mwait_play_dead(unsigned int hint);
 
 void native_smp_send_reschedule(int cpu);
 void native_send_call_func_ipi(const struct cpumask *mask);
@@ -164,6 +165,8 @@ static inline struct cpumask *cpu_llc_shared_mask(int cpu)
 {
 	return (struct cpumask *)cpumask_of(0);
 }
+
+static inline void mwait_play_dead(unsigned int eax_hint) { }
 #endif /* CONFIG_SMP */
 
 #ifdef CONFIG_DEBUG_NMI_SELFTEST
arch/x86/kernel/smpboot.c +48 -42

113 lines not on this page · raw message

Signature · 1 line
2.47.1
#2 lore raw

PATCH v9 2/4 ACPI: processor_idle: Add FFH state handling

In reply to Patryk Wlazlyn

Recent Intel platforms will depend on the idle driver to pass the correct hint for playing dead via mwait_play_dead_with_hint(). Expand the existing enter_dead interface with handling for FFH states and pass the MWAIT hint to the mwait_play_dead code.

---
 arch/x86/kernel/acpi/cstate.c | 10 ++++++++++
 drivers/acpi/processor_idle.c |  2 ++
 include/acpi/processor.h      |  5 +++++
 3 files changed, 17 insertions(+)
arch/x86/kernel/acpi/cstate.c +10 -0
index f3ffd0a3a012..8d7b8b02ddb9 100644
@@ -15,6 +15,7 @@
 #include <acpi/processor.h>
 #include <asm/mwait.h>
 #include <asm/special_insns.h>
+#include <asm/smp.h>
 
 /*
  * Initialize bm_flags based on the CPU cache properties
@@ -204,6 +205,15 @@ int acpi_processor_ffh_cstate_probe(unsigned int cpu,
 }
 EXPORT_SYMBOL_GPL(acpi_processor_ffh_cstate_probe);
 
+void acpi_processor_ffh_play_dead(struct acpi_processor_cx *cx)
+{
+	unsigned int cpu = smp_processor_id();
+	struct cstate_entry *percpu_entry;
+
+	percpu_entry = per_cpu_ptr(cpu_cstate_entry, cpu);
+	mwait_play_dead(percpu_entry->states[cx->index].eax);
+}
+
 void __cpuidle acpi_processor_ffh_cstate_enter(struct acpi_processor_cx *cx)
 {
 	unsigned int cpu = smp_processor_id();
drivers/acpi/processor_idle.c +2 -0
index 698897b29de2..586cc7d1d8aa 100644
@@ -590,6 +590,8 @@ static void acpi_idle_play_dead(struct cpuidle_device *dev, int index)
 			raw_safe_halt();
 		else if (cx->entry_method == ACPI_CSTATE_SYSTEMIO) {
 			io_idle(cx->address);
+		} else if (cx->entry_method == ACPI_CSTATE_FFH) {
+			acpi_processor_ffh_play_dead(cx);
 		} else
 			return;
 	}
include/acpi/processor.h +5 -0
index a17e97e634a6..63a37e72b721 100644
@@ -280,6 +280,7 @@ int acpi_processor_ffh_cstate_probe(unsigned int cpu,
 				    struct acpi_processor_cx *cx,
 				    struct acpi_power_register *reg);
 void acpi_processor_ffh_cstate_enter(struct acpi_processor_cx *cstate);
+void acpi_processor_ffh_play_dead(struct acpi_processor_cx *cx);
 #else
 static inline void acpi_processor_power_init_bm_check(struct
 						      acpi_processor_flags
@@ -300,6 +301,10 @@ static inline void acpi_processor_ffh_cstate_enter(struct acpi_processor_cx
 {
 	return;
 }
+static inline void acpi_processor_ffh_play_dead(struct acpi_processor_cx *cx)
+{
+	return;
+}
 #endif
 
 static inline int call_on_cpu(int cpu, long (*fn)(void *), void *arg,
Signature · 1 line
2.47.1
#3 lore raw

PATCH v9 3/4 intel_idle: Provide the default enter_dead() handler

In reply to Patryk Wlazlyn

Recent Intel platforms require idle driver to provide information about the MWAIT hint used to enter the deepest idle state in the play_dead code.

Provide the default enter_dead() handler for all of the platforms and allow overwriting with a custom handler for each platform if needed.

---
 drivers/idle/intel_idle.c | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
drivers/idle/intel_idle.c +15 -0
index ac4d8faa3886..53711ba59a56 100644
@@ -56,6 +56,7 @@
 #include <asm/mwait.h>
 #include <asm/spec-ctrl.h>
 #include <asm/fpu/api.h>
+#include <asm/smp.h>
 
 #define INTEL_IDLE_VERSION "0.5.1"
 
@@ -227,6 +228,16 @@ static __cpuidle int intel_idle_s2idle(struct cpuidle_device *dev,
 	return 0;
 }
 
+static __cpuidle void intel_idle_enter_dead(struct cpuidle_device *dev,
+					    int index)
+{
+	struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
+	struct cpuidle_state *state = &drv->states[index];
+	unsigned long eax = flg2MWAIT(state->flags);
+
+	mwait_play_dead(eax);
+}
+
 /*
  * States are indexed by the cstate number,
  * which is also the index into the MWAIT hint array.
@@ -1798,6 +1809,7 @@ static void __init intel_idle_init_cstates_acpi(struct cpuidle_driver *drv)
 			state->flags |= CPUIDLE_FLAG_TIMER_STOP;
 
 		state->enter = intel_idle;
+		state->enter_dead = intel_idle_enter_dead;
 		state->enter_s2idle = intel_idle_s2idle;
 	}
 }
@@ -2147,6 +2159,9 @@ static void __init intel_idle_init_cstates_icpu(struct cpuidle_driver *drv)
 		    !cpuidle_state_table[cstate].enter_s2idle)
 			break;
 
+		if (!cpuidle_state_table[cstate].enter_dead)
+			cpuidle_state_table[cstate].enter_dead = intel_idle_enter_dead;
+
 		/* If marked as unusable, skip this state. */
 		if (cpuidle_state_table[cstate].flags & CPUIDLE_FLAG_UNUSABLE) {
 			pr_debug("state %s is disabled\n",
Signature · 1 line
2.47.1
#4 lore raw

Re: [PATCH v9 3/4] intel_idle: Provide the default enter_dead() handler

drivers/idle/intel_idle.c static __cpuidle int intel_idle_s2idle(struct cpuidle_device *dev, · line 239

Quoted 9 lines · Patryk Wlazlyn
drivers/idle/intel_idle.c +9 -0
+static __cpuidle void intel_idle_enter_dead(struct cpuidle_device *dev,
+					    int index)
+{
+	struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
+	struct cpuidle_state *state = &drv->states[index];
+	unsigned long eax = flg2MWAIT(state->flags);
+
+	mwait_play_dead(eax);
+}

The __cpuidle marks this as noinstr, but cpuidle_get_cpu_driver() is not noinstr, so the objtool complains:

vmlinux.o: warning: objtool: intel_idle_enter_dead+0x7: call to cpuidle_get_cpu_driver() leaves .noinstr.text section

Patryk, can you fix this up, resync against 6.14-rc1 (there are some minor merge conflicts) and resubmit, please?

I assume that it's OK to just make cpuidle_get_cpu_driver() __cpuidle too. It doesn't do much.

#5 lore raw

PATCH v9 4/4 x86/smp: Eliminate mwait_play_dead_cpuid_hint()

In reply to Patryk Wlazlyn

Currently, mwait_play_dead_cpuid_hint() looks up the MWAIT hint of the deepest idle state by inspecting CPUID leaf 0x5 with the assumption that, if the number of sub-states for a given major C-state is nonzero, those sub-states are always represented by consecutive numbers starting from 0. This assumption is not based on the documented platform behavior and in fact it is not met on recent Intel platforms.

For example, Intel's Sierra Forest report two C-states with two substates each in cpuid leaf 0x5:

Name* target cstate target subcstate (mwait hint)
===========================================================

  C1      0x00             0x00
  C1E     0x00             0x01

-- 0x10 ----

  C6S     0x20             0x22
  C6P     0x20             0x23

-- 0x30 ----

/* No more (sub)states all the way down to the end. */
===========================================================

* Names of the cstates are not included in the CPUID leaf 0x5, they are taken from the product specific documentation.

Notice that hints 0x20 and 0x21 are not defined for C-state 0x20 (C6), so the existing MWAIT hint lookup in mwait_play_dead_cpuid_hint() based on the CPUID leaf 0x5 contents does not work in this case.

Instead of using MWAIT hint lookup that is not guaranteed to work, make native_play_dead() rely on the idle driver for the given platform to put CPUs going offline into appropriate idle state and, if that fails, fall back to hlt_play_dead().

Accordingly, drop mwait_play_dead_cpuid_hint() altogether and make native_play_dead() call cpuidle_play_dead() instead of it unconditionally with the assumption that it will not return if it is successful. Still, in case cpuidle_play_dead() fails, call hlt_play_dead() at the end.

---
 arch/x86/kernel/smpboot.c | 56 +++++----------------------------------
 1 file changed, 7 insertions(+), 49 deletions(-)
arch/x86/kernel/smpboot.c +7 -49
index 8a3545c2cae9..82801137486d 100644
@@ -1272,6 +1272,10 @@ void play_dead_common(void)
 	local_irq_disable();
 }
 
+/*
+ * We need to flush the caches before going to sleep, lest we have
+ * dirty data in our caches when we come back up.
+ */
 void __noreturn mwait_play_dead(unsigned int eax_hint)
 {
 	struct mwait_cpu_dead *md = this_cpu_ptr(&mwait_cpu_dead);
@@ -1317,52 +1321,6 @@ void __noreturn mwait_play_dead(unsigned int eax_hint)
 	}
 }
 
-/*
- * We need to flush the caches before going to sleep, lest we have
- * dirty data in our caches when we come back up.
- */
-static inline void mwait_play_dead_cpuid_hint(void)
-{
-	unsigned int eax, ebx, ecx, edx;
-	unsigned int highest_cstate = 0;
-	unsigned int highest_subcstate = 0;
-	int i;
-
-	if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD ||
-	    boot_cpu_data.x86_vendor == X86_VENDOR_HYGON)
-		return;
-	if (!this_cpu_has(X86_FEATURE_MWAIT))
-		return;
-	if (!this_cpu_has(X86_FEATURE_CLFLUSH))
-		return;
-	if (__this_cpu_read(cpu_info.cpuid_level) < CPUID_MWAIT_LEAF)
-		return;
-
-	eax = CPUID_MWAIT_LEAF;
-	ecx = 0;
-	native_cpuid(&eax, &ebx, &ecx, &edx);
-
-	/*
-	 * eax will be 0 if EDX enumeration is not valid.
-	 * Initialized below to cstate, sub_cstate value when EDX is valid.
-	 */
-	if (!(ecx & CPUID5_ECX_EXTENSIONS_SUPPORTED)) {
-		eax = 0;
-	} else {
-		edx >>= MWAIT_SUBSTATE_SIZE;
-		for (i = 0; i < 7 && edx; i++, edx >>= MWAIT_SUBSTATE_SIZE) {
-			if (edx & MWAIT_SUBSTATE_MASK) {
-				highest_cstate = i;
-				highest_subcstate = edx & MWAIT_SUBSTATE_MASK;
-			}
-		}
-		eax = (highest_cstate << MWAIT_SUBSTATE_SIZE) |
-			(highest_subcstate - 1);
-	}
-
-	mwait_play_dead(eax);
-}
-
 /*
  * Kick all "offline" CPUs out of mwait on kexec(). See comment in
  * mwait_play_dead().
@@ -1413,9 +1371,9 @@ void native_play_dead(void)
 	play_dead_common();
 	tboot_shutdown(TB_SHUTDOWN_WFS);
 
-	mwait_play_dead_cpuid_hint();
-	if (cpuidle_play_dead())
-		hlt_play_dead();
+	/* Below returns only on error. */
+	cpuidle_play_dead();
+	hlt_play_dead();
 }
 
 #else /* ... !CONFIG_HOTPLUG_CPU */
Signature · 1 line
2.47.1
#6 lore raw
In reply to Patryk Wlazlyn
Quoted 6 lines · Patryk Wlazlyn

Patryk Wlazlyn (4):
x86/smp: Allow calling mwait_play_dead with an arbitrary hint
ACPI: processor_idle: Add FFH state handling
intel_idle: Provide the default enter_dead() handler x86/smp: Eliminate mwait_play_dead_cpuid_hint()

Quoted 7 lines
 arch/x86/include/asm/smp.h    |  3 +++
 arch/x86/kernel/acpi/cstate.c | 10 ++++++++
 arch/x86/kernel/smpboot.c     | 46 ++++-------------------------------
 drivers/acpi/processor_idle.c |  2 ++
 drivers/idle/intel_idle.c     | 15 ++++++++++++
 include/acpi/processor.h      |  5 ++++
 6 files changed, 40 insertions(+), 41 deletions(-)

Is everybody happy with this now?

I noticed there are no Fixes: or Cc:stable@ tags on this. Should we be treating this like a new feature or a bug fix?

maintainer
#7 lore raw
Quoted 15 lines · Dave Hansen
Quoted 6 lines · Patryk Wlazlyn

Patryk Wlazlyn (4):
  x86/smp: Allow calling mwait_play_dead with an arbitrary hint
  ACPI: processor_idle: Add FFH state handling
  intel_idle: Provide the default enter_dead() handler x86/smp: Eliminate mwait_play_dead_cpuid_hint()

Quoted 7 lines

 arch/x86/include/asm/smp.h    |  3 +++ arch/x86/kernel/acpi/cstate.c | 10 ++++++++ arch/x86/kernel/smpboot.c     | 46 ++++------------------------------- drivers/acpi/processor_idle.c |  2 ++ drivers/idle/intel_idle.c     | 15 ++++++++++++ include/acpi/processor.h      |  5 ++++

 6 files changed, 40 insertions(+), 41 deletions(-)
Quoted 4 lines

Is everybody happy with this now?

I noticed there are no Fixes: or Cc:stable@ tags on this. Should we be treating this like a new feature or a bug fix?

It would be very helpful to have this in v6.12, because it is LTS. So I would suggest

Thanks,
Artem.

#8 lore raw
Quoted 6 lines · Artem Bityutskiy
Quoted 2 lines

I noticed there are no Fixes: or Cc:stable@ tags on this. Should we be treating this like a new feature or a bug fix?

It would be very helpful to have this in v6.12, because it is LTS. So I would suggest

I was _kinda_ hoping to hear how this affects users.

Is this a big deal for real users out in the field today? Or is this for some theoretical savings of 0.2% of battery life for a platform that's coming out in 2029?

maintainer
#9 lore raw
Quoted 7 lines · Dave Hansen
Quoted 6 lines · Artem Bityutskiy
Quoted 2 lines

I noticed there are no Fixes: or Cc:stable@ tags on this. Should we be treating this like a new feature or a bug fix?

It would be very helpful to have this in v6.12, because it is LTS. So I would
suggest

Quoted 5 lines
Quoted 1 line
Cc [email protected] # v6.12

I was _kinda_ hoping to hear how this affects users.

Is this a big deal for real users out in the field today?

Not a big deal. Most SRF users should have a BIOS with the workaround by now.

Quoted 3 lines

Or is this for
some theoretical savings of 0.2% of battery life for a platform that's coming out in 2029?

If I interpret this question as "what is your motivation precisely", I would answer this way:

* Because version 6.12 is LTS, there is a good chance that users of near-future new platforms will run 6.12 on them.
* If a near-future platform happens to miss the firmware workaround for this issue, having these patches in 6.12 will likely mean that most users are OK.

No other motivation.

Overall, I would categorize back-porting this patch-set to 6.12 as "nice to have".

Thank you!

#10 lore raw
Quoted 4 lines

* Because version 6.12 is LTS, there is a good chance that users of near-future new platforms will run 6.12 on them.
* If a near-future platform happens to miss the firmware workaround for this issue, having these patches in 6.12 will likely mean that most users are OK.

Make sense to me. Any objections to adding "Cc stable v6.12"?

#11 lore raw
Quoted 7 lines · Patryk Wlazlyn
Quoted 5 lines

* Because version 6.12 is LTS, there is a good chance that users of near-future new platforms will run 6.12 on them.
* If a near-future platform happens to miss the firmware workaround for this issue, having these patches in 6.12 will likely mean that most users are OK.

Make sense to me. Any objections to adding "Cc stable v6.12"?

Well, that's not what you add anyway. "Cc stable" is a maintainer thing.

maintainer Acked-by
#12 lore raw
In reply to Dave Hansen
Quoted 16 lines · Dave Hansen
Quoted 6 lines · Patryk Wlazlyn

Patryk Wlazlyn (4):
x86/smp: Allow calling mwait_play_dead with an arbitrary hint
ACPI: processor_idle: Add FFH state handling
intel_idle: Provide the default enter_dead() handler x86/smp: Eliminate mwait_play_dead_cpuid_hint()

Quoted 7 lines
 arch/x86/include/asm/smp.h    |  3 +++
 arch/x86/kernel/acpi/cstate.c | 10 ++++++++
 arch/x86/kernel/smpboot.c     | 46 ++++-------------------------------
 drivers/acpi/processor_idle.c |  2 ++
 drivers/idle/intel_idle.c     | 15 ++++++++++++
 include/acpi/processor.h      |  5 ++++
 6 files changed, 40 insertions(+), 41 deletions(-)
Quoted 1 line

Is everybody happy with this now?

It is fine by me.

Please feel free to add

to all patches.

Thanks!