Re: [PATCH] cpuidle-haltpoll: Disable kvm guest polling when mwait_idle is used
From: Rafael J. Wysocki
Date: Tue Dec 06 2022 - 07:01:36 EST
On Tue, Dec 6, 2022 at 7:43 AM <lirongqing@xxxxxxxxx> wrote:
>
> From: Li RongQing <lirongqing@xxxxxxxxx>
First off, can you please write the changelog as proper text in
English using capital letters and periods where applicable? It is
hard to read the way it is.
> 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,
Which is because the ->enter callback pointer of state 1 in the
haltpoll driver points to default_enter_idle() which in turn invokes
default_idle() directly, right?
> 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;
> +
So perhaps you could make default_enter_idle() be a bit more careful
about what it calls as the "default idle" routine?
> cpuidle_poll_state_init(drv);
>
> ret = cpuidle_register_driver(drv);
> --
Anyway, this is unlikely to get into 6.2, so please defer it until
6.2-rc1 is out.