Received: by 2002:a05:6a10:9afc:0:0:0:0 with SMTP id t28csp1565210pxm; Thu, 3 Mar 2022 22:02:33 -0800 (PST) X-Google-Smtp-Source: ABdhPJwsPO2IaDLqXIxqY2L5taOwZE3eE5o60e+rWBhL33mzyDjf8YkdOJQJ5cIe82vo1XlD3Otm X-Received: by 2002:a63:2a45:0:b0:373:1850:d5b with SMTP id q66-20020a632a45000000b0037318500d5bmr32083019pgq.563.1646373753090; Thu, 03 Mar 2022 22:02:33 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1646373753; cv=none; d=google.com; s=arc-20160816; b=sc8ciiYNtK9siMV/gKcPf7SmV+LVW/WFa2AxtP55nJr6Qv1F8SgbPz6NWIMadDpJp9 QRWfbMMFuWvG+9kR8g3QA+aYpwn44J/3/RlViwK+/cWmSQZKSRenHs3d183ZUDORHesE n2xUO7doYDL1wC6X0pQWRPVGckZI0+Aj9cHAxEO3axNIl0VYRCE3eSrUoZswp9y7KeTq g6nXFxcOhytrhS01aa+77cS66C+NfuUm65qcQwLOtv4Nuye1/n9MoJFPIWIvln4qwpRZ Y0WYr7NxYM7dwirsQXvvkqDluHG5hewdV6VuRm/kWjWFEmEaUlPtAT+irVbHjDZsMVlW WxBw== 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=zbrxoFXUbPPWy2OmGHM/87ehykZdoUkb/q8EdrCv/aE=; b=RjmP2hHtahuXc64RB8rQG+DQh4rGOBF2Oq4wJ1YbxrLaOAJxtIXg4ifUdqckNqQ5b9 rpFkFMH4BeZdyJ8+OrgToI3z8VS5cKGPkXR99esgIVVSQ3v6MNqCWZNfIUsrW4vCI/Ij opANSI0joWa93XV9xD7dKZRGwK4sFCIv1X0pOm8AukLsbJGMV6bARCeLAxKg75071cyJ f4FIEOkCRfp5RxEssZudCVirxgBOLRasyynxhbVrD2E6IbvMVH/Sgu7vhfe9bBb8SYMf nRYP+bvO12ijx3hwGJx+YirxoFYm8WICEqHoYILq5gRYqT//+DLzupYK08Rj5T33L/A9 dYFA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=kPQFbEid; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id g136-20020a62528e000000b004c9c1f6b9b9si3554962pfb.249.2022.03.03.22.02.16; Thu, 03 Mar 2022 22:02:33 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=kPQFbEid; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237279AbiCCXkY (ORCPT + 99 others); Thu, 3 Mar 2022 18:40:24 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42598 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231128AbiCCXkX (ORCPT ); Thu, 3 Mar 2022 18:40:23 -0500 Received: from mail-pj1-x1032.google.com (mail-pj1-x1032.google.com [IPv6:2607:f8b0:4864:20::1032]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 89D97117C99 for ; Thu, 3 Mar 2022 15:39:35 -0800 (PST) Received: by mail-pj1-x1032.google.com with SMTP id gj15-20020a17090b108f00b001bef86c67c1so6324526pjb.3 for ; Thu, 03 Mar 2022 15:39:35 -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=zbrxoFXUbPPWy2OmGHM/87ehykZdoUkb/q8EdrCv/aE=; b=kPQFbEidLAFavoqL/HEOhmglZRBz4Q68Wjxv38L/nrQW38gsLkeVaoFz6YtOG/32CC EDGDXsVNNg1nhObKAgqVXBB7xfrgxb2R1m/hOZjoW7tqFVDqneF8nkYiy8694vo/9vt1 J9JfkapL8D3Puoz4DNULTMQQdyanboKRrYace7dcwv6DsAJDK5sysjunkk1xM2qTj7dm yKMc4p9WW/u8pdzDdRfD6dqiv1UeFYAI5AvXEbocjxKD36IXqpF5y6RHHA6gq4fv/yKL tSWxu27g7bgHaof8/k6HMh+sbeDoxtVKxKTqhlpvhQASvR3TlH1GpCn+PUwvnANutGAS nSLg== 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=zbrxoFXUbPPWy2OmGHM/87ehykZdoUkb/q8EdrCv/aE=; b=VAFnL5y2TRkyFnNkDhQACdF4mwy7o3viumuzfIrygGKHjE4UAMZIfmKavJ6PxCtuBg FJtbEsZgb28GBq2MSqQiRz0pi32aU8ZTz2UPh9afMYX5J6Ab3jgzUJDUN6jVFO4E9wQM vnUxRo9xUAQAPOK5Iu4/+Q7VK+W7ReoYKlIHjhE+NijbRuSh9FgeYf4NwL8zrevyV7BW 8uS6gr0p7g86gjLPCmNfbvVvYCeGUhZQ5w3PAwCWBaEQiQh7T05IPf61/PqERNxJNA3K lPWvF5zbSDwpdL1ToSxhI/Qk91d2Czu/4PaK0pg084ej9JZAYbAYoOtl4iL79ep2bfmd evPw== X-Gm-Message-State: AOAM532FFhmI2DLjew+St0Vjnn23QuSL4cNQwSr3JQNl6QIBWBEb5Xma xEfQDcw4jajAH/+YgUA1A5lF4g== X-Received: by 2002:a17:90a:1108:b0:1be:e1bd:e2f0 with SMTP id d8-20020a17090a110800b001bee1bde2f0mr7787232pja.144.1646350774737; Thu, 03 Mar 2022 15:39:34 -0800 (PST) Received: from google.com (226.75.127.34.bc.googleusercontent.com. [34.127.75.226]) by smtp.gmail.com with ESMTPSA id x7-20020a17090a1f8700b001bf1db72189sm1124311pja.23.2022.03.03.15.39.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 03 Mar 2022 15:39:34 -0800 (PST) Date: Thu, 3 Mar 2022 23:39:30 +0000 From: Mingwei Zhang To: Paolo Bonzini Cc: linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , David Hildenbrand , David Matlack , Ben Gardon Subject: Re: [PATCH v4 03/30] KVM: x86/mmu: Formalize TDP MMU's (unintended?) deferred TLB flush logic Message-ID: References: <20220303193842.370645-1-pbonzini@redhat.com> <20220303193842.370645-4-pbonzini@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20220303193842.370645-4-pbonzini@redhat.com> X-Spam-Status: No, score=-18.1 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, ENV_AND_HDR_SPF_MATCH,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL,USER_IN_DEF_SPF_WL autolearn=ham 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 Thu, Mar 03, 2022, Paolo Bonzini wrote: > From: Sean Christopherson > > 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 > Message-Id: <20220226001546.360188-5-seanjc@google.com> > Reviewed-by: Mingwei Zhang > Signed-off-by: Paolo Bonzini > --- > 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 32c041ed65cb..9a6df2d02777 100644 > --- a/arch/x86/kvm/mmu/mmu.c > +++ b/arch/x86/kvm/mmu/mmu.c > @@ -5083,6 +5083,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. Even if KVM ensures there are no stale TLB entries > + * for a freed root, in theory another 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 b97a4125feac..921fa386df99 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); Discussed offline with Sean. Now I get myself comfortable with the style of mmu with multiple 'roots' and leaving TLB unflushed for invalidated roots. I guess one minor improvement on the comment could be: "A TLB flush is not necessary as each vCPU performs a local TLB flush when allocating or assigning a new root (see kvm_mmu_load()), and when migrating to a different pCPU." The above could be better since "KVM performs a local TLB flush" makes readers think why we miss the 'remote' TLB flushes? > > call_rcu(&root->rcu_head, tdp_mmu_free_sp_rcu_callback); > } > -- > 2.31.1 > >