Skip to contentSkip to conversation

vmlinux.lds.h: CFI: Reduce alignment of jump-table to function alignment

PATCH Applied Will Deacon 4 participants 5 messages Sep 22, 2022 – Sep 23, 2022 Generic include/asm header files

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 vmlinux.lds.h: CFI: Reduce alignment of jump-table to function alignment

Due to undocumented, hysterical raisins on x86, the CFI jump-table sections in .text are needlessly aligned to PMD_SIZE in the vmlinux linker script. When compiling a CFI-enabled arm64 kernel with a 64KiB page-size, a PMD maps 512MiB of virtual memory and so the .text section increases to a whopping 940MiB and blows the final Image up to 960MiB. Others report a link failure.

Since the CFI jump-table requires only instruction alignment, reduce the alignment directives to function alignment for parity with other parts of the .text section. This reduces the size of the .text section for the aforementioned 64KiB page size arm64 kernel to 19MiB for a much more reasonable total Image size of 39MiB.

Cc Sami Tolvanen <[email protected]>
Cc Mark Rutland <[email protected]>
Cc "Mohan Rao .vanimina" <[email protected]>
Cc Kees Cook <[email protected]>
Cc Nathan Chancellor <[email protected]>
Fixes cf68fffb66d60 ("add support for Clang CFI")
---
 include/asm-generic/vmlinux.lds.h | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
include/asm-generic/vmlinux.lds.h +1 -2
index 7515a465ec03..7c90b1ab3e00 100644
@@ -543,10 +543,9 @@
  */
 #ifdef CONFIG_CFI_CLANG
 #define TEXT_CFI_JT							\
-		. = ALIGN(PMD_SIZE);					\
+		ALIGN_FUNCTION();					\
 		__cfi_jt_start = .;					\
 		*(.text..L.cfi.jumptable .text..L.cfi.jumptable.*)	\
-		. = ALIGN(PMD_SIZE);					\
 		__cfi_jt_end = .;
 #else
 #define TEXT_CFI_JT
Signature · 1 line
2.37.3.998.g577e59143f-goog
Reviewed-by on v1 1/1
#1 lore raw
Quoted 32 lines · Will Deacon

Due to undocumented, hysterical raisins on x86, the CFI jump-table sections in .text are needlessly aligned to PMD_SIZE in the vmlinux linker script. When compiling a CFI-enabled arm64 kernel with a 64KiB page-size, a PMD maps 512MiB of virtual memory and so the .text section increases to a whopping 940MiB and blows the final Image up to 960MiB. Others report a link failure.

Since the CFI jump-table requires only instruction alignment, reduce the alignment directives to function alignment for parity with other parts of the .text section. This reduces the size of the .text section for the aforementioned 64KiB page size arm64 kernel to 19MiB for a much more reasonable total Image size of 39MiB.

Cc Sami Tolvanen <[email protected]>
Cc Mark Rutland <[email protected]>
Cc "Mohan Rao .vanimina" <[email protected]>
Cc Kees Cook <[email protected]>
Cc Nathan Chancellor <[email protected]>
Fixes cf68fffb66d60 ("add support for Clang CFI")
---
 include/asm-generic/vmlinux.lds.h | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
include/asm-generic/vmlinux.lds.h +0 -0
index 7515a465ec03..7c90b1ab3e00 100644
@@ -543,10 +543,9 @@
  */

include/asm-generic/vmlinux.lds.h line 551

Quoted 10 lines
include/asm-generic/vmlinux.lds.h +1 -2
 #ifdef CONFIG_CFI_CLANG
 #define TEXT_CFI_JT                                                    \
-               . = ALIGN(PMD_SIZE);                                    \
+               ALIGN_FUNCTION();                                       \
                __cfi_jt_start = .;                                     \
                *(.text..L.cfi.jumptable .text..L.cfi.jumptable.*)      \
-               . = ALIGN(PMD_SIZE);                                    \
                __cfi_jt_end = .;
 #else
 #define TEXT_CFI_JT

Thanks for sending the patch!

Sami

Reviewed-by on v1 1/1
#2 lore raw
In reply to Will Deacon
Quoted 6 lines · Will Deacon

Due to undocumented, hysterical raisins on x86, the CFI jump-table sections in .text are needlessly aligned to PMD_SIZE in the vmlinux linker script. When compiling a CFI-enabled arm64 kernel with a 64KiB page-size, a PMD maps 512MiB of virtual memory and so the .text section increases to a whopping 940MiB and blows the final Image up to 960MiB. Others report a link failure.

Heh. "That's not an image ... THIS is an image."[1]

Quoted 5 lines

Since the CFI jump-table requires only instruction alignment, reduce the alignment directives to function alignment for parity with other parts of the .text section. This reduces the size of the .text section for the aforementioned 64KiB page size arm64 kernel to 19MiB for a much more reasonable total Image size of 39MiB.

❤️

-Kees

[1] https://www.youtube.com/watch?v=rne8pOxGuwM#t=27

Signature · 1 line
Kees Cook
Reviewed-by Tested-by on v1 1/1
#3 lore raw
In reply to Will Deacon
Quoted 13 lines · Will Deacon

Due to undocumented, hysterical raisins on x86, the CFI jump-table sections in .text are needlessly aligned to PMD_SIZE in the vmlinux linker script. When compiling a CFI-enabled arm64 kernel with a 64KiB page-size, a PMD maps 512MiB of virtual memory and so the .text section increases to a whopping 940MiB and blows the final Image up to 960MiB. Others report a link failure.

Since the CFI jump-table requires only instruction alignment, reduce the alignment directives to function alignment for parity with other parts of the .text section. This reduces the size of the .text section for the aforementioned 64KiB page size arm64 kernel to 19MiB for a much more reasonable total Image size of 39MiB.

Quoted 8 lines
Cc Sami Tolvanen <[email protected]>
Cc Mark Rutland <[email protected]>
Cc "Mohan Rao .vanimina" <[email protected]>
Cc Kees Cook <[email protected]>
Cc Nathan Chancellor <[email protected]>
Fixes cf68fffb66d60 ("add support for Clang CFI")

Thanks for this!

I gave this a spin atop v6.-rc3 defconfig with:

* CONFIG_ARM64_64K_PAGES=y
* CONFIG_LTO_CLANG_THIN=y
* CONFIG_CFI_CLANG=y

The sizes clearly speak for themselves:

| % ls -al *-v6.0-rc3-64K-CFI*
| -rwxr-xr-x 1 mark mark  959693312 Sep 23 13:13 Image-v6.0-rc3-64K-CFI
| -rwxr-xr-x 1 mark mark   38781440 Sep 23 13:19 Image-v6.0-rc3-64K-CFI-patched
| -rwxr-xr-x 1 mark mark 1378631080 Sep 23 13:13 vmlinux-v6.0-rc3-64K-CFI
| -rwxr-xr-x 1 mark mark  453015376 Sep 23 13:19 vmlinux-v6.0-rc3-64K-CFI-patched

... and before and after applying the patch, the kernel booted fine under QEMU (in TCG mode, as Cortex-A53).

I see that in scripts/module.lds.S we place the CFI bits into the .text section, and that's only aligned to PAGE_SIZE, so we don't have a similar issue for modules.

FWIW:

Mark.

Quoted 23 lines
---
 include/asm-generic/vmlinux.lds.h | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
include/asm-generic/vmlinux.lds.h +1 -2
index 7515a465ec03..7c90b1ab3e00 100644
@@ -543,10 +543,9 @@
  */
 #ifdef CONFIG_CFI_CLANG
 #define TEXT_CFI_JT							\
-		. = ALIGN(PMD_SIZE);					\
+		ALIGN_FUNCTION();					\
 		__cfi_jt_start = .;					\
 		*(.text..L.cfi.jumptable .text..L.cfi.jumptable.*)	\
-		. = ALIGN(PMD_SIZE);					\
 		__cfi_jt_end = .;
 #else
 #define TEXT_CFI_JT
Signature · 1 line
2.37.3.998.g577e59143f-goog

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

author Applied
#4 lore raw
In reply to Will Deacon
Quoted 8 lines · Will Deacon

Due to undocumented, hysterical raisins on x86, the CFI jump-table sections in .text are needlessly aligned to PMD_SIZE in the vmlinux linker script. When compiling a CFI-enabled arm64 kernel with a 64KiB page-size, a PMD maps 512MiB of virtual memory and so the .text section increases to a whopping 940MiB and blows the final Image up to 960MiB. Others report a link failure.

[...]

Applied to arm64 (for-next/fixes), thanks!

[1/1] vmlinux.lds.h: CFI: Reduce alignment of jump-table to function alignment

      https://git.kernel.org/arm64/c/13b056696291

Cheers,

Signature · 5 lines
Will

https://fixes.arm64.dev
https://next.arm64.dev
https://will.arm64.dev