Skip to contentSkip to conversation

tracing,x86_64 - function/graph trace without mcount/-pg/framepointer

RFC In review Jiri Olsa 4 participants 9 messages Feb 3, 2011 – Feb 4, 2011

Subsystems (5)
x86 Kbuild Tracing ftrace +1 more subsystems
lore
Share & tools
lore

Jiri Olsa

author
#0 lore raw

cover tracing,x86_64 - function/graph trace without mcount/-pg/framepointer

hi,

I recently saw the direct jump probing made for kprobes and tried to use it inside the trace framework.

The global idea is patching the function entry with direct jump to the trace code, instead of using pregenerated gcc profile code.

I started this just to see if it would be even possible to hook with new probing to the current trace code. It appears it's not that bad. I was able to run function and function_graph trace on x86_64.

For details on direct jumps probe, please check:
http://www.linuxinsight.com/ols2007-djprobe-kernel-probing-with-the-smallest-overhead.html

I realize using this way to hook the functions has some drawbacks, from what I can see it's roughly:
- no all functions could be patched
- need to find a way to say which function is safe to patch
- memory consumption for detour buffers and symbol records

but seems there're some advantages as well:
- trace code could be in a module
- no profiling code is needed
- framepointer can be disabled (framepointer is needed for generating profile code)

As for the attached implementation it's hack mostly (expect bugs), especially the ftrace/kprobe integration could be probably done better. It's only for x86_64.

It can be used like this:

- new menu config item is added (function tracer engine), to choose mcount or ktrace
- new file "ktrace" is added to the tracing dir
- to add symbols to trace run:
echo mutex_unlock > ./ktrace
echo mutex_lock >> ./ktrace
- to display trace symbols:
cat ktrace
- to enable the trace, the usual is needed:
echo function > ./current_tracer
echo function_graph > ./current_tracer
- to remove symbols from trace:
echo nop > ./current_tracer
echo > ./ktrace
- if the function is added while the tracer is running, the symbol is enabled automatically.
- only all symbols could be removed and only if there's no tracer running.

I'm not sure how to choose from kallsyms interface what function is safe to patch, so I omit patching of all symbols so far.

attached patches:
1/4 - kprobe - ktrace instruction slot cache interface
using kprobe detour buffer allocation, adding interface to use it from trace framework

2/4 - tracing - adding size parameter to do_ftrace_mod_code
adding size parameter to be able to restore the saved instructions, which could be longer than relative call

3/4 - ktrace - function trace support
adding ktrace support with function tracer

4/4 - ktrace - function trace support
adding function graph support

please let me know what you think, thanks
jirka

---
 Makefile                   |    2 +-
 arch/x86/Kconfig           |    4 +-
 arch/x86/kernel/Makefile   |    1 +
 arch/x86/kernel/entry_64.S |   50 +++++++
 arch/x86/kernel/ftrace.c   |  157 +++++++++++----------
 arch/x86/kernel/ktrace.c   |  256 ++++++++++++++++++++++++++++++++++
 include/linux/ftrace.h     |   36 +++++-
 include/linux/kprobes.h    |    8 +
 kernel/kprobes.c           |   33 +++++
 kernel/trace/Kconfig       |   28 ++++-
 kernel/trace/Makefile      |    1 +
 kernel/trace/ftrace.c      |   21 +++
 kernel/trace/ktrace.c      |  330 ++++++++++++++++++++++++++++++++++++++++++++
 kernel/trace/trace.c       |    1 +
 14 files changed, 846 insertions(+), 82 deletions(-)
author
#1 lore raw

PATCH 1/4 kprobe - ktrace instruction slot cache interface

using kprobe detour buffer allocation, adding interface to use it from trace framework

wbr,
jirka

---
 include/linux/kprobes.h |    8 ++++++++
 kernel/kprobes.c        |   33 +++++++++++++++++++++++++++++++++
 2 files changed, 41 insertions(+), 0 deletions(-)
include/linux/kprobes.h +8 -0
index dd7c12e..1e984e9 100644
@@ -436,4 +436,12 @@ static inline int enable_jprobe(struct jprobe *jp)
 	return enable_kprobe(&jp->kp);
 }
 
+#ifdef CONFIG_KTRACE
+
+extern kprobe_opcode_t __kprobes *get_ktrace_insn_slot(void);
+extern void __kprobes free_ktrace_insn_slot(kprobe_opcode_t * slot, int dirty);
+extern void __init ktrace_insn_init(int size);
+
+#endif /* CONFIG_KTRACE */
+
 #endif /* _LINUX_KPROBES_H */
kernel/kprobes.c +33 -0
index 7798181..5bc31d6 100644
@@ -285,6 +285,39 @@ void __kprobes free_insn_slot(kprobe_opcode_t * slot, int dirty)
 	__free_insn_slot(&kprobe_insn_slots, slot, dirty);
 	mutex_unlock(&kprobe_insn_mutex);
 }
+
+#ifdef CONFIG_KTRACE
+static DEFINE_MUTEX(ktrace_insn_mutex);
+static struct kprobe_insn_cache ktrace_insn_slots = {
+	.pages = LIST_HEAD_INIT(ktrace_insn_slots.pages),
+	.insn_size = MAX_INSN_SIZE,
+	.nr_garbage = 0,
+};
+
+kprobe_opcode_t __kprobes *get_ktrace_insn_slot(void)
+{
+	kprobe_opcode_t *ret = NULL;
+
+	mutex_lock(&ktrace_insn_mutex);
+	ret = __get_insn_slot(&ktrace_insn_slots);
+	mutex_unlock(&ktrace_insn_mutex);
+
+	return ret;
+}
+
+void __kprobes free_ktrace_insn_slot(kprobe_opcode_t * slot, int dirty)
+{
+	mutex_lock(&ktrace_insn_mutex);
+	__free_insn_slot(&ktrace_insn_slots, slot, dirty);
+	mutex_unlock(&ktrace_insn_mutex);
+}
+
+void __init ktrace_insn_init(int size)
+{
+	ktrace_insn_slots.insn_size = size;
+}
+#endif /* CONFIG_KTRACE */
+
 #ifdef CONFIG_OPTPROBES
 /* For optimized_kprobe buffer */
 static DEFINE_MUTEX(kprobe_optinsn_mutex); /* Protects kprobe_optinsn_slots */
Signature · 1 line
1.7.1
author
#2 lore raw

PATCH 2/4 tracing - adding size parameter to do_ftrace_mod_code

In reply to Jiri Olsa

adding size parameter to be able to restore the saved instructions, which could be longer than relative call

wbr,
jirka

---
 arch/x86/kernel/ftrace.c |   10 ++++++----
 1 files changed, 6 insertions(+), 4 deletions(-)
arch/x86/kernel/ftrace.c +6 -4
index 382eb29..979ec14 100644
@@ -124,6 +124,7 @@ static atomic_t nmi_running = ATOMIC_INIT(0);
 static int mod_code_status;		/* holds return value of text write */
 static void *mod_code_ip;		/* holds the IP to write to */
 static void *mod_code_newcode;		/* holds the text to write to the IP */
+static int mod_code_size;		/* holds the size of the new code */
 
 static unsigned nmi_wait_count;
 static atomic_t nmi_update_count = ATOMIC_INIT(0);
@@ -161,7 +162,7 @@ static void ftrace_mod_code(void)
 	 * to succeed, then they all should.
 	 */
 	mod_code_status = probe_kernel_write(mod_code_ip, mod_code_newcode,
-					     MCOUNT_INSN_SIZE);
+					     mod_code_size);
 
 	/* if we fail, then kill any new writers */
 	if (mod_code_status)
@@ -225,7 +226,7 @@ within(unsigned long addr, unsigned long start, unsigned long end)
 }
 
 static int
-do_ftrace_mod_code(unsigned long ip, void *new_code)
+do_ftrace_mod_code(unsigned long ip, void *new_code, int size)
 {
 	/*
 	 * On x86_64, kernel text mappings are mapped read-only with
@@ -240,6 +241,7 @@ do_ftrace_mod_code(unsigned long ip, void *new_code)
 
 	mod_code_ip = (void *)ip;
 	mod_code_newcode = new_code;
+	mod_code_size = size;
 
 	/* The buffers need to be visible before we let NMIs write them */
 	smp_mb();
@@ -290,7 +292,7 @@ ftrace_modify_code(unsigned long ip, unsigned char *old_code,
 		return -EINVAL;
 
 	/* replace the text with the new text */
-	if (do_ftrace_mod_code(ip, new_code))
+	if (do_ftrace_mod_code(ip, new_code, MCOUNT_INSN_SIZE))
 		return -EPERM;
 
 	sync_core();
@@ -361,7 +363,7 @@ static int ftrace_mod_jmp(unsigned long ip,
 
 	*(int *)(&code[1]) = new_offset;
 
-	if (do_ftrace_mod_code(ip, &code))
+	if (do_ftrace_mod_code(ip, &code, MCOUNT_INSN_SIZE))
 		return -EPERM;
 
 	return 0;
Signature · 1 line
1.7.1
author
#3 lore raw

PATCH 3/4 ktrace - function trace support

In reply to Jiri Olsa

adding ktrace support with function tracer

wbr,
jirka

---
 Makefile                   |    2 +-
 arch/x86/Kconfig           |    2 +-
 arch/x86/kernel/Makefile   |    1 +
 arch/x86/kernel/entry_64.S |   23 +++
 arch/x86/kernel/ftrace.c   |  153 +++++++++++----------
 arch/x86/kernel/ktrace.c   |  256 ++++++++++++++++++++++++++++++++++
 include/linux/ftrace.h     |   36 +++++-
 kernel/trace/Kconfig       |   28 ++++-
 kernel/trace/Makefile      |    1 +
 kernel/trace/ftrace.c      |   11 ++
 kernel/trace/ktrace.c      |  330 ++++++++++++++++++++++++++++++++++++++++++++
 kernel/trace/trace.c       |    1 +
 12 files changed, 764 insertions(+), 80 deletions(-)
 create mode 100644 arch/x86/kernel/ktrace.c
 create mode 100644 kernel/trace/ktrace.c
Makefile +1 -1
index 66e7e97..26d3d60 100644
@@ -577,7 +577,7 @@ ifdef CONFIG_DEBUG_INFO_REDUCED
 KBUILD_CFLAGS 	+= $(call cc-option, -femit-struct-debug-baseonly)
 endif
 
-ifdef CONFIG_FUNCTION_TRACER
+ifdef CONFIG_FTRACE_MCOUNT_RECORD
 KBUILD_CFLAGS	+= -pg
 ifdef CONFIG_DYNAMIC_FTRACE
 	ifdef CONFIG_HAVE_C_RECORDMCOUNT
arch/x86/Kconfig +1 -1
index 95c36c4..a02718c 100644
@@ -38,7 +38,7 @@ config X86
 	select HAVE_FUNCTION_GRAPH_TRACER
 	select HAVE_FUNCTION_GRAPH_FP_TEST
 	select HAVE_FUNCTION_TRACE_MCOUNT_TEST
-	select HAVE_FTRACE_NMI_ENTER if DYNAMIC_FTRACE
+	select HAVE_FTRACE_NMI_ENTER if DYNAMIC_FTRACE || KTRACE
 	select HAVE_SYSCALL_TRACEPOINTS
 	select HAVE_KVM
 	select HAVE_ARCH_KGDB
arch/x86/kernel/Makefile +1 -0
index 34244b2..b664584 100644
@@ -73,6 +73,7 @@ obj-$(CONFIG_X86_TRAMPOLINE)	+= trampoline_$(BITS).o
 obj-$(CONFIG_X86_MPPARSE)	+= mpparse.o
 obj-y				+= apic/
 obj-$(CONFIG_X86_REBOOTFIXUPS)	+= reboot_fixups_32.o
+obj-$(CONFIG_KTRACE)		+= ktrace.o
 obj-$(CONFIG_DYNAMIC_FTRACE)	+= ftrace.o
 obj-$(CONFIG_FUNCTION_GRAPH_TRACER) += ftrace.o
 obj-$(CONFIG_FTRACE_SYSCALLS)	+= ftrace.o
arch/x86/kernel/entry_64.S +23 -0
index aed1ffb..4d70019 100644
@@ -62,6 +62,29 @@
 
 	.code64
 #ifdef CONFIG_FUNCTION_TRACER
+#ifdef CONFIG_KTRACE
+ENTRY(ktrace_callback)
+	cmpl $0, function_trace_stop
+	jne  ftrace_stub
+
+	cmpq $ftrace_stub, ftrace_trace_function
+	jnz ktrace_trace
+	retq
+
+ktrace_trace:
+	MCOUNT_SAVE_FRAME
+
+	movq 0x48(%rsp), %rdi
+	movq 0x50(%rsp), %rsi
+
+	call   *ftrace_trace_function
+
+	MCOUNT_RESTORE_FRAME
+
+	retq
+END(ktrace_callback)
+#endif /* CONFIG_KTRACE */
+
 #ifdef CONFIG_DYNAMIC_FTRACE
 ENTRY(mcount)
 	retq
arch/x86/kernel/ftrace.c +80 -73

189 lines not on this page · raw message

arch/x86/kernel/ktrace.c new file +256 -0

256 lines not on this page · raw message

include/linux/ftrace.h +33 -3
index dcd6a7c..11c3d5b 100644
@@ -116,9 +116,6 @@ struct ftrace_func_command {
 
 #ifdef CONFIG_DYNAMIC_FTRACE
 
-int ftrace_arch_code_modify_prepare(void);
-int ftrace_arch_code_modify_post_process(void);
-
 struct seq_file;
 
 struct ftrace_probe_ops {
@@ -530,4 +527,37 @@ unsigned long arch_syscall_addr(int nr);
 
 #endif /* CONFIG_FTRACE_SYSCALLS */
 
+#ifdef CONFIG_KTRACE
+enum {
+	KTRACE_ENABLE,
+	KTRACE_DISABLE
+};
+
+struct ktrace_symbol {
+        struct list_head list;
+        int enabled;
+
+        u8 *addr;
+        u8 *insn_templ;
+        u8 *insn_saved;
+        int insn_saved_size;
+};
+
+extern void ktrace_init(void);
+extern int ktrace_init_template(struct ktrace_symbol *ksym);
+extern int ktrace_arch_init(void);
+extern void ktrace_startup(void);
+extern void ktrace_shutdown(void);
+extern void ktrace_enable_sym(struct ktrace_symbol *ksym);
+extern void ktrace_disable_sym(struct ktrace_symbol *ksym);
+#else
+static inline void ktrace_init(void) {}
+#endif /* CONFIG_KTRACE */
+
+#if defined CONFIG_DYNAMIC_FTRACE || defined CONFIG_KTRACE
+extern int do_ftrace_mod_code(unsigned long ip, void *new_code, int size);
+extern int ftrace_arch_code_modify_prepare(void);
+extern int ftrace_arch_code_modify_post_process(void);
+#endif
+
 #endif /* _LINUX_FTRACE_H */
kernel/trace/Kconfig +26 -2
index 14674dc..1cf0aba 100644
@@ -140,8 +140,6 @@ if FTRACE
 
 config FUNCTION_TRACER
 	bool "Kernel Function Tracer"
-	depends on HAVE_FUNCTION_TRACER
-	select FRAME_POINTER if !ARM_UNWIND && !S390
 	select KALLSYMS
 	select GENERIC_TRACER
 	select CONTEXT_SWITCH_TRACER
@@ -168,6 +166,30 @@ config FUNCTION_GRAPH_TRACER
 	  the return value. This is done by setting the current return
 	  address on the current task structure into a stack of calls.
 
+config KTRACE
+	bool
+	depends on FTRACER_ENG_KTRACE
+
+choice
+	prompt "Function trace engine"
+	default FTRACER_ENG_MCOUNT_RECORD
+	depends on FUNCTION_TRACER
+
+config FTRACER_ENG_MCOUNT_RECORD
+	bool "mcount"
+	depends on HAVE_FUNCTION_TRACER
+	select FRAME_POINTER if !ARM_UNWIND && !S390
+	help
+	  standard -pg mcount record generation
+
+config FTRACER_ENG_KTRACE
+	bool "ktrace"
+	select KTRACE
+	help
+	  dynamic call probes
+
+endchoice
+
 
 config IRQSOFF_TRACER
 	bool "Interrupts-off Latency Tracer"
@@ -389,6 +411,7 @@ config DYNAMIC_FTRACE
 	bool "enable/disable ftrace tracepoints dynamically"
 	depends on FUNCTION_TRACER
 	depends on HAVE_DYNAMIC_FTRACE
+	depends on FTRACER_ENG_MCOUNT_RECORD
 	default y
 	help
           This option will modify all the calls to ftrace dynamically
@@ -422,6 +445,7 @@ config FTRACE_MCOUNT_RECORD
 	def_bool y
 	depends on DYNAMIC_FTRACE
 	depends on HAVE_FTRACE_MCOUNT_RECORD
+	depends on FTRACER_ENG_MCOUNT_RECORD
 
 config FTRACE_SELFTEST
 	bool
kernel/trace/Makefile +1 -0
index 761c510..f557200 100644
@@ -21,6 +21,7 @@ endif
 #
 obj-y += trace_clock.o
 
+obj-$(CONFIG_KTRACE) += ktrace.o
 obj-$(CONFIG_FUNCTION_TRACER) += libftrace.o
 obj-$(CONFIG_RING_BUFFER) += ring_buffer.o
 obj-$(CONFIG_RING_BUFFER_BENCHMARK) += ring_buffer_benchmark.o
kernel/trace/ftrace.c +11 -0
index f3dadae..762e2b3 100644
@@ -3152,7 +3152,12 @@ int register_ftrace_function(struct ftrace_ops *ops)
 	mutex_lock(&ftrace_lock);
 
 	ret = __register_ftrace_function(ops);
+
+#ifdef CONFIG_KTRACE
+	ktrace_startup();
+#else
 	ftrace_startup(0);
+#endif
 
 	mutex_unlock(&ftrace_lock);
 	return ret;
@@ -3170,7 +3175,13 @@ int unregister_ftrace_function(struct ftrace_ops *ops)
 
 	mutex_lock(&ftrace_lock);
 	ret = __unregister_ftrace_function(ops);
+
+#ifdef CONFIG_KTRACE
+	ktrace_shutdown();
+#else
 	ftrace_shutdown(0);
+#endif
+
 	mutex_unlock(&ftrace_lock);
 
 	return ret;
kernel/trace/ktrace.c new file +330 -0

330 lines not on this page · raw message

kernel/trace/trace.c +1 -0
index dc53ecb..b901c94 100644
@@ -4361,6 +4361,7 @@ static __init int tracer_init_debugfs(void)
 	for_each_tracing_cpu(cpu)
 		tracing_init_debugfs_percpu(cpu);
 
+	ktrace_init();
 	return 0;
 }
 
Signature · 1 line
1.7.1
author
#4 lore raw

PATCH 4/4 ktrace - function graph trace support

In reply to Jiri Olsa

adding function graph support

wbr,
jirka

---
 arch/x86/Kconfig           |    2 +-
 arch/x86/kernel/entry_64.S |   27 +++++++++++++++++++++++++++
 kernel/trace/ftrace.c      |   10 ++++++++++
 3 files changed, 38 insertions(+), 1 deletions(-)
arch/x86/Kconfig +1 -1
index a02718c..befe1e0 100644
@@ -36,7 +36,7 @@ config X86
 	select HAVE_DYNAMIC_FTRACE
 	select HAVE_FUNCTION_TRACER
 	select HAVE_FUNCTION_GRAPH_TRACER
-	select HAVE_FUNCTION_GRAPH_FP_TEST
+	select HAVE_FUNCTION_GRAPH_FP_TEST if !KTRACE
 	select HAVE_FUNCTION_TRACE_MCOUNT_TEST
 	select HAVE_FTRACE_NMI_ENTER if DYNAMIC_FTRACE || KTRACE
 	select HAVE_SYSCALL_TRACEPOINTS
arch/x86/kernel/entry_64.S +27 -0
index 4d70019..ec9e234 100644
@@ -69,6 +69,14 @@ ENTRY(ktrace_callback)
 
 	cmpq $ftrace_stub, ftrace_trace_function
 	jnz ktrace_trace
+
+#ifdef CONFIG_FUNCTION_GRAPH_TRACER
+	cmpq $ftrace_stub, ftrace_graph_return
+	jnz ktrace_graph_caller
+
+	cmpq $ftrace_graph_entry_stub, ftrace_graph_entry
+	jnz ktrace_graph_caller
+#endif
 	retq
 
 ktrace_trace:
@@ -83,6 +91,25 @@ ktrace_trace:
 
 	retq
 END(ktrace_callback)
+
+#ifdef CONFIG_FUNCTION_GRAPH_TRACER
+ENTRY(ktrace_graph_caller)
+	cmpl $0, function_trace_stop
+	jne ftrace_stub
+
+	MCOUNT_SAVE_FRAME
+
+	leaq 0x50(%rsp), %rdi
+	movq 0x48(%rsp), %rsi
+	movq $0, %rdx
+
+	call	prepare_ftrace_return
+
+	MCOUNT_RESTORE_FRAME
+
+	retq
+END(ktrace_graph_caller)
+#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
 #endif /* CONFIG_KTRACE */
 
 #ifdef CONFIG_DYNAMIC_FTRACE
kernel/trace/ftrace.c +10 -0
index 762e2b3..f6e30a8 100644
@@ -3404,7 +3404,11 @@ int register_ftrace_graph(trace_func_graph_ret_t retfunc,
 	ftrace_graph_return = retfunc;
 	ftrace_graph_entry = entryfunc;
 
+#ifdef CONFIG_KTRACE
+	ktrace_startup();
+#else
 	ftrace_startup(FTRACE_START_FUNC_RET);
+#endif
 
 out:
 	mutex_unlock(&ftrace_lock);
@@ -3421,7 +3425,13 @@ void unregister_ftrace_graph(void)
 	ftrace_graph_active--;
 	ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
 	ftrace_graph_entry = ftrace_graph_entry_stub;
+
+#ifdef CONFIG_KTRACE
+	ktrace_shutdown();
+#else
 	ftrace_shutdown(FTRACE_STOP_FUNC_RET);
+#endif
+
 	unregister_pm_notifier(&ftrace_suspend_notifier);
 	unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
 
Signature · 1 line
1.7.1
maintainer
#5 lore raw
In reply to Jiri Olsa
Quoted 8 lines · Jiri Olsa

hi,

I recently saw the direct jump probing made for kprobes and tried to use it inside the trace framework.

The global idea is patching the function entry with direct jump to the trace code, instead of using pregenerated gcc profile code.

Interesting, but ideally, it would be nice if gcc provided a better "mcount" mechanism. One that calls mcount (or whatever new name it would have) before it does anything with the stack.

Quoted 6 lines

I started this just to see if it would be even possible to hook with new probing to the current trace code. It appears it's not that bad. I was able to run function and function_graph trace on x86_64.

Quoted 7 lines

For details on direct jumps probe, please check:
http://www.linuxinsight.com/ols2007-djprobe-kernel-probing-with-the-smallest-overhead.html

I realize using this way to hook the functions has some drawbacks, from what I can see it's roughly:
- no all functions could be patched

What's the reason for not all functions?

Quoted 5 lines

- need to find a way to say which function is safe to patch
- memory consumption for detour buffers and symbol records

but seems there're some advantages as well:
- trace code could be in a module

What makes this allow module code?

ftrace could do that now, but it would require a separate handler. I would need to disable preemption before calling the module code function handler.

Quoted 3 lines

- no profiling code is needed
- framepointer can be disabled (framepointer is needed for generating profile code)

Again ideally, gcc should fix this.

-- Steve

#6 lore raw
Quoted 24 lines · Steven Rostedt
Quoted 8 lines · Jiri Olsa

hi,

I recently saw the direct jump probing made for kprobes and tried to use it inside the trace framework.

The global idea is patching the function entry with direct jump to the trace code, instead of using pregenerated gcc profile code.

Interesting, but ideally, it would be nice if gcc provided a better "mcount" mechanism. One that calls mcount (or whatever new name it would have) before it does anything with the stack.

Quoted 10 lines

I started this just to see if it would be even possible to hook with new probing to the current trace code. It appears it's not that bad. I was able to run function and function_graph trace on x86_64.

For details on direct jumps probe, please check:
http://www.linuxinsight.com/ols2007-djprobe-kernel-probing-with-the-smallest-overhead.html

Quoted 5 lines
Quoted 3 lines

I realize using this way to hook the functions has some drawbacks, from what I can see it's roughly:
- no all functions could be patched

What's the reason for not all functions?

Because of those that kprobes calls, so to avoid recursion. kprobes has some recursion detection mechanism, IIRC, but until we reach that checkpoint, I think there are some functions in the path.

Well, ftrace has the same problem. That's just due to the nature of function tracing.

There may be some places too fragile to use kprobes there too.

Ah, the whole trap path for example :-(

Quoted 11 lines
Quoted 5 lines

- need to find a way to say which function is safe to patch
- memory consumption for detour buffers and symbol records

but seems there're some advantages as well:
- trace code could be in a module

What makes this allow module code?

ftrace could do that now, but it would require a separate handler. I would need to disable preemption before calling the module code function handler.

Kprobes takes care of handlers from modules already.
I'm not sure we want that, it makes the tracing code more sensitive.

Look, for example I think kprobes doesn't trace kernel faults path because module space is allocated through vmalloc (hmm, is it still the case?).

Quoted 5 lines
Quoted 3 lines

- no profiling code is needed
- framepointer can be disabled (framepointer is needed for generating profile code)

Again ideally, gcc should fix this.

As another drawback of using kprobes, there is also the overhead. I can't imagine a trap triggering for every functions. But then yeah we have the jmp optimisation. But then it needs that detour buffer that we can avoid with mcount.

So like Steve I think mcount is still a better backend for function tracing. More optimized by nature, even though it indeed needs some fixes.

maintainer
#7 lore raw
Quoted 6 lines · Frederic Weisbecker
Quoted 3 lines

ftrace could do that now, but it would require a separate handler. I would need to disable preemption before calling the module code function handler.

Kprobes takes care of handlers from modules already.
I'm not sure we want that, it makes the tracing code more sensitive.

Masami,

I'm looking at the optimize code, particularly kprobes_optinsn_template_holder(), which looks to be the template that is called on optimized kprobes. I don't see where preemption or interrupts are disabled when a probe is called.

If modules can register probes, and we can call it in any arbitrary location of the kernel, then preemption must be disabled prior to calling the module code. Otherwise you risk crashing the system on module unload.

module:
-------
register_kprobe(probe);

Core:
-----
hit break point
call probe

module:
-------
in probe function
preempted

module:
-------
unregister_kprobe(probe);
stop_machine();
<module unloaded>

Core:
-----
module <zombie>:
----------------
gets CPU again
executes module code that's been freed
DEATH BY ZOMBIES

Maybe I missed something. But does the optimize kprobes disable preemption or interrupts before calling the optimized probe?

-- Steve

#8 lore raw
In reply to Jiri Olsa

Hi,

Quoted 9 lines · Jiri Olsa

hi,

I recently saw the direct jump probing made for kprobes and tried to use it inside the trace framework.

The global idea is patching the function entry with direct jump to the trace code, instead of using pregenerated gcc profile code.

Quoted 7 lines

I started this just to see if it would be even possible to hook with new probing to the current trace code. It appears it's not that bad. I was able to run function and function_graph trace on x86_64.

For details on direct jumps probe, please check:
http://www.linuxinsight.com/ols2007-djprobe-kernel-probing-with-the-smallest-overhead.html

Thank you for referring it ;-)

Quoted 3 lines

I realize using this way to hook the functions has some drawbacks, from what I can see it's roughly:
- no all functions could be patched

Yeah, that is why the "djprobe" becomes "optprobe". If kprobe finds there is no space to patch, it just fallback to a breakpoint. Since this check is done internally, kprobes user takes this benefit transparently ( don't need to change user's code).

Quoted 2 lines

- need to find a way to say which function is safe to patch
- memory consumption for detour buffers and symbol records

And also, you can't patch more than two instructions without int3 bypass method (or special stack checker), because a processor can run and may have been interrupted on the 2nd instruction when stop_machine is issued.
That's the 2nd reason why the djprobe is a part of kprobes. this "int3 bypass" method disallow you to probe NMI handlers, since int3 inside NMI will clear additional NMI masking by issuing IRET.

Quoted 5 lines

but seems there're some advantages as well:
- trace code could be in a module
- no profiling code is needed
- framepointer can be disabled (framepointer is needed for generating profile code)

nowadays profiling code with dynamic ftrace will not make visible overhead, and if you need to do that without profiling binary, you can already use kprobe-tracer for it. (Using kprobe-tracer via perf-probe allows you to probe not
only actual function but also inlined function entry ;-))

Thank you,

Quoted 71 lines

As for the attached implementation it's hack mostly (expect bugs), especially the ftrace/kprobe integration could be probably done better. It's only for x86_64.

It can be used like this:

- new menu config item is added (function tracer engine), to choose mcount or ktrace
- new file "ktrace" is added to the tracing dir
- to add symbols to trace run:
echo mutex_unlock > ./ktrace
echo mutex_lock >> ./ktrace
- to display trace symbols:
cat ktrace
- to enable the trace, the usual is needed:
echo function > ./current_tracer
echo function_graph > ./current_tracer
- to remove symbols from trace:
echo nop > ./current_tracer
echo > ./ktrace
- if the function is added while the tracer is running, the symbol is enabled automatically.
- only all symbols could be removed and only if there's no tracer running.

I'm not sure how to choose from kallsyms interface what function is safe to patch, so I omit patching of all symbols so far.

attached patches:
1/4 - kprobe - ktrace instruction slot cache interface
using kprobe detour buffer allocation, adding interface to use it from trace framework

2/4 - tracing - adding size parameter to do_ftrace_mod_code
adding size parameter to be able to restore the saved instructions, which could be longer than relative call

3/4 - ktrace - function trace support
adding ktrace support with function tracer

4/4 - ktrace - function trace support
adding function graph support

please let me know what you think, thanks
jirka

---
 Makefile                   |    2 +-
 arch/x86/Kconfig           |    4 +-
 arch/x86/kernel/Makefile   |    1 +
 arch/x86/kernel/entry_64.S |   50 +++++++
 arch/x86/kernel/ftrace.c   |  157 +++++++++++----------
 arch/x86/kernel/ktrace.c   |  256 ++++++++++++++++++++++++++++++++++
 include/linux/ftrace.h     |   36 +++++-
 include/linux/kprobes.h    |    8 +
 kernel/kprobes.c           |   33 +++++
 kernel/trace/Kconfig       |   28 ++++-
 kernel/trace/Makefile      |    1 +
 kernel/trace/ftrace.c      |   21 +++
 kernel/trace/ktrace.c      |  330 ++++++++++++++++++++++++++++++++++++++++++++
 kernel/trace/trace.c       |    1 +
 14 files changed, 846 insertions(+), 82 deletions(-)

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/

Signature · 4 lines
Masami HIRAMATSU
2nd Dept. Linux Technology Center
Hitachi, Ltd., Systems Development Laboratory
E-mail: [email protected]