* [PATCH 1/3] x86/smp: Move mwait hint computation out of mwait_play_dead
2024-10-10 13:39 [PATCH 0/3] SRF: Fix offline CPU preventing pc6 entry Patryk Wlazlyn
@ 2024-10-10 13:39 ` Patryk Wlazlyn
2024-10-10 13:39 ` [PATCH 2/3] x86/smp: Allow forcing the mwait hint for play dead loop Patryk Wlazlyn
2024-10-10 13:39 ` [PATCH 3/3] intel_idle: Identify the deepest cstate for cpu offline code Patryk Wlazlyn
2 siblings, 0 replies; 4+ messages in thread
From: Patryk Wlazlyn @ 2024-10-10 13:39 UTC (permalink / raw)
To: x86
Cc: linux-kernel, linux-pm, rafael.j.wysocki, len.brown,
artem.bityutskiy, dave.hansen, patryk.wlazlyn
The following patches will provide a way for idle driver to set the
mwait hint for offline code to use to reach the deepest cstate.
Make the following changes simpler by moving the mwait hint computation
to a separate function.
Signed-off-by: Patryk Wlazlyn <patryk.wlazlyn@linux.intel.com>
---
arch/x86/kernel/smpboot.c | 44 ++++++++++++++++++++++++---------------
1 file changed, 27 insertions(+), 17 deletions(-)
diff --git a/arch/x86/kernel/smpboot.c b/arch/x86/kernel/smpboot.c
index 0c35207320cb..683898e3b20e 100644
--- a/arch/x86/kernel/smpboot.c
+++ b/arch/x86/kernel/smpboot.c
@@ -1270,28 +1270,14 @@ 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.
- */
-static inline void mwait_play_dead(void)
+/* Computes mwait hint for the deepest mwait hint based on cpuid leaf 0x5 */
+static inline unsigned int get_deepest_mwait_hint(void)
{
- struct mwait_cpu_dead *md = this_cpu_ptr(&mwait_cpu_dead);
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);
@@ -1314,6 +1300,30 @@ static inline void mwait_play_dead(void)
(highest_subcstate - 1);
}
+ return eax;
+}
+
+/*
+ * 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(void)
+{
+ struct mwait_cpu_dead *md = this_cpu_ptr(&mwait_cpu_dead);
+ unsigned int hint;
+
+ 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;
+
+ hint = get_deepest_mwait_hint();
+
/* Set up state for the kexec() hack below */
md->status = CPUDEAD_MWAIT_WAIT;
md->control = CPUDEAD_MWAIT_WAIT;
@@ -1333,7 +1343,7 @@ static inline void mwait_play_dead(void)
mb();
__monitor(md, 0, 0);
mb();
- __mwait(eax, 0);
+ __mwait(hint, 0);
if (READ_ONCE(md->control) == CPUDEAD_MWAIT_KEXEC_HLT) {
/*
--
2.46.2
^ permalink raw reply [flat|nested] 4+ messages in thread* [PATCH 2/3] x86/smp: Allow forcing the mwait hint for play dead loop
2024-10-10 13:39 [PATCH 0/3] SRF: Fix offline CPU preventing pc6 entry Patryk Wlazlyn
2024-10-10 13:39 ` [PATCH 1/3] x86/smp: Move mwait hint computation out of mwait_play_dead Patryk Wlazlyn
@ 2024-10-10 13:39 ` Patryk Wlazlyn
2024-10-10 13:39 ` [PATCH 3/3] intel_idle: Identify the deepest cstate for cpu offline code Patryk Wlazlyn
2 siblings, 0 replies; 4+ messages in thread
From: Patryk Wlazlyn @ 2024-10-10 13:39 UTC (permalink / raw)
To: x86
Cc: linux-kernel, linux-pm, rafael.j.wysocki, len.brown,
artem.bityutskiy, dave.hansen, patryk.wlazlyn
The current implementation for looking up the mwait hint for the deepest
cstate depends on them to be continuous in range [0, NUM_SUBSTATES-1].
While that is correct on most Intel x86 platforms, it is not
architectural and may not result in reaching the most optimized idle
state on some of them.
For example Intel's Sierra Forest report two C6 substates in cpuid leaf 5:
C6S (hint 0x22)
C6SP (hint 0x23)
Hints 0x20 and 0x21 are skipped entirely, causing the current
implementation to compute the wrong hint, when looking for the deepest
cstate for offlined CPU to enter. As a result, package with an offlined
CPU can never reach PC6.
Allow the idle driver to communicate the deepest idle cstate to the x86
offline code.
Signed-off-by: Patryk Wlazlyn <patryk.wlazlyn@linux.intel.com>
---
arch/x86/include/asm/smp.h | 3 +++
arch/x86/kernel/smpboot.c | 12 +++++++++++-
2 files changed, 14 insertions(+), 1 deletion(-)
diff --git a/arch/x86/include/asm/smp.h b/arch/x86/include/asm/smp.h
index ca073f40698f..2cb083a84225 100644
--- a/arch/x86/include/asm/smp.h
+++ b/arch/x86/include/asm/smp.h
@@ -114,6 +114,7 @@ void wbinvd_on_cpu(int cpu);
int wbinvd_on_all_cpus(void);
void smp_kick_mwait_play_dead(void);
+void smp_set_mwait_play_dead_hint(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 smp_set_mwait_play_dead_hint(unsigned int hint) { }
#endif /* CONFIG_SMP */
#ifdef CONFIG_DEBUG_NMI_SELFTEST
diff --git a/arch/x86/kernel/smpboot.c b/arch/x86/kernel/smpboot.c
index 683898e3b20e..67d1fc976683 100644
--- a/arch/x86/kernel/smpboot.c
+++ b/arch/x86/kernel/smpboot.c
@@ -127,6 +127,9 @@ int __read_mostly __max_smt_threads = 1;
/* Flag to indicate if a complete sched domain rebuild is required */
bool x86_topology_update;
+#define PLAY_DEAD_MWAIT_HINT_UNSET 0U
+static unsigned int __read_mostly play_dead_mwait_hint = PLAY_DEAD_MWAIT_HINT_UNSET;
+
int arch_update_cpu_topology(void)
{
int retval = x86_topology_update;
@@ -1270,6 +1273,11 @@ void play_dead_common(void)
local_irq_disable();
}
+void smp_set_mwait_play_dead_hint(unsigned int hint)
+{
+ WRITE_ONCE(play_dead_mwait_hint, hint);
+}
+
/* Computes mwait hint for the deepest mwait hint based on cpuid leaf 0x5 */
static inline unsigned int get_deepest_mwait_hint(void)
{
@@ -1322,7 +1330,9 @@ static inline void mwait_play_dead(void)
if (__this_cpu_read(cpu_info.cpuid_level) < CPUID_MWAIT_LEAF)
return;
- hint = get_deepest_mwait_hint();
+ hint = READ_ONCE(play_dead_mwait_hint);
+ if (hint == PLAY_DEAD_MWAIT_HINT_UNSET)
+ hint = get_deepest_mwait_hint();
/* Set up state for the kexec() hack below */
md->status = CPUDEAD_MWAIT_WAIT;
--
2.46.2
^ permalink raw reply [flat|nested] 4+ messages in thread* [PATCH 3/3] intel_idle: Identify the deepest cstate for cpu offline code
2024-10-10 13:39 [PATCH 0/3] SRF: Fix offline CPU preventing pc6 entry Patryk Wlazlyn
2024-10-10 13:39 ` [PATCH 1/3] x86/smp: Move mwait hint computation out of mwait_play_dead Patryk Wlazlyn
2024-10-10 13:39 ` [PATCH 2/3] x86/smp: Allow forcing the mwait hint for play dead loop Patryk Wlazlyn
@ 2024-10-10 13:39 ` Patryk Wlazlyn
2 siblings, 0 replies; 4+ messages in thread
From: Patryk Wlazlyn @ 2024-10-10 13:39 UTC (permalink / raw)
To: x86
Cc: linux-kernel, linux-pm, rafael.j.wysocki, len.brown,
artem.bityutskiy, dave.hansen, patryk.wlazlyn
On some platforms, for example Sierra Forest, the mwait hints for
subsequent idle states are not continuous, resulting in play_dead() code
not computing the most optimized idle state when CPU is put offline.
This in turn prevents from entering PC6 state when any of the CPUs in
the package is offline. Force the known, best mwait hint for the deepest
cstate.
Signed-off-by: Patryk Wlazlyn <patryk.wlazlyn@linux.intel.com>
---
drivers/idle/intel_idle.c | 12 ++++++++++++
1 file changed, 12 insertions(+)
diff --git a/drivers/idle/intel_idle.c b/drivers/idle/intel_idle.c
index 9aab7abc2ae9..d0b4b231d9ad 100644
--- a/drivers/idle/intel_idle.c
+++ b/drivers/idle/intel_idle.c
@@ -48,6 +48,7 @@
#include <trace/events/power.h>
#include <linux/sched.h>
#include <linux/sched/smt.h>
+#include <linux/smp.h>
#include <linux/notifier.h>
#include <linux/cpu.h>
#include <linux/moduleparam.h>
@@ -1645,6 +1646,7 @@ static bool __init intel_idle_acpi_cst_extract(void)
static void __init intel_idle_init_cstates_acpi(struct cpuidle_driver *drv)
{
int cstate, limit = min_t(int, CPUIDLE_STATE_MAX, acpi_state_table.count);
+ unsigned int mwait_hint_deepest = 0;
/*
* If limit > 0, intel_idle_cst_usable() has returned 'true', so all of
@@ -1678,6 +1680,7 @@ static void __init intel_idle_init_cstates_acpi(struct cpuidle_driver *drv)
state->target_residency *= 3;
state->flags = MWAIT2flg(cx->address);
+ mwait_hint_deepest = cx->address;
if (cx->type > ACPI_STATE_C2)
state->flags |= CPUIDLE_FLAG_TLB_FLUSHED;
@@ -1690,6 +1693,9 @@ static void __init intel_idle_init_cstates_acpi(struct cpuidle_driver *drv)
state->enter = intel_idle;
state->enter_s2idle = intel_idle_s2idle;
}
+
+ if (mwait_hint_deepest)
+ smp_set_mwait_play_dead_hint(mwait_hint_deepest);
}
static bool __init intel_idle_off_by_default(u32 mwait_hint)
@@ -1988,6 +1994,7 @@ static void state_update_enter_method(struct cpuidle_state *state, int cstate)
static void __init intel_idle_init_cstates_icpu(struct cpuidle_driver *drv)
{
+ unsigned int mwait_hint_deepest = 0;
int cstate;
switch (boot_cpu_data.x86_vfm) {
@@ -2037,6 +2044,8 @@ static void __init intel_idle_init_cstates_icpu(struct cpuidle_driver *drv)
if (!intel_idle_verify_cstate(mwait_hint))
continue;
+ mwait_hint_deepest = mwait_hint;
+
/* Structure copy. */
drv->states[drv->state_count] = cpuidle_state_table[cstate];
state = &drv->states[drv->state_count];
@@ -2060,6 +2069,9 @@ static void __init intel_idle_init_cstates_icpu(struct cpuidle_driver *drv)
wrmsrl(MSR_CC6_DEMOTION_POLICY_CONFIG, 0);
wrmsrl(MSR_MC6_DEMOTION_POLICY_CONFIG, 0);
}
+
+ if (mwait_hint_deepest)
+ smp_set_mwait_play_dead_hint(mwait_hint_deepest);
}
/**
--
2.46.2
^ permalink raw reply [flat|nested] 4+ messages in thread