From patchwork Sun Jun 13 12:26: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: 105787 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by demeter.kernel.org (8.14.3/8.14.3) with ESMTP id o5DCQkc0027150 for ; Sun, 13 Jun 2010 12:26:46 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753363Ab0FMM0o (ORCPT ); Sun, 13 Jun 2010 08:26:44 -0400 Received: from mtagate2.uk.ibm.com ([194.196.100.162]:49861 "EHLO mtagate2.uk.ibm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752196Ab0FMM0o (ORCPT ); Sun, 13 Jun 2010 08:26:44 -0400 Received: from d06nrmr1407.portsmouth.uk.ibm.com (d06nrmr1407.portsmouth.uk.ibm.com [9.149.38.185]) by mtagate2.uk.ibm.com (8.13.1/8.13.1) with ESMTP id o5DCQhO2019463 for ; Sun, 13 Jun 2010 12:26:43 GMT Received: from d06av03.portsmouth.uk.ibm.com (d06av03.portsmouth.uk.ibm.com [9.149.37.213]) by d06nrmr1407.portsmouth.uk.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id o5DCQhKD630948 for ; Sun, 13 Jun 2010 13:26:43 +0100 Received: from d06av03.portsmouth.uk.ibm.com (loopback [127.0.0.1]) by d06av03.portsmouth.uk.ibm.com (8.12.11.20060308/8.13.3) with ESMTP id o5DCQgrT029960 for ; Sun, 13 Jun 2010 13:26:42 +0100 Received: from rice.haifa.ibm.com (rice.haifa.ibm.com [9.148.8.205]) by d06av03.portsmouth.uk.ibm.com (8.12.11.20060308/8.12.11) with ESMTP id o5DCQfIu029949 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Sun, 13 Jun 2010 13:26:42 +0100 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 o5DCQeln012954; Sun, 13 Jun 2010 15:26:40 +0300 Received: (from nyh@localhost) by rice.haifa.ibm.com (8.14.4/8.14.4/Submit) id o5DCQebV012952; Sun, 13 Jun 2010 15:26:40 +0300 Date: Sun, 13 Jun 2010 15:26:40 +0300 Message-Id: <201006131226.o5DCQebV012952@rice.haifa.ibm.com> X-Authentication-Warning: rice.haifa.ibm.com: nyh set sender to "Nadav Har'El" using -f Cc: kvm@vger.kernel.org To: avi@redhat.com From: "Nadav Har'El" References: <1276431753-nyh@il.ibm.com> Subject: [PATCH 8/24] 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 (demeter.kernel.org [140.211.167.41]); Sun, 13 Jun 2010 12:26:46 +0000 (UTC) --- .before/arch/x86/kvm/vmx.c 2010-06-13 15:01:29.000000000 +0300 +++ .after/arch/x86/kvm/vmx.c 2010-06-13 15:01:29.000000000 +0300 @@ -140,6 +140,12 @@ struct __attribute__ ((__packed__)) vmcs u32 abort; }; +struct vmcs_list { + struct list_head list; + gpa_t vmcs_addr; + struct vmcs *l2_vmcs; +}; + /* 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, * the current VMCS set by L1, a list of the VMCSs used to run the active @@ -153,6 +159,10 @@ struct nested_vmx { gpa_t current_vmptr; /* The host-usable pointer to the above. Set by nested_map_current() */ struct vmcs12 *current_l2_page; + + /* list of real (hardware) VMCS, one for each L2 guest of L1 */ + struct list_head l2_vmcs_list; /* a vmcs_list */ + int l2_vmcs_num; }; struct vcpu_vmx { @@ -1754,6 +1764,84 @@ 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.l2_vmcs_list, list) + if (list_item->vmcs_addr == vmx->nested.current_vmptr) + return list_item->l2_vmcs; + + 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 *l2_vmcs; + + if (nested_get_current_vmcs(vcpu)) + return 0; /* nothing to do - we already have a VMCS */ + + if (to_vmx(vcpu)->nested.l2_vmcs_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; + + l2_vmcs = alloc_vmcs(); + if (!l2_vmcs) { + kfree(new_l2_guest); + return -ENOMEM; + } + + new_l2_guest->vmcs_addr = to_vmx(vcpu)->nested.current_vmptr; + new_l2_guest->l2_vmcs = l2_vmcs; + list_add(&(new_l2_guest->list), &(to_vmx(vcpu)->nested.l2_vmcs_list)); + to_vmx(vcpu)->nested.l2_vmcs_num++; + return 0; +} + +/* Free the current L2 VMCS, and remove it from l2_vmcs_list */ +static void nested_free_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.l2_vmcs_list, list) + if (list_item->vmcs_addr == vmx->nested.current_vmptr) { + free_vmcs(list_item->l2_vmcs); + list_del(&(list_item->list)); + kfree(list_item); + vmx->nested.l2_vmcs_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.l2_vmcs_list, list) { + free_vmcs(list_item->l2_vmcs); + list_del(&(list_item->list)); + kfree(list_item); + } + vmx->nested.l2_vmcs_num = 0; +} + static void free_kvm_area(void) { int cpu; @@ -3606,6 +3694,9 @@ static int handle_vmon(struct kvm_vcpu * return 1; } + INIT_LIST_HEAD(&(vmx->nested.l2_vmcs_list)); + vmx->nested.l2_vmcs_num = 0; + vmx->nested.vmxon = 1; skip_emulated_instruction(vcpu); @@ -3650,6 +3741,8 @@ static int handle_vmoff(struct kvm_vcpu to_vmx(vcpu)->nested.vmxon = 0; + free_l1_state(vcpu); + skip_emulated_instruction(vcpu); return 1; } @@ -4402,6 +4495,8 @@ static void vmx_free_vcpu(struct kvm_vcp struct vcpu_vmx *vmx = to_vmx(vcpu); free_vpid(vmx); + if (vmx->nested.vmxon) + free_l1_state(vcpu); vmx_free_vmcs(vcpu); kfree(vmx->guest_msrs); kvm_vcpu_uninit(vcpu);