Skip to content

Commit 2940c25

Browse files
rostedtgregkh
authored andcommitted
ftrace: Fix function graph with loading of modules
commit 8a56d77 upstream. Commit 8c4f3c3 "ftrace: Check module functions being traced on reload" fixed module loading and unloading with respect to function tracing, but it missed the function graph tracer. If you perform the following # cd /sys/kernel/debug/tracing # echo function_graph > current_tracer # modprobe nfsd # echo nop > current_tracer You'll get the following oops message: ------------[ cut here ]------------ WARNING: CPU: 2 PID: 2910 at /linux.git/kernel/trace/ftrace.c:1640 __ftrace_hash_rec_update.part.35+0x168/0x1b9() Modules linked in: nfsd exportfs nfs_acl lockd ipt_MASQUERADE sunrpc ip6t_REJECT nf_conntrack_ipv6 nf_defrag_ipv6 ip6table_filter ip6_tables uinput snd_hda_codec_idt CPU: 2 PID: 2910 Comm: bash Not tainted 3.13.0-rc1-test hardkernel#7 Hardware name: To Be Filled By O.E.M. To Be Filled By O.E.M./To be filled by O.E.M., BIOS SDBLI944.86P 05/08/2007 0000000000000668 ffff8800787efcf8 ffffffff814fe193 ffff88007d500000 0000000000000000 ffff8800787efd38 ffffffff8103b80a 0000000000000668 ffffffff810b2b9a ffffffff81a48370 0000000000000001 ffff880037aea000 Call Trace: [<ffffffff814fe193>] dump_stack+0x4f/0x7c [<ffffffff8103b80a>] warn_slowpath_common+0x81/0x9b [<ffffffff810b2b9a>] ? __ftrace_hash_rec_update.part.35+0x168/0x1b9 [<ffffffff8103b83e>] warn_slowpath_null+0x1a/0x1c [<ffffffff810b2b9a>] __ftrace_hash_rec_update.part.35+0x168/0x1b9 [<ffffffff81502f89>] ? __mutex_lock_slowpath+0x364/0x364 [<ffffffff810b2cc2>] ftrace_shutdown+0xd7/0x12b [<ffffffff810b47f0>] unregister_ftrace_graph+0x49/0x78 [<ffffffff810c4b30>] graph_trace_reset+0xe/0x10 [<ffffffff810bf393>] tracing_set_tracer+0xa7/0x26a [<ffffffff810bf5e1>] tracing_set_trace_write+0x8b/0xbd [<ffffffff810c501c>] ? ftrace_return_to_handler+0xb2/0xde [<ffffffff811240a8>] ? __sb_end_write+0x5e/0x5e [<ffffffff81122aed>] vfs_write+0xab/0xf6 [<ffffffff8150a185>] ftrace_graph_caller+0x85/0x85 [<ffffffff81122dbd>] SyS_write+0x59/0x82 [<ffffffff8150a185>] ftrace_graph_caller+0x85/0x85 [<ffffffff8150a2d2>] system_call_fastpath+0x16/0x1b ---[ end trace 940358030751eafb ]--- The above mentioned commit didn't go far enough. Well, it covered the function tracer by adding checks in __register_ftrace_function(). The problem is that the function graph tracer circumvents that (for a slight efficiency gain when function graph trace is running with a function tracer. The gain was not worth this). The problem came with ftrace_startup() which should always be called after __register_ftrace_function(), if you want this bug to be completely fixed. Anyway, this solution moves __register_ftrace_function() inside of ftrace_startup() and removes the need to call them both. Reported-by: Dave Wysochanski <dwysocha@redhat.com> Fixes: ed926f9 ("ftrace: Use counters to enable functions to trace") Signed-off-by: Steven Rostedt <rostedt@goodmis.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent ad03714 commit 2940c25

1 file changed

Lines changed: 35 additions & 29 deletions

File tree

kernel/trace/ftrace.c

Lines changed: 35 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -367,9 +367,6 @@ static int remove_ftrace_list_ops(struct ftrace_ops **list,
367367

368368
static int __register_ftrace_function(struct ftrace_ops *ops)
369369
{
370-
if (unlikely(ftrace_disabled))
371-
return -ENODEV;
372-
373370
if (FTRACE_WARN_ON(ops == &global_ops))
374371
return -EINVAL;
375372

@@ -417,9 +414,6 @@ static int __unregister_ftrace_function(struct ftrace_ops *ops)
417414
{
418415
int ret;
419416

420-
if (ftrace_disabled)
421-
return -ENODEV;
422-
423417
if (WARN_ON(!(ops->flags & FTRACE_OPS_FL_ENABLED)))
424418
return -EBUSY;
425419

@@ -2048,10 +2042,15 @@ static void ftrace_startup_enable(int command)
20482042
static int ftrace_startup(struct ftrace_ops *ops, int command)
20492043
{
20502044
bool hash_enable = true;
2045+
int ret;
20512046

20522047
if (unlikely(ftrace_disabled))
20532048
return -ENODEV;
20542049

2050+
ret = __register_ftrace_function(ops);
2051+
if (ret)
2052+
return ret;
2053+
20552054
ftrace_start_up++;
20562055
command |= FTRACE_UPDATE_CALLS;
20572056

@@ -2073,12 +2072,17 @@ static int ftrace_startup(struct ftrace_ops *ops, int command)
20732072
return 0;
20742073
}
20752074

2076-
static void ftrace_shutdown(struct ftrace_ops *ops, int command)
2075+
static int ftrace_shutdown(struct ftrace_ops *ops, int command)
20772076
{
20782077
bool hash_disable = true;
2078+
int ret;
20792079

20802080
if (unlikely(ftrace_disabled))
2081-
return;
2081+
return -ENODEV;
2082+
2083+
ret = __unregister_ftrace_function(ops);
2084+
if (ret)
2085+
return ret;
20822086

20832087
ftrace_start_up--;
20842088
/*
@@ -2113,9 +2117,10 @@ static void ftrace_shutdown(struct ftrace_ops *ops, int command)
21132117
}
21142118

21152119
if (!command || !ftrace_enabled)
2116-
return;
2120+
return 0;
21172121

21182122
ftrace_run_update_code(command);
2123+
return 0;
21192124
}
21202125

21212126
static void ftrace_startup_sysctl(void)
@@ -3020,16 +3025,13 @@ static void __enable_ftrace_function_probe(void)
30203025
if (i == FTRACE_FUNC_HASHSIZE)
30213026
return;
30223027

3023-
ret = __register_ftrace_function(&trace_probe_ops);
3024-
if (!ret)
3025-
ret = ftrace_startup(&trace_probe_ops, 0);
3028+
ret = ftrace_startup(&trace_probe_ops, 0);
30263029

30273030
ftrace_probe_registered = 1;
30283031
}
30293032

30303033
static void __disable_ftrace_function_probe(void)
30313034
{
3032-
int ret;
30333035
int i;
30343036

30353037
if (!ftrace_probe_registered)
@@ -3042,9 +3044,7 @@ static void __disable_ftrace_function_probe(void)
30423044
}
30433045

30443046
/* no more funcs left */
3045-
ret = __unregister_ftrace_function(&trace_probe_ops);
3046-
if (!ret)
3047-
ftrace_shutdown(&trace_probe_ops, 0);
3047+
ftrace_shutdown(&trace_probe_ops, 0);
30483048

30493049
ftrace_probe_registered = 0;
30503050
}
@@ -4241,12 +4241,15 @@ core_initcall(ftrace_nodyn_init);
42414241
static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
42424242
static inline void ftrace_startup_enable(int command) { }
42434243
/* Keep as macros so we do not need to define the commands */
4244-
# define ftrace_startup(ops, command) \
4245-
({ \
4246-
(ops)->flags |= FTRACE_OPS_FL_ENABLED; \
4247-
0; \
4244+
# define ftrace_startup(ops, command) \
4245+
({ \
4246+
int ___ret = __register_ftrace_function(ops); \
4247+
if (!___ret) \
4248+
(ops)->flags |= FTRACE_OPS_FL_ENABLED; \
4249+
___ret; \
42484250
})
4249-
# define ftrace_shutdown(ops, command) do { } while (0)
4251+
# define ftrace_shutdown(ops, command) __unregister_ftrace_function(ops)
4252+
42504253
# define ftrace_startup_sysctl() do { } while (0)
42514254
# define ftrace_shutdown_sysctl() do { } while (0)
42524255

@@ -4646,9 +4649,7 @@ int register_ftrace_function(struct ftrace_ops *ops)
46464649

46474650
mutex_lock(&ftrace_lock);
46484651

4649-
ret = __register_ftrace_function(ops);
4650-
if (!ret)
4651-
ret = ftrace_startup(ops, 0);
4652+
ret = ftrace_startup(ops, 0);
46524653

46534654
mutex_unlock(&ftrace_lock);
46544655

@@ -4667,9 +4668,7 @@ int unregister_ftrace_function(struct ftrace_ops *ops)
46674668
int ret;
46684669

46694670
mutex_lock(&ftrace_lock);
4670-
ret = __unregister_ftrace_function(ops);
4671-
if (!ret)
4672-
ftrace_shutdown(ops, 0);
4671+
ret = ftrace_shutdown(ops, 0);
46734672
mutex_unlock(&ftrace_lock);
46744673

46754674
return ret;
@@ -4863,6 +4862,13 @@ ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
48634862
return NOTIFY_DONE;
48644863
}
48654864

4865+
/* Just a place holder for function graph */
4866+
static struct ftrace_ops fgraph_ops __read_mostly = {
4867+
.func = ftrace_stub,
4868+
.flags = FTRACE_OPS_FL_STUB | FTRACE_OPS_FL_GLOBAL |
4869+
FTRACE_OPS_FL_RECURSION_SAFE,
4870+
};
4871+
48664872
int register_ftrace_graph(trace_func_graph_ret_t retfunc,
48674873
trace_func_graph_ent_t entryfunc)
48684874
{
@@ -4889,7 +4895,7 @@ int register_ftrace_graph(trace_func_graph_ret_t retfunc,
48894895
ftrace_graph_return = retfunc;
48904896
ftrace_graph_entry = entryfunc;
48914897

4892-
ret = ftrace_startup(&global_ops, FTRACE_START_FUNC_RET);
4898+
ret = ftrace_startup(&fgraph_ops, FTRACE_START_FUNC_RET);
48934899

48944900
out:
48954901
mutex_unlock(&ftrace_lock);
@@ -4906,7 +4912,7 @@ void unregister_ftrace_graph(void)
49064912
ftrace_graph_active--;
49074913
ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
49084914
ftrace_graph_entry = ftrace_graph_entry_stub;
4909-
ftrace_shutdown(&global_ops, FTRACE_STOP_FUNC_RET);
4915+
ftrace_shutdown(&fgraph_ops, FTRACE_STOP_FUNC_RET);
49104916
unregister_pm_notifier(&ftrace_suspend_notifier);
49114917
unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
49124918

0 commit comments

Comments
 (0)