mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Ben Cressey <ben@cressey.dev>
To: Catalin Marinas <catalin.marinas@arm.com>,
	 Will Deacon <will@kernel.org>,
	Mark Rutland <mark.rutland@arm.com>,
	 Pasha Tatashin <pasha.tatashin@soleen.com>,
	 "Rafael J. Wysocki" <rafael@kernel.org>,
	Pavel Machek <pavel@kernel.org>
Cc: linux-arm-kernel@lists.infradead.org,
	linux-kernel@vger.kernel.org,  linux-pm@vger.kernel.org,
	Kees Cook <kees@kernel.org>,
	 Sami Tolvanen <samitolvanen@google.com>,
	kexec@lists.infradead.org,  llvm@lists.linux.dev,
	Nathan Chancellor <nathan@kernel.org>,
	 Zhaoyang Huang <zhaoyang.huang@unisoc.com>,
	 Jeson Gao <jeson.gao@unisoc.com>
Subject: [PATCH v2 2/2] arm64: hibernate: use a typed entry to drop __nocfi
Date: Wed, 11 Nov 2026 15:26:05 -0800	[thread overview]
Message-ID: <20261111-arm64-kexec-nocfi-v2-2-ddf0c4aa326f@cressey.dev> (raw)
In-Reply-To: <20261111-arm64-kexec-nocfi-v2-0-ddf0c4aa326f@cressey.dev>

swsusp_arch_resume() calls the safe page's copy of
swsusp_arch_suspend_exit() indirectly, without a kCFI check. The MMU is
on at the call and TTBR0 maps only that page. The copy starts at the
page's first byte, so the check's load of the type hash in front of it
would fault. SYM_CODE_START() also gives the routine no hash.
Commit e2f8216ca2d8 ("arm64: Set __nocfi on swsusp_arch_resume()")
disabled the check "for now", since the entry had to be first
in .hibernate_exit.text.

SYM_TYPED_FUNC_START() emits the hash inside that section, and
create_safe_exec_page() already copies the whole section, at most 4 KiB,
to the page's start, so the page holds both hash and entry. Relax the
linker script's ASSERT, so the entry need only be in the section.

Fix by typing the routine, with a C prototype, entering the copy at the
symbol's offset in the section, and dropping __nocfi.

Suggested-by: Mark Rutland <mark.rutland@arm.com>
Link: https://lore.kernel.org/linux-arm-kernel/arZSe8hNnRPM5FqY@J2N7QTR9R3.cambridge.arm.com/
Signed-off-by: Ben Cressey <ben@cressey.dev>
---
 arch/arm64/include/asm/suspend.h  |  5 +++++
 arch/arm64/kernel/hibernate-asm.S |  7 +++++--
 arch/arm64/kernel/hibernate.c     | 14 +++++++++-----
 arch/arm64/kernel/vmlinux.lds.S   |  5 +++--
 4 files changed, 22 insertions(+), 9 deletions(-)

diff --git a/arch/arm64/include/asm/suspend.h b/arch/arm64/include/asm/suspend.h
index e9ce68d50ba4..e75aa3681414 100644
--- a/arch/arm64/include/asm/suspend.h
+++ b/arch/arm64/include/asm/suspend.h
@@ -45,6 +45,11 @@ void _cpu_resume(void);
 
 int swsusp_arch_suspend(void);
 int swsusp_arch_resume(void);
+void __noreturn swsusp_arch_suspend_exit(phys_addr_t tmp_pg_dir,
+					 phys_addr_t ttbr1_el1,
+					 void *reenter_kernel, void *pblist,
+					 phys_addr_t hyp_stub_vectors,
+					 phys_addr_t zero_page);
 int arch_hibernation_header_save(void *addr, unsigned int max_size);
 int arch_hibernation_header_restore(void *addr);
 
diff --git a/arch/arm64/kernel/hibernate-asm.S b/arch/arm64/kernel/hibernate-asm.S
index 2baefe7a82d3..ef8c0b926dcb 100644
--- a/arch/arm64/kernel/hibernate-asm.S
+++ b/arch/arm64/kernel/hibernate-asm.S
@@ -6,6 +6,7 @@
  * Author:	James Morse <james.morse@arm.com>
  */
 #include <linux/linkage.h>
+#include <linux/cfi_types.h>
 #include <linux/errno.h>
 
 #include <asm/asm-offsets.h>
@@ -31,6 +32,8 @@
  * switches to a copy of the linear map in ttbr1, performs the restore, then
  * switches ttbr1 to the original kernel's swapper_pg_dir.
  *
+ * Called indirectly by swsusp_arch_resume(), this code needs a kCFI type hash.
+ *
  * All of memory gets written to, including code. We need to clean the kernel
  * text to the Point of Coherence (PoC) before secondary cores can be booted.
  * Because the kernel modules and executable pages mapped to user space are
@@ -45,7 +48,7 @@
  * x5: physical address of a  zero page that remains zero after resume
  */
 .pushsection    ".hibernate_exit.text", "ax"
-SYM_CODE_START(swsusp_arch_suspend_exit)
+SYM_TYPED_FUNC_START(swsusp_arch_suspend_exit)
 	/*
 	 * We execute from ttbr0, change ttbr1 to our copied linear map tables
 	 * with a break-before-make via the zero page
@@ -93,5 +96,5 @@ alternative_insn "dc cvau, x4",  "dc civac, x4",  ARM64_WORKAROUND_CLEAN_CACHE
 	mov	x0, #HVC_SET_VECTORS
 	hvc	#0
 3:	ret
-SYM_CODE_END(swsusp_arch_suspend_exit)
+SYM_FUNC_END(swsusp_arch_suspend_exit)
 .popsection
diff --git a/arch/arm64/kernel/hibernate.c b/arch/arm64/kernel/hibernate.c
index 8c39863d2d63..d5a066ece8b2 100644
--- a/arch/arm64/kernel/hibernate.c
+++ b/arch/arm64/kernel/hibernate.c
@@ -418,15 +418,15 @@ int swsusp_arch_suspend(void)
  * Memory allocated by get_safe_page() will be dealt with by the hibernate code,
  * we don't need to free it here.
  */
-int __nocfi swsusp_arch_resume(void)
+int swsusp_arch_resume(void)
 {
 	int rc;
 	void *zero_page;
 	size_t exit_size;
 	pgd_t *tmp_pg_dir;
 	phys_addr_t el2_vectors;
-	void __noreturn (*hibernate_exit)(phys_addr_t, phys_addr_t, void *,
-					  void *, phys_addr_t, phys_addr_t);
+	phys_addr_t exit_page;
+	typeof(swsusp_arch_suspend_exit) *hibernate_exit;
 	struct trans_pgd_info trans_info = {
 		.trans_alloc_page	= hibernate_page_alloc,
 		.trans_alloc_arg	= (__force void *)GFP_ATOMIC,
@@ -463,14 +463,18 @@ int __nocfi swsusp_arch_resume(void)
 	exit_size = __hibernate_exit_text_end - __hibernate_exit_text_start;
 	/*
 	 * Copy swsusp_arch_suspend_exit() to a safe page. This will generate
-	 * a new set of ttbr0 page tables and load them.
+	 * a new set of ttbr0 page tables and load them. The whole section is
+	 * copied: a kCFI hash may precede the entry point.
 	 */
 	rc = create_safe_exec_page(__hibernate_exit_text_start, exit_size,
-				   (phys_addr_t *)&hibernate_exit);
+				   &exit_page);
 	if (rc) {
 		pr_err("Failed to create safe executable page for hibernate_exit code.\n");
 		return rc;
 	}
+	hibernate_exit = (void *)(exit_page +
+				  ((unsigned long)swsusp_arch_suspend_exit -
+				   (unsigned long)__hibernate_exit_text_start));
 
 	/*
 	 * KASLR will cause the el2 vectors to be in a different location in
diff --git a/arch/arm64/kernel/vmlinux.lds.S b/arch/arm64/kernel/vmlinux.lds.S
index c95243a8b24e..641074a6e6ca 100644
--- a/arch/arm64/kernel/vmlinux.lds.S
+++ b/arch/arm64/kernel/vmlinux.lds.S
@@ -412,8 +412,9 @@ ASSERT(__idmap_text_end - (__idmap_text_start & ~(SZ_4K - 1)) <= SZ_4K,
 #ifdef CONFIG_HIBERNATION
 ASSERT(__hibernate_exit_text_end - __hibernate_exit_text_start <= SZ_4K,
        "Hibernate exit text is bigger than 4 KiB")
-ASSERT(__hibernate_exit_text_start == swsusp_arch_suspend_exit,
-       "Hibernate exit text does not start with swsusp_arch_suspend_exit")
+ASSERT(swsusp_arch_suspend_exit >= __hibernate_exit_text_start &&
+       swsusp_arch_suspend_exit < __hibernate_exit_text_end,
+       "Hibernate exit text does not contain swsusp_arch_suspend_exit")
 #endif
 #ifdef CONFIG_UNMAP_KERNEL_AT_EL0
 ASSERT((__entry_tramp_text_end - __entry_tramp_text_start) <= 3*PAGE_SIZE,

-- 
2.56.0


  parent reply	other threads:[~2026-11-11 23:26 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-11 23:26 [PATCH v2 0/2] arm64: use typed entries into copied code for kCFI Ben Cressey
2026-11-11 23:26 ` [PATCH v2 1/2] arm64: kexec: use a typed entry to fix kCFI oops Ben Cressey
2026-11-11 23:26 ` Ben Cressey [this message]
2026-11-18 18:33   ` [PATCH v2 2/2] arm64: hibernate: use a typed entry to drop __nocfi Catalin Marinas
2026-11-18 18:26 ` (subset) [PATCH v2 0/2] arm64: use typed entries into copied code for kCFI Catalin Marinas

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261111-arm64-kexec-nocfi-v2-2-ddf0c4aa326f@cressey.dev \
    --to=ben@cressey.dev \
    --cc=catalin.marinas@arm.com \
    --cc=jeson.gao@unisoc.com \
    --cc=kees@kernel.org \
    --cc=kexec@lists.infradead.org \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-pm@vger.kernel.org \
    --cc=llvm@lists.linux.dev \
    --cc=mark.rutland@arm.com \
    --cc=nathan@kernel.org \
    --cc=pasha.tatashin@soleen.com \
    --cc=pavel@kernel.org \
    --cc=rafael@kernel.org \
    --cc=samitolvanen@google.com \
    --cc=will@kernel.org \
    --cc=zhaoyang.huang@unisoc.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®