kvm/x86 : add coalesced pio support

Coalesced pio is based on coalesced mmio and can be used for some port
like rtc port, pci-host config port and so on.

Specially in case of rtc as coalesced pio, some versions of windows guest
access rtc frequently because of rtc as system tick. guest access rtc like
this: write register index to 0x70, then write or read data from 0x71.
writing 0x70 port is just as index and do nothing else. So we can use
coalesced pio to handle this scene to reduce VM-EXIT time.

When starting and closing a virtual machine, it will access pci-host config
port frequently. So setting these port as coalesced pio can reduce startup
and shutdown time.

without my patch, get the vm-exit time of accessing rtc 0x70 and piix 0xcf8
using perf tools: (guest OS : windows 7 64bit)
IO Port Access  Samples Samples%  Time%  Min Time  Max Time  Avg time
0x70:POUT        86     30.99%    74.59%   9us      29us    10.75us (+- 3.41%)
0xcf8:POUT     1119     2.60%     2.12%   2.79us    56.83us 3.41us (+- 2.23%)

with my patch
IO Port Access  Samples Samples%  Time%   Min Time  Max Time   Avg time
0x70:POUT       106    32.02%    29.47%    0us      10us     1.57us (+- 7.38%)
0xcf8:POUT      1065    1.67%     0.28%   0.41us    65.44us   0.66us (+- 10.55%)

Signed-off-by: Peng Hao <peng.hao2@zte.com.cn>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
This commit is contained in:
Peng Hao 2018-10-14 07:09:55 +08:00 committed by Paolo Bonzini
parent 9943450b7b
commit 0804c849f1
4 changed files with 30 additions and 11 deletions

View File

@ -3683,27 +3683,31 @@ the definition of struct kvm_nested_state, see KVM_GET_NESTED_STATE.
4.116 KVM_(UN)REGISTER_COALESCED_MMIO 4.116 KVM_(UN)REGISTER_COALESCED_MMIO
Capability: KVM_CAP_COALESCED_MMIO Capability: KVM_CAP_COALESCED_MMIO (for coalesced mmio)
KVM_CAP_COALESCED_PIO (for coalesced pio)
Architectures: all Architectures: all
Type: vm ioctl Type: vm ioctl
Parameters: struct kvm_coalesced_mmio_zone Parameters: struct kvm_coalesced_mmio_zone
Returns: 0 on success, < 0 on error Returns: 0 on success, < 0 on error
Coalesced mmio is a performance optimization that defers hardware Coalesced I/O is a performance optimization that defers hardware
register write emulation so that userspace exits are avoided. It is register write emulation so that userspace exits are avoided. It is
typically used to reduce the overhead of emulating frequently accessed typically used to reduce the overhead of emulating frequently accessed
hardware registers. hardware registers.
When a hardware register is configured for coalesced mmio, write accesses When a hardware register is configured for coalesced I/O, write accesses
do not exit to userspace and their value is recorded in a ring buffer do not exit to userspace and their value is recorded in a ring buffer
that is shared between kernel and userspace. that is shared between kernel and userspace.
Coalesced mmio is used if one or more write accesses to a hardware Coalesced I/O is used if one or more write accesses to a hardware
register can be deferred until a read or a write to another hardware register can be deferred until a read or a write to another hardware
register on the same device. This last access will cause a vmexit and register on the same device. This last access will cause a vmexit and
userspace will process accesses from the ring buffer before emulating userspace will process accesses from the ring buffer before emulating
it. That will avoid exiting to userspace on repeated writes to the it. That will avoid exiting to userspace on repeated writes.
first register.
Coalesced pio is based on coalesced mmio. There is little difference
between coalesced mmio and pio except that coalesced pio records accesses
to I/O ports.
5. The kvm_run structure 5. The kvm_run structure
------------------------ ------------------------

View File

@ -420,13 +420,19 @@ struct kvm_run {
struct kvm_coalesced_mmio_zone { struct kvm_coalesced_mmio_zone {
__u64 addr; __u64 addr;
__u32 size; __u32 size;
__u32 pad; union {
__u32 pad;
__u32 pio;
};
}; };
struct kvm_coalesced_mmio { struct kvm_coalesced_mmio {
__u64 phys_addr; __u64 phys_addr;
__u32 len; __u32 len;
__u32 pad; union {
__u32 pad;
__u32 pio;
};
__u8 data[8]; __u8 data[8];
}; };
@ -956,6 +962,7 @@ struct kvm_ppc_resize_hpt {
#define KVM_CAP_MSR_PLATFORM_INFO 159 #define KVM_CAP_MSR_PLATFORM_INFO 159
#define KVM_CAP_PPC_NESTED_HV 160 #define KVM_CAP_PPC_NESTED_HV 160
#define KVM_CAP_HYPERV_SEND_IPI 161 #define KVM_CAP_HYPERV_SEND_IPI 161
#define KVM_CAP_COALESCED_PIO 162
#ifdef KVM_CAP_IRQ_ROUTING #ifdef KVM_CAP_IRQ_ROUTING

View File

@ -83,6 +83,7 @@ static int coalesced_mmio_write(struct kvm_vcpu *vcpu,
ring->coalesced_mmio[ring->last].phys_addr = addr; ring->coalesced_mmio[ring->last].phys_addr = addr;
ring->coalesced_mmio[ring->last].len = len; ring->coalesced_mmio[ring->last].len = len;
memcpy(ring->coalesced_mmio[ring->last].data, val, len); memcpy(ring->coalesced_mmio[ring->last].data, val, len);
ring->coalesced_mmio[ring->last].pio = dev->zone.pio;
smp_wmb(); smp_wmb();
ring->last = (ring->last + 1) % KVM_COALESCED_MMIO_MAX; ring->last = (ring->last + 1) % KVM_COALESCED_MMIO_MAX;
spin_unlock(&dev->kvm->ring_lock); spin_unlock(&dev->kvm->ring_lock);
@ -140,6 +141,9 @@ int kvm_vm_ioctl_register_coalesced_mmio(struct kvm *kvm,
int ret; int ret;
struct kvm_coalesced_mmio_dev *dev; struct kvm_coalesced_mmio_dev *dev;
if (zone->pio != 1 && zone->pio != 0)
return -EINVAL;
dev = kzalloc(sizeof(struct kvm_coalesced_mmio_dev), GFP_KERNEL); dev = kzalloc(sizeof(struct kvm_coalesced_mmio_dev), GFP_KERNEL);
if (!dev) if (!dev)
return -ENOMEM; return -ENOMEM;
@ -149,8 +153,9 @@ int kvm_vm_ioctl_register_coalesced_mmio(struct kvm *kvm,
dev->zone = *zone; dev->zone = *zone;
mutex_lock(&kvm->slots_lock); mutex_lock(&kvm->slots_lock);
ret = kvm_io_bus_register_dev(kvm, KVM_MMIO_BUS, zone->addr, ret = kvm_io_bus_register_dev(kvm,
zone->size, &dev->dev); zone->pio ? KVM_PIO_BUS : KVM_MMIO_BUS,
zone->addr, zone->size, &dev->dev);
if (ret < 0) if (ret < 0)
goto out_free_dev; goto out_free_dev;
list_add_tail(&dev->list, &kvm->coalesced_zones); list_add_tail(&dev->list, &kvm->coalesced_zones);
@ -174,7 +179,8 @@ int kvm_vm_ioctl_unregister_coalesced_mmio(struct kvm *kvm,
list_for_each_entry_safe(dev, tmp, &kvm->coalesced_zones, list) list_for_each_entry_safe(dev, tmp, &kvm->coalesced_zones, list)
if (coalesced_mmio_in_range(dev, zone->addr, zone->size)) { if (coalesced_mmio_in_range(dev, zone->addr, zone->size)) {
kvm_io_bus_unregister_dev(kvm, KVM_MMIO_BUS, &dev->dev); kvm_io_bus_unregister_dev(kvm,
zone->pio ? KVM_PIO_BUS : KVM_MMIO_BUS, &dev->dev);
kvm_iodevice_destructor(&dev->dev); kvm_iodevice_destructor(&dev->dev);
} }

View File

@ -2949,6 +2949,8 @@ static long kvm_vm_ioctl_check_extension_generic(struct kvm *kvm, long arg)
#ifdef CONFIG_KVM_MMIO #ifdef CONFIG_KVM_MMIO
case KVM_CAP_COALESCED_MMIO: case KVM_CAP_COALESCED_MMIO:
return KVM_COALESCED_MMIO_PAGE_OFFSET; return KVM_COALESCED_MMIO_PAGE_OFFSET;
case KVM_CAP_COALESCED_PIO:
return 1;
#endif #endif
#ifdef CONFIG_HAVE_KVM_IRQ_ROUTING #ifdef CONFIG_HAVE_KVM_IRQ_ROUTING
case KVM_CAP_IRQ_ROUTING: case KVM_CAP_IRQ_ROUTING: