From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 47CB4C433EF for ; Fri, 11 Feb 2022 00:54:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1346574AbiBKAyh (ORCPT ); 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-Google-Smtp-Source: ABdhPJwMIqBDwfqoiGpu0MHaqps4fYrP/0DFsX46oK5L9gl0Z5pON7Wyrn1x5ew3ygjZ5M2UvVn6wA== 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: 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 > > > >