[PATCH v2 2/2] arm64: hibernate: use a typed entry to drop __nocfi

Ben Cressey ben at cressey.dev
Wed Nov 11 15:26:05 PST 2026


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 at arm.com>
Link: https://lore.kernel.org/linux-arm-kernel/arZSe8hNnRPM5FqY@J2N7QTR9R3.cambridge.arm.com/
Signed-off-by: Ben Cressey <ben at 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 at 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




More information about the kexec mailing list