Skip to contentSkip to conversation

arm64: Combine module .init.text with .text

PATCH In review Ard Biesheuvel 3 participants 6 messages Nov 19

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover arm64: Combine module .init.text with .text

From: Ard Biesheuvel <[email protected]>

This is a follow-up to "arm64: Implement support for BTI veneers" [0] which adds support to the module loader for indirect calls to functions that were emitted by the compiler without a BTI landing pad.

This series takes the ppc approach, and simply combines .init.text with .text when loading modules, so that cross-section calls in the same module (which may originate in the same compilation unit) can never go out of direct branching range.

This makes the BTI veneer solution unnecessary, and permits some other simplifications that were added, even though .init.text and .text rarely end up far away from each other in practice.

Live patch needs additional work, so BTI remains disabled when live patch is enabled. (It expects to be able to make indirect calls to any function in vmlinux or elsewhere, even if they were never exported)

Cc Mark Brown <[email protected]>
Cc Josh Poimboeuf <[email protected]>
Cc Nick Desaulniers <[email protected]>

[0] https://lore.kernel.org/all/[email protected]/

Ard Biesheuvel (3):
arm64: module: Keep executable init sections in the core text region
arm64: Re-enable kernel BTI for compatible linkers
arm64: module: Revert separate init PLT and init ftrace trampoline

 arch/arm64/Kconfig                  | 17 ++--
 arch/arm64/include/asm/module.h     | 11 +--
 arch/arm64/include/asm/module.lds.h |  2 -
 arch/arm64/kernel/ftrace.c          | 13 +--
 arch/arm64/kernel/module-plts.c     | 98 +++++++-------------
 arch/arm64/kernel/module.c          | 46 ++++++---
 6 files changed, 84 insertions(+), 103 deletions(-)
Signature · 1 line
2.56.1.708.g77570a6a1d-goog
#1 lore raw

PATCH 1/3 arm64: module: Keep executable init sections in the core text region

From: Ard Biesheuvel <[email protected]>

Since commit 3e35d303ab7d ("arm64: module: rework module VA range selection"), the core and init text regions of a module may end up more than 128 MiB apart, i.e., out of direct branching range of each other. This is why the module loader maintains a separate .init.plt section for branches emitted into .init.text, and a separate ftrace trampoline for ftrace callsites in .init.text.

__init code typically makes up only a small fraction of a module's code, so instead, keep .init.text in the core text region, by overriding module_init_section(). Do the same for .exit.text in module_exit_section(), which is only consulted when CONFIG_MODULE_UNLOAD is disabled, in which case .exit sections are moved into the init region too.

On arm64 allmodconfig (10604 modules, disregarding modules with 'test' or 'torture' in their name), this grows the aggregate size of the modules' core text regions by 2.6 MB (+0.58%), or 243 bytes per module on average. As this only costs an additional page at runtime if a module's core text region happens to end within that many bytes of a page boundary, this is expected to affect ~5.5% of modules. In practice, 527 modules (5%) grow by at least one page, of which 16 by more than one, adding up to 567 pages of 4 KiB. Given that allmodconfig enables KASAN, KCOV, UBSAN and GCOV instrumentation, which inflates __init code by 1.5-2x, these figures should be taken as an upper bound.

On a distro-style configuration without sanitizers (1023 modules), the average growth is 104 bytes per module, or ~10 KiB per 100 loaded modules, which is expected to cost ~2.5 additional pages of 4 KiB (or ~0.6 pages of 16 KiB).

This permits the separate init PLT and init ftrace trampoline to be removed in a subsequent patch.

---
 arch/arm64/kernel/module.c | 33 ++++++++++++++++++++
 1 file changed, 33 insertions(+)
arch/arm64/kernel/module.c +33 -0
index 24adb581af0e..39e7a50733a9 100644
@@ -25,6 +25,39 @@
 #include <asm/sections.h>
 #include <asm/text-patching.h>
 
+/*
+ * The module loader allocates the core and init text regions of a module
+ * independently, and so they may end up out of direct branching range of each
+ * other. This complicates the handling of PLTs and ftrace trampolines, which
+ * need to be emitted within range of the branch instructions that use them.
+ *
+ * So keep executable .init sections in the core text region instead. This
+ * costs a bit of memory, as __init code will no longer be freed after the
+ * module's init routine returns, but this is usually a small fraction of the
+ * overall size of the module. Non-executable .init sections are not affected.
+ */
+bool module_init_section(const char *name)
+{
+	if (strstarts(name, ".init.text"))
+		return false;
+
+	return strstarts(name, ".init");
+}
+
+/*
+ * Without CONFIG_MODULE_UNLOAD, the module loader places .exit sections into
+ * the init region, as they will never be used. Keep .exit.text in the core
+ * text region in this case too, so that branches emitted into it are always
+ * guaranteed to be in range of the core PLT.
+ */
+bool module_exit_section(const char *name)
+{
+	if (strstarts(name, ".exit.text"))
+		return false;
+
+	return strstarts(name, ".exit");
+}
+
 enum aarch64_reloc_op {
 	RELOC_OP_NONE,
 	RELOC_OP_ABS,
Signature · 1 line
2.56.1.708.g77570a6a1d-goog
#2 lore raw

PATCH 2/3 arm64: Re-enable kernel BTI for compatible linkers

In reply to Ard Biesheuvel

From: Ard Biesheuvel <[email protected]>

The compiler is permitted to omit BTI landing pads from static functions that never have their address taken, but are only called directly, even if those calls originate from other code sections. This causes problems for calls from .init.text into .text if those sections are placed far apart, as veneers involving indirect calls will be used in that case.

Now that .text and .init.text are combined, this can no longer happen, and so we can re-enable kernel mode BTI under two conditions:

- live patch is disabled - it also needs BTI landings pads on functions with static linkage inside vmlinux that were never exported (and therefore never had their address taken)

- landings pads are emitted even in pathological cases where the size of vmlinux's .text segment exceeds the range of a direct branch, and the linker needs to emit landing pads at the callee end so that the veneers can call them without causing a BTI fault.

---
 arch/arm64/Kconfig | 17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)
arch/arm64/Kconfig +11 -6
index 1075bc661833..f0fb2d7ff130 100644
@@ -2202,18 +2202,23 @@ config ARM64_BTI
 	  BTI, such binaries can still run, but you get no additional
 	  enforcement of branch destinations.
 
+config LD_HAS_BTI_STUBS
+	# A linker may need to insert a veneer when a call to a function with
+	# static linkage in the same compilation unit but in a different
+	# section is out of range for a direct branch instruction. Such a
+	# veneer performs an indirect call, and thus requires a landing pad
+	# under BTI, which the static function may lack. Only recent linkers
+	# are guaranteed to emit those landing pads correctly in all cases.
+	def_bool (LD_IS_BFD && LD_VERSION >= 24200) || \
+		 (LD_IS_LLD && LLD_VERSION >= 200000)
+
 config ARM64_BTI_KERNEL
 	bool "Use Branch Target Identification for kernel"
 	default y
 	depends on ARM64_BTI
 	depends on ARM64_PTR_AUTH_KERNEL
 	depends on CC_HAS_BRANCH_PROT_PAC_RET_BTI
-	# https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94697
-	depends on !CC_IS_GCC || GCC_VERSION >= 100100
-	# https://gcc.gnu.org/bugzilla/show_bug.cgi?id=106671
-	depends on !CC_IS_GCC
-	# https://github.com/llvm/llvm-project/issues/215547
-	depends on !CC_IS_CLANG || CLANG_VERSION < 210000
+	depends on !LIVEPATCH && LD_HAS_BTI_STUBS
 	depends on (!FUNCTION_GRAPH_TRACER || DYNAMIC_FTRACE_WITH_ARGS)
 	help
 	  Build the kernel with Branch Target Identification annotations
Signature · 1 line
2.56.1.708.g77570a6a1d-goog
#5 lore raw

PATCH 3/3 arm64: module: Revert separate init PLT and init ftrace trampoline

In reply to Ard Biesheuvel

From: Ard Biesheuvel <[email protected]>

This reverts commit a7ed7b9d0ebb ("arm64: ftrace: fix unreachable PLT for ftrace_caller in init_module with CONFIG_DYNAMIC_FTRACE") and commit 24af6c4e4e0f ("arm64: module: split core and init PLT sections").

Now that executable init sections are kept in the core text region, all branch instructions that may need a PLT entry, as well as all ftrace callsites, live in the core text region, and so a single PLT section and a single ftrace trampoline suffice.

This also means that branches to symbols defined in another section of the same module no longer need a PLT slot, as all executable sections are within direct branching range of each other. So only reserve PLT slots for branches to external symbols, i.e., SHN_UNDEF symbols as before 24af6c4e4e0f, but also SHN_LIVEPATCH symbols (and other reserved section indexes), as these are resolved against vmlinux or other modules.

On arm64 allmodconfig (10604 modules, disregarding modules with 'test' or 'torture' in their name), this drops 2126 reserved but never used PLT slots for intra-module branches (about 25 KB in total).

---
 arch/arm64/include/asm/module.h     | 11 +--
 arch/arm64/include/asm/module.lds.h |  2 -
 arch/arm64/kernel/ftrace.c          | 13 +--
 arch/arm64/kernel/module-plts.c     | 98 +++++++-------------
 arch/arm64/kernel/module.c          | 13 +--
 5 files changed, 40 insertions(+), 97 deletions(-)
arch/arm64/include/asm/module.h +2 -9
index fb9b88eebeb1..9d34f915d59b 100644
@@ -7,24 +7,17 @@
 
 #include <asm-generic/module.h>
 
-struct mod_plt_sec {
+struct mod_arch_specific {
 	int			plt_shndx;
 	int			plt_num_entries;
 	int			plt_max_entries;
-};
-
-struct mod_arch_specific {
-	struct mod_plt_sec	core;
-	struct mod_plt_sec	init;
 
 	/* for CONFIG_DYNAMIC_FTRACE */
 	struct plt_entry	*ftrace_trampolines;
-	struct plt_entry	*init_ftrace_trampolines;
 };
 
 u64 module_emit_plt_entry(struct module *mod, Elf64_Shdr *sechdrs,
-			  void *loc, const Elf64_Rela *rela,
-			  Elf64_Sym *sym);
+			  const Elf64_Rela *rela, Elf64_Sym *sym);
 
 u64 module_emit_veneer_for_adrp(struct module *mod, Elf64_Shdr *sechdrs,
 				void *loc, u64 val);
arch/arm64/include/asm/module.lds.h +0 -2
index 603f92a8a73e..983e768203a1 100644
@@ -1,8 +1,6 @@
 SECTIONS {
 	.plt 0 : { BYTE(0) }
-	.init.plt 0 : { BYTE(0) }
 	.text.ftrace_trampoline 0 : { BYTE(0) }
-	.init.text.ftrace_trampoline 0 : { BYTE(0) }
 
 #ifdef CONFIG_UNWIND_TABLES
 	/*
arch/arm64/kernel/ftrace.c +3 -10
index e1a3c0b3a051..389675690293 100644
@@ -258,17 +258,10 @@ int ftrace_update_ftrace_func(ftrace_func_t func)
 	return ftrace_modify_code(pc, 0, new, false);
 }
 
-static struct plt_entry *get_ftrace_plt(struct module *mod, unsigned long addr)
+static struct plt_entry *get_ftrace_plt(struct module *mod)
 {
 #ifdef CONFIG_MODULES
-	struct plt_entry *plt = NULL;
-
-	if (within_module_mem_type(addr, mod, MOD_INIT_TEXT))
-		plt = mod->arch.init_ftrace_trampolines;
-	else if (within_module_mem_type(addr, mod, MOD_TEXT))
-		plt = mod->arch.ftrace_trampolines;
-	else
-		return NULL;
+	struct plt_entry *plt = mod->arch.ftrace_trampolines;
 
 	return &plt[FTRACE_PLT_IDX];
 #else
@@ -339,7 +332,7 @@ static bool ftrace_find_callable_addr(struct dyn_ftrace *rec,
 	if (WARN_ON(!mod))
 		return false;
 
-	plt = get_ftrace_plt(mod, pc);
+	plt = get_ftrace_plt(mod);
 	if (!plt) {
 		pr_err("ftrace: no module PLT for %ps\n", (void *)*addr);
 		return false;
arch/arm64/kernel/module-plts.c +34 -64

184 lines not on this page · raw message

arch/arm64/kernel/module.c +1 -12
index 39e7a50733a9..e0cbd499e4a9 100644
@@ -450,7 +450,7 @@ int apply_relocate_add(Elf64_Shdr *sechdrs,
 			ovf = reloc_insn_imm(RELOC_OP_PREL, loc, val, 2, 26,
 					     AARCH64_INSN_IMM_26, me);
 			if (ovf == -ERANGE) {
-				val = module_emit_plt_entry(me, sechdrs, loc, &rel[i], sym);
+				val = module_emit_plt_entry(me, sechdrs, &rel[i], sym);
 				if (!val)
 					return -ENOEXEC;
 				ovf = reloc_insn_imm(RELOC_OP_PREL, loc, val, 2,
@@ -499,17 +499,6 @@ static int module_init_ftrace_plt(const Elf_Ehdr *hdr,
 	__init_plt(&plts[FTRACE_PLT_IDX], FTRACE_ADDR);
 
 	mod->arch.ftrace_trampolines = plts;
-
-	s = find_section(hdr, sechdrs, ".init.text.ftrace_trampoline");
-	if (!s)
-		return -ENOEXEC;
-
-	plts = (void *)s->sh_addr;
-
-	__init_plt(&plts[FTRACE_PLT_IDX], FTRACE_ADDR);
-
-	mod->arch.init_ftrace_trampolines = plts;
-
 #endif
 	return 0;
 }
Signature · 1 line
2.56.1.708.g77570a6a1d-goog
#3 lore raw

Re: [PATCH 2/3] arm64: Re-enable kernel BTI for compatible linkers

In reply to Ard Biesheuvel
Quoted 33 lines · Ard Biesheuvel

From: Ard Biesheuvel <[email protected]>

The compiler is permitted to omit BTI landing pads from static functions that never have their address taken, but are only called directly, even if those calls originate from other code sections. This causes problems for calls from .init.text into .text if those sections are placed far apart, as veneers involving indirect calls will be used in that case.

Now that .text and .init.text are combined, this can no longer happen, and so we can re-enable kernel mode BTI under two conditions:

- live patch is disabled - it also needs BTI landings pads on functions with static linkage inside vmlinux that were never exported (and therefore never had their address taken)

- landings pads are emitted even in pathological cases where the size of vmlinux's .text segment exceeds the range of a direct branch, and the linker needs to emit landing pads at the callee end so that the veneers can call them without causing a BTI fault.

---
 arch/arm64/Kconfig | 17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)
arch/arm64/Kconfig +0 -0
index 1075bc661833..f0fb2d7ff130 100644
@@ -2202,18 +2202,23 @@ config ARM64_BTI
          BTI, such binaries can still run, but you get no additional
          enforcement of branch destinations.

arch/arm64/Kconfig config ARM64_BTI · line 2213

Quoted 10 lines
arch/arm64/Kconfig +9 -0
 
+config LD_HAS_BTI_STUBS
+       # A linker may need to insert a veneer when a call to a function with
+       # static linkage in the same compilation unit but in a different
+       # section is out of range for a direct branch instruction. Such a
+       # veneer performs an indirect call, and thus requires a landing pad
+       # under BTI, which the static function may lack. Only recent linkers
+       # are guaranteed to emit those landing pads correctly in all cases.
+       def_bool (LD_IS_BFD && LD_VERSION >= 24200) || \
+                (LD_IS_LLD && LLD_VERSION >= 200000)

A bit of a rock fetching quest (sorry), but it would make it easier for reviewers to verify these version checks if you had git trailers linking to commits (or bug reports) for these linkers with the relevant fix. I don't think that necessitates a v2, if the maintainers you're expecting to pick up this series are kind enough to manually re-add them (unless b4 can pick up Link: trailers like that).

Quoted 20 lines
arch/arm64/Kconfig +2 -7
+
 config ARM64_BTI_KERNEL
        bool "Use Branch Target Identification for kernel"
        default y
        depends on ARM64_BTI
        depends on ARM64_PTR_AUTH_KERNEL
        depends on CC_HAS_BRANCH_PROT_PAC_RET_BTI
-       # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94697
-       depends on !CC_IS_GCC || GCC_VERSION >= 100100
-       # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=106671
-       depends on !CC_IS_GCC
-       # https://github.com/llvm/llvm-project/issues/215547
-       depends on !CC_IS_CLANG || CLANG_VERSION < 210000
+       depends on !LIVEPATCH && LD_HAS_BTI_STUBS
        depends on (!FUNCTION_GRAPH_TRACER || DYNAMIC_FTRACE_WITH_ARGS)
        help
          Build the kernel with Branch Target Identification annotations
--

2.56.1.708.g77570a6a1d-goog

Signature · 2 lines
Thanks,
~Nick Desaulniers
#4 lore raw

Re: [PATCH 2/3] arm64: Re-enable kernel BTI for compatible linkers

Quoted 45 lines · Nick Desaulniers
Quoted 33 lines · Ard Biesheuvel

From: Ard Biesheuvel <[email protected]>

The compiler is permitted to omit BTI landing pads from static functions that never have their address taken, but are only called directly, even if those calls originate from other code sections. This causes problems for calls from .init.text into .text if those sections are placed far apart, as veneers involving indirect calls will be used in that case.

Now that .text and .init.text are combined, this can no longer happen, and so we can re-enable kernel mode BTI under two conditions:

- live patch is disabled - it also needs BTI landings pads on functions with static linkage inside vmlinux that were never exported (and therefore never had their address taken)

- landings pads are emitted even in pathological cases where the size of vmlinux's .text segment exceeds the range of a direct branch, and the linker needs to emit landing pads at the callee end so that the veneers can call them without causing a BTI fault.

---
 arch/arm64/Kconfig | 17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)
arch/arm64/Kconfig +0 -0
index 1075bc661833..f0fb2d7ff130 100644
@@ -2202,18 +2202,23 @@ config ARM64_BTI
          BTI, such binaries can still run, but you get no additional
          enforcement of branch destinations.

arch/arm64/Kconfig config ARM64_BTI · line 2213

Quoted 10 lines
arch/arm64/Kconfig +9 -0
 
+config LD_HAS_BTI_STUBS
+       # A linker may need to insert a veneer when a call to a function with
+       # static linkage in the same compilation unit but in a different
+       # section is out of range for a direct branch instruction. Such a
+       # veneer performs an indirect call, and thus requires a landing pad
+       # under BTI, which the static function may lack. Only recent linkers
+       # are guaranteed to emit those landing pads correctly in all cases.
+       def_bool (LD_IS_BFD && LD_VERSION >= 24200) || \
+                (LD_IS_LLD && LLD_VERSION >= 200000)
Quoted 8 lines

A bit of a rock fetching quest (sorry), but it would make it easier for reviewers to verify these version checks if you had git trailers linking to commits (or bug reports) for these linkers with the relevant fix. I don't think that necessitates a v2, if the maintainers you're expecting to pick up this series are kind enough to manually re-add them (unless b4 can pick up Link: trailers like that).

Fair point.