Skip to content

Commit ff000db

Browse files
rostedtpopcornmix
authored andcommitted
ftrace: Synchronize setting function_trace_op with ftrace_trace_function
commit 405e1d8 upstream. ftrace_trace_function is a variable that holds what function will be called directly by the assembly code (mcount). If just a single function is registered and it handles recursion itself, then the assembly will call that function directly without any helper function. It also passes in the ftrace_op that was registered with the callback. The ftrace_op to send is stored in the function_trace_op variable. The ftrace_trace_function and function_trace_op needs to be coordinated such that the called callback wont be called with the wrong ftrace_op, otherwise bad things can happen if it expected a different op. Luckily, there's no callback that doesn't use the helper functions that requires this. But there soon will be and this needs to be fixed. Use a set_function_trace_op to store the ftrace_op to set the function_trace_op to when it is safe to do so (during the update function within the breakpoint or stop machine calls). Or if dynamic ftrace is not being used (static tracing) then we have to do a bit more synchronization when the ftrace_trace_function is set as that takes affect immediately (as oppose to dynamic ftrace doing it with the modification of the trampoline). Signed-off-by: Steven Rostedt <rostedt@goodmis.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 245359b commit ff000db

1 file changed

Lines changed: 72 additions & 4 deletions

File tree

‎kernel/trace/ftrace.c‎

Lines changed: 72 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ int function_trace_stop __read_mostly;
8585

8686
/* Current function tracing op */
8787
struct ftrace_ops *function_trace_op __read_mostly = &ftrace_list_end;
88+
/* What to set function_trace_op to */
89+
static struct ftrace_ops *set_function_trace_op;
8890

8991
/* List for set_ftrace_pid's pids. */
9092
LIST_HEAD(ftrace_pids);
@@ -278,6 +280,23 @@ static void update_global_ops(void)
278280
global_ops.func = func;
279281
}
280282

283+
static void ftrace_sync(struct work_struct *work)
284+
{
285+
/*
286+
* This function is just a stub to implement a hard force
287+
* of synchronize_sched(). This requires synchronizing
288+
* tasks even in userspace and idle.
289+
*
290+
* Yes, function tracing is rude.
291+
*/
292+
}
293+
294+
static void ftrace_sync_ipi(void *data)
295+
{
296+
/* Probably not needed, but do it anyway */
297+
smp_rmb();
298+
}
299+
281300
static void update_ftrace_function(void)
282301
{
283302
ftrace_func_t func;
@@ -296,16 +315,59 @@ static void update_ftrace_function(void)
296315
!FTRACE_FORCE_LIST_FUNC)) {
297316
/* Set the ftrace_ops that the arch callback uses */
298317
if (ftrace_ops_list == &global_ops)
299-
function_trace_op = ftrace_global_list;
318+
set_function_trace_op = ftrace_global_list;
300319
else
301-
function_trace_op = ftrace_ops_list;
320+
set_function_trace_op = ftrace_ops_list;
302321
func = ftrace_ops_list->func;
303322
} else {
304323
/* Just use the default ftrace_ops */
305-
function_trace_op = &ftrace_list_end;
324+
set_function_trace_op = &ftrace_list_end;
306325
func = ftrace_ops_list_func;
307326
}
308327

328+
/* If there's no change, then do nothing more here */
329+
if (ftrace_trace_function == func)
330+
return;
331+
332+
/*
333+
* If we are using the list function, it doesn't care
334+
* about the function_trace_ops.
335+
*/
336+
if (func == ftrace_ops_list_func) {
337+
ftrace_trace_function = func;
338+
/*
339+
* Don't even bother setting function_trace_ops,
340+
* it would be racy to do so anyway.
341+
*/
342+
return;
343+
}
344+
345+
#ifndef CONFIG_DYNAMIC_FTRACE
346+
/*
347+
* For static tracing, we need to be a bit more careful.
348+
* The function change takes affect immediately. Thus,
349+
* we need to coorditate the setting of the function_trace_ops
350+
* with the setting of the ftrace_trace_function.
351+
*
352+
* Set the function to the list ops, which will call the
353+
* function we want, albeit indirectly, but it handles the
354+
* ftrace_ops and doesn't depend on function_trace_op.
355+
*/
356+
ftrace_trace_function = ftrace_ops_list_func;
357+
/*
358+
* Make sure all CPUs see this. Yes this is slow, but static
359+
* tracing is slow and nasty to have enabled.
360+
*/
361+
schedule_on_each_cpu(ftrace_sync);
362+
/* Now all cpus are using the list ops. */
363+
function_trace_op = set_function_trace_op;
364+
/* Make sure the function_trace_op is visible on all CPUs */
365+
smp_wmb();
366+
/* Nasty way to force a rmb on all cpus */
367+
smp_call_function(ftrace_sync_ipi, NULL, 1);
368+
/* OK, we are all set to update the ftrace_trace_function now! */
369+
#endif /* !CONFIG_DYNAMIC_FTRACE */
370+
309371
ftrace_trace_function = func;
310372
}
311373

@@ -1952,8 +2014,14 @@ void ftrace_modify_all_code(int command)
19522014
else if (command & FTRACE_DISABLE_CALLS)
19532015
ftrace_replace_code(0);
19542016

1955-
if (command & FTRACE_UPDATE_TRACE_FUNC)
2017+
if (command & FTRACE_UPDATE_TRACE_FUNC) {
2018+
function_trace_op = set_function_trace_op;
2019+
smp_wmb();
2020+
/* If irqs are disabled, we are in stop machine */
2021+
if (!irqs_disabled())
2022+
smp_call_function(ftrace_sync_ipi, NULL, 1);
19562023
ftrace_update_ftrace_func(ftrace_trace_function);
2024+
}
19572025

19582026
if (command & FTRACE_START_FUNC_RET)
19592027
ftrace_enable_ftrace_graph_caller();

0 commit comments

Comments
 (0)