From patchwork Sun Oct 17 10:07:40 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nadav Har'El X-Patchwork-Id: 259791 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by demeter1.kernel.org (8.14.4/8.14.3) with ESMTP id o9HA7sNq015752 for ; Sun, 17 Oct 2010 10:07:54 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932352Ab0JQKHp (ORCPT ); Sun, 17 Oct 2010 06:07:45 -0400 Received: from mtagate1.de.ibm.com ([195.212.17.161]:34536 "EHLO mtagate1.de.ibm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932255Ab0JQKHo (ORCPT ); Sun, 17 Oct 2010 06:07:44 -0400 Received: from d12nrmr1607.megacenter.de.ibm.com (d12nrmr1607.megacenter.de.ibm.com [9.149.167.49]) by mtagate1.de.ibm.com (8.13.1/8.13.1) with ESMTP id o9HA7hg3032452 for ; Sun, 17 Oct 2010 10:07:43 GMT Received: from d12av03.megacenter.de.ibm.com (d12av03.megacenter.de.ibm.com [9.149.165.213]) by d12nrmr1607.megacenter.de.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id o9HA7hDI4136992 for ; Sun, 17 Oct 2010 12:07:43 +0200 Received: from d12av03.megacenter.de.ibm.com (loopback [127.0.0.1]) by d12av03.megacenter.de.ibm.com (8.12.11.20060308/8.13.3) with ESMTP id o9HA7g1d023203 for ; Sun, 17 Oct 2010 12:07:43 +0200 Received: from rice.haifa.ibm.com (rice.haifa.ibm.com [9.148.8.112]) by d12av03.megacenter.de.ibm.com (8.12.11.20060308/8.12.11) with ESMTP id o9HA7fNQ023191 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Sun, 17 Oct 2010 12:07:42 +0200 Received: from rice.haifa.ibm.com (lnx-nyh.haifa.ibm.com [127.0.0.1]) by rice.haifa.ibm.com (8.14.4/8.14.4) with ESMTP id o9HA7eIE029385; Sun, 17 Oct 2010 12:07:41 +0200 Received: (from nyh@localhost) by rice.haifa.ibm.com (8.14.4/8.14.4/Submit) id o9HA7e8T029383; Sun, 17 Oct 2010 12:07:40 +0200 Date: Sun, 17 Oct 2010 12:07:40 +0200 Message-Id: <201010171007.o9HA7e8T029383@rice.haifa.ibm.com> X-Authentication-Warning: rice.haifa.ibm.com: nyh set sender to "Nadav Har'El" using -f Cc: gleb@redhat.com, avi@redhat.com To: kvm@vger.kernel.org From: "Nadav Har'El" References: <1287309814-nyh@il.ibm.com> Subject: [PATCH 08/27] nVMX: Hold a vmcs02 for each vmcs12 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Greylist: IP, sender and recipient auto-whitelisted, not delayed by milter-greylist-4.2.3 (demeter1.kernel.org [140.211.167.41]); Sun, 17 Oct 2010 10:07:54 +0000 (UTC) --- .before/arch/x86/kvm/vmx.c 2010-10-17 11:52:00.000000000 +0200 +++ .after/arch/x86/kvm/vmx.c 2010-10-17 11:52:00.000000000 +0200 @@ -155,6 +155,12 @@ struct __packed vmcs12 { */ #define VMCS12_REVISION 0x11e57ed0 +struct vmcs_list { + struct list_head list; + gpa_t vmcs12_addr; + struct vmcs *vmcs02; +}; + /* * The nested_vmx structure is part of vcpu_vmx, and holds information we need * for correct emulation of VMX (i.e., nested VMX) on this vcpu. For example, @@ -170,6 +176,10 @@ struct nested_vmx { /* The host-usable pointer to the above */ struct page *current_vmcs12_page; struct vmcs12 *current_vmcs12; + + /* list of real (hardware) VMCS, one for each L2 guest of L1 */ + struct list_head vmcs02_list; /* a vmcs_list */ + int vmcs02_num; }; struct vcpu_vmx { @@ -1738,6 +1748,85 @@ static void free_vmcs(struct vmcs *vmcs) free_pages((unsigned long)vmcs, vmcs_config.order); } +static struct vmcs *nested_get_current_vmcs(struct kvm_vcpu *vcpu) +{ + struct vcpu_vmx *vmx = to_vmx(vcpu); + struct vmcs_list *list_item, *n; + + list_for_each_entry_safe(list_item, n, &vmx->nested.vmcs02_list, list) + if (list_item->vmcs12_addr == vmx->nested.current_vmptr) + return list_item->vmcs02; + + return NULL; +} + +/* + * Allocate an L0 VMCS (vmcs02) for the current L1 VMCS (vmcs12), if one + * does not already exist. The allocation is done in L0 memory, so to avoid + * denial-of-service attack by guests, we limit the number of concurrently- + * allocated vmcss. A well-behaving L1 will VMCLEAR unused vmcs12s and not + * trigger this limit. + */ +static const int NESTED_MAX_VMCS = 256; +static int nested_create_current_vmcs(struct kvm_vcpu *vcpu) +{ + struct vmcs_list *new_l2_guest; + struct vmcs *vmcs02; + + if (nested_get_current_vmcs(vcpu)) + return 0; /* nothing to do - we already have a VMCS */ + + if (to_vmx(vcpu)->nested.vmcs02_num >= NESTED_MAX_VMCS) + return -ENOMEM; + + new_l2_guest = (struct vmcs_list *) + kmalloc(sizeof(struct vmcs_list), GFP_KERNEL); + if (!new_l2_guest) + return -ENOMEM; + + vmcs02 = alloc_vmcs(); + if (!vmcs02) { + kfree(new_l2_guest); + return -ENOMEM; + } + + new_l2_guest->vmcs12_addr = to_vmx(vcpu)->nested.current_vmptr; + new_l2_guest->vmcs02 = vmcs02; + list_add(&(new_l2_guest->list), &(to_vmx(vcpu)->nested.vmcs02_list)); + to_vmx(vcpu)->nested.vmcs02_num++; + return 0; +} + +/* Free a vmcs12's associated vmcs02, and remove it from vmcs02_list */ +static void nested_free_vmcs(struct kvm_vcpu *vcpu, gpa_t vmptr) +{ + struct vcpu_vmx *vmx = to_vmx(vcpu); + struct vmcs_list *list_item, *n; + + list_for_each_entry_safe(list_item, n, &vmx->nested.vmcs02_list, list) + if (list_item->vmcs12_addr == vmptr) { + free_vmcs(list_item->vmcs02); + list_del(&(list_item->list)); + kfree(list_item); + vmx->nested.vmcs02_num--; + return; + } +} + +static void free_l1_state(struct kvm_vcpu *vcpu) +{ + struct vcpu_vmx *vmx = to_vmx(vcpu); + struct vmcs_list *list_item, *n; + + list_for_each_entry_safe(list_item, n, + &vmx->nested.vmcs02_list, list) { + free_vmcs(list_item->vmcs02); + list_del(&(list_item->list)); + kfree(list_item); + } + vmx->nested.vmcs02_num = 0; +} + static void free_kvm_area(void) { int cpu; @@ -3594,6 +3683,9 @@ static int handle_vmon(struct kvm_vcpu * return 1; } + INIT_LIST_HEAD(&(vmx->nested.vmcs02_list)); + vmx->nested.vmcs02_num = 0; + vmx->nested.vmxon = true; skip_emulated_instruction(vcpu); @@ -3643,6 +3735,8 @@ static int handle_vmoff(struct kvm_vcpu nested_release_page(to_vmx(vcpu)->nested.current_vmcs12_page); } + free_l1_state(vcpu); + skip_emulated_instruction(vcpu); return 1; } @@ -4409,6 +4503,8 @@ static void vmx_free_vcpu(struct kvm_vcp kunmap(to_vmx(vcpu)->nested.current_vmcs12_page); nested_release_page(to_vmx(vcpu)->nested.current_vmcs12_page); } + if (vmx->nested.vmxon) + free_l1_state(vcpu); vmx_free_vmcs(vcpu); kfree(vmx->guest_msrs); kvm_vcpu_uninit(vcpu);