Commit 97d3eb9d authored by Joao Martins's avatar Joao Martins Committed by Rafael J. Wysocki

cpuidle-haltpoll: vcpu hotplug support

When cpus != maxcpus cpuidle-haltpoll will fail to register all vcpus
past the online ones and thus fail to register the idle driver.
This is because cpuidle_add_sysfs() will return with -ENODEV as a
consequence from get_cpu_device() return no device for a non-existing
CPU.

Instead switch to cpuidle_register_driver() and manually register each
of the present cpus through cpuhp_setup_state() callbacks and future
ones that get onlined or offlined. This mimmics similar logic that
intel_idle does.

Fixes: fa86ee90 ("add cpuidle-haltpoll driver")
Signed-off-by: default avatarJoao Martins <joao.m.martins@oracle.com>
Signed-off-by: default avatarBoris Ostrovsky <boris.ostrovsky@oracle.com>
Reviewed-by: default avatarMarcelo Tosatti <mtosatti@redhat.com>
Signed-off-by: default avatarRafael J. Wysocki <rafael.j.wysocki@intel.com>
parent b7e7fffd
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
#ifndef _ARCH_HALTPOLL_H #ifndef _ARCH_HALTPOLL_H
#define _ARCH_HALTPOLL_H #define _ARCH_HALTPOLL_H
void arch_haltpoll_enable(void); void arch_haltpoll_enable(unsigned int cpu);
void arch_haltpoll_disable(void); void arch_haltpoll_disable(unsigned int cpu);
#endif #endif
...@@ -888,32 +888,26 @@ static void kvm_enable_host_haltpoll(void *i) ...@@ -888,32 +888,26 @@ static void kvm_enable_host_haltpoll(void *i)
wrmsrl(MSR_KVM_POLL_CONTROL, 1); wrmsrl(MSR_KVM_POLL_CONTROL, 1);
} }
void arch_haltpoll_enable(void) void arch_haltpoll_enable(unsigned int cpu)
{ {
if (!kvm_para_has_feature(KVM_FEATURE_POLL_CONTROL)) { if (!kvm_para_has_feature(KVM_FEATURE_POLL_CONTROL)) {
printk(KERN_ERR "kvm: host does not support poll control\n"); pr_err_once("kvm: host does not support poll control\n");
printk(KERN_ERR "kvm: host upgrade recommended\n"); pr_err_once("kvm: host upgrade recommended\n");
return; return;
} }
preempt_disable();
/* Enable guest halt poll disables host halt poll */ /* Enable guest halt poll disables host halt poll */
kvm_disable_host_haltpoll(NULL); smp_call_function_single(cpu, kvm_disable_host_haltpoll, NULL, 1);
smp_call_function(kvm_disable_host_haltpoll, NULL, 1);
preempt_enable();
} }
EXPORT_SYMBOL_GPL(arch_haltpoll_enable); EXPORT_SYMBOL_GPL(arch_haltpoll_enable);
void arch_haltpoll_disable(void) void arch_haltpoll_disable(unsigned int cpu)
{ {
if (!kvm_para_has_feature(KVM_FEATURE_POLL_CONTROL)) if (!kvm_para_has_feature(KVM_FEATURE_POLL_CONTROL))
return; return;
preempt_disable();
/* Enable guest halt poll disables host halt poll */ /* Enable guest halt poll disables host halt poll */
kvm_enable_host_haltpoll(NULL); smp_call_function_single(cpu, kvm_enable_host_haltpoll, NULL, 1);
smp_call_function(kvm_enable_host_haltpoll, NULL, 1);
preempt_enable();
} }
EXPORT_SYMBOL_GPL(arch_haltpoll_disable); EXPORT_SYMBOL_GPL(arch_haltpoll_disable);
#endif #endif
...@@ -11,12 +11,16 @@ ...@@ -11,12 +11,16 @@
*/ */
#include <linux/init.h> #include <linux/init.h>
#include <linux/cpu.h>
#include <linux/cpuidle.h> #include <linux/cpuidle.h>
#include <linux/module.h> #include <linux/module.h>
#include <linux/sched/idle.h> #include <linux/sched/idle.h>
#include <linux/kvm_para.h> #include <linux/kvm_para.h>
#include <linux/cpuidle_haltpoll.h> #include <linux/cpuidle_haltpoll.h>
static struct cpuidle_device __percpu *haltpoll_cpuidle_devices;
static enum cpuhp_state haltpoll_hp_state;
static int default_enter_idle(struct cpuidle_device *dev, static int default_enter_idle(struct cpuidle_device *dev,
struct cpuidle_driver *drv, int index) struct cpuidle_driver *drv, int index)
{ {
...@@ -46,6 +50,46 @@ static struct cpuidle_driver haltpoll_driver = { ...@@ -46,6 +50,46 @@ static struct cpuidle_driver haltpoll_driver = {
.state_count = 2, .state_count = 2,
}; };
static int haltpoll_cpu_online(unsigned int cpu)
{
struct cpuidle_device *dev;
dev = per_cpu_ptr(haltpoll_cpuidle_devices, cpu);
if (!dev->registered) {
dev->cpu = cpu;
if (cpuidle_register_device(dev)) {
pr_notice("cpuidle_register_device %d failed!\n", cpu);
return -EIO;
}
arch_haltpoll_enable(cpu);
}
return 0;
}
static int haltpoll_cpu_offline(unsigned int cpu)
{
struct cpuidle_device *dev;
dev = per_cpu_ptr(haltpoll_cpuidle_devices, cpu);
if (dev->registered) {
arch_haltpoll_disable(cpu);
cpuidle_unregister_device(dev);
}
return 0;
}
static void haltpoll_uninit(void)
{
if (haltpoll_hp_state)
cpuhp_remove_state(haltpoll_hp_state);
cpuidle_unregister_driver(&haltpoll_driver);
free_percpu(haltpoll_cpuidle_devices);
haltpoll_cpuidle_devices = NULL;
}
static int __init haltpoll_init(void) static int __init haltpoll_init(void)
{ {
int ret; int ret;
...@@ -56,17 +100,31 @@ static int __init haltpoll_init(void) ...@@ -56,17 +100,31 @@ static int __init haltpoll_init(void)
if (!kvm_para_available()) if (!kvm_para_available())
return 0; return 0;
ret = cpuidle_register(&haltpoll_driver, NULL); ret = cpuidle_register_driver(drv);
if (ret == 0) if (ret < 0)
arch_haltpoll_enable(); return ret;
haltpoll_cpuidle_devices = alloc_percpu(struct cpuidle_device);
if (haltpoll_cpuidle_devices == NULL) {
cpuidle_unregister_driver(drv);
return -ENOMEM;
}
ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "cpuidle/haltpoll:online",
haltpoll_cpu_online, haltpoll_cpu_offline);
if (ret < 0) {
haltpoll_uninit();
} else {
haltpoll_hp_state = ret;
ret = 0;
}
return ret; return ret;
} }
static void __exit haltpoll_exit(void) static void __exit haltpoll_exit(void)
{ {
arch_haltpoll_disable(); haltpoll_uninit();
cpuidle_unregister(&haltpoll_driver);
} }
module_init(haltpoll_init); module_init(haltpoll_init);
......
...@@ -5,11 +5,11 @@ ...@@ -5,11 +5,11 @@
#ifdef CONFIG_ARCH_CPUIDLE_HALTPOLL #ifdef CONFIG_ARCH_CPUIDLE_HALTPOLL
#include <asm/cpuidle_haltpoll.h> #include <asm/cpuidle_haltpoll.h>
#else #else
static inline void arch_haltpoll_enable(void) static inline void arch_haltpoll_enable(unsigned int cpu)
{ {
} }
static inline void arch_haltpoll_disable(void) static inline void arch_haltpoll_disable(unsigned int cpu)
{ {
} }
#endif #endif
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment