2019-05-19 21:51:49 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2008-02-23 01:21:36 +08:00
|
|
|
/*
|
|
|
|
* KVM paravirt_ops implementation
|
|
|
|
*
|
|
|
|
* Copyright (C) 2007, Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
|
|
|
|
* Copyright IBM Corporation, 2007
|
|
|
|
* Authors: Anthony Liguori <aliguori@us.ibm.com>
|
|
|
|
*/
|
|
|
|
|
2019-10-23 19:16:21 +08:00
|
|
|
#define pr_fmt(fmt) "kvm-guest: " fmt
|
|
|
|
|
2013-02-24 07:23:25 +08:00
|
|
|
#include <linux/context_tracking.h>
|
2016-07-14 08:18:56 +08:00
|
|
|
#include <linux/init.h>
|
2020-06-15 19:53:05 +08:00
|
|
|
#include <linux/irq.h>
|
2008-02-23 01:21:36 +08:00
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/kvm_para.h>
|
|
|
|
#include <linux/cpu.h>
|
|
|
|
#include <linux/mm.h>
|
2008-02-23 01:21:37 +08:00
|
|
|
#include <linux/highmem.h>
|
2008-02-23 01:21:38 +08:00
|
|
|
#include <linux/hardirq.h>
|
2010-10-14 17:22:51 +08:00
|
|
|
#include <linux/notifier.h>
|
|
|
|
#include <linux/reboot.h>
|
2010-10-14 17:22:52 +08:00
|
|
|
#include <linux/hash.h>
|
|
|
|
#include <linux/sched.h>
|
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/kprobes.h>
|
2014-10-14 06:55:37 +08:00
|
|
|
#include <linux/nmi.h>
|
2016-03-21 22:13:27 +08:00
|
|
|
#include <linux/swait.h>
|
2009-02-12 08:45:42 +08:00
|
|
|
#include <asm/timer.h>
|
2010-10-14 17:22:51 +08:00
|
|
|
#include <asm/cpu.h>
|
2010-10-14 17:22:52 +08:00
|
|
|
#include <asm/traps.h>
|
|
|
|
#include <asm/desc.h>
|
2010-10-14 17:22:54 +08:00
|
|
|
#include <asm/tlbflush.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-25 00:24:34 +08:00
|
|
|
#include <asm/apic.h>
|
|
|
|
#include <asm/apicdef.h>
|
2012-07-07 01:47:39 +08:00
|
|
|
#include <asm/hypervisor.h>
|
2018-08-22 23:30:16 +08:00
|
|
|
#include <asm/tlb.h>
|
2019-10-10 14:37:25 +08:00
|
|
|
#include <asm/cpuidle_haltpoll.h>
|
2008-02-23 01:21:38 +08:00
|
|
|
|
2020-02-29 02:42:48 +08:00
|
|
|
DEFINE_STATIC_KEY_FALSE(kvm_async_pf_enabled);
|
|
|
|
|
2010-10-14 17:22:51 +08:00
|
|
|
static int kvmapf = 1;
|
|
|
|
|
2018-01-17 11:46:54 +08:00
|
|
|
static int __init parse_no_kvmapf(char *arg)
|
2010-10-14 17:22:51 +08:00
|
|
|
{
|
|
|
|
kvmapf = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
early_param("no-kvmapf", parse_no_kvmapf);
|
|
|
|
|
2011-07-12 03:28:19 +08:00
|
|
|
static int steal_acc = 1;
|
2018-01-17 11:46:54 +08:00
|
|
|
static int __init parse_no_stealacc(char *arg)
|
2011-07-12 03:28:19 +08:00
|
|
|
{
|
|
|
|
steal_acc = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
early_param("no-steal-acc", parse_no_stealacc);
|
|
|
|
|
2017-10-20 22:30:58 +08:00
|
|
|
static DEFINE_PER_CPU_DECRYPTED(struct kvm_vcpu_pv_apf_data, apf_reason) __aligned(64);
|
2019-03-30 08:47:42 +08:00
|
|
|
DEFINE_PER_CPU_DECRYPTED(struct kvm_steal_time, steal_time) __aligned(64) __visible;
|
2011-07-12 03:28:19 +08:00
|
|
|
static int has_steal_clock = 0;
|
2008-02-23 01:21:38 +08:00
|
|
|
|
2008-02-23 01:21:36 +08:00
|
|
|
/*
|
|
|
|
* No need for any "IO delay" on KVM
|
|
|
|
*/
|
|
|
|
static void kvm_io_delay(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2010-10-14 17:22:52 +08: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;
|
2016-03-21 22:13:27 +08:00
|
|
|
struct swait_queue_head wq;
|
2010-10-14 17:22:52 +08:00
|
|
|
u32 token;
|
|
|
|
int cpu;
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct kvm_task_sleep_head {
|
2016-03-21 22:13:27 +08:00
|
|
|
raw_spinlock_t lock;
|
2010-10-14 17:22:52 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-04-24 15:57:56 +08:00
|
|
|
static bool kvm_async_pf_queue_task(u32 token, struct kvm_task_sleep_node *n)
|
2010-10-14 17:22:52 +08:00
|
|
|
{
|
|
|
|
u32 key = hash_32(token, KVM_TASK_SLEEP_HASHBITS);
|
|
|
|
struct kvm_task_sleep_head *b = &async_pf_sleepers[key];
|
2020-03-07 07:42:06 +08:00
|
|
|
struct kvm_task_sleep_node *e;
|
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 10:35:13 +08:00
|
|
|
|
2016-03-21 22:13:27 +08:00
|
|
|
raw_spin_lock(&b->lock);
|
2010-10-14 17:22:52 +08:00
|
|
|
e = _find_apf_task(b, token);
|
|
|
|
if (e) {
|
|
|
|
/* dummy entry exist -> wake up was delivered ahead of PF */
|
|
|
|
hlist_del(&e->link);
|
2016-03-21 22:13:27 +08:00
|
|
|
raw_spin_unlock(&b->lock);
|
2020-03-07 07:42:06 +08:00
|
|
|
kfree(e);
|
|
|
|
return false;
|
|
|
|
}
|
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 10:35:13 +08:00
|
|
|
|
2020-03-07 07:42:06 +08:00
|
|
|
n->token = token;
|
|
|
|
n->cpu = smp_processor_id();
|
|
|
|
init_swait_queue_head(&n->wq);
|
|
|
|
hlist_add_head(&n->link, &b->list);
|
|
|
|
raw_spin_unlock(&b->lock);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* kvm_async_pf_task_wait_schedule - Wait for pagefault to be handled
|
|
|
|
* @token: Token to identify the sleep node entry
|
|
|
|
*
|
|
|
|
* Invoked from the async pagefault handling code or from the VM exit page
|
|
|
|
* fault handler. In both cases RCU is watching.
|
|
|
|
*/
|
|
|
|
void kvm_async_pf_task_wait_schedule(u32 token)
|
|
|
|
{
|
|
|
|
struct kvm_task_sleep_node n;
|
|
|
|
DECLARE_SWAITQUEUE(wait);
|
|
|
|
|
|
|
|
lockdep_assert_irqs_disabled();
|
|
|
|
|
2020-04-24 15:57:56 +08:00
|
|
|
if (!kvm_async_pf_queue_task(token, &n))
|
2010-10-14 17:22:52 +08:00
|
|
|
return;
|
2020-03-07 07:42:06 +08:00
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
prepare_to_swait_exclusive(&n.wq, &wait, TASK_UNINTERRUPTIBLE);
|
|
|
|
if (hlist_unhashed(&n.link))
|
|
|
|
break;
|
|
|
|
|
|
|
|
local_irq_enable();
|
|
|
|
schedule();
|
|
|
|
local_irq_disable();
|
2010-10-14 17:22:52 +08:00
|
|
|
}
|
2020-03-07 07:42:06 +08:00
|
|
|
finish_swait(&n.wq, &wait);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(kvm_async_pf_task_wait_schedule);
|
2010-10-14 17:22:52 +08:00
|
|
|
|
|
|
|
static void apf_task_wake_one(struct kvm_task_sleep_node *n)
|
|
|
|
{
|
|
|
|
hlist_del_init(&n->link);
|
2020-04-24 15:57:56 +08:00
|
|
|
if (swq_has_sleeper(&n->wq))
|
2018-06-12 16:34:52 +08:00
|
|
|
swake_up_one(&n->wq);
|
2010-10-14 17:22:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void apf_task_wake_all(void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < KVM_TASK_SLEEP_HASHSIZE; i++) {
|
|
|
|
struct kvm_task_sleep_head *b = &async_pf_sleepers[i];
|
2020-03-07 07:42:06 +08:00
|
|
|
struct kvm_task_sleep_node *n;
|
|
|
|
struct hlist_node *p, *next;
|
|
|
|
|
2016-03-21 22:13:27 +08:00
|
|
|
raw_spin_lock(&b->lock);
|
2010-10-14 17:22:52 +08:00
|
|
|
hlist_for_each_safe(p, next, &b->list) {
|
2020-03-07 07:42:06 +08:00
|
|
|
n = hlist_entry(p, typeof(*n), link);
|
2010-10-14 17:22:52 +08:00
|
|
|
if (n->cpu == smp_processor_id())
|
|
|
|
apf_task_wake_one(n);
|
|
|
|
}
|
2016-03-21 22:13:27 +08:00
|
|
|
raw_spin_unlock(&b->lock);
|
2010-10-14 17:22:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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:
|
2016-03-21 22:13:27 +08:00
|
|
|
raw_spin_lock(&b->lock);
|
2010-10-14 17:22:52 +08:00
|
|
|
n = _find_apf_task(b, token);
|
|
|
|
if (!n) {
|
|
|
|
/*
|
|
|
|
* async PF was not yet handled.
|
|
|
|
* Add dummy entry for the token.
|
|
|
|
*/
|
2012-05-02 20:04:02 +08:00
|
|
|
n = kzalloc(sizeof(*n), GFP_ATOMIC);
|
2010-10-14 17:22:52 +08:00
|
|
|
if (!n) {
|
|
|
|
/*
|
|
|
|
* Allocation failed! Busy wait while other cpu
|
|
|
|
* handles async PF.
|
|
|
|
*/
|
2016-03-21 22:13:27 +08:00
|
|
|
raw_spin_unlock(&b->lock);
|
2010-10-14 17:22:52 +08:00
|
|
|
cpu_relax();
|
|
|
|
goto again;
|
|
|
|
}
|
|
|
|
n->token = token;
|
|
|
|
n->cpu = smp_processor_id();
|
2016-03-21 22:13:27 +08:00
|
|
|
init_swait_queue_head(&n->wq);
|
2010-10-14 17:22:52 +08:00
|
|
|
hlist_add_head(&n->link, &b->list);
|
2020-03-07 07:42:06 +08:00
|
|
|
} else {
|
2010-10-14 17:22:52 +08:00
|
|
|
apf_task_wake_one(n);
|
2020-03-07 07:42:06 +08:00
|
|
|
}
|
2016-03-21 22:13:27 +08:00
|
|
|
raw_spin_unlock(&b->lock);
|
2010-10-14 17:22:52 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(kvm_async_pf_task_wake);
|
|
|
|
|
2020-05-22 04:05:28 +08:00
|
|
|
noinstr u32 kvm_read_and_reset_apf_flags(void)
|
2010-10-14 17:22:52 +08:00
|
|
|
{
|
2020-05-25 22:41:17 +08:00
|
|
|
u32 flags = 0;
|
2010-10-14 17:22:52 +08:00
|
|
|
|
x86: Replace __get_cpu_var uses
__get_cpu_var() is used for multiple purposes in the kernel source. One of
them is address calculation via the form &__get_cpu_var(x). This calculates
the address for the instance of the percpu variable of the current processor
based on an offset.
Other use cases are for storing and retrieving data from the current
processors percpu area. __get_cpu_var() can be used as an lvalue when
writing data or on the right side of an assignment.
__get_cpu_var() is defined as :
#define __get_cpu_var(var) (*this_cpu_ptr(&(var)))
__get_cpu_var() always only does an address determination. However, store
and retrieve operations could use a segment prefix (or global register on
other platforms) to avoid the address calculation.
this_cpu_write() and this_cpu_read() can directly take an offset into a
percpu area and use optimized assembly code to read and write per cpu
variables.
This patch converts __get_cpu_var into either an explicit address
calculation using this_cpu_ptr() or into a use of this_cpu operations that
use the offset. Thereby address calculations are avoided and less registers
are used when code is generated.
Transformations done to __get_cpu_var()
1. Determine the address of the percpu instance of the current processor.
DEFINE_PER_CPU(int, y);
int *x = &__get_cpu_var(y);
Converts to
int *x = this_cpu_ptr(&y);
2. Same as #1 but this time an array structure is involved.
DEFINE_PER_CPU(int, y[20]);
int *x = __get_cpu_var(y);
Converts to
int *x = this_cpu_ptr(y);
3. Retrieve the content of the current processors instance of a per cpu
variable.
DEFINE_PER_CPU(int, y);
int x = __get_cpu_var(y)
Converts to
int x = __this_cpu_read(y);
4. Retrieve the content of a percpu struct
DEFINE_PER_CPU(struct mystruct, y);
struct mystruct x = __get_cpu_var(y);
Converts to
memcpy(&x, this_cpu_ptr(&y), sizeof(x));
5. Assignment to a per cpu variable
DEFINE_PER_CPU(int, y)
__get_cpu_var(y) = x;
Converts to
__this_cpu_write(y, x);
6. Increment/Decrement etc of a per cpu variable
DEFINE_PER_CPU(int, y);
__get_cpu_var(y)++
Converts to
__this_cpu_inc(y)
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: x86@kernel.org
Acked-by: H. Peter Anvin <hpa@linux.intel.com>
Acked-by: Ingo Molnar <mingo@kernel.org>
Signed-off-by: Christoph Lameter <cl@linux.com>
Signed-off-by: Tejun Heo <tj@kernel.org>
2014-08-18 01:30:40 +08:00
|
|
|
if (__this_cpu_read(apf_reason.enabled)) {
|
2020-05-25 22:41:17 +08:00
|
|
|
flags = __this_cpu_read(apf_reason.flags);
|
|
|
|
__this_cpu_write(apf_reason.flags, 0);
|
2010-10-14 17:22:52 +08:00
|
|
|
}
|
|
|
|
|
2020-05-25 22:41:17 +08:00
|
|
|
return flags;
|
2010-10-14 17:22:52 +08:00
|
|
|
}
|
2020-05-25 22:41:17 +08:00
|
|
|
EXPORT_SYMBOL_GPL(kvm_read_and_reset_apf_flags);
|
2010-10-14 17:22:52 +08:00
|
|
|
|
2020-05-22 04:05:28 +08:00
|
|
|
noinstr bool __kvm_handle_async_pf(struct pt_regs *regs, u32 token)
|
2010-10-14 17:22:52 +08:00
|
|
|
{
|
2020-05-25 22:41:23 +08:00
|
|
|
u32 flags = kvm_read_and_reset_apf_flags();
|
2020-07-23 06:00:08 +08:00
|
|
|
irqentry_state_t state;
|
2020-03-07 07:42:06 +08:00
|
|
|
|
2020-05-25 22:41:23 +08:00
|
|
|
if (!flags)
|
2020-02-29 02:42:48 +08:00
|
|
|
return false;
|
2020-03-07 07:42:06 +08:00
|
|
|
|
2020-07-23 06:00:08 +08:00
|
|
|
state = irqentry_enter(regs);
|
2020-05-22 04:05:28 +08:00
|
|
|
instrumentation_begin();
|
|
|
|
|
2020-03-07 07:42:06 +08:00
|
|
|
/*
|
|
|
|
* If the host managed to inject an async #PF into an interrupt
|
|
|
|
* disabled region, then die hard as this is not going to end well
|
|
|
|
* and the host side is seriously broken.
|
|
|
|
*/
|
|
|
|
if (unlikely(!(regs->flags & X86_EFLAGS_IF)))
|
|
|
|
panic("Host injected async #PF in interrupt disabled region\n");
|
|
|
|
|
2020-05-25 22:41:23 +08:00
|
|
|
if (flags & KVM_PV_REASON_PAGE_NOT_PRESENT) {
|
2020-04-24 15:57:56 +08:00
|
|
|
if (unlikely(!(user_mode(regs))))
|
|
|
|
panic("Host injected async #PF in kernel mode\n");
|
|
|
|
/* Page is swapped out by the host. */
|
|
|
|
kvm_async_pf_task_wait_schedule(token);
|
2020-03-07 07:42:06 +08:00
|
|
|
} else {
|
2020-06-15 19:53:05 +08:00
|
|
|
WARN_ONCE(1, "Unexpected async PF flags: %x\n", flags);
|
2010-10-14 17:22:52 +08:00
|
|
|
}
|
2020-05-22 04:05:28 +08:00
|
|
|
|
|
|
|
instrumentation_end();
|
2020-07-23 06:00:08 +08:00
|
|
|
irqentry_exit(regs, state);
|
2020-03-07 07:42:06 +08:00
|
|
|
return true;
|
2010-10-14 17:22:52 +08:00
|
|
|
}
|
|
|
|
|
2020-06-15 19:53:05 +08:00
|
|
|
DEFINE_IDTENTRY_SYSVEC(sysvec_kvm_asyncpf_interrupt)
|
2020-05-25 22:41:23 +08:00
|
|
|
{
|
2020-06-15 19:53:05 +08:00
|
|
|
struct pt_regs *old_regs = set_irq_regs(regs);
|
2020-05-25 22:41:23 +08:00
|
|
|
u32 token;
|
|
|
|
|
2020-09-08 21:53:50 +08:00
|
|
|
ack_APIC_irq();
|
|
|
|
|
2020-05-25 22:41:23 +08:00
|
|
|
inc_irq_stat(irq_hv_callback_count);
|
|
|
|
|
|
|
|
if (__this_cpu_read(apf_reason.enabled)) {
|
|
|
|
token = __this_cpu_read(apf_reason.token);
|
2020-02-29 02:42:48 +08:00
|
|
|
kvm_async_pf_task_wake(token);
|
2020-05-25 22:41:23 +08:00
|
|
|
__this_cpu_write(apf_reason.token, 0);
|
|
|
|
wrmsrl(MSR_KVM_ASYNC_PF_ACK, 1);
|
2010-10-14 17:22:52 +08:00
|
|
|
}
|
2020-05-25 22:41:23 +08:00
|
|
|
|
2020-06-15 19:53:05 +08:00
|
|
|
set_irq_regs(old_regs);
|
2010-10-14 17:22:52 +08:00
|
|
|
}
|
|
|
|
|
2009-07-02 13:40:36 +08:00
|
|
|
static void __init paravirt_ops_setup(void)
|
2008-02-23 01:21:36 +08:00
|
|
|
{
|
|
|
|
pv_info.name = "KVM";
|
2014-12-06 11:03:28 +08:00
|
|
|
|
2008-02-23 01:21:36 +08:00
|
|
|
if (kvm_para_has_feature(KVM_FEATURE_NOP_IO_DELAY))
|
2018-08-28 15:40:19 +08:00
|
|
|
pv_ops.cpu.io_delay = kvm_io_delay;
|
2008-02-23 01:21:36 +08:00
|
|
|
|
2009-02-12 08:45:42 +08:00
|
|
|
#ifdef CONFIG_X86_IO_APIC
|
|
|
|
no_timer_check = 1;
|
|
|
|
#endif
|
2008-02-23 01:21:36 +08:00
|
|
|
}
|
|
|
|
|
2011-07-12 03:28:19 +08: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;
|
|
|
|
|
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-23 05:24:35 +08:00
|
|
|
wrmsrl(MSR_KVM_STEAL_TIME, (slow_virt_to_phys(st) | KVM_MSR_ENABLED));
|
2019-10-23 19:16:21 +08:00
|
|
|
pr_info("stealtime: cpu %d, msr %llx\n", cpu,
|
|
|
|
(unsigned long long) slow_virt_to_phys(st));
|
2011-07-12 03:28:19 +08:00
|
|
|
}
|
|
|
|
|
2017-10-20 22:30:58 +08:00
|
|
|
static DEFINE_PER_CPU_DECRYPTED(unsigned long, kvm_apic_eoi) = KVM_PV_EOI_DISABLED;
|
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-25 00:24:34 +08:00
|
|
|
|
2016-11-07 11:13:40 +08:00
|
|
|
static notrace void kvm_guest_apic_eoi_write(u32 reg, u32 val)
|
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-25 00:24:34 +08:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
x86: Replace __get_cpu_var uses
__get_cpu_var() is used for multiple purposes in the kernel source. One of
them is address calculation via the form &__get_cpu_var(x). This calculates
the address for the instance of the percpu variable of the current processor
based on an offset.
Other use cases are for storing and retrieving data from the current
processors percpu area. __get_cpu_var() can be used as an lvalue when
writing data or on the right side of an assignment.
__get_cpu_var() is defined as :
#define __get_cpu_var(var) (*this_cpu_ptr(&(var)))
__get_cpu_var() always only does an address determination. However, store
and retrieve operations could use a segment prefix (or global register on
other platforms) to avoid the address calculation.
this_cpu_write() and this_cpu_read() can directly take an offset into a
percpu area and use optimized assembly code to read and write per cpu
variables.
This patch converts __get_cpu_var into either an explicit address
calculation using this_cpu_ptr() or into a use of this_cpu operations that
use the offset. Thereby address calculations are avoided and less registers
are used when code is generated.
Transformations done to __get_cpu_var()
1. Determine the address of the percpu instance of the current processor.
DEFINE_PER_CPU(int, y);
int *x = &__get_cpu_var(y);
Converts to
int *x = this_cpu_ptr(&y);
2. Same as #1 but this time an array structure is involved.
DEFINE_PER_CPU(int, y[20]);
int *x = __get_cpu_var(y);
Converts to
int *x = this_cpu_ptr(y);
3. Retrieve the content of the current processors instance of a per cpu
variable.
DEFINE_PER_CPU(int, y);
int x = __get_cpu_var(y)
Converts to
int x = __this_cpu_read(y);
4. Retrieve the content of a percpu struct
DEFINE_PER_CPU(struct mystruct, y);
struct mystruct x = __get_cpu_var(y);
Converts to
memcpy(&x, this_cpu_ptr(&y), sizeof(x));
5. Assignment to a per cpu variable
DEFINE_PER_CPU(int, y)
__get_cpu_var(y) = x;
Converts to
__this_cpu_write(y, x);
6. Increment/Decrement etc of a per cpu variable
DEFINE_PER_CPU(int, y);
__get_cpu_var(y)++
Converts to
__this_cpu_inc(y)
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: x86@kernel.org
Acked-by: H. Peter Anvin <hpa@linux.intel.com>
Acked-by: Ingo Molnar <mingo@kernel.org>
Signed-off-by: Christoph Lameter <cl@linux.com>
Signed-off-by: Tejun Heo <tj@kernel.org>
2014-08-18 01:30:40 +08:00
|
|
|
if (__test_and_clear_bit(KVM_PV_EOI_BIT, this_cpu_ptr(&kvm_apic_eoi)))
|
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-25 00:24:34 +08:00
|
|
|
return;
|
2016-11-07 11:13:40 +08:00
|
|
|
apic->native_eoi_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-25 00:24:34 +08:00
|
|
|
}
|
|
|
|
|
2015-05-20 12:24:10 +08:00
|
|
|
static void kvm_guest_cpu_init(void)
|
2010-10-14 17:22:51 +08:00
|
|
|
{
|
2020-05-25 22:41:23 +08:00
|
|
|
if (kvm_para_has_feature(KVM_FEATURE_ASYNC_PF_INT) && kvmapf) {
|
|
|
|
u64 pa = slow_virt_to_phys(this_cpu_ptr(&apf_reason));
|
2020-02-29 02:42:48 +08:00
|
|
|
|
|
|
|
WARN_ON_ONCE(!static_branch_likely(&kvm_async_pf_enabled));
|
|
|
|
|
|
|
|
pa = slow_virt_to_phys(this_cpu_ptr(&apf_reason));
|
2020-05-25 22:41:23 +08:00
|
|
|
pa |= KVM_ASYNC_PF_ENABLED | KVM_ASYNC_PF_DELIVERY_AS_INT;
|
2017-07-14 09:30:42 +08:00
|
|
|
|
2018-02-02 05:16:21 +08:00
|
|
|
if (kvm_para_has_feature(KVM_FEATURE_ASYNC_PF_VMEXIT))
|
|
|
|
pa |= KVM_ASYNC_PF_DELIVERY_AS_PF_VMEXIT;
|
|
|
|
|
2020-05-25 22:41:23 +08:00
|
|
|
wrmsrl(MSR_KVM_ASYNC_PF_INT, HYPERVISOR_CALLBACK_VECTOR);
|
|
|
|
|
2018-02-02 05:16:21 +08:00
|
|
|
wrmsrl(MSR_KVM_ASYNC_PF_EN, pa);
|
x86: Replace __get_cpu_var uses
__get_cpu_var() is used for multiple purposes in the kernel source. One of
them is address calculation via the form &__get_cpu_var(x). This calculates
the address for the instance of the percpu variable of the current processor
based on an offset.
Other use cases are for storing and retrieving data from the current
processors percpu area. __get_cpu_var() can be used as an lvalue when
writing data or on the right side of an assignment.
__get_cpu_var() is defined as :
#define __get_cpu_var(var) (*this_cpu_ptr(&(var)))
__get_cpu_var() always only does an address determination. However, store
and retrieve operations could use a segment prefix (or global register on
other platforms) to avoid the address calculation.
this_cpu_write() and this_cpu_read() can directly take an offset into a
percpu area and use optimized assembly code to read and write per cpu
variables.
This patch converts __get_cpu_var into either an explicit address
calculation using this_cpu_ptr() or into a use of this_cpu operations that
use the offset. Thereby address calculations are avoided and less registers
are used when code is generated.
Transformations done to __get_cpu_var()
1. Determine the address of the percpu instance of the current processor.
DEFINE_PER_CPU(int, y);
int *x = &__get_cpu_var(y);
Converts to
int *x = this_cpu_ptr(&y);
2. Same as #1 but this time an array structure is involved.
DEFINE_PER_CPU(int, y[20]);
int *x = __get_cpu_var(y);
Converts to
int *x = this_cpu_ptr(y);
3. Retrieve the content of the current processors instance of a per cpu
variable.
DEFINE_PER_CPU(int, y);
int x = __get_cpu_var(y)
Converts to
int x = __this_cpu_read(y);
4. Retrieve the content of a percpu struct
DEFINE_PER_CPU(struct mystruct, y);
struct mystruct x = __get_cpu_var(y);
Converts to
memcpy(&x, this_cpu_ptr(&y), sizeof(x));
5. Assignment to a per cpu variable
DEFINE_PER_CPU(int, y)
__get_cpu_var(y) = x;
Converts to
__this_cpu_write(y, x);
6. Increment/Decrement etc of a per cpu variable
DEFINE_PER_CPU(int, y);
__get_cpu_var(y)++
Converts to
__this_cpu_inc(y)
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: x86@kernel.org
Acked-by: H. Peter Anvin <hpa@linux.intel.com>
Acked-by: Ingo Molnar <mingo@kernel.org>
Signed-off-by: Christoph Lameter <cl@linux.com>
Signed-off-by: Tejun Heo <tj@kernel.org>
2014-08-18 01:30:40 +08:00
|
|
|
__this_cpu_write(apf_reason.enabled, 1);
|
2020-03-07 07:42:06 +08:00
|
|
|
pr_info("KVM setup async PF for cpu %d\n", smp_processor_id());
|
2010-10-14 17:22:51 +08:00
|
|
|
}
|
2011-07-12 03:28:19 +08: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-25 00:24:34 +08:00
|
|
|
if (kvm_para_has_feature(KVM_FEATURE_PV_EOI)) {
|
|
|
|
unsigned long pa;
|
2020-03-07 07:42:06 +08: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-25 00:24:34 +08:00
|
|
|
/* Size alignment is implied but just to make it explicit. */
|
|
|
|
BUILD_BUG_ON(__alignof__(kvm_apic_eoi) < 4);
|
x86: Replace __get_cpu_var uses
__get_cpu_var() is used for multiple purposes in the kernel source. One of
them is address calculation via the form &__get_cpu_var(x). This calculates
the address for the instance of the percpu variable of the current processor
based on an offset.
Other use cases are for storing and retrieving data from the current
processors percpu area. __get_cpu_var() can be used as an lvalue when
writing data or on the right side of an assignment.
__get_cpu_var() is defined as :
#define __get_cpu_var(var) (*this_cpu_ptr(&(var)))
__get_cpu_var() always only does an address determination. However, store
and retrieve operations could use a segment prefix (or global register on
other platforms) to avoid the address calculation.
this_cpu_write() and this_cpu_read() can directly take an offset into a
percpu area and use optimized assembly code to read and write per cpu
variables.
This patch converts __get_cpu_var into either an explicit address
calculation using this_cpu_ptr() or into a use of this_cpu operations that
use the offset. Thereby address calculations are avoided and less registers
are used when code is generated.
Transformations done to __get_cpu_var()
1. Determine the address of the percpu instance of the current processor.
DEFINE_PER_CPU(int, y);
int *x = &__get_cpu_var(y);
Converts to
int *x = this_cpu_ptr(&y);
2. Same as #1 but this time an array structure is involved.
DEFINE_PER_CPU(int, y[20]);
int *x = __get_cpu_var(y);
Converts to
int *x = this_cpu_ptr(y);
3. Retrieve the content of the current processors instance of a per cpu
variable.
DEFINE_PER_CPU(int, y);
int x = __get_cpu_var(y)
Converts to
int x = __this_cpu_read(y);
4. Retrieve the content of a percpu struct
DEFINE_PER_CPU(struct mystruct, y);
struct mystruct x = __get_cpu_var(y);
Converts to
memcpy(&x, this_cpu_ptr(&y), sizeof(x));
5. Assignment to a per cpu variable
DEFINE_PER_CPU(int, y)
__get_cpu_var(y) = x;
Converts to
__this_cpu_write(y, x);
6. Increment/Decrement etc of a per cpu variable
DEFINE_PER_CPU(int, y);
__get_cpu_var(y)++
Converts to
__this_cpu_inc(y)
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: x86@kernel.org
Acked-by: H. Peter Anvin <hpa@linux.intel.com>
Acked-by: Ingo Molnar <mingo@kernel.org>
Signed-off-by: Christoph Lameter <cl@linux.com>
Signed-off-by: Tejun Heo <tj@kernel.org>
2014-08-18 01:30:40 +08:00
|
|
|
__this_cpu_write(kvm_apic_eoi, 0);
|
|
|
|
pa = slow_virt_to_phys(this_cpu_ptr(&kvm_apic_eoi))
|
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-23 05:24:35 +08:00
|
|
|
| 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-25 00:24:34 +08:00
|
|
|
wrmsrl(MSR_KVM_PV_EOI_EN, pa);
|
|
|
|
}
|
|
|
|
|
2011-07-12 03:28:19 +08:00
|
|
|
if (has_steal_clock)
|
|
|
|
kvm_register_steal_time();
|
2010-10-14 17:22:51 +08: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-25 00:24:34 +08:00
|
|
|
static void kvm_pv_disable_apf(void)
|
2010-10-14 17:22:51 +08:00
|
|
|
{
|
x86: Replace __get_cpu_var uses
__get_cpu_var() is used for multiple purposes in the kernel source. One of
them is address calculation via the form &__get_cpu_var(x). This calculates
the address for the instance of the percpu variable of the current processor
based on an offset.
Other use cases are for storing and retrieving data from the current
processors percpu area. __get_cpu_var() can be used as an lvalue when
writing data or on the right side of an assignment.
__get_cpu_var() is defined as :
#define __get_cpu_var(var) (*this_cpu_ptr(&(var)))
__get_cpu_var() always only does an address determination. However, store
and retrieve operations could use a segment prefix (or global register on
other platforms) to avoid the address calculation.
this_cpu_write() and this_cpu_read() can directly take an offset into a
percpu area and use optimized assembly code to read and write per cpu
variables.
This patch converts __get_cpu_var into either an explicit address
calculation using this_cpu_ptr() or into a use of this_cpu operations that
use the offset. Thereby address calculations are avoided and less registers
are used when code is generated.
Transformations done to __get_cpu_var()
1. Determine the address of the percpu instance of the current processor.
DEFINE_PER_CPU(int, y);
int *x = &__get_cpu_var(y);
Converts to
int *x = this_cpu_ptr(&y);
2. Same as #1 but this time an array structure is involved.
DEFINE_PER_CPU(int, y[20]);
int *x = __get_cpu_var(y);
Converts to
int *x = this_cpu_ptr(y);
3. Retrieve the content of the current processors instance of a per cpu
variable.
DEFINE_PER_CPU(int, y);
int x = __get_cpu_var(y)
Converts to
int x = __this_cpu_read(y);
4. Retrieve the content of a percpu struct
DEFINE_PER_CPU(struct mystruct, y);
struct mystruct x = __get_cpu_var(y);
Converts to
memcpy(&x, this_cpu_ptr(&y), sizeof(x));
5. Assignment to a per cpu variable
DEFINE_PER_CPU(int, y)
__get_cpu_var(y) = x;
Converts to
__this_cpu_write(y, x);
6. Increment/Decrement etc of a per cpu variable
DEFINE_PER_CPU(int, y);
__get_cpu_var(y)++
Converts to
__this_cpu_inc(y)
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: x86@kernel.org
Acked-by: H. Peter Anvin <hpa@linux.intel.com>
Acked-by: Ingo Molnar <mingo@kernel.org>
Signed-off-by: Christoph Lameter <cl@linux.com>
Signed-off-by: Tejun Heo <tj@kernel.org>
2014-08-18 01:30:40 +08:00
|
|
|
if (!__this_cpu_read(apf_reason.enabled))
|
2010-10-14 17:22:51 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
wrmsrl(MSR_KVM_ASYNC_PF_EN, 0);
|
x86: Replace __get_cpu_var uses
__get_cpu_var() is used for multiple purposes in the kernel source. One of
them is address calculation via the form &__get_cpu_var(x). This calculates
the address for the instance of the percpu variable of the current processor
based on an offset.
Other use cases are for storing and retrieving data from the current
processors percpu area. __get_cpu_var() can be used as an lvalue when
writing data or on the right side of an assignment.
__get_cpu_var() is defined as :
#define __get_cpu_var(var) (*this_cpu_ptr(&(var)))
__get_cpu_var() always only does an address determination. However, store
and retrieve operations could use a segment prefix (or global register on
other platforms) to avoid the address calculation.
this_cpu_write() and this_cpu_read() can directly take an offset into a
percpu area and use optimized assembly code to read and write per cpu
variables.
This patch converts __get_cpu_var into either an explicit address
calculation using this_cpu_ptr() or into a use of this_cpu operations that
use the offset. Thereby address calculations are avoided and less registers
are used when code is generated.
Transformations done to __get_cpu_var()
1. Determine the address of the percpu instance of the current processor.
DEFINE_PER_CPU(int, y);
int *x = &__get_cpu_var(y);
Converts to
int *x = this_cpu_ptr(&y);
2. Same as #1 but this time an array structure is involved.
DEFINE_PER_CPU(int, y[20]);
int *x = __get_cpu_var(y);
Converts to
int *x = this_cpu_ptr(y);
3. Retrieve the content of the current processors instance of a per cpu
variable.
DEFINE_PER_CPU(int, y);
int x = __get_cpu_var(y)
Converts to
int x = __this_cpu_read(y);
4. Retrieve the content of a percpu struct
DEFINE_PER_CPU(struct mystruct, y);
struct mystruct x = __get_cpu_var(y);
Converts to
memcpy(&x, this_cpu_ptr(&y), sizeof(x));
5. Assignment to a per cpu variable
DEFINE_PER_CPU(int, y)
__get_cpu_var(y) = x;
Converts to
__this_cpu_write(y, x);
6. Increment/Decrement etc of a per cpu variable
DEFINE_PER_CPU(int, y);
__get_cpu_var(y)++
Converts to
__this_cpu_inc(y)
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: x86@kernel.org
Acked-by: H. Peter Anvin <hpa@linux.intel.com>
Acked-by: Ingo Molnar <mingo@kernel.org>
Signed-off-by: Christoph Lameter <cl@linux.com>
Signed-off-by: Tejun Heo <tj@kernel.org>
2014-08-18 01:30:40 +08:00
|
|
|
__this_cpu_write(apf_reason.enabled, 0);
|
2010-10-14 17:22:51 +08:00
|
|
|
|
2020-03-07 07:42:06 +08:00
|
|
|
pr_info("Unregister pv shared memory for cpu %d\n", smp_processor_id());
|
2010-10-14 17:22:51 +08: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-25 00:24:34 +08: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 22:00:40 +08: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-25 00:24:34 +08:00
|
|
|
}
|
|
|
|
|
2010-10-14 17:22:51 +08: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-25 00:24:34 +08:00
|
|
|
on_each_cpu(kvm_pv_guest_cpu_reboot, NULL, 1);
|
2010-10-14 17:22:51 +08:00
|
|
|
return NOTIFY_DONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct notifier_block kvm_pv_reboot_nb = {
|
|
|
|
.notifier_call = kvm_pv_reboot_notify,
|
|
|
|
};
|
|
|
|
|
2011-07-12 03:28:19 +08: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;
|
2017-04-11 17:49:21 +08:00
|
|
|
virt_rmb();
|
2011-07-12 03:28:19 +08:00
|
|
|
steal = src->steal;
|
2017-04-11 17:49:21 +08:00
|
|
|
virt_rmb();
|
2011-07-12 03:28:19 +08:00
|
|
|
} 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);
|
|
|
|
}
|
|
|
|
|
2017-10-20 22:30:58 +08:00
|
|
|
static inline void __set_percpu_decrypted(void *ptr, unsigned long size)
|
|
|
|
{
|
|
|
|
early_set_memory_decrypted((unsigned long) ptr, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Iterate through all possible CPUs and map the memory region pointed
|
|
|
|
* by apf_reason, steal_time and kvm_apic_eoi as decrypted at once.
|
|
|
|
*
|
|
|
|
* Note: we iterate through all possible CPUs to ensure that CPUs
|
|
|
|
* hotplugged will have their per-cpu variable already mapped as
|
|
|
|
* decrypted.
|
|
|
|
*/
|
|
|
|
static void __init sev_map_percpu_data(void)
|
|
|
|
{
|
|
|
|
int cpu;
|
|
|
|
|
|
|
|
if (!sev_active())
|
|
|
|
return;
|
|
|
|
|
|
|
|
for_each_possible_cpu(cpu) {
|
|
|
|
__set_percpu_decrypted(&per_cpu(apf_reason, cpu), sizeof(apf_reason));
|
|
|
|
__set_percpu_decrypted(&per_cpu(steal_time, cpu), sizeof(steal_time));
|
|
|
|
__set_percpu_decrypted(&per_cpu(kvm_apic_eoi, cpu), sizeof(kvm_apic_eoi));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-18 09:08:23 +08:00
|
|
|
static bool pv_tlb_flush_supported(void)
|
|
|
|
{
|
|
|
|
return (kvm_para_has_feature(KVM_FEATURE_PV_TLB_FLUSH) &&
|
|
|
|
!kvm_para_has_hint(KVM_HINTS_REALTIME) &&
|
|
|
|
kvm_para_has_feature(KVM_FEATURE_STEAL_TIME));
|
|
|
|
}
|
|
|
|
|
2020-02-18 09:08:24 +08:00
|
|
|
static DEFINE_PER_CPU(cpumask_var_t, __pv_cpu_mask);
|
|
|
|
|
2010-10-14 17:22:49 +08:00
|
|
|
#ifdef CONFIG_SMP
|
2020-02-18 09:08:23 +08:00
|
|
|
|
|
|
|
static bool pv_ipi_supported(void)
|
|
|
|
{
|
|
|
|
return kvm_para_has_feature(KVM_FEATURE_PV_SEND_IPI);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool pv_sched_yield_supported(void)
|
|
|
|
{
|
|
|
|
return (kvm_para_has_feature(KVM_FEATURE_PV_SCHED_YIELD) &&
|
|
|
|
!kvm_para_has_hint(KVM_HINTS_REALTIME) &&
|
|
|
|
kvm_para_has_feature(KVM_FEATURE_STEAL_TIME));
|
|
|
|
}
|
|
|
|
|
2018-07-23 14:39:52 +08:00
|
|
|
#define KVM_IPI_CLUSTER_SIZE (2 * BITS_PER_LONG)
|
|
|
|
|
|
|
|
static void __send_ipi_mask(const struct cpumask *mask, int vector)
|
|
|
|
{
|
|
|
|
unsigned long flags;
|
|
|
|
int cpu, apic_id, icr;
|
|
|
|
int min = 0, max = 0;
|
|
|
|
#ifdef CONFIG_X86_64
|
|
|
|
__uint128_t ipi_bitmap = 0;
|
|
|
|
#else
|
|
|
|
u64 ipi_bitmap = 0;
|
|
|
|
#endif
|
2019-01-24 01:22:40 +08:00
|
|
|
long ret;
|
2018-07-23 14:39:52 +08:00
|
|
|
|
|
|
|
if (cpumask_empty(mask))
|
|
|
|
return;
|
|
|
|
|
|
|
|
local_irq_save(flags);
|
|
|
|
|
|
|
|
switch (vector) {
|
|
|
|
default:
|
|
|
|
icr = APIC_DM_FIXED | vector;
|
|
|
|
break;
|
|
|
|
case NMI_VECTOR:
|
|
|
|
icr = APIC_DM_NMI;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
for_each_cpu(cpu, mask) {
|
|
|
|
apic_id = per_cpu(x86_cpu_to_apicid, cpu);
|
|
|
|
if (!ipi_bitmap) {
|
|
|
|
min = max = apic_id;
|
|
|
|
} else if (apic_id < min && max - apic_id < KVM_IPI_CLUSTER_SIZE) {
|
|
|
|
ipi_bitmap <<= min - apic_id;
|
|
|
|
min = apic_id;
|
|
|
|
} else if (apic_id < min + KVM_IPI_CLUSTER_SIZE) {
|
|
|
|
max = apic_id < max ? max : apic_id;
|
|
|
|
} else {
|
2019-01-24 01:22:40 +08:00
|
|
|
ret = kvm_hypercall4(KVM_HC_SEND_IPI, (unsigned long)ipi_bitmap,
|
2018-07-23 14:39:52 +08:00
|
|
|
(unsigned long)(ipi_bitmap >> BITS_PER_LONG), min, icr);
|
2019-10-23 19:16:21 +08:00
|
|
|
WARN_ONCE(ret < 0, "kvm-guest: failed to send PV IPI: %ld",
|
|
|
|
ret);
|
2018-07-23 14:39:52 +08:00
|
|
|
min = max = apic_id;
|
|
|
|
ipi_bitmap = 0;
|
|
|
|
}
|
|
|
|
__set_bit(apic_id - min, (unsigned long *)&ipi_bitmap);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ipi_bitmap) {
|
2019-01-24 01:22:40 +08:00
|
|
|
ret = kvm_hypercall4(KVM_HC_SEND_IPI, (unsigned long)ipi_bitmap,
|
2018-07-23 14:39:52 +08:00
|
|
|
(unsigned long)(ipi_bitmap >> BITS_PER_LONG), min, icr);
|
2019-10-23 19:16:21 +08:00
|
|
|
WARN_ONCE(ret < 0, "kvm-guest: failed to send PV IPI: %ld",
|
|
|
|
ret);
|
2018-07-23 14:39:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
local_irq_restore(flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void kvm_send_ipi_mask(const struct cpumask *mask, int vector)
|
|
|
|
{
|
|
|
|
__send_ipi_mask(mask, vector);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void kvm_send_ipi_mask_allbutself(const struct cpumask *mask, int vector)
|
|
|
|
{
|
|
|
|
unsigned int this_cpu = smp_processor_id();
|
2020-02-18 09:08:24 +08:00
|
|
|
struct cpumask *new_mask = this_cpu_cpumask_var_ptr(__pv_cpu_mask);
|
2018-07-23 14:39:52 +08:00
|
|
|
const struct cpumask *local_mask;
|
|
|
|
|
2020-02-18 09:08:24 +08:00
|
|
|
cpumask_copy(new_mask, mask);
|
|
|
|
cpumask_clear_cpu(this_cpu, new_mask);
|
|
|
|
local_mask = new_mask;
|
2018-07-23 14:39:52 +08:00
|
|
|
__send_ipi_mask(local_mask, vector);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set the IPI entry points
|
|
|
|
*/
|
|
|
|
static void kvm_setup_pv_ipi(void)
|
|
|
|
{
|
|
|
|
apic->send_IPI_mask = kvm_send_ipi_mask;
|
|
|
|
apic->send_IPI_mask_allbutself = kvm_send_ipi_mask_allbutself;
|
2019-10-23 19:16:21 +08:00
|
|
|
pr_info("setup PV IPIs\n");
|
2018-07-23 14:39:52 +08:00
|
|
|
}
|
|
|
|
|
2019-06-11 20:23:48 +08:00
|
|
|
static void kvm_smp_send_call_func_ipi(const struct cpumask *mask)
|
|
|
|
{
|
|
|
|
int cpu;
|
|
|
|
|
|
|
|
native_send_call_func_ipi(mask);
|
|
|
|
|
|
|
|
/* Make sure other vCPUs get a chance to run if they need to. */
|
|
|
|
for_each_cpu(cpu, mask) {
|
|
|
|
if (vcpu_is_preempted(cpu)) {
|
|
|
|
kvm_hypercall1(KVM_HC_SCHED_YIELD, per_cpu(x86_cpu_to_apicid, cpu));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-14 17:22:49 +08:00
|
|
|
static void __init kvm_smp_prepare_boot_cpu(void)
|
|
|
|
{
|
2017-10-20 22:30:58 +08:00
|
|
|
/*
|
|
|
|
* Map the per-cpu variables as decrypted before kvm_guest_cpu_init()
|
|
|
|
* shares the guest physical address with the hypervisor.
|
|
|
|
*/
|
|
|
|
sev_map_percpu_data();
|
|
|
|
|
2010-10-14 17:22:51 +08:00
|
|
|
kvm_guest_cpu_init();
|
2010-10-14 17:22:49 +08:00
|
|
|
native_smp_prepare_boot_cpu();
|
2013-08-06 17:25:41 +08:00
|
|
|
kvm_spinlock_init();
|
2010-10-14 17:22:49 +08:00
|
|
|
}
|
2010-10-14 17:22:51 +08:00
|
|
|
|
2016-08-18 20:57:29 +08:00
|
|
|
static void kvm_guest_cpu_offline(void)
|
2010-10-14 17:22:51 +08:00
|
|
|
{
|
2011-07-12 03:28:19 +08: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-25 00:24:34 +08:00
|
|
|
if (kvm_para_has_feature(KVM_FEATURE_PV_EOI))
|
|
|
|
wrmsrl(MSR_KVM_PV_EOI_EN, 0);
|
|
|
|
kvm_pv_disable_apf();
|
2010-10-14 17:22:52 +08:00
|
|
|
apf_task_wake_all();
|
2010-10-14 17:22:51 +08:00
|
|
|
}
|
|
|
|
|
2016-08-18 20:57:29 +08:00
|
|
|
static int kvm_cpu_online(unsigned int cpu)
|
2010-10-14 17:22:51 +08:00
|
|
|
{
|
2016-08-18 20:57:29 +08:00
|
|
|
local_irq_disable();
|
|
|
|
kvm_guest_cpu_init();
|
|
|
|
local_irq_enable();
|
|
|
|
return 0;
|
2010-10-14 17:22:51 +08:00
|
|
|
}
|
|
|
|
|
2016-08-18 20:57:29 +08:00
|
|
|
static int kvm_cpu_down_prepare(unsigned int cpu)
|
|
|
|
{
|
|
|
|
local_irq_disable();
|
|
|
|
kvm_guest_cpu_offline();
|
|
|
|
local_irq_enable();
|
|
|
|
return 0;
|
|
|
|
}
|
2010-10-14 17:22:49 +08:00
|
|
|
#endif
|
|
|
|
|
2017-12-13 09:33:02 +08:00
|
|
|
static void kvm_flush_tlb_others(const struct cpumask *cpumask,
|
|
|
|
const struct flush_tlb_info *info)
|
|
|
|
{
|
|
|
|
u8 state;
|
|
|
|
int cpu;
|
|
|
|
struct kvm_steal_time *src;
|
2020-02-18 09:08:24 +08:00
|
|
|
struct cpumask *flushmask = this_cpu_cpumask_var_ptr(__pv_cpu_mask);
|
2017-12-13 09:33:02 +08:00
|
|
|
|
|
|
|
cpumask_copy(flushmask, cpumask);
|
|
|
|
/*
|
|
|
|
* We have to call flush only on online vCPUs. And
|
|
|
|
* queue flush_on_enter for pre-empted vCPUs
|
|
|
|
*/
|
|
|
|
for_each_cpu(cpu, flushmask) {
|
|
|
|
src = &per_cpu(steal_time, cpu);
|
|
|
|
state = READ_ONCE(src->preempted);
|
|
|
|
if ((state & KVM_VCPU_PREEMPTED)) {
|
|
|
|
if (try_cmpxchg(&src->preempted, &state,
|
|
|
|
state | KVM_VCPU_FLUSH_TLB))
|
|
|
|
__cpumask_clear_cpu(cpu, flushmask);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
native_flush_tlb_others(flushmask, info);
|
|
|
|
}
|
|
|
|
|
2017-11-09 21:27:38 +08:00
|
|
|
static void __init kvm_guest_init(void)
|
2008-02-23 01:21:36 +08:00
|
|
|
{
|
2010-10-14 17:22:52 +08:00
|
|
|
int i;
|
|
|
|
|
2008-02-23 01:21:36 +08:00
|
|
|
paravirt_ops_setup();
|
2010-10-14 17:22:51 +08:00
|
|
|
register_reboot_notifier(&kvm_pv_reboot_nb);
|
2010-10-14 17:22:52 +08:00
|
|
|
for (i = 0; i < KVM_TASK_SLEEP_HASHSIZE; i++)
|
2016-03-21 22:13:27 +08:00
|
|
|
raw_spin_lock_init(&async_pf_sleepers[i].lock);
|
2010-10-14 17:22:52 +08:00
|
|
|
|
2011-07-12 03:28:19 +08:00
|
|
|
if (kvm_para_has_feature(KVM_FEATURE_STEAL_TIME)) {
|
|
|
|
has_steal_clock = 1;
|
2018-08-28 15:40:19 +08:00
|
|
|
pv_ops.time.steal_clock = kvm_steal_clock;
|
2011-07-12 03:28:19 +08:00
|
|
|
}
|
|
|
|
|
2020-02-18 09:08:23 +08:00
|
|
|
if (pv_tlb_flush_supported()) {
|
2020-09-20 21:10:12 +08:00
|
|
|
pv_ops.mmu.flush_tlb_others = kvm_flush_tlb_others;
|
2018-08-28 15:40:19 +08:00
|
|
|
pv_ops.mmu.tlb_remove_table = tlb_remove_table;
|
2020-02-18 09:08:24 +08:00
|
|
|
pr_info("KVM setup pv remote TLB flush\n");
|
2018-08-22 23:30:16 +08:00
|
|
|
}
|
2017-12-13 09:33:02 +08:00
|
|
|
|
2012-07-15 20:56:52 +08: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-25 00:24:34 +08:00
|
|
|
|
2020-05-25 22:41:23 +08:00
|
|
|
if (kvm_para_has_feature(KVM_FEATURE_ASYNC_PF_INT) && kvmapf) {
|
2020-02-29 02:42:48 +08:00
|
|
|
static_branch_enable(&kvm_async_pf_enabled);
|
2020-06-15 19:53:05 +08:00
|
|
|
alloc_intr_gate(HYPERVISOR_CALLBACK_VECTOR, asm_sysvec_kvm_asyncpf_interrupt);
|
2020-05-25 22:41:23 +08:00
|
|
|
}
|
2020-02-29 02:42:48 +08:00
|
|
|
|
2010-10-14 17:22:49 +08:00
|
|
|
#ifdef CONFIG_SMP
|
|
|
|
smp_ops.smp_prepare_boot_cpu = kvm_smp_prepare_boot_cpu;
|
2020-02-18 09:08:23 +08:00
|
|
|
if (pv_sched_yield_supported()) {
|
2019-06-11 20:23:48 +08:00
|
|
|
smp_ops.send_call_func_ipi = kvm_smp_send_call_func_ipi;
|
2019-10-23 19:16:21 +08:00
|
|
|
pr_info("setup PV sched yield\n");
|
2019-06-11 20:23:48 +08:00
|
|
|
}
|
2016-08-18 20:57:29 +08:00
|
|
|
if (cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN, "x86/kvm:online",
|
|
|
|
kvm_cpu_online, kvm_cpu_down_prepare) < 0)
|
2019-10-23 19:16:21 +08:00
|
|
|
pr_err("failed to install cpu hotplug callbacks\n");
|
2010-10-14 17:22:51 +08:00
|
|
|
#else
|
2017-10-20 22:30:58 +08:00
|
|
|
sev_map_percpu_data();
|
2010-10-14 17:22:51 +08:00
|
|
|
kvm_guest_cpu_init();
|
2010-10-14 17:22:49 +08:00
|
|
|
#endif
|
2014-10-14 06:55:37 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Hard lockup detection is enabled by default. Disable it, as guests
|
|
|
|
* can get false positives too easily, for example if the host is
|
|
|
|
* overcommitted.
|
|
|
|
*/
|
2015-04-15 06:44:19 +08:00
|
|
|
hardlockup_detector_disable();
|
2008-02-23 01:21:36 +08:00
|
|
|
}
|
2011-07-12 03:28:19 +08:00
|
|
|
|
2014-01-27 21:49:40 +08: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 */
|
|
|
|
|
2016-03-29 23:41:55 +08:00
|
|
|
if (boot_cpu_has(X86_FEATURE_HYPERVISOR))
|
2014-01-27 21:49:40 +08:00
|
|
|
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 21:51:44 +08:00
|
|
|
unsigned int kvm_arch_para_features(void)
|
|
|
|
{
|
|
|
|
return cpuid_eax(kvm_cpuid_base() | KVM_CPUID_FEATURES);
|
|
|
|
}
|
|
|
|
|
2018-02-13 09:05:40 +08:00
|
|
|
unsigned int kvm_arch_para_hints(void)
|
|
|
|
{
|
|
|
|
return cpuid_edx(kvm_cpuid_base() | KVM_CPUID_FEATURES);
|
|
|
|
}
|
2019-08-29 16:49:57 +08:00
|
|
|
EXPORT_SYMBOL_GPL(kvm_arch_para_hints);
|
2018-02-13 09:05:40 +08:00
|
|
|
|
2013-07-25 16:54:35 +08:00
|
|
|
static uint32_t __init kvm_detect(void)
|
2012-07-07 01:47:39 +08:00
|
|
|
{
|
2013-07-25 16:54:35 +08:00
|
|
|
return kvm_cpuid_base();
|
2012-07-07 01:47:39 +08:00
|
|
|
}
|
|
|
|
|
2018-07-23 14:39:51 +08:00
|
|
|
static void __init kvm_apic_init(void)
|
|
|
|
{
|
2018-07-23 14:39:52 +08:00
|
|
|
#if defined(CONFIG_SMP)
|
2020-02-18 09:08:23 +08:00
|
|
|
if (pv_ipi_supported())
|
2018-07-23 14:39:52 +08:00
|
|
|
kvm_setup_pv_ipi();
|
|
|
|
#endif
|
2018-07-23 14:39:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void __init kvm_init_platform(void)
|
|
|
|
{
|
Minor code cleanups for PPC.
For x86 this brings in PCID emulation and CR3 caching for shadow page
tables, nested VMX live migration, nested VMCS shadowing, an optimized
IPI hypercall, and some optimizations.
ARM will come next week.
There is a semantic conflict because tip also added an .init_platform
callback to kvm.c. Please keep the initializer from this branch,
and add a call to kvmclock_init (added by tip) inside kvm_init_platform
(added here).
Also, there is a backmerge from 4.18-rc6. This is because of a
refactoring that conflicted with a relatively late bugfix and
resulted in a particularly hellish conflict. Because the conflict
was only due to unfortunate timing of the bugfix, I backmerged and
rebased the refactoring rather than force the resolution on you.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.22 (GNU/Linux)
iQEcBAABAgAGBQJbdwNFAAoJEL/70l94x66DiPEH/1cAGZWGd85Y3yRu1dmTmqiz
kZy0V+WTQ5kyJF4ZsZKKOp+xK7Qxh5e9kLdTo70uPZCHwLu9IaGKN9+dL9Jar3DR
yLPX5bMsL8UUed9g9mlhdaNOquWi7d7BseCOnIyRTolb+cqnM5h3sle0gqXloVrS
UQb4QogDz8+86czqR8tNfazjQRKW/D2HEGD5NDNVY1qtpY+leCDAn9/u6hUT5c6z
EtufgyDh35UN+UQH0e2605gt3nN3nw3FiQJFwFF1bKeQ7k5ByWkuGQI68XtFVhs+
2WfqL3ftERkKzUOy/WoSJX/C9owvhMcpAuHDGOIlFwguNGroZivOMVnACG1AI3I=
=9Mgw
-----END PGP SIGNATURE-----
Merge tag 'for-linus' of git://git.kernel.org/pub/scm/virt/kvm/kvm
Pull first set of KVM updates from Paolo Bonzini:
"PPC:
- minor code cleanups
x86:
- PCID emulation and CR3 caching for shadow page tables
- nested VMX live migration
- nested VMCS shadowing
- optimized IPI hypercall
- some optimizations
ARM will come next week"
* tag 'for-linus' of git://git.kernel.org/pub/scm/virt/kvm/kvm: (85 commits)
kvm: x86: Set highest physical address bits in non-present/reserved SPTEs
KVM/x86: Use CC_SET()/CC_OUT in arch/x86/kvm/vmx.c
KVM: X86: Implement PV IPIs in linux guest
KVM: X86: Add kvm hypervisor init time platform setup callback
KVM: X86: Implement "send IPI" hypercall
KVM/x86: Move X86_CR4_OSXSAVE check into kvm_valid_sregs()
KVM: x86: Skip pae_root shadow allocation if tdp enabled
KVM/MMU: Combine flushing remote tlb in mmu_set_spte()
KVM: vmx: skip VMWRITE of HOST_{FS,GS}_BASE when possible
KVM: vmx: skip VMWRITE of HOST_{FS,GS}_SEL when possible
KVM: vmx: always initialize HOST_{FS,GS}_BASE to zero during setup
KVM: vmx: move struct host_state usage to struct loaded_vmcs
KVM: vmx: compute need to reload FS/GS/LDT on demand
KVM: nVMX: remove a misleading comment regarding vmcs02 fields
KVM: vmx: rename __vmx_load_host_state() and vmx_save_host_state()
KVM: vmx: add dedicated utility to access guest's kernel_gs_base
KVM: vmx: track host_state.loaded using a loaded_vmcs pointer
KVM: vmx: refactor segmentation code in vmx_save_host_state()
kvm: nVMX: Fix fault priority for VMX operations
kvm: nVMX: Fix fault vector for VMX operation at CPL > 0
...
2018-08-20 01:38:36 +08:00
|
|
|
kvmclock_init();
|
2018-07-23 14:39:51 +08:00
|
|
|
x86_platform.apic_post_init = kvm_apic_init;
|
|
|
|
}
|
|
|
|
|
2017-11-09 21:27:36 +08:00
|
|
|
const __initconst struct hypervisor_x86 x86_hyper_kvm = {
|
2012-07-07 01:47:39 +08:00
|
|
|
.name = "KVM",
|
|
|
|
.detect = kvm_detect,
|
2017-11-09 21:27:36 +08:00
|
|
|
.type = X86_HYPER_KVM,
|
2017-11-09 21:27:38 +08:00
|
|
|
.init.guest_late_init = kvm_guest_init,
|
2017-11-09 21:27:35 +08:00
|
|
|
.init.x2apic_available = kvm_para_available,
|
2018-07-23 14:39:51 +08:00
|
|
|
.init.init_platform = kvm_init_platform,
|
2012-07-07 01:47:39 +08:00
|
|
|
};
|
|
|
|
|
2011-07-12 03:28:19 +08:00
|
|
|
static __init int activate_jump_labels(void)
|
|
|
|
{
|
|
|
|
if (has_steal_clock) {
|
2012-02-24 15:31:31 +08:00
|
|
|
static_key_slow_inc(¶virt_steal_enabled);
|
2011-07-12 03:28:19 +08:00
|
|
|
if (steal_acc)
|
2012-02-24 15:31:31 +08:00
|
|
|
static_key_slow_inc(¶virt_steal_rq_enabled);
|
2011-07-12 03:28:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
arch_initcall(activate_jump_labels);
|
2013-08-06 17:25:41 +08:00
|
|
|
|
2020-02-18 09:08:24 +08:00
|
|
|
static __init int kvm_alloc_cpumask(void)
|
2017-12-13 09:33:02 +08:00
|
|
|
{
|
|
|
|
int cpu;
|
2020-02-18 09:08:24 +08:00
|
|
|
bool alloc = false;
|
2017-12-13 09:33:02 +08:00
|
|
|
|
2020-01-31 23:56:55 +08:00
|
|
|
if (!kvm_para_available() || nopv)
|
|
|
|
return 0;
|
|
|
|
|
2020-02-18 09:08:24 +08:00
|
|
|
if (pv_tlb_flush_supported())
|
|
|
|
alloc = true;
|
|
|
|
|
|
|
|
#if defined(CONFIG_SMP)
|
|
|
|
if (pv_ipi_supported())
|
|
|
|
alloc = true;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (alloc)
|
2017-12-13 09:33:02 +08:00
|
|
|
for_each_possible_cpu(cpu) {
|
2020-09-20 21:10:12 +08:00
|
|
|
zalloc_cpumask_var_node(per_cpu_ptr(&__pv_cpu_mask, cpu),
|
|
|
|
GFP_KERNEL, cpu_to_node(cpu));
|
2017-12-13 09:33:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2020-02-18 09:08:24 +08:00
|
|
|
arch_initcall(kvm_alloc_cpumask);
|
2017-12-13 09:33:02 +08:00
|
|
|
|
2013-08-06 17:25:41 +08:00
|
|
|
#ifdef CONFIG_PARAVIRT_SPINLOCKS
|
|
|
|
|
|
|
|
/* Kick a cpu by its apicid. Used to wake up a halted vcpu */
|
2013-08-16 17:38:41 +08:00
|
|
|
static void kvm_kick_cpu(int cpu)
|
2013-08-06 17:25:41 +08:00
|
|
|
{
|
|
|
|
int apicid;
|
|
|
|
unsigned long flags = 0;
|
|
|
|
|
|
|
|
apicid = per_cpu(x86_cpu_to_apicid, cpu);
|
|
|
|
kvm_hypercall2(KVM_HC_KICK_CPU, flags, apicid);
|
|
|
|
}
|
|
|
|
|
2015-04-25 02:56:39 +08:00
|
|
|
#include <asm/qspinlock.h>
|
|
|
|
|
|
|
|
static void kvm_wait(u8 *ptr, u8 val)
|
|
|
|
{
|
|
|
|
unsigned long flags;
|
|
|
|
|
|
|
|
if (in_nmi())
|
|
|
|
return;
|
|
|
|
|
|
|
|
local_irq_save(flags);
|
|
|
|
|
|
|
|
if (READ_ONCE(*ptr) != val)
|
|
|
|
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:
|
|
|
|
local_irq_restore(flags);
|
|
|
|
}
|
|
|
|
|
2017-02-21 02:36:04 +08:00
|
|
|
#ifdef CONFIG_X86_32
|
2017-02-21 02:36:03 +08:00
|
|
|
__visible bool __kvm_vcpu_is_preempted(long cpu)
|
2016-11-15 23:47:06 +08:00
|
|
|
{
|
|
|
|
struct kvm_steal_time *src = &per_cpu(steal_time, cpu);
|
|
|
|
|
2017-12-13 09:33:01 +08:00
|
|
|
return !!(src->preempted & KVM_VCPU_PREEMPTED);
|
2016-11-15 23:47:06 +08:00
|
|
|
}
|
|
|
|
PV_CALLEE_SAVE_REGS_THUNK(__kvm_vcpu_is_preempted);
|
|
|
|
|
2017-02-21 02:36:04 +08:00
|
|
|
#else
|
|
|
|
|
|
|
|
#include <asm/asm-offsets.h>
|
|
|
|
|
|
|
|
extern bool __raw_callee_save___kvm_vcpu_is_preempted(long);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Hand-optimize version for x86-64 to avoid 8 64-bit register saving and
|
|
|
|
* restoring to/from the stack.
|
|
|
|
*/
|
|
|
|
asm(
|
|
|
|
".pushsection .text;"
|
|
|
|
".global __raw_callee_save___kvm_vcpu_is_preempted;"
|
|
|
|
".type __raw_callee_save___kvm_vcpu_is_preempted, @function;"
|
|
|
|
"__raw_callee_save___kvm_vcpu_is_preempted:"
|
|
|
|
"movq __per_cpu_offset(,%rdi,8), %rax;"
|
|
|
|
"cmpb $0, " __stringify(KVM_STEAL_TIME_preempted) "+steal_time(%rax);"
|
|
|
|
"setne %al;"
|
|
|
|
"ret;"
|
2019-07-18 09:36:36 +08:00
|
|
|
".size __raw_callee_save___kvm_vcpu_is_preempted, .-__raw_callee_save___kvm_vcpu_is_preempted;"
|
2017-02-21 02:36:04 +08:00
|
|
|
".popsection");
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2013-08-06 17:25:41 +08:00
|
|
|
/*
|
|
|
|
* Setup pv_lock_ops to exploit KVM_FEATURE_PV_UNHALT if present.
|
|
|
|
*/
|
|
|
|
void __init kvm_spinlock_init(void)
|
|
|
|
{
|
2019-10-23 19:16:22 +08:00
|
|
|
/*
|
|
|
|
* In case host doesn't support KVM_FEATURE_PV_UNHALT there is still an
|
|
|
|
* advantage of keeping virt_spin_lock_key enabled: virt_spin_lock() is
|
|
|
|
* preferred over native qspinlock when vCPU is preempted.
|
|
|
|
*/
|
|
|
|
if (!kvm_para_has_feature(KVM_FEATURE_PV_UNHALT)) {
|
|
|
|
pr_info("PV spinlocks disabled, no host support\n");
|
2013-08-06 17:25:41 +08:00
|
|
|
return;
|
2019-10-23 19:16:22 +08:00
|
|
|
}
|
2013-08-06 17:25:41 +08:00
|
|
|
|
2019-10-23 19:16:22 +08:00
|
|
|
/*
|
|
|
|
* Disable PV spinlocks and use native qspinlock when dedicated pCPUs
|
|
|
|
* are available.
|
|
|
|
*/
|
2019-10-23 19:16:20 +08:00
|
|
|
if (kvm_para_has_hint(KVM_HINTS_REALTIME)) {
|
2019-10-23 19:16:22 +08:00
|
|
|
pr_info("PV spinlocks disabled with KVM_HINTS_REALTIME hints\n");
|
|
|
|
goto out;
|
2019-10-23 19:16:20 +08:00
|
|
|
}
|
2018-02-13 09:05:41 +08:00
|
|
|
|
2019-10-23 19:16:22 +08:00
|
|
|
if (num_possible_cpus() == 1) {
|
|
|
|
pr_info("PV spinlocks disabled, single CPU\n");
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nopvspin) {
|
|
|
|
pr_info("PV spinlocks disabled, forced by \"nopvspin\" parameter\n");
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
pr_info("PV spinlocks enabled\n");
|
2018-07-18 05:59:27 +08:00
|
|
|
|
2015-04-25 02:56:39 +08:00
|
|
|
__pv_init_lock_hash();
|
2018-08-28 15:40:19 +08:00
|
|
|
pv_ops.lock.queued_spin_lock_slowpath = __pv_queued_spin_lock_slowpath;
|
|
|
|
pv_ops.lock.queued_spin_unlock =
|
|
|
|
PV_CALLEE_SAVE(__pv_queued_spin_unlock);
|
|
|
|
pv_ops.lock.wait = kvm_wait;
|
|
|
|
pv_ops.lock.kick = kvm_kick_cpu;
|
2016-11-15 23:47:06 +08:00
|
|
|
|
|
|
|
if (kvm_para_has_feature(KVM_FEATURE_STEAL_TIME)) {
|
2018-08-28 15:40:19 +08:00
|
|
|
pv_ops.lock.vcpu_is_preempted =
|
2016-11-15 23:47:06 +08:00
|
|
|
PV_CALLEE_SAVE(__kvm_vcpu_is_preempted);
|
|
|
|
}
|
2019-10-23 19:16:22 +08:00
|
|
|
/*
|
|
|
|
* When PV spinlock is enabled which is preferred over
|
|
|
|
* virt_spin_lock(), virt_spin_lock_key's value is meaningless.
|
|
|
|
* Just disable it anyway.
|
|
|
|
*/
|
|
|
|
out:
|
|
|
|
static_branch_disable(&virt_spin_lock_key);
|
2013-10-09 17:03:21 +08:00
|
|
|
}
|
|
|
|
|
2013-08-06 17:25:41 +08:00
|
|
|
#endif /* CONFIG_PARAVIRT_SPINLOCKS */
|
2019-07-04 07:51:29 +08:00
|
|
|
|
|
|
|
#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);
|
|
|
|
}
|
|
|
|
|
2019-09-02 18:40:31 +08:00
|
|
|
void arch_haltpoll_enable(unsigned int cpu)
|
2019-07-04 07:51:29 +08:00
|
|
|
{
|
|
|
|
if (!kvm_para_has_feature(KVM_FEATURE_POLL_CONTROL)) {
|
2019-10-23 19:16:21 +08:00
|
|
|
pr_err_once("host does not support poll control\n");
|
|
|
|
pr_err_once("host upgrade recommended\n");
|
2019-07-04 07:51:29 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Enable guest halt poll disables host halt poll */
|
2019-09-02 18:40:31 +08:00
|
|
|
smp_call_function_single(cpu, kvm_disable_host_haltpoll, NULL, 1);
|
2019-07-04 07:51:29 +08:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(arch_haltpoll_enable);
|
|
|
|
|
2019-09-02 18:40:31 +08:00
|
|
|
void arch_haltpoll_disable(unsigned int cpu)
|
2019-07-04 07:51:29 +08:00
|
|
|
{
|
|
|
|
if (!kvm_para_has_feature(KVM_FEATURE_POLL_CONTROL))
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* Enable guest halt poll disables host halt poll */
|
2019-09-02 18:40:31 +08:00
|
|
|
smp_call_function_single(cpu, kvm_enable_host_haltpoll, NULL, 1);
|
2019-07-04 07:51:29 +08:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(arch_haltpoll_disable);
|
|
|
|
#endif
|