From patchwork Tue Jan 23 14:53:22 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 13527541 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 65F6DC47DDB for ; Tue, 23 Jan 2024 14:56:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:From:Subject:Message-ID: References:Mime-Version:In-Reply-To:Date:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=25o3HSQRc+zWEfc6JQrDH7MWyTnbL9zkLEKIDswq4RI=; b=2qwCVqedfKoP+45iQnx54n5vMM WNgMp4qyxfygk8sXq8GJJUH/kJIdClNudt+H7LypXDArE+0gEuVLoGF8BG1h6v5UDg0UcraNYmWsM +oEjbKxiVAfFKuUc59Zru96P6LIqzmos6SR6ATGr0sahxymR/8YA9nL6TJZKLZJ/9QfgJgJbKlIkT Sz01unrugslvKU1MHyF5ijN/t80neviZjnnOu8ECZsE4B5H1ZOVeVr+vfpfH/rY7KMXPXW8pvJ3CT zBF25VODSH7jkYHhMP2rkzosh/6flESEc/Is6jMxpJosyWOWnvZCX4bhob5vP3ydLklp1s1RZ6qe5 8vmWyr4g==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1rSICO-00GubZ-2g; Tue, 23 Jan 2024 14:56:24 +0000 Received: from mail-wm1-x34a.google.com ([2a00:1450:4864:20::34a]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1rSIBJ-00Gu2s-1I for linux-arm-kernel@lists.infradead.org; Tue, 23 Jan 2024 14:55:31 +0000 Received: by mail-wm1-x34a.google.com with SMTP id 5b1f17b1804b1-40e478693bcso24533495e9.1 for ; Tue, 23 Jan 2024 06:55:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1706021715; x=1706626515; darn=lists.infradead.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=Ukkq5zTjwFGShePAgonGWwjW49P0qsEhwo2lDCGDIME=; b=kv//iBtzrPoXPmTmvsDGmA2IoDGsNGRQ6sTFeOlBP9rHtMixFddOaKtsVsXhONVWTt KrtL9Sy7N2wP171sq5u36dZaOVw7IeVvgfoPJB5j/VtupZnZoHmkChvy1Jgan5WJKWkf 1Fz6jETe3zGh8AEEuZP3o529awiZL5aEDfGge+wpra+h/gmL0+1vBmKYjjZjprP2uRJY onKGkbNSTjNrDmn39F028WV/QDGohgjYVwGiPqG695pflyIJM/bXJ3usmbe2vPl0P+bG OilxeztM5d5vAxuOPeIc+09U5vUDWJYmO3MPJLjqXYX2KUuKszlMb/6ioEiajtLC52vu Bc6w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706021715; x=1706626515; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=Ukkq5zTjwFGShePAgonGWwjW49P0qsEhwo2lDCGDIME=; b=jfF+DHp3YSX/UEB6J3dkPrVwPU6WVMd+8+zZbN1AgTIukh7Bge4r7ELRGsKvueK5B2 5v2ldCIetj8Ij9kmqP9C5LzfIFPcnXH0kdB+NLFDZ1H/MG2Nb9MMNctBkSUZqht545dG zcbZEOBWGXF0kYX3zyaZT6FUXSooVYuUd0ZLXiq2x+uU/r/Xn71PGuOhZOi9GCbR5+wO jb9HmZniOY1QxsLXwvbkCul/sV0+kJXY+9bXOHCqKEhJNjqmL+LzlfaP9X6DgbD2+Fum QR97+ouVTCVO3uyUYq6fwLV6BbT8xarJ6VU+8Ik+21CxPF7kD0eGJZHz7IOyQzxHlAP6 2H4g== X-Gm-Message-State: AOJu0YytqRHBfxxCBMlcpUDjDVI3Cdz+9cJ8Px7dByI12WjhrAderaPP y9t6NIEdB/CLYGK514HxHSXfSfUL17uIkxWdvrIYN+LYBKlugnT+yEnk0vASKsDefYhuYJJuG2G 2PqrbTYsYJ9PRzHKW2Go82vJihtnrJuA4Kj3wCpWnYh5cOw6jad3uUnISsD5IvEGNeI5o/wO+uN S9HzQjO4mw0M61vFen2t2AZDU8h6L+WAb6d5qZo7zV X-Google-Smtp-Source: AGHT+IHGwM6WLchL0EH1pAuT/pcSWcSVupdfU1KXVpzCa1obUlSANj5imp+X5QDnkU6QkK41p9bucSdt X-Received: from palermo.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:118a]) (user=ardb job=sendgmr) by 2002:a05:600c:b93:b0:40e:9e30:5722 with SMTP id fl19-20020a05600c0b9300b0040e9e305722mr96547wmb.4.1706021715511; Tue, 23 Jan 2024 06:55:15 -0800 (PST) Date: Tue, 23 Jan 2024 15:53:22 +0100 In-Reply-To: <20240123145258.1462979-52-ardb+git@google.com> Mime-Version: 1.0 References: <20240123145258.1462979-52-ardb+git@google.com> X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Developer-Signature: v=1; a=openpgp-sha256; l=5075; i=ardb@kernel.org; h=from:subject; bh=AZz7FTFLHXTxu+8k1f0CpRqTjjs7igvnlb/1XVxMwRU=; b=owGbwMvMwCFmkMcZplerG8N4Wi2JIXX9pcdJAt2dkwqKD//Sijyl8O5Nsc31w7kXT/D5vdusf ONZ0ZfDHaUsDGIcDLJiiiwCs/++23l6olSt8yxZmDmsTCBDGLg4BWAiDuoM/wu5+/3uX+mQijls fuz0yhP7mR5EXPzMmuiwaP1Ps2Us7/gZ/hfJPFrglGcpctvFb4bbs61TP/x6u9iG4UqJi1Tc8uW F69gA X-Mailer: git-send-email 2.43.0.429.g432eaa2c6b-goog Message-ID: <20240123145258.1462979-75-ardb+git@google.com> Subject: [PATCH v7 23/50] arm64: mm: Make kaslr_requires_kpti() a static inline From: Ard Biesheuvel To: linux-arm-kernel@lists.infradead.org Cc: Ard Biesheuvel , Catalin Marinas , Will Deacon , Marc Zyngier , Mark Rutland , Ryan Roberts , Anshuman Khandual , Kees Cook X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240123_065517_486838_818BA30C X-CRM114-Status: GOOD ( 23.79 ) 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 From: Ard Biesheuvel In preparation for moving the first assignment of arm64_use_ng_mappings to an earlier stage in the boot, ensure that kaslr_requires_kpti() is accessible without relying on the core kernel's view on whether or not KASLR is enabled. So make it a static inline, and move the kaslr_enabled() check out of it and into the callers, one of which will disappear in a subsequent patch. Once/when support for the obsolete ThunderX 1 platform is dropped, this check reduces to a E0PD feature check on the local CPU. Signed-off-by: Ard Biesheuvel --- arch/arm64/include/asm/mmu.h | 38 +++++++++++++++++- arch/arm64/kernel/cpufeature.c | 42 +------------------- arch/arm64/kernel/setup.c | 2 +- 3 files changed, 39 insertions(+), 43 deletions(-) diff --git a/arch/arm64/include/asm/mmu.h b/arch/arm64/include/asm/mmu.h index 2fcf51231d6e..d0b8b4b413b6 100644 --- a/arch/arm64/include/asm/mmu.h +++ b/arch/arm64/include/asm/mmu.h @@ -71,7 +71,43 @@ extern void create_pgd_mapping(struct mm_struct *mm, phys_addr_t phys, pgprot_t prot, bool page_mappings_only); extern void *fixmap_remap_fdt(phys_addr_t dt_phys, int *size, pgprot_t prot); extern void mark_linear_text_alias_ro(void); -extern bool kaslr_requires_kpti(void); + +/* + * This check is triggered during the early boot before the cpufeature + * is initialised. Checking the status on the local CPU allows the boot + * CPU to detect the need for non-global mappings and thus avoiding a + * pagetable re-write after all the CPUs are booted. This check will be + * anyway run on individual CPUs, allowing us to get the consistent + * state once the SMP CPUs are up and thus make the switch to non-global + * mappings if required. + */ +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; + } + + /* + * Systems affected by Cavium erratum 24756 are incompatible + * with KPTI. + */ + if (IS_ENABLED(CONFIG_CAVIUM_ERRATUM_27456)) { + extern const struct midr_range cavium_erratum_27456_cpus[]; + + if (is_midr_in_range_list(read_cpuid_id(), + cavium_erratum_27456_cpus)) + return false; + } + + return true; +} #define INIT_MM_CONTEXT(name) \ .pgd = init_pg_dir, diff --git a/arch/arm64/kernel/cpufeature.c b/arch/arm64/kernel/cpufeature.c index d0ffb872a31a..7064cf13f226 100644 --- a/arch/arm64/kernel/cpufeature.c +++ b/arch/arm64/kernel/cpufeature.c @@ -1620,46 +1620,6 @@ has_useable_cnp(const struct arm64_cpu_capabilities *entry, int scope) return has_cpuid_feature(entry, scope); } -/* - * This check is triggered during the early boot before the cpufeature - * is initialised. Checking the status on the local CPU allows the boot - * CPU to detect the need for non-global mappings and thus avoiding a - * pagetable re-write after all the CPUs are booted. This check will be - * anyway run on individual CPUs, allowing us to get the consistent - * state once the SMP CPUs are up and thus make the switch to non-global - * mappings if required. - */ -bool kaslr_requires_kpti(void) -{ - if (!IS_ENABLED(CONFIG_RANDOMIZE_BASE)) - return false; - - /* - * 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; - } - - /* - * Systems affected by Cavium erratum 24756 are incompatible - * with KPTI. - */ - if (IS_ENABLED(CONFIG_CAVIUM_ERRATUM_27456)) { - extern const struct midr_range cavium_erratum_27456_cpus[]; - - if (is_midr_in_range_list(read_cpuid_id(), - cavium_erratum_27456_cpus)) - return false; - } - - return kaslr_enabled(); -} - static bool __meltdown_safe = true; static int __kpti_forced; /* 0: not forced, >0: forced on, <0: forced off */ @@ -1712,7 +1672,7 @@ static bool unmap_kernel_at_el0(const struct arm64_cpu_capabilities *entry, } /* Useful for KASLR robustness */ - if (kaslr_requires_kpti()) { + if (kaslr_enabled() && kaslr_requires_kpti()) { if (!__kpti_forced) { str = "KASLR"; __kpti_forced = 1; diff --git a/arch/arm64/kernel/setup.c b/arch/arm64/kernel/setup.c index 97d2143669cf..0ef45d1927b3 100644 --- a/arch/arm64/kernel/setup.c +++ b/arch/arm64/kernel/setup.c @@ -288,7 +288,7 @@ void __init __no_sanitize_address setup_arch(char **cmdline_p) * mappings from the start, avoiding the cost of rewriting * everything later. */ - arm64_use_ng_mappings = kaslr_requires_kpti(); + arm64_use_ng_mappings = kaslr_enabled() && kaslr_requires_kpti(); early_fixmap_init(); early_ioremap_init();