Add dynamic ftrace support for RISC-V platforms
cover Add dynamic ftrace support for RISC-V platforms
This patch set includes the building blocks of dynamic ftraces features for RISC-V machines.
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 | 6 +- arch/riscv/include/asm/ftrace.h | 47 ++++++++ arch/riscv/kernel/Makefile | 5 +- arch/riscv/kernel/ftrace.c | 136 +++++++++++++++++++++- 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, 460 insertions(+), 14 deletions(-) create mode 100644 arch/riscv/kernel/mcount-dyn.S
Signature · 1 line
2.15.1
PATCH 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. This is because relaxation happens after the script records offsets of _mcount call sites, resulting in a unreliable record.
--- arch/riscv/Kconfig | 1 + arch/riscv/Makefile | 6 +++++- scripts/recordmcount.pl | 5 +++++ 3 files changed, 11 insertions(+), 1 deletion(-)
arch/riscv/Kconfig +1 -0
@@ -112,6 +112,7 @@ config ARCH_RV64I select 64BIT select HAVE_FUNCTION_TRACER select HAVE_FUNCTION_GRAPH_TRACER + select HAVE_FTRACE_MCOUNT_RECORD endchoice
arch/riscv/Makefile +5 -1
@@ -10,7 +10,11 @@ LDFLAGS := OBJCOPYFLAGS := -O binary -LDFLAGS_vmlinux := +ifeq ($(CONFIG_DYNAMIC_FTRACE),y) + LDFLAGS_vmlinux := --no-relax +else + LDFLAGS_vmlinux := +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
Re: [patches] [PATCH 1/6] riscv/ftrace: Add RECORD_MCOUNT support
arch/riscv/Makefile line 17
Quoted 6 lines · Alan Kao
arch/riscv/Makefile +5
-1-LDFLAGS_vmlinux := +ifeq ($(CONFIG_DYNAMIC_FTRACE),y) + LDFLAGS_vmlinux := --no-relax +else + LDFLAGS_vmlinux := +endif
Why not:
LDFLAGS_vmlinux :=
ifeq ($(CONFIG_DYNAMIC_FTRACE),y)LDFLAGS_vmlinux += --no-relax
endif
Re: [patches] [PATCH 1/6] riscv/ftrace: Add RECORD_MCOUNT support
Quoted 10 lines · Christoph Hellwig
arch/riscv/Makefile line 17
Quoted 6 lines · Alan Kao
arch/riscv/Makefile +5
-1-LDFLAGS_vmlinux := +ifeq ($(CONFIG_DYNAMIC_FTRACE),y) + LDFLAGS_vmlinux := --no-relax +else + LDFLAGS_vmlinux := +endifWhy not:
Quoted 5 lines
LDFLAGS_vmlinux:=
ifeq ($(CONFIG_DYNAMIC_FTRACE),y)LDFLAGS_vmlinux+= --no-relax
endif
Thanks for the comment! This will be enhanced in the next try.
Alan
PATCH 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 site calls to once the function is 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 | 94 ++++++++++++++++++++++++++++++++++++++++- arch/riscv/kernel/mcount-dyn.S | 52 +++++++++++++++++++++++ arch/riscv/kernel/mcount.S | 22 ++++++---- 6 files changed, 207 insertions(+), 12 deletions(-) create mode 100644 arch/riscv/kernel/mcount-dyn.S
arch/riscv/Kconfig +1 -0
@@ -113,6 +113,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 +93 -1
102 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
Steven Rostedt
maintainerRe: [PATCH 2/6] riscv/ftrace: Add dynamic function tracer support
Quoted 6 lines · On Wed, 10 Jan 2018 15:38:10 +0800 Alan Kao <[email protected]> wrote:
arch/riscv/kernel/ftrace.c +6
-0+static int ftrace_check_current_call (unsigned long hook_pos, +unsigned int *expected) +{ +unsigned int replaced[2 ]; +unsigned int nops[2 ] = {NOP4 ,NOP4 }; +
arch/riscv/kernel/ftrace.c line 28
Quoted 10 lines
arch/riscv/kernel/ftrace.c +10
-0+/* we expect nops at the hook position */ +if (!expected) + expected = nops; + +/* read the text we want to modify */ +if (probe_kernel_read (replaced, (void *)hook_pos,MCOUNT_INSN_SIZE )) +return -EFAULT ; + +/* Make sure it is what we expect it to be */ +if (replaced[0 ] != expected[0 ] || replaced[1 ] != expected[1 ]) {
I wonder if we can have a slight enhancement by doing:
if (memcmp(replaced, expected, sizeof(replaced)) != 0) {-- Steve
Quoted 8 lines
arch/riscv/kernel/ftrace.c +8
-0+pr_err ("%p: expected (%08x %08x) but get (%08x %08x)" , + (void *)hook_pos, expected[0 ], expected[1 ], replaced[0 ], + replaced[1 ]); +return -EINVAL ; + } + +return 0 ; +}
Steven Rostedt
maintainerRe: [PATCH 2/6] riscv/ftrace: Add dynamic function tracer support
In reply to Alan KaoQuoted 11 lines · On Wed, 10 Jan 2018 15:38:10 +0800 Alan Kao <[email protected]> wrote:
arch/riscv/kernel/ftrace.c +11
-0+static int __ftrace_modify_call (unsigned long hook_pos,unsigned long target, +bool enable) +{ +unsigned int offset = (unsigned int )(target - hook_pos); +unsigned int auipc_call =to_auipc_insn (offset); +unsigned int jalr_call =to_jalr_insn (offset); +unsigned int calls[2 ] = {auipc_call, jalr_call}; +unsigned int nops[2 ] = {NOP4 ,NOP4 }; +int ret =0 ; + +/* when ftrace_make_nop is called */
arch/riscv/kernel/ftrace.c line 58
Quoted 10 lines
arch/riscv/kernel/ftrace.c +10
-0+if (!enable) + ret =ftrace_check_current_call (hook_pos, calls); + +if (ret) + goto out; + +/* replace the auipc-jalr pair at once */ + ret =probe_kernel_write ((void *)hook_pos, enable ? calls : nops, +MCOUNT_INSN_SIZE ); +if (ret)
You don't want to return the return of probe_kernel_write() here. For ftrace bug to work properly, if a fail to write occurs, you need to return -EPERM.
-- Steve
Quoted 42 lines
arch/riscv/kernel/ftrace.c +42
-0+ goto out; + +smp_mb (); +flush_icache_range ((void *)hook_pos, (void *)hook_pos +MCOUNT_INSN_SIZE ); + +out: +return ret; +} + +int ftrace_make_call (struct dyn_ftrace *rec,unsigned long addr) +{ +int ret =ftrace_check_current_call (rec->ip,NULL ); + +if (ret) +return ret; + +return __ftrace_modify_call (rec->ip, addr, true); +} + +int ftrace_make_nop (struct module *mod,struct dyn_ftrace *rec, +unsigned long addr) +{ +return __ftrace_modify_call (rec->ip, addr, false); +} + +int ftrace_update_ftrace_func (ftrace_func_t func) +{ +int ret =__ftrace_modify_call ((unsigned long )&ftrace_call, + (unsigned long )func, true); +if (!ret) { + ret =__ftrace_modify_call ((unsigned long )&ftrace_regs_call, + (unsigned long )func, true); + } + +return ret; +} + +int __init ftrace_dyn_arch_init (void ) +{ +return 0 ; +} +#endif
Re: [PATCH 2/6] riscv/ftrace: Add dynamic function tracer support
Quoted 29 lines · Steven Rostedt
Quoted 11 lines · On Wed, 10 Jan 2018 15:38:10 +0800 Alan Kao <[email protected]> wrote:
arch/riscv/kernel/ftrace.c +11
-0+static int __ftrace_modify_call (unsigned long hook_pos,unsigned long target, +bool enable) +{ +unsigned int offset = (unsigned int )(target - hook_pos); +unsigned int auipc_call =to_auipc_insn (offset); +unsigned int jalr_call =to_jalr_insn (offset); +unsigned int calls[2 ] = {auipc_call, jalr_call}; +unsigned int nops[2 ] = {NOP4 ,NOP4 }; +int ret =0 ; + +/* when ftrace_make_nop is called */ arch/riscv/kernel/ftrace.c line 58
Quoted 10 lines
arch/riscv/kernel/ftrace.c +10
-0+if (!enable) + ret =ftrace_check_current_call (hook_pos, calls); + +if (ret) + goto out; + +/* replace the auipc-jalr pair at once */ + ret =probe_kernel_write ((void *)hook_pos, enable ? calls : nops, +MCOUNT_INSN_SIZE ); +if (ret)You don't want to return the return of
probe_kernel_write()here. For ftrace bug to work properly, if a fail to write occurs, you need to return-EPERM.
Quoted 2 lines
-- Steve
Thanks for the correction and the style fix in the previous mail. These problems will be fixed in the next version patch.
Alan
PATCH 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 that the DYNAMIC_FTRACE_WITH_REGS feature, which introduces another hook entry ftrace_regs_caller, will access to ftrace_graph_call when needed, 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
@@ -45,7 +45,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);
@@ -99,6 +99,7 @@ int __init ftrace_dyn_arch_init(void) }#endif +#ifdef CONFIG_FUNCTION_GRAPH_TRACER /* * Most of this function is copied from arm64. */
@@ -131,3 +132,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 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 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
@@ -114,6 +114,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
@@ -99,6 +99,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 6/6 riscv/ftrace: Add HAVE_FUNCTION_GRAPH_RET_ADDR_PTR support
In reply to Alan KaoWhen doing unwinding in the function walk_stackframe, the pc now receives the address from calling ftrace_graph_ret_addr instead of manual calculation.
Note that the original expression,
pc = frame->ra - 4
is buggy if the instruction at the return address happened to be a compressed inst. But since it is not a critical part of ftrace and is a RISC-V-specific behavior, 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
@@ -144,7 +144,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