Skip to contentSkip to conversation

ftrace: avoid replacing the list func with itself

PATCH In review Song Shuai 2 participants 6 messages Oct 26, 2022 – Nov 23, 2022

Subsystems (2)
lore
Share & tools
lore

Song Shuai

author
#0 lore raw

PATCH 1/1 ftrace: avoid replacing the list func with itself

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.

This patch backups saved_ftrace_func by saved_ftrace_func_old which will be compared with the list func before trying to patch it.

---
 kernel/trace/ftrace.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
kernel/trace/ftrace.c +6 -1
index bc921a3f7ea8..56b1a42e1937 100644
@@ -2755,6 +2755,8 @@ void __weak ftrace_arch_code_modify_post_process(void)
 {
 }
 
+static ftrace_func_t saved_ftrace_func_old;
+
 void ftrace_modify_all_code(int command)
 {
 	int update = command & FTRACE_UPDATE_TRACE_FUNC;
@@ -2774,7 +2776,7 @@ void ftrace_modify_all_code(int command)
 	 * to make sure the ops are having the right functions
 	 * traced.
 	 */
-	if (update) {
+	if (update && saved_ftrace_func_old != ftrace_ops_list_func) {
 		err = ftrace_update_ftrace_func(ftrace_ops_list_func);
 		if (FTRACE_WARN_ON(err))
 			return;
@@ -2918,6 +2920,7 @@ static void ftrace_trampoline_free(struct ftrace_ops *ops)
 static void ftrace_startup_enable(int command)
 {
 	if (saved_ftrace_func != ftrace_trace_function) {
+		saved_ftrace_func_old = saved_ftrace_func;
 		saved_ftrace_func = ftrace_trace_function;
 		command |= FTRACE_UPDATE_TRACE_FUNC;
 	}
@@ -3007,6 +3010,7 @@ int ftrace_shutdown(struct ftrace_ops *ops, int command)
 	ops->flags &= ~FTRACE_OPS_FL_ENABLED;
 
 	if (saved_ftrace_func != ftrace_trace_function) {
+		saved_ftrace_func_old = saved_ftrace_func;
 		saved_ftrace_func = ftrace_trace_function;
 		command |= FTRACE_UPDATE_TRACE_FUNC;
 	}
@@ -8321,6 +8325,7 @@ static void ftrace_startup_sysctl(void)
 
 	/* Force update next time */
 	saved_ftrace_func = NULL;
+	saved_ftrace_func_old = NULL;
 	/* ftrace_start_up is true if we want ftrace running */
 	if (ftrace_start_up) {
 		command = FTRACE_UPDATE_CALLS;
Signature · 1 line
2.20.1
author
#1 lore raw
Quoted 5 lines · Song Shuai <[email protected]> 于2022年10月26日周三 13:20写道:

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.

Quoted 9 lines

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.

This patch backups saved_ftrace_func by saved_ftrace_func_old which will be compared with the list func before trying to patch it.

Ping...

Thanks,
Song

Quoted 54 lines
---
 kernel/trace/ftrace.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
kernel/trace/ftrace.c +6 -2
index bc921a3f7ea8..56b1a42e1937 100644
@@ -2755,6 +2755,8 @@ void __weak ftrace_arch_code_modify_post_process(void)
 {
 }
 
+static ftrace_func_t saved_ftrace_func_old;
+
 void ftrace_modify_all_code(int command)
 {
        int update = command & FTRACE_UPDATE_TRACE_FUNC;
@@ -2774,7 +2776,7 @@ void ftrace_modify_all_code(int command)
         * to make sure the ops are having the right functions
         * traced.
         */
-       if (update) {
+       if (update && saved_ftrace_func_old != ftrace_ops_list_func) {
                err = ftrace_update_ftrace_func(ftrace_ops_list_func);
                if (FTRACE_WARN_ON(err))
                        return;
@@ -2918,6 +2920,7 @@ static void ftrace_trampoline_free(struct ftrace_ops *ops)
 static void ftrace_startup_enable(int command)
 {
        if (saved_ftrace_func != ftrace_trace_function) {
+               saved_ftrace_func_old = saved_ftrace_func;
                saved_ftrace_func = ftrace_trace_function;
                command |= FTRACE_UPDATE_TRACE_FUNC;
        }
@@ -3007,6 +3010,7 @@ int ftrace_shutdown(struct ftrace_ops *ops, int command)
        ops->flags &= ~FTRACE_OPS_FL_ENABLED;
 
        if (saved_ftrace_func != ftrace_trace_function) {
+               saved_ftrace_func_old = saved_ftrace_func;
                saved_ftrace_func = ftrace_trace_function;
                command |= FTRACE_UPDATE_TRACE_FUNC;
        }
@@ -8321,6 +8325,7 @@ static void ftrace_startup_sysctl(void)
 
        /* Force update next time */
        saved_ftrace_func = NULL;
+       saved_ftrace_func_old = NULL;
        /* ftrace_start_up is true if we want ftrace running */
        if (ftrace_start_up) {
                command = FTRACE_UPDATE_CALLS;
--

2.20.1

maintainer
#2 lore raw
Quoted 12 lines · On Tue, 22 Nov 2022 02:28:25 +0000 Song Shuai <[email protected]> wrote:
Quoted 11 lines · Song Shuai <[email protected]> 于2022年10月26日周三 13:20写道:

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.

Quoted 4 lines
Quoted 3 lines

This patch backups saved_ftrace_func by saved_ftrace_func_old which will be compared with the list func before trying to patch it.

Ping...

Thanks for the ping. I had thought I had replied to this, but I don't see it in my sent folder. I may have been distracted, and lost the message.

I'll take a look at it tomorrow.

-- Steve

author
#3 lore raw
Quoted 21 lines · Steven Rostedt <[email protected]> 于2022年11月22日周二 02:34写道:
Quoted 12 lines · On Tue, 22 Nov 2022 02:28:25 +0000 Song Shuai <[email protected]> wrote:
Quoted 11 lines · Song Shuai <[email protected]> 于2022年10月26日周三 13:20写道:

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.

Quoted 4 lines
Quoted 3 lines

This patch backups saved_ftrace_func by saved_ftrace_func_old which will be compared with the list func before trying to patch it.

Ping...

Quoted 7 lines

Thanks for the ping. I had thought I had replied to this, but I don't see it in my sent folder. I may have been distracted, and lost the message.

I'll take a look at it tomorrow.

-- Steve

No problem, thanks!

-- Song

maintainer
#4 lore raw
In reply to Song Shuai
Quoted 12 lines · On Tue, 22 Nov 2022 02:28:25 +0000 Song Shuai <[email protected]> wrote:
Quoted 11 lines · Song Shuai <[email protected]> 于2022年10月26日周三 13:20写道:

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.

Quoted 4 lines
Quoted 3 lines

This patch backups saved_ftrace_func by saved_ftrace_func_old which will be compared with the list func before trying to patch it.

Ping...

Wouldn't something like this be simpler and easier to manage (without adding another variable to keep track of)?

-- Steve

kernel/trace/ftrace.c +15 -2
index 65a5d36463e0..d04552c0c275 100644
@@ -2763,6 +2763,19 @@ void __weak ftrace_arch_code_modify_post_process(void)
 {
 }
 
+static int update_ftrace_func(ftrace_func_t func)
+{
+	static ftrace_func_t save_func;
+
+	/* Avoid updating if it hasn't changed */
+	if (func == save_func)
+		return 0;
+
+	save_func = func;
+
+	return ftrace_update_ftrace_func(func);
+}
+
 void ftrace_modify_all_code(int command)
 {
 	int update = command & FTRACE_UPDATE_TRACE_FUNC;
@@ -2783,7 +2796,7 @@ void ftrace_modify_all_code(int command)
 	 * traced.
 	 */
 	if (update) {
-		err = ftrace_update_ftrace_func(ftrace_ops_list_func);
+		err = update_ftrace_func(ftrace_ops_list_func);
 		if (FTRACE_WARN_ON(err))
 			return;
 	}
@@ -2799,7 +2812,7 @@ void ftrace_modify_all_code(int command)
 		/* If irqs are disabled, we are in stop machine */
 		if (!irqs_disabled())
 			smp_call_function(ftrace_sync_ipi, NULL, 1);
-		err = ftrace_update_ftrace_func(ftrace_trace_function);
+		err = update_ftrace_func(ftrace_trace_function);
 		if (FTRACE_WARN_ON(err))
 			return;
 	}
author
#5 lore raw
Quoted 19 lines · Steven Rostedt <[email protected]> 于2022年11月22日周二 21:32写道:
Quoted 15 lines · On Tue, 22 Nov 2022 02:28:25 +0000 Song Shuai <[email protected]> wrote:
Quoted 14 lines · Song Shuai <[email protected]> 于2022年10月26日周三 13:20写道:

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.

This patch backups saved_ftrace_func by saved_ftrace_func_old which will be compared with the list func before trying to patch it.

Quoted 7 lines
Quoted 1 line

Ping...

Wouldn't something like this be simpler and easier to manage (without adding another variable to keep track of)?

-- Steve

Thanks for the corrections, this looks great to me.

Quoted 34 lines
kernel/trace/ftrace.c +14 -1
index 65a5d36463e0..d04552c0c275 100644
@@ -2763,6 +2763,19 @@ void __weak ftrace_arch_code_modify_post_process(void)
 {
 }
 
+static int update_ftrace_func(ftrace_func_t func)
+{
+       static ftrace_func_t save_func;
+
+       /* Avoid updating if it hasn't changed */
+       if (func == save_func)
+               return 0;
+
+       save_func = func;
+
+       return ftrace_update_ftrace_func(func);
+}
+
 void ftrace_modify_all_code(int command)
 {
        int update = command & FTRACE_UPDATE_TRACE_FUNC;
@@ -2783,7 +2796,7 @@ void ftrace_modify_all_code(int command)
         * traced.
         */
        if (update) {
-               err = ftrace_update_ftrace_func(ftrace_ops_list_func);
+               err = update_ftrace_func(ftrace_ops_list_func);
                if (FTRACE_WARN_ON(err))
                        return;
        }

kernel/trace/ftrace.c void ftrace_modify_all_code(int command) · line 2818

Quoted 8 lines
kernel/trace/ftrace.c +1 -1
@@ -2799,7 +2812,7 @@ void ftrace_modify_all_code(int command)
                /* If irqs are disabled, we are in stop machine */
                if (!irqs_disabled())
                        smp_call_function(ftrace_sync_ipi, NULL, 1);
-               err = ftrace_update_ftrace_func(ftrace_trace_function);
+               err = update_ftrace_func(ftrace_trace_function);
                if (FTRACE_WARN_ON(err))
                        return;
        }

Thanks,
Song