From patchwork Fri Nov 24 10:19:12 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 13467450 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 1538DC61D97 for ; Fri, 24 Nov 2023 10:23:33 +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=+kUihMkkXRgd8YdcUQ/SGVZqG4IttXDOz1CT6Ey9Lqk=; b=g4Sf1VI+rEH9a7qcG0Mougro37 U/cFc7M4nKVkaRTkvosws7DsOi2pTvBshCrpIAqIlA37rzFbVXGfPEGUSz1IcuLlvqFDlsZkweoXP UICUx88ZgL4ly+WU2nsSl9oJu0LQEGJgbhN6BG9tUVjtl1I7dnBWK4UX9gERLI9c1so2ICwrFcQwD 6mUwJb4ipfyYVES9R2g1ibDRY8i8zxjhYRjIs2GRWYEV4yU9fLlrzY698jO2QBG9/6A2AUj+a90OZ am08QpM752sWkfYXRa5cdePje66VdhlyYLF4wx19F3nfJm0koqdvZyJBYl18B/2IEmnJVJ75FWSDs GpWKAypA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1r6TKy-006prt-0G; Fri, 24 Nov 2023 10:23:04 +0000 Received: from casper.infradead.org ([2001:8b0:10b:1236::1]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1r6TJU-006pDo-3C for linux-arm-kernel@bombadil.infradead.org; Fri, 24 Nov 2023 10:21:33 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Content-Type:Cc:To:From:Subject: Message-ID:References:Mime-Version:In-Reply-To:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description; bh=qVZ3aLKPTG+oZezVT42SNwhH7ArWgZIhCYiS27q7XPw=; b=s7WFY3UOOQ/1ZUJDpvj3hTnTC+ xxZJ/pcLZJ77hGoPqpuE5XspZFJnl7WnAwd27JAqBbz/yGDSqZTgHphL/qcW8GzD9NvLGkslFqp7k /ijoT/LWR7XTJZ5SYz80JFfqsEG3v07+w3iP+gXmJhn+Gs8PElDstoQe2u45CEafaJuvnGSnxTzTs C5V26T1VziSophwDR52A4GjF0lyVs3JrMvCxHi/G+CpP7UU1iR7xPD0CJQOKrYCbR0+YRDwhcSAig 8TVyXqlG/VJMyrK3o41afKTGBtHtC3KWwEDzt5i26PQZm6qDVMMVQGceGObgHRjRbZ6dyQJEawWXk EeIAEwUg==; Received: from mail-yw1-x1149.google.com ([2607:f8b0:4864:20::1149]) by casper.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1r6TJR-008SqN-FN for linux-arm-kernel@lists.infradead.org; Fri, 24 Nov 2023 10:21:31 +0000 Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-5cc3e334d16so31260477b3.3 for ; Fri, 24 Nov 2023 02:21:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1700821287; x=1701426087; 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=qVZ3aLKPTG+oZezVT42SNwhH7ArWgZIhCYiS27q7XPw=; b=rLy4rQCkYdzpebJ98DSV/pfkxmsrsvu3HxE48JxJncnUrhQ/xEsgrdsyEI3q1yZPsl hZed/TnM3wKrG8EadMliGupOQ6/NgTN2rFsZtnRe/wcJG4nJyFy7UNlyZJM4p/ioYOs6 tOGn3kzMEZ/rsyIdmQI4T2z2nXrKEHc/Nwkl+DYCQhj7l0fNjK2ZjasJ/NukR+ADwA4p bRrsfQQLpgsJK82xX3VrHQX6AGBNJowUYvyIB4t7F48Hgl6kw9QM6U+9OjFE4nsOn+Dt Q35SJHxCrLNqPCbslLIRwvFaWIg4G6LA2MgJQshKYBUQgCo8qUNCfID93ImX5SA0/N97 hirA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1700821287; x=1701426087; 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=qVZ3aLKPTG+oZezVT42SNwhH7ArWgZIhCYiS27q7XPw=; b=nK9uNX3un8k1A0bUgoCvapx+tlLD5W0RzMBiMqjlUE9zD0STzM99b/zu7+rzuxqn9D LcqWRcHXsKzaKLsQAF8L0fm223dM4I0r0/qUvQ7I2TQzbm3PoTFsOck7C6NGy/TeluH7 sFhZgsLPjVG8R7aP/WrF7jKQo9eID5K5gEFqA1AUCOUXgLnxXxhahKIBo6N7eOgvkBG7 +lZcGqeDyb6+qqIx4T4DsqsBzoRlMlppDxmP69zCpB6iQlKjMAGik/nspSSB1pcFuOwl udW8YosSQgD+KQBumAorXlYzIbudiF+V9SqlmQuZJx0Lk18H598FSlcIWfX9/RDr7WR9 Sw3A== X-Gm-Message-State: AOJu0Yw4copW4JElJgplc+KfQ9IpMIfJ2/KTizwvodqOkT44NNkk1NPC w9diESJZm/0HTmQh38v30eVfFsK1xDuObogAk1vLJAPWU0uLPfx0SHQCynEdPNmHHmyKTh0fesb nbT9DbDjIBCpid8gvj2lNszYOs+EthFxro9OVY9QmxQGOLsv1gfxTi+I922NDM+Rn+LIWSv1CYf g= X-Google-Smtp-Source: AGHT+IEr4y3t8vkSbcL3DHVY7tO8qbgIK2id3Vx6CyENpCFACgH2jhxdMXZ6P26swyPOVK18swa8Eb8k X-Received: from palermo.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:118a]) (user=ardb job=sendgmr) by 2002:a05:690c:3102:b0:5ca:8979:e650 with SMTP id fb2-20020a05690c310200b005ca8979e650mr61676ywb.10.1700821287038; Fri, 24 Nov 2023 02:21:27 -0800 (PST) Date: Fri, 24 Nov 2023 11:19:12 +0100 In-Reply-To: <20231124101840.944737-41-ardb@google.com> Mime-Version: 1.0 References: <20231124101840.944737-41-ardb@google.com> X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Developer-Signature: v=1; a=openpgp-sha256; l=5079; i=ardb@kernel.org; h=from:subject; bh=9eDZ1Je5zVxggBBmH3/anCK0BzsjoAP/PqvFC9cVPlU=; b=owGbwMvMwCFmkMcZplerG8N4Wi2JITWhYo2dS/q6b8f5GI0yA36pfuhpXRx1NDH3tUlv9zbbs PQTyyw7SlkYxDgYZMUUWQRm/3238/REqVrnWbIwc1iZQIYwcHEKwETuH2T4Z2xvbFLB7RVValbn tXLXG47jQSurnh/+0FTlaNkplHB4G8N/f+Zmd+3aRzufSfUHzReduKla9MJThfndD7lWZz6837O PCQA= X-Mailer: git-send-email 2.43.0.rc1.413.gea7ed67945-goog Message-ID: <20231124101840.944737-73-ardb@google.com> Subject: [PATCH v5 32/39] 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-20231124_102129_550133_74ECBB18 X-CRM114-Status: GOOD ( 22.38 ) 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 b95374387946..7222f5a14794 100644 --- a/arch/arm64/kernel/cpufeature.c +++ b/arch/arm64/kernel/cpufeature.c @@ -1649,46 +1649,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 */ @@ -1741,7 +1701,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 4b0b3515ee20..c2d6852a4e0c 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();