Skip to content

Commit bd604f3

Browse files
committed
ftrace: Avoid needless updates of the ftrace function call
Song Shuai reported: The list func (ftrace_ops_list_func) will be patched first before the transition between old and new calls are set, which fixed the race described in this commit `59338f75`. While ftrace_trace_function changes from the list func to a ftrace_ops func, like unregistering the klp_ops to leave the only global_ops in ftrace_ops_list, the ftrace_[regs]_call will be replaced with the list func although it already exists. So there should be a condition to avoid this. And suggested using another variable to keep track of what the ftrace function is set to. But this could be simplified by using a helper function that does the same with a static variable. Link: https://lore.kernel.org/lkml/[email protected]/ Link: https://lore.kernel.org/linux-trace-kernel/[email protected] Reported-by: Song Shuai <[email protected]> Reviewed-by: Masami Hiramatsu (Google) <[email protected]> Signed-off-by: Steven Rostedt (Google) <[email protected]>
1 parent 96e6122 commit bd604f3

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

kernel/trace/ftrace.c

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2763,6 +2763,19 @@ void __weak ftrace_arch_code_modify_post_process(void)
27632763
{
27642764
}
27652765

2766+
static int update_ftrace_func(ftrace_func_t func)
2767+
{
2768+
static ftrace_func_t save_func;
2769+
2770+
/* Avoid updating if it hasn't changed */
2771+
if (func == save_func)
2772+
return 0;
2773+
2774+
save_func = func;
2775+
2776+
return ftrace_update_ftrace_func(func);
2777+
}
2778+
27662779
void ftrace_modify_all_code(int command)
27672780
{
27682781
int update = command & FTRACE_UPDATE_TRACE_FUNC;
@@ -2783,7 +2796,7 @@ void ftrace_modify_all_code(int command)
27832796
* traced.
27842797
*/
27852798
if (update) {
2786-
err = ftrace_update_ftrace_func(ftrace_ops_list_func);
2799+
err = update_ftrace_func(ftrace_ops_list_func);
27872800
if (FTRACE_WARN_ON(err))
27882801
return;
27892802
}
@@ -2799,7 +2812,7 @@ void ftrace_modify_all_code(int command)
27992812
/* If irqs are disabled, we are in stop machine */
28002813
if (!irqs_disabled())
28012814
smp_call_function(ftrace_sync_ipi, NULL, 1);
2802-
err = ftrace_update_ftrace_func(ftrace_trace_function);
2815+
err = update_ftrace_func(ftrace_trace_function);
28032816
if (FTRACE_WARN_ON(err))
28042817
return;
28052818
}

0 commit comments

Comments
 (0)