Skip to contentSkip to conversation

arm64: ftrace with regs

PATCH In review Torsten Duwe 9 participants 29 messages Feb 8, 2019 – Oct 21, 2019

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

v8 cover arm64: ftrace with regs

Patch series v8, as discussed.
The whole series applies cleanly on 5.0-rc5

---
 arch/arm64/Kconfig                    |    4 +
 arch/arm64/Makefile                   |   10 ++
 arch/arm64/include/asm/ftrace.h       |   16 ++++
 arch/arm64/include/asm/module.h       |    3 
 arch/arm64/kernel/Makefile            |   12 +--
 arch/arm64/kernel/entry-ftrace.S      |  125 ++++++++++++++++++++++++++++++++--
 arch/arm64/kernel/ftrace.c            |  117 ++++++++++++++++++++++++-------
 arch/arm64/kernel/module-plts.c       |    3 
 arch/arm64/kernel/module.c            |    2 
 arch/arm64/lib/Makefile               |    4 -
 drivers/firmware/efi/libstub/Makefile |   12 +--
 include/asm-generic/vmlinux.lds.h     |    2 
 include/linux/compiler_types.h        |    4 +
 kernel/module.c                       |   14 +++
 mm/kasan/Makefile                     |    8 +-
 15 files changed, 281 insertions(+), 55 deletions(-)

---
changes since v7:

* -pg -> $(CC_FLAGS_FTRACE) cleanup now split according to subtree maintainership.

* REC_IP_BRANCH_OFFSET is gone, the functionality went into ftrace_call_adjust(), where it belongs.

* MOV_X9_X30 macro is gone (why did we argue about its name anyway?); it is only used once now in the initial ftrace_make_nop new helper function ftrace_setup_lr_saver(), suggested by Julien.

* call site processing was missing for modules. Fixed.

changes since v6:

* change the stack layout once more; I hope I have it the "standard" way now. And yes, it looks simpler and cleaner; thanks, Mark, for nagging.

* split out the independent Kconfig and Makefile changes

* fixed style issues

* s/fp/x29/g

* MCOUNT_ADDR is now merely a 64-bit magic, as this is totally sufficient.

* QUICK_LR_SAVE renamed back to MOV_X9_X30.

* place MOV_X9_X30 insns on bootup, and only flip b <-> nop at runtime

* graph tracer "ifdeffery" reshuffle

Torsten

#1 lore raw

PATCH v8 1/5 arm64: replace -pg with CC_FLAGS_FTRACE in arm64 Makefiles

In preparation for arm64 supporting ftrace built on other compiler options, let's have the arm64 makefiles remove the $(CC_FLAGS_FTRACE) flags, whatever these may be, rather than assuming '-pg'.

There should be no functional change as a result of this patch.

---
 arch/arm64/kernel/Makefile |    6 +++---
 arch/arm64/lib/Makefile    |    2 +-
 2 files changed, 4 insertions(+), 4 deletions(-)
arch/arm64/kernel/Makefile +3 -3
@@ -7,9 +7,9 @@ CPPFLAGS_vmlinux.lds	:= -DTEXT_OFFSET=$(
 AFLAGS_head.o		:= -DTEXT_OFFSET=$(TEXT_OFFSET)
 CFLAGS_armv8_deprecated.o := -I$(src)
 
-CFLAGS_REMOVE_ftrace.o = -pg
-CFLAGS_REMOVE_insn.o = -pg
-CFLAGS_REMOVE_return_address.o = -pg
+CFLAGS_REMOVE_ftrace.o = $(CC_FLAGS_FTRACE)
+CFLAGS_REMOVE_insn.o = $(CC_FLAGS_FTRACE)
+CFLAGS_REMOVE_return_address.o = $(CC_FLAGS_FTRACE)
 
 # Object file lists.
 obj-y			:= debug-monitors.o entry.o irq.o fpsimd.o		\
arch/arm64/lib/Makefile +1 -1
@@ -24,7 +24,7 @@ CFLAGS_atomic_ll_sc.o	:= -ffixed-x1 -ffi
 		   -fcall-saved-x10 -fcall-saved-x11 -fcall-saved-x12	\
 		   -fcall-saved-x13 -fcall-saved-x14 -fcall-saved-x15	\
 		   -fcall-saved-x18 -fomit-frame-pointer
-CFLAGS_REMOVE_atomic_ll_sc.o := -pg
+CFLAGS_REMOVE_atomic_ll_sc.o := $(CC_FLAGS_FTRACE)
 GCOV_PROFILE_atomic_ll_sc.o	:= n
 KASAN_SANITIZE_atomic_ll_sc.o	:= n
 KCOV_INSTRUMENT_atomic_ll_sc.o	:= n
reviewer Reviewed-by on v8 1/5
#2 lore raw

Re: [PATCH v8 1/5] arm64: replace -pg with CC_FLAGS_FTRACE in arm64 Makefiles

Quoted 7 lines · Torsten Duwe

In preparation for arm64 supporting ftrace built on other compiler options, let's have the arm64 makefiles remove the $(CC_FLAGS_FTRACE) flags, whatever these may be, rather than assuming '-pg'.

There should be no functional change as a result of this patch.

Thanks for splitting this out.

With the indentation removed from the commit text:

Thanks,
Mark.

Quoted 31 lines
---
 arch/arm64/kernel/Makefile |    6 +++---
 arch/arm64/lib/Makefile    |    2 +-
 2 files changed, 4 insertions(+), 4 deletions(-)
arch/arm64/kernel/Makefile +3 -3
@@ -7,9 +7,9 @@ CPPFLAGS_vmlinux.lds	:= -DTEXT_OFFSET=$(
 AFLAGS_head.o		:= -DTEXT_OFFSET=$(TEXT_OFFSET)
 CFLAGS_armv8_deprecated.o := -I$(src)
 
-CFLAGS_REMOVE_ftrace.o = -pg
-CFLAGS_REMOVE_insn.o = -pg
-CFLAGS_REMOVE_return_address.o = -pg
+CFLAGS_REMOVE_ftrace.o = $(CC_FLAGS_FTRACE)
+CFLAGS_REMOVE_insn.o = $(CC_FLAGS_FTRACE)
+CFLAGS_REMOVE_return_address.o = $(CC_FLAGS_FTRACE)
 
 # Object file lists.
 obj-y			:= debug-monitors.o entry.o irq.o fpsimd.o		\
arch/arm64/lib/Makefile +1 -1
@@ -24,7 +24,7 @@ CFLAGS_atomic_ll_sc.o	:= -ffixed-x1 -ffi
 		   -fcall-saved-x10 -fcall-saved-x11 -fcall-saved-x12	\
 		   -fcall-saved-x13 -fcall-saved-x14 -fcall-saved-x15	\
 		   -fcall-saved-x18 -fomit-frame-pointer
-CFLAGS_REMOVE_atomic_ll_sc.o := -pg
+CFLAGS_REMOVE_atomic_ll_sc.o := $(CC_FLAGS_FTRACE)
 GCOV_PROFILE_atomic_ll_sc.o	:= n
 KASAN_SANITIZE_atomic_ll_sc.o	:= n
 KCOV_INSTRUMENT_atomic_ll_sc.o	:= n
#3 lore raw

PATCH v8 2/5 arm64: replace -pg with CC_FLAGS_FTRACE in efi Makefiles

In reply to Torsten Duwe

In preparation for arm64 supporting ftrace built on other compiler options, let's have makefiles remove the $(CC_FLAGS_FTRACE) flags, whatever these may be, rather than assuming '-pg'. While at it, fix arm32 as well.

There should be no functional change as a result of this patch.

---
 drivers/firmware/efi/libstub/Makefile |    6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/firmware/efi/libstub/Makefile +3 -3
@@ -16,9 +16,9 @@ cflags-$(CONFIG_X86)		+= -m$(BITS) -D__K
 
 # arm64 uses the full KBUILD_CFLAGS so it's necessary to explicitly
 # disable the stackleak plugin
-cflags-$(CONFIG_ARM64)		:= $(subst -pg,,$(KBUILD_CFLAGS)) -fpie \
-				   $(DISABLE_STACKLEAK_PLUGIN)
-cflags-$(CONFIG_ARM)		:= $(subst -pg,,$(KBUILD_CFLAGS)) \
+cflags-$(CONFIG_ARM64)		:= $(subst $(CC_FLAGS_FTRACE),,$(KBUILD_CFLAGS)) \
+				   -fpie $(DISABLE_STACKLEAK_PLUGIN)
+cflags-$(CONFIG_ARM)		:= $(subst $(CC_FLAGS_FTRACE),,$(KBUILD_CFLAGS)) \
 				   -fno-builtin -fpic \
 				   $(call cc-option,-mno-single-pic-base)
 
reviewer Reviewed-by on v8 2/5
#4 lore raw

Re: [PATCH v8 2/5] arm64: replace -pg with CC_FLAGS_FTRACE in efi Makefiles

Quoted 8 lines · Torsten Duwe

In preparation for arm64 supporting ftrace built on other compiler options, let's have makefiles remove the $(CC_FLAGS_FTRACE) flags, whatever these may be, rather than assuming '-pg'. While at it, fix arm32 as well.

There should be no functional change as a result of this patch.

With the indentation removed from the commit text:

Mark.

Quoted 19 lines
---
 drivers/firmware/efi/libstub/Makefile |    6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/firmware/efi/libstub/Makefile +3 -3
@@ -16,9 +16,9 @@ cflags-$(CONFIG_X86)		+= -m$(BITS) -D__K
 
 # arm64 uses the full KBUILD_CFLAGS so it's necessary to explicitly
 # disable the stackleak plugin
-cflags-$(CONFIG_ARM64)		:= $(subst -pg,,$(KBUILD_CFLAGS)) -fpie \
-				   $(DISABLE_STACKLEAK_PLUGIN)
-cflags-$(CONFIG_ARM)		:= $(subst -pg,,$(KBUILD_CFLAGS)) \
+cflags-$(CONFIG_ARM64)		:= $(subst $(CC_FLAGS_FTRACE),,$(KBUILD_CFLAGS)) \
+				   -fpie $(DISABLE_STACKLEAK_PLUGIN)
+cflags-$(CONFIG_ARM)		:= $(subst $(CC_FLAGS_FTRACE),,$(KBUILD_CFLAGS)) \
 				   -fno-builtin -fpic \
 				   $(call cc-option,-mno-single-pic-base)
 
reviewer
#5 lore raw

Re: [PATCH v8 2/5] arm64: replace -pg with CC_FLAGS_FTRACE in efi Makefiles

In reply to Torsten Duwe
Quoted 8 lines · Torsten Duwe

In preparation for arm64 supporting ftrace built on other compiler options, let's have makefiles remove the $(CC_FLAGS_FTRACE) flags, whatever these may be, rather than assuming '-pg'. While at it, fix arm32 as well.

There should be no functional change as a result of this patch.

Ard, would you be happy if we were to take this via the arm64 tree?

Assuming so, can we have your ack?

Thanks,
Mark.

Quoted 19 lines
---
 drivers/firmware/efi/libstub/Makefile |    6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/firmware/efi/libstub/Makefile +3 -3
@@ -16,9 +16,9 @@ cflags-$(CONFIG_X86)		+= -m$(BITS) -D__K
 
 # arm64 uses the full KBUILD_CFLAGS so it's necessary to explicitly
 # disable the stackleak plugin
-cflags-$(CONFIG_ARM64)		:= $(subst -pg,,$(KBUILD_CFLAGS)) -fpie \
-				   $(DISABLE_STACKLEAK_PLUGIN)
-cflags-$(CONFIG_ARM)		:= $(subst -pg,,$(KBUILD_CFLAGS)) \
+cflags-$(CONFIG_ARM64)		:= $(subst $(CC_FLAGS_FTRACE),,$(KBUILD_CFLAGS)) \
+				   -fpie $(DISABLE_STACKLEAK_PLUGIN)
+cflags-$(CONFIG_ARM)		:= $(subst $(CC_FLAGS_FTRACE),,$(KBUILD_CFLAGS)) \
 				   -fno-builtin -fpic \
 				   $(call cc-option,-mno-single-pic-base)
 
#6 lore raw

Re: [PATCH v8 2/5] arm64: replace -pg with CC_FLAGS_FTRACE in efi Makefiles

Quoted 7 lines · Mark Rutland
Quoted 5 lines · Torsten Duwe

In preparation for arm64 supporting ftrace built on other compiler options, let's have makefiles remove the $(CC_FLAGS_FTRACE) flags, whatever these may be, rather than assuming '-pg'. While at it, fix arm32 as well.

Quoted 8 lines
Quoted 3 lines

There should be no functional change as a result of this patch.

Ard, would you be happy if we were to take this via the arm64 tree?

Assuming so, can we have your ack?

Sure

#7 lore raw

PATCH v8 3/5 arm64: replace -pg with CC_FLAGS_FTRACE in mm/kasan Makefile

In reply to Torsten Duwe

In preparation for arm64 supporting ftrace built on other compiler options, let's have makefiles remove the $(CC_FLAGS_FTRACE) flags, whatever these may be, rather than assuming '-pg'.

There should be no functional change as a result of this patch.

---
 mm/kasan/Makefile |    4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
mm/kasan/Makefile +2 -2
@@ -5,8 +5,8 @@ UBSAN_SANITIZE_generic.o := n
 UBSAN_SANITIZE_tags.o := n
 KCOV_INSTRUMENT := n
 
-CFLAGS_REMOVE_common.o = -pg
-CFLAGS_REMOVE_generic.o = -pg
+CFLAGS_REMOVE_common.o = $(CC_FLAGS_FTRACE)
+CFLAGS_REMOVE_generic.o = $(CC_FLAGS_FTRACE)
 # Function splitter causes unnecessary splits in __asan_load1/__asan_store1
 # see: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=63533
 
reviewer Reviewed-by on v8 3/5
#8 lore raw

Re: [PATCH v8 3/5] arm64: replace -pg with CC_FLAGS_FTRACE in mm/kasan Makefile

Quoted 7 lines · Torsten Duwe

In preparation for arm64 supporting ftrace built on other compiler options, let's have makefiles remove the $(CC_FLAGS_FTRACE) flags, whatever these may be, rather than assuming '-pg'.

There should be no functional change as a result of this patch.

With the indentation removed from the commit text:

Mark.

Quoted 17 lines
---
 mm/kasan/Makefile |    4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
mm/kasan/Makefile +2 -2
@@ -5,8 +5,8 @@ UBSAN_SANITIZE_generic.o := n
 UBSAN_SANITIZE_tags.o := n
 KCOV_INSTRUMENT := n
 
-CFLAGS_REMOVE_common.o = -pg
-CFLAGS_REMOVE_generic.o = -pg
+CFLAGS_REMOVE_common.o = $(CC_FLAGS_FTRACE)
+CFLAGS_REMOVE_generic.o = $(CC_FLAGS_FTRACE)
 # Function splitter causes unnecessary splits in __asan_load1/__asan_store1
 # see: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=63533
 
reviewer Reviewed-by on v8 3/5
#9 lore raw

Re: [PATCH v8 3/5] arm64: replace -pg with CC_FLAGS_FTRACE in mm/kasan Makefile

In reply to Torsten Duwe
Quoted 7 lines · Torsten Duwe

In preparation for arm64 supporting ftrace built on other compiler options, let's have makefiles remove the $(CC_FLAGS_FTRACE) flags, whatever these may be, rather than assuming '-pg'.

There should be no functional change as a result of this patch.

With the indentation removed from the commit text:

Mark.

Quoted 17 lines
---
 mm/kasan/Makefile |    4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
mm/kasan/Makefile +2 -2
@@ -5,8 +5,8 @@ UBSAN_SANITIZE_generic.o := n
 UBSAN_SANITIZE_tags.o := n
 KCOV_INSTRUMENT := n
 
-CFLAGS_REMOVE_common.o = -pg
-CFLAGS_REMOVE_generic.o = -pg
+CFLAGS_REMOVE_common.o = $(CC_FLAGS_FTRACE)
+CFLAGS_REMOVE_generic.o = $(CC_FLAGS_FTRACE)
 # Function splitter causes unnecessary splits in __asan_load1/__asan_store1
 # see: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=63533
 
reviewer
#10 lore raw

Re: [PATCH v8 3/5] arm64: replace -pg with CC_FLAGS_FTRACE in mm/kasan Makefile

In reply to Torsten Duwe
Quoted 7 lines · Torsten Duwe

In preparation for arm64 supporting ftrace built on other compiler options, let's have makefiles remove the $(CC_FLAGS_FTRACE) flags, whatever these may be, rather than assuming '-pg'.

There should be no functional change as a result of this patch.

Andrey, would you be happy if we were to take this via the arm64 tree?

Assuming so, could we please have your ack?

Thanks,
Mark.

Quoted 17 lines
---
 mm/kasan/Makefile |    4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
mm/kasan/Makefile +2 -2
@@ -5,8 +5,8 @@ UBSAN_SANITIZE_generic.o := n
 UBSAN_SANITIZE_tags.o := n
 KCOV_INSTRUMENT := n
 
-CFLAGS_REMOVE_common.o = -pg
-CFLAGS_REMOVE_generic.o = -pg
+CFLAGS_REMOVE_common.o = $(CC_FLAGS_FTRACE)
+CFLAGS_REMOVE_generic.o = $(CC_FLAGS_FTRACE)
 # Function splitter causes unnecessary splits in __asan_load1/__asan_store1
 # see: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=63533
 
Acked-by on v8 3/5
#11 lore raw

Re: [PATCH v8 3/5] arm64: replace -pg with CC_FLAGS_FTRACE in mm/kasan Makefile

In reply to Torsten Duwe
Quoted 8 lines · Torsten Duwe

In preparation for arm64 supporting ftrace built on other compiler options, let's have makefiles remove the $(CC_FLAGS_FTRACE) flags, whatever these may be, rather than assuming '-pg'.

There should be no functional change as a result of this patch.

#12 lore raw

PATCH v8 4/5 arm64: implement ftrace with regs

In reply to Torsten Duwe

Implement ftrace with regs, based on the new gcc flag -fpatchable-function-entry (=2)

Now that gcc8 added 2 NOPs at the beginning of each function, replace the first NOP thus generated with a quick LR saver (move it to scratch reg x9), so the 2nd replacement insn, the call to ftrace, does not clobber the value. Ftrace will then generate the standard stack frames.

Note that patchable-function-entry in GCC disables IPA-RA, which means ABI register calling conventions are obeyed and scratch registers such as x9 are available.

Introduce and handle an ftrace_regs_trampoline for module PLTs, right after ftrace_trampoline in an ftrace_trampolines[2] array, and double the size of the corresponding special section.

---
 arch/arm64/include/asm/ftrace.h  |   16 ++++
 arch/arm64/include/asm/module.h  |    3 
 arch/arm64/kernel/entry-ftrace.S |  125 +++++++++++++++++++++++++++++++++++++--
 arch/arm64/kernel/ftrace.c       |  117 ++++++++++++++++++++++++++++--------
 arch/arm64/kernel/module-plts.c  |    3 
 arch/arm64/kernel/module.c       |    2 
 6 files changed, 231 insertions(+), 35 deletions(-)
arch/arm64/include/asm/ftrace.h +15 -1
@@ -14,9 +14,16 @@
 #include <asm/insn.h>
 
 #define HAVE_FUNCTION_GRAPH_FP_TEST
-#define MCOUNT_ADDR		((unsigned long)_mcount)
 #define MCOUNT_INSN_SIZE	AARCH64_INSN_SIZE
 
+#ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
+#define ARCH_SUPPORTS_FTRACE_OPS 1
+/* All we need is some magic value. Simply use "_mCount:" */
+#define MCOUNT_ADDR		(0x5f6d436f756e743a)
+#else
+#define MCOUNT_ADDR		((unsigned long)_mcount)
+#endif
+
 #ifndef __ASSEMBLY__
 #include <linux/compat.h>
 
@@ -34,6 +41,13 @@ extern void return_to_handler(void);
 static inline unsigned long ftrace_call_adjust(unsigned long addr)
 {
 	/*
+	 * For -fpatchable-function-entry=2, there's first the
+	 * LR saver, and only then the actual call insn.
+	 * Advance addr accordingly.
+	 */
+	if (IS_ENABLED(CONFIG_DYNAMIC_FTRACE_WITH_REGS))
+		return (addr + AARCH64_INSN_SIZE);
+	/*
 	 * addr is the address of the mcount call instruction.
 	 * recordmcount does the necessary offset calculation.
 	 */
arch/arm64/kernel/entry-ftrace.S +120 -5

150 lines not on this page · raw message

arch/arm64/kernel/ftrace.c +91 -26

149 lines not on this page · raw message

arch/arm64/include/asm/module.h +2 -1
@@ -32,7 +32,8 @@ struct mod_arch_specific {
 	struct mod_plt_sec	init;
 
 	/* for CONFIG_DYNAMIC_FTRACE */
-	struct plt_entry 	*ftrace_trampoline;
+	struct plt_entry	*ftrace_trampolines;
+#define MOD_ARCH_NR_FTRACE_TRAMPOLINES	2
 };
 #endif
 
arch/arm64/kernel/module.c +1 -1
@@ -452,7 +452,7 @@ int module_finalize(const Elf_Ehdr *hdr,
 #ifdef CONFIG_ARM64_MODULE_PLTS
 		if (IS_ENABLED(CONFIG_DYNAMIC_FTRACE) &&
 		    !strcmp(".text.ftrace_trampoline", secstrs + s->sh_name))
-			me->arch.ftrace_trampoline = (void *)s->sh_addr;
+			me->arch.ftrace_trampolines = (void *)s->sh_addr;
 #endif
 	}
 
arch/arm64/kernel/module-plts.c +2 -1
@@ -333,7 +333,8 @@ int module_frob_arch_sections(Elf_Ehdr *
 		tramp->sh_type = SHT_NOBITS;
 		tramp->sh_flags = SHF_EXECINSTR | SHF_ALLOC;
 		tramp->sh_addralign = __alignof__(struct plt_entry);
-		tramp->sh_size = sizeof(struct plt_entry);
+		tramp->sh_size = MOD_ARCH_NR_FTRACE_TRAMPOLINES
+				 * sizeof(struct plt_entry);
 	}
 
 	return 0;
Reviewed-by on v8 4/5
#13 lore raw

Re: [PATCH v8 4/5] arm64: implement ftrace with regs

Hi Torsten,

Quoted 27 lines · Torsten Duwe

Implement ftrace with regs, based on the new gcc flag -fpatchable-function-entry (=2)

Now that gcc8 added 2 NOPs at the beginning of each function, replace the first NOP thus generated with a quick LR saver (move it to scratch reg x9), so the 2nd replacement insn, the call to ftrace, does not clobber the value. Ftrace will then generate the standard stack frames.

Note that patchable-function-entry in GCC disables IPA-RA, which means ABI register calling conventions are obeyed and scratch registers such as x9 are available.

Introduce and handle an ftrace_regs_trampoline for module PLTs, right after ftrace_trampoline in an ftrace_trampolines[2] array, and double the size of the corresponding special section.

---
 arch/arm64/include/asm/ftrace.h  |   16 ++++
 arch/arm64/include/asm/module.h  |    3 
 arch/arm64/kernel/entry-ftrace.S |  125 +++++++++++++++++++++++++++++++++++++--
 arch/arm64/kernel/ftrace.c       |  117 ++++++++++++++++++++++++++++--------
 arch/arm64/kernel/module-plts.c  |    3 
 arch/arm64/kernel/module.c       |    2 
 6 files changed, 231 insertions(+), 35 deletions(-)

[...]

arch/arm64/include/asm/module.h struct mod_arch_specific { · line 36

Quoted 9 lines
arch/arm64/include/asm/module.h +2 -1
@@ -32,7 +32,8 @@ struct mod_arch_specific {
 	struct mod_plt_sec	init;
 
 	/* for CONFIG_DYNAMIC_FTRACE */
-	struct plt_entry 	*ftrace_trampoline;
+	struct plt_entry	*ftrace_trampolines;
+#define MOD_ARCH_NR_FTRACE_TRAMPOLINES	2

Nit: I'd define this outside of the struct, just below MODULE_ARCH_VERMAGIC, and use the prefix "MODULE_ARCH" instead of "MOD_ARCH" as this seem to be already in use for a few other defines.

Otherwise things look good to me:

Cheers,

Signature · 1 line
Julien Thierry
#14 lore raw

PATCH v8 5/5 arm64: use -fpatchable-function-entry if available

In reply to Torsten Duwe

Test whether gcc supports -fpatchable-function-entry and use it to promote DYNAMIC_FTRACE to DYNAMIC_FTRACE_WITH_REGS. Amend support for the new object section that holds the locations (__patchable_function_entries) and define a proper "notrace" attribute to switch it off.

---
 arch/arm64/Kconfig                |    2 ++
 arch/arm64/Makefile               |    5 +++++
 include/asm-generic/vmlinux.lds.h |    1 +
 include/linux/compiler_types.h    |    2 ++
 kernel/module.c                   |    7 ++++++-
 5 files changed, 16 insertions(+), 1 deletion(-)
arch/arm64/Kconfig +2 -0
@@ -132,6 +132,8 @@ config ARM64
 	select HAVE_DEBUG_KMEMLEAK
 	select HAVE_DMA_CONTIGUOUS
 	select HAVE_DYNAMIC_FTRACE
+	select HAVE_DYNAMIC_FTRACE_WITH_REGS \
+		if $(cc-option,-fpatchable-function-entry=2)
 	select HAVE_EFFICIENT_UNALIGNED_ACCESS
 	select HAVE_FTRACE_MCOUNT_RECORD
 	select HAVE_FUNCTION_TRACER
arch/arm64/Makefile +5 -0
@@ -89,6 +89,11 @@ ifeq ($(CONFIG_ARM64_MODULE_PLTS),y)
 KBUILD_LDFLAGS_MODULE	+= -T $(srctree)/arch/arm64/kernel/module.lds
 endif
 
+ifeq ($(CONFIG_DYNAMIC_FTRACE_WITH_REGS),y)
+  KBUILD_CPPFLAGS += -DCC_USING_PATCHABLE_FENTRY
+  CC_FLAGS_FTRACE := -fpatchable-function-entry=2
+endif
+
 # Default value
 head-y		:= arch/arm64/kernel/head.o
 
include/asm-generic/vmlinux.lds.h +1 -0
@@ -113,6 +113,7 @@
 #define MCOUNT_REC()	. = ALIGN(8);				\
 			__start_mcount_loc = .;			\
 			KEEP(*(__mcount_loc))			\
+			KEEP(*(__patchable_function_entries))	\
 			__stop_mcount_loc = .;
 #else
 #define MCOUNT_REC()
include/linux/compiler_types.h +2 -0
@@ -112,6 +112,8 @@ struct ftrace_likely_data {
 
 #if defined(CC_USING_HOTPATCH)
 #define notrace			__attribute__((hotpatch(0, 0)))
+#elif defined(CC_USING_PATCHABLE_FENTRY)
+#define notrace			__attribute__((patchable_function_entry(0)))
 #else
 #define notrace			__attribute__((__no_instrument_function__))
 #endif
kernel/module.c +6 -1
@@ -3122,7 +3122,12 @@ static int find_module_sections(struct m
 #endif
 #ifdef CONFIG_FTRACE_MCOUNT_RECORD
 	/* sechdrs[0].sh_size is always zero */
-	mod->ftrace_callsites = section_objs(info, "__mcount_loc",
+	mod->ftrace_callsites = section_objs(info,
+#ifdef CC_USING_PATCHABLE_FENTRY
+					     "__patchable_function_entries",
+#else
+					     "__mcount_loc",
+#endif
 					     sizeof(*mod->ftrace_callsites),
 					     &mod->num_ftrace_callsites);
 #endif
Tested-by on the v8 series
#15 lore raw
In reply to Torsten Duwe

Hi Torsten,

Quoted 3 lines · Torsten Duwe

Patch series v8, as discussed.
The whole series applies cleanly on 5.0-rc5

For this series:

Quoted 54 lines
---
 arch/arm64/Kconfig                    |    4 +
 arch/arm64/Makefile                   |   10 ++
 arch/arm64/include/asm/ftrace.h       |   16 ++++
 arch/arm64/include/asm/module.h       |    3 
 arch/arm64/kernel/Makefile            |   12 +--
 arch/arm64/kernel/entry-ftrace.S      |  125 ++++++++++++++++++++++++++++++++--
 arch/arm64/kernel/ftrace.c            |  117 ++++++++++++++++++++++++-------
 arch/arm64/kernel/module-plts.c       |    3 
 arch/arm64/kernel/module.c            |    2 
 arch/arm64/lib/Makefile               |    4 -
 drivers/firmware/efi/libstub/Makefile |   12 +--
 include/asm-generic/vmlinux.lds.h     |    2 
 include/linux/compiler_types.h        |    4 +
 kernel/module.c                       |   14 +++
 mm/kasan/Makefile                     |    8 +-
 15 files changed, 281 insertions(+), 55 deletions(-)

---
changes since v7:

* -pg -> $(CC_FLAGS_FTRACE) cleanup now split according to subtree maintainership.

* REC_IP_BRANCH_OFFSET is gone, the functionality went into ftrace_call_adjust(), where it belongs.

* MOV_X9_X30 macro is gone (why did we argue about its name anyway?); it is only used once now in the initial ftrace_make_nop new helper function ftrace_setup_lr_saver(), suggested by Julien.

* call site processing was missing for modules. Fixed.

changes since v6:

* change the stack layout once more; I hope I have it the "standard" way now. And yes, it looks simpler and cleaner; thanks, Mark, for nagging.

* split out the independent Kconfig and Makefile changes

* fixed style issues

* s/fp/x29/g

* MCOUNT_ADDR is now merely a 64-bit magic, as this is totally sufficient.

* QUICK_LR_SAVE renamed back to MOV_X9_X30.

* place MOV_X9_X30 insns on bootup, and only flip b <-> nop at runtime

* graph tracer "ifdeffery" reshuffle

Torsten

Signature · 1 line
Julien Thierry
#16 lore raw
Quoted 5 lines · Julien Thierry

Hi Torsten,

Quoted 2 lines · Torsten Duwe

Patch series v8, as discussed.
The whole series applies cleanly on 5.0-rc5

So what's the status now? Besides debatable minor style issues there were no more objections to v8. Would this go through the ARM repo or via the ftrace repo?

Torsten

reviewer
#17 lore raw

Hi Torsten,

Quoted 10 lines · Torsten Duwe
Quoted 5 lines · Julien Thierry

Hi Torsten,

Quoted 2 lines · Torsten Duwe

Patch series v8, as discussed.
The whole series applies cleanly on 5.0-rc5

So what's the status now? Besides debatable minor style issues there were no more objections to v8. Would this go through the ARM repo or via the ftrace repo?

Sorry, I have some half-written review comments that I will clean up and send shortly.

As commented on prior versions, I'd very much like to see the MCOUNT_ADDR hack go, by teaching the core ftrace code to not assume that an mcount symbol exists.

We should be able to do that by separating the notion of NOPing a patch site from the notion of initializing it for the first time.

Thanks,
Mark.

#18 lore raw
Quoted 10 lines · Mark Rutland

Hi Torsten,

Quoted 7 lines · Torsten Duwe
Quoted 5 lines · Julien Thierry

Hi Torsten,

Quoted 2 lines · Torsten Duwe

Patch series v8, as discussed.
The whole series applies cleanly on 5.0-rc5

Quoted 6 lines
Quoted 3 lines

So what's the status now? Besides debatable minor style issues there were no more objections to v8. Would this go through the ARM repo or via the ftrace repo?

Sorry, I have some half-written review comments that I will clean up and send shortly.

Ping?

Quoted 6 lines

As commented on prior versions, I'd very much like to see the MCOUNT_ADDR hack go, by teaching the core ftrace code to not assume that an mcount symbol exists.

We should be able to do that by separating the notion of NOPing a patch site from the notion of initializing it for the first time.

This is generally a good idea, and would affect other architectures as well, see arch/s390/kernel/ftrace.c ftrace_make_nop(...)

I propose to do this in a second round.

Torsten

reviewer
#19 lore raw
In reply to Torsten Duwe
Quoted 10 lines · Torsten Duwe
Quoted 5 lines · Julien Thierry

Hi Torsten,

Quoted 2 lines · Torsten Duwe

Patch series v8, as discussed.
The whole series applies cleanly on 5.0-rc5

So what's the status now? Besides debatable minor style issues there were no more objections to v8. Would this go through the ARM repo or via the ftrace repo?

Sorry agains for the delay on this. I'm now back in the office and in front of a computer daily, so I can spend a bit more time on this.

Regardless of anything else, I think that we should queue the first three patches now. I've poked the relevant maintainers for their acks so that those can be taken via the arm64 tree.

I'm happy to do the trivial cleanups on the last couple of patches (e.g. s/lr/x30), and I'm actively looking at the API rework I requested.

Thanks,
Mark.

#20 lore raw
Quoted 15 lines · Mark Rutland
Quoted 10 lines · Torsten Duwe
Quoted 5 lines · Julien Thierry

Hi Torsten,

Quoted 2 lines · Torsten Duwe

Patch series v8, as discussed.
The whole series applies cleanly on 5.0-rc5

So what's the status now? Besides debatable minor style issues there were no more objections to v8. Would this go through the ARM repo or via the ftrace repo?

Sorry agains for the delay on this. I'm now back in the office and in front of a computer daily, so I can spend a bit more time on this.

Quoted 6 lines

Regardless of anything else, I think that we should queue the first three patches now. I've poked the relevant maintainers for their acks so that those can be taken via the arm64 tree.

I'm happy to do the trivial cleanups on the last couple of patches (e.g. s/lr/x30), and I'm actively looking at the API rework I requested.

Ok, I've picked up patches 1-3 and I'll wait for you to spin updates to the last two.

Will

#21 lore raw
Quoted 21 lines · Will Deacon
Quoted 19 lines · Mark Rutland
Quoted 10 lines · Torsten Duwe
Quoted 5 lines · Julien Thierry

Hi Torsten,

Quoted 2 lines · Torsten Duwe

Patch series v8, as discussed.
The whole series applies cleanly on 5.0-rc5

So what's the status now? Besides debatable minor style issues there were no more objections to v8. Would this go through the ARM repo or via the ftrace repo?

Sorry agains for the delay on this. I'm now back in the office and in front of a computer daily, so I can spend a bit more time on this.

Regardless of anything else, I think that we should queue the first three patches now. I've poked the relevant maintainers for their acks so that those can be taken via the arm64 tree.

Quoted 5 lines
Quoted 2 lines

I'm happy to do the trivial cleanups on the last couple of patches (e.g. s/lr/x30), and I'm actively looking at the API rework I requested.

Ok, I've picked up patches 1-3 and I'll wait for you to spin updates to the last two.

Ok, I see that patches 1-3 are picked up and are already present in recent kernels.

Is there any progress on remaining two patches?
Any help required?

Thanks,
Ruslan

reviewer
#22 lore raw

Hi Ruslan,

Quoted 25 lines · Ruslan Bilovol
Quoted 24 lines · Will Deacon
Quoted 21 lines · Mark Rutland
Quoted 10 lines · Torsten Duwe
Quoted 5 lines · Julien Thierry

Hi Torsten,

Quoted 2 lines · Torsten Duwe

Patch series v8, as discussed.
The whole series applies cleanly on 5.0-rc5

So what's the status now? Besides debatable minor style issues there were no more objections to v8. Would this go through the ARM repo or via the ftrace repo?

Sorry agains for the delay on this. I'm now back in the office and in front of a computer daily, so I can spend a bit more time on this.

Regardless of anything else, I think that we should queue the first three patches now. I've poked the relevant maintainers for their acks so that those can be taken via the arm64 tree.

I'm happy to do the trivial cleanups on the last couple of patches (e.g. s/lr/x30), and I'm actively looking at the API rework I requested.

Quoted 7 lines
Quoted 2 lines

Ok, I've picked up patches 1-3 and I'll wait for you to spin updates to the last two.

Ok, I see that patches 1-3 are picked up and are already present in recent kernels.

Is there any progress on remaining two patches?

I'm afraid that I've been distracted on other fronts, so I haven't made progress there.

Quoted 1 line

Any help required?

If you'd be happy to look at the cleanup I previously suggested for the core, that would be great. When I last looked, it was simple to rework things so that arch code doesn't have to define MCOUNT_ADDR, but I hadn't figured out exactly how to split the core mcount assumptions from the important state machine bits.

I'll take another look and see if I can provide more detail. :)

Thanks,
Mark.

#23 lore raw
Quoted 27 lines · Mark Rutland
Quoted 14 lines
Quoted 14 lines
Quoted 13 lines
Quoted 3 lines

So what's the status now? Besides debatable minor style issues there were no more objections to v8. Would this go through the ARM repo or via the ftrace repo?

Sorry agains for the delay on this. I'm now back in the office and in front of a computer daily, so I can spend a bit more time on this.

Regardless of anything else, I think that we should queue the first three patches now. I've poked the relevant maintainers for their acks so that those can be taken via the arm64 tree.

I'm happy to do the trivial cleanups on the last couple of patches (e.g. s/lr/x30), and I'm actively looking at the API rework I requested.

Quoted 7 lines
Quoted 2 lines

Ok, I've picked up patches 1-3 and I'll wait for you to spin updates to the last two.

Ok, I see that patches 1-3 are picked up and are already present in recent kernels.

Is there any progress on remaining two patches?

I'm afraid that I've been distracted on other fronts, so I haven't made progress there.

Quoted 1 line

Any help required?

Quoted 7 lines

If you'd be happy to look at the cleanup I previously suggested for the core, that would be great. When I last looked, it was simple to rework things so that arch code doesn't have to define MCOUNT_ADDR, but I hadn't figured out exactly how to split the core mcount assumptions from the important state machine bits.

I'll take another look and see if I can provide more detail. :)

Hi Mark,

has any progress been made on any front? Feels like this got stuck a bit.

Thanks,

Signature · 2 lines
Jiri Kosina
SUSE Labs
reviewer
#24 lore raw
Quoted 35 lines · Jiri Kosina
Quoted 33 lines · Mark Rutland
Quoted 14 lines
Quoted 14 lines
Quoted 13 lines
Quoted 3 lines

So what's the status now? Besides debatable minor style issues there were no more objections to v8. Would this go through the ARM repo or via the ftrace repo?

Sorry agains for the delay on this. I'm now back in the office and in front of a computer daily, so I can spend a bit more time on this.

Regardless of anything else, I think that we should queue the first three patches now. I've poked the relevant maintainers for their acks so that those can be taken via the arm64 tree.

I'm happy to do the trivial cleanups on the last couple of patches (e.g. s/lr/x30), and I'm actively looking at the API rework I requested.

Quoted 7 lines
Quoted 2 lines

Ok, I've picked up patches 1-3 and I'll wait for you to spin updates to the last two.

Ok, I see that patches 1-3 are picked up and are already present in recent kernels.

Is there any progress on remaining two patches?

I'm afraid that I've been distracted on other fronts, so I haven't made progress there.

Quoted 1 line

Any help required?

If you'd be happy to look at the cleanup I previously suggested for the core, that would be great. When I last looked, it was simple to rework things so that arch code doesn't have to define MCOUNT_ADDR, but I hadn't figured out exactly how to split the core mcount assumptions from the important state machine bits.

Quoted 3 lines
Quoted 1 line

I'll take another look and see if I can provide more detail. :)

Hi Mark,

Hi Jiri,

Quoted 1 line

has any progress been made on any front? Feels like this got stuck a bit.

Sorry about this; I've been a bit distracted.

I've just done the core (non-arm64) bits today, and pushed that out:

https://git.kernel.org/pub/scm/linux/kernel/git/mark/linux.git/log/?h=arm64/ftrace-with-regs

... I'll fold the remainging bits of patches 4 and 5 together tomorrow atop of that.

Thanks,
Mark.

reviewer
#25 lore raw
Quoted 6 lines · Mark Rutland

I've just done the core (non-arm64) bits today, and pushed that out:

https://git.kernel.org/pub/scm/linux/kernel/git/mark/linux.git/log/?h=arm64/ftrace-with-regs

... I'll fold the remainging bits of patches 4 and 5 together tomorrow atop of that.

I've just force-pushed an updated version with the actual arm64 FTRACE_WITH_REGS bits. There are a couple of bits I still need to verify, but I'm hoping that I can send this out for real next week.

In the process of reworking this I spotted some issues that will get in the way of livepatching. Notably:

* When modules can be loaded far away from the kernel, we'll potentially need a PLT for each function within a module, if each can be patched to a unique function. Currently we have a fixed number, which is only sufficient for the two ftrace entry trampolines.

IIUC, the new code being patched in is itself a module, in which case we'd need a PLT for each function in the main kernel image.

We have a few options here, e.g. changing which memory size model we use, or reserving space for a PLT before each function using -f patchable-function-entry=N,M.

* There are windows where backtracing will miss the callsite's caller, as its address is not live in the LR or existing chain of frame records. Thus we cannot claim to have a reliable stacktrace.

I suspect we'll have to teach the stacktrace code to handle this as a special-case.

I'll try to write these up, as similar probably applies to other architectures with a link register.

Thanks,
Mark.

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

#26 lore raw

Hi Mark!

Quoted 10 lines · On Fri, 18 Oct 2019 18:41:02 +0100 Mark Rutland <[email protected]> wrote:

In the process of reworking this I spotted some issues that will get in the way of livepatching. Notably:

* When modules can be loaded far away from the kernel, we'll potentially need a PLT for each function within a module, if each can be patched to a unique function. Currently we have a fixed number, which is only sufficient for the two ftrace entry trampolines.

IIUC, the new code being patched in is itself a module, in which
case we'd need a PLT for each function in the main kernel image.

When no live patching is involved, obviously all cases need to have been handled so far. And when a live patching module comes in, there are calls in and out of the new patch code:

Calls going into the live patch are not aware of this. They are caught by an active ftrace intercept, and the actual call into the LP module is done in klp_arch_set_pc, by manipulating the intercept (call site) return address (in case thread lives in the "new world", for completeness' sake). This is an unsigned long write in C.

All calls going _out_ from the KLP module are newly generated, as part of the KLP module building process, and are thus aware of them being "extern" -- a PLT entry should be generated and accounted for in the KLP module.

Quoted 3 lines

We have a few options here, e.g. changing which memory size model we use, or reserving space for a PLT before each function using -f patchable-function-entry=N,M.

Nonetheless I'm happy I once added the ,M option here. You never know :)

Quoted 6 lines

* There are windows where backtracing will miss the callsite's caller, as its address is not live in the LR or existing chain of frame records. Thus we cannot claim to have a reliable stacktrace.

I suspect we'll have to teach the stacktrace code to handle this as
a special-case.

Yes, that's where I had to step back. The unwinder needs to stop where the chain is even questionable. In _all_ cases. Missing only one race condition means a lurking inconsistency.

OTOH it's not a problem to report "not reliable" when in doubt; the thread in question will then get woken up and unwind itself. It is only an optimisation to let all kernel threads which are guaranteed to not contain any patched functions sleep on.

Quoted 2 lines

I'll try to write these up, as similar probably applies to other architectures with a link register.

I thought I'd quickly give you my feedback upfront here.

Torsten

reviewer
#27 lore raw
Quoted 1 line · Torsten Duwe

Hi Mark!

Hi Torsten!

Quoted 18 lines
Quoted 10 lines · On Fri, 18 Oct 2019 18:41:02 +0100 Mark Rutland <[email protected]> wrote:

In the process of reworking this I spotted some issues that will get in the way of livepatching. Notably:

* When modules can be loaded far away from the kernel, we'll potentially need a PLT for each function within a module, if each can be patched to a unique function. Currently we have a fixed number, which is only sufficient for the two ftrace entry trampolines.

IIUC, the new code being patched in is itself a module, in which
case we'd need a PLT for each function in the main kernel image.

When no live patching is involved, obviously all cases need to have been handled so far. And when a live patching module comes in, there are calls in and out of the new patch code:

Quoted 5 lines

Calls going into the live patch are not aware of this. They are caught by an active ftrace intercept, and the actual call into the LP module is done in klp_arch_set_pc, by manipulating the intercept (call site) return address (in case thread lives in the "new world", for completeness' sake). This is an unsigned long write in C.

I was under the impression that (at some point) the patch site would be patched to call the LP code directly. From the above I understand that's not the case, and it will always be directed via the regular ftrace entry code -- have I got that right?

Assuming that is the case, that sounds fine to me, and sorry for the noise.

Quoted 4 lines

All calls going _out_ from the KLP module are newly generated, as part of the KLP module building process, and are thus aware of them being "extern" -- a PLT entry should be generated and accounted for in the KLP module.

Yup; understood.

Quoted 5 lines
Quoted 3 lines

We have a few options here, e.g. changing which memory size model we use, or reserving space for a PLT before each function using -f patchable-function-entry=N,M.

Nonetheless I'm happy I once added the ,M option here. You never know :)

Yup; we may have other reasons to need this in future (and I see parisc uses this today).

Quoted 10 lines
Quoted 6 lines

* There are windows where backtracing will miss the callsite's caller, as its address is not live in the LR or existing chain of frame records. Thus we cannot claim to have a reliable stacktrace.

I suspect we'll have to teach the stacktrace code to handle this as
a special-case.

Yes, that's where I had to step back. The unwinder needs to stop where the chain is even questionable. In _all_ cases. Missing only one race condition means a lurking inconsistency.

Sure. I'm calling this out now so that we don't miss this in future. I've added comments to the ftrace entry asm to this effect for now.

Quoted 4 lines

OTOH it's not a problem to report "not reliable" when in doubt; the thread in question will then get woken up and unwind itself. It is only an optimisation to let all kernel threads which are guaranteed to not contain any patched functions sleep on.

I just want to make it clear that some care will be needed if/when adding CONFIG_HAVE_RELIABLE_STACKTRACE so that we handle this case correctly.

Quoted 4 lines
Quoted 2 lines

I'll try to write these up, as similar probably applies to other architectures with a link register.

I thought I'd quickly give you my feedback upfront here.

Thanks; it's much appreciated!

Mark.

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

#28 lore raw
In reply to Torsten Duwe
Quoted 4 lines · Torsten Duwe

All calls going _out_ from the KLP module are newly generated, as part of the KLP module building process, and are thus aware of them being "extern" -- a PLT entry should be generated and accounted for in the KLP module.

Hm... for kpatch-build I assume we may need a GCC plugin to convert local calls to global somehow?

Signature · 1 line
Josh