Skip to contentSkip to conversation

x86_64,tracing: multiplexing function tracer

RFC In review Jiri Olsa 2 participants 7 messages Apr 29, 2011

Subsystems (4)
lore
Share & tools
lore

Jiri Olsa

author
#0 lore raw

cover x86_64,tracing: multiplexing function tracer

hi, me again ;)

from last RFC email it turned out it'd be good to have support for more than 1 currently running function tracers. With the possibility to specify separate filter for each of them.

I thought about one way how to do that and put it to the code for considering. It seems to work properly, though I was fighting with set_ftrace_filter for a while.. and it is still not ideal ;)

How does it work?

- when the function tracer is registered it got assigned unique ID
- 'mcount callback' beside ip and parent_ip provides bitmask set with tracers IDs which are interested in the current function
- 'mcount callback' calls tracers callbacks as set in the bitmask

How did that bitfield get to the 'mcount callback'?

- several 'mcount callbacks' are statically generated with code providing unique bitmask
- each combination of tracers (IDs) map to a single 'mcount callback' providing bitmask of this combination
- function pointers of these callbacks are stored and used to patch the function 'mcount call' instruction

To have all bitmask possibilities covered it is generated 2^(allowed tracers) number of 'mcount callbacks'. As this statical part is relativelly small it's probably ok.

I guess it's possible to use dynamic approach and allocate only as many 'mcount callbacks' as needed (similar as for optimized kprobes).

How does filtering work (interface) ?

- each tracer got unique name
- added file function_tracers providing list of registered tracers
- one tracer is allways default, and this one will be changed via set_ftrace_filter interface
- default tracer can be changed by write the name to the function_tracers file

How does filtering work (code) ?

- each 'struct dyn_ftrace' record now holds 2 bitmasks:
filter and notrace
- those 2 bitmask are updated by the set_ftrace_filter code to carry bitmask of tracers interested in the function
- this info is processed during the 'mcount callback' assigning

Example session

	# echo function > ./current_tracer 
	# cat function_tracers 
	*trace
	# echo 1 > ./function_profile_enabled 
	# cat function_tracers 
	 trace
	*trace_profile
	# cat set_ftrace_filter 
	[               trace] #### all functions enabled ####
	[       trace_profile] #### all functions enabled ####
	# echo sys_read > ./set_ftrace_filter 
	# cat set_ftrace_filter 
	[               trace] #### all functions enabled ####
	[       trace_profile] sys_read
	# echo trace > ./function_tracers 
	# cat function_tracers 
	*trace
	 trace_profile
	# echo sys_write > ./set_ftrace_filter 
	# cat set_ftrace_filter 
	[               trace] sys_write
	[       trace_profile] sys_read

attached patches:
- 1/3 tracing: function tracer registration
- 2/3 tracing: adding statical callers
- 3/3 tracing: set_ftrace_filter support

This is by no means complete solution, and there are many leftovers.. just wanted to try this way ;)

plz let me know what you think

wbr,
jirka

---
 arch/x86/include/asm/ftrace.h     |    4 +
 arch/x86/kernel/entry_64.S        |   22 ++-
 arch/x86/kernel/ftrace.c          |   50 ++--
 include/linux/ftrace.h            |   16 +-
 kernel/trace/ftrace.c             |  555 +++++++++++++++++++++----------------
 kernel/trace/trace_events.c       |    1 +
 kernel/trace/trace_functions.c    |    2 +
 kernel/trace/trace_irqsoff.c      |    1 +
 kernel/trace/trace_sched_wakeup.c |    1 +
 kernel/trace/trace_stack.c        |    1 +
 10 files changed, 383 insertions(+), 270 deletions(-)
author
#1 lore raw

RFC 1/3 tracing: function tracer registration

adding ID and default tracer support for function
tracer registration

---
 arch/x86/include/asm/ftrace.h     |    2 +
 arch/x86/kernel/entry_64.S        |    6 +-
 arch/x86/kernel/ftrace.c          |   13 --
 include/linux/ftrace.h            |    9 +-
 kernel/trace/ftrace.c             |  290 +++++++++++++++----------------------
 kernel/trace/trace_events.c       |    1 +
 kernel/trace/trace_functions.c    |    2 +
 kernel/trace/trace_irqsoff.c      |    1 +
 kernel/trace/trace_sched_wakeup.c |    1 +
 kernel/trace/trace_stack.c        |    1 +
 10 files changed, 136 insertions(+), 190 deletions(-)
arch/x86/include/asm/ftrace.h +2 -0
index db24c22..45ae1fa 100644
@@ -1,6 +1,8 @@
 #ifndef _ASM_X86_FTRACE_H
 #define _ASM_X86_FTRACE_H
 
+#define FTRACE_TRACERS_MAX 4
+
 #ifdef __ASSEMBLY__
 
 	.macro MCOUNT_SAVE_FRAME
arch/x86/kernel/entry_64.S +4 -2
index 8a445a0..e0ba7d9 100644
@@ -79,8 +79,10 @@ ENTRY(ftrace_caller)
 	movq 8(%rbp), %rsi
 	subq $MCOUNT_INSN_SIZE, %rdi
 
-GLOBAL(ftrace_call)
-	call ftrace_stub
+	/* by default call all registered handlers */
+	movq $-1, %rdx
+
+	call ftrace_call
 
 	MCOUNT_RESTORE_FRAME
 
arch/x86/kernel/ftrace.c +0 -13
index 0ba15a6..dc92378 100644
@@ -321,19 +321,6 @@ int ftrace_make_call(struct dyn_ftrace *rec, unsigned long 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;
-
-	memcpy(old, &ftrace_call, MCOUNT_INSN_SIZE);
-	new = ftrace_call_replace(ip, (unsigned long)func);
-	ret = ftrace_modify_code(ip, old, new);
-
-	return ret;
-}
-
 int __init ftrace_dyn_arch_init(void *data)
 {
 	/* The return code is retured via data */
include/linux/ftrace.h +5 -4
index ca29e03..b7ab09d 100644
@@ -30,8 +30,9 @@ ftrace_enable_sysctl(struct ctl_table *table, int write,
 typedef void (*ftrace_func_t)(unsigned long ip, unsigned long parent_ip);
 
 struct ftrace_ops {
-	ftrace_func_t	  func;
-	struct ftrace_ops *next;
+	char *name;
+	int   id;
+	ftrace_func_t func;
 };
 
 extern int function_trace_stop;
@@ -175,9 +176,9 @@ int unregister_ftrace_command(struct ftrace_func_command *cmd);
 /* defined in arch */
 extern int ftrace_ip_converted(unsigned long ip);
 extern int ftrace_dyn_arch_init(void *data);
-extern int ftrace_update_ftrace_func(ftrace_func_t func);
 extern void ftrace_caller(void);
-extern void ftrace_call(void);
+extern void ftrace_call(unsigned long ip, unsigned long parent_ip,
+			unsigned long flags);
 extern void mcount_call(void);
 
 #ifndef FTRACE_ADDR
kernel/trace/ftrace.c +119 -171

401 lines not on this page · raw message

kernel/trace/trace_events.c +1 -0
index e88f74f..43600a1 100644
@@ -1651,6 +1651,7 @@ function_test_events_call(unsigned long ip, unsigned long parent_ip)
 
 static struct ftrace_ops trace_ops __initdata  =
 {
+	.name = "trace_event",
 	.func = function_test_events_call,
 };
 
kernel/trace/trace_functions.c +2 -0
index 16aee4d..a991743 100644
@@ -148,11 +148,13 @@ function_stack_trace_call(unsigned long ip, unsigned long parent_ip)
 
 static struct ftrace_ops trace_ops __read_mostly =
 {
+	.name = "trace",
 	.func = function_trace_call,
 };
 
 static struct ftrace_ops trace_stack_ops __read_mostly =
 {
+	.name = "trace_stack",
 	.func = function_stack_trace_call,
 };
 
kernel/trace/trace_irqsoff.c +1 -0
index a4969b4..6983594 100644
@@ -152,6 +152,7 @@ irqsoff_tracer_call(unsigned long ip, unsigned long parent_ip)
 
 static struct ftrace_ops trace_ops __read_mostly =
 {
+	.name = "trace_irqs",
 	.func = irqsoff_tracer_call,
 };
 #endif /* CONFIG_FUNCTION_TRACER */
kernel/trace/trace_sched_wakeup.c +1 -0
index 7319559..f8b4622 100644
@@ -128,6 +128,7 @@ wakeup_tracer_call(unsigned long ip, unsigned long parent_ip)
 
 static struct ftrace_ops trace_ops __read_mostly =
 {
+	.name = "trace_wakeup",
 	.func = wakeup_tracer_call,
 };
 #endif /* CONFIG_FUNCTION_TRACER */
kernel/trace/trace_stack.c +1 -0
index 4c5dead..31f361e 100644
@@ -132,6 +132,7 @@ stack_trace_call(unsigned long ip, unsigned long parent_ip)
 
 static struct ftrace_ops trace_ops __read_mostly =
 {
+	.name = "trace_stack",
 	.func = stack_trace_call,
 };
 
Signature · 1 line
1.7.1
author
#2 lore raw

RFC 2/3 tracing: adding statical callers

In reply to Jiri Olsa

adding statically generated code for 'mcount callback' providing unique bitmask

---
 arch/x86/include/asm/ftrace.h |    2 ++
 arch/x86/kernel/entry_64.S    |   22 +++++++++++++++++-----
 arch/x86/kernel/ftrace.c      |   13 +++++++++++++
 include/linux/ftrace.h        |    4 +---
 kernel/trace/ftrace.c         |    2 +-
 5 files changed, 34 insertions(+), 9 deletions(-)
arch/x86/include/asm/ftrace.h +2 -0
index 45ae1fa..6ef436b 100644
@@ -2,6 +2,7 @@
 #define _ASM_X86_FTRACE_H
 
 #define FTRACE_TRACERS_MAX 4
+#define FTRACE_CALLERS_MAX ((1 << FTRACE_TRACERS_MAX) - 1)
 
 #ifdef __ASSEMBLY__
 
@@ -36,6 +37,7 @@
 
 #ifndef __ASSEMBLY__
 extern void mcount(void);
+extern unsigned long ftrace_callers[FTRACE_CALLERS_MAX];
 
 static inline unsigned long ftrace_call_adjust(unsigned long addr)
 {
arch/x86/kernel/entry_64.S +17 -5
index e0ba7d9..1ec1668 100644
@@ -69,19 +69,31 @@ ENTRY(mcount)
 	retq
 END(mcount)
 
-ENTRY(ftrace_caller)
+	.section .rodata,"a"
+ENTRY(ftrace_callers)
+	.section .entry.text
+.macro FTRACE_GEN_CALLERS num
+i=1
+.rept (1 << \num) - 1
+1:
 	cmpl $0, function_trace_stop
 	jne  ftrace_stub
-
 	MCOUNT_SAVE_FRAME
+	movq $i, %rdx
+	jmp ftrace_caller
+	.previous
+	.quad 1b
+	.section .entry.text
+i=i+1
+.endr
+.endm
+	FTRACE_GEN_CALLERS FTRACE_TRACERS_MAX
 
+ENTRY(ftrace_caller)
 	movq 0x38(%rsp), %rdi
 	movq 8(%rbp), %rsi
 	subq $MCOUNT_INSN_SIZE, %rdi
 
-	/* by default call all registered handlers */
-	movq $-1, %rdx
-
 	call ftrace_call
 
 	MCOUNT_RESTORE_FRAME
arch/x86/kernel/ftrace.c +13 -0
index dc92378..5a8e679 100644
@@ -328,6 +328,19 @@ int __init ftrace_dyn_arch_init(void *data)
 
 	return 0;
 }
+
+unsigned long ftrace_arch_tracer_addr(int mask)
+{
+	if (!mask)
+		return 0L;
+
+	/* tracer 1 is index 0 in ftrace_callers */
+	--mask;
+
+	BUG_ON(mask >= FTRACE_CALLERS_MAX);
+	return ftrace_callers[mask];
+}
+
 #endif
 
 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
include/linux/ftrace.h +1 -3
index b7ab09d..b12f5df 100644
@@ -180,10 +180,8 @@ extern void ftrace_caller(void);
 extern void ftrace_call(unsigned long ip, unsigned long parent_ip,
 			unsigned long flags);
 extern void mcount_call(void);
+extern unsigned long ftrace_arch_tracer_addr(int mask);
 
-#ifndef FTRACE_ADDR
-#define FTRACE_ADDR ((unsigned long)ftrace_caller)
-#endif
 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
 extern void ftrace_graph_caller(void);
 extern int ftrace_enable_ftrace_graph_caller(void);
kernel/trace/ftrace.c +1 -1
index f1eedda..b39dd52 100644
@@ -963,7 +963,7 @@ __ftrace_replace_code(struct dyn_ftrace *rec, int enable)
 	unsigned long ftrace_addr;
 	unsigned long flag = 0UL;
 
-	ftrace_addr = (unsigned long)FTRACE_ADDR;
+	ftrace_addr = ftrace_arch_tracer_addr(FTRACE_CALLERS_MAX);
 
 	/*
 	 * If this record is not to be traced or we want to disable it,
Signature · 1 line
1.7.1
author
#3 lore raw

RFC 3/3 tracing: set_ftrace_filter support

In reply to Jiri Olsa

adding support to use set_ftrace_filter for multiple tracers

---
 arch/x86/kernel/ftrace.c |   24 +++--
 include/linux/ftrace.h   |    3 +
 kernel/trace/ftrace.c    |  269 ++++++++++++++++++++++++++++++++++------------
 3 files changed, 219 insertions(+), 77 deletions(-)
arch/x86/kernel/ftrace.c +16 -8
index 5a8e679..5fdb40e 100644
@@ -76,18 +76,20 @@ static int ftrace_calc_offset(long ip, long addr)
 	return (int)(addr - ip);
 }
 
-static unsigned char *ftrace_call_replace(unsigned long ip, unsigned long addr)
+static unsigned char *ftrace_call_replace(unsigned long ip, unsigned long addr, int old)
 {
-	static union ftrace_code_union calc;
+	static union ftrace_code_union calc_new;
+	static union ftrace_code_union calc_old;
+	union ftrace_code_union *calc = old ? &calc_old : &calc_new;
 
-	calc.e8		= 0xe8;
-	calc.offset	= ftrace_calc_offset(ip + MCOUNT_INSN_SIZE, addr);
+	calc->e8	= 0xe8;
+	calc->offset	= ftrace_calc_offset(ip + MCOUNT_INSN_SIZE, addr);
 
 	/*
 	 * No locking needed, this must be called via kstop_machine
 	 * which in essence is like running on a uniprocessor machine.
 	 */
-	return calc.code;
+	return calc->code;
 }
 
 /*
@@ -304,9 +306,10 @@ int ftrace_make_nop(struct module *mod,
 	unsigned char *new, *old;
 	unsigned long ip = rec->ip;
 
-	old = ftrace_call_replace(ip, addr);
+	old = ftrace_call_replace(ip, addr, 1);
 	new = ftrace_nop_replace();
 
+	rec->addr = 0L;
 	return ftrace_modify_code(rec->ip, old, new);
 }
 
@@ -315,9 +318,14 @@ 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);
+	if (rec->addr)
+		old = ftrace_call_replace(ip, rec->addr, 1);
+	else
+		old = ftrace_nop_replace();
 
+	new = ftrace_call_replace(ip, addr, 0);
+
+	rec->addr = addr;
 	return ftrace_modify_code(rec->ip, old, new);
 }
 
include/linux/ftrace.h +3 -0
index b12f5df..9ce3484 100644
@@ -164,6 +164,9 @@ struct dyn_ftrace {
 		unsigned long		flags;
 		struct dyn_ftrace	*newlist;
 	};
+	unsigned long			filter;
+	unsigned long			no_trace;
+	unsigned long			addr;
 	struct dyn_arch_ftrace		arch;
 };
 
kernel/trace/ftrace.c +200 -69

449 lines not on this page · raw message

Signature · 1 line
1.7.1
maintainer
#4 lore raw

Re: [RFC 3/3] tracing: set_ftrace_filter support

Quoted 5 lines · Jiri Olsa
include/linux/ftrace.h +0 -0
index b12f5df..9ce3484 100644

include/linux/ftrace.h struct dyn_ftrace { · line 172

Quoted 9 lines
include/linux/ftrace.h +3 -0
@@ -164,6 +164,9 @@ struct dyn_ftrace {
                unsigned long           flags;
                struct dyn_ftrace       *newlist;
        };
+       unsigned long                   filter;
+       unsigned long                   no_trace;
+       unsigned long                   addr;
        struct dyn_arch_ftrace          arch;
 };
 

Big Fricken NACK!

Absolutely not! The dyn_ftrace structure is allocated for almost every single function in the kernel. Any function that can be traced has this structure allocated.

On my box I have:

# cat /debug/tracing/dyn_ftrace_total_info 
32605 0 0

That's 32,605 functions that can be traced, which turns to be 32,605 dyn_ftrace structs allocated. Now this number is after all modules have been loaded. You added 3 longs, on 64 bit boxes that's 24 bytes, you just increased the function tracer footprint by 782,520 bytes!

I'm working around not touching that structure, and I will continue to NAK any changes that increases its size. Actually, I did touch that structure, but I did not increase the size of it.

-- Steve

maintainer
#5 lore raw
In reply to Jiri Olsa
Quoted 5 lines · Jiri Olsa

hi, me again ;)

from last RFC email it turned out it'd be good to have support for more than 1 currently running function tracers. With the possibility to specify separate filter for each of them.

Jiri, I thought I told you to wait, as I'm basically doing the same thing. Except that I'm gutting the heck out of ftrace.c.

I've been working the last several days rewriting this code.

Quoted 5 lines

I thought about one way how to do that and put it to the code for considering. It seems to work properly, though I was fighting with set_ftrace_filter for a while.. and it is still not ideal ;)

Soon, set_ftrace_filter will only affect the function tracer and not any other user of function tracing.

I'm almost done. I was hoping to release something today, but it will probably have to wait till next week.

-- Steve

author
#6 lore raw
Quoted 9 lines · Steven Rostedt
Quoted 5 lines · Jiri Olsa

hi, me again ;)

from last RFC email it turned out it'd be good to have support for more than 1 currently running function tracers. With the possibility to specify separate filter for each of them.

Jiri, I thought I told you to wait, as I'm basically doing the same thing. Except that I'm gutting the heck out of ftrace.c.

yep, I was just keeping busy.. ;)
looking forward to see your change

jirka

Quoted 18 lines

I've been working the last several days rewriting this code.

Quoted 5 lines

I thought about one way how to do that and put it to the code for considering. It seems to work properly, though I was fighting with set_ftrace_filter for a while.. and it is still not ideal ;)

Soon, set_ftrace_filter will only affect the function tracer and not any other user of function tracing.

I'm almost done. I was hoping to release something today, but it will probably have to wait till next week.

-- Steve