KVM: Allow userspace to specify memory to be used for private regions.
Subsystems (9)
PATCH 1/1 KVM: Allow userspace to specify memory to be used for private regions.
The motivation for this patch is to fix a 20KB leak of memory in vmx.c when a VM is created and destroyed.
On x86/vmx platforms KVM needs 5 pages of userspace memory per VM for architecture specific reasons. It currently allocates the pages on behalf of user space, but has no way of cleanly freeing that memory while the user space process is still running. For user space processes that want more control over that memory, this patch allows user space to provide the memory that KVM uses.
--- Documentation/virtual/kvm/api.txt | 8 +++++++ arch/arm/kvm/arm.c | 6 +++++ arch/ia64/kvm/kvm-ia64.c | 6 +++++ arch/powerpc/kvm/powerpc.c | 6 +++++ arch/s390/kvm/kvm-s390.c | 6 +++++ arch/x86/include/asm/kvm_host.h | 7 ++++++ arch/x86/kvm/svm.c | 8 +++++++ arch/x86/kvm/vmx.c | 47 ++++++++++++++++++++++++++++++++++--- arch/x86/kvm/x86.c | 12 ++++++++-- include/linux/kvm_host.h | 2 ++ virt/kvm/kvm_main.c | 2 ++ 11 files changed, 105 insertions(+), 5 deletions(-)
Documentation/virtual/kvm/api.txt +8 -0
@@ -879,6 +879,14 @@ It is recommended that the lower 21 bits of guest_phys_addr and userspace_addr be identical. This allows large pages in the guest to be backed by large pages in the host. +On x86/vmx architectures KVM needs 5 pages of user space memory for architecture +specific reasons. Calling this ioctl with the special memslot +KVM_PRIVATE_MEMORY_MEMSLOT will tell kvm which user space memory to use for +that memory. If that memslot is not set before creating VCPUs for a VM then +kvm will allocate the memory on behalf of user space, but userspace will not +be able to free that memory. User space should treat this memory as opaque +and not modify it. + The flags field supports two flags: KVM_MEM_LOG_DIRTY_PAGES and KVM_MEM_READONLY. The former can be set to instruct KVM to keep track of writes to memory within the slot. See KVM_GET_DIRTY_LOG ioctl to know how to
arch/arm/kvm/arm.c +6 -0
@@ -228,6 +228,12 @@ int kvm_arch_set_memory_region(struct kvm *kvm,return 0 ; } +int kvm_arch_set_private_memory (struct kvm *kvm, +struct kvm_userspace_memory_region *mem) +{ +return 0 ; +} + int kvm_arch_prepare_memory_region(struct kvm *kvm, struct kvm_memory_slot *memslot, struct kvm_memory_slot old,
arch/ia64/kvm/kvm-ia64.c +6 -0
@@ -1576,6 +1576,12 @@ int kvm_arch_create_memslot(struct kvm_memory_slot *slot, unsigned long npages)return 0 ; } +int kvm_arch_set_private_memory (struct kvm *kvm, +struct kvm_userspace_memory_region *mem) +{ +return 0 ; +} + int kvm_arch_prepare_memory_region(struct kvm *kvm, struct kvm_memory_slot *memslot, struct kvm_memory_slot old,
arch/powerpc/kvm/powerpc.c +6 -0
@@ -410,6 +410,12 @@ int kvm_arch_create_memslot(struct kvm_memory_slot *slot, unsigned long npages)return kvmppc_core_create_memslot (slot, npages); } +int kvm_arch_set_private_memory (struct kvm *kvm, +struct kvm_userspace_memory_region *mem) +{ +return 0 ; +} + int kvm_arch_prepare_memory_region(struct kvm *kvm, struct kvm_memory_slot *memslot, struct kvm_memory_slot old,
arch/s390/kvm/kvm-s390.c +6 -0
@@ -971,6 +971,12 @@ int kvm_arch_create_memslot(struct kvm_memory_slot *slot, unsigned long npages)return 0 ; } +int kvm_arch_set_private_memory (struct kvm *kvm, +struct kvm_userspace_memory_region *mem) +{ +return 0 ; +} +/* Section: memory related */ int kvm_arch_prepare_memory_region(struct kvm *kvm, struct kvm_memory_slot *memslot,
arch/x86/include/asm/kvm_host.h +7 -0
@@ -37,6 +37,7 @@/* memory slots that are not exposed to userspace */ #define KVM_PRIVATE_MEM_SLOTS 3#define KVM_MEM_SLOTS_NUM (KVM_USER_MEM_SLOTS + KVM_PRIVATE_MEM_SLOTS) +#define KVM_PRIVATE_MEMORY_MEMSLOT 0x80000001#define KVM_MMIO_SIZE 16
@@ -553,6 +554,9 @@ struct kvm_arch {struct page *ept_identity_pagetable;bool ept_identity_pagetable_done;gpa_t ept_identity_map_addr; +unsigned long ept_ptr; +unsigned long apic_ptr; +unsigned long tss_ptr;unsigned long irq_sources_bitmap;s64 kvmclock_offset;
@@ -640,6 +644,9 @@ struct kvm_x86_ops {bool (*cpu_has_accelerated_tpr)(void );void (*cpuid_update)(struct kvm_vcpu *vcpu); +int (*set_private_memory)(struct kvm *kvm, +struct kvm_userspace_memory_region *mem); +/* Create, but do not attach this VCPU */ struct kvm_vcpu *(*vcpu_create)(struct kvm *kvm,unsigned id);void (*vcpu_free)(struct kvm_vcpu *vcpu);
arch/x86/kvm/svm.c +8 -0
@@ -1211,6 +1211,12 @@ static int svm_vcpu_reset(struct kvm_vcpu *vcpu)return 0 ; } +static int svm_set_private_memory (struct kvm *kvm, +struct kvm_userspace_memory_region *mem) +{ +return 0 ; +} +static struct kvm_vcpu *svm_create_vcpu (struct kvm *kvm,unsigned int id) {struct vcpu_svm *svm;
@@ -4257,6 +4263,8 @@ static struct kvm_x86_ops svm_x86_ops = { .hardware_disable = svm_hardware_disable, .cpu_has_accelerated_tpr = svm_cpu_has_accelerated_tpr, + .set_private_memory = svm_set_private_memory, + .vcpu_create = svm_create_vcpu, .vcpu_free = svm_free_vcpu, .vcpu_reset = svm_vcpu_reset,
arch/x86/kvm/vmx.c +44 -3
77 lines not on this page · raw message
arch/x86/kvm/x86.c +10 -2
@@ -6809,6 +6809,12 @@ void kvm_arch_sync_events(struct kvm *kvm)kvm_free_pit (kvm); } +int kvm_arch_set_private_memory (struct kvm *kvm, +struct kvm_userspace_memory_region *mem) +{ +return kvm_x86_ops->set_private_memory (kvm, mem); +} +void kvm_arch_destroy_vm (struct kvm *kvm) {kvm_iommu_unmap_guest (kvm);
@@ -6913,7 +6919,8 @@ int kvm_arch_prepare_memory_region(struct kvm *kvm,* Only private memory slots need to be mapped here since * KVM_SET_MEMORY_REGION ioctl is no longer supported. */ -if ((memslot->id >=KVM_USER_MEM_SLOTS ) && npages && !old.npages) { +if ((memslot->id >=KVM_USER_MEM_SLOTS ) && npages && !old.npages && + !user_alloc) {unsigned long userspace_addr;/*
@@ -6941,7 +6948,8 @@ void kvm_arch_commit_memory_region(struct kvm *kvm,int nr_mmu_pages =0 , npages = mem->memory_size >>PAGE_SHIFT ; -if ((mem->slot >=KVM_USER_MEM_SLOTS ) && old.npages && !npages) { +if ((mem->slot >=KVM_USER_MEM_SLOTS ) && old.npages && !npages && + !user_alloc) {int ret; ret = vm_munmap(old.userspace_addr,
include/linux/kvm_host.h +2 -0
@@ -461,6 +461,8 @@ int __kvm_set_memory_region(struct kvm *kvm,void kvm_arch_free_memslot (struct kvm_memory_slot *free,struct kvm_memory_slot *dont);int kvm_arch_create_memslot (struct kvm_memory_slot *slot,unsigned long npages); +int kvm_arch_set_private_memory (struct kvm *kvm, +struct kvm_userspace_memory_region *mem); int kvm_arch_prepare_memory_region(struct kvm *kvm, struct kvm_memory_slot *memslot, struct kvm_memory_slot old,
virt/kvm/kvm_main.c +2 -0
@@ -949,6 +949,8 @@ int kvm_vm_ioctl_set_memory_region(struct kvm *kvm, kvm_userspace_memory_region *mem, bool user_alloc) { +if (mem->slot ==KVM_PRIVATE_MEMORY_MEMSLOT ) +return kvm_arch_set_private_memory (kvm, mem);if (mem->slot >=KVM_USER_MEM_SLOTS )return -EINVAL ;return kvm_set_memory_region (kvm, mem, user_alloc);
Signature · 1 line
1.7.10.4
Paolo Bonzini
maintainerIl 16/04/2013 00:10, Andrew Honig ha scritto:
Quoted 10 lines
The motivation for this patch is to fix a 20KB leak of memory in
vmx.cwhen a VM is created and destroyed.On x86/vmx platforms KVM needs 5 pages of userspace memory per VM for architecture specific reasons. It currently allocates the pages on behalf of user space, but has no way of cleanly freeing that memory while the user space process is still running. For user space processes that want more control over that memory, this patch allows user space to provide the memory that KVM uses.
Isn't the bug simply that kvm_arch_free_memslot needs to free the memory that was allocated in kvm_arch_prepare_memory_region? Then closing /dev/kvm will fix it.
The conditions for doing vm_mmap and vm_munmap in kvm_arch_prepare_memory_region/kvm_arch_commit_memory_region perhaps can also be simplified.
The vm_munmamp in kvm_arch_commit_memory_region probably can go completely, the test on kvm_arch_prepare_memory_region simplified to npages != old.npages, and kvm_arch_free_memslot can check something like "if (free->user_alloc && (!dont || !dont->user_alloc || free->userspace_addr != dont->userspace_addr))".
Paolo
Quoted 310 lines
Signed-off-by Andrew Honig <[email protected]>--- Documentation/virtual/kvm/api.txt | 8 +++++++ arch/arm/kvm/arm.c | 6 +++++ arch/ia64/kvm/kvm-ia64.c | 6 +++++ arch/powerpc/kvm/powerpc.c | 6 +++++ arch/s390/kvm/kvm-s390.c | 6 +++++ arch/x86/include/asm/kvm_host.h | 7 ++++++ arch/x86/kvm/svm.c | 8 +++++++ arch/x86/kvm/vmx.c | 47 ++++++++++++++++++++++++++++++++++--- arch/x86/kvm/x86.c | 12 ++++++++-- include/linux/kvm_host.h | 2 ++ virt/kvm/kvm_main.c | 2 ++ 11 files changed, 105 insertions(+), 5 deletions(-)Documentation/virtual/kvm/api.txt +8
-0@@ -879,6 +879,14 @@ It is recommended that the lower 21 bits of guest_phys_addr and userspace_addr be identical. This allows large pages in the guest to be backed by large pages in the host. +On x86/vmx architectures KVM needs 5 pages of user space memory for architecture +specific reasons. Calling this ioctl with the special memslot +KVM_PRIVATE_MEMORY_MEMSLOT will tell kvm which user space memory to use for +that memory. If that memslot is not set before creating VCPUs for a VM then +kvm will allocate the memory on behalf of user space, but userspace will not +be able to free that memory. User space should treat this memory as opaque +and not modify it. + The flags field supports two flags: KVM_MEM_LOG_DIRTY_PAGES and KVM_MEM_READONLY. The former can be set to instruct KVM to keep track of writes to memory within the slot. See KVM_GET_DIRTY_LOG ioctl to know how toarch/arm/kvm/arm.c +6
-0@@ -228,6 +228,12 @@ int kvm_arch_set_memory_region(struct kvm *kvm,return 0 ; } +int kvm_arch_set_private_memory (struct kvm *kvm, +struct kvm_userspace_memory_region *mem) +{ +return 0 ; +} + int kvm_arch_prepare_memory_region(struct kvm *kvm, struct kvm_memory_slot *memslot, struct kvm_memory_slot old,arch/ia64/kvm/kvm-ia64.c +6
-0@@ -1576,6 +1576,12 @@ int kvm_arch_create_memslot(struct kvm_memory_slot *slot, unsigned long npages)return 0 ; } +int kvm_arch_set_private_memory (struct kvm *kvm, +struct kvm_userspace_memory_region *mem) +{ +return 0 ; +} + int kvm_arch_prepare_memory_region(struct kvm *kvm, struct kvm_memory_slot *memslot, struct kvm_memory_slot old,arch/powerpc/kvm/powerpc.c +6
-0@@ -410,6 +410,12 @@ int kvm_arch_create_memslot(struct kvm_memory_slot *slot, unsigned long npages)return kvmppc_core_create_memslot (slot, npages); } +int kvm_arch_set_private_memory (struct kvm *kvm, +struct kvm_userspace_memory_region *mem) +{ +return 0 ; +} + int kvm_arch_prepare_memory_region(struct kvm *kvm, struct kvm_memory_slot *memslot, struct kvm_memory_slot old,arch/s390/kvm/kvm-s390.c +6
-0@@ -971,6 +971,12 @@ int kvm_arch_create_memslot(struct kvm_memory_slot *slot, unsigned long npages)return 0 ; } +int kvm_arch_set_private_memory (struct kvm *kvm, +struct kvm_userspace_memory_region *mem) +{ +return 0 ; +} +/* Section: memory related */ int kvm_arch_prepare_memory_region(struct kvm *kvm, struct kvm_memory_slot *memslot,arch/x86/include/asm/kvm_host.h +7
-0@@ -37,6 +37,7 @@/* memory slots that are not exposed to userspace */ #define KVM_PRIVATE_MEM_SLOTS 3#define KVM_MEM_SLOTS_NUM (KVM_USER_MEM_SLOTS + KVM_PRIVATE_MEM_SLOTS) +#define KVM_PRIVATE_MEMORY_MEMSLOT 0x80000001#define KVM_MMIO_SIZE 16@@ -553,6 +554,9 @@ struct kvm_arch {struct page *ept_identity_pagetable;bool ept_identity_pagetable_done;gpa_t ept_identity_map_addr; +unsigned long ept_ptr; +unsigned long apic_ptr; +unsigned long tss_ptr;unsigned long irq_sourcesq_bitmap;s64 kvmclock_offset;@@ -640,6 +644,9 @@ struct kvm_x86_ops {bool (*cpu_has_accelerated_tpr)(void );void (*cpuid_update)(struct kvm_vcpu *vcpu); +int (*set_private_memory)(struct kvm *kvm, +struct kvm_userspace_memory_region *mem); +/* Create, but do not attach this VCPU */ struct kvm_vcpu *(*vcpu_create)(struct kvm *kvm,unsigned id);void (*vcpu_free)(struct kvm_vcpu *vcpu);arch/x86/kvm/svm.c +8
-0@@ -1211,6 +1211,12 @@ static int svm_vcpu_reset(struct kvm_vcpu *vcpu)return 0 ; } +static int svm_set_private_memory (struct kvm *kvm, +struct kvm_userspace_memory_region *mem) +{ +return 0 ; +} +static struct kvm_vcpu *svm_create_vcpu (struct kvm *kvm,unsigned int id) {struct vcpu_svm *svm;@@ -4257,6 +4263,8 @@ static struct kvm_x86_ops svm_x86_ops = { .hardware_disable = svm_hardware_disable, .cpu_has_accelerated_tpr = svm_cpu_has_accelerated_tpr, + .set_private_memory = svm_set_private_memory, + .vcpu_create = svm_create_vcpu, .vcpu_free = svm_free_vcpu, .vcpu_reset = svm_vcpu_reset,arch/x86/kvm/vmx.c +44
-3@@ -3692,7 +3692,13 @@ static int alloc_apic_access_page(struct kvm *kvm) kvm_userspace_mem.flags =0 ; kvm_userspace_mem.guest_phys_addr =0xfee00000ULL ; kvm_userspace_mem.memory_size =PAGE_SIZE ; - r =__kvm_set_memory_region (kvm, &kvm_userspace_mem, false); +if (kvm->arch.apic_ptr) { + kvm_userspace_mem.userspace_addr = kvm->arch.apic_ptr; + r =__kvm_set_memory_region (kvm, &kvm_userspace_mem, true); + }else { + r =__kvm_set_memory_region (kvm, &kvm_userspace_mem, false); + } +if (r) goto out;@@ -3722,7 +3728,13 @@ static int alloc_identity_pagetable(struct kvm *kvm) kvm_userspace_mem.guest_phys_addr = kvm->arch.ept_identity_map_addr; kvm_userspace_mem.memory_size =PAGE_SIZE ; - r =__kvm_set_memory_region (kvm, &kvm_userspace_mem, false); +if (kvm->arch.ept_ptr) { + kvm_userspace_mem.userspace_addr = kvm->arch.ept_ptr; + r =__kvm_set_memory_region (kvm, &kvm_userspace_mem, true); + }else { + r =__kvm_set_memory_region (kvm, &kvm_userspace_mem, false); + } +if (r) goto out;@@ -4362,7 +4374,13 @@ static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr) .flags = 0, }; - ret =kvm_set_memory_region (kvm, &tss_mem, false); +if (kvm->arch.tss_ptr) { + tss_mem.userspace_addr = kvm->arch.tss_ptr; + ret =kvm_set_memory_region (kvm, &tss_mem, true); + }else { + ret =kvm_set_memory_region (kvm, &tss_mem, false); + } +if (ret)return ret; kvm->arch.tss_addr = addr;@@ -6683,6 +6701,27 @@ static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)vmx_complete_interrupts (vmx); } +static int vmx_set_private_memory (struct kvm *kvm, +struct kvm_userspace_memory_region *mem) +{ +/* +* Early sanity checking so userspace gets an error message during +* memory setup and not when trying to use this memory. +* Checks to see if the memory is valid are performed later when +* the memory is used. +*/ +if (!mem->userspace_addr || mem->userspace_addr & (PAGE_SIZE -1 ) || + mem->memory_size & (PAGE_SIZE -1 ) || + mem->memory_size <PAGE_SIZE *5 ) +return -EINVAL ; + + kvm->arch.ept_ptr = mem->userspace_addr; + kvm->arch.apic_ptr = mem->userspace_addr +PAGE_SIZE ; + kvm->arch.tss_ptr = mem->userspace_addr +PAGE_SIZE *2 ; + +return 0 ; +} +static void vmx_free_vcpu (struct kvm_vcpu *vcpu) {struct vcpu_vmx *vmx =to_vmx (vcpu);@@ -7532,6 +7571,8 @@ static struct kvm_x86_ops vmx_x86_ops = { .hardware_disable = hardware_disable, .cpu_has_accelerated_tpr = report_flexpriority, + .set_private_memory = vmx_set_private_memory, + .vcpu_create = vmx_create_vcpu, .vcpu_free = vmx_free_vcpu, .vcpu_reset = vmx_vcpu_reset,arch/x86/kvm/x86.c +10
-2@@ -6809,6 +6809,12 @@ void kvm_arch_sync_events(struct kvm *kvm)kvm_free_pit (kvm); } +int kvm_arch_set_private_memory (struct kvm *kvm, +struct kvm_userspace_memory_region *mem) +{ +return kvm_x86_ops->set_private_memory (kvm, mem); +} +void kvm_arch_destroy_vm (struct kvm *kvm) {kvm_iommu_unmap_guest (kvm);@@ -6913,7 +6919,8 @@ int kvm_arch_prepare_memory_region(struct kvm *kvm,* Only private memory slots need to be mapped here since * KVM_SET_MEMORY_REGION ioctl is no longer supported. */ -if ((memslot->id >=KVM_USER_MEM_SLOTS ) && npages && !old.npages) { +if ((memslot->id >=KVM_USER_MEM_SLOTS ) && npages && !old.npages && + !user_alloc) {unsigned long userspace_addr;/* @@ -6941,7 +6948,8 @@ void kvm_arch_commit_memory_region(struct kvm *kvm,int nr_mmu_pages =0 , npages = mem->memory_size >>PAGE_SHIFT ; -if ((mem->slot >=KVM_USER_MEM_SLOTS ) && old.npages && !npages) { +if ((mem->slot >=KVM_USER_MEM_SLOTS ) && old.npages && !npages && + !user_alloc) {int ret; ret = vm_munmap(old.userspace_addr,include/linux/kvm_host.h +2
-0@@ -461,6 +461,8 @@ int __kvm_set_memory_region(struct kvm *kvm,void kvm_arch_free_memslot (struct kvm_memory_slot *free,struct kvm_memory_slot *dont);int kvm_arch_create_memslot (struct kvm_memory_slot *slot,unsigned long npages); +int kvm_arch_set_private_memory (struct kvm *kvm, +struct kvm_userspace_memory_region *mem); int kvm_arch_prepare_memory_region(struct kvm *kvm, struct kvm_memory_slot *memslot, struct kvm_memory_slot old,virt/kvm/kvm_main.c +2
-0@@ -949,6 +949,8 @@ int kvm_vm_ioctl_set_memory_region(struct kvm *kvm, kvm_userspace_memory_region *mem, bool user_alloc) { +if (mem->slot ==KVM_PRIVATE_MEMORY_MEMSLOT ) +return kvm_arch_set_private_memory (kvm, mem);if (mem->slot >=KVM_USER_MEM_SLOTS )return -EINVAL ;return kvm_set_memory_region (kvm, mem, user_alloc);
Quoted 4 lines · Andrew Honig
The motivation for this patch is to fix a 20KB leak of memory in
vmx.cwhen a VM is created and destroyed.
Quoted 8 lines
On x86/vmx platforms KVM needs 5 pages of userspace memory per VM for architecture specific reasons. It currently allocates the pages on behalf of user space, but has no way of cleanly freeing that memory while the user space process is still running. For user space processes that want more control over that memory, this patch allows user space to provide the memory that KVM uses.
Signed-off-by Andrew Honig <[email protected]>
I thought we agreed on not adding new API and using __kvm_set_memory_region() to unregister private memory regions.
Quoted 315 lines
--- Documentation/virtual/kvm/api.txt | 8 +++++++ arch/arm/kvm/arm.c | 6 +++++ arch/ia64/kvm/kvm-ia64.c | 6 +++++ arch/powerpc/kvm/powerpc.c | 6 +++++ arch/s390/kvm/kvm-s390.c | 6 +++++ arch/x86/include/asm/kvm_host.h | 7 ++++++ arch/x86/kvm/svm.c | 8 +++++++ arch/x86/kvm/vmx.c | 47 ++++++++++++++++++++++++++++++++++--- arch/x86/kvm/x86.c | 12 ++++++++-- include/linux/kvm_host.h | 2 ++ virt/kvm/kvm_main.c | 2 ++ 11 files changed, 105 insertions(+), 5 deletions(-)Documentation/virtual/kvm/api.txt +8
-0@@ -879,6 +879,14 @@ It is recommended that the lower 21 bits of guest_phys_addr and userspace_addr be identical. This allows large pages in the guest to be backed by large pages in the host. +On x86/vmx architectures KVM needs 5 pages of user space memory for architecture +specific reasons. Calling this ioctl with the special memslot +KVM_PRIVATE_MEMORY_MEMSLOT will tell kvm which user space memory to use for +that memory. If that memslot is not set before creating VCPUs for a VM then +kvm will allocate the memory on behalf of user space, but userspace will not +be able to free that memory. User space should treat this memory as opaque +and not modify it. + The flags field supports two flags: KVM_MEM_LOG_DIRTY_PAGES and KVM_MEM_READONLY. The former can be set to instruct KVM to keep track of writes to memory within the slot. See KVM_GET_DIRTY_LOG ioctl to know how toarch/arm/kvm/arm.c +6
-0@@ -228,6 +228,12 @@ int kvm_arch_set_memory_region(struct kvm *kvm,return 0 ; } +int kvm_arch_set_private_memory (struct kvm *kvm, +struct kvm_userspace_memory_region *mem) +{ +return 0 ; +} + int kvm_arch_prepare_memory_region(struct kvm *kvm, struct kvm_memory_slot *memslot, struct kvm_memory_slot old,arch/ia64/kvm/kvm-ia64.c +6
-0@@ -1576,6 +1576,12 @@ int kvm_arch_create_memslot(struct kvm_memory_slot *slot, unsigned long npages)return 0 ; } +int kvm_arch_set_private_memory (struct kvm *kvm, +struct kvm_userspace_memory_region *mem) +{ +return 0 ; +} + int kvm_arch_prepare_memory_region(struct kvm *kvm, struct kvm_memory_slot *memslot, struct kvm_memory_slot old,arch/powerpc/kvm/powerpc.c +6
-0@@ -410,6 +410,12 @@ int kvm_arch_create_memslot(struct kvm_memory_slot *slot, unsigned long npages)return kvmppc_core_create_memslot (slot, npages); } +int kvm_arch_set_private_memory (struct kvm *kvm, +struct kvm_userspace_memory_region *mem) +{ +return 0 ; +} + int kvm_arch_prepare_memory_region(struct kvm *kvm, struct kvm_memory_slot *memslot, struct kvm_memory_slot old,arch/s390/kvm/kvm-s390.c +6
-0@@ -971,6 +971,12 @@ int kvm_arch_create_memslot(struct kvm_memory_slot *slot, unsigned long npages)return 0 ; } +int kvm_arch_set_private_memory (struct kvm *kvm, +struct kvm_userspace_memory_region *mem) +{ +return 0 ; +} +/* Section: memory related */ int kvm_arch_prepare_memory_region(struct kvm *kvm, struct kvm_memory_slot *memslot,arch/x86/include/asm/kvm_host.h +7
-0@@ -37,6 +37,7 @@/* memory slots that are not exposed to userspace */ #define KVM_PRIVATE_MEM_SLOTS 3#define KVM_MEM_SLOTS_NUM (KVM_USER_MEM_SLOTS + KVM_PRIVATE_MEM_SLOTS) +#define KVM_PRIVATE_MEMORY_MEMSLOT 0x80000001#define KVM_MMIO_SIZE 16@@ -553,6 +554,9 @@ struct kvm_arch {struct page *ept_identity_pagetable;bool ept_identity_pagetable_done;gpa_t ept_identity_map_addr; +unsigned long ept_ptr; +unsigned long apic_ptr; +unsigned long tss_ptr;unsigned long irq_sources_bitmap;s64 kvmclock_offset;@@ -640,6 +644,9 @@ struct kvm_x86_ops {bool (*cpu_has_accelerated_tpr)(void );void (*cpuid_update)(struct kvm_vcpu *vcpu); +int (*set_private_memory)(struct kvm *kvm, +struct kvm_userspace_memory_region *mem); +/* Create, but do not attach this VCPU */ struct kvm_vcpu *(*vcpu_create)(struct kvm *kvm,unsigned id);void (*vcpu_free)(struct kvm_vcpu *vcpu);arch/x86/kvm/svm.c +8
-0@@ -1211,6 +1211,12 @@ static int svm_vcpu_reset(struct kvm_vcpu *vcpu)return 0 ; } +static int svm_set_private_memory (struct kvm *kvm, +struct kvm_userspace_memory_region *mem) +{ +return 0 ; +} +static struct kvm_vcpu *svm_create_vcpu (struct kvm *kvm,unsigned int id) {struct vcpu_svm *svm;@@ -4257,6 +4263,8 @@ static struct kvm_x86_ops svm_x86_ops = { .hardware_disable = svm_hardware_disable, .cpu_has_accelerated_tpr = svm_cpu_has_accelerated_tpr, + .set_private_memory = svm_set_private_memory, + .vcpu_create = svm_create_vcpu, .vcpu_free = svm_free_vcpu, .vcpu_reset = svm_vcpu_reset,arch/x86/kvm/vmx.c +44
-3@@ -3692,7 +3692,13 @@ static int alloc_apic_access_page(struct kvm *kvm) kvm_userspace_mem.flags =0 ; kvm_userspace_mem.guest_phys_addr =0xfee00000ULL ; kvm_userspace_mem.memory_size =PAGE_SIZE ; - r =__kvm_set_memory_region (kvm, &kvm_userspace_mem, false); +if (kvm->arch.apic_ptr) { + kvm_userspace_mem.userspace_addr = kvm->arch.apic_ptr; + r =__kvm_set_memory_region (kvm, &kvm_userspace_mem, true); + }else { + r =__kvm_set_memory_region (kvm, &kvm_userspace_mem, false); + } +if (r) goto out;@@ -3722,7 +3728,13 @@ static int alloc_identity_pagetable(struct kvm *kvm) kvm_userspace_mem.guest_phys_addr = kvm->arch.ept_identity_map_addr; kvm_userspace_mem.memory_size =PAGE_SIZE ; - r =__kvm_set_memory_region (kvm, &kvm_userspace_mem, false); +if (kvm->arch.ept_ptr) { + kvm_userspace_mem.userspace_addr = kvm->arch.ept_ptr; + r =__kvm_set_memory_region (kvm, &kvm_userspace_mem, true); + }else { + r =__kvm_set_memory_region (kvm, &kvm_userspace_mem, false); + } +if (r) goto out;@@ -4362,7 +4374,13 @@ static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr) .flags = 0, }; - ret =kvm_set_memory_region (kvm, &tss_mem, false); +if (kvm->arch.tss_ptr) { + tss_mem.userspace_addr = kvm->arch.tss_ptr; + ret =kvm_set_memory_region (kvm, &tss_mem, true); + }else { + ret =kvm_set_memory_region (kvm, &tss_mem, false); + } +if (ret)return ret; kvm->arch.tss_addr = addr;@@ -6683,6 +6701,27 @@ static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)vmx_complete_interrupts (vmx); } +static int vmx_set_private_memory (struct kvm *kvm, +struct kvm_userspace_memory_region *mem) +{ +/* +* Early sanity checking so userspace gets an error message during +* memory setup and not when trying to use this memory. +* Checks to see if the memory is valid are performed later when +* the memory is used. +*/ +if (!mem->userspace_addr || mem->userspace_addr & (PAGE_SIZE -1 ) || + mem->memory_size & (PAGE_SIZE -1 ) || + mem->memory_size <PAGE_SIZE *5 ) +return -EINVAL ; + + kvm->arch.ept_ptr = mem->userspace_addr; + kvm->arch.apic_ptr = mem->userspace_addr +PAGE_SIZE ; + kvm->arch.tss_ptr = mem->userspace_addr +PAGE_SIZE *2 ; + +return 0 ; +} +static void vmx_free_vcpu (struct kvm_vcpu *vcpu) {struct vcpu_vmx *vmx =to_vmx (vcpu);@@ -7532,6 +7571,8 @@ static struct kvm_x86_ops vmx_x86_ops = { .hardware_disable = hardware_disable, .cpu_has_accelerated_tpr = report_flexpriority, + .set_private_memory = vmx_set_private_memory, + .vcpu_create = vmx_create_vcpu, .vcpu_free = vmx_free_vcpu, .vcpu_reset = vmx_vcpu_reset,arch/x86/kvm/x86.c +10
-2@@ -6809,6 +6809,12 @@ void kvm_arch_sync_events(struct kvm *kvm)kvm_free_pit (kvm); } +int kvm_arch_set_private_memory (struct kvm *kvm, +struct kvm_userspace_memory_region *mem) +{ +return kvm_x86_ops->set_private_memory (kvm, mem); +} +void kvm_arch_destroy_vm (struct kvm *kvm) {kvm_iommu_unmap_guest (kvm);@@ -6913,7 +6919,8 @@ int kvm_arch_prepare_memory_region(struct kvm *kvm,* Only private memory slots need to be mapped here since * KVM_SET_MEMORY_REGION ioctl is no longer supported. */ -if ((memslot->id >=KVM_USER_MEM_SLOTS ) && npages && !old.npages) { +if ((memslot->id >=KVM_USER_MEM_SLOTS ) && npages && !old.npages && + !user_alloc) {unsigned long userspace_addr;/* @@ -6941,7 +6948,8 @@ void kvm_arch_commit_memory_region(struct kvm *kvm,int nr_mmu_pages =0 , npages = mem->memory_size >>PAGE_SHIFT ; -if ((mem->slot >=KVM_USER_MEM_SLOTS ) && old.npages && !npages) { +if ((mem->slot >=KVM_USER_MEM_SLOTS ) && old.npages && !npages && + !user_alloc) {int ret; ret = vm_munmap(old.userspace_addr,include/linux/kvm_host.h +2
-0@@ -461,6 +461,8 @@ int __kvm_set_memory_region(struct kvm *kvm,void kvm_arch_free_memslot (struct kvm_memory_slot *free,struct kvm_memory_slot *dont);int kvm_arch_create_memslot (struct kvm_memory_slot *slot,unsigned long npages); +int kvm_arch_set_private_memory (struct kvm *kvm, +struct kvm_userspace_memory_region *mem); int kvm_arch_prepare_memory_region(struct kvm *kvm, struct kvm_memory_slot *memslot, struct kvm_memory_slot old,virt/kvm/kvm_main.c +2
-0@@ -949,6 +949,8 @@ int kvm_vm_ioctl_set_memory_region(struct kvm *kvm, kvm_userspace_memory_region *mem, bool user_alloc) { +if (mem->slot ==KVM_PRIVATE_MEMORY_MEMSLOT ) +return kvm_arch_set_private_memory (kvm, mem);if (mem->slot >=KVM_USER_MEM_SLOTS )return -EINVAL ;return kvm_set_memory_region (kvm, mem, user_alloc);Signature · 6 lines
1.7.10.4 -- To unsubscribe from this list: send the line "unsubscribe kvm" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
--
Gleb.
Freeing the memory in kvm_arch_free_memslot is as good as anywhere else in KVM. The problem is that this memory is in the user space process mm. This codepath could be called after the mm is destroyed in the case of an process exit without closing the fd, which will result in a panic on vm_munmap when it tries to access the mm. There's also the possibility that another process closes the fd and messing with that processes memory map seems like it's asking for trouble.
Quoted 339 lines · Paolo Bonzini
Il 16/04/2013 00:10, Andrew Honig ha scritto:
Quoted 10 lines
The motivation for this patch is to fix a 20KB leak of memory in
vmx.cwhen a VM is created and destroyed.On x86/vmx platforms KVM needs 5 pages of userspace memory per VM for architecture specific reasons. It currently allocates the pages on behalf of user space, but has no way of cleanly freeing that memory while the user space process is still running. For user space processes that want more control over that memory, this patch allows user space to provide the memory that KVM uses.
Isn't the bug simply that
kvm_arch_free_memslotneeds to free the memory that was allocated inkvm_arch_prepare_memory_region? Then closing/dev/kvmwill fix it.The conditions for doing
vm_mmapandvm_munmapin kvm_arch_prepare_memory_region/kvm_arch_commit_memory_region perhaps can also be simplified.The
vm_munmampinkvm_arch_commit_memory_regionprobably can go completely, the test onkvm_arch_prepare_memory_regionsimplified to npages != old.npages, andkvm_arch_free_memslotcan check something like "if (free->user_alloc&& (!dont || !dont->user_alloc ||free->userspace_addr!=dont->userspace_addr))".Paolo
Quoted 310 lines
Signed-off-by Andrew Honig <[email protected]>--- Documentation/virtual/kvm/api.txt | 8 +++++++ arch/arm/kvm/arm.c | 6 +++++ arch/ia64/kvm/kvm-ia64.c | 6 +++++ arch/powerpc/kvm/powerpc.c | 6 +++++ arch/s390/kvm/kvm-s390.c | 6 +++++ arch/x86/include/asm/kvm_host.h | 7 ++++++ arch/x86/kvm/svm.c | 8 +++++++ arch/x86/kvm/vmx.c | 47 ++++++++++++++++++++++++++++++++++--- arch/x86/kvm/x86.c | 12 ++++++++-- include/linux/kvm_host.h | 2 ++ virt/kvm/kvm_main.c | 2 ++ 11 files changed, 105 insertions(+), 5 deletions(-)Documentation/virtual/kvm/api.txt +8
-0@@ -879,6 +879,14 @@ It is recommended that the lower 21 bits of guest_phys_addr and userspace_addr be identical. This allows large pages in the guest to be backed by large pages in the host. +On x86/vmx architectures KVM needs 5 pages of user space memory for architecture +specific reasons. Calling this ioctl with the special memslot +KVM_PRIVATE_MEMORY_MEMSLOT will tell kvm which user space memory to use for +that memory. If that memslot is not set before creating VCPUs for a VM then +kvm will allocate the memory on behalf of user space, but userspace will not +be able to free that memory. User space should treat this memory as opaque +and not modify it. + The flags field supports two flags: KVM_MEM_LOG_DIRTY_PAGES and KVM_MEM_READONLY. The former can be set to instruct KVM to keep track of writes to memory within the slot. See KVM_GET_DIRTY_LOG ioctl to know how toarch/arm/kvm/arm.c +6
-0@@ -228,6 +228,12 @@ int kvm_arch_set_memory_region(struct kvm *kvm,return 0 ; } +int kvm_arch_set_private_memory (struct kvm *kvm, +struct kvm_userspace_memory_region *mem) +{ +return 0 ; +} + int kvm_arch_prepare_memory_region(struct kvm *kvm, struct kvm_memory_slot *memslot, struct kvm_memory_slot old,arch/ia64/kvm/kvm-ia64.c +6
-0@@ -1576,6 +1576,12 @@ int kvm_arch_create_memslot(struct kvm_memory_slot *slot, unsigned long npages)return 0 ; } +int kvm_arch_set_private_memory (struct kvm *kvm, +struct kvm_userspace_memory_region *mem) +{ +return 0 ; +} + int kvm_arch_prepare_memory_region(struct kvm *kvm, struct kvm_memory_slot *memslot, struct kvm_memory_slot old,arch/powerpc/kvm/powerpc.c +6
-0@@ -410,6 +410,12 @@ int kvm_arch_create_memslot(struct kvm_memory_slot *slot, unsigned long npages)return kvmppc_core_create_memslot (slot, npages); } +int kvm_arch_set_private_memory (struct kvm *kvm, +struct kvm_userspace_memory_region *mem) +{ +return 0 ; +} + int kvm_arch_prepare_memory_region(struct kvm *kvm, struct kvm_memory_slot *memslot, struct kvm_memory_slot old,arch/s390/kvm/kvm-s390.c +6
-0@@ -971,6 +971,12 @@ int kvm_arch_create_memslot(struct kvm_memory_slot *slot, unsigned long npages)return 0 ; } +int kvm_arch_set_private_memory (struct kvm *kvm, +struct kvm_userspace_memory_region *mem) +{ +return 0 ; +} +/* Section: memory related */ int kvm_arch_prepare_memory_region(struct kvm *kvm, struct kvm_memory_slot *memslot,arch/x86/include/asm/kvm_host.h +7
-0@@ -37,6 +37,7 @@/* memory slots that are not exposed to userspace */ #define KVM_PRIVATE_MEM_SLOTS 3#define KVM_MEM_SLOTS_NUM (KVM_USER_MEM_SLOTS + KVM_PRIVATE_MEM_SLOTS) +#define KVM_PRIVATE_MEMORY_MEMSLOT 0x80000001#define KVM_MMIO_SIZE 16@@ -553,6 +554,9 @@ struct kvm_arch {struct page *ept_identity_pagetable;bool ept_identity_pagetable_done;gpa_t ept_identity_map_addr; +unsigned long ept_ptr; +unsigned long apic_ptr; +unsigned long tss_ptr;unsigned long irq_sourcesq_bitmap;s64 kvmclock_offset;@@ -640,6 +644,9 @@ struct kvm_x86_ops {bool (*cpu_has_accelerated_tpr)(void );void (*cpuid_update)(struct kvm_vcpu *vcpu); +int (*set_private_memory)(struct kvm *kvm, +struct kvm_userspace_memory_region *mem); +/* Create, but do not attach this VCPU */ struct kvm_vcpu *(*vcpu_create)(struct kvm *kvm,unsigned id);void (*vcpu_free)(struct kvm_vcpu *vcpu);arch/x86/kvm/svm.c +8
-0@@ -1211,6 +1211,12 @@ static int svm_vcpu_reset(struct kvm_vcpu *vcpu)return 0 ; } +static int svm_set_private_memory (struct kvm *kvm, +struct kvm_userspace_memory_region *mem) +{ +return 0 ; +} +static struct kvm_vcpu *svm_create_vcpu (struct kvm *kvm,unsigned int id) {struct vcpu_svm *svm;@@ -4257,6 +4263,8 @@ static struct kvm_x86_ops svm_x86_ops = { .hardware_disable = svm_hardware_disable, .cpu_has_accelerated_tpr = svm_cpu_has_accelerated_tpr, + .set_private_memory = svm_set_private_memory, + .vcpu_create = svm_create_vcpu, .vcpu_free = svm_free_vcpu, .vcpu_reset = svm_vcpu_reset,arch/x86/kvm/vmx.c +44
-3@@ -3692,7 +3692,13 @@ static int alloc_apic_access_page(struct kvm *kvm) kvm_userspace_mem.flags =0 ; kvm_userspace_mem.guest_phys_addr =0xfee00000ULL ; kvm_userspace_mem.memory_size =PAGE_SIZE ; - r =__kvm_set_memory_region (kvm, &kvm_userspace_mem, false); +if (kvm->arch.apic_ptr) { + kvm_userspace_mem.userspace_addr = kvm->arch.apic_ptr; + r =__kvm_set_memory_region (kvm, &kvm_userspace_mem, true); + }else { + r =__kvm_set_memory_region (kvm, &kvm_userspace_mem, false); + } +if (r) goto out;@@ -3722,7 +3728,13 @@ static int alloc_identity_pagetable(struct kvm *kvm) kvm_userspace_mem.guest_phys_addr = kvm->arch.ept_identity_map_addr; kvm_userspace_mem.memory_size =PAGE_SIZE ; - r =__kvm_set_memory_region (kvm, &kvm_userspace_mem, false); +if (kvm->arch.ept_ptr) { + kvm_userspace_mem.userspace_addr = kvm->arch.ept_ptr; + r =__kvm_set_memory_region (kvm, &kvm_userspace_mem, true); + }else { + r =__kvm_set_memory_region (kvm, &kvm_userspace_mem, false); + } +if (r) goto out;@@ -4362,7 +4374,13 @@ static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr) .flags = 0, }; - ret =kvm_set_memory_region (kvm, &tss_mem, false); +if (kvm->arch.tss_ptr) { + tss_mem.userspace_addr = kvm->arch.tss_ptr; + ret =kvm_set_memory_region (kvm, &tss_mem, true); + }else { + ret =kvm_set_memory_region (kvm, &tss_mem, false); + } +if (ret)return ret; kvm->arch.tss_addr = addr;@@ -6683,6 +6701,27 @@ static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)vmx_complete_interrupts (vmx); } +static int vmx_set_private_memory (struct kvm *kvm, +struct kvm_userspace_memory_region *mem) +{ +/* +* Early sanity checking so userspace gets an error message during +* memory setup and not when trying to use this memory. +* Checks to see if the memory is valid are performed later when +* the memory is used. +*/ +if (!mem->userspace_addr || mem->userspace_addr & (PAGE_SIZE -1 ) || + mem->memory_size & (PAGE_SIZE -1 ) || + mem->memory_size <PAGE_SIZE *5 ) +return -EINVAL ; + + kvm->arch.ept_ptr = mem->userspace_addr; + kvm->arch.apic_ptr = mem->userspace_addr +PAGE_SIZE ; + kvm->arch.tss_ptr = mem->userspace_addr +PAGE_SIZE *2 ; + +return 0 ; +} +static void vmx_free_vcpu (struct kvm_vcpu *vcpu) {struct vcpu_vmx *vmx =to_vmx (vcpu);@@ -7532,6 +7571,8 @@ static struct kvm_x86_ops vmx_x86_ops = { .hardware_disable = hardware_disable, .cpu_has_accelerated_tpr = report_flexpriority, + .set_private_memory = vmx_set_private_memory, + .vcpu_create = vmx_create_vcpu, .vcpu_free = vmx_free_vcpu, .vcpu_reset = vmx_vcpu_reset,arch/x86/kvm/x86.c +10
-2@@ -6809,6 +6809,12 @@ void kvm_arch_sync_events(struct kvm *kvm)kvm_free_pit (kvm); } +int kvm_arch_set_private_memory (struct kvm *kvm, +struct kvm_userspace_memory_region *mem) +{ +return kvm_x86_ops->set_private_memory (kvm, mem); +} +void kvm_arch_destroy_vm (struct kvm *kvm) {kvm_iommu_unmap_guest (kvm);@@ -6913,7 +6919,8 @@ int kvm_arch_prepare_memory_region(struct kvm *kvm,* Only private memory slots need to be mapped here since * KVM_SET_MEMORY_REGION ioctl is no longer supported. */ -if ((memslot->id >=KVM_USER_MEM_SLOTS ) && npages && !old.npages) { +if ((memslot->id >=KVM_USER_MEM_SLOTS ) && npages && !old.npages && + !user_alloc) {unsigned long userspace_addr;/* @@ -6941,7 +6948,8 @@ void kvm_arch_commit_memory_region(struct kvm *kvm,int nr_mmu_pages =0 , npages = mem->memory_size >>PAGE_SHIFT ; -if ((mem->slot >=KVM_USER_MEM_SLOTS ) && old.npages && !npages) { +if ((mem->slot >=KVM_USER_MEM_SLOTS ) && old.npages && !npages && + !user_alloc) {int ret; ret = vm_munmap(old.userspace_addr,include/linux/kvm_host.h +2
-0@@ -461,6 +461,8 @@ int __kvm_set_memory_region(struct kvm *kvm,void kvm_arch_free_memslot (struct kvm_memory_slot *free,struct kvm_memory_slot *dont);int kvm_arch_create_memslot (struct kvm_memory_slot *slot,unsigned long npages); +int kvm_arch_set_private_memory (struct kvm *kvm, +struct kvm_userspace_memory_region *mem); int kvm_arch_prepare_memory_region(struct kvm *kvm, struct kvm_memory_slot *memslot, struct kvm_memory_slot old,virt/kvm/kvm_main.c +2
-0@@ -949,6 +949,8 @@ int kvm_vm_ioctl_set_memory_region(struct kvm *kvm, kvm_userspace_memory_region *mem, bool user_alloc) { + if (mem->slot == KVM_PRIVATE_MEMORY_MEMSLOT) + return kvm_arch_set_private_memory(kvm, mem);if (mem->slot >=KVM_USER_MEM_SLOTS )return -EINVAL ;return kvm_set_memory_region (kvm, mem, user_alloc);
I'm happy to not add a new API and use __kvm_set_memory_region to unregister private memory regions, but I thought chaning the API was the approach you asked for when I sent a previous patch. See the end
of: http://article.gmane.org/gmane.comp.emulators.kvm.devel/107753
Did I misunderstand your comment from 8 April?
Quoted 335 lines · Gleb Natapov
Quoted 4 lines · Andrew Honig
The motivation for this patch is to fix a 20KB leak of memory in
vmx.cwhen a VM is created and destroyed.Quoted 8 lines
On x86/vmx platforms KVM needs 5 pages of userspace memory per VM for architecture specific reasons. It currently allocates the pages on behalf of user space, but has no way of cleanly freeing that memory while the user space process is still running. For user space processes that want more control over that memory, this patch allows user space to provide the memory that KVM uses.
Signed-off-by Andrew Honig <[email protected]>I thought we agreed on not adding new API and using
__kvm_set_memory_region()to unregister private memory regions.Quoted 311 lines
--- Documentation/virtual/kvm/api.txt | 8 +++++++ arch/arm/kvm/arm.c | 6 +++++ arch/ia64/kvm/kvm-ia64.c | 6 +++++ arch/powerpc/kvm/powerpc.c | 6 +++++ arch/s390/kvm/kvm-s390.c | 6 +++++ arch/x86/include/asm/kvm_host.h | 7 ++++++ arch/x86/kvm/svm.c | 8 +++++++ arch/x86/kvm/vmx.c | 47 ++++++++++++++++++++++++++++++++++--- arch/x86/kvm/x86.c | 12 ++++++++-- include/linux/kvm_host.h | 2 ++ virt/kvm/kvm_main.c | 2 ++ 11 files changed, 105 insertions(+), 5 deletions(-)Documentation/virtual/kvm/api.txt +8
-0@@ -879,6 +879,14 @@ It is recommended that the lower 21 bits of guest_phys_addr and userspace_addr be identical. This allows large pages in the guest to be backed by large pages in the host. +On x86/vmx architectures KVM needs 5 pages of user space memory for architecture +specific reasons. Calling this ioctl with the special memslot +KVM_PRIVATE_MEMORY_MEMSLOT will tell kvm which user space memory to use for +that memory. If that memslot is not set before creating VCPUs for a VM then +kvm will allocate the memory on behalf of user space, but userspace will not +be able to free that memory. User space should treat this memory as opaque +and not modify it. + The flags field supports two flags: KVM_MEM_LOG_DIRTY_PAGES and KVM_MEM_READONLY. The former can be set to instruct KVM to keep track of writes to memory within the slot. See KVM_GET_DIRTY_LOG ioctl to know how toarch/arm/kvm/arm.c +6
-0@@ -228,6 +228,12 @@ int kvm_arch_set_memory_region(struct kvm *kvm,return 0 ; } +int kvm_arch_set_private_memory (struct kvm *kvm, +struct kvm_userspace_memory_region *mem) +{ +return 0 ; +} + int kvm_arch_prepare_memory_region(struct kvm *kvm, struct kvm_memory_slot *memslot, struct kvm_memory_slot old,arch/ia64/kvm/kvm-ia64.c +6
-0@@ -1576,6 +1576,12 @@ int kvm_arch_create_memslot(struct kvm_memory_slot *slot, unsigned long npages)return 0 ; } +int kvm_arch_set_private_memory (struct kvm *kvm, +struct kvm_userspace_memory_region *mem) +{ +return 0 ; +} + int kvm_arch_prepare_memory_region(struct kvm *kvm, struct kvm_memory_slot *memslot, struct kvm_memory_slot old,arch/powerpc/kvm/powerpc.c +6
-0@@ -410,6 +410,12 @@ int kvm_arch_create_memslot(struct kvm_memory_slot *slot, unsigned long npages)return kvmppc_core_create_memslot (slot, npages); } +int kvm_arch_set_private_memory (struct kvm *kvm, +struct kvm_userspace_memory_region *mem) +{ +return 0 ; +} + int kvm_arch_prepare_memory_region(struct kvm *kvm, struct kvm_memory_slot *memslot, struct kvm_memory_slot old,arch/s390/kvm/kvm-s390.c +6
-0@@ -971,6 +971,12 @@ int kvm_arch_create_memslot(struct kvm_memory_slot *slot, unsigned long npages)return 0 ; } +int kvm_arch_set_private_memory (struct kvm *kvm, +struct kvm_userspace_memory_region *mem) +{ +return 0 ; +} +/* Section: memory related */ int kvm_arch_prepare_memory_region(struct kvm *kvm, struct kvm_memory_slot *memslot,arch/x86/include/asm/kvm_host.h +7
-0@@ -37,6 +37,7 @@/* memory slots that are not exposed to userspace */ #define KVM_PRIVATE_MEM_SLOTS 3#define KVM_MEM_SLOTS_NUM (KVM_USER_MEM_SLOTS + KVM_PRIVATE_MEM_SLOTS) +#define KVM_PRIVATE_MEMORY_MEMSLOT 0x80000001#define KVM_MMIO_SIZE 16@@ -553,6 +554,9 @@ struct kvm_arch {struct page *ept_identity_pagetable;bool ept_identity_pagetable_done;gpa_t ept_identity_map_addr; +unsigned long ept_ptr; +unsigned long apic_ptr; +unsigned long tss_ptr;unsigned long irq_sources_bitmap;s64 kvmclock_offset;@@ -640,6 +644,9 @@ struct kvm_x86_ops {bool (*cpu_has_accelerated_tpr)(void );void (*cpuid_update)(struct kvm_vcpu *vcpu); +int (*set_private_memory)(struct kvm *kvm, +struct kvm_userspace_memory_region *mem); +/* Create, but do not attach this VCPU */ struct kvm_vcpu *(*vcpu_create)(struct kvm *kvm,unsigned id);void (*vcpu_free)(struct kvm_vcpu *vcpu);arch/x86/kvm/svm.c +8
-0@@ -1211,6 +1211,12 @@ static int svm_vcpu_reset(struct kvm_vcpu *vcpu)return 0 ; } +static int svm_set_private_memory (struct kvm *kvm, +struct kvm_userspace_memory_region *mem) +{ +return 0 ; +} +static struct kvm_vcpu *svm_create_vcpu (struct kvm *kvm,unsigned int id) {struct vcpu_svm *svm;@@ -4257,6 +4263,8 @@ static struct kvm_x86_ops svm_x86_ops = { .hardware_disable = svm_hardware_disable, .cpu_has_accelerated_tpr = svm_cpu_has_accelerated_tpr, + .set_private_memory = svm_set_private_memory, + .vcpu_create = svm_create_vcpu, .vcpu_free = svm_free_vcpu, .vcpu_reset = svm_vcpu_reset,arch/x86/kvm/vmx.c +44
-3@@ -3692,7 +3692,13 @@ static int alloc_apic_access_page(struct kvm *kvm) kvm_userspace_mem.flags =0 ; kvm_userspace_mem.guest_phys_addr =0xfee00000ULL ; kvm_userspace_mem.memory_size =PAGE_SIZE ; - r =__kvm_set_memory_region (kvm, &kvm_userspace_mem, false); +if (kvm->arch.apic_ptr) { + kvm_userspace_mem.userspace_addr = kvm->arch.apic_ptr; + r =__kvm_set_memory_region (kvm, &kvm_userspace_mem, true); + }else { + r =__kvm_set_memory_region (kvm, &kvm_userspace_mem, false); + } +if (r) goto out;@@ -3722,7 +3728,13 @@ static int alloc_identity_pagetable(struct kvm *kvm) kvm_userspace_mem.guest_phys_addr = kvm->arch.ept_identity_map_addr; kvm_userspace_mem.memory_size =PAGE_SIZE ; - r =__kvm_set_memory_region (kvm, &kvm_userspace_mem, false); +if (kvm->arch.ept_ptr) { + kvm_userspace_mem.userspace_addr = kvm->arch.ept_ptr; + r =__kvm_set_memory_region (kvm, &kvm_userspace_mem, true); + }else { + r =__kvm_set_memory_region (kvm, &kvm_userspace_mem, false); + } +if (r) goto out;@@ -4362,7 +4374,13 @@ static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr) .flags = 0, }; - ret =kvm_set_memory_region (kvm, &tss_mem, false); +if (kvm->arch.tss_ptr) { + tss_mem.userspace_addr = kvm->arch.tss_ptr; + ret =kvm_set_memory_region (kvm, &tss_mem, true); + }else { + ret =kvm_set_memory_region (kvm, &tss_mem, false); + } +if (ret)return ret; kvm->arch.tss_addr = addr;@@ -6683,6 +6701,27 @@ static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)vmx_complete_interrupts (vmx); } +static int vmx_set_private_memory (struct kvm *kvm, +struct kvm_userspace_memory_region *mem) +{ +/* +* Early sanity checking so userspace gets an error message during +* memory setup and not when trying to use this memory. +* Checks to see if the memory is valid are performed later when +* the memory is used. +*/ +if (!mem->userspace_addr || mem->userspace_addr & (PAGE_SIZE -1 ) || + mem->memory_size & (PAGE_SIZE -1 ) || + mem->memory_size <PAGE_SIZE *5 ) +return -EINVAL ; + + kvm->arch.ept_ptr = mem->userspace_addr; + kvm->arch.apic_ptr = mem->userspace_addr +PAGE_SIZE ; + kvm->arch.tss_ptr = mem->userspace_addr +PAGE_SIZE *2 ; + +return 0 ; +} +static void vmx_free_vcpu (struct kvm_vcpu *vcpu) {struct vcpu_vmx *vmx =to_vmx (vcpu);@@ -7532,6 +7571,8 @@ static struct kvm_x86_ops vmx_x86_ops = { .hardware_disable = hardware_disable, .cpu_has_accelerated_tpr = report_flexpriority, + .set_private_memory = vmx_set_private_memory, + .vcpu_create = vmx_create_vcpu, .vcpu_free = vmx_free_vcpu, .vcpu_reset = vmx_vcpu_reset,arch/x86/kvm/x86.c +10
-2@@ -6809,6 +6809,12 @@ void kvm_arch_sync_events(struct kvm *kvm)kvm_free_pit (kvm); } +int kvm_arch_set_private_memory (struct kvm *kvm, +struct kvm_userspace_memory_region *mem) +{ +return kvm_x86_ops->set_private_memory (kvm, mem); +} +void kvm_arch_destroy_vm (struct kvm *kvm) {kvm_iommu_unmap_guest (kvm);@@ -6913,7 +6919,8 @@ int kvm_arch_prepare_memory_region(struct kvm *kvm,* Only private memory slots need to be mapped here since * KVM_SET_MEMORY_REGION ioctl is no longer supported. */ -if ((memslot->id >=KVM_USER_MEM_SLOTS ) && npages && !old.npages) { +if ((memslot->id >=KVM_USER_MEM_SLOTS ) && npages && !old.npages && + !user_alloc) {unsigned long userspace_addr;/* @@ -6941,7 +6948,8 @@ void kvm_arch_commit_memory_region(struct kvm *kvm,int nr_mmu_pages =0 , npages = mem->memory_size >>PAGE_SHIFT ; -if ((mem->slot >=KVM_USER_MEM_SLOTS ) && old.npages && !npages) { +if ((mem->slot >=KVM_USER_MEM_SLOTS ) && old.npages && !npages && + !user_alloc) {int ret; ret = vm_munmap(old.userspace_addr,include/linux/kvm_host.h +2
-0@@ -461,6 +461,8 @@ int __kvm_set_memory_region(struct kvm *kvm,void kvm_arch_free_memslot (struct kvm_memory_slot *free,struct kvm_memory_slot *dont);int kvm_arch_create_memslot (struct kvm_memory_slot *slot,unsigned long npages); +int kvm_arch_set_private_memory (struct kvm *kvm, +struct kvm_userspace_memory_region *mem); int kvm_arch_prepare_memory_region(struct kvm *kvm, struct kvm_memory_slot *memslot, struct kvm_memory_slot old,virt/kvm/kvm_main.c +2
-1@@ -949,6 +949,8 @@ int kvm_vm_ioctl_set_memory_region(struct kvm *kvm, kvm_userspace_memory_region *mem, bool user_alloc) { + if (mem->slot == KVM_PRIVATE_MEMORY_MEMSLOT) + return kvm_arch_set_private_memory(kvm, mem);if (mem->slot >=KVM_USER_MEM_SLOTS )return -EINVAL ;return kvm_set_memory_region (kvm, mem, user_alloc); --1.7.10.4
Quoted 4 lines
--
To unsubscribe from this list: send the line "unsubscribe kvm" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html--
Gleb.
Quoted 7 lines · Andrew Honig
I'm happy to not add a new API and use
__kvm_set_memory_regionto unregister private memory regions, but I thought chaning the API was the approach you asked for when I sent a previous patch. See the end
of: http://article.gmane.org/gmane.comp.emulators.kvm.devel/107753Did I misunderstand your comment from 8 April?
Ugh, yes. My "Please send a second version" was in response to your question "or should I send a second version of this current patch?" Where second version is the same as the first one but uses __kvm_set_memory_region() for slot deletion. I am sorry about the confusion :(
Quoted 329 lines · Gleb Natapov
Quoted 328 lines · Andrew Honig
Quoted 4 lines
The motivation for this patch is to fix a 20KB leak of memory in
vmx.cwhen a VM is created and destroyed.Quoted 8 lines
On x86/vmx platforms KVM needs 5 pages of userspace memory per VM for architecture specific reasons. It currently allocates the pages on behalf of user space, but has no way of cleanly freeing that memory while the user space process is still running. For user space processes that want more control over that memory, this patch allows user space to provide the memory that KVM uses.
Signed-off-by Andrew Honig <[email protected]>I thought we agreed on not adding new API and using
__kvm_set_memory_region()to unregister private memory regions.Quoted 311 lines
--- Documentation/virtual/kvm/api.txt | 8 +++++++ arch/arm/kvm/arm.c | 6 +++++ arch/ia64/kvm/kvm-ia64.c | 6 +++++ arch/powerpc/kvm/powerpc.c | 6 +++++ arch/s390/kvm/kvm-s390.c | 6 +++++ arch/x86/include/asm/kvm_host.h | 7 ++++++ arch/x86/kvm/svm.c | 8 +++++++ arch/x86/kvm/vmx.c | 47 ++++++++++++++++++++++++++++++++++--- arch/x86/kvm/x86.c | 12 ++++++++-- include/linux/kvm_host.h | 2 ++ virt/kvm/kvm_main.c | 2 ++ 11 files changed, 105 insertions(+), 5 deletions(-)Documentation/virtual/kvm/api.txt +8
-0@@ -879,6 +879,14 @@ It is recommended that the lower 21 bits of guest_phys_addr and userspace_addr be identical. This allows large pages in the guest to be backed by large pages in the host. +On x86/vmx architectures KVM needs 5 pages of user space memory for architecture +specific reasons. Calling this ioctl with the special memslot +KVM_PRIVATE_MEMORY_MEMSLOT will tell kvm which user space memory to use for +that memory. If that memslot is not set before creating VCPUs for a VM then +kvm will allocate the memory on behalf of user space, but userspace will not +be able to free that memory. User space should treat this memory as opaque +and not modify it. + The flags field supports two flags: KVM_MEM_LOG_DIRTY_PAGES and KVM_MEM_READONLY. The former can be set to instruct KVM to keep track of writes to memory within the slot. See KVM_GET_DIRTY_LOG ioctl to know how toarch/arm/kvm/arm.c +6
-0@@ -228,6 +228,12 @@ int kvm_arch_set_memory_region(struct kvm *kvm,return 0 ; } +int kvm_arch_set_private_memory (struct kvm *kvm, +struct kvm_userspace_memory_region *mem) +{ +return 0 ; +} + int kvm_arch_prepare_memory_region(struct kvm *kvm, struct kvm_memory_slot *memslot, struct kvm_memory_slot old,arch/ia64/kvm/kvm-ia64.c +6
-0@@ -1576,6 +1576,12 @@ int kvm_arch_create_memslot(struct kvm_memory_slot *slot, unsigned long npages)return 0 ; } +int kvm_arch_set_private_memory (struct kvm *kvm, +struct kvm_userspace_memory_region *mem) +{ +return 0 ; +} + int kvm_arch_prepare_memory_region(struct kvm *kvm, struct kvm_memory_slot *memslot, struct kvm_memory_slot old,arch/powerpc/kvm/powerpc.c +6
-0@@ -410,6 +410,12 @@ int kvm_arch_create_memslot(struct kvm_memory_slot *slot, unsigned long npages)return kvmppc_core_create_memslot (slot, npages); } +int kvm_arch_set_private_memory (struct kvm *kvm, +struct kvm_userspace_memory_region *mem) +{ +return 0 ; +} + int kvm_arch_prepare_memory_region(struct kvm *kvm, struct kvm_memory_slot *memslot, struct kvm_memory_slot old,arch/s390/kvm/kvm-s390.c +6
-0@@ -971,6 +971,12 @@ int kvm_arch_create_memslot(struct kvm_memory_slot *slot, unsigned long npages)return 0 ; } +int kvm_arch_set_private_memory (struct kvm *kvm, +struct kvm_userspace_memory_region *mem) +{ +return 0 ; +} +/* Section: memory related */ int kvm_arch_prepare_memory_region(struct kvm *kvm, struct kvm_memory_slot *memslot,arch/x86/include/asm/kvm_host.h +7
-0@@ -37,6 +37,7 @@/* memory slots that are not exposed to userspace */ #define KVM_PRIVATE_MEM_SLOTS 3#define KVM_MEM_SLOTS_NUM (KVM_USER_MEM_SLOTS + KVM_PRIVATE_MEM_SLOTS) +#define KVM_PRIVATE_MEMORY_MEMSLOT 0x80000001#define KVM_MMIO_SIZE 16@@ -553,6 +554,9 @@ struct kvm_arch {struct page *ept_identity_pagetable;bool ept_identity_pagetable_done;gpa_t ept_identity_map_addr; +unsigned long ept_ptr; +unsigned long apic_ptr; +unsigned long tss_ptr;unsigned long irq_sources_bitmap;s64 kvmclock_offset;@@ -640,6 +644,9 @@ struct kvm_x86_ops {bool (*cpu_has_accelerated_tpr)(void );void (*cpuid_update)(struct kvm_vcpu *vcpu); +int (*set_private_memory)(struct kvm *kvm, +struct kvm_userspace_memory_region *mem); +/* Create, but do not attach this VCPU */ struct kvm_vcpu *(*vcpu_create)(struct kvm *kvm,unsigned id);void (*vcpu_free)(struct kvm_vcpu *vcpu);arch/x86/kvm/svm.c +8
-0@@ -1211,6 +1211,12 @@ static int svm_vcpu_reset(struct kvm_vcpu *vcpu)return 0 ; } +static int svm_set_private_memory (struct kvm *kvm, +struct kvm_userspace_memory_region *mem) +{ +return 0 ; +} +static struct kvm_vcpu *svm_create_vcpu (struct kvm *kvm,unsigned int id) {struct vcpu_svm *svm;@@ -4257,6 +4263,8 @@ static struct kvm_x86_ops svm_x86_ops = { .hardware_disable = svm_hardware_disable, .cpu_has_accelerated_tpr = svm_cpu_has_accelerated_tpr, + .set_private_memory = svm_set_private_memory, + .vcpu_create = svm_create_vcpu, .vcpu_free = svm_free_vcpu, .vcpu_reset = svm_vcpu_reset,arch/x86/kvm/vmx.c +44
-3@@ -3692,7 +3692,13 @@ static int alloc_apic_access_page(struct kvm *kvm) kvm_userspace_mem.flags =0 ; kvm_userspace_mem.guest_phys_addr =0xfee00000ULL ; kvm_userspace_mem.memory_size =PAGE_SIZE ; - r =__kvm_set_memory_region (kvm, &kvm_userspace_mem, false); +if (kvm->arch.apic_ptr) { + kvm_userspace_mem.userspace_addr = kvm->arch.apic_ptr; + r =__kvm_set_memory_region (kvm, &kvm_userspace_mem, true); + }else { + r =__kvm_set_memory_region (kvm, &kvm_userspace_mem, false); + } +if (r) goto out;@@ -3722,7 +3728,13 @@ static int alloc_identity_pagetable(struct kvm *kvm) kvm_userspace_mem.guest_phys_addr = kvm->arch.ept_identity_map_addr; kvm_userspace_mem.memory_size =PAGE_SIZE ; - r =__kvm_set_memory_region (kvm, &kvm_userspace_mem, false); +if (kvm->arch.ept_ptr) { + kvm_userspace_mem.userspace_addr = kvm->arch.ept_ptr; + r =__kvm_set_memory_region (kvm, &kvm_userspace_mem, true); + }else { + r =__kvm_set_memory_region (kvm, &kvm_userspace_mem, false); + } +if (r) goto out;@@ -4362,7 +4374,13 @@ static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr) .flags = 0, }; - ret =kvm_set_memory_region (kvm, &tss_mem, false); +if (kvm->arch.tss_ptr) { + tss_mem.userspace_addr = kvm->arch.tss_ptr; + ret =kvm_set_memory_region (kvm, &tss_mem, true); + }else { + ret =kvm_set_memory_region (kvm, &tss_mem, false); + } +if (ret)return ret; kvm->arch.tss_addr = addr;@@ -6683,6 +6701,27 @@ static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)vmx_complete_interrupts (vmx); } +static int vmx_set_private_memory (struct kvm *kvm, +struct kvm_userspace_memory_region *mem) +{ +/* +* Early sanity checking so userspace gets an error message during +* memory setup and not when trying to use this memory. +* Checks to see if the memory is valid are performed later when +* the memory is used. +*/ +if (!mem->userspace_addr || mem->userspace_addr & (PAGE_SIZE -1 ) || + mem->memory_size & (PAGE_SIZE -1 ) || + mem->memory_size <PAGE_SIZE *5 ) +return -EINVAL ; + + kvm->arch.ept_ptr = mem->userspace_addr; + kvm->arch.apic_ptr = mem->userspace_addr +PAGE_SIZE ; + kvm->arch.tss_ptr = mem->userspace_addr +PAGE_SIZE *2 ; + +return 0 ; +} +static void vmx_free_vcpu (struct kvm_vcpu *vcpu) {struct vcpu_vmx *vmx =to_vmx (vcpu);@@ -7532,6 +7571,8 @@ static struct kvm_x86_ops vmx_x86_ops = { .hardware_disable = hardware_disable, .cpu_has_accelerated_tpr = report_flexpriority, + .set_private_memory = vmx_set_private_memory, + .vcpu_create = vmx_create_vcpu, .vcpu_free = vmx_free_vcpu, .vcpu_reset = vmx_vcpu_reset,arch/x86/kvm/x86.c +10
-2@@ -6809,6 +6809,12 @@ void kvm_arch_sync_events(struct kvm *kvm)kvm_free_pit (kvm); } +int kvm_arch_set_private_memory (struct kvm *kvm, +struct kvm_userspace_memory_region *mem) +{ +return kvm_x86_ops->set_private_memory (kvm, mem); +} +void kvm_arch_destroy_vm (struct kvm *kvm) {kvm_iommu_unmap_guest (kvm);@@ -6913,7 +6919,8 @@ int kvm_arch_prepare_memory_region(struct kvm *kvm,* Only private memory slots need to be mapped here since * KVM_SET_MEMORY_REGION ioctl is no longer supported. */ -if ((memslot->id >=KVM_USER_MEM_SLOTS ) && npages && !old.npages) { +if ((memslot->id >=KVM_USER_MEM_SLOTS ) && npages && !old.npages && + !user_alloc) {unsigned long userspace_addr;/* @@ -6941,7 +6948,8 @@ void kvm_arch_commit_memory_region(struct kvm *kvm,int nr_mmu_pages =0 , npages = mem->memory_size >>PAGE_SHIFT ; -if ((mem->slot >=KVM_USER_MEM_SLOTS ) && old.npages && !npages) { +if ((mem->slot >=KVM_USER_MEM_SLOTS ) && old.npages && !npages && + !user_alloc) {int ret; ret = vm_munmap(old.userspace_addr,include/linux/kvm_host.h +2
-0@@ -461,6 +461,8 @@ int __kvm_set_memory_region(struct kvm *kvm,void kvm_arch_free_memslot (struct kvm_memory_slot *free,struct kvm_memory_slot *dont);int kvm_arch_create_memslot (struct kvm_memory_slot *slot,unsigned long npages); +int kvm_arch_set_private_memory (struct kvm *kvm, +struct kvm_userspace_memory_region *mem); int kvm_arch_prepare_memory_region(struct kvm *kvm, struct kvm_memory_slot *memslot, struct kvm_memory_slot old,virt/kvm/kvm_main.c +2
-1@@ -949,6 +949,8 @@ int kvm_vm_ioctl_set_memory_region(struct kvm *kvm, kvm_userspace_memory_region *mem, bool user_alloc) { + if (mem->slot == KVM_PRIVATE_MEMORY_MEMSLOT) + return kvm_arch_set_private_memory(kvm, mem);if (mem->slot >=KVM_USER_MEM_SLOTS )return -EINVAL ;return kvm_set_memory_region (kvm, mem, user_alloc); --1.7.10.4
Quoted 7 lines
Quoted 7 lines
Quoted 4 lines
--
To unsubscribe from this list: send the line "unsubscribe kvm" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html--
Gleb.
--
Gleb.
Alright, I'll send that soon. Sorry for the mixup.
Quoted 353 lines · Gleb Natapov
Quoted 7 lines · Andrew Honig
I'm happy to not add a new API and use
__kvm_set_memory_regionto unregister private memory regions, but I thought chaning the API was the approach you asked for when I sent a previous patch. See the end
of: http://article.gmane.org/gmane.comp.emulators.kvm.devel/107753Did I misunderstand your comment from 8 April?
Ugh, yes. My "Please send a second version" was in response to your question "or should I send a second version of this current patch?" Where second version is the same as the first one but uses
__kvm_set_memory_region()for slot deletion. I am sorry about the confusion :(Quoted 329 lines · Gleb Natapov
Quoted 328 lines · Andrew Honig
Quoted 4 lines
The motivation for this patch is to fix a 20KB leak of memory in
vmx.cwhen a VM is created and destroyed.Quoted 8 lines
On x86/vmx platforms KVM needs 5 pages of userspace memory per VM for architecture specific reasons. It currently allocates the pages on behalf of user space, but has no way of cleanly freeing that memory while the user space process is still running. For user space processes that want more control over that memory, this patch allows user space to provide the memory that KVM uses.
Signed-off-by Andrew Honig <[email protected]>I thought we agreed on not adding new API and using
__kvm_set_memory_region()to unregister private memory regions.Quoted 311 lines
--- Documentation/virtual/kvm/api.txt | 8 +++++++ arch/arm/kvm/arm.c | 6 +++++ arch/ia64/kvm/kvm-ia64.c | 6 +++++ arch/powerpc/kvm/powerpc.c | 6 +++++ arch/s390/kvm/kvm-s390.c | 6 +++++ arch/x86/include/asm/kvm_host.h | 7 ++++++ arch/x86/kvm/svm.c | 8 +++++++ arch/x86/kvm/vmx.c | 47 ++++++++++++++++++++++++++++++++++--- arch/x86/kvm/x86.c | 12 ++++++++-- include/linux/kvm_host.h | 2 ++ virt/kvm/kvm_main.c | 2 ++ 11 files changed, 105 insertions(+), 5 deletions(-)Documentation/virtual/kvm/api.txt +8
-0@@ -879,6 +879,14 @@ It is recommended that the lower 21 bits of guest_phys_addr and userspace_addr be identical. This allows large pages in the guest to be backed by large pages in the host. +On x86/vmx architectures KVM needs 5 pages of user space memory for architecture +specific reasons. Calling this ioctl with the special memslot +KVM_PRIVATE_MEMORY_MEMSLOT will tell kvm which user space memory to use for +that memory. If that memslot is not set before creating VCPUs for a VM then +kvm will allocate the memory on behalf of user space, but userspace will not +be able to free that memory. User space should treat this memory as opaque +and not modify it. + The flags field supports two flags: KVM_MEM_LOG_DIRTY_PAGES and KVM_MEM_READONLY. The former can be set to instruct KVM to keep track of writes to memory within the slot. See KVM_GET_DIRTY_LOG ioctl to know how toarch/arm/kvm/arm.c +6
-0@@ -228,6 +228,12 @@ int kvm_arch_set_memory_region(struct kvm *kvm,return 0 ; } +int kvm_arch_set_private_memory (struct kvm *kvm, +struct kvm_userspace_memory_region *mem) +{ +return 0 ; +} + int kvm_arch_prepare_memory_region(struct kvm *kvm, struct kvm_memory_slot *memslot, struct kvm_memory_slot old,arch/ia64/kvm/kvm-ia64.c +6
-0@@ -1576,6 +1576,12 @@ int kvm_arch_create_memslot(struct kvm_memory_slot *slot, unsigned long npages)return 0 ; } +int kvm_arch_set_private_memory (struct kvm *kvm, +struct kvm_userspace_memory_region *mem) +{ +return 0 ; +} + int kvm_arch_prepare_memory_region(struct kvm *kvm, struct kvm_memory_slot *memslot, struct kvm_memory_slot old,arch/powerpc/kvm/powerpc.c +6
-0@@ -410,6 +410,12 @@ int kvm_arch_create_memslot(struct kvm_memory_slot *slot, unsigned long npages)return kvmppc_core_create_memslot (slot, npages); } +int kvm_arch_set_private_memory (struct kvm *kvm, +struct kvm_userspace_memory_region *mem) +{ +return 0 ; +} + int kvm_arch_prepare_memory_region(struct kvm *kvm, struct kvm_memory_slot *memslot, struct kvm_memory_slot old,arch/s390/kvm/kvm-s390.c +6
-0@@ -971,6 +971,12 @@ int kvm_arch_create_memslot(struct kvm_memory_slot *slot, unsigned long npages)return 0 ; } +int kvm_arch_set_private_memory (struct kvm *kvm, +struct kvm_userspace_memory_region *mem) +{ +return 0 ; +} +/* Section: memory related */ int kvm_arch_prepare_memory_region(struct kvm *kvm, struct kvm_memory_slot *memslot,arch/x86/include/asm/kvm_host.h +7
-0@@ -37,6 +37,7 @@/* memory slots that are not exposed to userspace */ #define KVM_PRIVATE_MEM_SLOTS 3#define KVM_MEM_SLOTS_NUM (KVM_USER_MEM_SLOTS + KVM_PRIVATE_MEM_SLOTS) +#define KVM_PRIVATE_MEMORY_MEMSLOT 0x80000001#define KVM_MMIO_SIZE 16@@ -553,6 +554,9 @@ struct kvm_arch {struct page *ept_identity_pagetable;bool ept_identity_pagetable_done;gpa_t ept_identity_map_addr; +unsigned long ept_ptr; +unsigned long apic_ptr; +unsigned long tss_ptr;unsigned long irq_sources_bitmap;s64 kvmclock_offset;@@ -640,6 +644,9 @@ struct kvm_x86_ops {bool (*cpu_has_accelerated_tpr)(void );void (*cpuid_update)(struct kvm_vcpu *vcpu); +int (*set_private_memory)(struct kvm *kvm, +struct kvm_userspace_memory_region *mem); +/* Create, but do not attach this VCPU */ struct kvm_vcpu *(*vcpu_create)(struct kvm *kvm,unsigned id);void (*vcpu_free)(struct kvm_vcpu *vcpu);arch/x86/kvm/svm.c +8
-0@@ -1211,6 +1211,12 @@ static int svm_vcpu_reset(struct kvm_vcpu *vcpu)return 0 ; } +static int svm_set_private_memory (struct kvm *kvm, +struct kvm_userspace_memory_region *mem) +{ +return 0 ; +} +static struct kvm_vcpu *svm_create_vcpu (struct kvm *kvm,unsigned int id) {struct vcpu_svm *svm;@@ -4257,6 +4263,8 @@ static struct kvm_x86_ops svm_x86_ops = { .hardware_disable = svm_hardware_disable, .cpu_has_accelerated_tpr = svm_cpu_has_accelerated_tpr, + .set_private_memory = svm_set_private_memory, + .vcpu_create = svm_create_vcpu, .vcpu_free = svm_free_vcpu, .vcpu_reset = svm_vcpu_reset,arch/x86/kvm/vmx.c +44
-3@@ -3692,7 +3692,13 @@ static int alloc_apic_access_page(struct kvm *kvm) kvm_userspace_mem.flags =0 ; kvm_userspace_mem.guest_phys_addr =0xfee00000ULL ; kvm_userspace_mem.memory_size =PAGE_SIZE ; - r =__kvm_set_memory_region (kvm, &kvm_userspace_mem, false); +if (kvm->arch.apic_ptr) { + kvm_userspace_mem.userspace_addr = kvm->arch.apic_ptr; + r =__kvm_set_memory_region (kvm, &kvm_userspace_mem, true); + }else { + r =__kvm_set_memory_region (kvm, &kvm_userspace_mem, false); + } +if (r) goto out;@@ -3722,7 +3728,13 @@ static int alloc_identity_pagetable(struct kvm *kvm) kvm_userspace_mem.guest_phys_addr = kvm->arch.ept_identity_map_addr; kvm_userspace_mem.memory_size =PAGE_SIZE ; - r =__kvm_set_memory_region (kvm, &kvm_userspace_mem, false); +if (kvm->arch.ept_ptr) { + kvm_userspace_mem.userspace_addr = kvm->arch.ept_ptr; + r =__kvm_set_memory_region (kvm, &kvm_userspace_mem, true); + }else { + r =__kvm_set_memory_region (kvm, &kvm_userspace_mem, false); + } +if (r) goto out;@@ -4362,7 +4374,13 @@ static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr) .flags = 0, }; - ret =kvm_set_memory_region (kvm, &tss_mem, false); +if (kvm->arch.tss_ptr) { + tss_mem.userspace_addr = kvm->arch.tss_ptr; + ret =kvm_set_memory_region (kvm, &tss_mem, true); + }else { + ret =kvm_set_memory_region (kvm, &tss_mem, false); + } +if (ret)return ret; kvm->arch.tss_addr = addr;@@ -6683,6 +6701,27 @@ static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)vmx_complete_interrupts (vmx); } +static int vmx_set_private_memory (struct kvm *kvm, +struct kvm_userspace_memory_region *mem) +{ +/* +* Early sanity checking so userspace gets an error message during +* memory setup and not when trying to use this memory. +* Checks to see if the memory is valid are performed later when +* the memory is used. +*/ +if (!mem->userspace_addr || mem->userspace_addr & (PAGE_SIZE -1 ) || + mem->memory_size & (PAGE_SIZE -1 ) || + mem->memory_size <PAGE_SIZE *5 ) +return -EINVAL ; + + kvm->arch.ept_ptr = mem->userspace_addr; + kvm->arch.apic_ptr = mem->userspace_addr +PAGE_SIZE ; + kvm->arch.tss_ptr = mem->userspace_addr +PAGE_SIZE *2 ; + +return 0 ; +} +static void vmx_free_vcpu (struct kvm_vcpu *vcpu) {struct vcpu_vmx *vmx =to_vmx (vcpu);@@ -7532,6 +7571,8 @@ static struct kvm_x86_ops vmx_x86_ops = { .hardware_disable = hardware_disable, .cpu_has_accelerated_tpr = report_flexpriority, + .set_private_memory = vmx_set_private_memory, + .vcpu_create = vmx_create_vcpu, .vcpu_free = vmx_free_vcpu, .vcpu_reset = vmx_vcpu_reset,arch/x86/kvm/x86.c +10
-2@@ -6809,6 +6809,12 @@ void kvm_arch_sync_events(struct kvm *kvm)kvm_free_pit (kvm); } +int kvm_arch_set_private_memory (struct kvm *kvm, +struct kvm_userspace_memory_region *mem) +{ +return kvm_x86_ops->set_private_memory (kvm, mem); +} +void kvm_arch_destroy_vm (struct kvm *kvm) {kvm_iommu_unmap_guest (kvm);@@ -6913,7 +6919,8 @@ int kvm_arch_prepare_memory_region(struct kvm *kvm,* Only private memory slots need to be mapped here since * KVM_SET_MEMORY_REGION ioctl is no longer supported. */ -if ((memslot->id >=KVM_USER_MEM_SLOTS ) && npages && !old.npages) { +if ((memslot->id >=KVM_USER_MEM_SLOTS ) && npages && !old.npages && + !user_alloc) {unsigned long userspace_addr;/* @@ -6941,7 +6948,8 @@ void kvm_arch_commit_memory_region(struct kvm *kvm,int nr_mmu_pages =0 , npages = mem->memory_size >>PAGE_SHIFT ; -if ((mem->slot >=KVM_USER_MEM_SLOTS ) && old.npages && !npages) { +if ((mem->slot >=KVM_USER_MEM_SLOTS ) && old.npages && !npages && + !user_alloc) {int ret; ret = vm_munmap(old.userspace_addr,include/linux/kvm_host.h +2
-0@@ -461,6 +461,8 @@ int __kvm_set_memory_region(struct kvm *kvm,void kvm_arch_free_memslot (struct kvm_memory_slot *free,struct kvm_memory_slot *dont);int kvm_arch_create_memslot (struct kvm_memory_slot *slot,unsigned long npages); +int kvm_arch_set_private_memory (struct kvm *kvm, +struct kvm_userspace_memory_region *mem); int kvm_arch_prepare_memory_region(struct kvm *kvm, struct kvm_memory_slot *memslot, struct kvm_memory_slot old,virt/kvm/kvm_main.c +2
-1@@ -949,6 +949,8 @@ int kvm_vm_ioctl_set_memory_region(struct kvm *kvm, kvm_userspace_memory_region *mem, bool user_alloc) { + if (mem->slot == KVM_PRIVATE_MEMORY_MEMSLOT) + return kvm_arch_set_private_memory(kvm, mem);if (mem->slot >=KVM_USER_MEM_SLOTS )return -EINVAL ;return kvm_set_memory_region (kvm, mem, user_alloc); --1.7.10.4
Quoted 7 lines
Quoted 7 lines
Quoted 4 lines
--
To unsubscribe from this list: send the line "unsubscribe kvm" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html--
Gleb.--
Gleb.
Paolo Bonzini
maintainerIl 17/04/2013 17:19, Andrew Honig ha scritto:
Quoted 5 lines
Freeing the memory in
kvm_arch_free_memslotis as good as anywhere else in KVM. The problem is that this memory is in the user space process mm. This codepath could be called after the mm is destroyed in the case of an process exit without closing the fd, which will result in a panic onvm_munmapwhen it tries to access the mm.
I think that's not a problem, the KVM file descriptor keeps the mm alive.
Quoted 3 lines
There's also the possibility that another process closes the fd and messing with that processes memory map seems like it's asking for trouble.
You can check that current->mm == kvm->mm and leak the memory if they don't match.
Paolo