[PATCH] cpuidle-haltpoll: Disable kvm guest polling when mwait_idle is used

From: lirongqing
Date: Tue Dec 06 2022 - 01:52:12 EST


From: Li RongQing <lirongqing@xxxxxxxxx>

when KVM guest has mwait and mwait_idle is used as default idle function,
Loading cpuidle-haltpoll will make idle function back to default_idle which
is using HLT, As the commit aebef63cf7ff ("x86: Remove vendor checks from
prefer_mwait_c1_over_halt") explains that mwait is preferred

so disable kvm guest polling in this conditions to improve performance,
like sockperf localhost test shows that latency is reduced by about 20%

Signed-off-by: Li RongQing <lirongqing@xxxxxxxxx>
---
arch/x86/include/asm/processor.h | 2 ++
arch/x86/kernel/process.c | 6 ++++++
drivers/cpuidle/cpuidle-haltpoll.c | 4 ++++
3 files changed, 12 insertions(+)

diff --git a/arch/x86/include/asm/processor.h b/arch/x86/include/asm/processor.h
index 67c9d73..159ef33 100644
--- a/arch/x86/include/asm/processor.h
+++ b/arch/x86/include/asm/processor.h
@@ -862,4 +862,6 @@ bool arch_is_platform_page(u64 paddr);
#define arch_is_platform_page arch_is_platform_page
#endif

+bool is_mwait_idle(void);
+
#endif /* _ASM_X86_PROCESSOR_H */
diff --git a/arch/x86/kernel/process.c b/arch/x86/kernel/process.c
index c21b734..330972c 100644
--- a/arch/x86/kernel/process.c
+++ b/arch/x86/kernel/process.c
@@ -896,6 +896,12 @@ void select_idle_routine(const struct cpuinfo_x86 *c)
x86_idle = default_idle;
}

+bool is_mwait_idle(void)
+{
+ return x86_idle == mwait_idle;
+}
+EXPORT_SYMBOL_GPL(is_mwait_idle);
+
void amd_e400_c1e_apic_setup(void)
{
if (boot_cpu_has_bug(X86_BUG_AMD_APIC_C1E)) {
diff --git a/drivers/cpuidle/cpuidle-haltpoll.c b/drivers/cpuidle/cpuidle-haltpoll.c
index 3a39a7f..8cf1ddf 100644
--- a/drivers/cpuidle/cpuidle-haltpoll.c
+++ b/drivers/cpuidle/cpuidle-haltpoll.c
@@ -17,6 +17,7 @@
#include <linux/sched/idle.h>
#include <linux/kvm_para.h>
#include <linux/cpuidle_haltpoll.h>
+#include <linux/processor.h>

static bool force __read_mostly;
module_param(force, bool, 0444);
@@ -111,6 +112,9 @@ static int __init haltpoll_init(void)
if (!kvm_para_available() || !haltpoll_want())
return -ENODEV;

+ if (is_mwait_idle())
+ return -ENODEV;
+
cpuidle_poll_state_init(drv);

ret = cpuidle_register_driver(drv);
--
2.9.4