Received: by 2002:a05:6a10:9afc:0:0:0:0 with SMTP id t28csp304176pxm; Wed, 2 Mar 2022 15:55:06 -0800 (PST) X-Google-Smtp-Source: ABdhPJzaVTvqSzB7cPLB4h53Y4oM7bCypDu/Riw3x25u5rGQ7pNV4WkHr6V8aU9O5dt+dGgQQIVK X-Received: by 2002:a05:6a00:1706:b0:4df:7fe0:841a with SMTP id h6-20020a056a00170600b004df7fe0841amr35278917pfc.64.1646265306031; Wed, 02 Mar 2022 15:55:06 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1646265306; cv=none; d=google.com; s=arc-20160816; b=ZfRz+xwdIs3fS2ppC/s+Itv6/E9WSymLSBL+MD0OO2c9RpKhtSO5BNudEA1HOlIwZ5 Wh13yIcaAB6cocoEs+p1HZYJ1Ar7VldvjJs0grvXmX0D+u24FWC7520qQ3E2V8ATDS57 dg9MeBFgqGmjrS+GYPXJwdQavRB54zHp32VWoQL7qVTMZ8ZyevxX1Dy2cUMkiBTPiyrj ptqFWbkqZ61IQUCiBkP1jz7Ac6rYxAto0RtTdXrn+2YdOO1DKbJpRmWQHnDG2XWyeYwd maeAcX9rR1AmNCX23wj7+vJ0sAz2Wb4Fn/GUk76TFe5QNdbEgx7NLzWDZd5yvMQozjU9 Ql8A== 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=K9X7JrhsxNcS6upS7mNhAwW5h+M5okT8FoWI5/raP6I=; b=kffzYtoSdKnR9hg8HNPyUaBhSL+R8sWGm8Tr4tUKE1NPaSdc2Dh5zwnY5ozI55G4Dn gCalvs5XmuT258qhogy1DVghVvoK4LRf/Ecgx9ruSZ7vs+umlSeTQnxRLmPJWTmXVeIK gTGIqPgkSAIQMAMLK3PA/NoRu6d7VMshXuy2UOAoNNHefL4xQpJq+7k9Dvz40awpl0hU He2k5OKacAyEzVMwwByvKqBKB2ZfW2Y4SgrpAlKJl2ZbwzWETV+HU8f0f29Igk4kh4UJ sxkfz2q5VbnoBAJHV1c5umlNJKA0C48dfzHc/Q0DCN/tBwT9iLB10JblqsdcqtTPudi/ 3FdQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=sS3kE8+P; 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 k8-20020a170902694800b0014fc3733e43si365000plt.551.2022.03.02.15.55.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Mar 2022 15:55:06 -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=sS3kE8+P; 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 7266C187E39; Wed, 2 Mar 2022 15:15:47 -0800 (PST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242422AbiCBTwa (ORCPT + 99 others); Wed, 2 Mar 2022 14:52:30 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37850 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242348AbiCBTw3 (ORCPT ); Wed, 2 Mar 2022 14:52:29 -0500 Received: from mail-pl1-x631.google.com (mail-pl1-x631.google.com [IPv6:2607:f8b0:4864:20::631]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5B893D95E1 for ; Wed, 2 Mar 2022 11:51:45 -0800 (PST) Received: by mail-pl1-x631.google.com with SMTP id z2so2485816plg.8 for ; Wed, 02 Mar 2022 11:51:45 -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=K9X7JrhsxNcS6upS7mNhAwW5h+M5okT8FoWI5/raP6I=; b=sS3kE8+PStEGIXHDPWwCcXaFdEQhEIzd9oj0L9waQDDookmKz6rjd8d6Vu5NBSuNj4 nqCMAfhxTsJSS5wE+scTdZznIvWkvkwDUEnyfIJaUTCpy6gXZl4J8PcNpTqohsjMhuFf 6vjSo1DGJqIWbOtLrL1lPLs2QqQg4xrl+q8hwIV05nAGEVsOLxDAbSLrkwMD8O3Ddh9X mLRB5TuAb+VVk8elJE0i1/xtKirCP4v86NnisaGDLN5/JUEmoC6E1J4xos7OwXt5tIW/ dXzwcxa6VsFzjVF0oKY5oy++wV2V9Au//9o8qjhqvKiKajawoMrL6PXiEHizGYgloNNB 88UA== 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=K9X7JrhsxNcS6upS7mNhAwW5h+M5okT8FoWI5/raP6I=; b=1JPkd73ncjDZZSgsfE0BfD37hESHmVuL/VgE8Jbs4y44nhEjPqBAqu8MBdNV/9y5+U u3sbGgwaOaPENbd2Y3ETQcc+79+UruaqPqj7T+q9iBCDzBSJp67rs56DjgFGOyKWR/nG tzPjzgGLm5rvpXV2u39zjSOiP3ta4JRZGlWaeqgw2PA9vBLDbYGVJ+MFfk5MnCMvUjWQ ++ygbE3f6293Ppxys3HBT5sQyXdEagEenhZZq8wnCHs3YgqpqFMAAIA29ambW77lszes b6Ec1r6bNAS1TjgUVq6Mpf/btfY/ImX2mgogtA+1ffslF3IqUR1GXlR5Uqvggk33BKis i7IA== X-Gm-Message-State: AOAM530EfXtbjdk2bxsPB5UVaXYJIPs8nCV8sBfxKgANBGxeQczZfkA6 rH80vSkrFI2V9qKZSchMnRIUiQ== X-Received: by 2002:a17:90a:5917:b0:1be:d200:2501 with SMTP id k23-20020a17090a591700b001bed2002501mr1473742pji.15.1646250704687; Wed, 02 Mar 2022 11:51:44 -0800 (PST) Received: from google.com (157.214.185.35.bc.googleusercontent.com. [35.185.214.157]) by smtp.gmail.com with ESMTPSA id g17-20020a62e311000000b004def1931fbcsm13903028pfh.63.2022.03.02.11.51.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Mar 2022 11:51:44 -0800 (PST) Date: Wed, 2 Mar 2022 19:51:40 +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 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: 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 Wed, Mar 02, 2022, Mingwei Zhang wrote: > 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? -1 is arbitrary, any non-zero value would work. More below. > > /* > > * 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)) && This is the code that deals with the enums. It's making the type a tri-state, where the values of VALID_ROOTS and INVALID_ROOTS align with converting role.invalid to a boolean (always '0' or '1') so that they can be directly compared as above. Any value for ALL_ROOTS (other than '0' or '1' obviously) would work since the above logic requires ALL_ROOTS to be explicitly checked first. > > kvm_tdp_mmu_get_root(next_root)) > > break; > >