Received: by 2002:a05:6a10:f3d0:0:0:0:0 with SMTP id a16csp2834232pxv; Mon, 12 Jul 2021 03:06:06 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwfqcwm1QwLCZCHjL++yDX6eIjji7oQLCKEv1yHwsWA6QYyKowpEhU34brtKD+D1zLurrc9 X-Received: by 2002:a92:cac5:: with SMTP id m5mr10949916ilq.112.1626084366105; Mon, 12 Jul 2021 03:06:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1626084366; cv=none; d=google.com; s=arc-20160816; b=ytuKkrbgkQHD2+78CKl0Q9z0uFwE7/eGrJbBpRLRrn7eoOFjlmoqcAB8ukiEeqafAo bGJTklNYDWKNshy3I3yDcdsHvP0c47N9JcZ4eoul5mvIyouydQijPtGdjpj/df1Exipd sys7XKSx+nBeoMK0vvGv8GaQwrvcEGwvyh+WPRjxEna9Z5L++fRXTdLjzj4Ds1lHUINz 6AiW6XuOGVutz8g8bpFZ/ohh18cAniewtqr46td9h06k/aw5/bZ/O/KfTKExBHF6nMis DoiCL9WUgSJtBAK/TwwSvKDfXFf9Oz93z+umaI9NynRNxKWMNX/9Mp6Q+TpncD8VQFVe 6fzQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=zIZkNQuYk05qbA1Jju9Sv02T1D+gM5WpjoxXOp1lGT8=; b=sWNA47ZT/jZB4Qo2H6r9NxZTr2YUZqkSF6Z/n32t/Ax7ALP4wLu5/6D+Hqxq3n2dgC QsENDcMwb72d0mRn2wQl/qzMZXzW1rrqN1oiKr/GcpGDQJ4aWKvCHMSn0GWUyQ1HrB0B ac6xxVFKgZuUKYMShexj6NfmjnIjpe7YwNSMHp33Pj2bjDyE2AJUcc4zatD4Dj3dw1Xy fjk7H1lZaXPyH9yQmTS7US8oGmN2Y7Cd9xMuP4/mqvJqxcmoMnXCXGONzWdXrNWUrz1h OBCPvMBjpgVgLdMad9g65Pw3SF48/1z/YheFI0KNRAhzAB+vC8HKuRXPVP7UCg1gBcVM uLDg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=1v7G3cWY; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id x6si17350706iol.30.2021.07.12.03.05.54; Mon, 12 Jul 2021 03:06:06 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=1v7G3cWY; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345607AbhGLHia (ORCPT + 99 others); Mon, 12 Jul 2021 03:38:30 -0400 Received: from mail.kernel.org ([198.145.29.99]:41186 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244172AbhGLHK2 (ORCPT ); Mon, 12 Jul 2021 03:10:28 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 114C9613C8; Mon, 12 Jul 2021 07:06:48 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1626073609; bh=h0Cn5GhxLzXOPtw4B1Llgz6v+xAvLX5jaF4saKaAz1s=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=1v7G3cWYm4kuj7wTyT3FQOl1O9TNQ2rZBRDvZv/2ehMjvuarLua8ULIVuGmpYc8Yo 6TJGlMQquPOdxfH0/JgQ0dBlFgN7yRmU46EPkI7tZ0qP68moXI13yGBC1A7ZwyWadd O0+eaMnJVXxkkARIGWRNAEV08fPKbpyYP0jUYnQ0= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Sean Christopherson , Paolo Bonzini , Sasha Levin Subject: [PATCH 5.12 297/700] KVM: nVMX: Sync all PGDs on nested transition with shadow paging Date: Mon, 12 Jul 2021 08:06:20 +0200 Message-Id: <20210712061007.920187076@linuxfoundation.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210712060924.797321836@linuxfoundation.org> References: <20210712060924.797321836@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Sean Christopherson [ Upstream commit 07ffaf343e34b555c9e7ea39a9c81c439a706f13 ] Trigger a full TLB flush on behalf of the guest on nested VM-Enter and VM-Exit when VPID is disabled for L2. kvm_mmu_new_pgd() syncs only the current PGD, which can theoretically leave stale, unsync'd entries in a previous guest PGD, which could be consumed if L2 is allowed to load CR3 with PCID_NOFLUSH=1. Rename KVM_REQ_HV_TLB_FLUSH to KVM_REQ_TLB_FLUSH_GUEST so that it can be utilized for its obvious purpose of emulating a guest TLB flush. Note, there is no change the actual TLB flush executed by KVM, even though the fast PGD switch uses KVM_REQ_TLB_FLUSH_CURRENT. When VPID is disabled for L2, vpid02 is guaranteed to be '0', and thus nested_get_vpid02() will return the VPID that is shared by L1 and L2. Generate the request outside of kvm_mmu_new_pgd(), as getting the common helper to correctly identify which requested is needed is quite painful. E.g. using KVM_REQ_TLB_FLUSH_GUEST when nested EPT is in play is wrong as a TLB flush from the L1 kernel's perspective does not invalidate EPT mappings. And, by using KVM_REQ_TLB_FLUSH_GUEST, nVMX can do future simplification by moving the logic into nested_vmx_transition_tlb_flush(). Fixes: 41fab65e7c44 ("KVM: nVMX: Skip MMU sync on nested VMX transition when possible") Signed-off-by: Sean Christopherson Message-Id: <20210609234235.1244004-2-seanjc@google.com> Signed-off-by: Paolo Bonzini Signed-off-by: Sasha Levin --- arch/x86/include/asm/kvm_host.h | 2 +- arch/x86/kvm/hyperv.c | 2 +- arch/x86/kvm/vmx/nested.c | 17 ++++++++++++----- arch/x86/kvm/x86.c | 2 +- 4 files changed, 15 insertions(+), 8 deletions(-) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index 0702adf2460b..0758ff3008c6 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -85,7 +85,7 @@ #define KVM_REQ_APICV_UPDATE \ KVM_ARCH_REQ_FLAGS(25, KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP) #define KVM_REQ_TLB_FLUSH_CURRENT KVM_ARCH_REQ(26) -#define KVM_REQ_HV_TLB_FLUSH \ +#define KVM_REQ_TLB_FLUSH_GUEST \ KVM_ARCH_REQ_FLAGS(27, KVM_REQUEST_NO_WAKEUP) #define KVM_REQ_APF_READY KVM_ARCH_REQ(28) #define KVM_REQ_MSR_FILTER_CHANGED KVM_ARCH_REQ(29) diff --git a/arch/x86/kvm/hyperv.c b/arch/x86/kvm/hyperv.c index f00830e5202f..fdd1eca717fd 100644 --- a/arch/x86/kvm/hyperv.c +++ b/arch/x86/kvm/hyperv.c @@ -1704,7 +1704,7 @@ static u64 kvm_hv_flush_tlb(struct kvm_vcpu *vcpu, u64 ingpa, u16 rep_cnt, bool * vcpu->arch.cr3 may not be up-to-date for running vCPUs so we can't * analyze it here, flush TLB regardless of the specified address space. */ - kvm_make_vcpus_request_mask(kvm, KVM_REQ_HV_TLB_FLUSH, + kvm_make_vcpus_request_mask(kvm, KVM_REQ_TLB_FLUSH_GUEST, NULL, vcpu_mask, &hv_vcpu->tlb_flush); ret_success: diff --git a/arch/x86/kvm/vmx/nested.c b/arch/x86/kvm/vmx/nested.c index 8cb5a95e0c54..eca3db08d183 100644 --- a/arch/x86/kvm/vmx/nested.c +++ b/arch/x86/kvm/vmx/nested.c @@ -1132,12 +1132,19 @@ static int nested_vmx_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3, bool ne /* * Unconditionally skip the TLB flush on fast CR3 switch, all TLB - * flushes are handled by nested_vmx_transition_tlb_flush(). See - * nested_vmx_transition_mmu_sync for details on skipping the MMU sync. + * flushes are handled by nested_vmx_transition_tlb_flush(). */ - if (!nested_ept) - kvm_mmu_new_pgd(vcpu, cr3, true, - !nested_vmx_transition_mmu_sync(vcpu)); + if (!nested_ept) { + kvm_mmu_new_pgd(vcpu, cr3, true, true); + + /* + * A TLB flush on VM-Enter/VM-Exit flushes all linear mappings + * across all PCIDs, i.e. all PGDs need to be synchronized. + * See nested_vmx_transition_mmu_sync() for more details. + */ + if (nested_vmx_transition_mmu_sync(vcpu)) + kvm_make_request(KVM_REQ_TLB_FLUSH_GUEST, vcpu); + } vcpu->arch.cr3 = cr3; kvm_register_mark_available(vcpu, VCPU_EXREG_CR3); diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index d46a6182d0e9..615dd236e842 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -9022,7 +9022,7 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu) } if (kvm_check_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu)) kvm_vcpu_flush_tlb_current(vcpu); - if (kvm_check_request(KVM_REQ_HV_TLB_FLUSH, vcpu)) + if (kvm_check_request(KVM_REQ_TLB_FLUSH_GUEST, vcpu)) kvm_vcpu_flush_tlb_guest(vcpu); if (kvm_check_request(KVM_REQ_REPORT_TPR_ACCESS, vcpu)) { -- 2.30.2