Skip to content

Commit fc0ea79

Browse files
ahunter6Peter Zijlstra
authored andcommitted
ftrace: Add symbols for ftrace trampolines
Symbols are needed for tools to describe instruction addresses. Pages allocated for ftrace's purposes need symbols to be created for them. Add such symbols to be visible via /proc/kallsyms. Example on x86 with CONFIG_DYNAMIC_FTRACE=y # echo function > /sys/kernel/debug/tracing/current_tracer # cat /proc/kallsyms | grep '\[__builtin__ftrace\]' ffffffffc0238000 t ftrace_trampoline [__builtin__ftrace] Note: This patch adds "__builtin__ftrace" as a module name in /proc/kallsyms for symbols for pages allocated for ftrace's purposes, even though "__builtin__ftrace" is not a module. Signed-off-by: Adrian Hunter <adrian.hunter@intel.com> Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Acked-by: Peter Zijlstra (Intel) <peterz@infradead.org> Link: https://lkml.kernel.org/r/20200512121922.8997-7-adrian.hunter@intel.com
1 parent 3e46bb4 commit fc0ea79

3 files changed

Lines changed: 88 additions & 6 deletions

File tree

‎include/linux/ftrace.h‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,16 +58,20 @@ struct ftrace_direct_func;
5858
const char *
5959
ftrace_mod_address_lookup(unsigned long addr, unsigned long *size,
6060
unsigned long *off, char **modname, char *sym);
61-
int ftrace_mod_get_kallsym(unsigned int symnum, unsigned long *value,
62-
char *type, char *name,
63-
char *module_name, int *exported);
6461
#else
6562
static inline const char *
6663
ftrace_mod_address_lookup(unsigned long addr, unsigned long *size,
6764
unsigned long *off, char **modname, char *sym)
6865
{
6966
return NULL;
7067
}
68+
#endif
69+
70+
#if defined(CONFIG_FUNCTION_TRACER) && defined(CONFIG_DYNAMIC_FTRACE)
71+
int ftrace_mod_get_kallsym(unsigned int symnum, unsigned long *value,
72+
char *type, char *name,
73+
char *module_name, int *exported);
74+
#else
7175
static inline int ftrace_mod_get_kallsym(unsigned int symnum, unsigned long *value,
7276
char *type, char *name,
7377
char *module_name, int *exported)
@@ -76,7 +80,6 @@ static inline int ftrace_mod_get_kallsym(unsigned int symnum, unsigned long *val
7680
}
7781
#endif
7882

79-
8083
#ifdef CONFIG_FUNCTION_TRACER
8184

8285
extern int ftrace_enabled;
@@ -207,6 +210,7 @@ struct ftrace_ops {
207210
struct ftrace_ops_hash old_hash;
208211
unsigned long trampoline;
209212
unsigned long trampoline_size;
213+
struct list_head list;
210214
#endif
211215
};
212216

‎kernel/kallsyms.c‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,11 @@ static int get_ksymbol_mod(struct kallsym_iter *iter)
482482
return 1;
483483
}
484484

485+
/*
486+
* ftrace_mod_get_kallsym() may also get symbols for pages allocated for ftrace
487+
* purposes. In that case "__builtin__ftrace" is used as a module name, even
488+
* though "__builtin__ftrace" is not a module.
489+
*/
485490
static int get_ksymbol_ftrace_mod(struct kallsym_iter *iter)
486491
{
487492
int ret = ftrace_mod_get_kallsym(iter->pos - iter->pos_mod_end,

‎kernel/trace/ftrace.c‎

Lines changed: 75 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2764,6 +2764,38 @@ void __weak arch_ftrace_trampoline_free(struct ftrace_ops *ops)
27642764
{
27652765
}
27662766

2767+
/* List of trace_ops that have allocated trampolines */
2768+
static LIST_HEAD(ftrace_ops_trampoline_list);
2769+
2770+
static void ftrace_add_trampoline_to_kallsyms(struct ftrace_ops *ops)
2771+
{
2772+
lockdep_assert_held(&ftrace_lock);
2773+
list_add_rcu(&ops->list, &ftrace_ops_trampoline_list);
2774+
}
2775+
2776+
static void ftrace_remove_trampoline_from_kallsyms(struct ftrace_ops *ops)
2777+
{
2778+
lockdep_assert_held(&ftrace_lock);
2779+
list_del_rcu(&ops->list);
2780+
}
2781+
2782+
/*
2783+
* "__builtin__ftrace" is used as a module name in /proc/kallsyms for symbols
2784+
* for pages allocated for ftrace purposes, even though "__builtin__ftrace" is
2785+
* not a module.
2786+
*/
2787+
#define FTRACE_TRAMPOLINE_MOD "__builtin__ftrace"
2788+
#define FTRACE_TRAMPOLINE_SYM "ftrace_trampoline"
2789+
2790+
static void ftrace_trampoline_free(struct ftrace_ops *ops)
2791+
{
2792+
if (ops && (ops->flags & FTRACE_OPS_FL_ALLOC_TRAMP) &&
2793+
ops->trampoline)
2794+
ftrace_remove_trampoline_from_kallsyms(ops);
2795+
2796+
arch_ftrace_trampoline_free(ops);
2797+
}
2798+
27672799
static void ftrace_startup_enable(int command)
27682800
{
27692801
if (saved_ftrace_func != ftrace_trace_function) {
@@ -2934,7 +2966,7 @@ int ftrace_shutdown(struct ftrace_ops *ops, int command)
29342966
synchronize_rcu_tasks();
29352967

29362968
free_ops:
2937-
arch_ftrace_trampoline_free(ops);
2969+
ftrace_trampoline_free(ops);
29382970
}
29392971

29402972
return 0;
@@ -6178,6 +6210,27 @@ struct ftrace_mod_map {
61786210
unsigned int num_funcs;
61796211
};
61806212

6213+
static int ftrace_get_trampoline_kallsym(unsigned int symnum,
6214+
unsigned long *value, char *type,
6215+
char *name, char *module_name,
6216+
int *exported)
6217+
{
6218+
struct ftrace_ops *op;
6219+
6220+
list_for_each_entry_rcu(op, &ftrace_ops_trampoline_list, list) {
6221+
if (!op->trampoline || symnum--)
6222+
continue;
6223+
*value = op->trampoline;
6224+
*type = 't';
6225+
strlcpy(name, FTRACE_TRAMPOLINE_SYM, KSYM_NAME_LEN);
6226+
strlcpy(module_name, FTRACE_TRAMPOLINE_MOD, MODULE_NAME_LEN);
6227+
*exported = 0;
6228+
return 0;
6229+
}
6230+
6231+
return -ERANGE;
6232+
}
6233+
61816234
#ifdef CONFIG_MODULES
61826235

61836236
#define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
@@ -6514,6 +6567,7 @@ int ftrace_mod_get_kallsym(unsigned int symnum, unsigned long *value,
65146567
{
65156568
struct ftrace_mod_map *mod_map;
65166569
struct ftrace_mod_func *mod_func;
6570+
int ret;
65176571

65186572
preempt_disable();
65196573
list_for_each_entry_rcu(mod_map, &ftrace_mod_maps, list) {
@@ -6540,8 +6594,10 @@ int ftrace_mod_get_kallsym(unsigned int symnum, unsigned long *value,
65406594
WARN_ON(1);
65416595
break;
65426596
}
6597+
ret = ftrace_get_trampoline_kallsym(symnum, value, type, name,
6598+
module_name, exported);
65436599
preempt_enable();
6544-
return -ERANGE;
6600+
return ret;
65456601
}
65466602

65476603
#else
@@ -6553,6 +6609,18 @@ allocate_ftrace_mod_map(struct module *mod,
65536609
{
65546610
return NULL;
65556611
}
6612+
int ftrace_mod_get_kallsym(unsigned int symnum, unsigned long *value,
6613+
char *type, char *name, char *module_name,
6614+
int *exported)
6615+
{
6616+
int ret;
6617+
6618+
preempt_disable();
6619+
ret = ftrace_get_trampoline_kallsym(symnum, value, type, name,
6620+
module_name, exported);
6621+
preempt_enable();
6622+
return ret;
6623+
}
65566624
#endif /* CONFIG_MODULES */
65576625

65586626
struct ftrace_init_func {
@@ -6733,7 +6801,12 @@ void __weak arch_ftrace_update_trampoline(struct ftrace_ops *ops)
67336801

67346802
static void ftrace_update_trampoline(struct ftrace_ops *ops)
67356803
{
6804+
unsigned long trampoline = ops->trampoline;
6805+
67366806
arch_ftrace_update_trampoline(ops);
6807+
if (ops->trampoline && ops->trampoline != trampoline &&
6808+
(ops->flags & FTRACE_OPS_FL_ALLOC_TRAMP))
6809+
ftrace_add_trampoline_to_kallsyms(ops);
67376810
}
67386811

67396812
void ftrace_init_trace_array(struct trace_array *tr)

0 commit comments

Comments
 (0)