Skip to content

Commit 7c4f6a4

Browse files
rostedtkamalmostafa
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 #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> [ kamal: backport to 3.8 (no FTRACE_OPS_FL_STUB); prereq for a4c35ed "ftrace: Fix synchronization location disabling and freeing ftrace_ops" ] Signed-off-by: Kamal Mostafa <kamal@canonical.com>
1 parent 14c8fea commit 7c4f6a4

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
@@ -324,9 +324,6 @@ static int remove_ftrace_list_ops(struct ftrace_ops **list,
324324

325325
static int __register_ftrace_function(struct ftrace_ops *ops)
326326
{
327-
if (unlikely(ftrace_disabled))
328-
return -ENODEV;
329-
330327
if (FTRACE_WARN_ON(ops == &global_ops))
331328
return -EINVAL;
332329

@@ -374,9 +371,6 @@ static int __unregister_ftrace_function(struct ftrace_ops *ops)
374371
{
375372
int ret;
376373

377-
if (ftrace_disabled)
378-
return -ENODEV;
379-
380374
if (WARN_ON(!(ops->flags & FTRACE_OPS_FL_ENABLED)))
381375
return -EBUSY;
382376

@@ -2009,10 +2003,15 @@ static void ftrace_startup_enable(int command)
20092003
static int ftrace_startup(struct ftrace_ops *ops, int command)
20102004
{
20112005
bool hash_enable = true;
2006+
int ret;
20122007

20132008
if (unlikely(ftrace_disabled))
20142009
return -ENODEV;
20152010

2011+
ret = __register_ftrace_function(ops);
2012+
if (ret)
2013+
return ret;
2014+
20162015
ftrace_start_up++;
20172016
command |= FTRACE_UPDATE_CALLS;
20182017

@@ -2034,12 +2033,17 @@ static int ftrace_startup(struct ftrace_ops *ops, int command)
20342033
return 0;
20352034
}
20362035

2037-
static void ftrace_shutdown(struct ftrace_ops *ops, int command)
2036+
static int ftrace_shutdown(struct ftrace_ops *ops, int command)
20382037
{
20392038
bool hash_disable = true;
2039+
int ret;
20402040

20412041
if (unlikely(ftrace_disabled))
2042-
return;
2042+
return -ENODEV;
2043+
2044+
ret = __unregister_ftrace_function(ops);
2045+
if (ret)
2046+
return ret;
20432047

20442048
ftrace_start_up--;
20452049
/*
@@ -2074,9 +2078,10 @@ static void ftrace_shutdown(struct ftrace_ops *ops, int command)
20742078
}
20752079

20762080
if (!command || !ftrace_enabled)
2077-
return;
2081+
return 0;
20782082

20792083
ftrace_run_update_code(command);
2084+
return 0;
20802085
}
20812086

20822087
static void ftrace_startup_sysctl(void)
@@ -2974,16 +2979,13 @@ static void __enable_ftrace_function_probe(void)
29742979
if (i == FTRACE_FUNC_HASHSIZE)
29752980
return;
29762981

2977-
ret = __register_ftrace_function(&trace_probe_ops);
2978-
if (!ret)
2979-
ret = ftrace_startup(&trace_probe_ops, 0);
2982+
ret = ftrace_startup(&trace_probe_ops, 0);
29802983

29812984
ftrace_probe_registered = 1;
29822985
}
29832986

29842987
static void __disable_ftrace_function_probe(void)
29852988
{
2986-
int ret;
29872989
int i;
29882990

29892991
if (!ftrace_probe_registered)
@@ -2996,9 +2998,7 @@ static void __disable_ftrace_function_probe(void)
29962998
}
29972999

29983000
/* no more funcs left */
2999-
ret = __unregister_ftrace_function(&trace_probe_ops);
3000-
if (!ret)
3001-
ftrace_shutdown(&trace_probe_ops, 0);
3001+
ftrace_shutdown(&trace_probe_ops, 0);
30023002

30033003
ftrace_probe_registered = 0;
30043004
}
@@ -4131,12 +4131,15 @@ core_initcall(ftrace_nodyn_init);
41314131
static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
41324132
static inline void ftrace_startup_enable(int command) { }
41334133
/* Keep as macros so we do not need to define the commands */
4134-
# define ftrace_startup(ops, command) \
4135-
({ \
4136-
(ops)->flags |= FTRACE_OPS_FL_ENABLED; \
4137-
0; \
4134+
# define ftrace_startup(ops, command) \
4135+
({ \
4136+
int ___ret = __register_ftrace_function(ops); \
4137+
if (!___ret) \
4138+
(ops)->flags |= FTRACE_OPS_FL_ENABLED; \
4139+
___ret; \
41384140
})
4139-
# define ftrace_shutdown(ops, command) do { } while (0)
4141+
# define ftrace_shutdown(ops, command) __unregister_ftrace_function(ops)
4142+
41404143
# define ftrace_startup_sysctl() do { } while (0)
41414144
# define ftrace_shutdown_sysctl() do { } while (0)
41424145

@@ -4536,9 +4539,7 @@ int register_ftrace_function(struct ftrace_ops *ops)
45364539

45374540
mutex_lock(&ftrace_lock);
45384541

4539-
ret = __register_ftrace_function(ops);
4540-
if (!ret)
4541-
ret = ftrace_startup(ops, 0);
4542+
ret = ftrace_startup(ops, 0);
45424543

45434544
mutex_unlock(&ftrace_lock);
45444545

@@ -4557,9 +4558,7 @@ int unregister_ftrace_function(struct ftrace_ops *ops)
45574558
int ret;
45584559

45594560
mutex_lock(&ftrace_lock);
4560-
ret = __unregister_ftrace_function(ops);
4561-
if (!ret)
4562-
ftrace_shutdown(ops, 0);
4561+
ret = ftrace_shutdown(ops, 0);
45634562
mutex_unlock(&ftrace_lock);
45644563

45654564
return ret;
@@ -4753,6 +4752,13 @@ ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
47534752
return NOTIFY_DONE;
47544753
}
47554754

4755+
/* Just a place holder for function graph */
4756+
static struct ftrace_ops fgraph_ops __read_mostly = {
4757+
.func = ftrace_stub,
4758+
.flags = FTRACE_OPS_FL_GLOBAL |
4759+
FTRACE_OPS_FL_RECURSION_SAFE,
4760+
};
4761+
47564762
int register_ftrace_graph(trace_func_graph_ret_t retfunc,
47574763
trace_func_graph_ent_t entryfunc)
47584764
{
@@ -4779,7 +4785,7 @@ int register_ftrace_graph(trace_func_graph_ret_t retfunc,
47794785
ftrace_graph_return = retfunc;
47804786
ftrace_graph_entry = entryfunc;
47814787

4782-
ret = ftrace_startup(&global_ops, FTRACE_START_FUNC_RET);
4788+
ret = ftrace_startup(&fgraph_ops, FTRACE_START_FUNC_RET);
47834789

47844790
out:
47854791
mutex_unlock(&ftrace_lock);
@@ -4796,7 +4802,7 @@ void unregister_ftrace_graph(void)
47964802
ftrace_graph_active--;
47974803
ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
47984804
ftrace_graph_entry = ftrace_graph_entry_stub;
4799-
ftrace_shutdown(&global_ops, FTRACE_STOP_FUNC_RET);
4805+
ftrace_shutdown(&fgraph_ops, FTRACE_STOP_FUNC_RET);
48004806
unregister_pm_notifier(&ftrace_suspend_notifier);
48014807
unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
48024808

0 commit comments

Comments
 (0)