From patchwork Thu Sep 6 13:32:25 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Liran Alon X-Patchwork-Id: 10590633 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 054DE5A4 for ; Thu, 6 Sep 2018 13:33:05 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id F04892A2DF for ; Thu, 6 Sep 2018 13:33:04 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id E4A0A2A3DA; Thu, 6 Sep 2018 13:33:04 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI, UNPARSEABLE_RELAY autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 84AFD2A2DF for ; Thu, 6 Sep 2018 13:33:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729614AbeIFSIg (ORCPT ); Thu, 6 Sep 2018 14:08:36 -0400 Received: from userp2130.oracle.com ([156.151.31.86]:58448 "EHLO userp2130.oracle.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729609AbeIFSIg (ORCPT ); Thu, 6 Sep 2018 14:08:36 -0400 Received: from pps.filterd (userp2130.oracle.com [127.0.0.1]) by userp2130.oracle.com (8.16.0.22/8.16.0.22) with SMTP id w86DTRo9068910; Thu, 6 Sep 2018 13:32:55 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : cc : subject : date : message-id : in-reply-to : references; s=corp-2018-07-02; bh=kCk3EEaY60H61MfVQ9ca92VBSxtnyJ0q2i+IDrxPfeM=; b=OmN2OxpmO4BppMdF2tANf6s2adMSKYejneMG13G0H4tv4vz9OIGdNoJWI9CSvkCyS8oc xrh6uQT6EmLkHRefTtqQscw8oooX+kXegy4AWuAbeMH42bEI7byjopBROwSSuauPogg5 zoijVqSunn0493t1AKzBTR/fH54hcEXANZNNlH7s4y5BPJFMeJf2fnPm8s2rXpmlppVG ntRhLna3EHIwHcaTqxI5SSpACeLq6fJHKXMDTTuiAg/w7LwgZ/YPuo1Mgorxp9EIR9Tn lHNcTcqsqZrPChh34tOLEZkvh3Sy2XCOB4XAkB+bRIFTT+tkFUiBHAiNzQYRyrbx5gHp Mw== Received: from aserv0021.oracle.com (aserv0021.oracle.com [141.146.126.233]) by userp2130.oracle.com with ESMTP id 2m7j6tv64d-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 06 Sep 2018 13:32:55 +0000 Received: from aserv0121.oracle.com (aserv0121.oracle.com [141.146.126.235]) by aserv0021.oracle.com (8.14.4/8.14.4) with ESMTP id w86DWnii018633 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 6 Sep 2018 13:32:49 GMT Received: from abhmp0009.oracle.com (abhmp0009.oracle.com [141.146.116.15]) by aserv0121.oracle.com (8.14.4/8.13.8) with ESMTP id w86DWnB6024967; Thu, 6 Sep 2018 13:32:49 GMT Received: from spark.ravello.local (/213.57.127.2) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Thu, 06 Sep 2018 06:32:49 -0700 From: Liran Alon To: pbonzini@redhat.com, rkrcmar@redhat.com, kvm@vger.kernel.org Cc: jmattson@google.com, idan.brown@oracle.com, Liran Alon Subject: [PATCH 1/4] KVM: nVMX: Flush TLB entries tagged by dest EPTP on L1<->L2 transitions Date: Thu, 6 Sep 2018 16:32:25 +0300 Message-Id: <20180906133228.118282-2-liran.alon@oracle.com> X-Mailer: git-send-email 2.16.1 In-Reply-To: <20180906133228.118282-1-liran.alon@oracle.com> References: <20180906133228.118282-1-liran.alon@oracle.com> X-Proofpoint-Virus-Version: vendor=nai engine=5900 definitions=9007 signatures=668708 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 suspectscore=0 malwarescore=0 phishscore=0 bulkscore=0 spamscore=0 mlxscore=0 mlxlogscore=553 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1807170000 definitions=main-1809060137 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP If L1 and L2 share VPID (because L1 don't use VPID or we haven't allocated a vpid02), we need to flush TLB on L1<->L2 transitions. Before this patch, this TLB flushing was done by vmx_flush_tlb(). If L0 use EPT, this will translate into INVEPT(active_eptp); However, if L1 use EPT, in L1->L2 VMEntry, active EPTP is EPTP01 but TLB entries populated by L2 are tagged with EPTP02. Therefore we should delay vmx_flush_tlb() until active_eptp is EPTP02. To achieve this, instead of directly calling vmx_flush_tlb() we request it to be called by KVM_REQ_TLB_FLUSH which is evaluated after KVM_REQ_LOAD_CR3 which sets the active_eptp to EPTP02 as required. Similarly, on L2->L1 VMExit, active EPTP is EPTP02 but TLB entries populated by L1 are tagged with EPTP01 and therefore we should delay vmx_flush_tlb() until active_eptp is EPTP01. Reviewed-by: Mihai Carabas Reviewed-by: Darren Kenny Reviewed-by: Nikita Leshchenko Signed-off-by: Liran Alon --- arch/x86/kvm/vmx.c | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/arch/x86/kvm/vmx.c b/arch/x86/kvm/vmx.c index bed75118111c..7dcf5e96039d 100644 --- a/arch/x86/kvm/vmx.c +++ b/arch/x86/kvm/vmx.c @@ -12245,7 +12245,15 @@ static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12, __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true); } } else { - vmx_flush_tlb(vcpu, true); + /* + * If L1 use EPT, then L0 needs to execute INVEPT on + * EPTP02 instead of EPTP01. Therefore, delay TLB + * flush until vmcs02->eptp is fully updated by + * KVM_REQ_LOAD_CR3. Note that this assumes + * KVM_REQ_TLB_FLUSH is evaluated after + * KVM_REQ_LOAD_CR3 in vcpu_enter_guest(). + */ + kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu); } } @@ -13131,10 +13139,14 @@ static void load_vmcs12_host_state(struct kvm_vcpu *vcpu, * Therefore, flush TLB only in case vmcs01 uses VPID and * vmcs12 don't use VPID as in this case L1 & L2 TLB entries * are both tagged with vmx->vpid. + * + * If vmcs12 uses EPT, we need to execute this flush on EPTP01 + * and therefore we request the TLB flush to happen only after VMCS EPTP + * has been set by KVM_REQ_LOAD_CR3. */ if (enable_vpid && - !(nested_cpu_has_vpid(vmcs12) && to_vmx(vcpu)->nested.vpid02)) { - vmx_flush_tlb(vcpu, true); + !(nested_cpu_has_vpid(vmcs12) && to_vmx(vcpu)->nested.vpid02)) { + kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu); } vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs); From patchwork Thu Sep 6 13:32:26 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Liran Alon X-Patchwork-Id: 10590631 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 5C454180E for ; Thu, 6 Sep 2018 13:33:04 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 51D4B2A2DF for ; Thu, 6 Sep 2018 13:33:04 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 4617B2A3DE; Thu, 6 Sep 2018 13:33:04 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI, UNPARSEABLE_RELAY autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 651842A3DA for ; Thu, 6 Sep 2018 13:33:02 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729608AbeIFSIe (ORCPT ); Thu, 6 Sep 2018 14:08:34 -0400 Received: from userp2130.oracle.com ([156.151.31.86]:58390 "EHLO userp2130.oracle.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728826AbeIFSIe (ORCPT ); Thu, 6 Sep 2018 14:08:34 -0400 Received: from pps.filterd (userp2130.oracle.com [127.0.0.1]) by userp2130.oracle.com (8.16.0.22/8.16.0.22) with SMTP id w86DTRh2068905; Thu, 6 Sep 2018 13:32:53 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : cc : subject : date : message-id : in-reply-to : references; s=corp-2018-07-02; bh=LOYNO825qHbJkMWDgkrps1f590QhbyeNL7pxD/s3cT8=; b=L/4/GESiIHIFgHcRT80AgqSPOZW7ccRxEI+xYZdCtopSfZzaW2ABWfIstIoJ1tR3HxI6 z8J4BmnizGJ2OH/3Q2DhH88INoeXgEyEBKGolGq9+hgjJk3bm6NweSDYyV3RyoKgtY6j trj+j/csgLYjBKtVUs30FGg+34nlBQ01oiAJvJeOidcrhW7Acqhvqbo6crRNxx8s4ESY JSn506atypJgvX7UzpuvUVvjHSu2nEgdW1wik19blc2FtkqSR11LiVkL50B7Iwb1WmnB yHGEQ2TN+WmEgQhTPjZIdzT1LmpAJXRKbd8U/uFv+cjwlFLe9MzuiH0OhW7//nmPB4B0 hA== Received: from aserv0021.oracle.com (aserv0021.oracle.com [141.146.126.233]) by userp2130.oracle.com with ESMTP id 2m7j6tv643-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 06 Sep 2018 13:32:53 +0000 Received: from userv0122.oracle.com (userv0122.oracle.com [156.151.31.75]) by aserv0021.oracle.com (8.14.4/8.14.4) with ESMTP id w86DWq9C018817 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 6 Sep 2018 13:32:52 GMT Received: from abhmp0009.oracle.com (abhmp0009.oracle.com [141.146.116.15]) by userv0122.oracle.com (8.14.4/8.14.4) with ESMTP id w86DWpFF005315; Thu, 6 Sep 2018 13:32:52 GMT Received: from spark.ravello.local (/213.57.127.2) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Thu, 06 Sep 2018 06:32:51 -0700 From: Liran Alon To: pbonzini@redhat.com, rkrcmar@redhat.com, kvm@vger.kernel.org Cc: jmattson@google.com, idan.brown@oracle.com, Liran Alon Subject: [PATCH 2/4] KVM: nVMX: Do not flush TLB on L2->L1 VMExit if L1 uses VPID and EPT Date: Thu, 6 Sep 2018 16:32:26 +0300 Message-Id: <20180906133228.118282-3-liran.alon@oracle.com> X-Mailer: git-send-email 2.16.1 In-Reply-To: <20180906133228.118282-1-liran.alon@oracle.com> References: <20180906133228.118282-1-liran.alon@oracle.com> X-Proofpoint-Virus-Version: vendor=nai engine=5900 definitions=9007 signatures=668708 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 suspectscore=0 malwarescore=0 phishscore=0 bulkscore=0 spamscore=0 mlxscore=0 mlxlogscore=819 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1807170000 definitions=main-1809060137 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP If L1 uses VPID, it expects TLB to not be flushed on L2->L1 VMExit. However, code currently flushes TLB nonetheless if we didn't allocate a vpid02 for L2. As in this case, vmcs02->vpid == vmcs01->vpid == vmx->vpid. But, if L1 uses EPT, TLB entires populated by L2 are tagged with EPTP02 while TLB entries populated by L1 are tagged with EPTP01. Therefore, we can also avoid TLB flush if L1 uses VPID and EPT. Reviewed-by: Mihai Carabas Reviewed-by: Darren Kenny Reviewed-by: Nikita Leshchenko Signed-off-by: Liran Alon --- arch/x86/kvm/vmx.c | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/arch/x86/kvm/vmx.c b/arch/x86/kvm/vmx.c index 7dcf5e96039d..b97e0c5ccb46 100644 --- a/arch/x86/kvm/vmx.c +++ b/arch/x86/kvm/vmx.c @@ -13132,20 +13132,26 @@ static void load_vmcs12_host_state(struct kvm_vcpu *vcpu, * If vmcs01 don't use VPID, CPU flushes TLB on every * VMEntry/VMExit. Thus, no need to flush TLB. * - * If vmcs12 uses VPID, TLB entries populated by L2 are - * tagged with vmx->nested.vpid02 while L1 entries are tagged - * with vmx->vpid. Thus, no need to flush TLB. + * If vmcs12 uses VPID then: + * (*) If vmcs12 uses EPT, TLB entries populated by L2 are tagged + * with EPTP02 which is different than EPTP01 which is used to tag + * TLB entries populated by L1. Therefore, no need to flush TLB. + * (*) Otherwise, TLB entries populated by L2 are tagged with + * vmcs02->vpid02 which is either vmx->nested.vpid02 or vmx->vpid. + * Because vmx->nested.vpid02 is different than vmx->vpid used to + * tag L1 TLB entries, no need to flush TLB in case we have + * vmx->nested.vpid02. Otherwise, both L1 and L2 TLB entries are + * tagged with vmx->vpid and therefore we must flush TLB. * - * Therefore, flush TLB only in case vmcs01 uses VPID and - * vmcs12 don't use VPID as in this case L1 & L2 TLB entries - * are both tagged with vmx->vpid. + * If vmcs12 don't use VPID, we must flush TLB. * * If vmcs12 uses EPT, we need to execute this flush on EPTP01 * and therefore we request the TLB flush to happen only after VMCS EPTP * has been set by KVM_REQ_LOAD_CR3. */ if (enable_vpid && - !(nested_cpu_has_vpid(vmcs12) && to_vmx(vcpu)->nested.vpid02)) { + (!nested_cpu_has_vpid(vmcs12) || + (!nested_cpu_has_ept(vmcs12) && !to_vmx(vcpu)->nested.vpid02))) { kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu); } From patchwork Thu Sep 6 13:32:27 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Liran Alon X-Patchwork-Id: 10590635 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 68DFE5A4 for ; Thu, 6 Sep 2018 13:33:10 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 5F3C52A2DF for ; Thu, 6 Sep 2018 13:33:10 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 514E92A3DE; Thu, 6 Sep 2018 13:33:10 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI, UNPARSEABLE_RELAY autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 3781A2A2DF for ; Thu, 6 Sep 2018 13:33:09 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729619AbeIFSIl (ORCPT ); Thu, 6 Sep 2018 14:08:41 -0400 Received: from aserp2120.oracle.com ([141.146.126.78]:52990 "EHLO aserp2120.oracle.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729565AbeIFSIl (ORCPT ); Thu, 6 Sep 2018 14:08:41 -0400 Received: from pps.filterd (aserp2120.oracle.com [127.0.0.1]) by aserp2120.oracle.com (8.16.0.22/8.16.0.22) with SMTP id w86DTSQG099092; Thu, 6 Sep 2018 13:33:00 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : cc : subject : date : message-id : in-reply-to : references; s=corp-2018-07-02; bh=OpT8MogXjPS89S20czZDevIq1d21uEJORWJXZXxMJuY=; b=zfkAcTknSdTPBfJbkTTk77ion1BUVjvz6DN2r9XG9AwFD33xlo7cXphK8DLYVTp3HgSX 85SloORlLbn3oSSyM3gsK6RJD8gy5cWDO5xSfixoLD/4Tpqa7+AZz8H+MGfR97RTgI/H 6zNlpv8P/FWxVxhkQ0EE11qBEj/VDmDtWZUio7Ay7nPX6v5nkdarASlTvfZXl9ZMpIhw aZp34XcsBitix6jJUc8tkXLhk5k9p1v6tB/BcWtaUE0WPcFY+r3VcNBqyRoTlKcBCIzF XabQdVYgaklnW77vqYjyjhEKWnTbyqE+3ExiqY/W0JmPnEFN5uaWvD/P4m0Sy8H8kxpI bw== Received: from userv0022.oracle.com (userv0022.oracle.com [156.151.31.74]) by aserp2120.oracle.com with ESMTP id 2m7jqpv5c6-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 06 Sep 2018 13:33:00 +0000 Received: from aserv0121.oracle.com (aserv0121.oracle.com [141.146.126.235]) by userv0022.oracle.com (8.14.4/8.14.4) with ESMTP id w86DWs8E013815 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 6 Sep 2018 13:32:54 GMT Received: from abhmp0009.oracle.com (abhmp0009.oracle.com [141.146.116.15]) by aserv0121.oracle.com (8.14.4/8.13.8) with ESMTP id w86DWsmg024992; Thu, 6 Sep 2018 13:32:54 GMT Received: from spark.ravello.local (/213.57.127.2) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Thu, 06 Sep 2018 06:32:53 -0700 From: Liran Alon To: pbonzini@redhat.com, rkrcmar@redhat.com, kvm@vger.kernel.org Cc: jmattson@google.com, idan.brown@oracle.com, Liran Alon Subject: [PATCH 3/4] KVM: nVMX: Set vmcs02->vpid to vmcs12->vpid if L1 uses EPT Date: Thu, 6 Sep 2018 16:32:27 +0300 Message-Id: <20180906133228.118282-4-liran.alon@oracle.com> X-Mailer: git-send-email 2.16.1 In-Reply-To: <20180906133228.118282-1-liran.alon@oracle.com> References: <20180906133228.118282-1-liran.alon@oracle.com> X-Proofpoint-Virus-Version: vendor=nai engine=5900 definitions=9007 signatures=668708 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 suspectscore=0 malwarescore=0 phishscore=0 bulkscore=0 spamscore=0 mlxscore=0 mlxlogscore=666 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1807170000 definitions=main-1809060137 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP If CPU use both VPID and EPT, TLB entries populated by CPU are tagged with both EPTP and VPID. Therefore, if L1 uses EPT, L2 TLB entries are separated from L1 TLB entries by the EPTP tags as vmcs02 use EPTP02 while vmcs01 use EPTP01. Thus, we don't need to make sure that vmcs02->vpid != vmcs01->vpid. Therefore, we can just set vmcs02->vpid to vmcs12->vpid. Reviewed-by: Mihai Carabas Reviewed-by: Darren Kenny Reviewed-by: Nikita Leshchenko Signed-off-by: Liran Alon Reviewed-by: Jim Mattson --- arch/x86/kvm/vmx.c | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/arch/x86/kvm/vmx.c b/arch/x86/kvm/vmx.c index b97e0c5ccb46..98faba65c24a 100644 --- a/arch/x86/kvm/vmx.c +++ b/arch/x86/kvm/vmx.c @@ -12018,10 +12018,20 @@ static void prepare_vmcs02_full(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12) vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs); if (enable_vpid) { - if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02) - vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02); - else - vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid); + u16 vmcs02_vpid; + + if (nested_cpu_has_vpid(vmcs12)) { + if (nested_cpu_has_ept(vmcs12)) + vmcs02_vpid = vmcs12->virtual_processor_id; + else if (vmx->nested.vpid02) + vmcs02_vpid = vmx->nested.vpid02; + else + vmcs02_vpid = vmx->vpid; + } else { + vmcs02_vpid = vmx->vpid; + } + + vmcs_write16(VIRTUAL_PROCESSOR_ID, vmcs02_vpid); } /* From patchwork Thu Sep 6 13:32:28 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Liran Alon X-Patchwork-Id: 10590637 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id DF56713BB for ; Thu, 6 Sep 2018 13:33:13 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id D4E8D2A2DF for ; Thu, 6 Sep 2018 13:33:13 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id C87EE2A3DE; Thu, 6 Sep 2018 13:33:13 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI, UNPARSEABLE_RELAY autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 6EBB82A2DF for ; Thu, 6 Sep 2018 13:33:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729618AbeIFSIp (ORCPT ); Thu, 6 Sep 2018 14:08:45 -0400 Received: from userp2120.oracle.com ([156.151.31.85]:60774 "EHLO userp2120.oracle.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729616AbeIFSIp (ORCPT ); Thu, 6 Sep 2018 14:08:45 -0400 Received: from pps.filterd (userp2120.oracle.com [127.0.0.1]) by userp2120.oracle.com (8.16.0.22/8.16.0.22) with SMTP id w86DTQYn051114; Thu, 6 Sep 2018 13:33:02 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : cc : subject : date : message-id : in-reply-to : references; s=corp-2018-07-02; bh=BZDRA5mgT3SkW4EAE1J61V4KmwJxNpSxk+rWcLEavu4=; b=mS3bd2lrufgS9Uw/3pGPiNYNsKDXeNIYQvGBSx6ewxdSvDqcieanwg4LZKdsSv0vSvQU gk7KRwvsSMRAgAQFmmluQZp2UVwDRVO5vRtheGBb7xtaq49DryzDyjgme3U19Je+vzOE UhQChDXhkzHf6CzBLolmUny96SzWRCpqhb7kWEj43adMBThtm5sfIQVuKU8Qe1F5ojCu 5mkg4sMPmsBwUTZEJR/7VHubJyivMCVd8J1ObJQF4FlGNnTjwucL5QPjkGe+HDjYt4IC gTkjltqmvTedUqTTTRuLAv6FgzyEfi0eVSLSIme06v6ETbml4EyL/RLAjS+5uYuyTwoj yQ== Received: from userv0022.oracle.com (userv0022.oracle.com [156.151.31.74]) by userp2120.oracle.com with ESMTP id 2m7kdqv0m4-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 06 Sep 2018 13:33:02 +0000 Received: from aserv0121.oracle.com (aserv0121.oracle.com [141.146.126.235]) by userv0022.oracle.com (8.14.4/8.14.4) with ESMTP id w86DWuS7014031 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 6 Sep 2018 13:32:57 GMT Received: from abhmp0009.oracle.com (abhmp0009.oracle.com [141.146.116.15]) by aserv0121.oracle.com (8.14.4/8.13.8) with ESMTP id w86DWuc8024996; Thu, 6 Sep 2018 13:32:56 GMT Received: from spark.ravello.local (/213.57.127.2) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Thu, 06 Sep 2018 06:32:56 -0700 From: Liran Alon To: pbonzini@redhat.com, rkrcmar@redhat.com, kvm@vger.kernel.org Cc: jmattson@google.com, idan.brown@oracle.com, Liran Alon Subject: [PATCH 4/4] KVM: nVMX: Do not flush TLB on L1->L2 VMEntry if L1 uses EPT Date: Thu, 6 Sep 2018 16:32:28 +0300 Message-Id: <20180906133228.118282-5-liran.alon@oracle.com> X-Mailer: git-send-email 2.16.1 In-Reply-To: <20180906133228.118282-1-liran.alon@oracle.com> References: <20180906133228.118282-1-liran.alon@oracle.com> X-Proofpoint-Virus-Version: vendor=nai engine=5900 definitions=9007 signatures=668708 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 suspectscore=0 malwarescore=0 phishscore=0 bulkscore=0 spamscore=0 mlxscore=0 mlxlogscore=479 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1807170000 definitions=main-1809060137 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP If L1 uses EPT and VPID, vmcs02->vpid is set to vmcs12->vpid because L2 TLB entries are separated from L1 TLB entries by having different EPTP tags and therefore we don't need a special vpid02 to separate them. Thus, we can avoid flush TLB on L1->L2 VMEntry if L1 uses VPID and EPT. Reviewed-by: Mihai Carabas Reviewed-by: Darren Kenny Reviewed-by: Nikita Leshchenko Signed-off-by: Liran Alon --- arch/x86/kvm/vmx.c | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/arch/x86/kvm/vmx.c b/arch/x86/kvm/vmx.c index 98faba65c24a..bf25317495da 100644 --- a/arch/x86/kvm/vmx.c +++ b/arch/x86/kvm/vmx.c @@ -12241,18 +12241,16 @@ static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12, decache_tsc_multiplier(vmx); if (enable_vpid) { - /* - * There is no direct mapping between vpid02 and vpid12, the - * vpid02 is per-vCPU for L0 and reused while the value of - * vpid12 is changed w/ one invvpid during nested vmentry. - * The vpid12 is allocated by L1 for L2, so it will not - * influence global bitmap(for vpid01 and vpid02 allocation) - * even if spawn a lot of nested vCPUs. - */ - if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02) { - if (vmcs12->virtual_processor_id != vmx->nested.last_vpid) { - vmx->nested.last_vpid = vmcs12->virtual_processor_id; - __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true); + if (nested_cpu_has_vpid(vmcs12)) { + if (!nested_cpu_has_ept(vmcs12)) { + if (vmx->nested.vpid02) { + if (vmcs12->virtual_processor_id != vmx->nested.last_vpid) { + vmx->nested.last_vpid = vmcs12->virtual_processor_id; + __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true); + } + } else { + vmx_flush_tlb(vcpu, true); + } } } else { /*