From patchwork Tue Sep 12 14:16:22 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 13381793 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 AC01DCA0EEC for ; Tue, 12 Sep 2023 14:19: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=9PhXarDoG3bulz9rsEXPjDBv9kRk4gzjzfOg0CakuMI=; b=fCPst9DFb91c7mkG9kElJj/DnG ZjtfMzZ/yDBHZVSlc3zmVlVpvBFN3WF3YdlIEgamhLigan0ZGDZhAFbQIq5IHPpxlZEgV0zKbJkFY D/56UXckhaIO9D5NEdseNRaFuAy8oAw+fUe/nksWkxtmiTbozSZWY8vBNK6WtjHNrounrJ9A2ncnK wxLmJfRP2pV0TGUd+p6AXynsLtfhy4g9VoHwYLAeneNhEN/f3nR9WA+yPl12EiL1LHTzrngrn8xqq RA7CSJ9DZYnQZ5JT9Lmp1IiZgHraj0wWFlmANgQDSdB8fzAED4/NoMFEoOuHOfRRZnnjyrXEdvwIG XbYKNs9g==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qg4Eg-003XTv-2a; Tue, 12 Sep 2023 14:19:26 +0000 Received: from mail-yb1-xb49.google.com ([2607:f8b0:4864:20::b49]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qg4Dr-003WkJ-00 for linux-arm-kernel@lists.infradead.org; Tue, 12 Sep 2023 14:18:48 +0000 Received: by mail-yb1-xb49.google.com with SMTP id 3f1490d57ef6-d7e79ec07b4so5569611276.0 for ; Tue, 12 Sep 2023 07:18:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1694528313; x=1695133113; 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=iYrAAXbuRoK7lbpKClqf0Z9KrlvpEUh/9+3vTgpo0TY=; b=QxpBZeDEV1esWvdNE8MYJym7/A5SPrrjBpCjLw+/ga+m40HS/xGM21LndaIsUmhpgn 2ilUyuDMp5UKNweRmAVEGSMdwAGKDB6SZbTiOwWdhpjqOHz+JNyomtpiVp9hh6i85L7t J3kE3ATRLosjTl6yTlrBjxbRslojyqRbpCG5FiFBdvqWKlv5+w5s24EY3JIRWS1wDMNh dMh0i1vE++1mPJb4aiZpBfqrzEI8SY4TiGhjnkBpp2rCzoTkuF9a8jDu4MFLtos89h4I DG4Ct9jyWdLuwc229g0t1z442o+BMiY44wMSDHbj33XUpMIrEANS3KtKXrKLS6nFJC7C uVlg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694528313; x=1695133113; 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=iYrAAXbuRoK7lbpKClqf0Z9KrlvpEUh/9+3vTgpo0TY=; b=S+ivQb/vqGJU1SmHb7pkmIXx5iWkIOLr4r48+SFblCNMQuCOilzq1AtcnBh4VNA1/x lpg0TJHNfv5FKFxZFAfnFCwptIUFAOt9raxVtMYumV5pyfLkkMEGDZBKnEexsBlDWF5Z c7IuArjCui6JsWk5d4hK0QP4hx/fjPEDjkiRimD29oYmPrZ7Kr0Zu7JcQoycWcZNjM3E k89L0uW5T4rmGZ33N+TpbpS04Xyf8M7B1eTb6GHPe6zusDkF43A+3AhePj0sJqvJ06/p /0Hs2E5rsP7sidTUCBEOcBzBshgqJdB97ubR8GieI8O0TLPhnabN7NdxxkBEETb6r8wi d8EQ== X-Gm-Message-State: AOJu0YxXeORXWviNNEslLlGR3jtSZPLTgj8dZStaaRiBZ8RGZ1ifW7YX XZ6gr+OxPX5orIv+hycDZmPB9lcsKcWQkmgvltRNa9KOwj6e4QzKyNultFInr021CY7dniRz5Lo EoPUOJYfWlk1Sz1TYdKUgcznOqDKu7aAYYCVmLKTwWHDsyNnftdbWKZjcOQAR0eppLA54cvoMyY w= X-Google-Smtp-Source: AGHT+IFqbuZm7mbI9ikzsLYSVtPEqn9c8elQrZ+LhAno3j9wZ62vinTSFaMk9EPT3kcpgdNGHZ2/eWH3 X-Received: from palermo.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:118a]) (user=ardb job=sendgmr) by 2002:a05:6902:108f:b0:d78:3c2e:b186 with SMTP id v15-20020a056902108f00b00d783c2eb186mr294108ybu.5.1694528313222; Tue, 12 Sep 2023 07:18:33 -0700 (PDT) Date: Tue, 12 Sep 2023 14:16:22 +0000 In-Reply-To: <20230912141549.278777-63-ardb@google.com> Mime-Version: 1.0 References: <20230912141549.278777-63-ardb@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=sUQNGAibWz3dErcNZKyYQRoQSQUITT2GMpOs/+vgEVY=; b=owGbwMvMwCFmkMcZplerG8N4Wi2JIZWhaPspHtOr3yN//lojcWPyrd9bPa/zbtLbudOMezqHv kF6RurHjlIWBjEOBlkxRRaB2X/f7Tw9UarWeZYszBxWJpAhDFycAjCRUycYGfr6lpw8cXPlUZYv ppv3/PzWca8/4l2V/S2JjtW394VM3nCe4X9QnOCc7skWs/X8eFZoV8++7Tgxw+F17qrANTeuBEV fv84FAA== X-Mailer: git-send-email 2.42.0.283.g2d96d420d3-goog Message-ID: <20230912141549.278777-95-ardb@google.com> Subject: [PATCH v4 32/61] 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 , Joey Gouly X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230912_071835_078931_1EC762D1 X-CRM114-Status: GOOD ( 24.82 ) 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 94b68850cb9f..be41054a500e 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 bdd492cea88b..c91f658b175e 100644 --- a/arch/arm64/kernel/cpufeature.c +++ b/arch/arm64/kernel/cpufeature.c @@ -1626,46 +1626,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 */ @@ -1718,7 +1678,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();