From: Andrew Honig Subject: [PATCHv2] KVM: x86: Fix memory leak in vmx.c Date: Wed, 17 Apr 2013 10:54:32 -0700 (PDT) Message-ID: Mime-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII To: ahonig@google.com, kvm@vger.kernel.org Return-path: Received: from mail-gg0-f201.google.com ([209.85.161.201]:60992 "EHLO mail-gg0-f201.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1755239Ab3DQRye (ORCPT ); Wed, 17 Apr 2013 13:54:34 -0400 Received: by mail-gg0-f201.google.com with SMTP id q6so188350ggc.2 for ; Wed, 17 Apr 2013 10:54:34 -0700 (PDT) Sender: kvm-owner@vger.kernel.org List-ID: If userspace creates and destroys multiple VMs within the same process we leak 20k of memory in the userspace process context per VM. This patch frees the memory in kvm_arch_destroy_vm. If the process exits without closing the VM file descriptor or the file descriptor has been shared with another process then we don't need to free the memory. Signed-off-by: Andrew Honig --- arch/x86/kvm/x86.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index e172132..e93e16b 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -6811,6 +6811,23 @@ void kvm_arch_sync_events(struct kvm *kvm) void kvm_arch_destroy_vm(struct kvm *kvm) { + if (current->mm == kvm->mm) { + /* + * Free memory regions allocated on behalf of userspace, + * unless the the memory map has changed due to process exit + * or fd copying. + */ + struct kvm_userspace_memory_region mem; + memset(&mem, 0, sizeof(mem)); + mem.slot = APIC_ACCESS_PAGE_PRIVATE_MEMSLOT; + kvm_set_memory_region(kvm, &mem, 0); + + mem.slot = IDENTITY_PAGETABLE_PRIVATE_MEMSLOT; + kvm_set_memory_region(kvm, &mem, 0); + + mem.slot = TSS_PRIVATE_MEMSLOT; + kvm_set_memory_region(kvm, &mem, 0); + } kvm_iommu_unmap_guest(kvm); kfree(kvm->arch.vpic); kfree(kvm->arch.vioapic); -- 1.7.10.4