Received: by 2002:a05:6a10:9afc:0:0:0:0 with SMTP id t28csp373849pxm; Wed, 2 Mar 2022 17:41:34 -0800 (PST) X-Google-Smtp-Source: ABdhPJyUnHyz0t5vOrsVGTobe8jBNLgeHhgiRubG6u7BkaUu3PjxcyVdNkJJtgbs+7h5WZI2RQKt X-Received: by 2002:a17:90b:2496:b0:1b9:a6dd:ae7 with SMTP id nt22-20020a17090b249600b001b9a6dd0ae7mr2727518pjb.35.1646271693756; Wed, 02 Mar 2022 17:41:33 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1646271693; cv=none; d=google.com; s=arc-20160816; b=bQwkIiEs/UH1UNvX6iYdtr3VOf32xYYwuLW8kzx5qYSzJ9toxv1mZExdf1MTVU/xoS D+WIGy6GoTNwNg5Pz1adtwgjpWUkTHlIa88TIU3mHTL+kv+eze4IKDsHug2c5Ffna4vw NlSWLA5QljXTVej3w8h+bkZVkVYUE11gcWf96IklrIoE9Z3Fy32gtfaKv5tigPenygxe LTh1pA1CySTpCXaYe1aD3DDtH9KLxP95YGXYP8+VIHESgVt0uhpsn/RE9Yitk5G2nv8O fbmcaIDa4wChiOlhWrSpzN68CIbHId1xiO8VknAL2acZHEfahgLx9uJl87BWdbvPKd3t /hdw== 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=ECPY2rXs0gj3ZMmKf1ZVTljD0jLnkN0k48s4x2qcO38=; b=NxiO+Unirk8qDlW7E5hzu7sNpHJ13qU21FLZlcHkeQ5jkAmyGKxIrE3SXN1ixrFhcv A17bdsLVWYSC5XopTH8dAGg0gvdCjTZYVuNU84G3ZknnZ1gebGiXQrOAXVoa+61OMppu N98T/FUBZrbbqaKDYXM0b68t1t0dj0L+PWBHiUc6QsgHry/+A6W75TXtf/wFxhtneDv3 TvIX8EJH+V+YtbrHL45SAuVyIMdsjLSKtuU/XHhDNq3vbMv53ZtwxcLkOoeDHCqO/dtp B7fznmSLjWy5pQfSRvoqYByqgThnWP6wJt/o0s/8C8A9pXiBzKuwdV4OueX5iLMpBcnK 5oAg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=PUNnxzMx; 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 s14-20020a056a00178e00b004f124e36301si761209pfg.77.2022.03.02.17.41.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Mar 2022 17:41:33 -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=PUNnxzMx; 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 DEF5A1B513F; Wed, 2 Mar 2022 17:41:31 -0800 (PST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231420AbiCCBmI (ORCPT + 99 others); Wed, 2 Mar 2022 20:42:08 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46200 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231366AbiCCBmH (ORCPT ); Wed, 2 Mar 2022 20:42:07 -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 418081B5124 for ; Wed, 2 Mar 2022 17:41:23 -0800 (PST) Received: by mail-pj1-x1035.google.com with SMTP id v4so3385861pjh.2 for ; Wed, 02 Mar 2022 17:41:23 -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=ECPY2rXs0gj3ZMmKf1ZVTljD0jLnkN0k48s4x2qcO38=; b=PUNnxzMxpSFabi/9CdWyQIWtepUkWGHx6ZZ3CkeKQhscGAuM5uGHPfE++wPJvpyxmA iLtzRMTJlF0Yp5b8Tc6dpABJxKBLyU5qNxRJJCww8OtC9Dq+1P24VVmU7Jx6iyvDAa6U JJELAb8jqSKUOmuZfn3Px0gqTxGqHKGr/twCLzRb5SbEsgc+nE6Ze+RcKYM2Y/oHrkr6 t8gFHqxjZTemPlhXm+xUEUPX035fKE8KJDr1po7YBU3hlRFJPD6XvqugIVGHEA6xjJ9G q0eO2Ej12fMySEyh9Cd3ipzTaziiKoFmZGJNqZf5+tN4doooPAHHZ0vyZhD8eRalgKJB q1vA== 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=ECPY2rXs0gj3ZMmKf1ZVTljD0jLnkN0k48s4x2qcO38=; b=7m7a0wlWrBAZhPIpvmni5aOSDHVe0d1psjOqoGAj3rAzNMDCvKERzXMoHe0p2RpCTS 2huz8Bxw49xzGEDro8zz+TAlvf6wKjOQqNc9JBiQwTJDz9jewhf9GgoBnuSNAQviu0OA 0udV+BjFvoTDjzDqfSgpTVJfOOELyCGV5XLkSF247lVA+EJ945CcUEZD/JhVrlB00UUL NFQZS/DK3Feqy4iv3K9/qAiRfRdKR2ltXLdRvBKzg0iDu3HDZkBo9plEQdXj3U8WyfnJ DBAVgIQVlQ92A59QZCSvW7SOlHWFki47M0exC3utpSNaOGmxUr0RfGCBQdv4KQH4rKCx KVhA== X-Gm-Message-State: AOAM530dyAOAR23GPM+0rvPVtMYnvVrefHQuWr3U0BDO7uCaqVGx3Ze4 5KuQQt22OnevKHXoSv88MVQlvg== X-Received: by 2002:a17:90a:67c9:b0:1b9:51d5:6c13 with SMTP id g9-20020a17090a67c900b001b951d56c13mr2734061pjm.216.1646271682558; Wed, 02 Mar 2022 17:41:22 -0800 (PST) Received: from google.com (157.214.185.35.bc.googleusercontent.com. [35.185.214.157]) by smtp.gmail.com with ESMTPSA id ip3-20020a17090b314300b001b9cb7ab96fsm309872pjb.8.2022.03.02.17.41.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Mar 2022 17:41:21 -0800 (PST) Date: Thu, 3 Mar 2022 01:41:18 +0000 From: Sean Christopherson To: Mingwei Zhang 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: 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 Thu, Mar 03, 2022, Mingwei Zhang wrote: > On Thu, Mar 03, 2022, Sean Christopherson wrote: > > On Wed, Mar 02, 2022, Mingwei Zhang wrote: > > > On Sat, Feb 26, 2022, Sean Christopherson wrote: > > > > 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? > > > > Nope, no functional purpose, though there might be some "advanced" warning or > > static checkers that care. > > > > The "(void)" is to communicate to human readers that the result is intentionally > > ignored, e.g. to reduce the probability of someone "fixing" the code by acting on > > the result of zap_gfn_range(). The comment should suffice, but it's nice to have > > the code be self-documenting as much as possible. > > Right, I got the point. Thanks. > > Coming back. It seems that I pretended to understand that we should > avoid the TLB flush without really knowing why. > > I mean, leaving (part of the) stale TLB entries unflushed will still be > dangerous right? Or am I missing something that guarantees to flush the > local TLB before returning to the guest? For instance, > kvm_mmu_{re,}load()? Heh, if SVM's ASID management wasn't a mess[*], it'd be totally fine. The idea, and what EPT architectures mandates, is that each TDP root is associated with an ASID. So even though there may be stale entries in the TLB for a root, because that root is no longer used those stale entries are unreachable. And if KVM ever happens to reallocate the same physical page for a root, that's ok because KVM must be paranoid and flush that root (see code comment in this patch). What we're missing on SVM is proper ASID handling. If KVM uses ASIDs the way AMD intends them to be used, then this works as intended because each root is again associated with a specific ASID, and KVM just needs to flush when (re)allocating a root and when reusing an ASID (which it already handles). [*] https://lore.kernel.org/all/Yh%2FJdHphCLOm4evG@google.com