Received: by 2002:a05:7412:31a9:b0:e2:908c:2ebd with SMTP id et41csp3492684rdb; Wed, 13 Sep 2023 13:51:33 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHmhaZ1zA+9DtaL7I0Zt4jYoxl8/7oRSEVuKE74I1c1lrLZAFFIu1BEK9NDmLmYFsuSGqsZ X-Received: by 2002:a05:6358:52c8:b0:134:e458:688d with SMTP id z8-20020a05635852c800b00134e458688dmr5099214rwz.15.1694638292836; Wed, 13 Sep 2023 13:51:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694638292; cv=none; d=google.com; s=arc-20160816; b=qcb0VaHZKFBCg3IlSlKzkUY9zjVU9R/txSuiA9dRK+JmCIJLDtKigjT4Ckyuzdml1t HmkO/4Oe+pu7JCKg9rnJLwQlYW4ibxF+76h5NgmdlaakfFaeE0U1Gj4Z3MEmsFACswfV nWSU1wAD+891w3XyH//7XbkbSbg1Xi91QNSZlnjUwVMXuE6OzCsNngkIC57DDmgvUKV5 +7vcSCHgiG5JehFgTfnnj8ERaPNv2xLnjN77DIbVEOHYKKQceJfSqDDxqMiAUnyEZO8X nZeZuff5LIM9xDfaKyNKLCVq7o+D3Zw+2Z2xhQvWwdgcy5iBcS5MQN33nMeAWEH2BcJS scOA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=Zy/+yYsX4Y2EXbKaPc9rlY6wbSEfBepJpHDj/vaSjKs=; fh=XU07i6VjSHyq3GxtsXSBrQ3+Dy8rztqr00WST2j1mn0=; b=GzBSPQ+q/EJFe2QC5KTyb6cq4ksr1Ns2WPHQkWeG2Xr9oHriggCDt4Dplbn/hdfRGI WeaFWuyKw/r+hlfcEdL6i2Ya7FDa89iCEfYd8ZBP2/2b+2XB61OSjiBLnYSOd1mIfe1I 4U5s3rymcJRtC36U5ZbdT/S+mKynggjiL4otK3fdm5vkAWuNzQ0EGUXBIoVUY1R+dkb7 6JJ1byVoOA9kLtpdt7bIsA07M9r63HLRfoSdcsN1066z5Fi+6htMChYwpMVNT/ZzTs1v nHq6Ppl3bxdMGwyHsqLvo+YMk6O/r+GR6RWJpVWPWkx8tOJToXw/YhJORTlqre1jovvF cHzQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=a2wrrql0; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from morse.vger.email (morse.vger.email. [23.128.96.31]) by mx.google.com with ESMTPS id a73-20020a63904c000000b005775302b11csi8132715pge.81.2023.09.13.13.51.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Sep 2023 13:51:32 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) client-ip=23.128.96.31; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=a2wrrql0; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by morse.vger.email (Postfix) with ESMTP id C4EAA801F9A6; Wed, 13 Sep 2023 08:40:21 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230034AbjIMPkI (ORCPT + 99 others); Wed, 13 Sep 2023 11:40:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51692 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229936AbjIMPkB (ORCPT ); Wed, 13 Sep 2023 11:40:01 -0400 Received: from mgamail.intel.com (mgamail.intel.com [192.55.52.93]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 85E551BFC; Wed, 13 Sep 2023 08:39:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1694619595; x=1726155595; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=rYEBqAIo0P765JtuiX1UKxLDu6cAJk+hM7akS4M4yss=; b=a2wrrql0Qyy931qD2s1d3PnwYKRIRJGdc0Ex6doTexFeyHZ9MUjmhmKf olZAW+ad5gxQ0DGMlwiFN/TRkDHE3kT3KwgtpcX9FJBfFlez1UfBSurMF Vi1wbzBlfg2YhjLgE+VCCLMS8yh0Nf/Yuw/4e12MvVrd38SEslQdHBGK1 oUPN0+zEiNtcKlJf92AvTcbN7ulnhgKrJ7sPfU5nHp6Srhn8r3wyY/d6t 1bk5c/kavXZrRwgzoJspk9E6vhmLx+l6rHCNOpyxGwPuRk3vKGAFmkQZN 5ikYABVfZptb8eYoIyodrNn+vj739Qk+rRuIU/43cpp49zihb8o0s2WO4 g==; X-IronPort-AV: E=McAfee;i="6600,9927,10832"; a="376030175" X-IronPort-AV: E=Sophos;i="6.02,143,1688454000"; d="scan'208";a="376030175" Received: from orsmga004.jf.intel.com ([10.7.209.38]) by fmsmga102.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 13 Sep 2023 08:39:55 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10832"; a="867852088" X-IronPort-AV: E=Sophos;i="6.02,143,1688454000"; d="scan'208";a="867852088" Received: from binbinwu-mobl.ccr.corp.intel.com (HELO binbinwu-mobl.sh.intel.com) ([10.93.2.44]) by orsmga004-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 13 Sep 2023 08:39:52 -0700 From: Binbin Wu To: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Cc: seanjc@google.com, pbonzini@redhat.com, chao.gao@intel.com, kai.huang@intel.com, David.Laight@ACULAB.COM, robert.hu@linux.intel.com, guang.zeng@intel.com, binbin.wu@linux.intel.com Subject: [PATCH v11 05/16] KVM: x86/mmu: Drop non-PA bits when getting GFN for guest's PGD Date: Wed, 13 Sep 2023 20:42:16 +0800 Message-Id: <20230913124227.12574-6-binbin.wu@linux.intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230913124227.12574-1-binbin.wu@linux.intel.com> References: <20230913124227.12574-1-binbin.wu@linux.intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (morse.vger.email [0.0.0.0]); Wed, 13 Sep 2023 08:40:22 -0700 (PDT) X-Spam-Status: No, score=-0.8 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on morse.vger.email Drop non-PA bits when getting GFN for guest's PGD with the maximum theoretical mask for guest MAXPHYADDR. Do it unconditionally because it's harmless for 32-bit guests, querying 64-bit mode would be more expensive, and for EPT the mask isn't tied to guest mode. Using PT_BASE_ADDR_MASK would be technically wrong (PAE paging has 64-bit elements _excpet_ for CR3, which has only 32 valid bits), it wouldn't matter in practice though. Opportunistically use GENMASK_ULL() to define __PT_BASE_ADDR_MASK. Signed-off-by: Binbin Wu Tested-by: Xuelian Guo --- arch/x86/kvm/mmu/mmu.c | 2 +- arch/x86/kvm/mmu/mmu_internal.h | 1 + arch/x86/kvm/mmu/paging_tmpl.h | 2 +- 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c index e1d011c67cc6..f316df038e61 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -3774,7 +3774,7 @@ static int mmu_alloc_shadow_roots(struct kvm_vcpu *vcpu) hpa_t root; root_pgd = kvm_mmu_get_guest_pgd(vcpu, mmu); - root_gfn = root_pgd >> PAGE_SHIFT; + root_gfn = (root_pgd & __PT_BASE_ADDR_MASK) >> PAGE_SHIFT; if (!kvm_vcpu_is_visible_gfn(vcpu, root_gfn)) { mmu->root.hpa = kvm_mmu_get_dummy_root(); diff --git a/arch/x86/kvm/mmu/mmu_internal.h b/arch/x86/kvm/mmu/mmu_internal.h index b102014e2c60..b5aca7560fd0 100644 --- a/arch/x86/kvm/mmu/mmu_internal.h +++ b/arch/x86/kvm/mmu/mmu_internal.h @@ -13,6 +13,7 @@ #endif /* Page table builder macros common to shadow (host) PTEs and guest PTEs. */ +#define __PT_BASE_ADDR_MASK GENMASK_ULL(51, 12) #define __PT_LEVEL_SHIFT(level, bits_per_level) \ (PAGE_SHIFT + ((level) - 1) * (bits_per_level)) #define __PT_INDEX(address, level, bits_per_level) \ diff --git a/arch/x86/kvm/mmu/paging_tmpl.h b/arch/x86/kvm/mmu/paging_tmpl.h index c85255073f67..4d4e98fe4f35 100644 --- a/arch/x86/kvm/mmu/paging_tmpl.h +++ b/arch/x86/kvm/mmu/paging_tmpl.h @@ -62,7 +62,7 @@ #endif /* Common logic, but per-type values. These also need to be undefined. */ -#define PT_BASE_ADDR_MASK ((pt_element_t)(((1ULL << 52) - 1) & ~(u64)(PAGE_SIZE-1))) +#define PT_BASE_ADDR_MASK ((pt_element_t)__PT_BASE_ADDR_MASK) #define PT_LVL_ADDR_MASK(lvl) __PT_LVL_ADDR_MASK(PT_BASE_ADDR_MASK, lvl, PT_LEVEL_BITS) #define PT_LVL_OFFSET_MASK(lvl) __PT_LVL_OFFSET_MASK(PT_BASE_ADDR_MASK, lvl, PT_LEVEL_BITS) #define PT_INDEX(addr, lvl) __PT_INDEX(addr, lvl, PT_LEVEL_BITS) -- 2.25.1