Received: by 2002:a05:6a10:9afc:0:0:0:0 with SMTP id t28csp336707pxm; Wed, 2 Mar 2022 16:43:03 -0800 (PST) X-Google-Smtp-Source: ABdhPJzd3Ctv8xW01inArv8i3ALctKArVUC7tmN6PrnfdgnR9OR3QvYGLmYvh/WjruRZznYjnbCE X-Received: by 2002:a17:90a:d081:b0:1bd:4c83:56ec with SMTP id k1-20020a17090ad08100b001bd4c8356ecmr2531392pju.238.1646268183040; Wed, 02 Mar 2022 16:43:03 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1646268183; cv=none; d=google.com; s=arc-20160816; b=X40Q6B6oQfXNk1wthVba7jp5jF/r08VNxvLwa3bVn4QQGZouQ3Na7CSzRObv9kNZ22 eFIX11ddrNq5s6NpyCA8BK4V00tTkdS0otG2SE1SsvmZf/EXebkoN1WEiRGL6Ez1xhpa IyS/+o9Y096/KHZq70B+ujmE4bBasOAUgtMb3yLzUnilLWd/E/+/Xd4rr3vj/XjzqjtX Pta/ZZAPMcmuAziJ9XtZiL4N/MHHfGGqWsN/rhfDEPMthSTz4f1Vd04uFb/ILDw14hxB SofrzcaOCZcbwRUZbsi9cK0LuIybPNnIIvMGebnR7QyuYq7NuoXty8tRBDX/0jbWA3ta 3TkQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=ckCBduCU8rIC3xMm3IAzUST/V5HZeQGdJRo5tI2p308=; b=s9bl8jJiwMf/zaS5/y3XZrvM+15C4wJtVeUSGCj6EtrIgq8hv3Imucf5XaRzldDZYF wao67Sc10QBNXyuYB/DVISEOymYp4YEdf9HphAS6KfcxXtC+QyQnUjHTGnwb854cd10I tZtMHMV4aiRZKRGO6zEUy6d5U1drf9LN2NzZ1vaKhFLDc06lWI7VE9nJmG6Y6ph1wZAo PK/PkDgBotcGI89+vPKjGY8dvcFuuy0puiUXRkDXpV8qki59XsxBDDS+uhMfqqJoI+jl zAYvfDgVoSAxeuVLOsrI/VU8N5nrsJPAE1bMRCR2h08JpIj7/3tKREDOFru3oFQf1yo2 ADtQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=KxhA+CG1; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id jx3-20020a17090b46c300b001bbc63e19fbsi466951pjb.188.2022.03.02.16.43.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Mar 2022 16:43:03 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=KxhA+CG1; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 7A3DD4C792; Wed, 2 Mar 2022 15:59:43 -0800 (PST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229806AbiCCAAW (ORCPT + 99 others); Wed, 2 Mar 2022 19:00:22 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48314 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229793AbiCCAAU (ORCPT ); Wed, 2 Mar 2022 19:00:20 -0500 Received: from mail-pj1-x1035.google.com (mail-pj1-x1035.google.com [IPv6:2607:f8b0:4864:20::1035]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 07073496A7 for ; Wed, 2 Mar 2022 15:59:36 -0800 (PST) Received: by mail-pj1-x1035.google.com with SMTP id p3-20020a17090a680300b001bbfb9d760eso6219120pjj.2 for ; Wed, 02 Mar 2022 15:59:36 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=ckCBduCU8rIC3xMm3IAzUST/V5HZeQGdJRo5tI2p308=; b=KxhA+CG1r0bDnPfRKPRFC4gWUhhykeI8AD5VmSGys12v1gnCr1jqlAXuyRGCQEa5xu MbxFpEqZObEdVp9jxGfe67+QSPDRS3eTe7Knh2vk8ZwqewwaTmNiZmbGTB/JCDGJrzt0 Kr7KUS9mo0vj05w/YDjQlaHpwIy5/BKQoglm9GVlHLe0e4jDc9EG8M1vN6BPd19TwktT pHCwVpH0nat/DKrUdcH21H5XA8rHzH3vn0zpKjYPLfTLOEMlOaAZ0yu03RSIvjMik4iU sFJFW8xyL7nOO3FV/nd6fHVCtBIXYCUvAn3UTVbbMXXgOaEBUf210V2B8LzHhtVXpqXm CD6g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=ckCBduCU8rIC3xMm3IAzUST/V5HZeQGdJRo5tI2p308=; b=3IJrxt8GzTE1OWC4GKNIAddk92QcBZgNPMAEgQItjqlIDSNNPy6sR4nN6emtzeGuYt YX6yQJVg5rUu5w+V6NlTdCuR/hDxXQtFHBlOPgn3HQAJXFfBiDrGdU+aQ9+tELK4gOqa qbf+YcaKVXv84HG/X6/ty/Kh47lF/KjMZxAkGapm/vZEsqCtu2UtEuqQ/99SWaw9drt8 Bd+VhuaXTU4MRUzNaP7wo0WYEKnpPs6pA6LOfw1f5KQ2h2l1s5XOiZmZPVsYCf0JnmeS +SW5LOkfDzU5+ouLOI0xyXNYhTLTTtT0OhBcz8W/AiOaVZByRXuASHsbrG/i++T6gexp PywA== X-Gm-Message-State: AOAM533UB9xRo4szV3BTga/7A6DSG2n1SOFW0TvacIAvT/XHnxtZJ5bS wn8Bpj0BPxZXMO/C5R1K4nhZiQ== X-Received: by 2002:a17:902:8306:b0:14f:a386:6a44 with SMTP id bd6-20020a170902830600b0014fa3866a44mr33123916plb.140.1646265575211; Wed, 02 Mar 2022 15:59:35 -0800 (PST) Received: from google.com (226.75.127.34.bc.googleusercontent.com. [34.127.75.226]) by smtp.gmail.com with ESMTPSA id c18-20020a056a000ad200b004cdccd3da08sm299171pfl.44.2022.03.02.15.59.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Mar 2022 15:59:34 -0800 (PST) Date: Wed, 2 Mar 2022 23:59:30 +0000 From: Mingwei Zhang To: Sean Christopherson Cc: Paolo Bonzini , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , David Hildenbrand , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, David Matlack , Ben Gardon Subject: Re: [PATCH v3 04/28] KVM: x86/mmu: Formalize TDP MMU's (unintended?) deferred TLB flush logic Message-ID: References: <20220226001546.360188-1-seanjc@google.com> <20220226001546.360188-5-seanjc@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20220226001546.360188-5-seanjc@google.com> X-Spam-Status: No, score=-10.0 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE, USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Sat, Feb 26, 2022, Sean Christopherson wrote: > Explicitly ignore the result of zap_gfn_range() when putting the last > reference to a TDP MMU root, and add a pile of comments to formalize the > TDP MMU's behavior of deferring TLB flushes to alloc/reuse. Note, this > only affects the !shared case, as zap_gfn_range() subtly never returns > true for "flush" as the flush is handled by tdp_mmu_zap_spte_atomic(). > > Putting the root without a flush is ok because even if there are stale > references to the root in the TLB, they are unreachable because KVM will > not run the guest with the same ASID without first flushing (where ASID > in this context refers to both SVM's explicit ASID and Intel's implicit > ASID that is constructed from VPID+PCID+EPT4A+etc...). > > Signed-off-by: Sean Christopherson Reviewed-by: Mingwei Zhang > --- > arch/x86/kvm/mmu/mmu.c | 8 ++++++++ > arch/x86/kvm/mmu/tdp_mmu.c | 10 +++++++++- > 2 files changed, 17 insertions(+), 1 deletion(-) > > diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c > index 80607513a1f2..5a931c89d27b 100644 > --- a/arch/x86/kvm/mmu/mmu.c > +++ b/arch/x86/kvm/mmu/mmu.c > @@ -5069,6 +5069,14 @@ int kvm_mmu_load(struct kvm_vcpu *vcpu) > kvm_mmu_sync_roots(vcpu); > > kvm_mmu_load_pgd(vcpu); > + > + /* > + * Flush any TLB entries for the new root, the provenance of the root > + * is unknown. In theory, even if KVM ensures there are no stale TLB > + * entries for a freed root, in theory, an out-of-tree hypervisor could > + * have left stale entries. Flushing on alloc also allows KVM to skip > + * the TLB flush when freeing a root (see kvm_tdp_mmu_put_root()). > + */ > static_call(kvm_x86_flush_tlb_current)(vcpu); > out: > return r; > diff --git a/arch/x86/kvm/mmu/tdp_mmu.c b/arch/x86/kvm/mmu/tdp_mmu.c > index 12866113fb4f..e35bd88d92fd 100644 > --- a/arch/x86/kvm/mmu/tdp_mmu.c > +++ b/arch/x86/kvm/mmu/tdp_mmu.c > @@ -93,7 +93,15 @@ void kvm_tdp_mmu_put_root(struct kvm *kvm, struct kvm_mmu_page *root, > list_del_rcu(&root->link); > spin_unlock(&kvm->arch.tdp_mmu_pages_lock); > > - zap_gfn_range(kvm, root, 0, -1ull, false, false, shared); > + /* > + * A TLB flush is not necessary as KVM performs a local TLB flush when > + * allocating a new root (see kvm_mmu_load()), and when migrating vCPU > + * to a different pCPU. Note, the local TLB flush on reuse also > + * invalidates any paging-structure-cache entries, i.e. TLB entries for > + * intermediate paging structures, that may be zapped, as such entries > + * are associated with the ASID on both VMX and SVM. > + */ > + (void)zap_gfn_range(kvm, root, 0, -1ull, false, false, shared); Understood that we could avoid the TLB flush here. Just curious why the "(void)" is needed here? Is it for compile time reason? > > call_rcu(&root->rcu_head, tdp_mmu_free_sp_rcu_callback); > } > -- > 2.35.1.574.g5d30c73bfb-goog >