Received: by 2002:a05:6a10:9afc:0:0:0:0 with SMTP id t28csp319855pxm; Wed, 2 Mar 2022 16:17:38 -0800 (PST) X-Google-Smtp-Source: ABdhPJyeu+M7raXMs0iF8DqPqS6IlG/5S63rGRxlcAIHHHuvzE9bZclQYugn7PF9n26qPhu3bSwY X-Received: by 2002:a17:90a:ac09:b0:1bc:3e9d:158 with SMTP id o9-20020a17090aac0900b001bc3e9d0158mr2386242pjq.235.1646266658426; Wed, 02 Mar 2022 16:17:38 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1646266658; cv=none; d=google.com; s=arc-20160816; b=HDR+oIh4QYhZRe7e3FZr23hZEdCQQgzzlGrkxJxQVEf4YXMOysBxuSrThmrlU8BdDF JjnaCuwsnFkVGlm0k52DfZk8GdMZa69kjddmf5t1GYtcCeeS87nL4DdZEvm8Xs3nlKDW XBOTYQxrIxXp2pl5REbolyHnocd+nUhCjQND8fMr6zgaPUokQwf8GkDO+Yum7Yc9GCbA Sqsw6oxM1AL0tfSfNadmSt/wSoAobJ9DFBmpfuGxC/PiMkQ1zFxZep2XgkA537U/1Z3k 8bcj59eAT3kqjjTXTQnfa/BOL2LqcZEPqJzNCHJ8fvinLRRh+8k7FIoqkCAoR5xnTrmh 631g== 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=ikC7Wxp/jzr+tnNZpmEM/sGB+lmtBh1UpNvi+15LDck=; b=L8Z9ZSzv1tXTLCW66KKmTnyPrOC2HaGhVWLCpVa8HoPtKszPALVAEhQ1wLTAC07SNn +1Tb9nFFFwrgBcUlcQ6c+pfCD+BVC+LnfTnzkawHKLI9CEcDAdMnl6nst+mn5+N2o3Pv MU0rBwi2lU6x2pu3fMI4M1KTbLiP9TKwxt2QQzxeImBP43e3oXSWWwWGLtMwN1Hcq6Vx fhACoWir5CcbBlqpBNA7YZM4imdqf5366+cIZ2sNE+c3bqIDoQ2TKBnmDr6t9P3ErI1C UKdxoNg76NaTKBDrV7NBDYbOqhczgCX8MArX7lFhPf9OwlkJJJg8YLtPrtxp0Z3NpjuD FuiQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=qU4M+Z71; 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 mg23-20020a17090b371700b001beef128ab6si4428991pjb.117.2022.03.02.16.17.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Mar 2022 16:17:38 -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=qU4M+Z71; 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 4A8D014F994; Wed, 2 Mar 2022 15:31:31 -0800 (PST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241666AbiCBTJA (ORCPT + 99 others); Wed, 2 Mar 2022 14:09:00 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50276 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230433AbiCBTI5 (ORCPT ); Wed, 2 Mar 2022 14:08:57 -0500 Received: from mail-pl1-x62f.google.com (mail-pl1-x62f.google.com [IPv6:2607:f8b0:4864:20::62f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F3C6849C9F for ; Wed, 2 Mar 2022 11:08:12 -0800 (PST) Received: by mail-pl1-x62f.google.com with SMTP id p17so2387034plo.9 for ; Wed, 02 Mar 2022 11:08:12 -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=ikC7Wxp/jzr+tnNZpmEM/sGB+lmtBh1UpNvi+15LDck=; b=qU4M+Z712jujMdXR5jH2f1PCoN8a/EMK3uEKMf8fbrbKbzYop+gQ0b5jhDhSKlfPNg uqc6viy3rsO7PSbikjaW6Q0yVyKAoOqZaEM8vCoj03wd505NGOMN5lauAfiQoDB8FDy4 aly8qy9uoAM4s88ix5I5P/Ul7Dyl+lq0yKyt2oX9VeCxbJLfteMkzB8d2y9hNVoXJ8dv UCmHXxWly2YPQwwrS7xh5hCNIA25FuPF48Npcch9e78sPZmBMVNPf6pmClVPqy2tCHnf aveLuE/U5M3Dn6BVy60R8IppxpS9wIBWwXzhn9TMvKVNH25TrLwiCEJnIu8ph4S1YjbA ETDw== 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=ikC7Wxp/jzr+tnNZpmEM/sGB+lmtBh1UpNvi+15LDck=; b=v5bxoEMKmyhaDYz7OPESb0pHuOsmo8NCdvZQLCmtPe/vHd3Os7rss2J9QO2VvWuyVQ p1GS9txY5y1mw16pDvBR/ePerTrjH6uXsvMsyvRLlEQESa0tS1csJhI4Du8aeUnise69 b+noKaSvMuAYpLPTSPqeb3yKt0OQSAhZGlxZ3SR142IyEVS5sOrv4i76jYWbry3hv/qD XIaw61UJayiwWdt28KqQtZPPCt+fLwQh/IpDmGNrdeyDpl3U9OTQF7/sokcWrVhrg/3E OZhAEGDAyEnVrA58Rca4a0wpbjsK5mIFKy6jEJWTug6aaZw+O3ztAXWop43tOZ4yFn5H zzcw== X-Gm-Message-State: AOAM531zjBz+1zGvI5GMxiiyYuika94oKMxd3E4Bkp73Vc/vciRSGYMV hlLhikdduH+sHfCyCwjvNDN//A== X-Received: by 2002:a17:902:d484:b0:151:6964:2bd with SMTP id c4-20020a170902d48400b00151696402bdmr16870438plg.108.1646248092136; Wed, 02 Mar 2022 11:08:12 -0800 (PST) Received: from google.com (226.75.127.34.bc.googleusercontent.com. [34.127.75.226]) by smtp.gmail.com with ESMTPSA id 124-20020a620582000000b004dee0e77128sm20357580pff.166.2022.03.02.11.08.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Mar 2022 11:08:11 -0800 (PST) Date: Wed, 2 Mar 2022 19:08:07 +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 01/28] KVM: x86/mmu: Use common iterator for walking invalid TDP MMU roots Message-ID: References: <20220226001546.360188-1-seanjc@google.com> <20220226001546.360188-2-seanjc@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20220226001546.360188-2-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: > Now that tdp_mmu_next_root() can process both valid and invalid roots, > extend it to be able to process _only_ invalid roots, add yet another > iterator macro for walking invalid roots, and use the new macro in > kvm_tdp_mmu_zap_invalidated_roots(). > > No functional change intended. > > Reviewed-by: David Matlack > Signed-off-by: Sean Christopherson > --- > arch/x86/kvm/mmu/tdp_mmu.c | 74 ++++++++++++++------------------------ > 1 file changed, 26 insertions(+), 48 deletions(-) > > diff --git a/arch/x86/kvm/mmu/tdp_mmu.c b/arch/x86/kvm/mmu/tdp_mmu.c > index debf08212f12..25148e8b711d 100644 > --- a/arch/x86/kvm/mmu/tdp_mmu.c > +++ b/arch/x86/kvm/mmu/tdp_mmu.c > @@ -98,6 +98,12 @@ void kvm_tdp_mmu_put_root(struct kvm *kvm, struct kvm_mmu_page *root, > call_rcu(&root->rcu_head, tdp_mmu_free_sp_rcu_callback); > } > > +enum tdp_mmu_roots_iter_type { > + ALL_ROOTS = -1, > + VALID_ROOTS = 0, > + INVALID_ROOTS = 1, > +}; I am wondering what the trick is to start from -1? > + > /* > * Returns the next root after @prev_root (or the first root if @prev_root is > * NULL). A reference to the returned root is acquired, and the reference to > @@ -110,10 +116,16 @@ void kvm_tdp_mmu_put_root(struct kvm *kvm, struct kvm_mmu_page *root, > */ > static struct kvm_mmu_page *tdp_mmu_next_root(struct kvm *kvm, > struct kvm_mmu_page *prev_root, > - bool shared, bool only_valid) > + bool shared, > + enum tdp_mmu_roots_iter_type type) > { > struct kvm_mmu_page *next_root; > > + kvm_lockdep_assert_mmu_lock_held(kvm, shared); > + > + /* Ensure correctness for the below comparison against role.invalid. */ > + BUILD_BUG_ON(!!VALID_ROOTS || !INVALID_ROOTS); > + > rcu_read_lock(); > > if (prev_root) > @@ -125,7 +137,7 @@ static struct kvm_mmu_page *tdp_mmu_next_root(struct kvm *kvm, > typeof(*next_root), link); > > while (next_root) { > - if ((!only_valid || !next_root->role.invalid) && > + if ((type == ALL_ROOTS || (type == !!next_root->role.invalid)) && > kvm_tdp_mmu_get_root(next_root)) > break; > > @@ -151,18 +163,21 @@ static struct kvm_mmu_page *tdp_mmu_next_root(struct kvm *kvm, > * mode. In the unlikely event that this thread must free a root, the lock > * will be temporarily dropped and reacquired in write mode. > */ > -#define __for_each_tdp_mmu_root_yield_safe(_kvm, _root, _as_id, _shared, _only_valid)\ > - for (_root = tdp_mmu_next_root(_kvm, NULL, _shared, _only_valid); \ > +#define __for_each_tdp_mmu_root_yield_safe(_kvm, _root, _as_id, _shared, _type) \ > + for (_root = tdp_mmu_next_root(_kvm, NULL, _shared, _type); \ > _root; \ > - _root = tdp_mmu_next_root(_kvm, _root, _shared, _only_valid)) \ > - if (kvm_mmu_page_as_id(_root) != _as_id) { \ > + _root = tdp_mmu_next_root(_kvm, _root, _shared, _type)) \ > + if (_as_id > 0 && kvm_mmu_page_as_id(_root) != _as_id) { \ > } else > > +#define for_each_invalid_tdp_mmu_root_yield_safe(_kvm, _root) \ > + __for_each_tdp_mmu_root_yield_safe(_kvm, _root, -1, true, INVALID_ROOTS) > + > #define for_each_valid_tdp_mmu_root_yield_safe(_kvm, _root, _as_id, _shared) \ > - __for_each_tdp_mmu_root_yield_safe(_kvm, _root, _as_id, _shared, true) > + __for_each_tdp_mmu_root_yield_safe(_kvm, _root, _as_id, _shared, VALID_ROOTS) > > #define for_each_tdp_mmu_root_yield_safe(_kvm, _root, _as_id, _shared) \ > - __for_each_tdp_mmu_root_yield_safe(_kvm, _root, _as_id, _shared, false) > + __for_each_tdp_mmu_root_yield_safe(_kvm, _root, _as_id, _shared, ALL_ROOTS) > > #define for_each_tdp_mmu_root(_kvm, _root, _as_id) \ > list_for_each_entry_rcu(_root, &_kvm->arch.tdp_mmu_roots, link, \ > @@ -810,28 +825,6 @@ void kvm_tdp_mmu_zap_all(struct kvm *kvm) > kvm_flush_remote_tlbs(kvm); > } > > -static struct kvm_mmu_page *next_invalidated_root(struct kvm *kvm, > - struct kvm_mmu_page *prev_root) > -{ > - struct kvm_mmu_page *next_root; > - > - if (prev_root) > - next_root = list_next_or_null_rcu(&kvm->arch.tdp_mmu_roots, > - &prev_root->link, > - typeof(*prev_root), link); > - else > - next_root = list_first_or_null_rcu(&kvm->arch.tdp_mmu_roots, > - typeof(*next_root), link); > - > - while (next_root && !(next_root->role.invalid && > - refcount_read(&next_root->tdp_mmu_root_count))) > - next_root = list_next_or_null_rcu(&kvm->arch.tdp_mmu_roots, > - &next_root->link, > - typeof(*next_root), link); > - > - return next_root; > -} > - > /* > * Since kvm_tdp_mmu_zap_all_fast has acquired a reference to each > * invalidated root, they will not be freed until this function drops the > @@ -842,36 +835,21 @@ static struct kvm_mmu_page *next_invalidated_root(struct kvm *kvm, > */ > void kvm_tdp_mmu_zap_invalidated_roots(struct kvm *kvm) > { > - struct kvm_mmu_page *next_root; > struct kvm_mmu_page *root; > bool flush = false; > > lockdep_assert_held_read(&kvm->mmu_lock); > > - rcu_read_lock(); > - > - root = next_invalidated_root(kvm, NULL); > - > - while (root) { > - next_root = next_invalidated_root(kvm, root); > - > - rcu_read_unlock(); > - > + for_each_invalid_tdp_mmu_root_yield_safe(kvm, root) { > flush = zap_gfn_range(kvm, root, 0, -1ull, true, flush, true); > > /* > - * Put the reference acquired in > - * kvm_tdp_mmu_invalidate_roots > + * Put the reference acquired in kvm_tdp_mmu_invalidate_roots(). > + * Note, the iterator holds its own reference. > */ > kvm_tdp_mmu_put_root(kvm, root, true); > - > - root = next_root; > - > - rcu_read_lock(); > } > > - rcu_read_unlock(); > - > if (flush) > kvm_flush_remote_tlbs(kvm); > } > -- > 2.35.1.574.g5d30c73bfb-goog >