2019-02-08 15:08:56

by Torsten Duwe

[permalink] [raw]
Subject: [PATCH v8 0/5] 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



2019-02-08 15:10:31

by Torsten Duwe

[permalink] [raw]
Subject: [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.

Signed-off-by: Torsten Duwe <[email protected]>

---
arch/arm64/kernel/Makefile | 6 +++---
arch/arm64/lib/Makefile | 2 +-
2 files changed, 4 insertions(+), 4 deletions(-)
--- a/arch/arm64/kernel/Makefile
+++ b/arch/arm64/kernel/Makefile
@@ -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 \
--- a/arch/arm64/lib/Makefile
+++ b/arch/arm64/lib/Makefile
@@ -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

2019-02-08 15:10:49

by Torsten Duwe

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

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.

Signed-off-by: Torsten Duwe <[email protected]>

---
mm/kasan/Makefile | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
--- a/mm/kasan/Makefile
+++ b/mm/kasan/Makefile
@@ -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


2019-02-08 15:11:21

by Torsten Duwe

[permalink] [raw]
Subject: [PATCH v8 5/5] arm64: use -fpatchable-function-entry if available

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.

Signed-off-by: Torsten Duwe <[email protected]>

---
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(-)
--- a/arch/arm64/Kconfig
+++ b/arch/arm64/Kconfig
@@ -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
--- a/arch/arm64/Makefile
+++ b/arch/arm64/Makefile
@@ -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

--- a/include/asm-generic/vmlinux.lds.h
+++ b/include/asm-generic/vmlinux.lds.h
@@ -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()
--- a/include/linux/compiler_types.h
+++ b/include/linux/compiler_types.h
@@ -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
--- a/kernel/module.c
+++ b/kernel/module.c
@@ -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

2019-02-08 15:11:30

by Torsten Duwe

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

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.

Signed-off-by: Torsten Duwe <[email protected]>

---
drivers/firmware/efi/libstub/Makefile | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
--- a/drivers/firmware/efi/libstub/Makefile
+++ b/drivers/firmware/efi/libstub/Makefile
@@ -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)


2019-02-08 15:12:11

by Torsten Duwe

[permalink] [raw]
Subject: [PATCH v8 4/5] arm64: implement ftrace with regs

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.

Signed-off-by: Torsten Duwe <[email protected]>

---
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(-)
--- a/arch/arm64/include/asm/ftrace.h
+++ b/arch/arm64/include/asm/ftrace.h
@@ -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.
*/
--- a/arch/arm64/kernel/entry-ftrace.S
+++ b/arch/arm64/kernel/entry-ftrace.S
@@ -10,6 +10,7 @@
*/

#include <linux/linkage.h>
+#include <asm/asm-offsets.h>
#include <asm/assembler.h>
#include <asm/ftrace.h>
#include <asm/insn.h>
@@ -124,6 +125,7 @@ EXPORT_SYMBOL(_mcount)
NOKPROBE(_mcount)

#else /* CONFIG_DYNAMIC_FTRACE */
+#ifndef CONFIG_DYNAMIC_FTRACE_WITH_REGS
/*
* _mcount() is used to build the kernel with -pg option, but all the branch
* instructions to _mcount() are replaced to NOP initially at kernel start up,
@@ -163,11 +165,6 @@ GLOBAL(ftrace_graph_call) // ftrace_gra

mcount_exit
ENDPROC(ftrace_caller)
-#endif /* CONFIG_DYNAMIC_FTRACE */
-
-ENTRY(ftrace_stub)
- ret
-ENDPROC(ftrace_stub)

#ifdef CONFIG_FUNCTION_GRAPH_TRACER
/*
@@ -187,7 +184,125 @@ ENTRY(ftrace_graph_caller)

mcount_exit
ENDPROC(ftrace_graph_caller)
+#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
+
+#else /* CONFIG_DYNAMIC_FTRACE_WITH_REGS */
+
+ .macro ftrace_regs_entry, allregs=0
+ /* make room for pt_regs, plus a callee frame */
+ sub sp, sp, #(S_FRAME_SIZE + 16)
+
+ /* save function arguments */
+ stp x0, x1, [sp, #S_X0]
+ stp x2, x3, [sp, #S_X2]
+ stp x4, x5, [sp, #S_X4]
+ stp x6, x7, [sp, #S_X6]
+ stp x8, x9, [sp, #S_X8]

+ .if \allregs == 1
+ stp x10, x11, [sp, #S_X10]
+ stp x12, x13, [sp, #S_X12]
+ stp x14, x15, [sp, #S_X14]
+ stp x16, x17, [sp, #S_X16]
+ stp x18, x19, [sp, #S_X18]
+ stp x20, x21, [sp, #S_X20]
+ stp x22, x23, [sp, #S_X22]
+ stp x24, x25, [sp, #S_X24]
+ stp x26, x27, [sp, #S_X26]
+ .endif
+
+ /* Save fp and x28, which is used in this function. */
+ stp x28, x29, [sp, #S_X28]
+
+ /* The stack pointer as it was on ftrace_caller entry... */
+ add x28, sp, #(S_FRAME_SIZE + 16)
+ /* ...and the link Register at callee entry */
+ stp x9, x28, [sp, #S_LR] /* to pt_regs.r[30] and .sp */
+
+ /* The program counter just after the ftrace call site */
+ str lr, [sp, #S_PC]
+
+ /* Now fill in callee's preliminary stackframe. */
+ stp x29, x9, [sp, #S_FRAME_SIZE]
+ /* Let FP point to it. */
+ add x29, sp, #S_FRAME_SIZE
+
+ /* Our stackframe, stored inside pt_regs. */
+ stp x29, x30, [sp, #S_STACKFRAME]
+ add x29, sp, #S_STACKFRAME
+ .endm
+
+ENTRY(ftrace_regs_caller)
+ ftrace_regs_entry 1
+ b ftrace_common
+ENDPROC(ftrace_regs_caller)
+
+ENTRY(ftrace_caller)
+ ftrace_regs_entry 0
+ b ftrace_common
+ENDPROC(ftrace_caller)
+
+ENTRY(ftrace_common)
+
+ mov x3, sp /* pt_regs are @sp */
+ ldr_l x2, function_trace_op, x0
+ mov x1, x9 /* parent IP */
+ sub x0, lr, #AARCH64_INSN_SIZE
+
+GLOBAL(ftrace_call)
+ bl ftrace_stub
+
+#ifdef CONFIG_FUNCTION_GRAPH_TRACER
+GLOBAL(ftrace_graph_call) // ftrace_graph_caller();
+ nop // If enabled, this will be replaced
+ // "b ftrace_graph_caller"
+#endif
+
+/*
+ * GCC's patchable-function-entry implicitly disables IPA-RA,
+ * so all non-argument registers are either scratch / dead
+ * or callee-saved (within the ftrace framework). Function
+ * arguments of the call we are intercepting right now however
+ * need to be preserved in any case.
+ */
+ftrace_common_return:
+ /* restore function args */
+ ldp x0, x1, [sp]
+ ldp x2, x3, [sp, #S_X2]
+ ldp x4, x5, [sp, #S_X4]
+ ldp x6, x7, [sp, #S_X6]
+ ldr x8, [sp, #S_X8]
+
+ /* restore fp and x28 */
+ ldp x28, x29, [sp, #S_X28]
+
+ ldr lr, [sp, #S_LR]
+ ldr x9, [sp, #S_PC]
+ /* clean up both frames, ours and callee preliminary */
+ add sp, sp, #S_FRAME_SIZE + 16
+
+ ret x9
+ENDPROC(ftrace_common)
+
+#ifdef CONFIG_FUNCTION_GRAPH_TRACER
+ENTRY(ftrace_graph_caller)
+ ldr x0, [sp, #S_PC] /* pc */
+ sub x0, x0, #AARCH64_INSN_SIZE
+ add x1, sp, #S_LR /* &lr */
+ ldr x2, [sp, #S_FRAME_SIZE] /* fp */
+ bl prepare_ftrace_return
+ b ftrace_common_return
+ENDPROC(ftrace_graph_caller)
+#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
+#endif /* CONFIG_DYNAMIC_FTRACE_WITH_REGS */
+#endif /* CONFIG_DYNAMIC_FTRACE */
+
+ENTRY(ftrace_stub)
+ ret
+ENDPROC(ftrace_stub)
+
+
+#ifdef CONFIG_FUNCTION_GRAPH_TRACER
/*
* void return_to_handler(void)
*
--- a/arch/arm64/kernel/ftrace.c
+++ b/arch/arm64/kernel/ftrace.c
@@ -65,6 +65,40 @@ int ftrace_update_ftrace_func(ftrace_fun
return ftrace_modify_code(pc, 0, new, false);
}

+#ifdef CONFIG_ARM64_MODULE_PLTS
+static int install_ftrace_trampoline(struct module *mod, unsigned long *addr)
+{
+ struct plt_entry trampoline, *mod_trampoline;
+
+ /*
+ * Iterate over
+ * mod->arch.ftrace_trampolines[MOD_ARCH_NR_FTRACE_TRAMPOLINES]
+ * The assignment to various ftrace functions happens here.
+ */
+ if (*addr == FTRACE_ADDR)
+ mod_trampoline = &mod->arch.ftrace_trampolines[0];
+ else if (*addr == FTRACE_REGS_ADDR)
+ mod_trampoline = &mod->arch.ftrace_trampolines[1];
+ else
+ return -EINVAL;
+
+ trampoline = get_plt_entry(*addr, mod_trampoline);
+
+ if (!plt_entries_equal(mod_trampoline, &trampoline)) {
+ /* point the trampoline at our ftrace entry point */
+ module_disable_ro(mod);
+ *mod_trampoline = trampoline;
+ module_enable_ro(mod, true);
+
+ /* update trampoline before patching in the branch */
+ smp_wmb();
+ }
+ *addr = (unsigned long)(void *)mod_trampoline;
+
+ return 0;
+}
+#endif
+
/*
* Turn on the call to ftrace_caller() in instrumented function
*/
@@ -76,8 +110,8 @@ int ftrace_make_call(struct dyn_ftrace *

if (offset < -SZ_128M || offset >= SZ_128M) {
#ifdef CONFIG_ARM64_MODULE_PLTS
- struct plt_entry trampoline;
struct module *mod;
+ int ret;

/*
* On kernels that support module PLTs, the offset between the
@@ -96,32 +130,14 @@ int ftrace_make_call(struct dyn_ftrace *
if (WARN_ON(!mod))
return -EINVAL;

- /*
- * There is only one ftrace trampoline per module. For now,
- * this is not a problem since on arm64, all dynamic ftrace
- * invocations are routed via ftrace_caller(). This will need
- * to be revisited if support for multiple ftrace entry points
- * is added in the future, but for now, the pr_err() below
- * deals with a theoretical issue only.
- */
- trampoline = get_plt_entry(addr, mod->arch.ftrace_trampoline);
- if (!plt_entries_equal(mod->arch.ftrace_trampoline,
- &trampoline)) {
- if (!plt_entries_equal(mod->arch.ftrace_trampoline,
- &(struct plt_entry){})) {
- pr_err("ftrace: far branches to multiple entry points unsupported inside a single module\n");
- return -EINVAL;
- }
-
- /* point the trampoline to our ftrace entry point */
- module_disable_ro(mod);
- *mod->arch.ftrace_trampoline = trampoline;
- module_enable_ro(mod, true);
+ /* Check against our well-known list of ftrace entry points */
+ if (addr == FTRACE_ADDR || addr == FTRACE_REGS_ADDR) {
+ ret = install_ftrace_trampoline(mod, &addr);
+ if (ret < 0)
+ return ret;
+ } else
+ return -EINVAL;

- /* update trampoline before patching in the branch */
- smp_wmb();
- }
- addr = (unsigned long)(void *)mod->arch.ftrace_trampoline;
#else /* CONFIG_ARM64_MODULE_PLTS */
return -EINVAL;
#endif /* CONFIG_ARM64_MODULE_PLTS */
@@ -133,6 +149,45 @@ int ftrace_make_call(struct dyn_ftrace *
return ftrace_modify_code(pc, old, new, true);
}

+#ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
+int ftrace_modify_call(struct dyn_ftrace *rec, unsigned long old_addr,
+ unsigned long addr)
+{
+ unsigned long pc = rec->ip;
+ u32 old, new;
+
+ old = aarch64_insn_gen_branch_imm(pc, old_addr,
+ AARCH64_INSN_BRANCH_LINK);
+ new = aarch64_insn_gen_branch_imm(pc, addr, AARCH64_INSN_BRANCH_LINK);
+
+ return ftrace_modify_code(pc, old, new, true);
+}
+
+/*
+ * Ftrace with regs generates the tracer calls as close as possible to
+ * the function entry; no stack frame has been set up at that point.
+ * In order to make another call e.g to ftrace_caller, the LR must be
+ * saved from being overwritten.
+ * Between two functions, and with IPA-RA turned off, the scratch registers
+ * are available, so move the LR to x9 before calling into ftrace.
+ *
+ * This function is called once during kernel startup for each call site.
+ * The address passed is that of the actual branch, so patch in the LR saver
+ * just before that.
+ */
+static int ftrace_setup_lr_saver(unsigned long addr)
+{
+ u32 old, new;
+
+ old = aarch64_insn_gen_nop();
+ /* "mov x9, lr" is officially aliased from "orr x9, xzr, lr". */
+ new = aarch64_insn_gen_logical_shifted_reg(AARCH64_INSN_REG_9,
+ AARCH64_INSN_REG_ZR, AARCH64_INSN_REG_LR, 0,
+ AARCH64_INSN_VARIANT_64BIT, AARCH64_INSN_LOGIC_ORR);
+ return ftrace_modify_code(addr - AARCH64_INSN_SIZE, old, new, true);
+}
+#endif
+
/*
* Turn off the call to ftrace_caller() in instrumented function
*/
@@ -144,6 +199,16 @@ int ftrace_make_nop(struct module *mod,
u32 old = 0, new;
long offset = (long)pc - (long)addr;

+ /*
+ * -fpatchable-function-entry= does not generate a profiling call
+ * initially; the NOPs are already there. So instead,
+ * put the LR saver there ahead of time, in order to avoid
+ * any race condition over patching 2 instructions.
+ */
+ if (IS_ENABLED(CONFIG_DYNAMIC_FTRACE_WITH_REGS) &&
+ addr == MCOUNT_ADDR)
+ return ftrace_setup_lr_saver(pc);
+
if (offset < -SZ_128M || offset >= SZ_128M) {
#ifdef CONFIG_ARM64_MODULE_PLTS
u32 replaced;
--- a/arch/arm64/include/asm/module.h
+++ b/arch/arm64/include/asm/module.h
@@ -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

--- a/arch/arm64/kernel/module.c
+++ b/arch/arm64/kernel/module.c
@@ -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
}

--- a/arch/arm64/kernel/module-plts.c
+++ b/arch/arm64/kernel/module-plts.c
@@ -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;

2019-02-11 11:54:30

by Mark Rutland

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

On Fri, Feb 08, 2019 at 04:10:07PM +0100, Torsten Duwe wrote:
> 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.
>
> Signed-off-by: Torsten Duwe <[email protected]>

Thanks for splitting this out.

With the indentation removed from the commit text:

Reviewed-by: Mark Rutland <[email protected]>

Thanks,
Mark.

>
> ---
> arch/arm64/kernel/Makefile | 6 +++---
> arch/arm64/lib/Makefile | 2 +-
> 2 files changed, 4 insertions(+), 4 deletions(-)
> --- a/arch/arm64/kernel/Makefile
> +++ b/arch/arm64/kernel/Makefile
> @@ -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 \
> --- a/arch/arm64/lib/Makefile
> +++ b/arch/arm64/lib/Makefile
> @@ -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

2019-02-11 11:56:28

by Mark Rutland

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

On Fri, Feb 08, 2019 at 04:10:14PM +0100, Torsten Duwe wrote:
> 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.
>
> Signed-off-by: Torsten Duwe <[email protected]>

With the indentation removed from the commit text:

Reviewed-by: Mark Rutland <[email protected]>

Mark.

>
> ---
> mm/kasan/Makefile | 4 ++--
> 1 file changed, 2 insertions(+), 2 deletions(-)
> --- a/mm/kasan/Makefile
> +++ b/mm/kasan/Makefile
> @@ -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
>

2019-02-11 11:59:22

by Mark Rutland

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

On Fri, Feb 08, 2019 at 04:10:14PM +0100, Torsten Duwe wrote:
> 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.
>
> Signed-off-by: Torsten Duwe <[email protected]>

With the indentation removed from the commit text:

Reviewed-by: Mark Rutland <[email protected]>

Mark.

>
> ---
> mm/kasan/Makefile | 4 ++--
> 1 file changed, 2 insertions(+), 2 deletions(-)
> --- a/mm/kasan/Makefile
> +++ b/mm/kasan/Makefile
> @@ -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
>

2019-02-11 12:02:56

by Mark Rutland

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

On Fri, Feb 08, 2019 at 04:10:11PM +0100, Torsten Duwe wrote:
> 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.
>
> Signed-off-by: Torsten Duwe <[email protected]>

With the indentation removed from the commit text:

Reviewed-by: Mark Rutland <[email protected]>

Mark.

>
> ---
> drivers/firmware/efi/libstub/Makefile | 6 +++---
> 1 file changed, 3 insertions(+), 3 deletions(-)
> --- a/drivers/firmware/efi/libstub/Makefile
> +++ b/drivers/firmware/efi/libstub/Makefile
> @@ -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)
>

2019-02-13 13:46:55

by Julien Thierry

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

Hi Torsten,

On 08/02/2019 15:10, Torsten Duwe wrote:
> 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.
>
> Signed-off-by: Torsten Duwe <[email protected]>
>
> ---
> 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(-)

[...]

> --- a/arch/arm64/include/asm/module.h
> +++ b/arch/arm64/include/asm/module.h
> @@ -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:

Reviewed-by: Julien Thierry <[email protected]>

Cheers,

--
Julien Thierry

2019-02-13 13:48:53

by Julien Thierry

[permalink] [raw]
Subject: Re: [PATCH v8 0/5] arm64: ftrace with regs

Hi Torsten,

On 08/02/2019 15:08, Torsten Duwe wrote:
> Patch series v8, as discussed.
> The whole series applies cleanly on 5.0-rc5
>

For this series:

Tested-by: Julien Thierry <[email protected]>

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

--
Julien Thierry

2019-03-11 11:51:28

by Torsten Duwe

[permalink] [raw]
Subject: Re: [PATCH v8 0/5] arm64: ftrace with regs

On Wed, Feb 13, 2019 at 11:11:04AM +0000, Julien Thierry wrote:
> Hi Torsten,
>
> On 08/02/2019 15:08, Torsten Duwe wrote:
> > 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


2019-03-11 12:18:46

by Mark Rutland

[permalink] [raw]
Subject: Re: [PATCH v8 0/5] arm64: ftrace with regs

Hi Torsten,

On Mon, Mar 11, 2019 at 12:49:46PM +0100, Torsten Duwe wrote:
> On Wed, Feb 13, 2019 at 11:11:04AM +0000, Julien Thierry wrote:
> > Hi Torsten,
> >
> > On 08/02/2019 15:08, Torsten Duwe wrote:
> > > 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.

2019-03-29 10:19:55

by Torsten Duwe

[permalink] [raw]
Subject: Re: [PATCH v8 0/5] arm64: ftrace with regs

On Mon, Mar 11, 2019 at 12:18:05PM +0000, Mark Rutland wrote:
> Hi Torsten,
>
> On Mon, Mar 11, 2019 at 12:49:46PM +0100, Torsten Duwe wrote:
> > On Wed, Feb 13, 2019 at 11:11:04AM +0000, Julien Thierry wrote:
> > > Hi Torsten,
> > >
> > > On 08/02/2019 15:08, Torsten Duwe wrote:
> > > > 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.

Ping?

> 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

2019-04-08 15:28:22

by Mark Rutland

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

On Fri, Feb 08, 2019 at 04:10:14PM +0100, Torsten Duwe wrote:
> 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.
>
> Signed-off-by: Torsten Duwe <[email protected]>

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.

>
> ---
> mm/kasan/Makefile | 4 ++--
> 1 file changed, 2 insertions(+), 2 deletions(-)
> --- a/mm/kasan/Makefile
> +++ b/mm/kasan/Makefile
> @@ -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
>

2019-04-08 17:29:56

by Mark Rutland

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

On Fri, Feb 08, 2019 at 04:10:11PM +0100, Torsten Duwe wrote:
> 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.
>
> Signed-off-by: Torsten Duwe <[email protected]>

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

Assuming so, can we have your ack?

Thanks,
Mark.

>
> ---
> drivers/firmware/efi/libstub/Makefile | 6 +++---
> 1 file changed, 3 insertions(+), 3 deletions(-)
> --- a/drivers/firmware/efi/libstub/Makefile
> +++ b/drivers/firmware/efi/libstub/Makefile
> @@ -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)
>

2019-04-08 17:52:36

by Andrey Ryabinin

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



On 2/8/19 6:10 PM, Torsten Duwe wrote:
> 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.
>
> Signed-off-by: Torsten Duwe <[email protected]>
>

Acked-by: Andrey Ryabinin <[email protected]>

2019-04-08 17:52:41

by Mark Rutland

[permalink] [raw]
Subject: Re: [PATCH v8 0/5] arm64: ftrace with regs

On Mon, Mar 11, 2019 at 12:49:46PM +0100, Torsten Duwe wrote:
> On Wed, Feb 13, 2019 at 11:11:04AM +0000, Julien Thierry wrote:
> > Hi Torsten,
> >
> > On 08/02/2019 15:08, Torsten Duwe wrote:
> > > 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.

2019-04-08 21:30:13

by Ard Biesheuvel

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

On Mon, 8 Apr 2019 at 17:23, Mark Rutland <[email protected]> wrote:
>
> On Fri, Feb 08, 2019 at 04:10:11PM +0100, Torsten Duwe wrote:
> > 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.
> >
> > Signed-off-by: Torsten Duwe <[email protected]>
>
> Ard, would you be happy if we were to take this via the arm64 tree?
>
> Assuming so, can we have your ack?
>

Sure

Acked-by: Ard Biesheuvel <[email protected]>

2019-04-09 17:53:36

by Will Deacon

[permalink] [raw]
Subject: Re: [PATCH v8 0/5] arm64: ftrace with regs

On Mon, Apr 08, 2019 at 04:36:28PM +0100, Mark Rutland wrote:
> On Mon, Mar 11, 2019 at 12:49:46PM +0100, Torsten Duwe wrote:
> > On Wed, Feb 13, 2019 at 11:11:04AM +0000, Julien Thierry wrote:
> > > Hi Torsten,
> > >
> > > On 08/02/2019 15:08, Torsten Duwe wrote:
> > > > 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.

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

Will

2019-07-10 12:55:26

by Ruslan Bilovol

[permalink] [raw]
Subject: Re: [PATCH v8 0/5] arm64: ftrace with regs

On Tue, Apr 9, 2019 at 8:52 PM Will Deacon <[email protected]> wrote:
>
> On Mon, Apr 08, 2019 at 04:36:28PM +0100, Mark Rutland wrote:
> > On Mon, Mar 11, 2019 at 12:49:46PM +0100, Torsten Duwe wrote:
> > > On Wed, Feb 13, 2019 at 11:11:04AM +0000, Julien Thierry wrote:
> > > > Hi Torsten,
> > > >
> > > > On 08/02/2019 15:08, Torsten Duwe wrote:
> > > > > 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.
>
> 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

2019-07-24 17:00:34

by Mark Rutland

[permalink] [raw]
Subject: Re: [PATCH v8 0/5] arm64: ftrace with regs

Hi Ruslan,

On Wed, Jul 10, 2019 at 03:27:58PM +0300, Ruslan Bilovol wrote:
> On Tue, Apr 9, 2019 at 8:52 PM Will Deacon <[email protected]> wrote:
> >
> > On Mon, Apr 08, 2019 at 04:36:28PM +0100, Mark Rutland wrote:
> > > On Mon, Mar 11, 2019 at 12:49:46PM +0100, Torsten Duwe wrote:
> > > > On Wed, Feb 13, 2019 at 11:11:04AM +0000, Julien Thierry wrote:
> > > > > Hi Torsten,
> > > > >
> > > > > On 08/02/2019 15:08, Torsten Duwe wrote:
> > > > > > 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.
> >
> > 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.

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

2019-10-16 15:05:26

by Jiri Kosina

[permalink] [raw]
Subject: Re: [PATCH v8 0/5] arm64: ftrace with regs

On Wed, 24 Jul 2019, Mark Rutland wrote:

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

Hi Mark,

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

Thanks,

--
Jiri Kosina
SUSE Labs

2019-10-17 12:52:04

by Mark Rutland

[permalink] [raw]
Subject: Re: [PATCH v8 0/5] arm64: ftrace with regs

On Wed, Oct 16, 2019 at 01:42:59PM +0200, Jiri Kosina wrote:
> On Wed, 24 Jul 2019, Mark Rutland wrote:
>
> > > > > > 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.
> > > >
> > > > 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.
> >
> > > 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. :)
>
> Hi Mark,

Hi Jiri,

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

2019-10-19 08:59:15

by Mark Rutland

[permalink] [raw]
Subject: Re: [PATCH v8 0/5] arm64: ftrace with regs

On Wed, Oct 16, 2019 at 06:58:42PM +0100, Mark Rutland wrote:
> 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.

2019-10-19 11:05:08

by Torsten Duwe

[permalink] [raw]
Subject: Re: [PATCH v8 0/5] arm64: ftrace with regs

Hi Mark!

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.

> 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 :)

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

> 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

2019-10-21 11:38:55

by Mark Rutland

[permalink] [raw]
Subject: Re: [PATCH v8 0/5] arm64: ftrace with regs

On Sat, Oct 19, 2019 at 01:01:35PM +0200, Torsten Duwe wrote:
> Hi Mark!

Hi Torsten!

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

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.

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

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

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

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

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

2019-10-21 13:21:51

by Josh Poimboeuf

[permalink] [raw]
Subject: Re: [PATCH v8 0/5] arm64: ftrace with regs

On Sat, Oct 19, 2019 at 01:01:35PM +0200, Torsten Duwe wrote:
> 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?

--
Josh