From patchwork Wed Feb 14 12:29:02 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 13556417 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 DC74FC48BED for ; Wed, 14 Feb 2024 12:32:24 +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=c+qnMNEKAeXH7F5OfjB8Zw13kBoUwgKGDAoDjlicOVQ=; b=K4dcyvyxc80gRWTdKUcewOLFa8 PGqDhWwtdkg+hlkj5uWutfcWv+q3/opg8NZ43I66u4lDzWIz2gzgQSwNYhs4U/qmF6USFxYwQXoAk Z7fa3Fq6m0RENo2HQZ6Cc6ktxYl6G8RfQioiYgp18T4ms1PwfGwasJdJ1+IWx8Ur23MPjENRvCZrr +QcAXyxnSZHjzSnUG+oW0kpNZ9aHZ3fHt089Srrs/5RMZBUAVgO01tFiF/GqVOgU8i2r9Xnjc9mD5 xz/TQFUJovkoAVtk5QjqExcAY4rlwIPza11gAX62j4rFrbESvK80eGqtTAwUltVdrIPoy6+jRS4Ly fzpDFLbw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1raEQp-0000000CpOY-03jI; Wed, 14 Feb 2024 12:32:07 +0000 Received: from mail-wm1-f73.google.com ([209.85.128.73]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1raEP5-0000000CoUw-0CGu for linux-arm-kernel@lists.infradead.org; Wed, 14 Feb 2024 12:30:40 +0000 Received: by mail-wm1-f73.google.com with SMTP id 5b1f17b1804b1-40fd2fe490bso3343015e9.0 for ; Wed, 14 Feb 2024 04:30:17 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1707913815; x=1708518615; 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=TEtQPPcWPY0ALv0OwL+bGysAAwWr707upVDE6b/XzmA=; b=ItItrDhb/FTwmMcMcauGdN2Hl4DHvdD7RkU4qp7gJ28PIslZqWBVfUbBUPf+H950FM 61AByS0aL4cqwCS7CKrsjwBrTaAFjWSaP+C5v710qjIRp/jEnmGR6HreYtkERSVQ9/ww nz/VCly9ytBkvAtD1QWtyV+ykDLlYoPiH22ld9Vgp+5DyVQNLhuQNpuVww+3AHhywRbn piMKmWZleb7Q8nWOghI/k83um4afUFqMQurAoETdIOI/lS9LBBlR40d+hp0kiy9TwsMm Ud6MD3BlcGBiHIcQu0Ar3GPybZEeJrEM6LUTrGqho24T8vCkVZwTEWkSU5ideoBj4uRh RmOA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707913815; x=1708518615; 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=TEtQPPcWPY0ALv0OwL+bGysAAwWr707upVDE6b/XzmA=; b=ssERzwdsmmc5+oNmFE+dBKVUFlO86E4n+AtUedXfYCtJ9/77tuytG152H74TRxGzS5 yt8EgQcj4OHyNfdB86m6NCSANtZAvNGdzlRNVbr92ATsGpNP0/bw+uB/hYvsu2D+Nz9x iCbQNufGPPgWTvT5lDksTx3xQdH3al6Lmjy21we9KugILh5RHhFVr2HSUhNhi0uDTfHi X4qTVsnrWdZq8L5WJhyEuVNpQqD0e6ZLRAFRJS65YFXZd1Ka6NbuLD+uElt6L+NWXqwT mjeOEPvlsTs87D2ycUZks7OJTieMkClLdTmwMtYTBkPeqqnDMthSGP860ejwrFO5jUYF Rhsw== X-Gm-Message-State: AOJu0YwiOJ50VUu8URp/58bvgRO6kNlcPB0lE0H0taAoQzbD1CE978Su +Af6YEtIbwjYJ6vQFf6Y5fzED1/bRd1kIlY9Pb3GVUq8H3ODWdLiYaBatG6g4p8Qku7h+YlR6Pv aUhe9nIvSP55lfPmGET5DCOU5gcLVIPWXuoK9GhpefOdGI7o5qVKU8k4aG8NUuDM3aeb05CPd3B qMYNT39FSy0hidKmYMESYaIh9cehDfiYpbaj/VURCA X-Google-Smtp-Source: AGHT+IGKgzBaCx1uc8lpYFEGvXBQGLgG3xISEHWN/WQLPXSiZD1fXfW02wVkPSmx/CQhCW+LWb0FYCnF X-Received: from palermo.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:118a]) (user=ardb job=sendgmr) by 2002:a05:600c:54e5:b0:410:e4fe:1a1f with SMTP id jb5-20020a05600c54e500b00410e4fe1a1fmr47704wmb.0.1707913814861; Wed, 14 Feb 2024 04:30:14 -0800 (PST) Date: Wed, 14 Feb 2024 13:29:02 +0100 In-Reply-To: <20240214122845.2033971-45-ardb+git@google.com> Mime-Version: 1.0 References: <20240214122845.2033971-45-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=tdrW6PySIyYhT0Z6b6jbS+n+X05qOd2DwGUk6HZP+Fg=; b=owGbwMvMwCFmkMcZplerG8N4Wi2JIfXMJv7jN1aybEhcPy3TMbzs+eHvUqufy5fPPOuv2v3F/ 2xmwZMDHaUsDGIcDLJiiiwCs/++23l6olSt8yxZmDmsTCBDGLg4BWAiKQKMDDPK6hcuuJ8W/+DQ pNsFb/R03m0Wd7y0zC+g56HBchbvaakM/5OuLp87TYJ1aVFi9/LozwZsTFMPTPS9/cZ504JWzts fNjIBAA== X-Mailer: git-send-email 2.43.0.687.g38aa6559b0-goog Message-ID: <20240214122845.2033971-61-ardb+git@google.com> Subject: [PATCH v8 16/43] 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-20240214_043019_402006_F7370757 X-CRM114-Status: GOOD ( 23.76 ) 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();