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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 0EF0DC369BD for ; Tue, 15 Apr 2025 15:29:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:MIME-Version: References:In-Reply-To:Subject:Cc:To:From:Message-ID:Date:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=+lAs687pHuPllkEbXGMAuSrfZYiYfg3uTDZbT9aeUww=; b=MdwjWOd3Y8mU7OpmpNyB8ddCOn SfZiZM5wjr6RjkVdykBqTJkt9lOSByOCA6R4NTATbVeU5/AICackeSodB+13v5Sh7cilv72Xhfr/Q 5su7NpgSsqkpKgC4BNyBzNsT/RVRzAr0lusVMcGXKbJxBPVsrvR8pnr2UR4hI9g5txhCjMpQD5MAl v8teOXyxu06IGOI8h1QXsglUCPEi7fy17PTXmD7xWUaebXvanLuZNFpNkoDUQxU6oJEoPMCtHLdOq Jhn9rX+nvXJhsd0gvd0K0v6otXdru0ZJsex3PttoYAi7tW3kaKzNAbJ/aa1KbwSQk6zNcvHYAekQA oV9cT5Tw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98.2 #2 (Red Hat Linux)) id 1u4iDV-00000006F5M-46mp; Tue, 15 Apr 2025 15:28:53 +0000 Received: from dfw.source.kernel.org ([139.178.84.217]) by bombadil.infradead.org with esmtps (Exim 4.98.2 #2 (Red Hat Linux)) id 1u4iBe-00000006Ets-3ZaI for linux-arm-kernel@lists.infradead.org; Tue, 15 Apr 2025 15:27:00 +0000 Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id F26FC5C5893; Tue, 15 Apr 2025 15:24:39 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id A28BBC4CEEB; Tue, 15 Apr 2025 15:26:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1744730816; bh=0OCFJS3E/Th2WG+ERLo1oYrZ1u0uOV4dVimjJHRegMM=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=iMycN3UuOciq/1+mQG94zsf/bPiykUjQx1Ybatab2cx99n1pv38gVM8rIq2cFdG04 rW7rRmDrnJgtjZekWRQzKRWBbEqnDvoJlWLZ1wiy8R0LJq7fYqFNWM5vJ8zq77c3Sb AV3Cx2KKwBupv5yX474zvPPcWH6jjC75WfhKY3Nw/LshWWgtNZm5k3Y2AHwphWdI2N t8Kw1n4yJFy84D9V4W8i8ydKKMci2d8Plq7wp+wTgBV2fw46/pk/KRRJXwCDQD6t02 KbEYDiPMsrsz3Q3ec1nlsJ2bBK1v/zl+UPQWvcfrBEWt956tjM9PIEBcIMgl73llAZ 86D2g7f6ZZgpg== Received: from sofa.misterjones.org ([185.219.108.64] helo=goblin-girl.misterjones.org) by disco-boy.misterjones.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.95) (envelope-from ) id 1u4iBa-005iz5-7h; Tue, 15 Apr 2025 16:26:54 +0100 Date: Tue, 15 Apr 2025 16:26:53 +0100 Message-ID: <865xj5la2q.wl-maz@kernel.org> From: Marc Zyngier To: Ada Couprie Diaz Cc: Shameer Kolothum , kvmarm@lists.linux.dev, oliver.upton@linux.dev, catalin.marinas@arm.com, will@kernel.org, mark.rutland@arm.com, cohuck@redhat.com, eric.auger@redhat.com, sebott@redhat.com, yuzenghui@huawei.com, wangzhou1@hisilicon.com, jiangkunkun@huawei.com, jonathan.cameron@huawei.com, anthony.jebson@huawei.com, linux-arm-kernel@lists.infradead.org, linuxarm@huawei.com Subject: Re: [BUG][PATCH v8 4/6] arm64: Make _midr_in_range_list() an exported function In-Reply-To: <3d97e45a-23cf-419b-9b6f-140b4d88de7b@arm.com> References: <20250221140229.12588-1-shameerali.kolothum.thodi@huawei.com> <20250221140229.12588-5-shameerali.kolothum.thodi@huawei.com> <3d97e45a-23cf-419b-9b6f-140b4d88de7b@arm.com> User-Agent: Wanderlust/2.15.9 (Almost Unreal) SEMI-EPG/1.14.7 (Harue) FLIM-LB/1.14.9 (=?UTF-8?B?R29qxY0=?=) APEL-LB/10.8 EasyPG/1.0.0 Emacs/29.4 (aarch64-unknown-linux-gnu) MULE/6.0 (HANACHIRUSATO) MIME-Version: 1.0 (generated by SEMI-EPG 1.14.7 - "Harue") Content-Type: text/plain; charset=US-ASCII X-SA-Exim-Connect-IP: 185.219.108.64 X-SA-Exim-Rcpt-To: ada.coupriediaz@arm.com, shameerali.kolothum.thodi@huawei.com, kvmarm@lists.linux.dev, oliver.upton@linux.dev, catalin.marinas@arm.com, will@kernel.org, mark.rutland@arm.com, cohuck@redhat.com, eric.auger@redhat.com, sebott@redhat.com, yuzenghui@huawei.com, wangzhou1@hisilicon.com, jiangkunkun@huawei.com, jonathan.cameron@huawei.com, anthony.jebson@huawei.com, linux-arm-kernel@lists.infradead.org, linuxarm@huawei.com X-SA-Exim-Mail-From: maz@kernel.org X-SA-Exim-Scanned: No (on disco-boy.misterjones.org); SAEximRunCond expanded to false X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20250415_082658_979956_C8CD8C47 X-CRM114-Status: GOOD ( 30.47 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org On Tue, 15 Apr 2025 11:57:50 +0100, Ada Couprie Diaz wrote: > > Hello, > > I discovered that this patch breaks boot for some CPUs when building > the default defconfig plus KASAN. This is still the case in v6.15-rc1 > and rc2. > > This patch marks `is_midr_in_range_list` as position independent but > it isn't, breaking early boot when instrumented with KASAN and > `CONFIG_RANDOMIZE_BASE` enabled. > > The breaking usage seems to be in `kaslr_requires_kpti()` called in > `early_map_kernel()`. > My testing on an AMD Seattle board does crash, but newer machines > implementing E0PD do not crash as they do not need to check MIDRs in > `kaslr_requires_kpti()`. > `is_mdr_in_range_list` did work in PI code previously because it was > `inline`, which this patch changes. OK, this is much more of a pain than I thought. I tried bringing the various helpers into the PI section, but it ended up being extremely ugly. More importantly, this is something that is pretty much at odds with the whole idea of the MIDR override -- it happens way earlier than we can populate the table. The thing is, the only reason we need to do this is that we need to support the Cavium SEFAC (Sorry Excuse For A Computer) that cannot run with KPTI. I can restore harmony with the following hack. But maybe we should just prune TX from the kernel and be done with this contraption. M. diff --git a/arch/arm64/include/asm/cputype.h b/arch/arm64/include/asm/cputype.h index d1cc0571798bf..bdfefca166baa 100644 --- a/arch/arm64/include/asm/cputype.h +++ b/arch/arm64/include/asm/cputype.h @@ -292,6 +292,20 @@ static inline bool midr_is_cpu_model_range(u32 midr, u32 model, u32 rv_min, return _model == model && rv >= rv_min && rv <= rv_max; } +static inline bool __is_midr_in_range(struct midr_range const *range) +{ + return midr_is_cpu_model_range(read_cpuid_id(), range->model, + range->rv_min, range->rv_max); +} + +static inline bool __is_midr_in_range_list(struct midr_range const *ranges) +{ + while (ranges->model) + if (__is_midr_in_range(ranges++)) + return true; + return false; +} + struct target_impl_cpu { u64 midr; u64 revidr; diff --git a/arch/arm64/include/asm/mmu.h b/arch/arm64/include/asm/mmu.h index 30a29e88994ba..e54a384826dc1 100644 --- a/arch/arm64/include/asm/mmu.h +++ b/arch/arm64/include/asm/mmu.h @@ -72,6 +72,17 @@ extern void create_pgd_mapping(struct mm_struct *mm, phys_addr_t phys, extern void *fixmap_remap_fdt(phys_addr_t dt_phys, int *size, pgprot_t prot); extern void mark_linear_text_alias_ro(void); +static inline bool cpu_has_e0pd(void) +{ + if (IS_ENABLED(CONFIG_ARM64_E0PD)) { + u64 mmfr2 = read_sysreg_s(SYS_ID_AA64MMFR2_EL1); + return (cpuid_feature_extract_unsigned_field(mmfr2, + ID_AA64MMFR2_EL1_E0PD_SHIFT)); + } + + return false; +} + /* * This check is triggered during the early boot before the cpufeature * is initialised. Checking the status on the local CPU allows the boot @@ -87,12 +98,8 @@ static inline bool kaslr_requires_kpti(void) * E0PD does a similar job to KPTI so can be used instead * where available. */ - if (IS_ENABLED(CONFIG_ARM64_E0PD)) { - u64 mmfr2 = read_sysreg_s(SYS_ID_AA64MMFR2_EL1); - if (cpuid_feature_extract_unsigned_field(mmfr2, - ID_AA64MMFR2_EL1_E0PD_SHIFT)) - return false; - } + if (cpu_has_e0pd()) + return false; /* * Systems affected by Cavium erratum 24756 are incompatible @@ -108,5 +115,21 @@ static inline bool kaslr_requires_kpti(void) return true; } +/* Same as the above, but limited to the local CPU, ignoring the MIDR list */ +static inline bool __kaslr_requires_kpti(void) +{ + if (cpu_has_e0pd()) + return false; + + if (IS_ENABLED(CONFIG_CAVIUM_ERRATUM_27456)) { + extern const struct midr_range cavium_erratum_27456_cpus[]; + + if (__is_midr_in_range_list(cavium_erratum_27456_cpus)) + return false; + } + + return true; +} + #endif /* !__ASSEMBLY__ */ #endif diff --git a/arch/arm64/kernel/cpu_errata.c b/arch/arm64/kernel/cpu_errata.c index b55f5f7057502..ba8e05d640c7e 100644 --- a/arch/arm64/kernel/cpu_errata.c +++ b/arch/arm64/kernel/cpu_errata.c @@ -32,8 +32,7 @@ static inline bool is_midr_in_range(struct midr_range const *range) int i; if (!target_impl_cpu_num) - return midr_is_cpu_model_range(read_cpuid_id(), range->model, - range->rv_min, range->rv_max); + return __is_midr_in_range(range); for (i = 0; i < target_impl_cpu_num; i++) { if (midr_is_cpu_model_range(target_impl_cpus[i].midr, diff --git a/arch/arm64/kernel/pi/map_kernel.c b/arch/arm64/kernel/pi/map_kernel.c index e57b043f324b5..2df76d44a1072 100644 --- a/arch/arm64/kernel/pi/map_kernel.c +++ b/arch/arm64/kernel/pi/map_kernel.c @@ -245,7 +245,7 @@ asmlinkage void __init early_map_kernel(u64 boot_status, void *fdt) if (IS_ENABLED(CONFIG_RANDOMIZE_BASE)) { u64 kaslr_seed = kaslr_early_init(fdt, chosen); - if (kaslr_seed && kaslr_requires_kpti()) + if (kaslr_seed && __kaslr_requires_kpti()) arm64_use_ng_mappings = true; kaslr_offset |= kaslr_seed & ~(MIN_KIMG_ALIGN - 1); -- Without deviation from the norm, progress is not possible.