Add dynamic ftrace support for RISC-V platforms
v2 cover Add dynamic ftrace support for RISC-V platforms
This patch set includes the building blocks of dynamic ftrace features for RISC-V machines.
Changes in v2:
- Fix the return value as writing to kernel text goes wrong (2/6)
- Replace manual comparisons by calling memcmp (2/6)
- Simplify the conditional assignment in the Makefile (1/6)
Alan Kao (6):
riscv/ftrace: Add RECORD_MCOUNT support riscv/ftrace: Add dynamic function tracer support riscv/ftrace: Add dynamic function graph tracer support riscv/ftrace: Add ARCH_SUPPORTS_FTRACE_OPS support riscv/ftrace: Add DYNAMIC_FTRACE_WITH_REGS support riscv/ftrace: Add HAVE_FUNCTION_GRAPH_RET_ADDR_PTR support
arch/riscv/Kconfig | 3 + arch/riscv/Makefile | 3 + arch/riscv/include/asm/ftrace.h | 47 ++++++++ arch/riscv/kernel/Makefile | 5 +- arch/riscv/kernel/ftrace.c | 142 ++++++++++++++++++++++- arch/riscv/kernel/mcount-dyn.S | 244 ++++++++++++++++++++++++++++++++++++++++ arch/riscv/kernel/mcount.S | 22 ++-- arch/riscv/kernel/stacktrace.c | 6 + scripts/recordmcount.pl | 5 + 9 files changed, 464 insertions(+), 13 deletions(-) create mode 100644 arch/riscv/kernel/mcount-dyn.S
Signature · 1 line
2.15.1
PATCH v2 1/6 riscv/ftrace: Add RECORD_MCOUNT support
Now recordmcount.pl recognizes RISC-V object files. For the mechanism to work, we have to disable the linker relaxation.
--- arch/riscv/Kconfig | 1 + arch/riscv/Makefile | 3 +++ scripts/recordmcount.pl | 5 +++++ 3 files changed, 9 insertions(+)
arch/riscv/Kconfig +1 -0
@@ -117,6 +117,7 @@ config ARCH_RV64I select 64BIT select HAVE_FUNCTION_TRACER select HAVE_FUNCTION_GRAPH_TRACER + select HAVE_FTRACE_MCOUNT_RECORD endchoice
arch/riscv/Makefile +3 -0
@@ -11,6 +11,9 @@ LDFLAGS := OBJCOPYFLAGS := -O binary LDFLAGS_vmlinux := +ifeq ($(CONFIG_DYNAMIC_FTRACE),y) + LDFLAGS_vmlinux := --no-relax +endif KBUILD_AFLAGS_MODULE += -fPIC KBUILD_CFLAGS_MODULE += -fPIC
scripts/recordmcount.pl +5 -0
@@ -376,6 +376,11 @@ if ($arch eq "x86_64") { $mcount_regex = "^\\s*([0-9a-fA-F]+):.*\\s__mcount\$"; $type = ".quad"; $alignment = 8; +} elsif ($arch eq "riscv") { + $function_regex = "^([0-9a-fA-F]+)\\s+<([^.0-9][0-9a-zA-Z_\\.]+)>:"; + $mcount_regex = "^\\s*([0-9a-fA-F]+):\\sR_RISCV_CALL\\s_mcount\$"; + $type = ".quad"; + $alignment = 2; } else { die "Arch $arch is not supported with CONFIG_FTRACE_MCOUNT_RECORD"; }
Signature · 1 line
2.15.1
PATCH v2 2/6 riscv/ftrace: Add dynamic function tracer support
In reply to Alan KaoWe now have dynamic ftrace with the following added items:
* ftrace_make_call, ftrace_make_nop (in kernel/ftrace.c) The two functions turns any recorded call site of filtered functions into a call to ftrace_caller or nops
* ftracce_update_ftrace_func (in kernel/ftrace.c)
turns the nops at ftrace_call into a call to a generic entry for function tracers.
* ftrace_caller (in kernel/mcount-dyn.S)
The entry where each _mcount call sites calls to once they are filtered to be traced.
Also, this patch fixes the semantic problems in mcount.S, which will be treated as only a reference implementation once we have the dynamic ftrace.
--- arch/riscv/Kconfig | 1 + arch/riscv/include/asm/ftrace.h | 45 ++++++++++++++++++ arch/riscv/kernel/Makefile | 5 +- arch/riscv/kernel/ftrace.c | 100 +++++++++++++++++++++++++++++++++++++++- arch/riscv/kernel/mcount-dyn.S | 52 +++++++++++++++++++++ arch/riscv/kernel/mcount.S | 22 +++++---- 6 files changed, 213 insertions(+), 12 deletions(-) create mode 100644 arch/riscv/kernel/mcount-dyn.S
arch/riscv/Kconfig +1 -0
@@ -118,6 +118,7 @@ config ARCH_RV64I select HAVE_FUNCTION_TRACER select HAVE_FUNCTION_GRAPH_TRACER select HAVE_FTRACE_MCOUNT_RECORD + select HAVE_DYNAMIC_FTRACE endchoice
arch/riscv/include/asm/ftrace.h +45 -0
48 lines not on this page · raw message
arch/riscv/kernel/Makefile +3 -2
@@ -34,7 +34,8 @@ CFLAGS_setup.o := -mcmodel=medany obj-$(CONFIG_SMP) += smpboot.o obj-$(CONFIG_SMP) += smp.o obj-$(CONFIG_MODULES) += module.o -obj-$(CONFIG_FUNCTION_TRACER) += mcount.o -obj-$(CONFIG_FUNCTION_GRAPH_TRACER) += ftrace.o + +obj-$(CONFIG_FUNCTION_TRACER) += mcount.o ftrace.o +obj-$(CONFIG_DYNAMIC_FTRACE) += mcount-dyn.o clean:
arch/riscv/kernel/ftrace.c +99 -1
108 lines not on this page · raw message
arch/riscv/kernel/mcount-dyn.S new file +52 -0
52 lines not on this page · raw message
arch/riscv/kernel/mcount.S +13 -9
@@ -32,13 +32,13 @@ addi s0, sp, 32 .endm - .macro STORE_ABI_STATE + .macro RESTORE_ABI_STATE ld ra, 8(sp) ld s0, 0(sp) addi sp, sp, 16 .endm - .macro STORE_RET_ABI_STATE + .macro RESTORE_RET_ABI_STATE ld ra, 24(sp) ld s0, 16(sp) ld a0, 8(sp)
@@ -46,6 +46,10 @@ .endm ENTRY(ftrace_stub) +#ifdef CONFIG_DYNAMIC_FTRACE + .global _mcount + .set _mcount, ftrace_stub +#endif ret ENDPROC(ftrace_stub)
@@ -66,15 +70,15 @@ ENTRY(return_to_handler) #ifdef HAVE_FUNCTION_GRAPH_FP_TEST mv a0, t6 #endif - la t0, ftrace_return_to_handler - jalr t0 + call ftrace_return_to_handler mv a1, a0 - STORE_RET_ABI_STATE + RESTORE_RET_ABI_STATE jalr a1 ENDPROC(return_to_handler) EXPORT_SYMBOL(return_to_handler) #endif +#ifndef CONFIG_DYNAMIC_FTRACE ENTRY(_mcount) la t4, ftrace_stub #ifdef CONFIG_FUNCTION_GRAPH_TRACER
@@ -104,9 +108,8 @@ do_ftrace_graph_caller: ld a2, -16(s0) #endif SAVE_ABI_STATE - la t0, prepare_ftrace_return - jalr t0 - STORE_ABI_STATE + call prepare_ftrace_return + RESTORE_ABI_STATE ret #endif
@@ -120,7 +123,8 @@ do_trace: SAVE_ABI_STATE jalr t5 - STORE_ABI_STATE + RESTORE_ABI_STATE ret ENDPROC(_mcount) EXPORT_SYMBOL(_mcount) +#endif
Signature · 1 line
2.15.1
Re: [patches] [PATCH v2 2/6] riscv/ftrace: Add dynamic function tracer support
arch/riscv/kernel/mcount-dyn.S line 49
Quoted 11 lines · Alan Kao
arch/riscv/kernel/mcount-dyn.S new file +11
-0+ /* + * For the dynamic ftrace to work, here we should reserve at least + * 8 bytes for a functional auipc-jalr pair. Pseudo inst nop may be + * interpreted as different length in different models, so we manually + * *expand* two 4-byte nops here. + * + * Calling ftrace_update_ftrace_func would overwrite the nops below. + * Check ftrace_modify_all_code for details. + */ + addi x0, x0, 0 + addi x0, x0, 0
This relies on behavior of the assembler which is undocumented and, if my reading of the specification is correct, a bug.
The documented way to assemble an sequence of 2 4-byte NOPs regardless of subtarget is as follows:
.option push
.option norvc
nop
nop
.option pop
I have filed https://github.com/riscv/riscv-binutils-gdb/issues/135 to get clarity on the assembler behavior; the explicit approach may be preferable even if the assembler behavior turns out to be correct.
-s
Re: [patches] [PATCH v2 2/6] riscv/ftrace: Add dynamic function tracer support
Quoted 25 lines · Stefan O'Rear
arch/riscv/kernel/mcount-dyn.S line 49
Quoted 11 lines · Alan Kao
arch/riscv/kernel/mcount-dyn.S new file +11
-0+ /* + * For the dynamic ftrace to work, here we should reserve at least + * 8 bytes for a functional auipc-jalr pair. Pseudo inst nop may be + * interpreted as different length in different models, so we manually + * *expand* two 4-byte nops here. + * + * Calling ftrace_update_ftrace_func would overwrite the nops below. + * Check ftrace_modify_all_code for details. + */ + addi x0, x0, 0 + addi x0, x0, 0This relies on behavior of the assembler which is undocumented and, if my reading of the specification is correct, a bug.
The documented way to assemble an sequence of 2 4-byte NOPs regardless of subtarget is as follows:
.option push
.option norvc
nop
nop
.option pop
Quoted 6 lines
I have filed https://github.com/riscv/riscv-binutils-gdb/issues/135 to get clarity on the assembler behavior; the explicit approach may be preferable even if the assembler behavior turns out to be correct.
-s
Thanks for pointing this out.
After checking the way other architectures have done this, I think we can just put a
call ftrace_stub
here. Currently we don't support linker relaxing with ftrace, so this macro will be expand to 8-byte inst-pair.
Alan
Quoted 7 lines
Signature · 6 lines
You received this message because you are subscribed to the Google Groups "RISC-V Patches" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at https://groups.google.com/a/groups.riscv.org/group/patches/. To view this discussion on the web visit https://groups.google.com/a/groups.riscv.org/d/msgid/patches/CADJ6UvOZz%3DWZ18wYVhZcH0u58u_snDxmgbwsfTOS2ski-4O7tg%40mail.gmail.com. For more options, visit https://groups.google.com/a/groups.riscv.org/d/optout.
PATCH v2 3/6 riscv/ftrace: Add dynamic function graph tracer support
In reply to Alan KaoOnce the function_graph tracer is enabled, a filtered function has the following call sequence:
* ftracer_caller ==> on/off by ftrace_make_call/ftrace_make_nop
* ftrace_graph_caller
* ftrace_graph_call ==> on/off by ftrace_en/disable_ftrace_graph_caller
* prepare_ftrace_return
Considering the following DYNAMIC_FTRACE_WITH_REGS feature, it would be more extendable to have a ftrace_graph_caller function, instead of calling prepare_ftrace_return directly in ftrace_caller.
--- arch/riscv/kernel/ftrace.c | 25 +++++++++++++++- arch/riscv/kernel/mcount-dyn.S | 65 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+), 1 deletion(-)
arch/riscv/kernel/ftrace.c +24 -1
@@ -51,7 +51,7 @@ static int __ftrace_modify_call(unsigned long hook_pos, unsigned long target,unsigned int nops[2 ] = {NOP4 ,NOP4 };int ret =0 ; -/* when ftrace_make_nop is called */ +/* for ftrace_make_nop and ftrace_disable_ftrace_graph_caller */ if (!enable) ret =ftrace_check_current_call (hook_pos, calls);
@@ -105,6 +105,7 @@ int __init ftrace_dyn_arch_init(void) }#endif +#ifdef CONFIG_FUNCTION_GRAPH_TRACER /* * Most of this function is copied from arm64. */
@@ -137,3 +138,25 @@ void prepare_ftrace_return(unsigned long *parent, unsigned long self_addr,return ; *parent = return_hooker; } + +#ifdef CONFIG_DYNAMIC_FTRACE +extern void ftrace_graph_call (void ); +int ftrace_enable_ftrace_graph_caller (void ) +{ +int ret =ftrace_check_current_call ((unsigned long )&ftrace_graph_call, +NULL ); + +if (ret) +return ret; + +return __ftrace_modify_call ((unsigned long )&ftrace_graph_call, + (unsigned long )&prepare_ftrace_return, true); +} + +int ftrace_disable_ftrace_graph_caller (void ) +{ +return __ftrace_modify_call ((unsigned long )&ftrace_graph_call, + (unsigned long )&prepare_ftrace_return, false); +} +#endif /* CONFIG_DYNAMIC_FTRACE */ +#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
arch/riscv/kernel/mcount-dyn.S +65 -0
95 lines not on this page · raw message
Signature · 1 line
2.15.1
PATCH v2 4/6 riscv/ftrace: Add ARCH_SUPPORTS_FTRACE_OPS support
In reply to Alan Kao--- arch/riscv/include/asm/ftrace.h | 1 + arch/riscv/kernel/mcount-dyn.S | 3 +++ 2 files changed, 4 insertions(+)
arch/riscv/include/asm/ftrace.h +1 -0
@@ -9,6 +9,7 @@#define HAVE_FUNCTION_GRAPH_FP_TEST #endif +#define ARCH_SUPPORTS_FTRACE_OPS 1#ifndef __ASSEMBLY__void _mcount (void );static inline unsigned long ftrace_call_adjust (unsigned long addr)
arch/riscv/kernel/mcount-dyn.S +3 -0
@@ -75,9 +75,12 @@ ENTRY(ftrace_caller) /* * a0: the address in the caller when calling ftrace_caller * a1: the caller's return address + * a2: the address of global variable function_trace_op */ ld a1, -8(s0) addi a0, ra, -MCOUNT_INSN_SIZE + la t5, function_trace_op + ld a2, 0(t5) #ifdef CONFIG_FUNCTION_GRAPH_TRACER /*
Signature · 1 line
2.15.1
PATCH v2 5/6 riscv/ftrace: Add DYNAMIC_FTRACE_WITH_REGS support
In reply to Alan Kao--- arch/riscv/Kconfig | 1 + arch/riscv/kernel/ftrace.c | 17 ++++++ arch/riscv/kernel/mcount-dyn.S | 124 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 142 insertions(+)
arch/riscv/Kconfig +1 -0
@@ -119,6 +119,7 @@ config ARCH_RV64I select HAVE_FUNCTION_GRAPH_TRACER select HAVE_FTRACE_MCOUNT_RECORD select HAVE_DYNAMIC_FTRACE + select HAVE_DYNAMIC_FTRACE_WITH_REGS endchoice
arch/riscv/kernel/ftrace.c +17 -0
@@ -105,6 +105,23 @@ int __init ftrace_dyn_arch_init(void) }#endif +#ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS +int ftrace_modify_call (struct dyn_ftrace *rec,unsigned long old_addr, +unsigned long addr) +{ +unsigned int offset = (unsigned int )(old_addr - rec->ip); +unsigned int auipc_call =to_auipc_insn (offset); +unsigned int jalr_call =to_jalr_insn (offset); +unsigned int calls[2 ] = {auipc_call, jalr_call}; +int ret =ftrace_check_current_call (rec->ip, calls); + +if (ret) +return ret; + +return __ftrace_modify_call (rec->ip, addr, true); +} +#endif +#ifdef CONFIG_FUNCTION_GRAPH_TRACER /* * Most of this function is copied from arm64.
arch/riscv/kernel/mcount-dyn.S +124 -0
127 lines not on this page · raw message
Signature · 1 line
2.15.1
PATCH v2 6/6 riscv/ftrace: Add HAVE_FUNCTION_GRAPH_RET_ADDR_PTR support
In reply to Alan KaoIn walk_stackframe, the pc now receives the address from calling ftrace_graph_ret_addr instead of manual calculation.
Note that the original calculation,
pc = frame->ra - 4
is buggy when the instruction at the return address happened to be a compressed inst. But since it is not a critical part of ftrace, it is ignored for now to ease the review process.
--- arch/riscv/include/asm/ftrace.h | 1 + arch/riscv/kernel/ftrace.c | 2 +- arch/riscv/kernel/stacktrace.c | 6 ++++++ 3 files changed, 8 insertions(+), 1 deletion(-)
arch/riscv/include/asm/ftrace.h +1 -0
@@ -8,6 +8,7 @@#if defined(CONFIG_FUNCTION_GRAPH_TRACER ) && defined(CONFIG_FRAME_POINTER )#define HAVE_FUNCTION_GRAPH_FP_TEST #endif +#define HAVE_FUNCTION_GRAPH_RET_ADDR_PTR #define ARCH_SUPPORTS_FTRACE_OPS 1#ifndef __ASSEMBLY__
arch/riscv/kernel/ftrace.c +1 -1
@@ -150,7 +150,7 @@ void prepare_ftrace_return(unsigned long *parent, unsigned long self_addr,return ; err =ftrace_push_return_trace (old, self_addr, &trace.depth, - frame_pointer,NULL ); + frame_pointer, parent);if (err == -EBUSY )return ; *parent = return_hooker;
arch/riscv/kernel/stacktrace.c +6 -0
@@ -18,6 +18,7 @@#include <linux/sched/debug.h> #include <linux/sched/task_stack.h> #include <linux/stacktrace.h> +#include <linux/ftrace.h> #ifdef CONFIG_FRAME_POINTER
@@ -63,7 +64,12 @@ static void notrace walk_stackframe(struct task_struct *task, frame = (struct stackframe *)fp -1 ; sp = fp; fp = frame->fp; +#ifdef HAVE_FUNCTION_GRAPH_RET_ADDR_PTR + pc =ftrace_graph_ret_addr (current,NULL , frame->ra, + (unsigned long *)(fp -8 )); +#else pc = frame->ra -0x4 ; +#endif } }
Signature · 1 line
2.15.1