Skip to contentSkip to conversation

ftrace: mcount record based dynamic tracing for ARM

PATCH Steven Rostedt 3 participants 4 messages Nov 21, 2008 5 messages not in the archive

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): [email protected]

#5 lore raw
Quoted 7 lines · Jim Radford

commit 3b17e9f39304e266edf8ef05c4a01e31c6ecbf5c

Author: Jim Radford <[email protected]>
Date:   Thu Nov 20 19:48:39 2008 -0800

ftrace: enable dynamic ftrace for arm

Update to the latest api and syncing functions with the x86 versions.

Again, Signed-off-by: is missing.

arch/arm/Kconfig config ARM · line 20

Quoted 12 lines
arch/arm/Kconfig +2 -1
index 9722f8b..1b4162c 100644
@@ -16,7 +16,9 @@ config ARM
 	select HAVE_ARCH_KGDB
 	select HAVE_KPROBES if (!XIP_KERNEL)
 	select HAVE_KRETPROBES if (HAVE_KPROBES)
-	select HAVE_FUNCTION_TRACER if (!XIP_KERNEL)
+	select HAVE_FTRACE_MCOUNT_RECORD
+	select HAVE_DYNAMIC_FTRACE if (!XIP_KERNEL)

Russell mentioned something about the code not being compatible with Thumb2, is the above if statement enough?

Quoted 12 lines
arch/arm/Kconfig +1 -0
+	select HAVE_FUNCTION_TRACER
 	select HAVE_GENERIC_DMA_COHERENT
 	help
 	  The ARM series is a line of low-power-consumption RISC chip designs
arch/arm/include/asm/ftrace.h +0 -0
index 39c8bc1..877c0d4 100644
@@ -7,6 +7,19 @@
 
 #ifndef __ASSEMBLY__
 extern void mcount(void);

arch/arm/include/asm/ftrace.h line 19

Quoted 10 lines
arch/arm/include/asm/ftrace.h +10 -0
+
+static inline unsigned long ftrace_call_adjust(unsigned long addr)
+{
+	return addr;
+}
+
+#ifdef CONFIG_DYNAMIC_FTRACE
+
+struct dyn_arch_ftrace {
+	/* No extra data needed for x86 */

Do you mean: /* No extra data needed for ARM */

;-)

Quoted 13 lines
arch/arm/include/asm/ftrace.h +3 -0
+};
+
+#endif /*  CONFIG_DYNAMIC_FTRACE */
 #endif
 
 #endif
arch/arm/kernel/entry-common.S +0 -0
index 06269ea..06216af 100644
@@ -104,14 +104,7 @@ ENDPROC(ret_from_fork)
 #ifdef CONFIG_FUNCTION_TRACER
 #ifdef CONFIG_DYNAMIC_FTRACE

arch/arm/kernel/entry-common.S ENDPROC(ret_from_fork) · line 107

Quoted 10 lines
arch/arm/kernel/entry-common.S +1 -8
 ENTRY(mcount)
-	stmdb sp!, {r0-r3, lr}
-	mov r0, lr
-	sub r0, r0, #MCOUNT_INSN_SIZE
-
-	.globl mcount_call
-mcount_call:
-	bl ftrace_stub
-	ldmia sp!, {r0-r3, pc}
+	mov pc, lr

This looks OK. The new mcount stub for dynamic ftrace is a simple return. It has been a while since I've programmed ARM (7 years ago), but I'm assuming that loading the program counter with the link register is a simple return.

Quoted 24 lines
arch/arm/kernel/entry-common.S +0 -0
 ENTRY(ftrace_caller)
 	stmdb sp!, {r0-r3, lr}
arch/arm/kernel/ftrace.c +2 -2
index 6c90479..d63b5a6 100644
@@ -23,13 +23,13 @@
 static unsigned long bl_insn;
 static const unsigned long NOP = 0xe1a00000; /* mov r0, r0 */
 
-unsigned char *ftrace_nop_replace(void)
+static unsigned char *ftrace_nop_replace(void)
 {
 	return (char *)&NOP;
 }
 
 /* construct a branch (BL) instruction to addr */
-unsigned char *ftrace_call_replace(unsigned long pc, unsigned long addr)
+static unsigned char *ftrace_call_replace(unsigned long pc, unsigned long addr)
 {
 	long offset;
 

arch/arm/kernel/ftrace.c unsigned char *ftrace_call_replace(unsigned long pc, unsigned long addr) · line 51

Quoted 7 lines
arch/arm/kernel/ftrace.c +1 -1
@@ -46,7 +46,7 @@ unsigned char *ftrace_call_replace(unsigned long pc, unsigned long addr)
 	return (unsigned char *)&bl_insn;
 }
 
-int ftrace_modify_code(unsigned long pc, unsigned char *old_code,
+static int ftrace_modify_code(unsigned long pc, unsigned char *old_code,
 		       unsigned char *new_code)
 {

The ftrace_modify_code could use a bit of clean up. Unless there is some strange reason that it can not use the probe_kernel_ functions, it needs to be updated. That approach is the most robust. Here's an example of what it should look like:

{
	unsigned char replaced[MCOUNT_INSN_SIZE];

	/*
	 * Note: Due to modules and __init, code can
	 *  disappear and change, we need to protect against faulting
	 *  as well as code changing. We do this by using the
	 *  probe_kernel_* functions.
	 *
	 * No real locking needed, this code is run through
	 * kstop_machine, or before SMP starts.
	 */

	/* read the text we want to modify */
	if (probe_kernel_read(replaced, (void *)pc, MCOUNT_INSN_SIZE))
		return -EFAULT;

	/* Make sure it is what we expect it to be */
	if (memcmp(replaced, old_code, MCOUNT_INSN_SIZE) != 0)
		return -EINVAL;

	/* replace the text with the new text */
	if (probe_kernel_write((void *)pc, new_code, MCOUNT_INSN_SIZE))
		return -EPERM;

	flush_icache_range(pc, pc + MCOUNT_INSN_SIZE);

	return 0;
}

And the new design expects special return values:

-EFAULT: error reading the address
-EINVAL: what was at the address was not what we expected -EPERM: could not write to the address

-- Steve

Quoted 55 lines
arch/arm/kernel/ftrace.c +31 -7
 	unsigned long err = 0, replaced = 0, old, new;
@@ -82,22 +82,46 @@ int ftrace_modify_code(unsigned long pc, unsigned char *old_code,
 	return err;
 }
 
+int ftrace_make_nop(struct module *mod,
+		    struct dyn_ftrace *rec, unsigned long addr)
+{
+	unsigned char *new, *old;
+	unsigned long ip = rec->ip;
+
+	old = ftrace_call_replace(ip, addr);
+	new = ftrace_nop_replace();
+
+	return ftrace_modify_code(rec->ip, old, new);
+}
+
+int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
+{
+	unsigned char *new, *old;
+	unsigned long ip = rec->ip;
+
+	old = ftrace_nop_replace();
+	new = ftrace_call_replace(ip, addr);
+
+	return ftrace_modify_code(rec->ip, old, new);
+}
+
 int ftrace_update_ftrace_func(ftrace_func_t func)
 {
+	unsigned long ip = (unsigned long)(&ftrace_call);
+	unsigned char old[MCOUNT_INSN_SIZE], *new;
 	int ret;
-	unsigned long pc, old;
-	unsigned char *new;
 
-	pc = (unsigned long)&ftrace_call;
-	memcpy(&old, &ftrace_call, MCOUNT_INSN_SIZE);
-	new = ftrace_call_replace(pc, (unsigned long)func);
-	ret = ftrace_modify_code(pc, (unsigned char *)&old, new);
+	memcpy(old, &ftrace_call, sizeof(old));
+	new = ftrace_call_replace(ip, (unsigned long)func);
+	ret = ftrace_modify_code(ip, (unsigned char *)&old, new);
+
 	return ret;
 }
 
 /* run from kstop_machine */
 int __init ftrace_dyn_arch_init(void *data)
 {
-	ftrace_mcount_set(data);
+	/* The return code is retured via data */
+	*(unsigned long *)data = 0;
 	return 0;
 }
#6 lore raw
Quoted 7 lines · Steven Rostedt

Review

Quoted 3 lines · Jim Radford
(quoted diff) +2 -1
-	select HAVE_FUNCTION_TRACER if (!XIP_KERNEL)
+	select HAVE_FTRACE_MCOUNT_RECORD
+	select HAVE_DYNAMIC_FTRACE if (!XIP_KERNEL)

Russell mentioned something about the code not being compatible with Thumb2, is the above if statement enough?

I think I said (or should've said) "upcoming Thumb 2" - it's not yet in the kernel but there's a patch series floating around for it. We've started on merging some of the pre-requisits, and it will mean that the instruction length is no longer constant. (It may be a 16bit or 32bit instruction.)

I suspect that ftrace won't be able to handle that, so it may have to depend on !THUMB2_KERNEL for the time being.

Quoted 14 lines
Quoted 14 lines
arch/arm/kernel/entry-common.S +0 -6
index 06269ea..06216af 100644
@@ -104,14 +104,7 @@ ENDPROC(ret_from_fork)
 #ifdef CONFIG_FUNCTION_TRACER
 #ifdef CONFIG_DYNAMIC_FTRACE
 ENTRY(mcount)
-	stmdb sp!, {r0-r3, lr}
-	mov r0, lr
-	sub r0, r0, #MCOUNT_INSN_SIZE
-
-	.globl mcount_call
-mcount_call:
Quoted 8 lines

arch/arm/kernel/entry-common.S ENDPROC(ret_from_fork) · line 107

Quoted 3 lines
arch/arm/kernel/entry-common.S +1 -2
-	bl ftrace_stub
-	ldmia sp!, {r0-r3, pc}
+	mov pc, lr

This looks OK. The new mcount stub for dynamic ftrace is a simple return. It has been a while since I've programmed ARM (7 years ago), but I'm assuming that loading the program counter with the link register is a simple return.

Yes, that's all that's required.

#7 lore raw
Quoted 9 lines · Russell King - ARM Linux
Quoted 7 lines · Steven Rostedt

Review

Quoted 3 lines · Jim Radford
(quoted diff) +2 -1
-	select HAVE_FUNCTION_TRACER if (!XIP_KERNEL)
+	select HAVE_FTRACE_MCOUNT_RECORD
+	select HAVE_DYNAMIC_FTRACE if (!XIP_KERNEL)

Russell mentioned something about the code not being compatible with Thumb2, is the above if statement enough?

Quoted 8 lines

I think I said (or should've said) "upcoming Thumb 2" - it's not yet in the kernel but there's a patch series floating around for it. We've started on merging some of the pre-requisits, and it will mean that the instruction length is no longer constant. (It may be a 16bit or 32bit instruction.)

I suspect that ftrace won't be able to handle that, so it may have to depend on !THUMB2_KERNEL for the time being.

Actually it depends on how the compiler adds the mcount call. That's all that ftrace touches. The call to mcount. If all callers to mcount stay as 32 bit, then it may still work.

-- Steve

#8 lore raw
In reply to Russell King - ARM Linux
Quoted 9 lines · Russell King - ARM Linux
Quoted 7 lines · Steven Rostedt

Review

Quoted 3 lines · Jim Radford
(quoted diff) +2 -1
-	select HAVE_FUNCTION_TRACER if (!XIP_KERNEL)
+	select HAVE_FTRACE_MCOUNT_RECORD
+	select HAVE_DYNAMIC_FTRACE if (!XIP_KERNEL)

Russell mentioned something about the code not being compatible with Thumb2, is the above if statement enough?

Quoted 3 lines

We've started on merging some of the pre-requisits, and it will mean that the instruction length is no longer constant. (It may be a 16bit or 32bit instruction.)

The only instruction that matters for arm is "bl <func>" since that's what's emitted by gcc to call mcount(). I suspect thumb will be easy to support. <func> isn't known when the file is compiled, so I assume in that case the assembler will have to leave at least 4 bytes (even in thumb) in case mcount() gets linked far away.

I haven't looked at the return tracing code yet. That might be harder to support, but given x86 works, I suspect it'll be doable.

-Jim