Skip to contentSkip to conversation

ftrace: support very early function tracing

RFC PATCH Superseded Abderrahmane Benbachir 1 participant 1 message Nov 8, 2017

Subsystems (3)

v2 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

RFC PATCH v2 1/1 ftrace: support very early function tracing

Hi Steve, here is the latest changes and fixes of v2, there are 3 points that still need to be fixed:

1) Use early boot timestamps : I removed arch specific code and use the default clock.

2) Make filters depends on FTRACE_MCOUNT_RECORD instead of CONFIG_DYNAMIC_FTRACE, so we can use filtering without relying on dynamic config, because we dont do live patching.

But FTRACE_MCOUNT_RECORD depends on CONFIG_DYNAMIC_FTRACE, should we break this dependency ?

3) Use ring_buffer_set_clock, then timestamps got changed when enabling function tracing :

[0.340111]   <idle>-0  0dp.. 18446744073671635us : mem_cache_init <-start_kernel
[0.340622]   <idle>-0  0dp.. 18446744073671775us : vmalloc_init <-start_kernel

ftrace=function started here

[0.341119]   <idle>-0  0dp.1 48004us : sched_init <-start_kernel
[0.341520]   <idle>-0  0dp.1 48005us : wait_bit_init <-sched

--Abder

PATCH v2 improved
---------------------------------------------------------------------
The very early tracing will start from the beginning of start_kernel() and will stop at ftrace_init()

start_kernel()
{
ftrace_early_init() <--- start very early tracing
...
(calls)
...
ftrace_init() <--- stop very early tracing
early_trace_init(); <--- fill ring buffer, start function tracer if on
...
}

The events are placed in a temporary buffer, which will be copied to the trace buffer after memory setup.

Dynamic tracing is not implemented with live patching, we use ftrace_filter and ftrace_notrace to find which functions to be filtered (traced / not traced), then during the callback from mcount hook, we do binary search lookup to decide which function to be save or not.

Cc Steven Rostedt <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Mathieu Desnoyers <[email protected]>
---
 arch/x86/Kconfig            |   1 +
 arch/x86/kernel/ftrace_32.S |  47 +++++--
 arch/x86/kernel/ftrace_64.S |  14 +++
 include/linux/ftrace.h      |  16 ++-
 init/main.c                 |   1 +
 kernel/trace/Kconfig        |  51 ++++++++
 kernel/trace/ftrace.c       | 295 +++++++++++++++++++++++++++++++++++++++++++-
 kernel/trace/trace.c        |   3 +
 8 files changed, 414 insertions(+), 14 deletions(-)
arch/x86/Kconfig +1 -0
index 2fdb23313dd5..c74870150c54 100644
@@ -138,6 +138,7 @@ config X86
 	select HAVE_FENTRY			if X86_64 || DYNAMIC_FTRACE
 	select HAVE_FTRACE_MCOUNT_RECORD
 	select HAVE_FUNCTION_GRAPH_TRACER
+	select HAVE_VERY_EARLY_FTRACE
 	select HAVE_FUNCTION_TRACER
 	select HAVE_GCC_PLUGINS
 	select HAVE_HW_BREAKPOINT
arch/x86/kernel/ftrace_32.S +37 -10

66 lines not on this page · raw message

arch/x86/kernel/ftrace_64.S +14 -0
index c832291d948a..a2949701771f 100644
@@ -147,7 +147,21 @@ EXPORT_SYMBOL(mcount)
 #ifdef CONFIG_DYNAMIC_FTRACE
 
 ENTRY(function_hook)
+#ifdef CONFIG_VERY_EARLY_FUNCTION_TRACER
+	cmpq $ftrace_stub, ftrace_vearly_trace_function
+	jnz vearly_trace
+
+vearly_stub:
 	retq
+
+vearly_trace:
+	save_mcount_regs
+	call *ftrace_vearly_trace_function
+	restore_mcount_regs
+	jmp vearly_stub
+#else
+	retq
+#endif
 END(function_hook)
 
 ENTRY(ftrace_caller)
include/linux/ftrace.h +15 -1
index e54d257983f2..b11f0169f2c9 100644
@@ -273,6 +273,16 @@ static inline void ftrace_kill(void) { }
 static inline void ftrace_free_init_mem(void) { }
 #endif /* CONFIG_FUNCTION_TRACER */
 
+#ifdef CONFIG_VERY_EARLY_FUNCTION_TRACER
+extern void ftrace_early_init(char *command_line);
+extern void ftrace_early_shutdown(void);
+extern void ftrace_early_fill_ringbuffer(void *data);
+#else
+static inline void ftrace_early_init(char *command_line) { }
+static inline void ftrace_early_shutdown(void) { }
+static inline void ftrace_early_fill_ringbuffer(void *data) { }
+#endif
+
 #ifdef CONFIG_STACK_TRACER
 
 #define STACK_TRACE_ENTRIES 500
@@ -477,6 +487,10 @@ unsigned long ftrace_get_addr_curr(struct dyn_ftrace *rec);
 
 extern ftrace_func_t ftrace_trace_function;
 
+#if defined(CONFIG_VERY_EARLY_FUNCTION_TRACER) && defined(CONFIG_DYNAMIC_FTRACE)
+extern ftrace_func_t ftrace_vearly_trace_function;
+#endif
+
 int ftrace_regex_open(struct ftrace_ops *ops, int flag,
 		  struct inode *inode, struct file *file);
 ssize_t ftrace_filter_write(struct file *file, const char __user *ubuf,
@@ -758,7 +772,7 @@ static inline unsigned long get_lock_parent_ip(void)
 #ifdef CONFIG_FTRACE_MCOUNT_RECORD
 extern void ftrace_init(void);
 #else
-static inline void ftrace_init(void) { }
+static inline void ftrace_init(void) { ftrace_early_shutdown(); }
 #endif
 
 /*
init/main.c +1 -0
index 0ee9c6866ada..c794292b18ef 100644
@@ -511,6 +511,7 @@ asmlinkage __visible void __init start_kernel(void)
 	char *command_line;
 	char *after_dashes;
 
+	ftrace_early_init(boot_command_line);
 	set_task_stack_end_magic(&init_task);
 	smp_setup_processor_id();
 	debug_objects_early_init();
kernel/trace/Kconfig +51 -0

63 lines not on this page · raw message

kernel/trace/ftrace.c +292 -3

319 lines not on this page · raw message

kernel/trace/trace.c +3 -0
index 752e5daf0896..c097b97b3025 100644
@@ -8364,6 +8364,9 @@ __init static int tracer_alloc_buffers(void)
 	/* Function tracing may start here (via kernel command line) */
 	init_function_trace();
 
+	/* Pre-fill the ring buffer with very early events */
+	ftrace_early_fill_ringbuffer(&global_trace);
+
 	/* All seems OK, enable tracing */
 	tracing_disabled = 0;
 
Signature · 1 line
2.11.0