Skip to content

Commit aec0be2

Browse files
committed
ftrace/x86/extable: Add is_ftrace_trampoline() function
Stack traces that happen from function tracing check if the address on the stack is a __kernel_text_address(). That is, is the address kernel code. This calls core_kernel_text() which returns true if the address is part of the builtin kernel code. It also calls is_module_text_address() which returns true if the address belongs to module code. But what is missing is ftrace dynamically allocated trampolines. These trampolines are allocated for individual ftrace_ops that call the ftrace_ops callback functions directly. But if they do a stack trace, the code checking the stack wont detect them as they are neither core kernel code nor module address space. Adding another field to ftrace_ops that also stores the size of the trampoline assigned to it we can create a new function called is_ftrace_trampoline() that returns true if the address is a dynamically allocate ftrace trampoline. Note, it ignores trampolines that are not dynamically allocated as they will return true with the core_kernel_text() function. Link: http://lkml.kernel.org/r/20141119034829.497125839@goodmis.org Cc: Ingo Molnar <mingo@redhat.com> Cc: "H. Peter Anvin" <hpa@zytor.com> Acked-by: Thomas Gleixner <tglx@linutronix.de> Signed-off-by: Steven Rostedt <rostedt@goodmis.org>
1 parent 9960efe commit aec0be2

4 files changed

Lines changed: 59 additions & 3 deletions

File tree

arch/x86/kernel/ftrace.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -712,7 +712,8 @@ union ftrace_op_code_union {
712712
} __attribute__((packed));
713713
};
714714

715-
static unsigned long create_trampoline(struct ftrace_ops *ops)
715+
static unsigned long
716+
create_trampoline(struct ftrace_ops *ops, unsigned int *tramp_size)
716717
{
717718
unsigned const char *jmp;
718719
unsigned long start_offset;
@@ -749,6 +750,8 @@ static unsigned long create_trampoline(struct ftrace_ops *ops)
749750
if (!trampoline)
750751
return 0;
751752

753+
*tramp_size = size + MCOUNT_INSN_SIZE + sizeof(void *);
754+
752755
/* Copy ftrace_caller onto the trampoline memory */
753756
ret = probe_kernel_read(trampoline, (void *)start_offset, size);
754757
if (WARN_ON(ret < 0)) {
@@ -819,6 +822,7 @@ void arch_ftrace_update_trampoline(struct ftrace_ops *ops)
819822
unsigned char *new;
820823
unsigned long offset;
821824
unsigned long ip;
825+
unsigned int size;
822826
int ret;
823827

824828
if (ops->trampoline) {
@@ -829,9 +833,10 @@ void arch_ftrace_update_trampoline(struct ftrace_ops *ops)
829833
if (!(ops->flags & FTRACE_OPS_FL_ALLOC_TRAMP))
830834
return;
831835
} else {
832-
ops->trampoline = create_trampoline(ops);
836+
ops->trampoline = create_trampoline(ops, &size);
833837
if (!ops->trampoline)
834838
return;
839+
ops->trampoline_size = size;
835840
}
836841

837842
offset = calc_trampoline_call_offset(ops->flags & FTRACE_OPS_FL_SAVE_REGS);

include/linux/ftrace.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ struct ftrace_ops {
150150
struct ftrace_ops_hash *func_hash;
151151
struct ftrace_ops_hash old_hash;
152152
unsigned long trampoline;
153+
unsigned long trampoline_size;
153154
#endif
154155
};
155156

@@ -297,6 +298,8 @@ extern int ftrace_text_reserved(const void *start, const void *end);
297298

298299
extern int ftrace_nr_registered_ops(void);
299300

301+
bool is_ftrace_trampoline(unsigned long addr);
302+
300303
/*
301304
* The dyn_ftrace record's flags field is split into two parts.
302305
* the first part which is '0-FTRACE_REF_MAX' is a counter of
@@ -596,6 +599,11 @@ static inline ssize_t ftrace_notrace_write(struct file *file, const char __user
596599
size_t cnt, loff_t *ppos) { return -ENODEV; }
597600
static inline int
598601
ftrace_regex_release(struct inode *inode, struct file *file) { return -ENODEV; }
602+
603+
static inline bool is_ftrace_trampoline(unsigned long addr)
604+
{
605+
return false;
606+
}
599607
#endif /* CONFIG_DYNAMIC_FTRACE */
600608

601609
/* totally disable ftrace - can not re-enable after this */

kernel/extable.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <linux/ftrace.h>
1919
#include <linux/memory.h>
2020
#include <linux/module.h>
21+
#include <linux/ftrace.h>
2122
#include <linux/mutex.h>
2223
#include <linux/init.h>
2324

@@ -102,6 +103,8 @@ int __kernel_text_address(unsigned long addr)
102103
return 1;
103104
if (is_module_text_address(addr))
104105
return 1;
106+
if (is_ftrace_trampoline(addr))
107+
return 1;
105108
/*
106109
* There might be init symbols in saved stacktraces.
107110
* Give those symbols a chance to be printed in
@@ -119,7 +122,9 @@ int kernel_text_address(unsigned long addr)
119122
{
120123
if (core_kernel_text(addr))
121124
return 1;
122-
return is_module_text_address(addr);
125+
if (is_module_text_address(addr))
126+
return 1;
127+
return is_ftrace_trampoline(addr);
123128
}
124129

125130
/*

kernel/trace/ftrace.c

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1117,6 +1117,43 @@ static struct ftrace_ops global_ops = {
11171117
FTRACE_OPS_FL_INITIALIZED,
11181118
};
11191119

1120+
/*
1121+
* This is used by __kernel_text_address() to return true if the
1122+
* the address is on a dynamically allocated trampoline that would
1123+
* not return true for either core_kernel_text() or
1124+
* is_module_text_address().
1125+
*/
1126+
bool is_ftrace_trampoline(unsigned long addr)
1127+
{
1128+
struct ftrace_ops *op;
1129+
bool ret = false;
1130+
1131+
/*
1132+
* Some of the ops may be dynamically allocated,
1133+
* they are freed after a synchronize_sched().
1134+
*/
1135+
preempt_disable_notrace();
1136+
1137+
do_for_each_ftrace_op(op, ftrace_ops_list) {
1138+
/*
1139+
* This is to check for dynamically allocated trampolines.
1140+
* Trampolines that are in kernel text will have
1141+
* core_kernel_text() return true.
1142+
*/
1143+
if (op->trampoline && op->trampoline_size)
1144+
if (addr >= op->trampoline &&
1145+
addr < op->trampoline + op->trampoline_size) {
1146+
ret = true;
1147+
goto out;
1148+
}
1149+
} while_for_each_ftrace_op(op);
1150+
1151+
out:
1152+
preempt_enable_notrace();
1153+
1154+
return ret;
1155+
}
1156+
11201157
struct ftrace_page {
11211158
struct ftrace_page *next;
11221159
struct dyn_ftrace *records;
@@ -5373,6 +5410,7 @@ static struct ftrace_ops graph_ops = {
53735410
FTRACE_OPS_FL_STUB,
53745411
#ifdef FTRACE_GRAPH_TRAMP_ADDR
53755412
.trampoline = FTRACE_GRAPH_TRAMP_ADDR,
5413+
/* trampoline_size is only needed for dynamically allocated tramps */
53765414
#endif
53775415
ASSIGN_OPS_HASH(graph_ops, &global_ops.local_hash)
53785416
};

0 commit comments

Comments
 (0)