Skip to contentSkip to conversation

tracing: dynamic ftrace selftest detected failures

BUG Masami Hiramatsu (Google) 4 participants 21 messages Aug 19, 2024 – Aug 23, 2024 Tracing

lore
Share & tools
lore

Masami Hiramatsu (Google)

author maintainer
#0 lore raw

Hi Steve,

On trace-v6.11-rc3, ftrace selftest detected failures. I attached kconfigs and dmesg. I've built this with clang 16.0.6 (debian). And it seems v6.11-rc3 also has this issue. Can you also check it?

Thank you,

Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
maintainer
#1 lore raw
Quoted 2 lines · On Mon, 19 Aug 2024 17:11:52 +0900 Masami Hiramatsu (Google)…

CONFIG_LTO=y
CONFIG_LTO_CLANG=y

Hi Masami,

Does it still fail if you disable the above?

I wonder if that causes functions to not be part of the available filter functions that the ftrace filter test is using :-/

We may need to add "noinline" or something to make sure those functions don't get inlined for LTO.

-- Steve

author maintainer
#2 lore raw
Quoted 9 lines · On Mon, 19 Aug 2024 11:29:02 -0400 Steven Rostedt <[email protected]> wrote:
Quoted 2 lines · On Mon, 19 Aug 2024 17:11:52 +0900 Masami Hiramatsu (Google)…

CONFIG_LTO=y
CONFIG_LTO_CLANG=y

Hi Masami,

Does it still fail if you disable the above?

No, I found that caused these failure.

Quoted 2 lines

I wonder if that causes functions to not be part of the available filter functions that the ftrace filter test is using :-/

Ah, LTO optimized the target function call... (same as ftrace-direct example)

Quoted 3 lines

We may need to add "noinline" or something to make sure those functions don't get inlined for LTO.

Yeah, we need such option at least for function call test.

Thank you,

Quoted 2 lines

-- Steve

Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
maintainer
#3 lore raw
Quoted 6 lines · On Tue, 20 Aug 2024 00:56:49 +0900 Masami Hiramatsu (Google)…
Quoted 4 lines

We may need to add "noinline" or something to make sure those functions don't get inlined for LTO.

Yeah, we need such option at least for function call test.

Could you add the noinline, and if it fixes the issue send a patch?

Thanks,

-- Steve

author maintainer
#4 lore raw
Quoted 10 lines · On Mon, 19 Aug 2024 12:02:44 -0400 Steven Rostedt <[email protected]> wrote:
Quoted 6 lines · On Tue, 20 Aug 2024 00:56:49 +0900 Masami Hiramatsu (Google)…
Quoted 4 lines

We may need to add "noinline" or something to make sure those functions don't get inlined for LTO.

Yeah, we need such option at least for function call test.

Could you add the noinline, and if it fixes the issue send a patch?

OK, BTW, I got warnings from kmemleak when I disabled LTO in the selftest. Let me fix it too.

Thanks,

Quoted 5 lines

Thanks,

-- Steve

Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
author maintainer
#5 lore raw
In reply to Steven Rostedt
Quoted 10 lines · On Mon, 19 Aug 2024 12:02:44 -0400 Steven Rostedt <[email protected]> wrote:
Quoted 6 lines · On Tue, 20 Aug 2024 00:56:49 +0900 Masami Hiramatsu (Google)…
Quoted 4 lines

We may need to add "noinline" or something to make sure those functions don't get inlined for LTO.

Yeah, we need such option at least for function call test.

Could you add the noinline, and if it fixes the issue send a patch?

I found the target function already has "noinline". I tried to add noinline to the testing function (callsite), but it also did not work.
I think "noinline" is for the compiler, but LTO is done by the linker.

I found a discussion similar to this problem, but it seems very hacky.

https://groups.google.com/g/llvm-dev/c/6baI9LISmSU/m/uEeY_CRbBQAJ?pli=1

Thank you,

Quoted 5 lines

Thanks,

-- Steve

Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
#6 lore raw
Quoted 10 lines · Masami Hiramatsu
Quoted 7 lines · On Mon, 19 Aug 2024 12:02:44 -0400 Steven Rostedt <[email protected]> wrote:
Quoted 5 lines · On Tue, 20 Aug 2024 00:56:49 +0900 Masami Hiramatsu (Google)…
Quoted 4 lines

We may need to add "noinline" or something to make sure those functions don't get inlined for LTO.

Quoted 7 lines
Quoted 3 lines
Quoted 1 line

Yeah, we need such option at least for function call test.

Could you add the noinline, and if it fixes the issue send a patch?

I found the target function already has "noinline". I tried to add noinline to the testing function (callsite), but it also did not work.
I think "noinline" is for the compiler, but LTO is done by the linker.

If LTO is breaking noinline, then that has much larger implications for noinstr code and similar, and means that LTO is unsound...

Quoted 3 lines

I found a discussion similar to this problem, but it seems very hacky.

https://groups.google.com/g/llvm-dev/c/6baI9LISmSU/m/uEeY_CRbBQAJ?pli=1

Thanks for the link!

Mark.

maintainer
#7 lore raw
Quoted 6 lines · On Tue, 20 Aug 2024 11:48:07 +0100 Mark Rutland <[email protected]> wrote:
Quoted 3 lines

I found the target function already has "noinline". I tried to add noinline to the testing function (callsite), but it also did not work.
I think "noinline" is for the compiler, but LTO is done by the linker.

If LTO is breaking noinline, then that has much larger implications for noinstr code and similar, and means that LTO is unsound...

Hmm, doesn't noinstr place the code in a separate section?

I wonder if we create a separate section for the test function that LTO is inlining, if it will prevent it from being inlined. That is, noinline tells the compiler not to inline, but LTO happens after the compiler is done and may inline functions in the same section. But the linker does see separate sections and I don't think it will try to inline that code into another section. But I could be wrong. ;-)

-- Steve

#8 lore raw
In reply to Mark Rutland
Quoted 14 lines · Mark Rutland
Quoted 12 lines · Masami Hiramatsu
Quoted 9 lines · On Mon, 19 Aug 2024 12:02:44 -0400 Steven Rostedt <[email protected]> wrote:
Quoted 6 lines · On Tue, 20 Aug 2024 00:56:49 +0900 Masami Hiramatsu (Google)…
Quoted 4 lines

We may need to add "noinline" or something to make sure those functions don't get inlined for LTO.

Yeah, we need such option at least for function call test.

Quoted 8 lines
Quoted 5 lines
Quoted 1 line

Could you add the noinline, and if it fixes the issue send a patch?

I found the target function already has "noinline". I tried to add noinline to the testing function (callsite), but it also did not work.
I think "noinline" is for the compiler, but LTO is done by the linker.

If LTO is breaking noinline, then that has much larger implications for noinstr code and similar, and means that LTO is unsound...

The noinline attribute is preserved in LLVM IR, so it should continue to work with LTO. Which function are we talking about here? Are you sure the function was inlined instead of being dropped completely? Does marking the function __used help?

Sami

maintainer
#9 lore raw
Quoted 21 lines · On Tue, 20 Aug 2024 08:10:42 -0700 Sami Tolvanen <[email protected]>…
Quoted 20 lines · Mark Rutland
Quoted 17 lines · Masami Hiramatsu
Quoted 10 lines · On Mon, 19 Aug 2024 12:02:44 -0400 Steven Rostedt <[email protected]> wrote:
Quoted 6 lines · On Tue, 20 Aug 2024 00:56:49 +0900 Masami Hiramatsu (Google)…
Quoted 4 lines

We may need to add "noinline" or something to make sure those functions don't get inlined for LTO.

Yeah, we need such option at least for function call test.

Could you add the noinline, and if it fixes the issue send a patch?

I found the target function already has "noinline". I tried to add noinline to the testing function (callsite), but it also did not work.
I think "noinline" is for the compiler, but LTO is done by the linker.

Quoted 7 lines
Quoted 2 lines

If LTO is breaking noinline, then that has much larger implications for noinstr code and similar, and means that LTO is unsound...

The noinline attribute is preserved in LLVM IR, so it should continue to work with LTO. Which function are we talking about here? Are you sure the function was inlined instead of being dropped completely? Does marking the function __used help?

Hmm, maybe it was dropped, as the functions are basically just stubs.

Masami, can you add __used to the functions in trace_selftests_dynamic.c and see if that fixes it?

-- Steve

author maintainer
#10 lore raw
In reply to Sami Tolvanen
Quoted 21 lines · On Tue, 20 Aug 2024 08:10:42 -0700 Sami Tolvanen <[email protected]>…
Quoted 20 lines · Mark Rutland
Quoted 17 lines · Masami Hiramatsu
Quoted 10 lines · On Mon, 19 Aug 2024 12:02:44 -0400 Steven Rostedt <[email protected]> wrote:
Quoted 6 lines · On Tue, 20 Aug 2024 00:56:49 +0900 Masami Hiramatsu (Google)…
Quoted 4 lines

We may need to add "noinline" or something to make sure those functions don't get inlined for LTO.

Yeah, we need such option at least for function call test.

Could you add the noinline, and if it fixes the issue send a patch?

I found the target function already has "noinline". I tried to add noinline to the testing function (callsite), but it also did not work.
I think "noinline" is for the compiler, but LTO is done by the linker.

Quoted 7 lines
Quoted 2 lines

If LTO is breaking noinline, then that has much larger implications for noinstr code and similar, and means that LTO is unsound...

The noinline attribute is preserved in LLVM IR, so it should continue to work with LTO. Which function are we talking about here? Are you sure the function was inlined instead of being dropped completely? Does marking the function __used help?

We are talking about trace_selftest_startup_dynamic_tracing() in kernel/trace/trace_selftest.c. The callee is func() which is actually DYN_FTRACE_TEST_NAME() in kernel/trace/trace_selftest_dynamic.c. That function passed as pointer (but the compiler can embed it by constant propagation.)

Does the noinline attribute prevent embedding callsite too? I mean

extern callee()

noinline callee()
{
...
}

caller()

{
	callee() // (*)
}

In this case, does noinline prevent LTO to embed the callee at the callsite(*) or prevent LTO remove the callee() symbol?

Thank you,

Quoted 3 lines

Sami

Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
maintainer
#11 lore raw
Quoted 9 lines · On Wed, 21 Aug 2024 07:05:39 +0900 Masami Hiramatsu (Google)…

Does the noinline attribute prevent embedding callsite too? I mean

extern callee()

noinline callee()
{
...
}

Quoted 8 lines

caller()

{
	callee() // (*)
}

In this case, does noinline prevent LTO to embed the callee at the callsite(*) or prevent LTO remove the callee() symbol?

Even though we have it passed as a parameter, I think the compiler and linker is smart enough to see that and notice its use, and that the function passed in is a nop, which doesn't break the flow.

Can you add the __used and see if it fixes it?

-- Steve

author maintainer
#12 lore raw
Quoted 22 lines · On Tue, 20 Aug 2024 18:11:09 -0400 Steven Rostedt <[email protected]> wrote:
Quoted 9 lines · On Wed, 21 Aug 2024 07:05:39 +0900 Masami Hiramatsu (Google)…

Does the noinline attribute prevent embedding callsite too? I mean

extern callee()

noinline callee()
{
...
}

Quoted 8 lines

caller()

{
	callee() // (*)
}

In this case, does noinline prevent LTO to embed the callee at the callsite(*) or prevent LTO remove the callee() symbol?

Quoted 5 lines

Even though we have it passed as a parameter, I think the compiler and linker is smart enough to see that and notice its use, and that the function passed in is a nop, which doesn't break the flow.

Can you add the __used and see if it fixes it?

Adding __used to DYN_FTRACE_TEST_NAME() and DYN_FTRACE_TEST_NAME2() does not change, the test still fails. Hmm, what about makes the caller (trace_selftest_startup_dynamic_tracing()) called via a function pointer? In that case, wouldn't it be subject to constant propagetion?

Let me try.

Thanks,

Quoted 2 lines

-- Steve

Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
maintainer
#13 lore raw
Quoted 4 lines · On Wed, 21 Aug 2024 08:43:51 +0900 Masami Hiramatsu (Google)…
Quoted 1 line

Can you add the __used and see if it fixes it?

Adding __used to DYN_FTRACE_TEST_NAME() and DYN_FTRACE_TEST_NAME2() does not change, the test still fails.

OK, now that sounds like a bug in LTO itself.

-- Steve

author maintainer
#14 lore raw
Quoted 9 lines · On Tue, 20 Aug 2024 19:49:14 -0400 Steven Rostedt <[email protected]> wrote:
Quoted 4 lines · On Wed, 21 Aug 2024 08:43:51 +0900 Masami Hiramatsu (Google)…
Quoted 1 line

Can you add the __used and see if it fixes it?

Adding __used to DYN_FTRACE_TEST_NAME() and DYN_FTRACE_TEST_NAME2() does not change, the test still fails.

OK, now that sounds like a bug in LTO itself.

Hmm, I think __used just preserve the function exists as a function. But the callsite can be optimized. This mean the __used function code is duplicated, and embedded one copy in the callsite, but keep another copy as a function which can be used from outside. But "noinline" attribute seems to be expected not embedded in the callsite (because that is what "inlined" mean), so it looks like a bug.

Thank you,

Quoted 2 lines

-- Steve

Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
author maintainer
#15 lore raw
In reply to Masami Hiramatsu (Google)
Quoted 29 lines · On Wed, 21 Aug 2024 08:43:51 +0900 Masami Hiramatsu (Google)…
Quoted 26 lines · On Tue, 20 Aug 2024 18:11:09 -0400 Steven Rostedt <[email protected]> wrote:
Quoted 9 lines · On Wed, 21 Aug 2024 07:05:39 +0900 Masami Hiramatsu (Google)…

Does the noinline attribute prevent embedding callsite too? I mean

extern callee()

noinline callee()
{
...
}

Quoted 8 lines

caller()

{
	callee() // (*)
}

In this case, does noinline prevent LTO to embed the callee at the callsite(*) or prevent LTO remove the callee() symbol?

Even though we have it passed as a parameter, I think the compiler and linker is smart enough to see that and notice its use, and that the function passed in is a nop, which doesn't break the flow.

Quoted 8 lines
Quoted 1 line

Can you add the __used and see if it fixes it?

Adding __used to DYN_FTRACE_TEST_NAME() and DYN_FTRACE_TEST_NAME2() does not change, the test still fails. Hmm, what about makes the caller (trace_selftest_startup_dynamic_tracing()) called via a function pointer? In that case, wouldn't it be subject to constant propagetion?

Let me try.

OK, it is succeeded! Calling caller via global function pointer makes it run as we expected. It passed the dynamic_ftrace test, but other tests still fails. Those need to be called via function pointer too.

[    1.851324] Testing dynamic ftrace: PASSED                                     
[    2.083329] Testing dynamic ftrace ops #1:                                     
[    2.173751] (0 0 0 0 0) FAILED!                                                
[    2.182337] ------------[ cut here ]------------                               
[    2.183323] WARNING: CPU: 0 PID: 1 at kernel/trace/trace.c:2143 run_tracer_sel0
[    2.184323] Modules linked in:                              

Anyway, here is what I did.

kernel/trace/trace_selftest.c +18 -5
index 97f1e4bc47dc..9663bc777888 100644
@@ -353,9 +353,10 @@ static int trace_selftest_ops(struct trace_array *tr, int cnt)
 }
 
 /* Test dynamic code modification and ftrace filters */
-static int trace_selftest_startup_dynamic_tracing(struct tracer *trace,
-						  struct trace_array *tr,
-						  int (*func)(void))
+static int noinline
+trace_selftest_startup_dynamic_tracing(struct tracer *trace,
+					struct trace_array *tr,
+					int (*func)(void))
 {
 	int save_ftrace_enabled = ftrace_enabled;
 	unsigned long count;
@@ -569,10 +570,22 @@ trace_selftest_function_recursion(void)
 	return ret;
 }
 #else
-# define trace_selftest_startup_dynamic_tracing(trace, tr, func) ({ 0; })
+static int trace_selftest_startup_dynamic_tracing(struct tracer *trace,
+					struct trace_array *tr,
+					int (*func)(void))
+{
+	if (!trace || !tr || !func)
+		return -EINVAL;
+	return 0;
+}
 # define trace_selftest_function_recursion() ({ 0; })
 #endif /* CONFIG_DYNAMIC_FTRACE */
 
+int (*global_trace_selftest_startup_dynamic_tracing)(struct tracer *trace,
+					struct trace_array *tr,
+					int (*func)(void))
+	= trace_selftest_startup_dynamic_tracing;
+
 static enum {
 	TRACE_SELFTEST_REGS_START,
 	TRACE_SELFTEST_REGS_FOUND,
@@ -732,7 +745,7 @@ trace_selftest_startup_function(struct tracer *trace, struct trace_array *tr)
 		goto out;
 	}
 
-	ret = trace_selftest_startup_dynamic_tracing(trace, tr,
+	ret = global_trace_selftest_startup_dynamic_tracing(trace, tr,
 						     DYN_FTRACE_TEST_NAME);
 	if (ret)
 		goto out;
Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
#16 lore raw
Quoted 40 lines · Masami Hiramatsu
Quoted 36 lines · On Wed, 21 Aug 2024 08:43:51 +0900 Masami Hiramatsu (Google)…
Quoted 22 lines · On Tue, 20 Aug 2024 18:11:09 -0400 Steven Rostedt <[email protected]> wrote:
Quoted 9 lines · On Wed, 21 Aug 2024 07:05:39 +0900 Masami Hiramatsu (Google)…

Does the noinline attribute prevent embedding callsite too? I mean

extern callee()

noinline callee()
{
...
}

Quoted 8 lines

caller()
{
callee() // (*)
}

In this case, does noinline prevent LTO to embed the callee at the callsite(*) or prevent LTO remove the callee() symbol?

Quoted 5 lines

Even though we have it passed as a parameter, I think the compiler and linker is smart enough to see that and notice its use, and that the function passed in is a nop, which doesn't break the flow.

Can you add the __used and see if it fixes it?

Adding __used to DYN_FTRACE_TEST_NAME() and DYN_FTRACE_TEST_NAME2() does not change, the test still fails. Hmm, what about makes the caller (trace_selftest_startup_dynamic_tracing()) called via a function pointer? In that case, wouldn't it be subject to constant propagetion?

Quoted 5 lines
Quoted 1 line

Let me try.

OK, it is succeeded! Calling caller via global function pointer makes it run as we expected. It passed the dynamic_ftrace test, but other tests still fails. Those need to be called via function pointer too.

I was able to reproduce this with LTO_CLANG_FULL, and it indeed optimizes away the direct calls to the empty stubs, but the functions are still there. LTO_CLANG_THIN doesn't perform this optimization, presumably because it doesn't have full visibility to the other TU.

Sami

#17 lore raw
In reply to Masami Hiramatsu (Google)
Quoted 32 lines · Masami Hiramatsu
Quoted 21 lines · On Tue, 20 Aug 2024 08:10:42 -0700 Sami Tolvanen <[email protected]>…
Quoted 20 lines · Mark Rutland
Quoted 17 lines · Masami Hiramatsu
Quoted 10 lines · On Mon, 19 Aug 2024 12:02:44 -0400 Steven Rostedt <[email protected]> wrote:
Quoted 6 lines · On Tue, 20 Aug 2024 00:56:49 +0900 Masami Hiramatsu (Google)…
Quoted 4 lines

We may need to add "noinline" or something to make sure those functions don't get inlined for LTO.

Yeah, we need such option at least for function call test.

Could you add the noinline, and if it fixes the issue send a patch?

I found the target function already has "noinline". I tried to add noinline to the testing function (callsite), but it also did not work.
I think "noinline" is for the compiler, but LTO is done by the linker.

Quoted 7 lines
Quoted 2 lines

If LTO is breaking noinline, then that has much larger implications for noinstr code and similar, and means that LTO is unsound...

The noinline attribute is preserved in LLVM IR, so it should continue to work with LTO. Which function are we talking about here? Are you sure the function was inlined instead of being dropped completely? Does marking the function __used help?

Quoted 5 lines

We are talking about trace_selftest_startup_dynamic_tracing() in kernel/trace/trace_selftest.c. The callee is func() which is actually DYN_FTRACE_TEST_NAME() in kernel/trace/trace_selftest_dynamic.c. That function passed as pointer (but the compiler can embed it by constant propagation.)

Ah, so IIUC the function isn't being inlined; the call is being optimized away becase callee() has no side-effects.

That can happen without LTO if the caller is in the same compilation unit, and I have worked around that in the past by adding a barrier() into the callee.

If you add a barrier() into the callee, does it work? I suspect that's a reasonable workaround.

Mark.

#18 lore raw
Quoted 39 lines · Mark Rutland
Quoted 32 lines · Masami Hiramatsu
Quoted 21 lines · On Tue, 20 Aug 2024 08:10:42 -0700 Sami Tolvanen <[email protected]>…
Quoted 20 lines · Mark Rutland
Quoted 17 lines · Masami Hiramatsu
Quoted 10 lines · On Mon, 19 Aug 2024 12:02:44 -0400 Steven Rostedt <[email protected]> wrote:
Quoted 6 lines · On Tue, 20 Aug 2024 00:56:49 +0900 Masami Hiramatsu (Google)…
Quoted 4 lines

We may need to add "noinline" or something to make sure those functions don't get inlined for LTO.

Yeah, we need such option at least for function call test.

Could you add the noinline, and if it fixes the issue send a patch?

I found the target function already has "noinline". I tried to add noinline to the testing function (callsite), but it also did not work.
I think "noinline" is for the compiler, but LTO is done by the linker.

Quoted 7 lines
Quoted 2 lines

If LTO is breaking noinline, then that has much larger implications for noinstr code and similar, and means that LTO is unsound...

The noinline attribute is preserved in LLVM IR, so it should continue to work with LTO. Which function are we talking about here? Are you sure the function was inlined instead of being dropped completely? Does marking the function __used help?

Quoted 5 lines

We are talking about trace_selftest_startup_dynamic_tracing() in kernel/trace/trace_selftest.c. The callee is func() which is actually DYN_FTRACE_TEST_NAME() in kernel/trace/trace_selftest_dynamic.c. That function passed as pointer (but the compiler can embed it by constant propagation.)

Quoted 6 lines

Ah, so IIUC the function isn't being inlined; the call is being optimized away becase callee() has no side-effects.

That can happen without LTO if the caller is in the same compilation unit, and I have worked around that in the past by adding a barrier() into the callee.

FWIW, that was in samples/ftrace/ftrace-ops.c, where tracee_relevant() and tracee_irrelevant() have the barrier():

| /*
|  * Marked as noinline to ensure that an out-of-line traceable copy is
|  * generated by the compiler.
|  *
|  * The barrier() ensures the compiler won't elide calls by determining there
|  * are no side-effects.
|  */
| static noinline void tracee_relevant(void)
| {
|         barrier();
| }

... so we already have precedent for that in tracing code.

Mark.

maintainer
#19 lore raw
Quoted 15 lines · On Wed, 21 Aug 2024 16:42:07 +0100 Mark Rutland <[email protected]> wrote:

FWIW, that was in samples/ftrace/ftrace-ops.c, where tracee_relevant() and tracee_irrelevant() have the barrier():

| /*
|  * Marked as noinline to ensure that an out-of-line traceable copy is
|  * generated by the compiler.
|  *
|  * The barrier() ensures the compiler won't elide calls by determining there
|  * are no side-effects.
|  */
| static noinline void tracee_relevant(void)
| {
|         barrier();
| }
Quoted 1 line

... so we already have precedent for that in tracing code.

If adding a barrier() fixes things, then I'm 100% for adding them.

Masami, does this fix it?

-- Steve

kernel/trace/trace_selftest_dynamic.c +10 -2
index c364cf777e1a..a112433d78ce 100644
@@ -4,12 +4,20 @@
 
 noinline __noclone int DYN_FTRACE_TEST_NAME(void)
 {
-	/* used to call mcount */
+	/*
+	 * Used to test function tracing. Make sure the compiler doesn't
+	 * remove it.
+	 */
+	barrier();
 	return 0;
 }
 
 noinline __noclone int DYN_FTRACE_TEST_NAME2(void)
 {
-	/* used to call mcount */
+	/*
+	 * Used to test function tracing. Make sure the compiler doesn't
+	 * remove it.
+	 */
+	barrier();
 	return 0;
 }
author maintainer Tested-by
#20 lore raw
Quoted 18 lines · On Wed, 21 Aug 2024 11:50:00 -0400 Steven Rostedt <[email protected]> wrote:
Quoted 15 lines · On Wed, 21 Aug 2024 16:42:07 +0100 Mark Rutland <[email protected]> wrote:

FWIW, that was in samples/ftrace/ftrace-ops.c, where tracee_relevant() and tracee_irrelevant() have the barrier():

| /*
|  * Marked as noinline to ensure that an out-of-line traceable copy is
|  * generated by the compiler.
|  *
|  * The barrier() ensures the compiler won't elide calls by determining there
|  * are no side-effects.
|  */
| static noinline void tracee_relevant(void)
| {
|         barrier();
| }
Quoted 5 lines
Quoted 1 line

... so we already have precedent for that in tracing code.

If adding a barrier() fixes things, then I'm 100% for adding them.

Masami, does this fix it?

Interesting, this fixes the issue!
And it is great to find how to avoid the LTO removes function like this.

Thank you,

Quoted 30 lines

-- Steve

kernel/trace/trace_selftest_dynamic.c +10 -2
index c364cf777e1a..a112433d78ce 100644
@@ -4,12 +4,20 @@
 
 noinline __noclone int DYN_FTRACE_TEST_NAME(void)
 {
-	/* used to call mcount */
+	/*
+	 * Used to test function tracing. Make sure the compiler doesn't
+	 * remove it.
+	 */
+	barrier();
 	return 0;
 }
 
 noinline __noclone int DYN_FTRACE_TEST_NAME2(void)
 {
-	/* used to call mcount */
+	/*
+	 * Used to test function tracing. Make sure the compiler doesn't
+	 * remove it.
+	 */
+	barrier();
 	return 0;
 }
Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>