Received: by 2002:a05:6a10:1a4d:0:0:0:0 with SMTP id nk13csp3575547pxb; Fri, 11 Feb 2022 03:04:27 -0800 (PST) X-Google-Smtp-Source: ABdhPJy5zeVKTRXXXemCl+tMqUKZQAMIXSXtm5OPzzQkIoMx+rEqEFGDwi8J2b/UzOH4Xk1Ss/kw X-Received: by 2002:a05:6402:42d4:: with SMTP id i20mr1273325edc.306.1644577466989; Fri, 11 Feb 2022 03:04:26 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1644577466; cv=none; d=google.com; s=arc-20160816; b=AZaIOkvsy8pi7gwlcR4ogwao0lHDpKZYj9q/3lgyANjhdyK4zvgVWthK31yEeJ7VH4 obfnSjNKWrdB7L3rDxYTr6kpj2h7+QA3yoVW4wjMNhZ37EQ7ipSKAfpIspyFA6//RQzx 1LDxa5c9GWKyGd1fUaiafn+QGxRzTl+G4iebfZzB8onu38/qVIfBdY8lWxBur422NDun n/kd8mZ7IjITxdRRmIEjW5ETTeAPlq02zRzUSQAfvFAY+KrnkFoFjYOFgO9kvjN/AxHF wieTL0dWCanFV6qUH26E62t0gW2de7/zU9dq0PGs8gtxbBx28p/fSrSMveMhTr0i6gDr j1Jw== 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=m7OTZN6MMJaOtWh3GEnwnLzNFllEBgIohS86VWKpGKg=; b=tNmfiOWWk8SsjbU7D7GN+Ol/dPLsEwnIrBJ0ITDOuqsLE5BBrhlit8Te593cOI+8Bi vzpeZnyM5UU4z0eIjZZMaD0OzxsVPtMYqSTONQDwRebbiIXimXyhVoEHWZDOh9GJf90E UgF9/f4j0sWVEP3sG4A9xzv27CHMFGDf7yk7fy0ULp0x0Nmvss5+M7Fl/ii0926MjFz9 KMUx3YsqFYhH+XM9a09e/tUaOKKOtbSPort0C0Kz5U+iv3ICsI/64Wg3Ity2hpx+QEoM 7uVoC6neLTb25hG1W+mhRX2yND0NXj4lYmfuLUKk0YxfFcpZuXNTdP4qi31lXPqC1XAu DLGg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=pwTr6Xih; 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 c13si14874330ejj.7.2022.02.11.03.04.01; Fri, 11 Feb 2022 03:04:26 -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=pwTr6Xih; 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 S1346840AbiBKBfr (ORCPT + 99 others); Thu, 10 Feb 2022 20:35:47 -0500 Received: from mxb-00190b01.gslb.pphosted.com ([23.128.96.19]:44762 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1346823AbiBKBfq (ORCPT ); Thu, 10 Feb 2022 20:35:46 -0500 Received: from mail-pf1-x435.google.com (mail-pf1-x435.google.com [IPv6:2607:f8b0:4864:20::435]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 002525F71 for ; Thu, 10 Feb 2022 17:35:46 -0800 (PST) Received: by mail-pf1-x435.google.com with SMTP id i6so11593555pfc.9 for ; Thu, 10 Feb 2022 17:35:46 -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=m7OTZN6MMJaOtWh3GEnwnLzNFllEBgIohS86VWKpGKg=; b=pwTr6XihNQSpOrHNa73cPDI9LUWGbCjQmOTkYSAUF4oHhAL6nnH1MPWpx0PWgO13zw hZQ3dtKXaI+WL0N8yW65h4Gak8Xb9Ojg+TxwGAqzEaj6yB+13WtQRqRn8WKqjtRuAPWw Cwamfr7Gn7rcKUiGHGtprSiAieYVaTRUaAy4wBTh70sYVlIgdwAMxw1u2s10X2KulZ8o SQyabb3il4e7E/NrDxw5k7bFQ40uxVKLYLuPDxGZbjm2g8oxu6TLSeW77Ym1VOvHa5eD v7gTEZ+uu2PNbaZiJ0Nbmg7xaiU8RWfiqgWcyWaYJW0srHhPxIwJDqFnyjKRgW/qPXqW 8TrQ== 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=m7OTZN6MMJaOtWh3GEnwnLzNFllEBgIohS86VWKpGKg=; b=kgLfELQD9Cs8bSL/HaOPwqBH03s7OMPFL/YMwUDMCJyfB493ulYq/j5wFfpiUk4i85 NFQWDsw8sn41cIBKO1QHLiF319ti5WRpyF1Z4vCc7N1HYXvcYHVnjExsZhi9wcRuKwmx K4aM3qrux/+QaCtGdNlsUV1wZu/zkYho81eDIMMh5NoMxWlHCMAB+1e/cQ1pEU8NSsCP WEj03iGQVS7lzVLba3zh7pSGGKswcW8JmSjMhiL2EgiuqUtFL/ADcuWWGGoCyB4WGUhO pF7hCeUNPvSLb7GSMg33EvH6bUsKvPnqXm7MC+draIi6/6pawp4+MNmnXH0aD171Jfoi jsAA== X-Gm-Message-State: AOAM531ZTc5vePCxrzpQ/tc0O6qezXxexS/IeudCxcSH2ED++tsPe5v7 WjsT5xo79apMbJ9s3kwg7ZogKA== X-Received: by 2002:a05:6a00:c83:: with SMTP id a3mr10367017pfv.36.1644543346299; Thu, 10 Feb 2022 17:35:46 -0800 (PST) Received: from google.com (157.214.185.35.bc.googleusercontent.com. [35.185.214.157]) by smtp.gmail.com with ESMTPSA id u25sm2905415pgf.42.2022.02.10.17.35.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 10 Feb 2022 17:35:45 -0800 (PST) Date: Fri, 11 Feb 2022 01:35:42 +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, Paolo Bonzini wrote: > On 2/11/22 01:54, Sean Christopherson wrote: > > > 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? > > mmu_free_root_page checks VALID_PAGE(*root_hpa). If that's what you meant, > then it wouldn't be a preexisting bug (and I think it'd be a fairly common > case). Ahh, yep. > > > > + > > > > + 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. > > pml4_root and pml5_root are dummy, and the first "real" level of page tables > is stored in pae_root for that case too, so I think that should DTRT. Ugh, completely forgot that detail. You're correct. Probably worth a comment? > That's why I also disliked the shadow_root_level/root_level/direct check: > even though there's half a dozen of cases involved, they all boil down to > either 4 pae_roots or a single root with a backing kvm_mmu_page. > > It's even more obscure to check shadow_root_level/root_level/direct in > fast_pgd_switch, where it's pretty obvious that you cannot cache 4 pae_roots > in a single (hpa, pgd) pair... Heh, apparently not obvious enough for me :-)