Received: by 2002:a25:6193:0:0:0:0:0 with SMTP id v141csp3911788ybb; Mon, 23 Mar 2020 09:58:23 -0700 (PDT) X-Google-Smtp-Source: ADFU+vsskHMkPNPCpeGVP2LSWi+3Woej/ECwsEqyO/A5XwSiHeibU8V15B1sHWl613Ca7xefCT5i X-Received: by 2002:a4a:b90e:: with SMTP id x14mr669507ooo.34.1584982703091; Mon, 23 Mar 2020 09:58:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1584982703; cv=none; d=google.com; s=arc-20160816; b=YUGjkcI0IttLjbUDqo2YNEOKNsSBjtzJXzT/F5h6sOdBKF5EMjJtLocblp10OTy2nS ga8WPzlWOZtBTTlc6I1TLBI39Er2g0DTnh5nXFF/FoSEZR3d3l9ylmYZ9f7hv27JhmHW Ppz4gggH7PThWgoc32D6iwSBswXJlD1bVpQOU7iDtASRBxMmz4Y6rRngvxF3PvbZLmHl 1bgfIZ0h9Z3VnCab+OmfYR2XjSOd+7ofJufGpocWgpPmr+mtzVTPoW83cFzoLi5aUqtx 3q0HegTRqPTrr0adGgweFw2w1yWY/0/oqaPyi0X2VcgqQa6ty2TjDJyzPjBJFIuHrNZ6 Sdsw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:message-id:date:references :in-reply-to:subject:cc:to:from:dkim-signature; bh=oCiKY/awUsZ/73HEBO+Ebhw+NCIAIbZULxjjUGIv5cM=; b=crlnxM5zaMbpr48lPk6BvmOgMA4iydxsfdpdHLvXn1BycW9QnmGUwWTgDgc187SAAJ AXGzVIZG+GTPS+4t7m8BqYLW6xgiyaSaSJuCPqLoeGrj6QTTQbS0+/01N6D0oWscHFWk 4WlMjsQsFjYqhKnB3542JZzbj6fbMYamXk3LOtxhtU8xMMxRS4QNQw3utX+1AJ3ggiKu WOmqjIUaZh9hscoKn44LVK7SF22uz1QYT8t6ljKvKgID1kuU909WSYLWx4Y4hojl7Y/R a7QtHuZJSpKFnVHopqjC3Z9XqlvPZbhOXq6a6NzOcy5sZM8xoCU2jcPVI1AILg81fIIJ xSlg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=LJJI9X25; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 145si8284139oie.185.2020.03.23.09.58.10; Mon, 23 Mar 2020 09:58:23 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=LJJI9X25; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727851AbgCWQ5Y (ORCPT + 99 others); Mon, 23 Mar 2020 12:57:24 -0400 Received: from us-smtp-delivery-74.mimecast.com ([63.128.21.74]:49339 "EHLO us-smtp-delivery-74.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727067AbgCWQ5Y (ORCPT ); Mon, 23 Mar 2020 12:57:24 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1584982642; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=oCiKY/awUsZ/73HEBO+Ebhw+NCIAIbZULxjjUGIv5cM=; b=LJJI9X25he9ww4l+OQss/9RcD/lk9pTk1/Q2Fp1rz6tKTMq1ipazhszEBaPIowI3OZEdcF wdMiFAYRXAiE8x6/c410S5MOvO2rplqB/Kd7ofq0Q7AaQsXkAMbpawhd9BoPJRhBp8qzJo pLgHsP1COixqFXs+3/cIx+wwl5Tl22c= Received: from mail-wr1-f70.google.com (mail-wr1-f70.google.com [209.85.221.70]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-440-E7kXS4UQN3KIx_b4ACReAw-1; Mon, 23 Mar 2020 12:57:20 -0400 X-MC-Unique: E7kXS4UQN3KIx_b4ACReAw-1 Received: by mail-wr1-f70.google.com with SMTP id h17so7662691wru.16 for ; Mon, 23 Mar 2020 09:57:20 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:in-reply-to:references:date :message-id:mime-version; bh=oCiKY/awUsZ/73HEBO+Ebhw+NCIAIbZULxjjUGIv5cM=; b=O4Xt0oflkI8WszNFFieiJK5RgKrLQ1jEdRf0vKOqPrGL3g3vEx5PQaYkJZXrG3R6YY m6wLe5GqGfYK79oODVxpAiFfHPximRAfBA601AKGoIDsP+METviJy80vrX1wPx/Fk92o Q5ZSyMmGj/F4Rf7MDsMxl+ncPTV1YPrFFJrxoeKzm0iMs1uHgpZ0IcPeclGsIQ+dv+GD GT6XhRxWjMHKaPd9bzlgr1DO738+SfO1vj2MFJ8SaT3pVYSx1AmdOQXTcruyPEF49vHx CdhQk7EEed+XFQnfwmAxMkZ+6wAIeegT8LvArRJl9tPL/us2egBiVNDvy1VrXSNF/IQO 7NNg== X-Gm-Message-State: ANhLgQ0vuCceXCs45ZtEu6QyQVUMAu4VnvJ4cmk28NX4CPC1XHYtwYYJ d5ubhOTf4n1hIJJuDMc10a795fKtaWy6PzuT6FWVvQfYOSz+nhn/wWFOVe3JLSgMTSO/OSJOjo7 KScShdn/3n5AU9X8aR86nDwEy X-Received: by 2002:a1c:6146:: with SMTP id v67mr243977wmb.78.1584982639607; Mon, 23 Mar 2020 09:57:19 -0700 (PDT) X-Received: by 2002:a1c:6146:: with SMTP id v67mr243861wmb.78.1584982638429; Mon, 23 Mar 2020 09:57:18 -0700 (PDT) Received: from vitty.brq.redhat.com (g-server-2.ign.cz. [91.219.240.2]) by smtp.gmail.com with ESMTPSA id i4sm25236470wrm.32.2020.03.23.09.57.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 23 Mar 2020 09:57:17 -0700 (PDT) From: Vitaly Kuznetsov To: Sean Christopherson Cc: Paolo Bonzini , Wanpeng Li , Jim Mattson , Joerg Roedel , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Ben Gardon , Junaid Shahid , Liran Alon , Boris Ostrovsky , John Haxby , Miaohe Lin , Tom Lendacky Subject: Re: [PATCH v3 04/37] KVM: nVMX: Invalidate all roots when emulating INVVPID without EPT In-Reply-To: <20200323165001.GR28711@linux.intel.com> References: <20200320212833.3507-1-sean.j.christopherson@intel.com> <20200320212833.3507-5-sean.j.christopherson@intel.com> <87v9mv84qu.fsf@vitty.brq.redhat.com> <20200323160432.GJ28711@linux.intel.com> <87lfnr820r.fsf@vitty.brq.redhat.com> <20200323165001.GR28711@linux.intel.com> Date: Mon, 23 Mar 2020 17:57:16 +0100 Message-ID: <87imiv80wj.fsf@vitty.brq.redhat.com> MIME-Version: 1.0 Content-Type: text/plain Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Sean Christopherson writes: > On Mon, Mar 23, 2020 at 05:33:08PM +0100, Vitaly Kuznetsov wrote: >> Sean Christopherson writes: >> >> > On Mon, Mar 23, 2020 at 04:34:17PM +0100, Vitaly Kuznetsov wrote: >> >> Sean Christopherson writes: >> >> >> >> > From: Junaid Shahid >> >> > >> >> > Free all roots when emulating INVVPID for L1 and EPT is disabled, as >> >> > outstanding changes to the page tables managed by L1 need to be >> >> > recognized. Because L1 and L2 share an MMU when EPT is disabled, and >> >> > because VPID is not tracked by the MMU role, all roots in the current >> >> > MMU (root_mmu) need to be freed, otherwise a future nested VM-Enter or >> >> > VM-Exit could do a fast CR3 switch (without a flush/sync) and consume >> >> > stale SPTEs. >> >> > >> >> > Fixes: 5c614b3583e7b ("KVM: nVMX: nested VPID emulation") >> >> > Signed-off-by: Junaid Shahid >> >> > [sean: ported to upstream KVM, reworded the comment and changelog] >> >> > Signed-off-by: Sean Christopherson >> >> > --- >> >> > arch/x86/kvm/vmx/nested.c | 14 ++++++++++++++ >> >> > 1 file changed, 14 insertions(+) >> >> > >> >> > diff --git a/arch/x86/kvm/vmx/nested.c b/arch/x86/kvm/vmx/nested.c >> >> > index 9624cea4ed9f..bc74fbbf33c6 100644 >> >> > --- a/arch/x86/kvm/vmx/nested.c >> >> > +++ b/arch/x86/kvm/vmx/nested.c >> >> > @@ -5250,6 +5250,20 @@ static int handle_invvpid(struct kvm_vcpu *vcpu) >> >> > return kvm_skip_emulated_instruction(vcpu); >> >> > } >> >> > >> >> > + /* >> >> > + * Sync the shadow page tables if EPT is disabled, L1 is invalidating >> >> > + * linear mappings for L2 (tagged with L2's VPID). Free all roots as >> >> > + * VPIDs are not tracked in the MMU role. >> >> > + * >> >> > + * Note, this operates on root_mmu, not guest_mmu, as L1 and L2 share >> >> > + * an MMU when EPT is disabled. >> >> > + * >> >> > + * TODO: sync only the affected SPTEs for INVDIVIDUAL_ADDR. >> >> > + */ >> >> > + if (!enable_ept) >> >> > + kvm_mmu_free_roots(vcpu, &vcpu->arch.root_mmu, >> >> > + KVM_MMU_ROOTS_ALL); >> >> > + >> >> >> >> This is related to my remark on the previous patch; the comment above >> >> makes me think I'm missing something obvious, enlighten me please) >> >> >> >> My understanding is that L1 and L2 will share arch.root_mmu not only >> >> when EPT is globally disabled, we seem to switch between >> >> root_mmu/guest_mmu only when nested_cpu_has_ept(vmcs12) but different L2 >> >> guests may be different on this. Do we need to handle this somehow? >> > >> > guest_mmu is used iff nested EPT is enabled, which requires enable_ept=1. >> > enable_ept is global and cannot be changed without reloading kvm_intel. >> > >> > This most definitely over-invalidates, e.g. it blasts away L1's page >> > tables. But, fixing that requires tracking VPID in mmu_role and/or adding >> > support for using guest_mmu when L1 isn't using TDP, i.e. nested EPT is >> > disabled. Assuming the vast majority of nested deployments enable EPT in >> > L0, the cost of both options likely outweighs the benefits. >> > >> >> Yes but my question rather was: what if global 'enable_ept' is true but >> nested EPT is not being used by L1, don't we still need to do >> kvm_mmu_free_roots(&vcpu->arch.root_mmu) here? > > No, because L0 isn't shadowing the L1->L2 page tables, i.e. there can't be > unsync'd SPTEs for L2. The vpid_sync_*() above flushes the TLB for L2's > effective VPID, which is all that's required. Ah, stupid me, it's actually EPT and not nested EPT which we care about here. Thank you for the clarification! Reviewed-by: Vitaly Kuznetsov -- Vitaly