Skip to content

Commit

Permalink
cpuidle-haltpoll: disable host side polling when kvm virtualized
Browse files Browse the repository at this point in the history
When performing guest side polling, it is not necessary to
also perform host side polling.

So disable host side polling, via the new MSR interface,
when loading cpuidle-haltpoll driver.

Signed-off-by: Marcelo Tosatti <mtosatti@redhat.com>
Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
  • Loading branch information
matosatti authored and rafaeljw committed Jul 30, 2019
1 parent 2cffe9f commit a1c4423
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 1 deletion.
7 changes: 7 additions & 0 deletions arch/x86/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -794,6 +794,7 @@ config KVM_GUEST
bool "KVM Guest support (including kvmclock)"
depends on PARAVIRT
select PARAVIRT_CLOCK
select ARCH_CPUIDLE_HALTPOLL
default y
---help---
This option enables various optimizations for running under the KVM
Expand All @@ -802,6 +803,12 @@ config KVM_GUEST
underlying device model, the host provides the guest with
timing infrastructure such as time of day, and system time

config ARCH_CPUIDLE_HALTPOLL
def_bool n
prompt "Disable host haltpoll when loading haltpoll driver"
help
If virtualized under KVM, disable host haltpoll.

config PVH
bool "Support for running PVH guests"
---help---
Expand Down
8 changes: 8 additions & 0 deletions arch/x86/include/asm/cpuidle_haltpoll.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/* SPDX-License-Identifier: GPL-2.0 */
#ifndef _ARCH_HALTPOLL_H
#define _ARCH_HALTPOLL_H

void arch_haltpoll_enable(void);
void arch_haltpoll_disable(void);

#endif
42 changes: 42 additions & 0 deletions arch/x86/kernel/kvm.c
Original file line number Diff line number Diff line change
Expand Up @@ -875,3 +875,45 @@ void __init kvm_spinlock_init(void)
}

#endif /* CONFIG_PARAVIRT_SPINLOCKS */

#ifdef CONFIG_ARCH_CPUIDLE_HALTPOLL

static void kvm_disable_host_haltpoll(void *i)
{
wrmsrl(MSR_KVM_POLL_CONTROL, 0);
}

static void kvm_enable_host_haltpoll(void *i)
{
wrmsrl(MSR_KVM_POLL_CONTROL, 1);
}

void arch_haltpoll_enable(void)
{
if (!kvm_para_has_feature(KVM_FEATURE_POLL_CONTROL)) {
printk(KERN_ERR "kvm: host does not support poll control\n");
printk(KERN_ERR "kvm: host upgrade recommended\n");
return;
}

preempt_disable();
/* Enable guest halt poll disables host halt poll */
kvm_disable_host_haltpoll(NULL);
smp_call_function(kvm_disable_host_haltpoll, NULL, 1);
preempt_enable();
}
EXPORT_SYMBOL_GPL(arch_haltpoll_enable);

void arch_haltpoll_disable(void)
{
if (!kvm_para_has_feature(KVM_FEATURE_POLL_CONTROL))
return;

preempt_disable();
/* Enable guest halt poll disables host halt poll */
kvm_enable_host_haltpoll(NULL);
smp_call_function(kvm_enable_host_haltpoll, NULL, 1);
preempt_enable();
}
EXPORT_SYMBOL_GPL(arch_haltpoll_disable);
#endif
9 changes: 8 additions & 1 deletion drivers/cpuidle/cpuidle-haltpoll.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <linux/module.h>
#include <linux/sched/idle.h>
#include <linux/kvm_para.h>
#include <linux/cpuidle_haltpoll.h>

static int default_enter_idle(struct cpuidle_device *dev,
struct cpuidle_driver *drv, int index)
Expand Down Expand Up @@ -47,18 +48,24 @@ static struct cpuidle_driver haltpoll_driver = {

static int __init haltpoll_init(void)
{
int ret;
struct cpuidle_driver *drv = &haltpoll_driver;

cpuidle_poll_state_init(drv);

if (!kvm_para_available())
return 0;

return cpuidle_register(&haltpoll_driver, NULL);
ret = cpuidle_register(&haltpoll_driver, NULL);
if (ret == 0)
arch_haltpoll_enable();

return ret;
}

static void __exit haltpoll_exit(void)
{
arch_haltpoll_disable();
cpuidle_unregister(&haltpoll_driver);
}

Expand Down
16 changes: 16 additions & 0 deletions include/linux/cpuidle_haltpoll.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/* SPDX-License-Identifier: GPL-2.0 */
#ifndef _CPUIDLE_HALTPOLL_H
#define _CPUIDLE_HALTPOLL_H

#ifdef CONFIG_ARCH_CPUIDLE_HALTPOLL
#include <asm/cpuidle_haltpoll.h>
#else
static inline void arch_haltpoll_enable(void)
{
}

static inline void arch_haltpoll_disable(void)
{
}
#endif
#endif

0 comments on commit a1c4423

Please sign in to comment.