Received: by 2002:a05:6a10:1a4d:0:0:0:0 with SMTP id nk13csp3661981pxb; Fri, 11 Feb 2022 05:09:02 -0800 (PST) X-Google-Smtp-Source: ABdhPJxjX/p+yhQfbPn2P5DfQkDa1xKU+rDDh7YtF9XT3cNmRbJuKkbYtblqoJRlsWadlA8OmwBN X-Received: by 2002:a17:902:ed89:: with SMTP id e9mr1582290plj.88.1644584942715; Fri, 11 Feb 2022 05:09:02 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1644584942; cv=none; d=google.com; s=arc-20160816; b=jOT94AccAVvKmHuV75ELLK9BqeuIMa4dsZ5sF7Gb307cc+jR5Qoh0ilJQEoSlLWCcd bTzAATGF3X1RNNzP31wDGQ3FY9QLSVmyPJDuR4y29aaw+1ogz5hLPNjaQajJqvaRWI08 a0yC+WXeMvVyob/FtU5Y21uJjRlIDVJViZgclPzGgMrp1zHfvgWkM1lJ82aPvRNDeBMI 6h2AOqkOxMa8dZ2Yd206zPHPZffQMXUSOPHBjKzabSvymYbKqhjnGUtqu7IIee2vqwlb A53mhuKt3/S1dZFMnMQ+IZ1USPdviCka68iKeIBLcSG5gnVpxtJiGENnP9wkLVyxn5rh 5h+w== 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=gSx28UKeCMp/oRLOiqMvFzHyx/ZVhU1gpNBGY870w4s=; b=bX+402b0uP0AELgLee/QjDa6GjSL2z6p28SQ5hEUOfAq2Hg24d8LHDSJPfklbRo+VK cG4uvScyHZQAHNzVGdHJa5/flC3yuK/lv03onjuNBMMIx7iyOGCuzTR44WGiKPI+psgu vjRZpMqLfbdpC3Uhg6/PXuIDOWGwxoEt0x88zJqt23cU9SPqTt/pgtovYnYiGsKSRlhO 4+50B/O07nsxonLW6ePpaozGyCc1FohTYnSPDgnu/vEPMyDvc2jhq8eAwstDkGQxdZ+i 75NYtx7Hh0EVHnK4mLddyk9s0y22a/R0+7aTYvDSmqwjTuF1fpUtNHgqx32I1YjmmDmc Zrpg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=gTn6ZDSc; 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 e7si5125219plh.513.2022.02.11.05.08.48; Fri, 11 Feb 2022 05:09:02 -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=gTn6ZDSc; 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 S1346569AbiBKAyh (ORCPT + 99 others); Thu, 10 Feb 2022 19:54:37 -0500 Received: from mxb-00190b01.gslb.pphosted.com ([23.128.96.19]:42546 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345588AbiBKAyg (ORCPT ); Thu, 10 Feb 2022 19:54:36 -0500 Received: from mail-pf1-x431.google.com (mail-pf1-x431.google.com [IPv6:2607:f8b0:4864:20::431]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2E14CF24 for ; Thu, 10 Feb 2022 16:54:37 -0800 (PST) Received: by mail-pf1-x431.google.com with SMTP id i21so11869694pfd.13 for ; Thu, 10 Feb 2022 16:54:37 -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=gSx28UKeCMp/oRLOiqMvFzHyx/ZVhU1gpNBGY870w4s=; b=gTn6ZDScoBvH/pGmW3TK5xT8a9Hgez9llKDPunKEUTCGGGYm9Ke49V5aBzNJuSDL6j VKGUmJBdSr0LCili5IUypGaVbPqYt74yidh+hr07HSUI+/CfS3UIvZlipnxFOOi4g3Is i2V4lrazCctH1SCM/lsDxsSUz0fuTjH6Ctn9uzDMWkM3R/27da7zmgBfTSKwJdMhduwm aW6VMBM8bN3bLbB9GFLrn6ytXaxyAkRKB8RcF1n62aGANIyKMOna6IXJnEoXlNvN09uq 0fXfqBl7W7iC6Gr987ZoMHEGJhDOf87jFBJSitXNyJWqlmEfWmyg8IfpRAoYkGrMjfaW mvRg== 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=gSx28UKeCMp/oRLOiqMvFzHyx/ZVhU1gpNBGY870w4s=; b=UMC0qid/fgzh8+r0hXbovhJ+yl3r7nd3eckwWbXeTJIaAHP0LaUtYikUu4xT48KIIh e5qJ4M05gHPMV6jjmay0GSidDpIC7c6vwM+AAZ4pyjtBGY1qdw9r6q6ibam3B0X5RP36 0ZSz+tNlq6CyGp7A4sFbA4w6PX0RTf8mzwEYfS/KjuWaxVUhmA9T7X5jdweiKOXcFIam RfQVgnIjw4S0f/PBLQSOI6sZjUrCoFmdQ7gu9hqjVoZT+7knUxLkYbTD9AGkkeTkvTgO 2Fh1NyqLOf1kwpJUBvpDJadEfeLLu5oJYzuuAYU8jbjWNwDa8jxqRsHvzoXF/iCX7FIi hvkA== X-Gm-Message-State: AOAM533lFb9pQdKoRwweF302G5MRCDbwUT7z2PRMJ7fmmG97xq9tfdMl 2TZoYc8D0YJOiYuaqBqDdvfUYw== X-Received: by 2002:a63:1249:: with SMTP id 9mr8333712pgs.417.1644540876576; Thu, 10 Feb 2022 16:54:36 -0800 (PST) Received: from google.com (157.214.185.35.bc.googleusercontent.com. [35.185.214.157]) by smtp.gmail.com with ESMTPSA id nu15sm3625642pjb.5.2022.02.10.16.54.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 10 Feb 2022 16:54:35 -0800 (PST) Date: Fri, 11 Feb 2022 00:54:32 +0000 From: Sean Christopherson To: Paolo Bonzini Cc: linux-kernel@vger.kernel.org, kvm@vger.kernel.org, vkuznets@redhat.com, mlevitsk@redhat.com, dmatlack@google.com Subject: Re: [PATCH 08/12] KVM: MMU: do not consult levels when freeing roots Message-ID: References: <20220209170020.1775368-1-pbonzini@redhat.com> <20220209170020.1775368-9-pbonzini@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-17.6 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 Fri, Feb 11, 2022, Sean Christopherson wrote: > On Wed, Feb 09, 2022, Paolo Bonzini wrote: > > Right now, PGD caching requires a complicated dance of first computing > > the MMU role and passing it to __kvm_mmu_new_pgd, and then separately calling > > Nit, adding () after function names helps readers easily recognize when you're > taking about a specific function, e.g. as opposed to a concept or whatever. > > > kvm_init_mmu. > > > > Part of this is due to kvm_mmu_free_roots using mmu->root_level and > > mmu->shadow_root_level to distinguish whether the page table uses a single > > root or 4 PAE roots. Because kvm_init_mmu can overwrite mmu->root_level, > > kvm_mmu_free_roots must be called before kvm_init_mmu. > > > > However, even after kvm_init_mmu there is a way to detect whether the page table > > has a single root or four, because the pae_root does not have an associated > > struct kvm_mmu_page. > > Suggest a reword on the final paragraph, because there's a discrepancy with the > code (which handles 0, 1, or 4 "roots", versus just "single or four"). > > However, even after kvm_init_mmu() there is a way to detect whether the > page table may hold PAE roots, as root.hpa isn't backed by a shadow when > it points at PAE roots. > > > Signed-off-by: Paolo Bonzini > > --- > > arch/x86/kvm/mmu/mmu.c | 10 ++++++---- > > 1 file changed, 6 insertions(+), 4 deletions(-) > > > > diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c > > index 3c3f597ea00d..95d0fa0bb876 100644 > > --- a/arch/x86/kvm/mmu/mmu.c > > +++ b/arch/x86/kvm/mmu/mmu.c > > @@ -3219,12 +3219,15 @@ void kvm_mmu_free_roots(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu, > > struct kvm *kvm = vcpu->kvm; > > int i; > > LIST_HEAD(invalid_list); > > - bool free_active_root = roots_to_free & KVM_MMU_ROOT_CURRENT; > > + bool free_active_root; > > > > BUILD_BUG_ON(KVM_MMU_NUM_PREV_ROOTS >= BITS_PER_LONG); > > > > /* Before acquiring the MMU lock, see if we need to do any real work. */ > > - if (!(free_active_root && VALID_PAGE(mmu->root.hpa))) { > > + free_active_root = (roots_to_free & KVM_MMU_ROOT_CURRENT) > > + && VALID_PAGE(mmu->root.hpa); > > free_active_root = (roots_to_free & KVM_MMU_ROOT_CURRENT) && > VALID_PAGE(mmu->root.hpa); > > Isn't this a separate bug fix? E.g. call kvm_mmu_unload() without a valid current > root, but with valid previous roots? In which case we'd try to free garbage, no? > > > + > > + if (!free_active_root) { > > for (i = 0; i < KVM_MMU_NUM_PREV_ROOTS; i++) > > if ((roots_to_free & KVM_MMU_ROOT_PREVIOUS(i)) && > > VALID_PAGE(mmu->prev_roots[i].hpa)) > > @@ -3242,8 +3245,7 @@ void kvm_mmu_free_roots(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu, > > &invalid_list); > > > > if (free_active_root) { > > - if (mmu->shadow_root_level >= PT64_ROOT_4LEVEL && > > - (mmu->root_level >= PT64_ROOT_4LEVEL || mmu->direct_map)) { > > + if (to_shadow_page(mmu->root.hpa)) { > > mmu_free_root_page(kvm, &mmu->root.hpa, &invalid_list); > > } else if (mmu->pae_root) { Gah, this is technically wrong. It shouldn't truly matter, but it's wrong. root.hpa will not be backed by shadow page if the root is pml4_root or pml5_root, in which case freeing the PAE root is wrong. They should obviously be invalid already, but it's a little confusing because KVM wanders down a path that may not be relevant to the current mode. For clarity, I think it's worth doing: } else if (mmu->root.hpa == __pa(mmu->pae_root)) { > > for (i = 0; i < 4; ++i) { > > -- > > 2.31.1 > > > >