Skip to contentSkip to conversation

Add dynamic ftrace support for RISC-V platforms

PATCH In review Alan Kao 2 participants 10 messages Jan 18, 2018 – Feb 8, 2018

Subsystems (2)
lore
Share & tools
lore

Alan Kao

author
#0 lore raw

v3 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 v3:
- Replace the nops at the tracer call sites into a "call ftrace_stub" instruction for better understanding (1/6 and 2/6)

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  | 241 ++++++++++++++++++++++++++++++++++++++++
 arch/riscv/kernel/mcount.S      |  22 ++--
 arch/riscv/kernel/stacktrace.c  |   6 +
 scripts/recordmcount.pl         |   5 +
 9 files changed, 461 insertions(+), 13 deletions(-)
 create mode 100644 arch/riscv/kernel/mcount-dyn.S
Signature · 1 line
2.15.1
author
#1 lore raw

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

Cc Greentime Hu <[email protected]>
---
 arch/riscv/Kconfig      | 1 +
 arch/riscv/Makefile     | 3 +++
 scripts/recordmcount.pl | 5 +++++
 3 files changed, 9 insertions(+)
arch/riscv/Kconfig +1 -0
index 40a67fc12328..1f45a76f412e 100644
@@ -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
index 6719dd30ec5b..899226e0da7d 100644
@@ -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
index 2033af758173..d44d55db7c06 100755
@@ -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
author
#2 lore raw

PATCH v3 2/6 riscv/ftrace: Add dynamic function tracer support

In reply to Alan Kao

We 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 stub call 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.

Cc Greentime Hu <[email protected]>
---
 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  |  50 ++++++++++++++++++++
 arch/riscv/kernel/mcount.S      |  22 +++++----
 6 files changed, 211 insertions(+), 12 deletions(-)
 create mode 100644 arch/riscv/kernel/mcount-dyn.S
arch/riscv/Kconfig +1 -0
index 1f45a76f412e..a5c5c88700d4 100644
@@ -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
index 196f62ffc428..d7bdf888f1ca 100644
@@ -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 +50 -0

50 lines not on this page · raw message

arch/riscv/kernel/mcount.S +13 -9
index c46a778627be..ce9bdc57a2a1 100644
@@ -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
author
#3 lore raw

PATCH v3 3/6 riscv/ftrace: Add dynamic function graph tracer support

In reply to Alan Kao

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

Cc Greentime Hu <[email protected]>
---
 arch/riscv/kernel/ftrace.c     | 25 ++++++++++++++++-
 arch/riscv/kernel/mcount-dyn.S | 64 ++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 88 insertions(+), 1 deletion(-)
arch/riscv/kernel/ftrace.c +24 -1
index 311c287433c9..dce1286af9b0 100644
@@ -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 +64 -0

94 lines not on this page · raw message

Signature · 1 line
2.15.1
author
#4 lore raw

PATCH v3 4/6 riscv/ftrace: Add ARCH_SUPPORTS_FTRACE_OPS support

In reply to Alan Kao
Cc Greentime Hu <[email protected]>
---
 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
index acf0c7d001f3..429a6a156645 100644
@@ -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
index 1ddf8b1a9345..c8959bdd4f38 100644
@@ -74,9 +74,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
author
#5 lore raw

PATCH v3 5/6 riscv/ftrace: Add DYNAMIC_FTRACE_WITH_REGS support

In reply to Alan Kao
Cc Greentime Hu <[email protected]>
---
 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
index a5c5c88700d4..bb2c64976583 100644
@@ -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
index dce1286af9b0..3922b29c107b 100644
@@ -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
author
#6 lore raw

PATCH v3 6/6 riscv/ftrace: Add HAVE_FUNCTION_GRAPH_RET_ADDR_PTR support

In reply to Alan Kao

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

Cc Greentime Hu <[email protected]>
---
 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
index 429a6a156645..6e4b4c96b63e 100644
@@ -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
index 3922b29c107b..ded8ab25f4ad 100644
@@ -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
index 559aae781154..a4b1d94371a0 100644
@@ -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
#7 lore raw
In reply to Alan Kao
Quoted 20 lines · On Thu, 18 Jan 2018 07:45:39 PST (-0800), [email protected] wrote:

This patch set includes the building blocks of dynamic ftrace features for RISC-V machines.

Changes in v3:
- Replace the nops at the tracer call sites into a "call ftrace_stub" instruction for better understanding (1/6 and 2/6)

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

Quoted 11 lines
 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  | 241 ++++++++++++++++++++++++++++++++++++++++
 arch/riscv/kernel/mcount.S      |  22 ++--
 arch/riscv/kernel/stacktrace.c  |   6 +
 scripts/recordmcount.pl         |   5 +
 9 files changed, 461 insertions(+), 13 deletions(-)
 create mode 100644 arch/riscv/kernel/mcount-dyn.S

Sorry it took me a while to get around to these. Do you mind submitting a v4 that's based on linux-4.15? I'm getting all sorts of merge errors trying to "git am" these.

author
#8 lore raw
Quoted 33 lines · Palmer Dabbelt
Quoted 20 lines · On Thu, 18 Jan 2018 07:45:39 PST (-0800), [email protected] wrote:

This patch set includes the building blocks of dynamic ftrace features for RISC-V machines.

Changes in v3:
- Replace the nops at the tracer call sites into a "call ftrace_stub" instruction for better understanding (1/6 and 2/6)

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

Quoted 11 lines
 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  | 241 ++++++++++++++++++++++++++++++++++++++++
 arch/riscv/kernel/mcount.S      |  22 ++--
 arch/riscv/kernel/stacktrace.c  |   6 +
 scripts/recordmcount.pl         |   5 +
 9 files changed, 461 insertions(+), 13 deletions(-)
 create mode 100644 arch/riscv/kernel/mcount-dyn.S
Quoted 3 lines

Sorry it took me a while to get around to these. Do you mind submitting a v4 that's based on linux-4.15? I'm getting all sorts of merge errors trying to "git am" these.

Sure. But do you mean the riscv-linux-4.15 at the github repo or Linus's linux-4.15-rc......,says, rc8?

Also, we have fixed our internal email server issue, so I will send this v4 set using [email protected], by which I signed off all previous ftrace patches, instead of the current gmail one. I hope this won't be confusing to you.

Thanks,
Alan

author
#9 lore raw
In reply to Palmer Dabbelt
Quoted 33 lines · Palmer Dabbelt
Quoted 20 lines · On Thu, 18 Jan 2018 07:45:39 PST (-0800), [email protected] wrote:

This patch set includes the building blocks of dynamic ftrace features for RISC-V machines.

Changes in v3:
- Replace the nops at the tracer call sites into a "call ftrace_stub" instruction for better understanding (1/6 and 2/6)

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

Quoted 11 lines
 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  | 241 ++++++++++++++++++++++++++++++++++++++++
 arch/riscv/kernel/mcount.S      |  22 ++--
 arch/riscv/kernel/stacktrace.c  |   6 +
 scripts/recordmcount.pl         |   5 +
 9 files changed, 461 insertions(+), 13 deletions(-)
 create mode 100644 arch/riscv/kernel/mcount-dyn.S
Quoted 3 lines

Sorry it took me a while to get around to these. Do you mind submitting a v4 that's based on linux-4.15? I'm getting all sorts of merge errors trying to "git am" these.

Sorry for the misunderstanding in the previous email. I will rebase the set on linux-4.15. Thanks.