2008-02-22 17:21:36 +00:00
|
|
|
/*
|
|
|
|
* KVM paravirt_ops implementation
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2007, Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
|
|
|
|
* Copyright IBM Corporation, 2007
|
|
|
|
* Authors: Anthony Liguori <aliguori@us.ibm.com>
|
|
|
|
*/
|
|
|
|
|
2013-02-23 23:23:25 +00:00
|
|
|
#include <linux/context_tracking.h>
|
2008-02-22 17:21:36 +00:00
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/kvm_para.h>
|
|
|
|
#include <linux/cpu.h>
|
|
|
|
#include <linux/mm.h>
|
2008-02-22 17:21:37 +00:00
|
|
|
#include <linux/highmem.h>
|
2008-02-22 17:21:38 +00:00
|
|
|
#include <linux/hardirq.h>
|
2010-10-14 09:22:51 +00:00
|
|
|
#include <linux/notifier.h>
|
|
|
|
#include <linux/reboot.h>
|
2010-10-14 09:22:52 +00:00
|
|
|
#include <linux/hash.h>
|
|
|
|
#include <linux/sched.h>
|
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/kprobes.h>
|
2013-08-06 09:25:41 +00:00
|
|
|
#include <linux/debugfs.h>
|
2009-02-12 00:45:42 +00:00
|
|
|
#include <asm/timer.h>
|
2010-10-14 09:22:51 +00:00
|
|
|
#include <asm/cpu.h>
|
2010-10-14 09:22:52 +00:00
|
|
|
#include <asm/traps.h>
|
|
|
|
#include <asm/desc.h>
|
2010-10-14 09:22:54 +00:00
|
|
|
#include <asm/tlbflush.h>
|
2012-04-04 12:30:33 +00:00
|
|
|
#include <asm/idle.h>
|
KVM guest: guest side for eoi avoidance
The idea is simple: there's a bit, per APIC, in guest memory,
that tells the guest that it does not need EOI.
Guest tests it using a single est and clear operation - this is
necessary so that host can detect interrupt nesting - and if set, it can
skip the EOI MSR.
I run a simple microbenchmark to show exit reduction
(note: for testing, need to apply follow-up patch
'kvm: host side for eoi optimization' + a qemu patch
I posted separately, on host):
Before:
Performance counter stats for 'sleep 1s':
47,357 kvm:kvm_entry [99.98%]
0 kvm:kvm_hypercall [99.98%]
0 kvm:kvm_hv_hypercall [99.98%]
5,001 kvm:kvm_pio [99.98%]
0 kvm:kvm_cpuid [99.98%]
22,124 kvm:kvm_apic [99.98%]
49,849 kvm:kvm_exit [99.98%]
21,115 kvm:kvm_inj_virq [99.98%]
0 kvm:kvm_inj_exception [99.98%]
0 kvm:kvm_page_fault [99.98%]
22,937 kvm:kvm_msr [99.98%]
0 kvm:kvm_cr [99.98%]
0 kvm:kvm_pic_set_irq [99.98%]
0 kvm:kvm_apic_ipi [99.98%]
22,207 kvm:kvm_apic_accept_irq [99.98%]
22,421 kvm:kvm_eoi [99.98%]
0 kvm:kvm_pv_eoi [99.99%]
0 kvm:kvm_nested_vmrun [99.99%]
0 kvm:kvm_nested_intercepts [99.99%]
0 kvm:kvm_nested_vmexit [99.99%]
0 kvm:kvm_nested_vmexit_inject [99.99%]
0 kvm:kvm_nested_intr_vmexit [99.99%]
0 kvm:kvm_invlpga [99.99%]
0 kvm:kvm_skinit [99.99%]
57 kvm:kvm_emulate_insn [99.99%]
0 kvm:vcpu_match_mmio [99.99%]
0 kvm:kvm_userspace_exit [99.99%]
2 kvm:kvm_set_irq [99.99%]
2 kvm:kvm_ioapic_set_irq [99.99%]
23,609 kvm:kvm_msi_set_irq [99.99%]
1 kvm:kvm_ack_irq [99.99%]
131 kvm:kvm_mmio [99.99%]
226 kvm:kvm_fpu [100.00%]
0 kvm:kvm_age_page [100.00%]
0 kvm:kvm_try_async_get_page [100.00%]
0 kvm:kvm_async_pf_doublefault [100.00%]
0 kvm:kvm_async_pf_not_present [100.00%]
0 kvm:kvm_async_pf_ready [100.00%]
0 kvm:kvm_async_pf_completed
1.002100578 seconds time elapsed
After:
Performance counter stats for 'sleep 1s':
28,354 kvm:kvm_entry [99.98%]
0 kvm:kvm_hypercall [99.98%]
0 kvm:kvm_hv_hypercall [99.98%]
1,347 kvm:kvm_pio [99.98%]
0 kvm:kvm_cpuid [99.98%]
1,931 kvm:kvm_apic [99.98%]
29,595 kvm:kvm_exit [99.98%]
24,884 kvm:kvm_inj_virq [99.98%]
0 kvm:kvm_inj_exception [99.98%]
0 kvm:kvm_page_fault [99.98%]
1,986 kvm:kvm_msr [99.98%]
0 kvm:kvm_cr [99.98%]
0 kvm:kvm_pic_set_irq [99.98%]
0 kvm:kvm_apic_ipi [99.99%]
25,953 kvm:kvm_apic_accept_irq [99.99%]
26,132 kvm:kvm_eoi [99.99%]
26,593 kvm:kvm_pv_eoi [99.99%]
0 kvm:kvm_nested_vmrun [99.99%]
0 kvm:kvm_nested_intercepts [99.99%]
0 kvm:kvm_nested_vmexit [99.99%]
0 kvm:kvm_nested_vmexit_inject [99.99%]
0 kvm:kvm_nested_intr_vmexit [99.99%]
0 kvm:kvm_invlpga [99.99%]
0 kvm:kvm_skinit [99.99%]
284 kvm:kvm_emulate_insn [99.99%]
68 kvm:vcpu_match_mmio [99.99%]
68 kvm:kvm_userspace_exit [99.99%]
2 kvm:kvm_set_irq [99.99%]
2 kvm:kvm_ioapic_set_irq [99.99%]
28,288 kvm:kvm_msi_set_irq [99.99%]
1 kvm:kvm_ack_irq [99.99%]
131 kvm:kvm_mmio [100.00%]
588 kvm:kvm_fpu [100.00%]
0 kvm:kvm_age_page [100.00%]
0 kvm:kvm_try_async_get_page [100.00%]
0 kvm:kvm_async_pf_doublefault [100.00%]
0 kvm:kvm_async_pf_not_present [100.00%]
0 kvm:kvm_async_pf_ready [100.00%]
0 kvm:kvm_async_pf_completed
1.002039622 seconds time elapsed
We see that # of exits is almost halved.
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
Signed-off-by: Avi Kivity <avi@redhat.com>
2012-06-24 16:24:34 +00:00
|
|
|
#include <asm/apic.h>
|
|
|
|
#include <asm/apicdef.h>
|
2012-07-06 17:47:39 +00:00
|
|
|
#include <asm/hypervisor.h>
|
2012-11-28 01:28:56 +00:00
|
|
|
#include <asm/kvm_guest.h>
|
2008-02-22 17:21:38 +00:00
|
|
|
|
2010-10-14 09:22:51 +00:00
|
|
|
static int kvmapf = 1;
|
|
|
|
|
|
|
|
static int parse_no_kvmapf(char *arg)
|
|
|
|
{
|
|
|
|
kvmapf = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
early_param("no-kvmapf", parse_no_kvmapf);
|
|
|
|
|
2011-07-11 19:28:19 +00:00
|
|
|
static int steal_acc = 1;
|
|
|
|
static int parse_no_stealacc(char *arg)
|
|
|
|
{
|
|
|
|
steal_acc = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
early_param("no-steal-acc", parse_no_stealacc);
|
|
|
|
|
2012-11-28 01:28:56 +00:00
|
|
|
static int kvmclock_vsyscall = 1;
|
|
|
|
static int parse_no_kvmclock_vsyscall(char *arg)
|
|
|
|
{
|
|
|
|
kvmclock_vsyscall = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
early_param("no-kvmclock-vsyscall", parse_no_kvmclock_vsyscall);
|
|
|
|
|
2010-10-14 09:22:51 +00:00
|
|
|
static DEFINE_PER_CPU(struct kvm_vcpu_pv_apf_data, apf_reason) __aligned(64);
|
2011-07-11 19:28:19 +00:00
|
|
|
static DEFINE_PER_CPU(struct kvm_steal_time, steal_time) __aligned(64);
|
|
|
|
static int has_steal_clock = 0;
|
2008-02-22 17:21:38 +00:00
|
|
|
|
2008-02-22 17:21:36 +00:00
|
|
|
/*
|
|
|
|
* No need for any "IO delay" on KVM
|
|
|
|
*/
|
|
|
|
static void kvm_io_delay(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2010-10-14 09:22:52 +00:00
|
|
|
#define KVM_TASK_SLEEP_HASHBITS 8
|
|
|
|
#define KVM_TASK_SLEEP_HASHSIZE (1<<KVM_TASK_SLEEP_HASHBITS)
|
|
|
|
|
|
|
|
struct kvm_task_sleep_node {
|
|
|
|
struct hlist_node link;
|
|
|
|
wait_queue_head_t wq;
|
|
|
|
u32 token;
|
|
|
|
int cpu;
|
2010-10-14 09:22:54 +00:00
|
|
|
bool halted;
|
2010-10-14 09:22:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static struct kvm_task_sleep_head {
|
|
|
|
spinlock_t lock;
|
|
|
|
struct hlist_head list;
|
|
|
|
} async_pf_sleepers[KVM_TASK_SLEEP_HASHSIZE];
|
|
|
|
|
|
|
|
static struct kvm_task_sleep_node *_find_apf_task(struct kvm_task_sleep_head *b,
|
|
|
|
u32 token)
|
|
|
|
{
|
|
|
|
struct hlist_node *p;
|
|
|
|
|
|
|
|
hlist_for_each(p, &b->list) {
|
|
|
|
struct kvm_task_sleep_node *n =
|
|
|
|
hlist_entry(p, typeof(*n), link);
|
|
|
|
if (n->token == token)
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void kvm_async_pf_task_wait(u32 token)
|
|
|
|
{
|
|
|
|
u32 key = hash_32(token, KVM_TASK_SLEEP_HASHBITS);
|
|
|
|
struct kvm_task_sleep_head *b = &async_pf_sleepers[key];
|
|
|
|
struct kvm_task_sleep_node n, *e;
|
|
|
|
DEFINE_WAIT(wait);
|
|
|
|
|
Add rcu user eqs exception hooks for async page fault
This patch adds user eqs exception hooks for async page fault page not
present code path, to exit the user eqs and re-enter it as necessary.
Async page fault is different from other exceptions that it may be
triggered from idle process, so we still need rcu_irq_enter() and
rcu_irq_exit() to exit cpu idle eqs when needed, to protect the code
that needs use rcu.
As Frederic pointed out it would be safest and simplest to protect the
whole kvm_async_pf_task_wait(). Otherwise, "we need to check all the
code there deeply for potential RCU uses and ensure it will never be
extended later to use RCU.".
However, We'd better re-enter the cpu idle eqs if we get the exception
in cpu idle eqs, by calling rcu_irq_exit() before native_safe_halt().
So the patch does what Frederic suggested for rcu_irq_*() API usage
here, except that I moved the rcu_irq_*() pair originally in
do_async_page_fault() into kvm_async_pf_task_wait().
That's because, I think it's better to have rcu_irq_*() pairs to be in
one function ( rcu_irq_exit() after rcu_irq_enter() ), especially here,
kvm_async_pf_task_wait() has other callers, which might cause
rcu_irq_exit() be called without a matching rcu_irq_enter() before it,
which is illegal if the cpu happens to be in rcu idle state.
Signed-off-by: Li Zhong <zhong@linux.vnet.ibm.com>
Signed-off-by: Gleb Natapov <gleb@redhat.com>
2012-12-04 02:35:13 +00:00
|
|
|
rcu_irq_enter();
|
|
|
|
|
2010-10-14 09:22:52 +00:00
|
|
|
spin_lock(&b->lock);
|
|
|
|
e = _find_apf_task(b, token);
|
|
|
|
if (e) {
|
|
|
|
/* dummy entry exist -> wake up was delivered ahead of PF */
|
|
|
|
hlist_del(&e->link);
|
|
|
|
kfree(e);
|
|
|
|
spin_unlock(&b->lock);
|
Add rcu user eqs exception hooks for async page fault
This patch adds user eqs exception hooks for async page fault page not
present code path, to exit the user eqs and re-enter it as necessary.
Async page fault is different from other exceptions that it may be
triggered from idle process, so we still need rcu_irq_enter() and
rcu_irq_exit() to exit cpu idle eqs when needed, to protect the code
that needs use rcu.
As Frederic pointed out it would be safest and simplest to protect the
whole kvm_async_pf_task_wait(). Otherwise, "we need to check all the
code there deeply for potential RCU uses and ensure it will never be
extended later to use RCU.".
However, We'd better re-enter the cpu idle eqs if we get the exception
in cpu idle eqs, by calling rcu_irq_exit() before native_safe_halt().
So the patch does what Frederic suggested for rcu_irq_*() API usage
here, except that I moved the rcu_irq_*() pair originally in
do_async_page_fault() into kvm_async_pf_task_wait().
That's because, I think it's better to have rcu_irq_*() pairs to be in
one function ( rcu_irq_exit() after rcu_irq_enter() ), especially here,
kvm_async_pf_task_wait() has other callers, which might cause
rcu_irq_exit() be called without a matching rcu_irq_enter() before it,
which is illegal if the cpu happens to be in rcu idle state.
Signed-off-by: Li Zhong <zhong@linux.vnet.ibm.com>
Signed-off-by: Gleb Natapov <gleb@redhat.com>
2012-12-04 02:35:13 +00:00
|
|
|
|
|
|
|
rcu_irq_exit();
|
2010-10-14 09:22:52 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
n.token = token;
|
|
|
|
n.cpu = smp_processor_id();
|
2012-11-28 13:19:08 +00:00
|
|
|
n.halted = is_idle_task(current) || preempt_count() > 1;
|
2010-10-14 09:22:52 +00:00
|
|
|
init_waitqueue_head(&n.wq);
|
|
|
|
hlist_add_head(&n.link, &b->list);
|
|
|
|
spin_unlock(&b->lock);
|
|
|
|
|
|
|
|
for (;;) {
|
2010-10-14 09:22:54 +00:00
|
|
|
if (!n.halted)
|
|
|
|
prepare_to_wait(&n.wq, &wait, TASK_UNINTERRUPTIBLE);
|
2010-10-14 09:22:52 +00:00
|
|
|
if (hlist_unhashed(&n.link))
|
|
|
|
break;
|
2010-10-14 09:22:54 +00:00
|
|
|
|
|
|
|
if (!n.halted) {
|
|
|
|
local_irq_enable();
|
|
|
|
schedule();
|
|
|
|
local_irq_disable();
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* We cannot reschedule. So halt.
|
|
|
|
*/
|
Add rcu user eqs exception hooks for async page fault
This patch adds user eqs exception hooks for async page fault page not
present code path, to exit the user eqs and re-enter it as necessary.
Async page fault is different from other exceptions that it may be
triggered from idle process, so we still need rcu_irq_enter() and
rcu_irq_exit() to exit cpu idle eqs when needed, to protect the code
that needs use rcu.
As Frederic pointed out it would be safest and simplest to protect the
whole kvm_async_pf_task_wait(). Otherwise, "we need to check all the
code there deeply for potential RCU uses and ensure it will never be
extended later to use RCU.".
However, We'd better re-enter the cpu idle eqs if we get the exception
in cpu idle eqs, by calling rcu_irq_exit() before native_safe_halt().
So the patch does what Frederic suggested for rcu_irq_*() API usage
here, except that I moved the rcu_irq_*() pair originally in
do_async_page_fault() into kvm_async_pf_task_wait().
That's because, I think it's better to have rcu_irq_*() pairs to be in
one function ( rcu_irq_exit() after rcu_irq_enter() ), especially here,
kvm_async_pf_task_wait() has other callers, which might cause
rcu_irq_exit() be called without a matching rcu_irq_enter() before it,
which is illegal if the cpu happens to be in rcu idle state.
Signed-off-by: Li Zhong <zhong@linux.vnet.ibm.com>
Signed-off-by: Gleb Natapov <gleb@redhat.com>
2012-12-04 02:35:13 +00:00
|
|
|
rcu_irq_exit();
|
2010-10-14 09:22:54 +00:00
|
|
|
native_safe_halt();
|
Add rcu user eqs exception hooks for async page fault
This patch adds user eqs exception hooks for async page fault page not
present code path, to exit the user eqs and re-enter it as necessary.
Async page fault is different from other exceptions that it may be
triggered from idle process, so we still need rcu_irq_enter() and
rcu_irq_exit() to exit cpu idle eqs when needed, to protect the code
that needs use rcu.
As Frederic pointed out it would be safest and simplest to protect the
whole kvm_async_pf_task_wait(). Otherwise, "we need to check all the
code there deeply for potential RCU uses and ensure it will never be
extended later to use RCU.".
However, We'd better re-enter the cpu idle eqs if we get the exception
in cpu idle eqs, by calling rcu_irq_exit() before native_safe_halt().
So the patch does what Frederic suggested for rcu_irq_*() API usage
here, except that I moved the rcu_irq_*() pair originally in
do_async_page_fault() into kvm_async_pf_task_wait().
That's because, I think it's better to have rcu_irq_*() pairs to be in
one function ( rcu_irq_exit() after rcu_irq_enter() ), especially here,
kvm_async_pf_task_wait() has other callers, which might cause
rcu_irq_exit() be called without a matching rcu_irq_enter() before it,
which is illegal if the cpu happens to be in rcu idle state.
Signed-off-by: Li Zhong <zhong@linux.vnet.ibm.com>
Signed-off-by: Gleb Natapov <gleb@redhat.com>
2012-12-04 02:35:13 +00:00
|
|
|
rcu_irq_enter();
|
2010-10-14 09:22:54 +00:00
|
|
|
local_irq_disable();
|
|
|
|
}
|
2010-10-14 09:22:52 +00:00
|
|
|
}
|
2010-10-14 09:22:54 +00:00
|
|
|
if (!n.halted)
|
|
|
|
finish_wait(&n.wq, &wait);
|
2010-10-14 09:22:52 +00:00
|
|
|
|
Add rcu user eqs exception hooks for async page fault
This patch adds user eqs exception hooks for async page fault page not
present code path, to exit the user eqs and re-enter it as necessary.
Async page fault is different from other exceptions that it may be
triggered from idle process, so we still need rcu_irq_enter() and
rcu_irq_exit() to exit cpu idle eqs when needed, to protect the code
that needs use rcu.
As Frederic pointed out it would be safest and simplest to protect the
whole kvm_async_pf_task_wait(). Otherwise, "we need to check all the
code there deeply for potential RCU uses and ensure it will never be
extended later to use RCU.".
However, We'd better re-enter the cpu idle eqs if we get the exception
in cpu idle eqs, by calling rcu_irq_exit() before native_safe_halt().
So the patch does what Frederic suggested for rcu_irq_*() API usage
here, except that I moved the rcu_irq_*() pair originally in
do_async_page_fault() into kvm_async_pf_task_wait().
That's because, I think it's better to have rcu_irq_*() pairs to be in
one function ( rcu_irq_exit() after rcu_irq_enter() ), especially here,
kvm_async_pf_task_wait() has other callers, which might cause
rcu_irq_exit() be called without a matching rcu_irq_enter() before it,
which is illegal if the cpu happens to be in rcu idle state.
Signed-off-by: Li Zhong <zhong@linux.vnet.ibm.com>
Signed-off-by: Gleb Natapov <gleb@redhat.com>
2012-12-04 02:35:13 +00:00
|
|
|
rcu_irq_exit();
|
2010-10-14 09:22:52 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(kvm_async_pf_task_wait);
|
|
|
|
|
|
|
|
static void apf_task_wake_one(struct kvm_task_sleep_node *n)
|
|
|
|
{
|
|
|
|
hlist_del_init(&n->link);
|
2010-10-14 09:22:54 +00:00
|
|
|
if (n->halted)
|
|
|
|
smp_send_reschedule(n->cpu);
|
|
|
|
else if (waitqueue_active(&n->wq))
|
2010-10-14 09:22:52 +00:00
|
|
|
wake_up(&n->wq);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void apf_task_wake_all(void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < KVM_TASK_SLEEP_HASHSIZE; i++) {
|
|
|
|
struct hlist_node *p, *next;
|
|
|
|
struct kvm_task_sleep_head *b = &async_pf_sleepers[i];
|
|
|
|
spin_lock(&b->lock);
|
|
|
|
hlist_for_each_safe(p, next, &b->list) {
|
|
|
|
struct kvm_task_sleep_node *n =
|
|
|
|
hlist_entry(p, typeof(*n), link);
|
|
|
|
if (n->cpu == smp_processor_id())
|
|
|
|
apf_task_wake_one(n);
|
|
|
|
}
|
|
|
|
spin_unlock(&b->lock);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void kvm_async_pf_task_wake(u32 token)
|
|
|
|
{
|
|
|
|
u32 key = hash_32(token, KVM_TASK_SLEEP_HASHBITS);
|
|
|
|
struct kvm_task_sleep_head *b = &async_pf_sleepers[key];
|
|
|
|
struct kvm_task_sleep_node *n;
|
|
|
|
|
|
|
|
if (token == ~0) {
|
|
|
|
apf_task_wake_all();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
again:
|
|
|
|
spin_lock(&b->lock);
|
|
|
|
n = _find_apf_task(b, token);
|
|
|
|
if (!n) {
|
|
|
|
/*
|
|
|
|
* async PF was not yet handled.
|
|
|
|
* Add dummy entry for the token.
|
|
|
|
*/
|
2012-05-02 12:04:02 +00:00
|
|
|
n = kzalloc(sizeof(*n), GFP_ATOMIC);
|
2010-10-14 09:22:52 +00:00
|
|
|
if (!n) {
|
|
|
|
/*
|
|
|
|
* Allocation failed! Busy wait while other cpu
|
|
|
|
* handles async PF.
|
|
|
|
*/
|
|
|
|
spin_unlock(&b->lock);
|
|
|
|
cpu_relax();
|
|
|
|
goto again;
|
|
|
|
}
|
|
|
|
n->token = token;
|
|
|
|
n->cpu = smp_processor_id();
|
|
|
|
init_waitqueue_head(&n->wq);
|
|
|
|
hlist_add_head(&n->link, &b->list);
|
|
|
|
} else
|
|
|
|
apf_task_wake_one(n);
|
|
|
|
spin_unlock(&b->lock);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(kvm_async_pf_task_wake);
|
|
|
|
|
|
|
|
u32 kvm_read_and_reset_pf_reason(void)
|
|
|
|
{
|
|
|
|
u32 reason = 0;
|
|
|
|
|
|
|
|
if (__get_cpu_var(apf_reason).enabled) {
|
|
|
|
reason = __get_cpu_var(apf_reason).reason;
|
|
|
|
__get_cpu_var(apf_reason).reason = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return reason;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(kvm_read_and_reset_pf_reason);
|
2014-04-17 08:18:14 +00:00
|
|
|
NOKPROBE_SYMBOL(kvm_read_and_reset_pf_reason);
|
2010-10-14 09:22:52 +00:00
|
|
|
|
2014-04-17 08:18:14 +00:00
|
|
|
dotraplinkage void
|
2010-10-14 09:22:52 +00:00
|
|
|
do_async_page_fault(struct pt_regs *regs, unsigned long error_code)
|
|
|
|
{
|
2013-02-24 00:19:14 +00:00
|
|
|
enum ctx_state prev_state;
|
|
|
|
|
2010-10-14 09:22:52 +00:00
|
|
|
switch (kvm_read_and_reset_pf_reason()) {
|
|
|
|
default:
|
2014-05-16 19:45:15 +00:00
|
|
|
trace_do_page_fault(regs, error_code);
|
2010-10-14 09:22:52 +00:00
|
|
|
break;
|
|
|
|
case KVM_PV_REASON_PAGE_NOT_PRESENT:
|
|
|
|
/* page is swapped out by the host. */
|
2013-02-24 00:19:14 +00:00
|
|
|
prev_state = exception_enter();
|
2012-10-19 16:11:55 +00:00
|
|
|
exit_idle();
|
2010-10-14 09:22:52 +00:00
|
|
|
kvm_async_pf_task_wait((u32)read_cr2());
|
2013-02-24 00:19:14 +00:00
|
|
|
exception_exit(prev_state);
|
2010-10-14 09:22:52 +00:00
|
|
|
break;
|
|
|
|
case KVM_PV_REASON_PAGE_READY:
|
2012-04-04 12:30:33 +00:00
|
|
|
rcu_irq_enter();
|
|
|
|
exit_idle();
|
2010-10-14 09:22:52 +00:00
|
|
|
kvm_async_pf_task_wake((u32)read_cr2());
|
2012-04-04 12:30:33 +00:00
|
|
|
rcu_irq_exit();
|
2010-10-14 09:22:52 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-04-17 08:18:14 +00:00
|
|
|
NOKPROBE_SYMBOL(do_async_page_fault);
|
2010-10-14 09:22:52 +00:00
|
|
|
|
2009-07-02 05:40:36 +00:00
|
|
|
static void __init paravirt_ops_setup(void)
|
2008-02-22 17:21:36 +00:00
|
|
|
{
|
|
|
|
pv_info.name = "KVM";
|
|
|
|
pv_info.paravirt_enabled = 1;
|
|
|
|
|
|
|
|
if (kvm_para_has_feature(KVM_FEATURE_NOP_IO_DELAY))
|
|
|
|
pv_cpu_ops.io_delay = kvm_io_delay;
|
|
|
|
|
2009-02-12 00:45:42 +00:00
|
|
|
#ifdef CONFIG_X86_IO_APIC
|
|
|
|
no_timer_check = 1;
|
|
|
|
#endif
|
2008-02-22 17:21:36 +00:00
|
|
|
}
|
|
|
|
|
2011-07-11 19:28:19 +00:00
|
|
|
static void kvm_register_steal_time(void)
|
|
|
|
{
|
|
|
|
int cpu = smp_processor_id();
|
|
|
|
struct kvm_steal_time *st = &per_cpu(steal_time, cpu);
|
|
|
|
|
|
|
|
if (!has_steal_clock)
|
|
|
|
return;
|
|
|
|
|
|
|
|
memset(st, 0, sizeof(*st));
|
|
|
|
|
x86, kvm: Fix kvm's use of __pa() on percpu areas
In short, it is illegal to call __pa() on an address holding
a percpu variable. This replaces those __pa() calls with
slow_virt_to_phys(). All of the cases in this patch are
in boot time (or CPU hotplug time at worst) code, so the
slow pagetable walking in slow_virt_to_phys() is not expected
to have a performance impact.
The times when this actually matters are pretty obscure
(certain 32-bit NUMA systems), but it _does_ happen. It is
important to keep KVM guests working on these systems because
the real hardware is getting harder and harder to find.
This bug manifested first by me seeing a plain hang at boot
after this message:
CPU 0 irqstacks, hard=f3018000 soft=f301a000
or, sometimes, it would actually make it out to the console:
[ 0.000000] BUG: unable to handle kernel paging request at ffffffff
I eventually traced it down to the KVM async pagefault code.
This can be worked around by disabling that code either at
compile-time, or on the kernel command-line.
The kvm async pagefault code was injecting page faults in
to the guest which the guest misinterpreted because its
"reason" was not being properly sent from the host.
The guest passes a physical address of an per-cpu async page
fault structure via an MSR to the host. Since __pa() is
broken on percpu data, the physical address it sent was
bascially bogus and the host went scribbling on random data.
The guest never saw the real reason for the page fault (it
was injected by the host), assumed that the kernel had taken
a _real_ page fault, and panic()'d. The behavior varied,
though, depending on what got corrupted by the bad write.
Signed-off-by: Dave Hansen <dave@linux.vnet.ibm.com>
Link: http://lkml.kernel.org/r/20130122212435.4905663F@kernel.stglabs.ibm.com
Acked-by: Rik van Riel <riel@redhat.com>
Reviewed-by: Marcelo Tosatti <mtosatti@redhat.com>
Signed-off-by: H. Peter Anvin <hpa@linux.intel.com>
2013-01-22 21:24:35 +00:00
|
|
|
wrmsrl(MSR_KVM_STEAL_TIME, (slow_virt_to_phys(st) | KVM_MSR_ENABLED));
|
2013-02-06 02:57:22 +00:00
|
|
|
pr_info("kvm-stealtime: cpu %d, msr %llx\n",
|
|
|
|
cpu, (unsigned long long) slow_virt_to_phys(st));
|
2011-07-11 19:28:19 +00:00
|
|
|
}
|
|
|
|
|
KVM guest: guest side for eoi avoidance
The idea is simple: there's a bit, per APIC, in guest memory,
that tells the guest that it does not need EOI.
Guest tests it using a single est and clear operation - this is
necessary so that host can detect interrupt nesting - and if set, it can
skip the EOI MSR.
I run a simple microbenchmark to show exit reduction
(note: for testing, need to apply follow-up patch
'kvm: host side for eoi optimization' + a qemu patch
I posted separately, on host):
Before:
Performance counter stats for 'sleep 1s':
47,357 kvm:kvm_entry [99.98%]
0 kvm:kvm_hypercall [99.98%]
0 kvm:kvm_hv_hypercall [99.98%]
5,001 kvm:kvm_pio [99.98%]
0 kvm:kvm_cpuid [99.98%]
22,124 kvm:kvm_apic [99.98%]
49,849 kvm:kvm_exit [99.98%]
21,115 kvm:kvm_inj_virq [99.98%]
0 kvm:kvm_inj_exception [99.98%]
0 kvm:kvm_page_fault [99.98%]
22,937 kvm:kvm_msr [99.98%]
0 kvm:kvm_cr [99.98%]
0 kvm:kvm_pic_set_irq [99.98%]
0 kvm:kvm_apic_ipi [99.98%]
22,207 kvm:kvm_apic_accept_irq [99.98%]
22,421 kvm:kvm_eoi [99.98%]
0 kvm:kvm_pv_eoi [99.99%]
0 kvm:kvm_nested_vmrun [99.99%]
0 kvm:kvm_nested_intercepts [99.99%]
0 kvm:kvm_nested_vmexit [99.99%]
0 kvm:kvm_nested_vmexit_inject [99.99%]
0 kvm:kvm_nested_intr_vmexit [99.99%]
0 kvm:kvm_invlpga [99.99%]
0 kvm:kvm_skinit [99.99%]
57 kvm:kvm_emulate_insn [99.99%]
0 kvm:vcpu_match_mmio [99.99%]
0 kvm:kvm_userspace_exit [99.99%]
2 kvm:kvm_set_irq [99.99%]
2 kvm:kvm_ioapic_set_irq [99.99%]
23,609 kvm:kvm_msi_set_irq [99.99%]
1 kvm:kvm_ack_irq [99.99%]
131 kvm:kvm_mmio [99.99%]
226 kvm:kvm_fpu [100.00%]
0 kvm:kvm_age_page [100.00%]
0 kvm:kvm_try_async_get_page [100.00%]
0 kvm:kvm_async_pf_doublefault [100.00%]
0 kvm:kvm_async_pf_not_present [100.00%]
0 kvm:kvm_async_pf_ready [100.00%]
0 kvm:kvm_async_pf_completed
1.002100578 seconds time elapsed
After:
Performance counter stats for 'sleep 1s':
28,354 kvm:kvm_entry [99.98%]
0 kvm:kvm_hypercall [99.98%]
0 kvm:kvm_hv_hypercall [99.98%]
1,347 kvm:kvm_pio [99.98%]
0 kvm:kvm_cpuid [99.98%]
1,931 kvm:kvm_apic [99.98%]
29,595 kvm:kvm_exit [99.98%]
24,884 kvm:kvm_inj_virq [99.98%]
0 kvm:kvm_inj_exception [99.98%]
0 kvm:kvm_page_fault [99.98%]
1,986 kvm:kvm_msr [99.98%]
0 kvm:kvm_cr [99.98%]
0 kvm:kvm_pic_set_irq [99.98%]
0 kvm:kvm_apic_ipi [99.99%]
25,953 kvm:kvm_apic_accept_irq [99.99%]
26,132 kvm:kvm_eoi [99.99%]
26,593 kvm:kvm_pv_eoi [99.99%]
0 kvm:kvm_nested_vmrun [99.99%]
0 kvm:kvm_nested_intercepts [99.99%]
0 kvm:kvm_nested_vmexit [99.99%]
0 kvm:kvm_nested_vmexit_inject [99.99%]
0 kvm:kvm_nested_intr_vmexit [99.99%]
0 kvm:kvm_invlpga [99.99%]
0 kvm:kvm_skinit [99.99%]
284 kvm:kvm_emulate_insn [99.99%]
68 kvm:vcpu_match_mmio [99.99%]
68 kvm:kvm_userspace_exit [99.99%]
2 kvm:kvm_set_irq [99.99%]
2 kvm:kvm_ioapic_set_irq [99.99%]
28,288 kvm:kvm_msi_set_irq [99.99%]
1 kvm:kvm_ack_irq [99.99%]
131 kvm:kvm_mmio [100.00%]
588 kvm:kvm_fpu [100.00%]
0 kvm:kvm_age_page [100.00%]
0 kvm:kvm_try_async_get_page [100.00%]
0 kvm:kvm_async_pf_doublefault [100.00%]
0 kvm:kvm_async_pf_not_present [100.00%]
0 kvm:kvm_async_pf_ready [100.00%]
0 kvm:kvm_async_pf_completed
1.002039622 seconds time elapsed
We see that # of exits is almost halved.
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
Signed-off-by: Avi Kivity <avi@redhat.com>
2012-06-24 16:24:34 +00:00
|
|
|
static DEFINE_PER_CPU(unsigned long, kvm_apic_eoi) = KVM_PV_EOI_DISABLED;
|
|
|
|
|
|
|
|
static void kvm_guest_apic_eoi_write(u32 reg, u32 val)
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* This relies on __test_and_clear_bit to modify the memory
|
|
|
|
* in a way that is atomic with respect to the local CPU.
|
|
|
|
* The hypervisor only accesses this memory from the local CPU so
|
|
|
|
* there's no need for lock or memory barriers.
|
|
|
|
* An optimization barrier is implied in apic write.
|
|
|
|
*/
|
|
|
|
if (__test_and_clear_bit(KVM_PV_EOI_BIT, &__get_cpu_var(kvm_apic_eoi)))
|
|
|
|
return;
|
2012-07-15 12:56:52 +00:00
|
|
|
apic_write(APIC_EOI, APIC_EOI_ACK);
|
KVM guest: guest side for eoi avoidance
The idea is simple: there's a bit, per APIC, in guest memory,
that tells the guest that it does not need EOI.
Guest tests it using a single est and clear operation - this is
necessary so that host can detect interrupt nesting - and if set, it can
skip the EOI MSR.
I run a simple microbenchmark to show exit reduction
(note: for testing, need to apply follow-up patch
'kvm: host side for eoi optimization' + a qemu patch
I posted separately, on host):
Before:
Performance counter stats for 'sleep 1s':
47,357 kvm:kvm_entry [99.98%]
0 kvm:kvm_hypercall [99.98%]
0 kvm:kvm_hv_hypercall [99.98%]
5,001 kvm:kvm_pio [99.98%]
0 kvm:kvm_cpuid [99.98%]
22,124 kvm:kvm_apic [99.98%]
49,849 kvm:kvm_exit [99.98%]
21,115 kvm:kvm_inj_virq [99.98%]
0 kvm:kvm_inj_exception [99.98%]
0 kvm:kvm_page_fault [99.98%]
22,937 kvm:kvm_msr [99.98%]
0 kvm:kvm_cr [99.98%]
0 kvm:kvm_pic_set_irq [99.98%]
0 kvm:kvm_apic_ipi [99.98%]
22,207 kvm:kvm_apic_accept_irq [99.98%]
22,421 kvm:kvm_eoi [99.98%]
0 kvm:kvm_pv_eoi [99.99%]
0 kvm:kvm_nested_vmrun [99.99%]
0 kvm:kvm_nested_intercepts [99.99%]
0 kvm:kvm_nested_vmexit [99.99%]
0 kvm:kvm_nested_vmexit_inject [99.99%]
0 kvm:kvm_nested_intr_vmexit [99.99%]
0 kvm:kvm_invlpga [99.99%]
0 kvm:kvm_skinit [99.99%]
57 kvm:kvm_emulate_insn [99.99%]
0 kvm:vcpu_match_mmio [99.99%]
0 kvm:kvm_userspace_exit [99.99%]
2 kvm:kvm_set_irq [99.99%]
2 kvm:kvm_ioapic_set_irq [99.99%]
23,609 kvm:kvm_msi_set_irq [99.99%]
1 kvm:kvm_ack_irq [99.99%]
131 kvm:kvm_mmio [99.99%]
226 kvm:kvm_fpu [100.00%]
0 kvm:kvm_age_page [100.00%]
0 kvm:kvm_try_async_get_page [100.00%]
0 kvm:kvm_async_pf_doublefault [100.00%]
0 kvm:kvm_async_pf_not_present [100.00%]
0 kvm:kvm_async_pf_ready [100.00%]
0 kvm:kvm_async_pf_completed
1.002100578 seconds time elapsed
After:
Performance counter stats for 'sleep 1s':
28,354 kvm:kvm_entry [99.98%]
0 kvm:kvm_hypercall [99.98%]
0 kvm:kvm_hv_hypercall [99.98%]
1,347 kvm:kvm_pio [99.98%]
0 kvm:kvm_cpuid [99.98%]
1,931 kvm:kvm_apic [99.98%]
29,595 kvm:kvm_exit [99.98%]
24,884 kvm:kvm_inj_virq [99.98%]
0 kvm:kvm_inj_exception [99.98%]
0 kvm:kvm_page_fault [99.98%]
1,986 kvm:kvm_msr [99.98%]
0 kvm:kvm_cr [99.98%]
0 kvm:kvm_pic_set_irq [99.98%]
0 kvm:kvm_apic_ipi [99.99%]
25,953 kvm:kvm_apic_accept_irq [99.99%]
26,132 kvm:kvm_eoi [99.99%]
26,593 kvm:kvm_pv_eoi [99.99%]
0 kvm:kvm_nested_vmrun [99.99%]
0 kvm:kvm_nested_intercepts [99.99%]
0 kvm:kvm_nested_vmexit [99.99%]
0 kvm:kvm_nested_vmexit_inject [99.99%]
0 kvm:kvm_nested_intr_vmexit [99.99%]
0 kvm:kvm_invlpga [99.99%]
0 kvm:kvm_skinit [99.99%]
284 kvm:kvm_emulate_insn [99.99%]
68 kvm:vcpu_match_mmio [99.99%]
68 kvm:kvm_userspace_exit [99.99%]
2 kvm:kvm_set_irq [99.99%]
2 kvm:kvm_ioapic_set_irq [99.99%]
28,288 kvm:kvm_msi_set_irq [99.99%]
1 kvm:kvm_ack_irq [99.99%]
131 kvm:kvm_mmio [100.00%]
588 kvm:kvm_fpu [100.00%]
0 kvm:kvm_age_page [100.00%]
0 kvm:kvm_try_async_get_page [100.00%]
0 kvm:kvm_async_pf_doublefault [100.00%]
0 kvm:kvm_async_pf_not_present [100.00%]
0 kvm:kvm_async_pf_ready [100.00%]
0 kvm:kvm_async_pf_completed
1.002039622 seconds time elapsed
We see that # of exits is almost halved.
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
Signed-off-by: Avi Kivity <avi@redhat.com>
2012-06-24 16:24:34 +00:00
|
|
|
}
|
|
|
|
|
x86: delete __cpuinit usage from all x86 files
The __cpuinit type of throwaway sections might have made sense
some time ago when RAM was more constrained, but now the savings
do not offset the cost and complications. For example, the fix in
commit 5e427ec2d0 ("x86: Fix bit corruption at CPU resume time")
is a good example of the nasty type of bugs that can be created
with improper use of the various __init prefixes.
After a discussion on LKML[1] it was decided that cpuinit should go
the way of devinit and be phased out. Once all the users are gone,
we can then finally remove the macros themselves from linux/init.h.
Note that some harmless section mismatch warnings may result, since
notify_cpu_starting() and cpu_up() are arch independent (kernel/cpu.c)
are flagged as __cpuinit -- so if we remove the __cpuinit from
arch specific callers, we will also get section mismatch warnings.
As an intermediate step, we intend to turn the linux/init.h cpuinit
content into no-ops as early as possible, since that will get rid
of these warnings. In any case, they are temporary and harmless.
This removes all the arch/x86 uses of the __cpuinit macros from
all C files. x86 only had the one __CPUINIT used in assembly files,
and it wasn't paired off with a .previous or a __FINIT, so we can
delete it directly w/o any corresponding additional change there.
[1] https://lkml.org/lkml/2013/5/20/589
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: "H. Peter Anvin" <hpa@zytor.com>
Cc: x86@kernel.org
Acked-by: Ingo Molnar <mingo@kernel.org>
Acked-by: Thomas Gleixner <tglx@linutronix.de>
Acked-by: H. Peter Anvin <hpa@linux.intel.com>
Signed-off-by: Paul Gortmaker <paul.gortmaker@windriver.com>
2013-06-18 22:23:59 +00:00
|
|
|
void kvm_guest_cpu_init(void)
|
2010-10-14 09:22:51 +00:00
|
|
|
{
|
|
|
|
if (!kvm_para_available())
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (kvm_para_has_feature(KVM_FEATURE_ASYNC_PF) && kvmapf) {
|
x86, kvm: Fix kvm's use of __pa() on percpu areas
In short, it is illegal to call __pa() on an address holding
a percpu variable. This replaces those __pa() calls with
slow_virt_to_phys(). All of the cases in this patch are
in boot time (or CPU hotplug time at worst) code, so the
slow pagetable walking in slow_virt_to_phys() is not expected
to have a performance impact.
The times when this actually matters are pretty obscure
(certain 32-bit NUMA systems), but it _does_ happen. It is
important to keep KVM guests working on these systems because
the real hardware is getting harder and harder to find.
This bug manifested first by me seeing a plain hang at boot
after this message:
CPU 0 irqstacks, hard=f3018000 soft=f301a000
or, sometimes, it would actually make it out to the console:
[ 0.000000] BUG: unable to handle kernel paging request at ffffffff
I eventually traced it down to the KVM async pagefault code.
This can be worked around by disabling that code either at
compile-time, or on the kernel command-line.
The kvm async pagefault code was injecting page faults in
to the guest which the guest misinterpreted because its
"reason" was not being properly sent from the host.
The guest passes a physical address of an per-cpu async page
fault structure via an MSR to the host. Since __pa() is
broken on percpu data, the physical address it sent was
bascially bogus and the host went scribbling on random data.
The guest never saw the real reason for the page fault (it
was injected by the host), assumed that the kernel had taken
a _real_ page fault, and panic()'d. The behavior varied,
though, depending on what got corrupted by the bad write.
Signed-off-by: Dave Hansen <dave@linux.vnet.ibm.com>
Link: http://lkml.kernel.org/r/20130122212435.4905663F@kernel.stglabs.ibm.com
Acked-by: Rik van Riel <riel@redhat.com>
Reviewed-by: Marcelo Tosatti <mtosatti@redhat.com>
Signed-off-by: H. Peter Anvin <hpa@linux.intel.com>
2013-01-22 21:24:35 +00:00
|
|
|
u64 pa = slow_virt_to_phys(&__get_cpu_var(apf_reason));
|
2010-10-14 09:22:51 +00:00
|
|
|
|
2010-10-14 09:22:55 +00:00
|
|
|
#ifdef CONFIG_PREEMPT
|
|
|
|
pa |= KVM_ASYNC_PF_SEND_ALWAYS;
|
|
|
|
#endif
|
2010-10-14 09:22:51 +00:00
|
|
|
wrmsrl(MSR_KVM_ASYNC_PF_EN, pa | KVM_ASYNC_PF_ENABLED);
|
|
|
|
__get_cpu_var(apf_reason).enabled = 1;
|
|
|
|
printk(KERN_INFO"KVM setup async PF for cpu %d\n",
|
|
|
|
smp_processor_id());
|
|
|
|
}
|
2011-07-11 19:28:19 +00:00
|
|
|
|
KVM guest: guest side for eoi avoidance
The idea is simple: there's a bit, per APIC, in guest memory,
that tells the guest that it does not need EOI.
Guest tests it using a single est and clear operation - this is
necessary so that host can detect interrupt nesting - and if set, it can
skip the EOI MSR.
I run a simple microbenchmark to show exit reduction
(note: for testing, need to apply follow-up patch
'kvm: host side for eoi optimization' + a qemu patch
I posted separately, on host):
Before:
Performance counter stats for 'sleep 1s':
47,357 kvm:kvm_entry [99.98%]
0 kvm:kvm_hypercall [99.98%]
0 kvm:kvm_hv_hypercall [99.98%]
5,001 kvm:kvm_pio [99.98%]
0 kvm:kvm_cpuid [99.98%]
22,124 kvm:kvm_apic [99.98%]
49,849 kvm:kvm_exit [99.98%]
21,115 kvm:kvm_inj_virq [99.98%]
0 kvm:kvm_inj_exception [99.98%]
0 kvm:kvm_page_fault [99.98%]
22,937 kvm:kvm_msr [99.98%]
0 kvm:kvm_cr [99.98%]
0 kvm:kvm_pic_set_irq [99.98%]
0 kvm:kvm_apic_ipi [99.98%]
22,207 kvm:kvm_apic_accept_irq [99.98%]
22,421 kvm:kvm_eoi [99.98%]
0 kvm:kvm_pv_eoi [99.99%]
0 kvm:kvm_nested_vmrun [99.99%]
0 kvm:kvm_nested_intercepts [99.99%]
0 kvm:kvm_nested_vmexit [99.99%]
0 kvm:kvm_nested_vmexit_inject [99.99%]
0 kvm:kvm_nested_intr_vmexit [99.99%]
0 kvm:kvm_invlpga [99.99%]
0 kvm:kvm_skinit [99.99%]
57 kvm:kvm_emulate_insn [99.99%]
0 kvm:vcpu_match_mmio [99.99%]
0 kvm:kvm_userspace_exit [99.99%]
2 kvm:kvm_set_irq [99.99%]
2 kvm:kvm_ioapic_set_irq [99.99%]
23,609 kvm:kvm_msi_set_irq [99.99%]
1 kvm:kvm_ack_irq [99.99%]
131 kvm:kvm_mmio [99.99%]
226 kvm:kvm_fpu [100.00%]
0 kvm:kvm_age_page [100.00%]
0 kvm:kvm_try_async_get_page [100.00%]
0 kvm:kvm_async_pf_doublefault [100.00%]
0 kvm:kvm_async_pf_not_present [100.00%]
0 kvm:kvm_async_pf_ready [100.00%]
0 kvm:kvm_async_pf_completed
1.002100578 seconds time elapsed
After:
Performance counter stats for 'sleep 1s':
28,354 kvm:kvm_entry [99.98%]
0 kvm:kvm_hypercall [99.98%]
0 kvm:kvm_hv_hypercall [99.98%]
1,347 kvm:kvm_pio [99.98%]
0 kvm:kvm_cpuid [99.98%]
1,931 kvm:kvm_apic [99.98%]
29,595 kvm:kvm_exit [99.98%]
24,884 kvm:kvm_inj_virq [99.98%]
0 kvm:kvm_inj_exception [99.98%]
0 kvm:kvm_page_fault [99.98%]
1,986 kvm:kvm_msr [99.98%]
0 kvm:kvm_cr [99.98%]
0 kvm:kvm_pic_set_irq [99.98%]
0 kvm:kvm_apic_ipi [99.99%]
25,953 kvm:kvm_apic_accept_irq [99.99%]
26,132 kvm:kvm_eoi [99.99%]
26,593 kvm:kvm_pv_eoi [99.99%]
0 kvm:kvm_nested_vmrun [99.99%]
0 kvm:kvm_nested_intercepts [99.99%]
0 kvm:kvm_nested_vmexit [99.99%]
0 kvm:kvm_nested_vmexit_inject [99.99%]
0 kvm:kvm_nested_intr_vmexit [99.99%]
0 kvm:kvm_invlpga [99.99%]
0 kvm:kvm_skinit [99.99%]
284 kvm:kvm_emulate_insn [99.99%]
68 kvm:vcpu_match_mmio [99.99%]
68 kvm:kvm_userspace_exit [99.99%]
2 kvm:kvm_set_irq [99.99%]
2 kvm:kvm_ioapic_set_irq [99.99%]
28,288 kvm:kvm_msi_set_irq [99.99%]
1 kvm:kvm_ack_irq [99.99%]
131 kvm:kvm_mmio [100.00%]
588 kvm:kvm_fpu [100.00%]
0 kvm:kvm_age_page [100.00%]
0 kvm:kvm_try_async_get_page [100.00%]
0 kvm:kvm_async_pf_doublefault [100.00%]
0 kvm:kvm_async_pf_not_present [100.00%]
0 kvm:kvm_async_pf_ready [100.00%]
0 kvm:kvm_async_pf_completed
1.002039622 seconds time elapsed
We see that # of exits is almost halved.
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
Signed-off-by: Avi Kivity <avi@redhat.com>
2012-06-24 16:24:34 +00:00
|
|
|
if (kvm_para_has_feature(KVM_FEATURE_PV_EOI)) {
|
|
|
|
unsigned long pa;
|
|
|
|
/* Size alignment is implied but just to make it explicit. */
|
|
|
|
BUILD_BUG_ON(__alignof__(kvm_apic_eoi) < 4);
|
|
|
|
__get_cpu_var(kvm_apic_eoi) = 0;
|
x86, kvm: Fix kvm's use of __pa() on percpu areas
In short, it is illegal to call __pa() on an address holding
a percpu variable. This replaces those __pa() calls with
slow_virt_to_phys(). All of the cases in this patch are
in boot time (or CPU hotplug time at worst) code, so the
slow pagetable walking in slow_virt_to_phys() is not expected
to have a performance impact.
The times when this actually matters are pretty obscure
(certain 32-bit NUMA systems), but it _does_ happen. It is
important to keep KVM guests working on these systems because
the real hardware is getting harder and harder to find.
This bug manifested first by me seeing a plain hang at boot
after this message:
CPU 0 irqstacks, hard=f3018000 soft=f301a000
or, sometimes, it would actually make it out to the console:
[ 0.000000] BUG: unable to handle kernel paging request at ffffffff
I eventually traced it down to the KVM async pagefault code.
This can be worked around by disabling that code either at
compile-time, or on the kernel command-line.
The kvm async pagefault code was injecting page faults in
to the guest which the guest misinterpreted because its
"reason" was not being properly sent from the host.
The guest passes a physical address of an per-cpu async page
fault structure via an MSR to the host. Since __pa() is
broken on percpu data, the physical address it sent was
bascially bogus and the host went scribbling on random data.
The guest never saw the real reason for the page fault (it
was injected by the host), assumed that the kernel had taken
a _real_ page fault, and panic()'d. The behavior varied,
though, depending on what got corrupted by the bad write.
Signed-off-by: Dave Hansen <dave@linux.vnet.ibm.com>
Link: http://lkml.kernel.org/r/20130122212435.4905663F@kernel.stglabs.ibm.com
Acked-by: Rik van Riel <riel@redhat.com>
Reviewed-by: Marcelo Tosatti <mtosatti@redhat.com>
Signed-off-by: H. Peter Anvin <hpa@linux.intel.com>
2013-01-22 21:24:35 +00:00
|
|
|
pa = slow_virt_to_phys(&__get_cpu_var(kvm_apic_eoi))
|
|
|
|
| KVM_MSR_ENABLED;
|
KVM guest: guest side for eoi avoidance
The idea is simple: there's a bit, per APIC, in guest memory,
that tells the guest that it does not need EOI.
Guest tests it using a single est and clear operation - this is
necessary so that host can detect interrupt nesting - and if set, it can
skip the EOI MSR.
I run a simple microbenchmark to show exit reduction
(note: for testing, need to apply follow-up patch
'kvm: host side for eoi optimization' + a qemu patch
I posted separately, on host):
Before:
Performance counter stats for 'sleep 1s':
47,357 kvm:kvm_entry [99.98%]
0 kvm:kvm_hypercall [99.98%]
0 kvm:kvm_hv_hypercall [99.98%]
5,001 kvm:kvm_pio [99.98%]
0 kvm:kvm_cpuid [99.98%]
22,124 kvm:kvm_apic [99.98%]
49,849 kvm:kvm_exit [99.98%]
21,115 kvm:kvm_inj_virq [99.98%]
0 kvm:kvm_inj_exception [99.98%]
0 kvm:kvm_page_fault [99.98%]
22,937 kvm:kvm_msr [99.98%]
0 kvm:kvm_cr [99.98%]
0 kvm:kvm_pic_set_irq [99.98%]
0 kvm:kvm_apic_ipi [99.98%]
22,207 kvm:kvm_apic_accept_irq [99.98%]
22,421 kvm:kvm_eoi [99.98%]
0 kvm:kvm_pv_eoi [99.99%]
0 kvm:kvm_nested_vmrun [99.99%]
0 kvm:kvm_nested_intercepts [99.99%]
0 kvm:kvm_nested_vmexit [99.99%]
0 kvm:kvm_nested_vmexit_inject [99.99%]
0 kvm:kvm_nested_intr_vmexit [99.99%]
0 kvm:kvm_invlpga [99.99%]
0 kvm:kvm_skinit [99.99%]
57 kvm:kvm_emulate_insn [99.99%]
0 kvm:vcpu_match_mmio [99.99%]
0 kvm:kvm_userspace_exit [99.99%]
2 kvm:kvm_set_irq [99.99%]
2 kvm:kvm_ioapic_set_irq [99.99%]
23,609 kvm:kvm_msi_set_irq [99.99%]
1 kvm:kvm_ack_irq [99.99%]
131 kvm:kvm_mmio [99.99%]
226 kvm:kvm_fpu [100.00%]
0 kvm:kvm_age_page [100.00%]
0 kvm:kvm_try_async_get_page [100.00%]
0 kvm:kvm_async_pf_doublefault [100.00%]
0 kvm:kvm_async_pf_not_present [100.00%]
0 kvm:kvm_async_pf_ready [100.00%]
0 kvm:kvm_async_pf_completed
1.002100578 seconds time elapsed
After:
Performance counter stats for 'sleep 1s':
28,354 kvm:kvm_entry [99.98%]
0 kvm:kvm_hypercall [99.98%]
0 kvm:kvm_hv_hypercall [99.98%]
1,347 kvm:kvm_pio [99.98%]
0 kvm:kvm_cpuid [99.98%]
1,931 kvm:kvm_apic [99.98%]
29,595 kvm:kvm_exit [99.98%]
24,884 kvm:kvm_inj_virq [99.98%]
0 kvm:kvm_inj_exception [99.98%]
0 kvm:kvm_page_fault [99.98%]
1,986 kvm:kvm_msr [99.98%]
0 kvm:kvm_cr [99.98%]
0 kvm:kvm_pic_set_irq [99.98%]
0 kvm:kvm_apic_ipi [99.99%]
25,953 kvm:kvm_apic_accept_irq [99.99%]
26,132 kvm:kvm_eoi [99.99%]
26,593 kvm:kvm_pv_eoi [99.99%]
0 kvm:kvm_nested_vmrun [99.99%]
0 kvm:kvm_nested_intercepts [99.99%]
0 kvm:kvm_nested_vmexit [99.99%]
0 kvm:kvm_nested_vmexit_inject [99.99%]
0 kvm:kvm_nested_intr_vmexit [99.99%]
0 kvm:kvm_invlpga [99.99%]
0 kvm:kvm_skinit [99.99%]
284 kvm:kvm_emulate_insn [99.99%]
68 kvm:vcpu_match_mmio [99.99%]
68 kvm:kvm_userspace_exit [99.99%]
2 kvm:kvm_set_irq [99.99%]
2 kvm:kvm_ioapic_set_irq [99.99%]
28,288 kvm:kvm_msi_set_irq [99.99%]
1 kvm:kvm_ack_irq [99.99%]
131 kvm:kvm_mmio [100.00%]
588 kvm:kvm_fpu [100.00%]
0 kvm:kvm_age_page [100.00%]
0 kvm:kvm_try_async_get_page [100.00%]
0 kvm:kvm_async_pf_doublefault [100.00%]
0 kvm:kvm_async_pf_not_present [100.00%]
0 kvm:kvm_async_pf_ready [100.00%]
0 kvm:kvm_async_pf_completed
1.002039622 seconds time elapsed
We see that # of exits is almost halved.
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
Signed-off-by: Avi Kivity <avi@redhat.com>
2012-06-24 16:24:34 +00:00
|
|
|
wrmsrl(MSR_KVM_PV_EOI_EN, pa);
|
|
|
|
}
|
|
|
|
|
2011-07-11 19:28:19 +00:00
|
|
|
if (has_steal_clock)
|
|
|
|
kvm_register_steal_time();
|
2010-10-14 09:22:51 +00:00
|
|
|
}
|
|
|
|
|
KVM guest: guest side for eoi avoidance
The idea is simple: there's a bit, per APIC, in guest memory,
that tells the guest that it does not need EOI.
Guest tests it using a single est and clear operation - this is
necessary so that host can detect interrupt nesting - and if set, it can
skip the EOI MSR.
I run a simple microbenchmark to show exit reduction
(note: for testing, need to apply follow-up patch
'kvm: host side for eoi optimization' + a qemu patch
I posted separately, on host):
Before:
Performance counter stats for 'sleep 1s':
47,357 kvm:kvm_entry [99.98%]
0 kvm:kvm_hypercall [99.98%]
0 kvm:kvm_hv_hypercall [99.98%]
5,001 kvm:kvm_pio [99.98%]
0 kvm:kvm_cpuid [99.98%]
22,124 kvm:kvm_apic [99.98%]
49,849 kvm:kvm_exit [99.98%]
21,115 kvm:kvm_inj_virq [99.98%]
0 kvm:kvm_inj_exception [99.98%]
0 kvm:kvm_page_fault [99.98%]
22,937 kvm:kvm_msr [99.98%]
0 kvm:kvm_cr [99.98%]
0 kvm:kvm_pic_set_irq [99.98%]
0 kvm:kvm_apic_ipi [99.98%]
22,207 kvm:kvm_apic_accept_irq [99.98%]
22,421 kvm:kvm_eoi [99.98%]
0 kvm:kvm_pv_eoi [99.99%]
0 kvm:kvm_nested_vmrun [99.99%]
0 kvm:kvm_nested_intercepts [99.99%]
0 kvm:kvm_nested_vmexit [99.99%]
0 kvm:kvm_nested_vmexit_inject [99.99%]
0 kvm:kvm_nested_intr_vmexit [99.99%]
0 kvm:kvm_invlpga [99.99%]
0 kvm:kvm_skinit [99.99%]
57 kvm:kvm_emulate_insn [99.99%]
0 kvm:vcpu_match_mmio [99.99%]
0 kvm:kvm_userspace_exit [99.99%]
2 kvm:kvm_set_irq [99.99%]
2 kvm:kvm_ioapic_set_irq [99.99%]
23,609 kvm:kvm_msi_set_irq [99.99%]
1 kvm:kvm_ack_irq [99.99%]
131 kvm:kvm_mmio [99.99%]
226 kvm:kvm_fpu [100.00%]
0 kvm:kvm_age_page [100.00%]
0 kvm:kvm_try_async_get_page [100.00%]
0 kvm:kvm_async_pf_doublefault [100.00%]
0 kvm:kvm_async_pf_not_present [100.00%]
0 kvm:kvm_async_pf_ready [100.00%]
0 kvm:kvm_async_pf_completed
1.002100578 seconds time elapsed
After:
Performance counter stats for 'sleep 1s':
28,354 kvm:kvm_entry [99.98%]
0 kvm:kvm_hypercall [99.98%]
0 kvm:kvm_hv_hypercall [99.98%]
1,347 kvm:kvm_pio [99.98%]
0 kvm:kvm_cpuid [99.98%]
1,931 kvm:kvm_apic [99.98%]
29,595 kvm:kvm_exit [99.98%]
24,884 kvm:kvm_inj_virq [99.98%]
0 kvm:kvm_inj_exception [99.98%]
0 kvm:kvm_page_fault [99.98%]
1,986 kvm:kvm_msr [99.98%]
0 kvm:kvm_cr [99.98%]
0 kvm:kvm_pic_set_irq [99.98%]
0 kvm:kvm_apic_ipi [99.99%]
25,953 kvm:kvm_apic_accept_irq [99.99%]
26,132 kvm:kvm_eoi [99.99%]
26,593 kvm:kvm_pv_eoi [99.99%]
0 kvm:kvm_nested_vmrun [99.99%]
0 kvm:kvm_nested_intercepts [99.99%]
0 kvm:kvm_nested_vmexit [99.99%]
0 kvm:kvm_nested_vmexit_inject [99.99%]
0 kvm:kvm_nested_intr_vmexit [99.99%]
0 kvm:kvm_invlpga [99.99%]
0 kvm:kvm_skinit [99.99%]
284 kvm:kvm_emulate_insn [99.99%]
68 kvm:vcpu_match_mmio [99.99%]
68 kvm:kvm_userspace_exit [99.99%]
2 kvm:kvm_set_irq [99.99%]
2 kvm:kvm_ioapic_set_irq [99.99%]
28,288 kvm:kvm_msi_set_irq [99.99%]
1 kvm:kvm_ack_irq [99.99%]
131 kvm:kvm_mmio [100.00%]
588 kvm:kvm_fpu [100.00%]
0 kvm:kvm_age_page [100.00%]
0 kvm:kvm_try_async_get_page [100.00%]
0 kvm:kvm_async_pf_doublefault [100.00%]
0 kvm:kvm_async_pf_not_present [100.00%]
0 kvm:kvm_async_pf_ready [100.00%]
0 kvm:kvm_async_pf_completed
1.002039622 seconds time elapsed
We see that # of exits is almost halved.
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
Signed-off-by: Avi Kivity <avi@redhat.com>
2012-06-24 16:24:34 +00:00
|
|
|
static void kvm_pv_disable_apf(void)
|
2010-10-14 09:22:51 +00:00
|
|
|
{
|
|
|
|
if (!__get_cpu_var(apf_reason).enabled)
|
|
|
|
return;
|
|
|
|
|
|
|
|
wrmsrl(MSR_KVM_ASYNC_PF_EN, 0);
|
|
|
|
__get_cpu_var(apf_reason).enabled = 0;
|
|
|
|
|
|
|
|
printk(KERN_INFO"Unregister pv shared memory for cpu %d\n",
|
|
|
|
smp_processor_id());
|
|
|
|
}
|
|
|
|
|
KVM guest: guest side for eoi avoidance
The idea is simple: there's a bit, per APIC, in guest memory,
that tells the guest that it does not need EOI.
Guest tests it using a single est and clear operation - this is
necessary so that host can detect interrupt nesting - and if set, it can
skip the EOI MSR.
I run a simple microbenchmark to show exit reduction
(note: for testing, need to apply follow-up patch
'kvm: host side for eoi optimization' + a qemu patch
I posted separately, on host):
Before:
Performance counter stats for 'sleep 1s':
47,357 kvm:kvm_entry [99.98%]
0 kvm:kvm_hypercall [99.98%]
0 kvm:kvm_hv_hypercall [99.98%]
5,001 kvm:kvm_pio [99.98%]
0 kvm:kvm_cpuid [99.98%]
22,124 kvm:kvm_apic [99.98%]
49,849 kvm:kvm_exit [99.98%]
21,115 kvm:kvm_inj_virq [99.98%]
0 kvm:kvm_inj_exception [99.98%]
0 kvm:kvm_page_fault [99.98%]
22,937 kvm:kvm_msr [99.98%]
0 kvm:kvm_cr [99.98%]
0 kvm:kvm_pic_set_irq [99.98%]
0 kvm:kvm_apic_ipi [99.98%]
22,207 kvm:kvm_apic_accept_irq [99.98%]
22,421 kvm:kvm_eoi [99.98%]
0 kvm:kvm_pv_eoi [99.99%]
0 kvm:kvm_nested_vmrun [99.99%]
0 kvm:kvm_nested_intercepts [99.99%]
0 kvm:kvm_nested_vmexit [99.99%]
0 kvm:kvm_nested_vmexit_inject [99.99%]
0 kvm:kvm_nested_intr_vmexit [99.99%]
0 kvm:kvm_invlpga [99.99%]
0 kvm:kvm_skinit [99.99%]
57 kvm:kvm_emulate_insn [99.99%]
0 kvm:vcpu_match_mmio [99.99%]
0 kvm:kvm_userspace_exit [99.99%]
2 kvm:kvm_set_irq [99.99%]
2 kvm:kvm_ioapic_set_irq [99.99%]
23,609 kvm:kvm_msi_set_irq [99.99%]
1 kvm:kvm_ack_irq [99.99%]
131 kvm:kvm_mmio [99.99%]
226 kvm:kvm_fpu [100.00%]
0 kvm:kvm_age_page [100.00%]
0 kvm:kvm_try_async_get_page [100.00%]
0 kvm:kvm_async_pf_doublefault [100.00%]
0 kvm:kvm_async_pf_not_present [100.00%]
0 kvm:kvm_async_pf_ready [100.00%]
0 kvm:kvm_async_pf_completed
1.002100578 seconds time elapsed
After:
Performance counter stats for 'sleep 1s':
28,354 kvm:kvm_entry [99.98%]
0 kvm:kvm_hypercall [99.98%]
0 kvm:kvm_hv_hypercall [99.98%]
1,347 kvm:kvm_pio [99.98%]
0 kvm:kvm_cpuid [99.98%]
1,931 kvm:kvm_apic [99.98%]
29,595 kvm:kvm_exit [99.98%]
24,884 kvm:kvm_inj_virq [99.98%]
0 kvm:kvm_inj_exception [99.98%]
0 kvm:kvm_page_fault [99.98%]
1,986 kvm:kvm_msr [99.98%]
0 kvm:kvm_cr [99.98%]
0 kvm:kvm_pic_set_irq [99.98%]
0 kvm:kvm_apic_ipi [99.99%]
25,953 kvm:kvm_apic_accept_irq [99.99%]
26,132 kvm:kvm_eoi [99.99%]
26,593 kvm:kvm_pv_eoi [99.99%]
0 kvm:kvm_nested_vmrun [99.99%]
0 kvm:kvm_nested_intercepts [99.99%]
0 kvm:kvm_nested_vmexit [99.99%]
0 kvm:kvm_nested_vmexit_inject [99.99%]
0 kvm:kvm_nested_intr_vmexit [99.99%]
0 kvm:kvm_invlpga [99.99%]
0 kvm:kvm_skinit [99.99%]
284 kvm:kvm_emulate_insn [99.99%]
68 kvm:vcpu_match_mmio [99.99%]
68 kvm:kvm_userspace_exit [99.99%]
2 kvm:kvm_set_irq [99.99%]
2 kvm:kvm_ioapic_set_irq [99.99%]
28,288 kvm:kvm_msi_set_irq [99.99%]
1 kvm:kvm_ack_irq [99.99%]
131 kvm:kvm_mmio [100.00%]
588 kvm:kvm_fpu [100.00%]
0 kvm:kvm_age_page [100.00%]
0 kvm:kvm_try_async_get_page [100.00%]
0 kvm:kvm_async_pf_doublefault [100.00%]
0 kvm:kvm_async_pf_not_present [100.00%]
0 kvm:kvm_async_pf_ready [100.00%]
0 kvm:kvm_async_pf_completed
1.002039622 seconds time elapsed
We see that # of exits is almost halved.
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
Signed-off-by: Avi Kivity <avi@redhat.com>
2012-06-24 16:24:34 +00:00
|
|
|
static void kvm_pv_guest_cpu_reboot(void *unused)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* We disable PV EOI before we load a new kernel by kexec,
|
|
|
|
* since MSR_KVM_PV_EOI_EN stores a pointer into old kernel's memory.
|
|
|
|
* New kernel can re-enable when it boots.
|
|
|
|
*/
|
|
|
|
if (kvm_para_has_feature(KVM_FEATURE_PV_EOI))
|
|
|
|
wrmsrl(MSR_KVM_PV_EOI_EN, 0);
|
|
|
|
kvm_pv_disable_apf();
|
2012-08-15 14:00:40 +00:00
|
|
|
kvm_disable_steal_time();
|
KVM guest: guest side for eoi avoidance
The idea is simple: there's a bit, per APIC, in guest memory,
that tells the guest that it does not need EOI.
Guest tests it using a single est and clear operation - this is
necessary so that host can detect interrupt nesting - and if set, it can
skip the EOI MSR.
I run a simple microbenchmark to show exit reduction
(note: for testing, need to apply follow-up patch
'kvm: host side for eoi optimization' + a qemu patch
I posted separately, on host):
Before:
Performance counter stats for 'sleep 1s':
47,357 kvm:kvm_entry [99.98%]
0 kvm:kvm_hypercall [99.98%]
0 kvm:kvm_hv_hypercall [99.98%]
5,001 kvm:kvm_pio [99.98%]
0 kvm:kvm_cpuid [99.98%]
22,124 kvm:kvm_apic [99.98%]
49,849 kvm:kvm_exit [99.98%]
21,115 kvm:kvm_inj_virq [99.98%]
0 kvm:kvm_inj_exception [99.98%]
0 kvm:kvm_page_fault [99.98%]
22,937 kvm:kvm_msr [99.98%]
0 kvm:kvm_cr [99.98%]
0 kvm:kvm_pic_set_irq [99.98%]
0 kvm:kvm_apic_ipi [99.98%]
22,207 kvm:kvm_apic_accept_irq [99.98%]
22,421 kvm:kvm_eoi [99.98%]
0 kvm:kvm_pv_eoi [99.99%]
0 kvm:kvm_nested_vmrun [99.99%]
0 kvm:kvm_nested_intercepts [99.99%]
0 kvm:kvm_nested_vmexit [99.99%]
0 kvm:kvm_nested_vmexit_inject [99.99%]
0 kvm:kvm_nested_intr_vmexit [99.99%]
0 kvm:kvm_invlpga [99.99%]
0 kvm:kvm_skinit [99.99%]
57 kvm:kvm_emulate_insn [99.99%]
0 kvm:vcpu_match_mmio [99.99%]
0 kvm:kvm_userspace_exit [99.99%]
2 kvm:kvm_set_irq [99.99%]
2 kvm:kvm_ioapic_set_irq [99.99%]
23,609 kvm:kvm_msi_set_irq [99.99%]
1 kvm:kvm_ack_irq [99.99%]
131 kvm:kvm_mmio [99.99%]
226 kvm:kvm_fpu [100.00%]
0 kvm:kvm_age_page [100.00%]
0 kvm:kvm_try_async_get_page [100.00%]
0 kvm:kvm_async_pf_doublefault [100.00%]
0 kvm:kvm_async_pf_not_present [100.00%]
0 kvm:kvm_async_pf_ready [100.00%]
0 kvm:kvm_async_pf_completed
1.002100578 seconds time elapsed
After:
Performance counter stats for 'sleep 1s':
28,354 kvm:kvm_entry [99.98%]
0 kvm:kvm_hypercall [99.98%]
0 kvm:kvm_hv_hypercall [99.98%]
1,347 kvm:kvm_pio [99.98%]
0 kvm:kvm_cpuid [99.98%]
1,931 kvm:kvm_apic [99.98%]
29,595 kvm:kvm_exit [99.98%]
24,884 kvm:kvm_inj_virq [99.98%]
0 kvm:kvm_inj_exception [99.98%]
0 kvm:kvm_page_fault [99.98%]
1,986 kvm:kvm_msr [99.98%]
0 kvm:kvm_cr [99.98%]
0 kvm:kvm_pic_set_irq [99.98%]
0 kvm:kvm_apic_ipi [99.99%]
25,953 kvm:kvm_apic_accept_irq [99.99%]
26,132 kvm:kvm_eoi [99.99%]
26,593 kvm:kvm_pv_eoi [99.99%]
0 kvm:kvm_nested_vmrun [99.99%]
0 kvm:kvm_nested_intercepts [99.99%]
0 kvm:kvm_nested_vmexit [99.99%]
0 kvm:kvm_nested_vmexit_inject [99.99%]
0 kvm:kvm_nested_intr_vmexit [99.99%]
0 kvm:kvm_invlpga [99.99%]
0 kvm:kvm_skinit [99.99%]
284 kvm:kvm_emulate_insn [99.99%]
68 kvm:vcpu_match_mmio [99.99%]
68 kvm:kvm_userspace_exit [99.99%]
2 kvm:kvm_set_irq [99.99%]
2 kvm:kvm_ioapic_set_irq [99.99%]
28,288 kvm:kvm_msi_set_irq [99.99%]
1 kvm:kvm_ack_irq [99.99%]
131 kvm:kvm_mmio [100.00%]
588 kvm:kvm_fpu [100.00%]
0 kvm:kvm_age_page [100.00%]
0 kvm:kvm_try_async_get_page [100.00%]
0 kvm:kvm_async_pf_doublefault [100.00%]
0 kvm:kvm_async_pf_not_present [100.00%]
0 kvm:kvm_async_pf_ready [100.00%]
0 kvm:kvm_async_pf_completed
1.002039622 seconds time elapsed
We see that # of exits is almost halved.
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
Signed-off-by: Avi Kivity <avi@redhat.com>
2012-06-24 16:24:34 +00:00
|
|
|
}
|
|
|
|
|
2010-10-14 09:22:51 +00:00
|
|
|
static int kvm_pv_reboot_notify(struct notifier_block *nb,
|
|
|
|
unsigned long code, void *unused)
|
|
|
|
{
|
|
|
|
if (code == SYS_RESTART)
|
KVM guest: guest side for eoi avoidance
The idea is simple: there's a bit, per APIC, in guest memory,
that tells the guest that it does not need EOI.
Guest tests it using a single est and clear operation - this is
necessary so that host can detect interrupt nesting - and if set, it can
skip the EOI MSR.
I run a simple microbenchmark to show exit reduction
(note: for testing, need to apply follow-up patch
'kvm: host side for eoi optimization' + a qemu patch
I posted separately, on host):
Before:
Performance counter stats for 'sleep 1s':
47,357 kvm:kvm_entry [99.98%]
0 kvm:kvm_hypercall [99.98%]
0 kvm:kvm_hv_hypercall [99.98%]
5,001 kvm:kvm_pio [99.98%]
0 kvm:kvm_cpuid [99.98%]
22,124 kvm:kvm_apic [99.98%]
49,849 kvm:kvm_exit [99.98%]
21,115 kvm:kvm_inj_virq [99.98%]
0 kvm:kvm_inj_exception [99.98%]
0 kvm:kvm_page_fault [99.98%]
22,937 kvm:kvm_msr [99.98%]
0 kvm:kvm_cr [99.98%]
0 kvm:kvm_pic_set_irq [99.98%]
0 kvm:kvm_apic_ipi [99.98%]
22,207 kvm:kvm_apic_accept_irq [99.98%]
22,421 kvm:kvm_eoi [99.98%]
0 kvm:kvm_pv_eoi [99.99%]
0 kvm:kvm_nested_vmrun [99.99%]
0 kvm:kvm_nested_intercepts [99.99%]
0 kvm:kvm_nested_vmexit [99.99%]
0 kvm:kvm_nested_vmexit_inject [99.99%]
0 kvm:kvm_nested_intr_vmexit [99.99%]
0 kvm:kvm_invlpga [99.99%]
0 kvm:kvm_skinit [99.99%]
57 kvm:kvm_emulate_insn [99.99%]
0 kvm:vcpu_match_mmio [99.99%]
0 kvm:kvm_userspace_exit [99.99%]
2 kvm:kvm_set_irq [99.99%]
2 kvm:kvm_ioapic_set_irq [99.99%]
23,609 kvm:kvm_msi_set_irq [99.99%]
1 kvm:kvm_ack_irq [99.99%]
131 kvm:kvm_mmio [99.99%]
226 kvm:kvm_fpu [100.00%]
0 kvm:kvm_age_page [100.00%]
0 kvm:kvm_try_async_get_page [100.00%]
0 kvm:kvm_async_pf_doublefault [100.00%]
0 kvm:kvm_async_pf_not_present [100.00%]
0 kvm:kvm_async_pf_ready [100.00%]
0 kvm:kvm_async_pf_completed
1.002100578 seconds time elapsed
After:
Performance counter stats for 'sleep 1s':
28,354 kvm:kvm_entry [99.98%]
0 kvm:kvm_hypercall [99.98%]
0 kvm:kvm_hv_hypercall [99.98%]
1,347 kvm:kvm_pio [99.98%]
0 kvm:kvm_cpuid [99.98%]
1,931 kvm:kvm_apic [99.98%]
29,595 kvm:kvm_exit [99.98%]
24,884 kvm:kvm_inj_virq [99.98%]
0 kvm:kvm_inj_exception [99.98%]
0 kvm:kvm_page_fault [99.98%]
1,986 kvm:kvm_msr [99.98%]
0 kvm:kvm_cr [99.98%]
0 kvm:kvm_pic_set_irq [99.98%]
0 kvm:kvm_apic_ipi [99.99%]
25,953 kvm:kvm_apic_accept_irq [99.99%]
26,132 kvm:kvm_eoi [99.99%]
26,593 kvm:kvm_pv_eoi [99.99%]
0 kvm:kvm_nested_vmrun [99.99%]
0 kvm:kvm_nested_intercepts [99.99%]
0 kvm:kvm_nested_vmexit [99.99%]
0 kvm:kvm_nested_vmexit_inject [99.99%]
0 kvm:kvm_nested_intr_vmexit [99.99%]
0 kvm:kvm_invlpga [99.99%]
0 kvm:kvm_skinit [99.99%]
284 kvm:kvm_emulate_insn [99.99%]
68 kvm:vcpu_match_mmio [99.99%]
68 kvm:kvm_userspace_exit [99.99%]
2 kvm:kvm_set_irq [99.99%]
2 kvm:kvm_ioapic_set_irq [99.99%]
28,288 kvm:kvm_msi_set_irq [99.99%]
1 kvm:kvm_ack_irq [99.99%]
131 kvm:kvm_mmio [100.00%]
588 kvm:kvm_fpu [100.00%]
0 kvm:kvm_age_page [100.00%]
0 kvm:kvm_try_async_get_page [100.00%]
0 kvm:kvm_async_pf_doublefault [100.00%]
0 kvm:kvm_async_pf_not_present [100.00%]
0 kvm:kvm_async_pf_ready [100.00%]
0 kvm:kvm_async_pf_completed
1.002039622 seconds time elapsed
We see that # of exits is almost halved.
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
Signed-off-by: Avi Kivity <avi@redhat.com>
2012-06-24 16:24:34 +00:00
|
|
|
on_each_cpu(kvm_pv_guest_cpu_reboot, NULL, 1);
|
2010-10-14 09:22:51 +00:00
|
|
|
return NOTIFY_DONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct notifier_block kvm_pv_reboot_nb = {
|
|
|
|
.notifier_call = kvm_pv_reboot_notify,
|
|
|
|
};
|
|
|
|
|
2011-07-11 19:28:19 +00:00
|
|
|
static u64 kvm_steal_clock(int cpu)
|
|
|
|
{
|
|
|
|
u64 steal;
|
|
|
|
struct kvm_steal_time *src;
|
|
|
|
int version;
|
|
|
|
|
|
|
|
src = &per_cpu(steal_time, cpu);
|
|
|
|
do {
|
|
|
|
version = src->version;
|
|
|
|
rmb();
|
|
|
|
steal = src->steal;
|
|
|
|
rmb();
|
|
|
|
} while ((version & 1) || (version != src->version));
|
|
|
|
|
|
|
|
return steal;
|
|
|
|
}
|
|
|
|
|
|
|
|
void kvm_disable_steal_time(void)
|
|
|
|
{
|
|
|
|
if (!has_steal_clock)
|
|
|
|
return;
|
|
|
|
|
|
|
|
wrmsr(MSR_KVM_STEAL_TIME, 0, 0);
|
|
|
|
}
|
|
|
|
|
2010-10-14 09:22:49 +00:00
|
|
|
#ifdef CONFIG_SMP
|
|
|
|
static void __init kvm_smp_prepare_boot_cpu(void)
|
|
|
|
{
|
2010-10-14 09:22:51 +00:00
|
|
|
kvm_guest_cpu_init();
|
2010-10-14 09:22:49 +00:00
|
|
|
native_smp_prepare_boot_cpu();
|
2013-08-06 09:25:41 +00:00
|
|
|
kvm_spinlock_init();
|
2010-10-14 09:22:49 +00:00
|
|
|
}
|
2010-10-14 09:22:51 +00:00
|
|
|
|
x86: delete __cpuinit usage from all x86 files
The __cpuinit type of throwaway sections might have made sense
some time ago when RAM was more constrained, but now the savings
do not offset the cost and complications. For example, the fix in
commit 5e427ec2d0 ("x86: Fix bit corruption at CPU resume time")
is a good example of the nasty type of bugs that can be created
with improper use of the various __init prefixes.
After a discussion on LKML[1] it was decided that cpuinit should go
the way of devinit and be phased out. Once all the users are gone,
we can then finally remove the macros themselves from linux/init.h.
Note that some harmless section mismatch warnings may result, since
notify_cpu_starting() and cpu_up() are arch independent (kernel/cpu.c)
are flagged as __cpuinit -- so if we remove the __cpuinit from
arch specific callers, we will also get section mismatch warnings.
As an intermediate step, we intend to turn the linux/init.h cpuinit
content into no-ops as early as possible, since that will get rid
of these warnings. In any case, they are temporary and harmless.
This removes all the arch/x86 uses of the __cpuinit macros from
all C files. x86 only had the one __CPUINIT used in assembly files,
and it wasn't paired off with a .previous or a __FINIT, so we can
delete it directly w/o any corresponding additional change there.
[1] https://lkml.org/lkml/2013/5/20/589
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: "H. Peter Anvin" <hpa@zytor.com>
Cc: x86@kernel.org
Acked-by: Ingo Molnar <mingo@kernel.org>
Acked-by: Thomas Gleixner <tglx@linutronix.de>
Acked-by: H. Peter Anvin <hpa@linux.intel.com>
Signed-off-by: Paul Gortmaker <paul.gortmaker@windriver.com>
2013-06-18 22:23:59 +00:00
|
|
|
static void kvm_guest_cpu_online(void *dummy)
|
2010-10-14 09:22:51 +00:00
|
|
|
{
|
|
|
|
kvm_guest_cpu_init();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void kvm_guest_cpu_offline(void *dummy)
|
|
|
|
{
|
2011-07-11 19:28:19 +00:00
|
|
|
kvm_disable_steal_time();
|
KVM guest: guest side for eoi avoidance
The idea is simple: there's a bit, per APIC, in guest memory,
that tells the guest that it does not need EOI.
Guest tests it using a single est and clear operation - this is
necessary so that host can detect interrupt nesting - and if set, it can
skip the EOI MSR.
I run a simple microbenchmark to show exit reduction
(note: for testing, need to apply follow-up patch
'kvm: host side for eoi optimization' + a qemu patch
I posted separately, on host):
Before:
Performance counter stats for 'sleep 1s':
47,357 kvm:kvm_entry [99.98%]
0 kvm:kvm_hypercall [99.98%]
0 kvm:kvm_hv_hypercall [99.98%]
5,001 kvm:kvm_pio [99.98%]
0 kvm:kvm_cpuid [99.98%]
22,124 kvm:kvm_apic [99.98%]
49,849 kvm:kvm_exit [99.98%]
21,115 kvm:kvm_inj_virq [99.98%]
0 kvm:kvm_inj_exception [99.98%]
0 kvm:kvm_page_fault [99.98%]
22,937 kvm:kvm_msr [99.98%]
0 kvm:kvm_cr [99.98%]
0 kvm:kvm_pic_set_irq [99.98%]
0 kvm:kvm_apic_ipi [99.98%]
22,207 kvm:kvm_apic_accept_irq [99.98%]
22,421 kvm:kvm_eoi [99.98%]
0 kvm:kvm_pv_eoi [99.99%]
0 kvm:kvm_nested_vmrun [99.99%]
0 kvm:kvm_nested_intercepts [99.99%]
0 kvm:kvm_nested_vmexit [99.99%]
0 kvm:kvm_nested_vmexit_inject [99.99%]
0 kvm:kvm_nested_intr_vmexit [99.99%]
0 kvm:kvm_invlpga [99.99%]
0 kvm:kvm_skinit [99.99%]
57 kvm:kvm_emulate_insn [99.99%]
0 kvm:vcpu_match_mmio [99.99%]
0 kvm:kvm_userspace_exit [99.99%]
2 kvm:kvm_set_irq [99.99%]
2 kvm:kvm_ioapic_set_irq [99.99%]
23,609 kvm:kvm_msi_set_irq [99.99%]
1 kvm:kvm_ack_irq [99.99%]
131 kvm:kvm_mmio [99.99%]
226 kvm:kvm_fpu [100.00%]
0 kvm:kvm_age_page [100.00%]
0 kvm:kvm_try_async_get_page [100.00%]
0 kvm:kvm_async_pf_doublefault [100.00%]
0 kvm:kvm_async_pf_not_present [100.00%]
0 kvm:kvm_async_pf_ready [100.00%]
0 kvm:kvm_async_pf_completed
1.002100578 seconds time elapsed
After:
Performance counter stats for 'sleep 1s':
28,354 kvm:kvm_entry [99.98%]
0 kvm:kvm_hypercall [99.98%]
0 kvm:kvm_hv_hypercall [99.98%]
1,347 kvm:kvm_pio [99.98%]
0 kvm:kvm_cpuid [99.98%]
1,931 kvm:kvm_apic [99.98%]
29,595 kvm:kvm_exit [99.98%]
24,884 kvm:kvm_inj_virq [99.98%]
0 kvm:kvm_inj_exception [99.98%]
0 kvm:kvm_page_fault [99.98%]
1,986 kvm:kvm_msr [99.98%]
0 kvm:kvm_cr [99.98%]
0 kvm:kvm_pic_set_irq [99.98%]
0 kvm:kvm_apic_ipi [99.99%]
25,953 kvm:kvm_apic_accept_irq [99.99%]
26,132 kvm:kvm_eoi [99.99%]
26,593 kvm:kvm_pv_eoi [99.99%]
0 kvm:kvm_nested_vmrun [99.99%]
0 kvm:kvm_nested_intercepts [99.99%]
0 kvm:kvm_nested_vmexit [99.99%]
0 kvm:kvm_nested_vmexit_inject [99.99%]
0 kvm:kvm_nested_intr_vmexit [99.99%]
0 kvm:kvm_invlpga [99.99%]
0 kvm:kvm_skinit [99.99%]
284 kvm:kvm_emulate_insn [99.99%]
68 kvm:vcpu_match_mmio [99.99%]
68 kvm:kvm_userspace_exit [99.99%]
2 kvm:kvm_set_irq [99.99%]
2 kvm:kvm_ioapic_set_irq [99.99%]
28,288 kvm:kvm_msi_set_irq [99.99%]
1 kvm:kvm_ack_irq [99.99%]
131 kvm:kvm_mmio [100.00%]
588 kvm:kvm_fpu [100.00%]
0 kvm:kvm_age_page [100.00%]
0 kvm:kvm_try_async_get_page [100.00%]
0 kvm:kvm_async_pf_doublefault [100.00%]
0 kvm:kvm_async_pf_not_present [100.00%]
0 kvm:kvm_async_pf_ready [100.00%]
0 kvm:kvm_async_pf_completed
1.002039622 seconds time elapsed
We see that # of exits is almost halved.
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
Signed-off-by: Avi Kivity <avi@redhat.com>
2012-06-24 16:24:34 +00:00
|
|
|
if (kvm_para_has_feature(KVM_FEATURE_PV_EOI))
|
|
|
|
wrmsrl(MSR_KVM_PV_EOI_EN, 0);
|
|
|
|
kvm_pv_disable_apf();
|
2010-10-14 09:22:52 +00:00
|
|
|
apf_task_wake_all();
|
2010-10-14 09:22:51 +00:00
|
|
|
}
|
|
|
|
|
x86: delete __cpuinit usage from all x86 files
The __cpuinit type of throwaway sections might have made sense
some time ago when RAM was more constrained, but now the savings
do not offset the cost and complications. For example, the fix in
commit 5e427ec2d0 ("x86: Fix bit corruption at CPU resume time")
is a good example of the nasty type of bugs that can be created
with improper use of the various __init prefixes.
After a discussion on LKML[1] it was decided that cpuinit should go
the way of devinit and be phased out. Once all the users are gone,
we can then finally remove the macros themselves from linux/init.h.
Note that some harmless section mismatch warnings may result, since
notify_cpu_starting() and cpu_up() are arch independent (kernel/cpu.c)
are flagged as __cpuinit -- so if we remove the __cpuinit from
arch specific callers, we will also get section mismatch warnings.
As an intermediate step, we intend to turn the linux/init.h cpuinit
content into no-ops as early as possible, since that will get rid
of these warnings. In any case, they are temporary and harmless.
This removes all the arch/x86 uses of the __cpuinit macros from
all C files. x86 only had the one __CPUINIT used in assembly files,
and it wasn't paired off with a .previous or a __FINIT, so we can
delete it directly w/o any corresponding additional change there.
[1] https://lkml.org/lkml/2013/5/20/589
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: "H. Peter Anvin" <hpa@zytor.com>
Cc: x86@kernel.org
Acked-by: Ingo Molnar <mingo@kernel.org>
Acked-by: Thomas Gleixner <tglx@linutronix.de>
Acked-by: H. Peter Anvin <hpa@linux.intel.com>
Signed-off-by: Paul Gortmaker <paul.gortmaker@windriver.com>
2013-06-18 22:23:59 +00:00
|
|
|
static int kvm_cpu_notify(struct notifier_block *self, unsigned long action,
|
|
|
|
void *hcpu)
|
2010-10-14 09:22:51 +00:00
|
|
|
{
|
|
|
|
int cpu = (unsigned long)hcpu;
|
|
|
|
switch (action) {
|
|
|
|
case CPU_ONLINE:
|
|
|
|
case CPU_DOWN_FAILED:
|
|
|
|
case CPU_ONLINE_FROZEN:
|
|
|
|
smp_call_function_single(cpu, kvm_guest_cpu_online, NULL, 0);
|
|
|
|
break;
|
|
|
|
case CPU_DOWN_PREPARE:
|
|
|
|
case CPU_DOWN_PREPARE_FROZEN:
|
|
|
|
smp_call_function_single(cpu, kvm_guest_cpu_offline, NULL, 1);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return NOTIFY_OK;
|
|
|
|
}
|
|
|
|
|
x86: delete __cpuinit usage from all x86 files
The __cpuinit type of throwaway sections might have made sense
some time ago when RAM was more constrained, but now the savings
do not offset the cost and complications. For example, the fix in
commit 5e427ec2d0 ("x86: Fix bit corruption at CPU resume time")
is a good example of the nasty type of bugs that can be created
with improper use of the various __init prefixes.
After a discussion on LKML[1] it was decided that cpuinit should go
the way of devinit and be phased out. Once all the users are gone,
we can then finally remove the macros themselves from linux/init.h.
Note that some harmless section mismatch warnings may result, since
notify_cpu_starting() and cpu_up() are arch independent (kernel/cpu.c)
are flagged as __cpuinit -- so if we remove the __cpuinit from
arch specific callers, we will also get section mismatch warnings.
As an intermediate step, we intend to turn the linux/init.h cpuinit
content into no-ops as early as possible, since that will get rid
of these warnings. In any case, they are temporary and harmless.
This removes all the arch/x86 uses of the __cpuinit macros from
all C files. x86 only had the one __CPUINIT used in assembly files,
and it wasn't paired off with a .previous or a __FINIT, so we can
delete it directly w/o any corresponding additional change there.
[1] https://lkml.org/lkml/2013/5/20/589
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: "H. Peter Anvin" <hpa@zytor.com>
Cc: x86@kernel.org
Acked-by: Ingo Molnar <mingo@kernel.org>
Acked-by: Thomas Gleixner <tglx@linutronix.de>
Acked-by: H. Peter Anvin <hpa@linux.intel.com>
Signed-off-by: Paul Gortmaker <paul.gortmaker@windriver.com>
2013-06-18 22:23:59 +00:00
|
|
|
static struct notifier_block kvm_cpu_notifier = {
|
2010-10-14 09:22:51 +00:00
|
|
|
.notifier_call = kvm_cpu_notify,
|
|
|
|
};
|
2010-10-14 09:22:49 +00:00
|
|
|
#endif
|
|
|
|
|
2010-10-14 09:22:52 +00:00
|
|
|
static void __init kvm_apf_trap_init(void)
|
|
|
|
{
|
2013-10-30 20:37:00 +00:00
|
|
|
set_intr_gate(14, async_page_fault);
|
2010-10-14 09:22:52 +00:00
|
|
|
}
|
|
|
|
|
2008-02-22 17:21:36 +00:00
|
|
|
void __init kvm_guest_init(void)
|
|
|
|
{
|
2010-10-14 09:22:52 +00:00
|
|
|
int i;
|
|
|
|
|
2008-02-22 17:21:36 +00:00
|
|
|
if (!kvm_para_available())
|
|
|
|
return;
|
|
|
|
|
|
|
|
paravirt_ops_setup();
|
2010-10-14 09:22:51 +00:00
|
|
|
register_reboot_notifier(&kvm_pv_reboot_nb);
|
2010-10-14 09:22:52 +00:00
|
|
|
for (i = 0; i < KVM_TASK_SLEEP_HASHSIZE; i++)
|
|
|
|
spin_lock_init(&async_pf_sleepers[i].lock);
|
|
|
|
if (kvm_para_has_feature(KVM_FEATURE_ASYNC_PF))
|
|
|
|
x86_init.irqs.trap_init = kvm_apf_trap_init;
|
|
|
|
|
2011-07-11 19:28:19 +00:00
|
|
|
if (kvm_para_has_feature(KVM_FEATURE_STEAL_TIME)) {
|
|
|
|
has_steal_clock = 1;
|
|
|
|
pv_time_ops.steal_clock = kvm_steal_clock;
|
|
|
|
}
|
|
|
|
|
2012-07-15 12:56:52 +00:00
|
|
|
if (kvm_para_has_feature(KVM_FEATURE_PV_EOI))
|
|
|
|
apic_set_eoi_write(kvm_guest_apic_eoi_write);
|
KVM guest: guest side for eoi avoidance
The idea is simple: there's a bit, per APIC, in guest memory,
that tells the guest that it does not need EOI.
Guest tests it using a single est and clear operation - this is
necessary so that host can detect interrupt nesting - and if set, it can
skip the EOI MSR.
I run a simple microbenchmark to show exit reduction
(note: for testing, need to apply follow-up patch
'kvm: host side for eoi optimization' + a qemu patch
I posted separately, on host):
Before:
Performance counter stats for 'sleep 1s':
47,357 kvm:kvm_entry [99.98%]
0 kvm:kvm_hypercall [99.98%]
0 kvm:kvm_hv_hypercall [99.98%]
5,001 kvm:kvm_pio [99.98%]
0 kvm:kvm_cpuid [99.98%]
22,124 kvm:kvm_apic [99.98%]
49,849 kvm:kvm_exit [99.98%]
21,115 kvm:kvm_inj_virq [99.98%]
0 kvm:kvm_inj_exception [99.98%]
0 kvm:kvm_page_fault [99.98%]
22,937 kvm:kvm_msr [99.98%]
0 kvm:kvm_cr [99.98%]
0 kvm:kvm_pic_set_irq [99.98%]
0 kvm:kvm_apic_ipi [99.98%]
22,207 kvm:kvm_apic_accept_irq [99.98%]
22,421 kvm:kvm_eoi [99.98%]
0 kvm:kvm_pv_eoi [99.99%]
0 kvm:kvm_nested_vmrun [99.99%]
0 kvm:kvm_nested_intercepts [99.99%]
0 kvm:kvm_nested_vmexit [99.99%]
0 kvm:kvm_nested_vmexit_inject [99.99%]
0 kvm:kvm_nested_intr_vmexit [99.99%]
0 kvm:kvm_invlpga [99.99%]
0 kvm:kvm_skinit [99.99%]
57 kvm:kvm_emulate_insn [99.99%]
0 kvm:vcpu_match_mmio [99.99%]
0 kvm:kvm_userspace_exit [99.99%]
2 kvm:kvm_set_irq [99.99%]
2 kvm:kvm_ioapic_set_irq [99.99%]
23,609 kvm:kvm_msi_set_irq [99.99%]
1 kvm:kvm_ack_irq [99.99%]
131 kvm:kvm_mmio [99.99%]
226 kvm:kvm_fpu [100.00%]
0 kvm:kvm_age_page [100.00%]
0 kvm:kvm_try_async_get_page [100.00%]
0 kvm:kvm_async_pf_doublefault [100.00%]
0 kvm:kvm_async_pf_not_present [100.00%]
0 kvm:kvm_async_pf_ready [100.00%]
0 kvm:kvm_async_pf_completed
1.002100578 seconds time elapsed
After:
Performance counter stats for 'sleep 1s':
28,354 kvm:kvm_entry [99.98%]
0 kvm:kvm_hypercall [99.98%]
0 kvm:kvm_hv_hypercall [99.98%]
1,347 kvm:kvm_pio [99.98%]
0 kvm:kvm_cpuid [99.98%]
1,931 kvm:kvm_apic [99.98%]
29,595 kvm:kvm_exit [99.98%]
24,884 kvm:kvm_inj_virq [99.98%]
0 kvm:kvm_inj_exception [99.98%]
0 kvm:kvm_page_fault [99.98%]
1,986 kvm:kvm_msr [99.98%]
0 kvm:kvm_cr [99.98%]
0 kvm:kvm_pic_set_irq [99.98%]
0 kvm:kvm_apic_ipi [99.99%]
25,953 kvm:kvm_apic_accept_irq [99.99%]
26,132 kvm:kvm_eoi [99.99%]
26,593 kvm:kvm_pv_eoi [99.99%]
0 kvm:kvm_nested_vmrun [99.99%]
0 kvm:kvm_nested_intercepts [99.99%]
0 kvm:kvm_nested_vmexit [99.99%]
0 kvm:kvm_nested_vmexit_inject [99.99%]
0 kvm:kvm_nested_intr_vmexit [99.99%]
0 kvm:kvm_invlpga [99.99%]
0 kvm:kvm_skinit [99.99%]
284 kvm:kvm_emulate_insn [99.99%]
68 kvm:vcpu_match_mmio [99.99%]
68 kvm:kvm_userspace_exit [99.99%]
2 kvm:kvm_set_irq [99.99%]
2 kvm:kvm_ioapic_set_irq [99.99%]
28,288 kvm:kvm_msi_set_irq [99.99%]
1 kvm:kvm_ack_irq [99.99%]
131 kvm:kvm_mmio [100.00%]
588 kvm:kvm_fpu [100.00%]
0 kvm:kvm_age_page [100.00%]
0 kvm:kvm_try_async_get_page [100.00%]
0 kvm:kvm_async_pf_doublefault [100.00%]
0 kvm:kvm_async_pf_not_present [100.00%]
0 kvm:kvm_async_pf_ready [100.00%]
0 kvm:kvm_async_pf_completed
1.002039622 seconds time elapsed
We see that # of exits is almost halved.
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
Signed-off-by: Avi Kivity <avi@redhat.com>
2012-06-24 16:24:34 +00:00
|
|
|
|
2012-11-28 01:28:56 +00:00
|
|
|
if (kvmclock_vsyscall)
|
|
|
|
kvm_setup_vsyscall_timeinfo();
|
|
|
|
|
2010-10-14 09:22:49 +00:00
|
|
|
#ifdef CONFIG_SMP
|
|
|
|
smp_ops.smp_prepare_boot_cpu = kvm_smp_prepare_boot_cpu;
|
2010-10-14 09:22:51 +00:00
|
|
|
register_cpu_notifier(&kvm_cpu_notifier);
|
|
|
|
#else
|
|
|
|
kvm_guest_cpu_init();
|
2010-10-14 09:22:49 +00:00
|
|
|
#endif
|
2008-02-22 17:21:36 +00:00
|
|
|
}
|
2011-07-11 19:28:19 +00:00
|
|
|
|
2014-01-27 13:49:40 +00:00
|
|
|
static noinline uint32_t __kvm_cpuid_base(void)
|
|
|
|
{
|
|
|
|
if (boot_cpu_data.cpuid_level < 0)
|
|
|
|
return 0; /* So we don't blow up on old processors */
|
|
|
|
|
|
|
|
if (cpu_has_hypervisor)
|
|
|
|
return hypervisor_cpuid_base("KVMKVMKVM\0\0\0", 0);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline uint32_t kvm_cpuid_base(void)
|
|
|
|
{
|
|
|
|
static int kvm_cpuid_base = -1;
|
|
|
|
|
|
|
|
if (kvm_cpuid_base == -1)
|
|
|
|
kvm_cpuid_base = __kvm_cpuid_base();
|
|
|
|
|
|
|
|
return kvm_cpuid_base;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool kvm_para_available(void)
|
|
|
|
{
|
|
|
|
return kvm_cpuid_base() != 0;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(kvm_para_available);
|
|
|
|
|
2014-01-27 13:51:44 +00:00
|
|
|
unsigned int kvm_arch_para_features(void)
|
|
|
|
{
|
|
|
|
return cpuid_eax(kvm_cpuid_base() | KVM_CPUID_FEATURES);
|
|
|
|
}
|
|
|
|
|
2013-07-25 08:54:35 +00:00
|
|
|
static uint32_t __init kvm_detect(void)
|
2012-07-06 17:47:39 +00:00
|
|
|
{
|
2013-07-25 08:54:35 +00:00
|
|
|
return kvm_cpuid_base();
|
2012-07-06 17:47:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const struct hypervisor_x86 x86_hyper_kvm __refconst = {
|
|
|
|
.name = "KVM",
|
|
|
|
.detect = kvm_detect,
|
2013-01-17 23:44:42 +00:00
|
|
|
.x2apic_available = kvm_para_available,
|
2012-07-06 17:47:39 +00:00
|
|
|
};
|
|
|
|
EXPORT_SYMBOL_GPL(x86_hyper_kvm);
|
|
|
|
|
2011-07-11 19:28:19 +00:00
|
|
|
static __init int activate_jump_labels(void)
|
|
|
|
{
|
|
|
|
if (has_steal_clock) {
|
2012-02-24 07:31:31 +00:00
|
|
|
static_key_slow_inc(¶virt_steal_enabled);
|
2011-07-11 19:28:19 +00:00
|
|
|
if (steal_acc)
|
2012-02-24 07:31:31 +00:00
|
|
|
static_key_slow_inc(¶virt_steal_rq_enabled);
|
2011-07-11 19:28:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
arch_initcall(activate_jump_labels);
|
2013-08-06 09:25:41 +00:00
|
|
|
|
|
|
|
#ifdef CONFIG_PARAVIRT_SPINLOCKS
|
|
|
|
|
|
|
|
/* Kick a cpu by its apicid. Used to wake up a halted vcpu */
|
2013-08-16 09:38:41 +00:00
|
|
|
static void kvm_kick_cpu(int cpu)
|
2013-08-06 09:25:41 +00:00
|
|
|
{
|
|
|
|
int apicid;
|
|
|
|
unsigned long flags = 0;
|
|
|
|
|
|
|
|
apicid = per_cpu(x86_cpu_to_apicid, cpu);
|
|
|
|
kvm_hypercall2(KVM_HC_KICK_CPU, flags, apicid);
|
|
|
|
}
|
|
|
|
|
|
|
|
enum kvm_contention_stat {
|
|
|
|
TAKEN_SLOW,
|
|
|
|
TAKEN_SLOW_PICKUP,
|
|
|
|
RELEASED_SLOW,
|
|
|
|
RELEASED_SLOW_KICKED,
|
|
|
|
NR_CONTENTION_STATS
|
|
|
|
};
|
|
|
|
|
|
|
|
#ifdef CONFIG_KVM_DEBUG_FS
|
|
|
|
#define HISTO_BUCKETS 30
|
|
|
|
|
|
|
|
static struct kvm_spinlock_stats
|
|
|
|
{
|
|
|
|
u32 contention_stats[NR_CONTENTION_STATS];
|
|
|
|
u32 histo_spin_blocked[HISTO_BUCKETS+1];
|
|
|
|
u64 time_blocked;
|
|
|
|
} spinlock_stats;
|
|
|
|
|
|
|
|
static u8 zero_stats;
|
|
|
|
|
|
|
|
static inline void check_zero(void)
|
|
|
|
{
|
|
|
|
u8 ret;
|
|
|
|
u8 old;
|
|
|
|
|
|
|
|
old = ACCESS_ONCE(zero_stats);
|
|
|
|
if (unlikely(old)) {
|
|
|
|
ret = cmpxchg(&zero_stats, old, 0);
|
|
|
|
/* This ensures only one fellow resets the stat */
|
|
|
|
if (ret == old)
|
|
|
|
memset(&spinlock_stats, 0, sizeof(spinlock_stats));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void add_stats(enum kvm_contention_stat var, u32 val)
|
|
|
|
{
|
|
|
|
check_zero();
|
|
|
|
spinlock_stats.contention_stats[var] += val;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static inline u64 spin_time_start(void)
|
|
|
|
{
|
|
|
|
return sched_clock();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void __spin_time_accum(u64 delta, u32 *array)
|
|
|
|
{
|
|
|
|
unsigned index;
|
|
|
|
|
|
|
|
index = ilog2(delta);
|
|
|
|
check_zero();
|
|
|
|
|
|
|
|
if (index < HISTO_BUCKETS)
|
|
|
|
array[index]++;
|
|
|
|
else
|
|
|
|
array[HISTO_BUCKETS]++;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void spin_time_accum_blocked(u64 start)
|
|
|
|
{
|
|
|
|
u32 delta;
|
|
|
|
|
|
|
|
delta = sched_clock() - start;
|
|
|
|
__spin_time_accum(delta, spinlock_stats.histo_spin_blocked);
|
|
|
|
spinlock_stats.time_blocked += delta;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct dentry *d_spin_debug;
|
|
|
|
static struct dentry *d_kvm_debug;
|
|
|
|
|
|
|
|
struct dentry *kvm_init_debugfs(void)
|
|
|
|
{
|
2013-10-29 15:13:54 +00:00
|
|
|
d_kvm_debug = debugfs_create_dir("kvm-guest", NULL);
|
2013-08-06 09:25:41 +00:00
|
|
|
if (!d_kvm_debug)
|
|
|
|
printk(KERN_WARNING "Could not create 'kvm' debugfs directory\n");
|
|
|
|
|
|
|
|
return d_kvm_debug;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int __init kvm_spinlock_debugfs(void)
|
|
|
|
{
|
|
|
|
struct dentry *d_kvm;
|
|
|
|
|
|
|
|
d_kvm = kvm_init_debugfs();
|
|
|
|
if (d_kvm == NULL)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
d_spin_debug = debugfs_create_dir("spinlocks", d_kvm);
|
|
|
|
|
|
|
|
debugfs_create_u8("zero_stats", 0644, d_spin_debug, &zero_stats);
|
|
|
|
|
|
|
|
debugfs_create_u32("taken_slow", 0444, d_spin_debug,
|
|
|
|
&spinlock_stats.contention_stats[TAKEN_SLOW]);
|
|
|
|
debugfs_create_u32("taken_slow_pickup", 0444, d_spin_debug,
|
|
|
|
&spinlock_stats.contention_stats[TAKEN_SLOW_PICKUP]);
|
|
|
|
|
|
|
|
debugfs_create_u32("released_slow", 0444, d_spin_debug,
|
|
|
|
&spinlock_stats.contention_stats[RELEASED_SLOW]);
|
|
|
|
debugfs_create_u32("released_slow_kicked", 0444, d_spin_debug,
|
|
|
|
&spinlock_stats.contention_stats[RELEASED_SLOW_KICKED]);
|
|
|
|
|
|
|
|
debugfs_create_u64("time_blocked", 0444, d_spin_debug,
|
|
|
|
&spinlock_stats.time_blocked);
|
|
|
|
|
|
|
|
debugfs_create_u32_array("histo_blocked", 0444, d_spin_debug,
|
|
|
|
spinlock_stats.histo_spin_blocked, HISTO_BUCKETS + 1);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
fs_initcall(kvm_spinlock_debugfs);
|
|
|
|
#else /* !CONFIG_KVM_DEBUG_FS */
|
|
|
|
static inline void add_stats(enum kvm_contention_stat var, u32 val)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline u64 spin_time_start(void)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void spin_time_accum_blocked(u64 start)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_KVM_DEBUG_FS */
|
|
|
|
|
|
|
|
struct kvm_lock_waiting {
|
|
|
|
struct arch_spinlock *lock;
|
|
|
|
__ticket_t want;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* cpus 'waiting' on a spinlock to become available */
|
|
|
|
static cpumask_t waiting_cpus;
|
|
|
|
|
|
|
|
/* Track spinlock on which a cpu is waiting */
|
|
|
|
static DEFINE_PER_CPU(struct kvm_lock_waiting, klock_waiting);
|
|
|
|
|
2013-10-22 16:07:58 +00:00
|
|
|
__visible void kvm_lock_spinning(struct arch_spinlock *lock, __ticket_t want)
|
2013-08-06 09:25:41 +00:00
|
|
|
{
|
|
|
|
struct kvm_lock_waiting *w;
|
|
|
|
int cpu;
|
|
|
|
u64 start;
|
|
|
|
unsigned long flags;
|
|
|
|
|
|
|
|
if (in_nmi())
|
|
|
|
return;
|
|
|
|
|
|
|
|
w = &__get_cpu_var(klock_waiting);
|
|
|
|
cpu = smp_processor_id();
|
|
|
|
start = spin_time_start();
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Make sure an interrupt handler can't upset things in a
|
|
|
|
* partially setup state.
|
|
|
|
*/
|
|
|
|
local_irq_save(flags);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The ordering protocol on this is that the "lock" pointer
|
|
|
|
* may only be set non-NULL if the "want" ticket is correct.
|
|
|
|
* If we're updating "want", we must first clear "lock".
|
|
|
|
*/
|
|
|
|
w->lock = NULL;
|
|
|
|
smp_wmb();
|
|
|
|
w->want = want;
|
|
|
|
smp_wmb();
|
|
|
|
w->lock = lock;
|
|
|
|
|
|
|
|
add_stats(TAKEN_SLOW, 1);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This uses set_bit, which is atomic but we should not rely on its
|
|
|
|
* reordering gurantees. So barrier is needed after this call.
|
|
|
|
*/
|
|
|
|
cpumask_set_cpu(cpu, &waiting_cpus);
|
|
|
|
|
|
|
|
barrier();
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Mark entry to slowpath before doing the pickup test to make
|
|
|
|
* sure we don't deadlock with an unlocker.
|
|
|
|
*/
|
|
|
|
__ticket_enter_slowpath(lock);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* check again make sure it didn't become free while
|
|
|
|
* we weren't looking.
|
|
|
|
*/
|
|
|
|
if (ACCESS_ONCE(lock->tickets.head) == want) {
|
|
|
|
add_stats(TAKEN_SLOW_PICKUP, 1);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* halt until it's our turn and kicked. Note that we do safe halt
|
|
|
|
* for irq enabled case to avoid hang when lock info is overwritten
|
|
|
|
* in irq spinlock slowpath and no spurious interrupt occur to save us.
|
|
|
|
*/
|
|
|
|
if (arch_irqs_disabled_flags(flags))
|
|
|
|
halt();
|
|
|
|
else
|
|
|
|
safe_halt();
|
|
|
|
|
|
|
|
out:
|
|
|
|
cpumask_clear_cpu(cpu, &waiting_cpus);
|
|
|
|
w->lock = NULL;
|
|
|
|
local_irq_restore(flags);
|
|
|
|
spin_time_accum_blocked(start);
|
|
|
|
}
|
|
|
|
PV_CALLEE_SAVE_REGS_THUNK(kvm_lock_spinning);
|
|
|
|
|
|
|
|
/* Kick vcpu waiting on @lock->head to reach value @ticket */
|
|
|
|
static void kvm_unlock_kick(struct arch_spinlock *lock, __ticket_t ticket)
|
|
|
|
{
|
|
|
|
int cpu;
|
|
|
|
|
|
|
|
add_stats(RELEASED_SLOW, 1);
|
|
|
|
for_each_cpu(cpu, &waiting_cpus) {
|
|
|
|
const struct kvm_lock_waiting *w = &per_cpu(klock_waiting, cpu);
|
|
|
|
if (ACCESS_ONCE(w->lock) == lock &&
|
|
|
|
ACCESS_ONCE(w->want) == ticket) {
|
|
|
|
add_stats(RELEASED_SLOW_KICKED, 1);
|
|
|
|
kvm_kick_cpu(cpu);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Setup pv_lock_ops to exploit KVM_FEATURE_PV_UNHALT if present.
|
|
|
|
*/
|
|
|
|
void __init kvm_spinlock_init(void)
|
|
|
|
{
|
|
|
|
if (!kvm_para_available())
|
|
|
|
return;
|
|
|
|
/* Does host kernel support KVM_FEATURE_PV_UNHALT? */
|
|
|
|
if (!kvm_para_has_feature(KVM_FEATURE_PV_UNHALT))
|
|
|
|
return;
|
|
|
|
|
2013-10-09 09:03:21 +00:00
|
|
|
pv_lock_ops.lock_spinning = PV_CALLEE_SAVE(kvm_lock_spinning);
|
|
|
|
pv_lock_ops.unlock_kick = kvm_unlock_kick;
|
|
|
|
}
|
|
|
|
|
|
|
|
static __init int kvm_spinlock_init_jump(void)
|
|
|
|
{
|
|
|
|
if (!kvm_para_available())
|
|
|
|
return 0;
|
|
|
|
if (!kvm_para_has_feature(KVM_FEATURE_PV_UNHALT))
|
|
|
|
return 0;
|
2013-08-06 09:25:41 +00:00
|
|
|
|
|
|
|
static_key_slow_inc(¶virt_ticketlocks_enabled);
|
2013-10-09 09:03:21 +00:00
|
|
|
printk(KERN_INFO "KVM setup paravirtual spinlock\n");
|
2013-08-06 09:25:41 +00:00
|
|
|
|
2013-10-09 09:03:21 +00:00
|
|
|
return 0;
|
2013-08-06 09:25:41 +00:00
|
|
|
}
|
2013-10-09 09:03:21 +00:00
|
|
|
early_initcall(kvm_spinlock_init_jump);
|
|
|
|
|
2013-08-06 09:25:41 +00:00
|
|
|
#endif /* CONFIG_PARAVIRT_SPINLOCKS */
|