From patchwork Thu Apr 18 16:38:14 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Honig X-Patchwork-Id: 2461541 Return-Path: X-Original-To: patchwork-kvm@patchwork.kernel.org Delivered-To: patchwork-process-083081@patchwork2.kernel.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by patchwork2.kernel.org (Postfix) with ESMTP id E0802DF2E5 for ; Thu, 18 Apr 2013 16:38:20 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S936197Ab3DRQiR (ORCPT ); Thu, 18 Apr 2013 12:38:17 -0400 Received: from mail-vc0-f202.google.com ([209.85.220.202]:39430 "EHLO mail-vc0-f202.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932233Ab3DRQiQ (ORCPT ); Thu, 18 Apr 2013 12:38:16 -0400 Received: by mail-vc0-f202.google.com with SMTP id m16so310787vca.5 for ; Thu, 18 Apr 2013 09:38:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=x-received:date:from:x-x-sender:to:subject:message-id:user-agent :mime-version:content-type; bh=ZJ4slx0wFFdNWwlXzEhHdvZ2P3Dq9a1MRKyCkiNXV4c=; b=ahz4IKr2z3S/6QE0f+PfqKpnxyiuiVnHOJwqn/n75jiWo9zP9yKSFwCVUy1rwleI9S xFt8XgAo2lJm5xYrhPeM9Ek5Ty8zEBYoIZQPI22DOwb8N9A42I4bxOXC8cMP/m+wFBrX mk7B7bJHTbHm+2Iy8BOVMBW0GwuBnu/i90OsERrHARHJqegAADFy0+anjroWMc5HKl4E CuXqfvoai1pDljnpcyBQGIt148WqLl05aWvPxPTViyJbyp9sO3q3BtwN3Xgt3LrtDawY xDWUZM4U6TCvcCjJsysmJrxczZHmmYVvGwTkjwRaeLYut7bI1dzmkl7VQE8xmX4/Gow4 xISQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=x-received:date:from:x-x-sender:to:subject:message-id:user-agent :mime-version:content-type:x-gm-message-state; bh=ZJ4slx0wFFdNWwlXzEhHdvZ2P3Dq9a1MRKyCkiNXV4c=; b=C3iJVILGJQlTEjHZTHmWYZqJnX87DrPP0ZDasZ4IUFSrR87+yeH2e2PxcmpDc/lj4y K7h5IxZ+MZit/XNcGrA5zXMAd7FXjQ74pER9TRSxUsUUqGJlxkZk5Li1vs1a6YzsdtlT hlc97XaW5pQ5ATntKaV0YkTaVi8mMKW4qbYlLdQSUUHEKs0g3Tp/raqqdB8OKymU95ls PssNUwe7k5sKmx+R6vl+Cizljbg03VNjZCJPp6JIB3SftTGI0MWSMRFp999QxBG0ET8d REjSkzITQjhVA3TXnZXVOnxvBOTRNkrgcruv8y/8IERxxDKBQbw8bskwn2gJlovK4nY5 Tp2Q== X-Received: by 10.236.203.134 with SMTP id f6mr5800094yho.46.1366303095856; Thu, 18 Apr 2013 09:38:15 -0700 (PDT) Received: from corp2gmr1-1.hot.corp.google.com (corp2gmr1-1.hot.corp.google.com [172.24.189.92]) by gmr-mx.google.com with ESMTPS id c11si765878yhi.3.2013.04.18.09.38.15 (version=TLSv1.1 cipher=AES128-SHA bits=128/128); Thu, 18 Apr 2013 09:38:15 -0700 (PDT) Received: from dhcp-192-168-61-190.mtv.corp.google.com (dhcp-172-31-1-189.kir.corp.google.com [172.31.1.189]) by corp2gmr1-1.hot.corp.google.com (Postfix) with ESMTPS id 6437D31C2EA; Thu, 18 Apr 2013 09:38:15 -0700 (PDT) Date: Thu, 18 Apr 2013 09:38:14 -0700 (PDT) From: Andrew Honig X-X-Sender: ahonig@ahonig-virtual-machine To: ahonig@google.com, kvm@vger.kernel.org Subject: [PATCHv3] KVM: x86: Fix memory leak in vmx.c Message-ID: User-Agent: Alpine 2.02 (DEB 1266 2009-07-14) MIME-Version: 1.0 X-Gm-Message-State: ALoCoQlZ3z8+c26ApVIdL78V1PYF7B8ja5ldI7X+S0OYk1WJc3q8vnhL/zBpZYhHWT32K6rzu/qIcPGOFt2ZTP5UoYRggGYmZ0O/t98xSuRfddj8CYGv2/C2idKTfzUouMBa0A7w/dx287U90UsMMMH9sBl3tWCmY2ZO/i6SrDb5yXHjUvALOCwI2zRxIRvj6Cm8NMF4mSxb Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org 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 free the memory. It's still possible for a user space process to leak memory if the last process to close the fd for the VM is not the process that created it. However, this is an unexpected case that's only caused by a user space process that's misbehaving. Signed-off-by: Andrew Honig Reviewed-by: Gleb Natapov --- 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 8ffac42..3b389bf 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -6856,6 +6856,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); + + mem.slot = IDENTITY_PAGETABLE_PRIVATE_MEMSLOT; + kvm_set_memory_region(kvm, &mem); + + mem.slot = TSS_PRIVATE_MEMSLOT; + kvm_set_memory_region(kvm, &mem); + } kvm_iommu_unmap_guest(kvm); kfree(kvm->arch.vpic); kfree(kvm->arch.vioapic);