From nobody Fri Sep 12 14:21:55 2025 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 9EDBBC05027 for ; Thu, 9 Feb 2023 07:23:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229784AbjBIHXD (ORCPT ); Thu, 9 Feb 2023 02:23:03 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34388 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229520AbjBIHW7 (ORCPT ); Thu, 9 Feb 2023 02:22:59 -0500 Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.220.29]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8C0191C5BF for ; Wed, 8 Feb 2023 23:22:30 -0800 (PST) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id EF1505C21E; Thu, 9 Feb 2023 07:22:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1675927348; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=998PlzyGhJ297sizxtNW0VRfCBnaljIvChj+4yF8+uw=; b=dfiL1nOtR014Mz1p84BufnOvHdkW/9BC/jY88D4OMKrpaxgV7v+aR7ILK347DcafNIDpAT HqPk92CNWPr02i1ZqRiA8rKnyIWMI+uCk/fR8+aLa59CYhL2l7v6xeQYDJBQTGQ1JhhhpH PzVfIK7py6ubPSvGI3jAw4Q0rZ1AN+A= Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id A078B1339E; Thu, 9 Feb 2023 07:22:28 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id hPfSJTSf5GPneAAAMHmgww (envelope-from ); Thu, 09 Feb 2023 07:22:28 +0000 From: Juergen Gross To: linux-kernel@vger.kernel.org, x86@kernel.org Cc: lists@nerdbynature.de, mikelley@microsoft.com, torvalds@linux-foundation.org, Juergen Gross , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , "H. Peter Anvin" Subject: [PATCH v2 1/8] x86/mtrr: split off physical address size calculation Date: Thu, 9 Feb 2023 08:22:13 +0100 Message-Id: <20230209072220.6836-2-jgross@suse.com> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20230209072220.6836-1-jgross@suse.com> References: <20230209072220.6836-1-jgross@suse.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Move the calculation of the physical address size in mtrr_bp_init() into a helper function. This will be needed later. Signed-off-by: Juergen Gross --- V2: - new patch --- arch/x86/kernel/cpu/mtrr/mtrr.c | 70 ++++++++++++++++----------------- 1 file changed, 33 insertions(+), 37 deletions(-) diff --git a/arch/x86/kernel/cpu/mtrr/mtrr.c b/arch/x86/kernel/cpu/mtrr/mtr= r.c index 783f3210d582..542ca5639dfd 100644 --- a/arch/x86/kernel/cpu/mtrr/mtrr.c +++ b/arch/x86/kernel/cpu/mtrr/mtrr.c @@ -617,27 +617,11 @@ static struct syscore_ops mtrr_syscore_ops =3D { .resume =3D mtrr_restore, }; =20 -int __initdata changed_by_mtrr_cleanup; - -#define SIZE_OR_MASK_BITS(n) (~((1ULL << ((n) - PAGE_SHIFT)) - 1)) -/** - * mtrr_bp_init - initialize mtrrs on the boot CPU - * - * This needs to be called early; before any of the other CPUs are - * initialized (i.e. before smp_init()). - * - */ -void __init mtrr_bp_init(void) +static unsigned int __init mtrr_calc_physbits(bool generic) { - const char *why =3D "(not available)"; - u32 phys_addr; - - phys_addr =3D 32; + unsigned int phys_addr =3D 32; =20 - if (boot_cpu_has(X86_FEATURE_MTRR)) { - mtrr_if =3D &generic_mtrr_ops; - size_or_mask =3D SIZE_OR_MASK_BITS(36); - size_and_mask =3D 0x00f00000; + if (generic) { phys_addr =3D 36; =20 /* @@ -654,42 +638,54 @@ void __init mtrr_bp_init(void) (boot_cpu_data.x86_stepping =3D=3D 0x3 || boot_cpu_data.x86_stepping =3D=3D 0x4)) phys_addr =3D 36; - - size_or_mask =3D SIZE_OR_MASK_BITS(phys_addr); - size_and_mask =3D ~size_or_mask & 0xfffff00000ULL; } else if (boot_cpu_data.x86_vendor =3D=3D X86_VENDOR_CENTAUR && boot_cpu_data.x86 =3D=3D 6) { /* * VIA C* family have Intel style MTRRs, * but don't support PAE */ - size_or_mask =3D SIZE_OR_MASK_BITS(32); - size_and_mask =3D 0; phys_addr =3D 32; } + } + + size_or_mask =3D ~((1ULL << ((phys_addr) - PAGE_SHIFT)) - 1); + size_and_mask =3D ~size_or_mask & 0xfffff00000ULL; + + return phys_addr; +} + +int __initdata changed_by_mtrr_cleanup; + +/** + * mtrr_bp_init - initialize mtrrs on the boot CPU + * + * This needs to be called early; before any of the other CPUs are + * initialized (i.e. before smp_init()). + * + */ +void __init mtrr_bp_init(void) +{ + const char *why =3D "(not available)"; + unsigned int phys_addr; + + phys_addr =3D mtrr_calc_physbits(boot_cpu_has(X86_FEATURE_MTRR)); + + if (boot_cpu_has(X86_FEATURE_MTRR)) { + mtrr_if =3D &generic_mtrr_ops; } else { switch (boot_cpu_data.x86_vendor) { case X86_VENDOR_AMD: - if (cpu_feature_enabled(X86_FEATURE_K6_MTRR)) { - /* Pre-Athlon (K6) AMD CPU MTRRs */ + /* Pre-Athlon (K6) AMD CPU MTRRs */ + if (cpu_feature_enabled(X86_FEATURE_K6_MTRR)) mtrr_if =3D &amd_mtrr_ops; - size_or_mask =3D SIZE_OR_MASK_BITS(32); - size_and_mask =3D 0; - } break; case X86_VENDOR_CENTAUR: - if (cpu_feature_enabled(X86_FEATURE_CENTAUR_MCR)) { + if (cpu_feature_enabled(X86_FEATURE_CENTAUR_MCR)) mtrr_if =3D ¢aur_mtrr_ops; - size_or_mask =3D SIZE_OR_MASK_BITS(32); - size_and_mask =3D 0; - } break; case X86_VENDOR_CYRIX: - if (cpu_feature_enabled(X86_FEATURE_CYRIX_ARR)) { + if (cpu_feature_enabled(X86_FEATURE_CYRIX_ARR)) mtrr_if =3D &cyrix_mtrr_ops; - size_or_mask =3D SIZE_OR_MASK_BITS(32); - size_and_mask =3D 0; - } break; default: break; --=20 2.35.3 From nobody Fri Sep 12 14:21:55 2025 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 B6DF4C05027 for ; Thu, 9 Feb 2023 07:23:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229866AbjBIHXM (ORCPT ); Thu, 9 Feb 2023 02:23:12 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34628 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229672AbjBIHXF (ORCPT ); Thu, 9 Feb 2023 02:23:05 -0500 Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.220.28]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E5AB31C59E for ; Wed, 8 Feb 2023 23:22:35 -0800 (PST) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id A942F34DB4; Thu, 9 Feb 2023 07:22:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1675927354; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=6KlzZtRVU/BktL0oxuQOwlCZm+eOW7c+Ihxdj/RddRU=; b=SghSS7PtDhKUO4VPKdXdgTYUXvRc6OBO3BazFVVGVDSKJq1l9SDT4wz6p9G9ynon3ndyEI UetnZPhGah5APTgvI58VZrC1vMRgjtivd3NkzGtzqDzXMtLOF+mwAYOpTh4b9s22cC8Fka YnfIYXeaPFKaGefmU0jzkBfIdtmECHk= Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id 5CC2D1339E; Thu, 9 Feb 2023 07:22:34 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id SPNcFTqf5GPzeAAAMHmgww (envelope-from ); Thu, 09 Feb 2023 07:22:34 +0000 From: Juergen Gross To: linux-kernel@vger.kernel.org, x86@kernel.org Cc: lists@nerdbynature.de, mikelley@microsoft.com, torvalds@linux-foundation.org, Juergen Gross , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , "H. Peter Anvin" Subject: [PATCH v2 2/8] x86/mtrr: support setting MTRR state for software defined MTRRs Date: Thu, 9 Feb 2023 08:22:14 +0100 Message-Id: <20230209072220.6836-3-jgross@suse.com> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20230209072220.6836-1-jgross@suse.com> References: <20230209072220.6836-1-jgross@suse.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" When running virtualized, MTRR access can be reduced (e.g. in Xen PV guests or when running as a SEV-SNP guest under Hyper-V). Typically the hypervisor will reset the MTRR feature in cpuid data, resulting in no MTRR memory type information being available for the kernel. This has turned out to result in problems: - Hyper-V SEV-SNP guests using uncached mappings where they shouldn't - Xen PV dom0 mapping memory as WB which should be UC- instead Solve those problems by supporting to set a fixed MTRR state, overwriting the empty state used today. In case such a state has been set, don't call get_mtrr_state() in mtrr_bp_init(). The set state will only be used by mtrr_type_lookup(), as in all other cases mtrr_enabled() is being checked, which will return false. Accept the overwrite call only in case of MTRRs being disabled in cpuid. Signed-off-by: Juergen Gross --- V2: - new patch --- arch/x86/include/asm/mtrr.h | 2 ++ arch/x86/kernel/cpu/mtrr/generic.c | 38 ++++++++++++++++++++++++++++++ arch/x86/kernel/cpu/mtrr/mtrr.c | 9 +++++++ 3 files changed, 49 insertions(+) diff --git a/arch/x86/include/asm/mtrr.h b/arch/x86/include/asm/mtrr.h index f0eeaf6e5f5f..0b8f51d683dc 100644 --- a/arch/x86/include/asm/mtrr.h +++ b/arch/x86/include/asm/mtrr.h @@ -31,6 +31,8 @@ */ # ifdef CONFIG_MTRR void mtrr_bp_init(void); +void mtrr_overwrite_state(struct mtrr_var_range *var, unsigned int num_var, + mtrr_type *fixed, mtrr_type def_type); extern u8 mtrr_type_lookup(u64 addr, u64 end, u8 *uniform); extern void mtrr_save_fixed_ranges(void *); extern void mtrr_save_state(void); diff --git a/arch/x86/kernel/cpu/mtrr/generic.c b/arch/x86/kernel/cpu/mtrr/= generic.c index ee09d359e08f..788bc16888a5 100644 --- a/arch/x86/kernel/cpu/mtrr/generic.c +++ b/arch/x86/kernel/cpu/mtrr/generic.c @@ -240,6 +240,44 @@ static u8 mtrr_type_lookup_variable(u64 start, u64 end= , u64 *partial_end, return mtrr_state.def_type; } =20 +/** + * mtrr_overwrite_state - set fixed MTRR state + * + * Used to set MTRR state via different means (e.g. with data obtained from + * a hypervisor). + */ +void mtrr_overwrite_state(struct mtrr_var_range *var, unsigned int num_var, + mtrr_type *fixed, mtrr_type def_type) +{ + unsigned int i; + + if (boot_cpu_has(X86_FEATURE_MTRR)) + return; + + if (var) { + if (num_var > MTRR_MAX_VAR_RANGES) { + pr_warn("Trying to overwrite MTRR state with %u variable entries\n", + num_var); + num_var =3D MTRR_MAX_VAR_RANGES; + } + for (i =3D 0; i < num_var; i++) + mtrr_state.var_ranges[i] =3D var[i]; + num_var_ranges =3D num_var; + } + + if (fixed) { + for (i =3D 0; i < MTRR_NUM_FIXED_RANGES; i++) + mtrr_state.fixed_ranges[i] =3D fixed[i]; + mtrr_state.enabled |=3D MTRR_STATE_MTRR_FIXED_ENABLED; + mtrr_state.have_fixed =3D 1; + } + + mtrr_state.def_type =3D def_type; + mtrr_state.enabled |=3D MTRR_STATE_MTRR_ENABLED; + + mtrr_state_set =3D 1; +} + /** * mtrr_type_lookup - look up memory type in MTRR * diff --git a/arch/x86/kernel/cpu/mtrr/mtrr.c b/arch/x86/kernel/cpu/mtrr/mtr= r.c index 542ca5639dfd..b73fe243c7fd 100644 --- a/arch/x86/kernel/cpu/mtrr/mtrr.c +++ b/arch/x86/kernel/cpu/mtrr/mtrr.c @@ -668,6 +668,15 @@ void __init mtrr_bp_init(void) const char *why =3D "(not available)"; unsigned int phys_addr; =20 + if (mtrr_state.enabled) { + /* Software overwrite of MTRR state, only for generic case. */ + mtrr_calc_physbits(true); + init_table(); + pr_info("MTRRs set to read-only\n"); + + return; + } + phys_addr =3D mtrr_calc_physbits(boot_cpu_has(X86_FEATURE_MTRR)); =20 if (boot_cpu_has(X86_FEATURE_MTRR)) { --=20 2.35.3 From nobody Fri Sep 12 14:21:55 2025 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 5BDAAC05027 for ; Thu, 9 Feb 2023 07:23:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229551AbjBIHXO (ORCPT ); Thu, 9 Feb 2023 02:23:14 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34608 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229788AbjBIHXJ (ORCPT ); Thu, 9 Feb 2023 02:23:09 -0500 Received: from smtp-out2.suse.de (smtp-out2.suse.de [IPv6:2001:67c:2178:6::1d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C85241EBC1; Wed, 8 Feb 2023 23:22:41 -0800 (PST) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id 7DDD75C221; Thu, 9 Feb 2023 07:22:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1675927360; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=zyHEmKeSFHe5+oN4UisgzjtUbIgCf4lrIbd7KednrR0=; b=k4aWSkE2BdtZFM8V//WNQjG7991Som7Xdthx3JxwnltctXG4G560YCzM645blnAk5C0FSS dJNKo7/JR5JdWeVZi3grOarBdIRkBuONkWYEoPOKCNkJwSTASjeXt1BQ4CDGaZwVjFKYx+ E7kDdY95qpv+pxR1L9jYpElSa/0WIjc= Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id 1FDF71339E; Thu, 9 Feb 2023 07:22:40 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id iS6GBkCf5GP6eAAAMHmgww (envelope-from ); Thu, 09 Feb 2023 07:22:40 +0000 From: Juergen Gross To: linux-kernel@vger.kernel.org, x86@kernel.org, linux-hyperv@vger.kernel.org Cc: lists@nerdbynature.de, mikelley@microsoft.com, torvalds@linux-foundation.org, Juergen Gross , "K. Y. Srinivasan" , Haiyang Zhang , Wei Liu , Dexuan Cui , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , "H. Peter Anvin" Subject: [PATCH v2 3/8] x86/hyperv: set MTRR state when running as SEV-SNP Hyper-V guest Date: Thu, 9 Feb 2023 08:22:15 +0100 Message-Id: <20230209072220.6836-4-jgross@suse.com> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20230209072220.6836-1-jgross@suse.com> References: <20230209072220.6836-1-jgross@suse.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" In order to avoid mappings using the UC- cache attribute, set the MTRR state to use WB caching as the default. This is needed in order to cope with the fact that PAT is enabled, while MTRRs are disabled by the hypervisor. Fixes: 90b926e68f50 ("x86/pat: Fix pat_x_mtrr_type() for MTRR disabled case= ") Signed-off-by: Juergen Gross --- V2: - new patch --- arch/x86/kernel/cpu/mshyperv.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/arch/x86/kernel/cpu/mshyperv.c b/arch/x86/kernel/cpu/mshyperv.c index 46668e255421..51e47dc0e987 100644 --- a/arch/x86/kernel/cpu/mshyperv.c +++ b/arch/x86/kernel/cpu/mshyperv.c @@ -34,6 +34,7 @@ #include #include #include +#include =20 /* Is Linux running as the root partition? */ bool hv_root_partition; @@ -335,6 +336,13 @@ static void __init ms_hyperv_init_platform(void) static_branch_enable(&isolation_type_snp); #ifdef CONFIG_SWIOTLB swiotlb_unencrypted_base =3D ms_hyperv.shared_gpa_boundary; +#endif +#ifdef CONFIG_MTRR + /* + * Set WB as the default cache mode in case MTRRs are + * disabled by the hypervisor. + */ + mtrr_overwrite_state(NULL, 0, NULL, MTRR_TYPE_WRBACK); #endif } /* Isolation VMs are unenlightened SEV-based VMs, thus this check: */ --=20 2.35.3 From nobody Fri Sep 12 14:21:55 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=quarantine dis=none) header.from=suse.com ARC-Seal: i=1; a=rsa-sha256; t=1675927404; cv=none; d=zohomail.com; s=zohoarc; b=JZCh70i+d0wHgY6RLU3wKKtQL1dwBmnN97apAMErKALKfrv1WV7JPvgNJL811W0F8ia89ZjsCHxD3yVRmQ3+yHjuBU5xZEsozH3LxaggMQ1tpF1zurYrfoxA9QPhRMxWJ6gIoVQ8MgmJb0MeeKbVFR65sVZpfYLbkIFsDZ32XpI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1675927404; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=jPHTwfsDZTgmdyR5HshjupjvhAk0TKRehG5uFEDCaGY=; b=JYIU6CZ/hjZO1Jv5s8qKp3B45e01ukH84UAyFzi+s7TUGwUA7k2fCOZqdn+GRoSiRkPgKk4yZY5Xyhx7/P+P9nhsvH5D9QMuBdIs7KvTOqX4h/gRrBMV+ImmQbPgbEF/D4zRiy5cYVRLbexYdMYnSfOU+iTO8w6Hj6RmoweeNeU= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1675927404067730.596734071309; Wed, 8 Feb 2023 23:23:24 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.492187.761616 (Exim 4.92) (envelope-from ) id 1pQ1Ga-0004u7-LH; Thu, 09 Feb 2023 07:22:48 +0000 Received: by outflank-mailman (output) from mailman id 492187.761616; Thu, 09 Feb 2023 07:22:48 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1pQ1Ga-0004u0-Ic; Thu, 09 Feb 2023 07:22:48 +0000 Received: by outflank-mailman (input) for mailman id 492187; Thu, 09 Feb 2023 07:22:47 +0000 Received: from se1-gles-sth1-in.inumbo.com ([159.253.27.254] helo=se1-gles-sth1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1pQ1GZ-0004Wx-0j for xen-devel@lists.xenproject.org; Thu, 09 Feb 2023 07:22:47 +0000 Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.220.29]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id 8cea6629-a84a-11ed-933c-83870f6b2ba8; Thu, 09 Feb 2023 08:22:46 +0100 (CET) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id 476D85C220; Thu, 9 Feb 2023 07:22:46 +0000 (UTC) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id E04511339E; Thu, 9 Feb 2023 07:22:45 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id W5psNUWf5GMEeQAAMHmgww (envelope-from ); Thu, 09 Feb 2023 07:22:45 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 8cea6629-a84a-11ed-933c-83870f6b2ba8 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1675927366; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=jPHTwfsDZTgmdyR5HshjupjvhAk0TKRehG5uFEDCaGY=; b=jTQFlvog2tMj+BfGgU8rR5EaQbbXOxSH0z7WFdcmWrC4Ksfox4FiMX3eJAmKvb7DyQNkv0 KJE7/cahAd1ty7jbY4etBmQrkyOMWfGfL/UYbaKnz5sQzTi9sDtat3hOCW61/Bx2sXpirP fTZDfv+AJdEdeCjgCTqTHZ+DUyWwi5A= From: Juergen Gross To: linux-kernel@vger.kernel.org, x86@kernel.org Cc: lists@nerdbynature.de, mikelley@microsoft.com, torvalds@linux-foundation.org, Juergen Gross , Boris Ostrovsky , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , "H. Peter Anvin" , xen-devel@lists.xenproject.org Subject: [PATCH v2 4/8] x86/xen: set MTRR state when running as Xen PV initial domain Date: Thu, 9 Feb 2023 08:22:16 +0100 Message-Id: <20230209072220.6836-5-jgross@suse.com> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20230209072220.6836-1-jgross@suse.com> References: <20230209072220.6836-1-jgross@suse.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @suse.com) X-ZM-MESSAGEID: 1675927405670100001 Content-Type: text/plain; charset="utf-8" When running as Xen PV initial domain (aka dom0), MTRRs are disabled by the hypervisor, but the system should nevertheless use correct cache memory types. This has always kind of worked, as disabled MTRRs resulted in disabled PAT, too, so that the kernel avoided code paths resulting in inconsistencies. This bypassed all of the sanity checks the kernel is doing with enabled MTRRs in order to avoid memory mappings with conflicting memory types. This has been changed recently, leading to PAT being accepted to be enabled, while MTRRs stayed disabled. The result is that mtrr_type_lookup() no longer is accepting all memory type requests, but started to return WB even if UC- was requested. This led to driver failures during initialization of some devices. In reality MTRRs are still in effect, but they are under complete control of the Xen hypervisor. It is possible, however, to retrieve the MTRR settings from the hypervisor. In order to fix those problems, overwrite the MTRR state via mtrr_overwrite_state() with the MTRR data from the hypervisor, if the system is running as a Xen dom0. Fixes: 72cbc8f04fe2 ("x86/PAT: Have pat_enabled() properly reflect state wh= en running on Xen") Signed-off-by: Juergen Gross --- V2: - new patch --- arch/x86/xen/enlighten_pv.c | 49 +++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/arch/x86/xen/enlighten_pv.c b/arch/x86/xen/enlighten_pv.c index 5b1379662877..9cf520c0c810 100644 --- a/arch/x86/xen/enlighten_pv.c +++ b/arch/x86/xen/enlighten_pv.c @@ -68,6 +68,7 @@ #include #include #include +#include #include #include #include @@ -1200,6 +1201,52 @@ static void __init xen_boot_params_init_edd(void) #endif } =20 +/* Get MTRR settings from Xen and put them into mtrr_state. */ +static void __init xen_set_mtrr_data(void) +{ +#ifdef CONFIG_MTRR + struct xen_platform_op op =3D { + .cmd =3D XENPF_read_memtype, + .interface_version =3D XENPF_INTERFACE_VERSION, + }; + unsigned int reg; + unsigned long mask; + uint32_t eax, width; + static struct mtrr_var_range var[MTRR_MAX_VAR_RANGES] __initdata; + + /* Get physical address width (only 64-bit cpus supported). */ + width =3D 36; + eax =3D cpuid_eax(0x80000000); + if ((eax >> 16) =3D=3D 0x8000 && eax >=3D 0x80000008) { + eax =3D cpuid_eax(0x80000008); + width =3D eax & 0xff; + } + + for (reg =3D 0; reg < MTRR_MAX_VAR_RANGES; reg++) { + op.u.read_memtype.reg =3D reg; + if (HYPERVISOR_platform_op(&op)) + break; + + /* + * Only called in dom0, which has all RAM PFNs mapped at + * RAM MFNs, and all PCI space etc. is identity mapped. + * This means we can treat MFN =3D=3D PFN regarding MTTR settings. + */ + var[reg].base_lo =3D op.u.read_memtype.type; + var[reg].base_lo |=3D op.u.read_memtype.mfn << PAGE_SHIFT; + var[reg].base_hi =3D op.u.read_memtype.mfn >> (32 - PAGE_SHIFT); + mask =3D ~((op.u.read_memtype.nr_mfns << PAGE_SHIFT) - 1); + mask &=3D (1UL << width) - 1; + if (mask) + mask |=3D 1 << 11; + var[reg].mask_lo =3D mask; + var[reg].mask_hi =3D mask >> 32; + } + + mtrr_overwrite_state(var, reg, NULL, MTRR_TYPE_UNCACHABLE); +#endif +} + /* * Set up the GDT and segment registers for -fstack-protector. Until * we do this, we have to be careful not to call any stack-protected @@ -1403,6 +1450,8 @@ asmlinkage __visible void __init xen_start_kernel(str= uct start_info *si) =20 xen_boot_params_init_edd(); =20 + xen_set_mtrr_data(); + #ifdef CONFIG_ACPI /* * Disable selecting "Firmware First mode" for correctable --=20 2.35.3 From nobody Fri Sep 12 14:21:55 2025 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 6DCDEC05027 for ; Thu, 9 Feb 2023 07:23:22 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229943AbjBIHXU (ORCPT ); Thu, 9 Feb 2023 02:23:20 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34822 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229612AbjBIHXK (ORCPT ); Thu, 9 Feb 2023 02:23:10 -0500 Received: from smtp-out1.suse.de (smtp-out1.suse.de [IPv6:2001:67c:2178:6::1c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A71231A8 for ; Wed, 8 Feb 2023 23:22:53 -0800 (PST) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id 10FCE34DB5; Thu, 9 Feb 2023 07:22:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1675927372; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=hQfuC28mlaLh9I0Auwhmi7p4E8oJ358WdbS83VTVrH4=; b=QVxkqyKwUppd9azMYiG9BFJQTC5IR16oq3ZSRSa5ll2tDuUvUQEZSA5ozX47EUsSPLIG6y ToGEeYD9z/oFBPo/Q6vY6TY6/65BfrKEW7DaMD4lYdrnjwUma9A17wnJuXZpVcSN1du3MF lAyMbOHODDDWWHlN19KY4AmQDLxB76g= Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id A9F661339E; Thu, 9 Feb 2023 07:22:51 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id fa0+KEuf5GMJeQAAMHmgww (envelope-from ); Thu, 09 Feb 2023 07:22:51 +0000 From: Juergen Gross To: linux-kernel@vger.kernel.org, x86@kernel.org Cc: lists@nerdbynature.de, mikelley@microsoft.com, torvalds@linux-foundation.org, Juergen Gross , Dave Hansen , Andy Lutomirski , Peter Zijlstra , Thomas Gleixner , Ingo Molnar , Borislav Petkov , "H. Peter Anvin" Subject: [PATCH v2 5/8] x86/mtrr: revert commit 90b926e68f50 Date: Thu, 9 Feb 2023 08:22:17 +0100 Message-Id: <20230209072220.6836-6-jgross@suse.com> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20230209072220.6836-1-jgross@suse.com> References: <20230209072220.6836-1-jgross@suse.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Commit 90b926e68f50 ("x86/pat: Fix pat_x_mtrr_type() for MTRR disabled case") has introduced a regression with Xen. Revert the patch. Signed-off-by: Juergen Gross --- arch/x86/mm/pat/memtype.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/arch/x86/mm/pat/memtype.c b/arch/x86/mm/pat/memtype.c index fb4b1b5e0dea..46de9cf5c91d 100644 --- a/arch/x86/mm/pat/memtype.c +++ b/arch/x86/mm/pat/memtype.c @@ -387,8 +387,7 @@ static unsigned long pat_x_mtrr_type(u64 start, u64 end, u8 mtrr_type, uniform; =20 mtrr_type =3D mtrr_type_lookup(start, end, &uniform); - if (mtrr_type !=3D MTRR_TYPE_WRBACK && - mtrr_type !=3D MTRR_TYPE_INVALID) + if (mtrr_type !=3D MTRR_TYPE_WRBACK) return _PAGE_CACHE_MODE_UC_MINUS; =20 return _PAGE_CACHE_MODE_WB; --=20 2.35.3 From nobody Fri Sep 12 14:21:55 2025 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 9A9ABC61DA4 for ; Thu, 9 Feb 2023 07:23:28 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229975AbjBIHX1 (ORCPT ); Thu, 9 Feb 2023 02:23:27 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34608 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229868AbjBIHXM (ORCPT ); Thu, 9 Feb 2023 02:23:12 -0500 Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.220.29]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1D35342DF6 for ; Wed, 8 Feb 2023 23:22:59 -0800 (PST) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id C4ECF5C222; Thu, 9 Feb 2023 07:22:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1675927377; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=z/TdnBDA6vtZIeltc4dNIeTCjXcEoUw2SXAii0hNOAs=; b=rU6L7/gAsZQbnSs8QIonGyYpZQf6gX6EDnm1EQJPpZsDdwU2Vk/eF1pD4eIU7yjHIQbBp3 ywLutBDNczAAroxue8LtKXBGjwEyucyXXa/ajr7nNHKEYpoTn+EiQKdT0f2AmHHi7S3eHY TU2dpIs4JBoXxMC7lMAgUMHReF8NnFg= Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id 7C97D1339E; Thu, 9 Feb 2023 07:22:57 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id 7S87HVGf5GMTeQAAMHmgww (envelope-from ); Thu, 09 Feb 2023 07:22:57 +0000 From: Juergen Gross To: linux-kernel@vger.kernel.org, x86@kernel.org Cc: lists@nerdbynature.de, mikelley@microsoft.com, torvalds@linux-foundation.org, Juergen Gross , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , "H. Peter Anvin" Subject: [PATCH v2 6/8] x86/mtrr: don't let mtrr_type_lookup() return MTRR_TYPE_INVALID Date: Thu, 9 Feb 2023 08:22:18 +0100 Message-Id: <20230209072220.6836-7-jgross@suse.com> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20230209072220.6836-1-jgross@suse.com> References: <20230209072220.6836-1-jgross@suse.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" mtrr_type_lookup() should always return a valid memory type. In case there is no information available, it should return the default UC. At the same time the mtrr_type_lookup() stub for the !CONFIG_MTRR case should set uniform to 1, as if the memory range would be covered by no MTRR at all. In the CONFIG_MTRR case make sure uniform is always set to either 0 or 1. Without mtrr_state_set set it to 0, as it isn't known yet whether the covered range is uniform or not (in fact there is currently no caller interested in the uniform value before mtrr_state_set is being set). With MTRRs disabled uniform can be set to 1. Suggested-by: Linus Torvalds Signed-off-by: Juergen Gross --- V2: - always set uniform - set uniform to 1 in case of disabled MTRRs (Linus Torvalds) --- arch/x86/include/asm/mtrr.h | 7 +++++-- arch/x86/kernel/cpu/mtrr/generic.c | 12 ++++++++---- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/arch/x86/include/asm/mtrr.h b/arch/x86/include/asm/mtrr.h index 0b8f51d683dc..f362c33e3d74 100644 --- a/arch/x86/include/asm/mtrr.h +++ b/arch/x86/include/asm/mtrr.h @@ -53,9 +53,12 @@ void mtrr_generic_set_state(void); static inline u8 mtrr_type_lookup(u64 addr, u64 end, u8 *uniform) { /* - * Return no-MTRRs: + * Return the default MTRR type, without any known other types in + * that range. */ - return MTRR_TYPE_INVALID; + *uniform =3D 1; + + return MTRR_TYPE_UNCACHABLE; } #define mtrr_save_fixed_ranges(arg) do {} while (0) #define mtrr_save_state() do {} while (0) diff --git a/arch/x86/kernel/cpu/mtrr/generic.c b/arch/x86/kernel/cpu/mtrr/= generic.c index 788bc16888a5..afb59ff2cc00 100644 --- a/arch/x86/kernel/cpu/mtrr/generic.c +++ b/arch/x86/kernel/cpu/mtrr/generic.c @@ -299,11 +299,15 @@ u8 mtrr_type_lookup(u64 start, u64 end, u8 *uniform) /* Make end inclusive instead of exclusive */ end--; =20 - if (!mtrr_state_set) - return MTRR_TYPE_INVALID; + if (!mtrr_state_set) { + *uniform =3D 0; /* Uniformity is unknown. */ + return MTRR_TYPE_UNCACHABLE; + } =20 - if (!(mtrr_state.enabled & MTRR_STATE_MTRR_ENABLED)) - return MTRR_TYPE_INVALID; + if (!(mtrr_state.enabled & MTRR_STATE_MTRR_ENABLED)) { + *uniform =3D 1; + return MTRR_TYPE_UNCACHABLE; + } =20 /* * Look up the fixed ranges first, which take priority over --=20 2.35.3 From nobody Fri Sep 12 14:21:55 2025 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 489ABC05027 for ; Thu, 9 Feb 2023 07:23:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229819AbjBIHXh (ORCPT ); Thu, 9 Feb 2023 02:23:37 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34608 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229958AbjBIHXY (ORCPT ); Thu, 9 Feb 2023 02:23:24 -0500 Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.220.29]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1E82B4B1BB for ; Wed, 8 Feb 2023 23:23:04 -0800 (PST) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id 920B15C21E; Thu, 9 Feb 2023 07:23:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1675927383; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=JQKD/lPKkbb0qc+8vhUXutdmkqWMq0MqdsxYYVlo8yw=; b=JGSuT3c00URgBx3jlPXWSEIffym0BdFx2yUguZRDV33VGPqO6l76IYPCFpExHSArPjdIuc Ex6uV/C5p+S2ISH8FwrCTyFucgikotYb0ni+3Yp8PIpS9POS6HiIcPhI0e2jk6UO7fuz7w HSYZEol2ZnM/VGfWSUEvxTNbDWdcgiY= Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id 3E89B1339E; Thu, 9 Feb 2023 07:23:03 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id we8VDlef5GMbeQAAMHmgww (envelope-from ); Thu, 09 Feb 2023 07:23:03 +0000 From: Juergen Gross To: linux-kernel@vger.kernel.org, x86@kernel.org Cc: lists@nerdbynature.de, mikelley@microsoft.com, torvalds@linux-foundation.org, Juergen Gross , Dave Hansen , Andy Lutomirski , Peter Zijlstra , Thomas Gleixner , Ingo Molnar , Borislav Petkov , "H. Peter Anvin" Subject: [PATCH v2 7/8] x86/mm: only check uniform after calling mtrr_type_lookup() Date: Thu, 9 Feb 2023 08:22:19 +0100 Message-Id: <20230209072220.6836-8-jgross@suse.com> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20230209072220.6836-1-jgross@suse.com> References: <20230209072220.6836-1-jgross@suse.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Today pud_set_huge() and pmd_set_huge() test for the MTRR type to be WB or INVALID after calling mtrr_type_lookup(). Those tests can be dropped, as the only reason to not use a large mapping would be uniform being 0. Any MTRR type can be accepted as long as it applies to the whole memory range covered by the mapping, as the alternative would only be to map the same region with smaller pages instead using the same PAT type as for the large mapping. Suggested-by: Linus Torvalds Signed-off-by: Juergen Gross --- arch/x86/mm/pgtable.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/arch/x86/mm/pgtable.c b/arch/x86/mm/pgtable.c index e4f499eb0f29..7b9c5443d176 100644 --- a/arch/x86/mm/pgtable.c +++ b/arch/x86/mm/pgtable.c @@ -721,8 +721,7 @@ int pud_set_huge(pud_t *pud, phys_addr_t addr, pgprot_t= prot) u8 mtrr, uniform; =20 mtrr =3D mtrr_type_lookup(addr, addr + PUD_SIZE, &uniform); - if ((mtrr !=3D MTRR_TYPE_INVALID) && (!uniform) && - (mtrr !=3D MTRR_TYPE_WRBACK)) + if (!uniform) return 0; =20 /* Bail out if we are we on a populated non-leaf entry: */ @@ -748,8 +747,7 @@ int pmd_set_huge(pmd_t *pmd, phys_addr_t addr, pgprot_t= prot) u8 mtrr, uniform; =20 mtrr =3D mtrr_type_lookup(addr, addr + PMD_SIZE, &uniform); - if ((mtrr !=3D MTRR_TYPE_INVALID) && (!uniform) && - (mtrr !=3D MTRR_TYPE_WRBACK)) { + if (!uniform) { pr_warn_once("%s: Cannot satisfy [mem %#010llx-%#010llx] with a huge-pag= e mapping due to MTRR override.\n", __func__, addr, addr + PMD_SIZE); return 0; --=20 2.35.3 From nobody Fri Sep 12 14:21:55 2025 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 AB6ECC05027 for ; Thu, 9 Feb 2023 07:23:51 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230029AbjBIHXu (ORCPT ); Thu, 9 Feb 2023 02:23:50 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35564 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229891AbjBIHXe (ORCPT ); Thu, 9 Feb 2023 02:23:34 -0500 Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.220.29]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1AB3049010 for ; Wed, 8 Feb 2023 23:23:13 -0800 (PST) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id 49A965C21F; Thu, 9 Feb 2023 07:23:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1675927389; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=9mEOMWv35R/f7zhiS0ZyJfuk4uTp5/VuATvRXuKHpDE=; b=YFgxsrgrMz4OTZGEV5t5OQQp2iOYlVpEFNMiezmnGgZnwDiDSDSyf8RHaZOMax41g5Zcok yBS7FM0NkUE0dp0cGPu6gi3UCIZXgvJ5pzmt69/xbPQrdyb+ojPlogLUzXuxJ7Sxdb81tQ 92eLAn/u4svn9et+ALd6eMI3ZUo2Zbc= Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id F20C91339E; Thu, 9 Feb 2023 07:23:08 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id sGnGOVyf5GMjeQAAMHmgww (envelope-from ); Thu, 09 Feb 2023 07:23:08 +0000 From: Juergen Gross To: linux-kernel@vger.kernel.org, x86@kernel.org Cc: lists@nerdbynature.de, mikelley@microsoft.com, torvalds@linux-foundation.org, Juergen Gross , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , "H. Peter Anvin" Subject: [PATCH v2 8/8] x86/mtrr: drop sanity check in mtrr_type_lookup_fixed() Date: Thu, 9 Feb 2023 08:22:20 +0100 Message-Id: <20230209072220.6836-9-jgross@suse.com> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20230209072220.6836-1-jgross@suse.com> References: <20230209072220.6836-1-jgross@suse.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" mtrr_type_lookup_fixed() contains a sanity check for the case it is being called with a start address above 1 MB. As it is static and it is called only iff the start address is below 1MB, this sanity check can be dropped. This will remove the last case where mtrr_type_lookup() can return MTRR_TYPE_INVALID, so adjust the comment in include/uapi/asm/mtrr.h. Note that removing the MTRR_TYPE_INVALID #define from that header could break user code, so it has to stay. Signed-off-by: Juergen Gross --- arch/x86/include/uapi/asm/mtrr.h | 6 +++--- arch/x86/kernel/cpu/mtrr/generic.c | 6 +----- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/arch/x86/include/uapi/asm/mtrr.h b/arch/x86/include/uapi/asm/m= trr.h index 376563f2bac1..4aa05c2ffa78 100644 --- a/arch/x86/include/uapi/asm/mtrr.h +++ b/arch/x86/include/uapi/asm/mtrr.h @@ -115,9 +115,9 @@ struct mtrr_state_type { #define MTRR_NUM_TYPES 7 =20 /* - * Invalid MTRR memory type. mtrr_type_lookup() returns this value when - * MTRRs are disabled. Note, this value is allocated from the reserved - * values (0x7-0xff) of the MTRR memory types. + * Invalid MTRR memory type. No longer used outside of MTRR code. + * Note, this value is allocated from the reserved values (0x7-0xff) of + * the MTRR memory types. */ #define MTRR_TYPE_INVALID 0xff =20 diff --git a/arch/x86/kernel/cpu/mtrr/generic.c b/arch/x86/kernel/cpu/mtrr/= generic.c index afb59ff2cc00..575b050a55bf 100644 --- a/arch/x86/kernel/cpu/mtrr/generic.c +++ b/arch/x86/kernel/cpu/mtrr/generic.c @@ -114,16 +114,12 @@ static int check_type_overlap(u8 *prev, u8 *curr) * 0xC0000 - 0xFFFFF : This range is divided into sixty-four 4KB sub-rang= es * * Return Values: - * MTRR_TYPE_(type) - Matched memory type - * MTRR_TYPE_INVALID - Unmatched + * MTRR_TYPE_(type) - Memory type */ static u8 mtrr_type_lookup_fixed(u64 start, u64 end) { int idx; =20 - if (start >=3D 0x100000) - return MTRR_TYPE_INVALID; - /* 0x0 - 0x7FFFF */ if (start < 0x80000) { idx =3D 0; --=20 2.35.3