linux-arm-kernel.lists.infradead.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] vmlinux.lds.h: CFI: Reduce alignment of jump-table to function alignment
@ 2022-09-22 21:57 Will Deacon
  2022-09-22 22:00 ` Sami Tolvanen
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: Will Deacon @ 2022-09-22 21:57 UTC (permalink / raw)
  To: linux-kernel
  Cc: linux-arm-kernel, Will Deacon, Sami Tolvanen, Mark Rutland,
	Mohan Rao .vanimina, Kees Cook, Nathan Chancellor

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 <samitolvanen@google.com>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: "Mohan Rao .vanimina" <mailtoc.mohanrao@gmail.com>
Cc: Kees Cook <keescook@chromium.org>
Cc: Nathan Chancellor <nathan@kernel.org>
Link: https://lore.kernel.org/all/CAL_GTzigiNOMYkOPX1KDnagPhJtFNqSK=1USNbS0wUL4PW6-Uw@mail.gmail.com/
Fixes: cf68fffb66d60 ("add support for Clang CFI")
Signed-off-by: Will Deacon <will@kernel.org>
---
 include/asm-generic/vmlinux.lds.h | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/include/asm-generic/vmlinux.lds.h b/include/asm-generic/vmlinux.lds.h
index 7515a465ec03..7c90b1ab3e00 100644
--- a/include/asm-generic/vmlinux.lds.h
+++ b/include/asm-generic/vmlinux.lds.h
@@ -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
-- 
2.37.3.998.g577e59143f-goog


_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH] vmlinux.lds.h: CFI: Reduce alignment of jump-table to function alignment
  2022-09-22 21:57 [PATCH] vmlinux.lds.h: CFI: Reduce alignment of jump-table to function alignment Will Deacon
@ 2022-09-22 22:00 ` Sami Tolvanen
  2022-09-22 22:18 ` Kees Cook
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: Sami Tolvanen @ 2022-09-22 22:00 UTC (permalink / raw)
  To: Will Deacon
  Cc: LKML, linux-arm-kernel, Mark Rutland, Mohan Rao .vanimina,
	Kees Cook, Nathan Chancellor

On Thu, Sep 22, 2022 at 2:57 PM Will Deacon <will@kernel.org> wrote:
>
> 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 <samitolvanen@google.com>
> Cc: Mark Rutland <mark.rutland@arm.com>
> Cc: "Mohan Rao .vanimina" <mailtoc.mohanrao@gmail.com>
> Cc: Kees Cook <keescook@chromium.org>
> Cc: Nathan Chancellor <nathan@kernel.org>
> Link: https://lore.kernel.org/all/CAL_GTzigiNOMYkOPX1KDnagPhJtFNqSK=1USNbS0wUL4PW6-Uw@mail.gmail.com/
> Fixes: cf68fffb66d60 ("add support for Clang CFI")
> Signed-off-by: Will Deacon <will@kernel.org>
> ---
>  include/asm-generic/vmlinux.lds.h | 3 +--
>  1 file changed, 1 insertion(+), 2 deletions(-)
>
> diff --git a/include/asm-generic/vmlinux.lds.h b/include/asm-generic/vmlinux.lds.h
> index 7515a465ec03..7c90b1ab3e00 100644
> --- a/include/asm-generic/vmlinux.lds.h
> +++ b/include/asm-generic/vmlinux.lds.h
> @@ -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

Thanks for sending the patch!

Reviewed-by: Sami Tolvanen <samitolvanen@google.com>

Sami

_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] vmlinux.lds.h: CFI: Reduce alignment of jump-table to function alignment
  2022-09-22 21:57 [PATCH] vmlinux.lds.h: CFI: Reduce alignment of jump-table to function alignment Will Deacon
  2022-09-22 22:00 ` Sami Tolvanen
@ 2022-09-22 22:18 ` Kees Cook
  2022-09-23 12:24 ` Mark Rutland
  2022-09-23 15:40 ` Will Deacon
  3 siblings, 0 replies; 5+ messages in thread
From: Kees Cook @ 2022-09-22 22:18 UTC (permalink / raw)
  To: Will Deacon
  Cc: linux-kernel, linux-arm-kernel, Sami Tolvanen, Mark Rutland,
	Mohan Rao .vanimina, Nathan Chancellor

On Thu, Sep 22, 2022 at 10:57:15PM +0100, Will Deacon wrote:
> 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]

> 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.

❤️

Reviewed-by: Kees Cook <keescook@chromium.org>

-Kees

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

-- 
Kees Cook

_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] vmlinux.lds.h: CFI: Reduce alignment of jump-table to function alignment
  2022-09-22 21:57 [PATCH] vmlinux.lds.h: CFI: Reduce alignment of jump-table to function alignment Will Deacon
  2022-09-22 22:00 ` Sami Tolvanen
  2022-09-22 22:18 ` Kees Cook
@ 2022-09-23 12:24 ` Mark Rutland
  2022-09-23 15:40 ` Will Deacon
  3 siblings, 0 replies; 5+ messages in thread
From: Mark Rutland @ 2022-09-23 12:24 UTC (permalink / raw)
  To: Will Deacon
  Cc: linux-kernel, linux-arm-kernel, Sami Tolvanen,
	Mohan Rao .vanimina, Kees Cook, Nathan Chancellor

On Thu, Sep 22, 2022 at 10:57:15PM +0100, Will Deacon wrote:
> 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 <samitolvanen@google.com>
> Cc: Mark Rutland <mark.rutland@arm.com>
> Cc: "Mohan Rao .vanimina" <mailtoc.mohanrao@gmail.com>
> Cc: Kees Cook <keescook@chromium.org>
> Cc: Nathan Chancellor <nathan@kernel.org>
> Link: https://lore.kernel.org/all/CAL_GTzigiNOMYkOPX1KDnagPhJtFNqSK=1USNbS0wUL4PW6-Uw@mail.gmail.com/
> Fixes: cf68fffb66d60 ("add support for Clang CFI")
> Signed-off-by: Will Deacon <will@kernel.org>

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:

Reviewed-by: Mark Rutland <mark.rutland@arm.com>
Tested-by: Mark Rutland <mark.rutland@arm.com>

Mark.

> ---
>  include/asm-generic/vmlinux.lds.h | 3 +--
>  1 file changed, 1 insertion(+), 2 deletions(-)
> 
> diff --git a/include/asm-generic/vmlinux.lds.h b/include/asm-generic/vmlinux.lds.h
> index 7515a465ec03..7c90b1ab3e00 100644
> --- a/include/asm-generic/vmlinux.lds.h
> +++ b/include/asm-generic/vmlinux.lds.h
> @@ -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
> -- 
> 2.37.3.998.g577e59143f-goog
> 

_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] vmlinux.lds.h: CFI: Reduce alignment of jump-table to function alignment
  2022-09-22 21:57 [PATCH] vmlinux.lds.h: CFI: Reduce alignment of jump-table to function alignment Will Deacon
                   ` (2 preceding siblings ...)
  2022-09-23 12:24 ` Mark Rutland
@ 2022-09-23 15:40 ` Will Deacon
  3 siblings, 0 replies; 5+ messages in thread
From: Will Deacon @ 2022-09-23 15:40 UTC (permalink / raw)
  To: linux-kernel, Will Deacon
  Cc: catalin.marinas, kernel-team, Mark Rutland, Nathan Chancellor,
	Kees Cook, Mohan Rao .vanimina, Sami Tolvanen, linux-arm-kernel

On Thu, 22 Sep 2022 22:57:15 +0100, Will Deacon wrote:
> 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,
-- 
Will

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

_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2022-09-23 15:42 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-09-22 21:57 [PATCH] vmlinux.lds.h: CFI: Reduce alignment of jump-table to function alignment Will Deacon
2022-09-22 22:00 ` Sami Tolvanen
2022-09-22 22:18 ` Kees Cook
2022-09-23 12:24 ` Mark Rutland
2022-09-23 15:40 ` Will Deacon

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).