From patchwork Wed Oct 19 16:56:16 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 5761 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4ac7:0:0:0:0:0 with SMTP id y7csp432940wrs; Wed, 19 Oct 2022 10:00:36 -0700 (PDT) X-Google-Smtp-Source: AMsMyM7xzOf/hl74Lv6qjpv0jestIm7sTrsJ92h2nc/DKGXU8yS0LHML5CpxZTXGsOfdbJJjGxyb X-Received: by 2002:aa7:cd74:0:b0:45c:78fb:1d89 with SMTP id ca20-20020aa7cd74000000b0045c78fb1d89mr8291578edb.87.1666198836200; Wed, 19 Oct 2022 10:00:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666198836; cv=none; d=google.com; s=arc-20160816; b=06qdEwv1qHdlD5TpDGY46SikBQPvafkSK4W1uV8mGTHN8+LdqXWuUg0fCEx0q6PQ1C AK8KHg4sYBqvS13i7DS/jjXDFOdCfyIrgVzQgNqAVdgGyajV+f3zEfkIBEBpP5NFgi66 BviVqoyN+dcduLBvAMe07mBq+V2488u4ThWkHcKHjOmQmscTP8oHyA1+pIefj17RlelT CY3rfpu+21eGQfsszlbpn0zr74SW3XEg5fqI7V+QiY5VBbhy41bzvwPyZxqERjocKCMr o5EOKUL2XPrZ/+CGsNsiRFIowagktVEF6MBHOIRKI/KRWRLQQdD9ag067WLEG+mMkHzu fgVw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:reply-to:dkim-signature; bh=waroNZFBJLQwvUgcfZIcey5TzJEOeoUZhGZO7shkmD4=; b=y6xFg7oVK8/ymiI5cpBAsOP1yD7h5PcjDzIuL7atby5wtVFLBiMhE0k/rXhPmEOkKw cHzUJGhVLdXCCJO3sS/QnVsIAKNVHIMHOA3GAwCKk67EDMZ8huMkzCR05nWkfTXXi+t0 oVhZWL4Ce5hfnEbFAaoBXyVzzR4lXAihzXJdqpN0Ikaq4uXEqjOR7saYGCaPGDQ9mK5B tT1NZSXSf81ilJ+NJYpAtTK1d+OOg8mQfD57Wul9F0jsfORiHLYOBb857OVxdUsrgoF3 c5MjUtk0ul9uN6PQYRXQVFevVMAKW0zzN5l9ENCmgvuc1+M/BBjyUwIc0F/hbEGGjxPk jOMw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=e3P08m4q; 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: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id e27-20020a50d4db000000b004594ac619b4si12836939edj.245.2022.10.19.10.00.09; Wed, 19 Oct 2022 10:00:36 -0700 (PDT) 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=e3P08m4q; 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 S231397AbiJSQ5K (ORCPT + 99 others); Wed, 19 Oct 2022 12:57:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37394 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231193AbiJSQ4i (ORCPT ); Wed, 19 Oct 2022 12:56:38 -0400 Received: from mail-pl1-x649.google.com (mail-pl1-x649.google.com [IPv6:2607:f8b0:4864:20::649]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0954A1D064C for ; Wed, 19 Oct 2022 09:56:33 -0700 (PDT) Received: by mail-pl1-x649.google.com with SMTP id b14-20020a170902d50e00b001854f631c4eso6828821plg.8 for ; Wed, 19 Oct 2022 09:56:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=waroNZFBJLQwvUgcfZIcey5TzJEOeoUZhGZO7shkmD4=; b=e3P08m4q0X038sMHBa7OpQBM91n7Ix99ZqQp7w8a4ngY84stpxKOpAS0LH0DxkudXa SxRdayNSOHpNA1a6MRDLHSkqmPNogQcLiNzgVsg6mOkE/3gKwKLWSmfQJtFEgFvtxswj QI49YQrjbyRhGIbq1OBl7DsSgaKWLLNUDYRpL3FFV6KpsUMoOTuVUyTHFSCylEFqyOY4 OPNVJXAB/Jda/Jki2rwzILN54htLopyJjnhCT5gxOeCD4EVWmHB0Y3E32Xoq7AXsww3E Em1WA8pdw+VaaObOsGKceZcu+YLwjkM+VT31zVYPy/KQ+CaBQ1SHrNZo9t1Gok+7mTjo dkww== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=waroNZFBJLQwvUgcfZIcey5TzJEOeoUZhGZO7shkmD4=; b=1HsDIYOoZZzR2e84APh1Zl5TNu15Tvc4YfsBXcnTFnvXWETu3l8zJmcsxy9BFnDKR4 7V/VAdeHrFhsWZh6o19o9ZS01tCnt0YY//nSaRi+kr+dEp+C8WPyiNO866mBr5/Mcppb oUMx0ucPZHecaXl0MuObVcbZfco5aJydWA6Fh55wV1i27sB5S/X14il7jQX82qoy2SRe b6z0VVZLimVROg8fQ3LOFALWXujNJR5SU0e3li6de1Med0JHIoCTpqzDcFlMRgu0AsLm UI7qLXebGaOGfgC8pRUTkKn5Ss2wlUKDUWs7qr2cbSeADpiIoJa4snU582aFIxwvaKzp 6Kvw== X-Gm-Message-State: ACrzQf0UL/FLnZ8Y5csYZQJX9b/A0XVREzd6m7bfvFyMU3AbEwWqEO3O SBl2y9GEpDlLCGhKaHumDR6qDUhMcnY= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:90a:c986:b0:205:f08c:a82b with SMTP id w6-20020a17090ac98600b00205f08ca82bmr2823094pjt.1.1666198592333; Wed, 19 Oct 2022 09:56:32 -0700 (PDT) Reply-To: Sean Christopherson Date: Wed, 19 Oct 2022 16:56:16 +0000 In-Reply-To: <20221019165618.927057-1-seanjc@google.com> Mime-Version: 1.0 References: <20221019165618.927057-1-seanjc@google.com> X-Mailer: git-send-email 2.38.0.413.g74048e4d9e-goog Message-ID: <20221019165618.927057-7-seanjc@google.com> Subject: [PATCH v6 6/8] KVM: x86/mmu: Add helper to convert SPTE value to its shadow page From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Mingwei Zhang , David Matlack , Yan Zhao , Ben Gardon X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1747136110380758286?= X-GMAIL-MSGID: =?utf-8?q?1747136110380758286?= Add a helper to convert a SPTE to its shadow page to deduplicate a variety of flows and hopefully avoid future bugs, e.g. if KVM attempts to get the shadow page for a SPTE without dropping high bits. Opportunistically add a comment in mmu_free_root_page() documenting why it treats the root HPA as a SPTE. No functional change intended. Signed-off-by: Sean Christopherson --- arch/x86/kvm/mmu/mmu.c | 17 ++++++++++------- arch/x86/kvm/mmu/mmu_internal.h | 12 ------------ arch/x86/kvm/mmu/spte.h | 17 +++++++++++++++++ arch/x86/kvm/mmu/tdp_mmu.h | 2 ++ 4 files changed, 29 insertions(+), 19 deletions(-) diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c index 57c7c52d137a..f4f1b1591a02 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -1818,7 +1818,7 @@ static int __mmu_unsync_walk(struct kvm_mmu_page *sp, continue; } - child = to_shadow_page(ent & SPTE_BASE_ADDR_MASK); + child = spte_to_child_sp(ent); if (child->unsync_children) { if (mmu_pages_add(pvec, child, i)) @@ -2377,7 +2377,7 @@ static void validate_direct_spte(struct kvm_vcpu *vcpu, u64 *sptep, * so we should update the spte at this point to get * a new sp with the correct access. */ - child = to_shadow_page(*sptep & SPTE_BASE_ADDR_MASK); + child = spte_to_child_sp(*sptep); if (child->role.access == direct_access) return; @@ -2398,7 +2398,7 @@ static int mmu_page_zap_pte(struct kvm *kvm, struct kvm_mmu_page *sp, if (is_last_spte(pte, sp->role.level)) { drop_spte(kvm, spte); } else { - child = to_shadow_page(pte & SPTE_BASE_ADDR_MASK); + child = spte_to_child_sp(pte); drop_parent_pte(child, spte); /* @@ -2837,7 +2837,7 @@ static int mmu_set_spte(struct kvm_vcpu *vcpu, struct kvm_memory_slot *slot, struct kvm_mmu_page *child; u64 pte = *sptep; - child = to_shadow_page(pte & SPTE_BASE_ADDR_MASK); + child = spte_to_child_sp(pte); drop_parent_pte(child, sptep); flush = true; } else if (pfn != spte_to_pfn(*sptep)) { @@ -3449,7 +3449,11 @@ static void mmu_free_root_page(struct kvm *kvm, hpa_t *root_hpa, if (!VALID_PAGE(*root_hpa)) return; - sp = to_shadow_page(*root_hpa & SPTE_BASE_ADDR_MASK); + /* + * The "root" may be a special root, e.g. a PAE entry, treat it as a + * SPTE to ensure any non-PA bits are dropped. + */ + sp = spte_to_child_sp(*root_hpa); if (WARN_ON(!sp)) return; @@ -3934,8 +3938,7 @@ void kvm_mmu_sync_roots(struct kvm_vcpu *vcpu) hpa_t root = vcpu->arch.mmu->pae_root[i]; if (IS_VALID_PAE_ROOT(root)) { - root &= SPTE_BASE_ADDR_MASK; - sp = to_shadow_page(root); + sp = spte_to_child_sp(root); mmu_sync_children(vcpu, sp, true); } } diff --git a/arch/x86/kvm/mmu/mmu_internal.h b/arch/x86/kvm/mmu/mmu_internal.h index 22152241bd29..dbaf6755c5a7 100644 --- a/arch/x86/kvm/mmu/mmu_internal.h +++ b/arch/x86/kvm/mmu/mmu_internal.h @@ -133,18 +133,6 @@ struct kvm_mmu_page { extern struct kmem_cache *mmu_page_header_cache; -static inline struct kvm_mmu_page *to_shadow_page(hpa_t shadow_page) -{ - struct page *page = pfn_to_page(shadow_page >> PAGE_SHIFT); - - return (struct kvm_mmu_page *)page_private(page); -} - -static inline struct kvm_mmu_page *sptep_to_sp(u64 *sptep) -{ - return to_shadow_page(__pa(sptep)); -} - static inline int kvm_mmu_role_as_id(union kvm_mmu_page_role role) { return role.smm ? 1 : 0; diff --git a/arch/x86/kvm/mmu/spte.h b/arch/x86/kvm/mmu/spte.h index 7670c13ce251..7e5343339b90 100644 --- a/arch/x86/kvm/mmu/spte.h +++ b/arch/x86/kvm/mmu/spte.h @@ -219,6 +219,23 @@ static inline int spte_index(u64 *sptep) */ extern u64 __read_mostly shadow_nonpresent_or_rsvd_lower_gfn_mask; +static inline struct kvm_mmu_page *to_shadow_page(hpa_t shadow_page) +{ + struct page *page = pfn_to_page((shadow_page) >> PAGE_SHIFT); + + return (struct kvm_mmu_page *)page_private(page); +} + +static inline struct kvm_mmu_page *spte_to_child_sp(u64 spte) +{ + return to_shadow_page(spte & SPTE_BASE_ADDR_MASK); +} + +static inline struct kvm_mmu_page *sptep_to_sp(u64 *sptep) +{ + return to_shadow_page(__pa(sptep)); +} + static inline bool is_mmio_spte(u64 spte) { return (spte & shadow_mmio_mask) == shadow_mmio_value && diff --git a/arch/x86/kvm/mmu/tdp_mmu.h b/arch/x86/kvm/mmu/tdp_mmu.h index c163f7cc23ca..d3714200b932 100644 --- a/arch/x86/kvm/mmu/tdp_mmu.h +++ b/arch/x86/kvm/mmu/tdp_mmu.h @@ -5,6 +5,8 @@ #include +#include "spte.h" + hpa_t kvm_tdp_mmu_get_vcpu_root_hpa(struct kvm_vcpu *vcpu); __must_check static inline bool kvm_tdp_mmu_get_root(struct kvm_mmu_page *root)