[PATCH] selftests/ftrace: Account for fprobe attachment at creation

Cao Ruichuang posted 1 patch 2 months, 1 week ago
.../test.d/dynevent/add_remove_fprobe.tc      | 28 +++++++++++++------
1 file changed, 19 insertions(+), 9 deletions(-)
[PATCH] selftests/ftrace: Account for fprobe attachment at creation
Posted by Cao Ruichuang 2 months, 1 week ago
add_remove_fprobe.tc assumes that enabling an fprobe event is what adds
its target function to enabled_functions.

On the current kernel, the fprobe target already appears in
enabled_functions as soon as the event is created, and enabling the
event does not change that count again. That makes the test fail even
though the event lifecycle itself works.

Record the attachment baseline after creating the probe events and only
check that enabling them keeps the expected functions attached. The
cleanup checks still verify that removing the events returns
enabled_functions to its original state.

Signed-off-by: Cao Ruichuang <create0818@163.com>
---
 .../test.d/dynevent/add_remove_fprobe.tc      | 28 +++++++++++++------
 1 file changed, 19 insertions(+), 9 deletions(-)

diff --git a/tools/testing/selftests/ftrace/test.d/dynevent/add_remove_fprobe.tc b/tools/testing/selftests/ftrace/test.d/dynevent/add_remove_fprobe.tc
index 47067a5e3..ff08bd1ac 100644
--- a/tools/testing/selftests/ftrace/test.d/dynevent/add_remove_fprobe.tc
+++ b/tools/testing/selftests/ftrace/test.d/dynevent/add_remove_fprobe.tc
@@ -26,23 +26,29 @@ grep -q myevent2 dynamic_events
 grep -q myevent3 dynamic_events
 test -d events/fprobes/myevent1
 test -d events/fprobes/myevent2
-
-echo 1 > events/fprobes/myevent1/enable
-# Make sure the event is attached.
 grep -q $PLACE enabled_functions
+grep -q $PLACE2 enabled_functions
 cnt=`cat enabled_functions | wc -l`
-if [ $cnt -eq $ocnt ]; then
+if [ $cnt -le $ocnt ]; then
+	exit_fail
+fi
+
+echo 1 > events/fprobes/myevent1/enable
+cnt1=`cat enabled_functions | wc -l`
+if [ $cnt1 -ne $cnt ]; then
 	exit_fail
 fi
 
 echo 1 > events/fprobes/myevent2/enable
 cnt2=`cat enabled_functions | wc -l`
+if [ $cnt2 -ne $cnt1 ]; then
+	exit_fail
+fi
 
 echo 1 > events/fprobes/myevent3/enable
-# If the function is different, the attached function should be increased
 grep -q $PLACE2 enabled_functions
 cnt=`cat enabled_functions | wc -l`
-if [ $cnt -eq $cnt2 ]; then
+if [ $cnt -ne $cnt2 ]; then
 	exit_fail
 fi
 
@@ -62,11 +68,15 @@ if [ $cnt -ne $ocnt ]; then
 fi
 
 echo "f:myevent4 $PLACE" >> dynamic_events
+grep -q $PLACE enabled_functions
+cnt=`cat enabled_functions | wc -l`
+if [ $cnt -le $ocnt ]; then
+	exit_fail
+fi
 
 echo 1 > events/fprobes/myevent4/enable
-# Should only have one enabled
-cnt=`cat enabled_functions | wc -l`
-if [ $cnt -ne $((ocnt + 1)) ]; then
+cnt2=`cat enabled_functions | wc -l`
+if [ $cnt2 -ne $cnt ]; then
 	exit_fail
 fi
 
-- 
2.39.5 (Apple Git-154)
Re: [PATCH] selftests/ftrace: Account for fprobe attachment at creation
Posted by Masami Hiramatsu (Google) 2 months, 1 week ago
On Tue,  7 Apr 2026 19:57:51 +0800
Cao Ruichuang <create0818@163.com> wrote:

> add_remove_fprobe.tc assumes that enabling an fprobe event is what adds
> its target function to enabled_functions.
> 
> On the current kernel, the fprobe target already appears in
> enabled_functions as soon as the event is created, and enabling the
> event does not change that count again. That makes the test fail even
> though the event lifecycle itself works.
> 
> Record the attachment baseline after creating the probe events and only
> check that enabling them keeps the expected functions attached. The
> cleanup checks still verify that removing the events returns
> enabled_functions to its original state.
> 

Hmm, did you run this test with not clean environment?
It is not designed to do so.
(I think we should not check ocnt, or if there are any entries,
 we should stop this test as unresolved.)

Thank you,

> Signed-off-by: Cao Ruichuang <create0818@163.com>
> ---
>  .../test.d/dynevent/add_remove_fprobe.tc      | 28 +++++++++++++------
>  1 file changed, 19 insertions(+), 9 deletions(-)
> 
> diff --git a/tools/testing/selftests/ftrace/test.d/dynevent/add_remove_fprobe.tc b/tools/testing/selftests/ftrace/test.d/dynevent/add_remove_fprobe.tc
> index 47067a5e3..ff08bd1ac 100644
> --- a/tools/testing/selftests/ftrace/test.d/dynevent/add_remove_fprobe.tc
> +++ b/tools/testing/selftests/ftrace/test.d/dynevent/add_remove_fprobe.tc
> @@ -26,23 +26,29 @@ grep -q myevent2 dynamic_events
>  grep -q myevent3 dynamic_events
>  test -d events/fprobes/myevent1
>  test -d events/fprobes/myevent2
> -
> -echo 1 > events/fprobes/myevent1/enable
> -# Make sure the event is attached.
>  grep -q $PLACE enabled_functions
> +grep -q $PLACE2 enabled_functions
>  cnt=`cat enabled_functions | wc -l`
> -if [ $cnt -eq $ocnt ]; then
> +if [ $cnt -le $ocnt ]; then
> +	exit_fail
> +fi
> +
> +echo 1 > events/fprobes/myevent1/enable
> +cnt1=`cat enabled_functions | wc -l`
> +if [ $cnt1 -ne $cnt ]; then
>  	exit_fail
>  fi
>  
>  echo 1 > events/fprobes/myevent2/enable
>  cnt2=`cat enabled_functions | wc -l`
> +if [ $cnt2 -ne $cnt1 ]; then
> +	exit_fail
> +fi
>  
>  echo 1 > events/fprobes/myevent3/enable
> -# If the function is different, the attached function should be increased
>  grep -q $PLACE2 enabled_functions
>  cnt=`cat enabled_functions | wc -l`
> -if [ $cnt -eq $cnt2 ]; then
> +if [ $cnt -ne $cnt2 ]; then
>  	exit_fail
>  fi
>  
> @@ -62,11 +68,15 @@ if [ $cnt -ne $ocnt ]; then
>  fi
>  
>  echo "f:myevent4 $PLACE" >> dynamic_events
> +grep -q $PLACE enabled_functions
> +cnt=`cat enabled_functions | wc -l`
> +if [ $cnt -le $ocnt ]; then
> +	exit_fail
> +fi
>  
>  echo 1 > events/fprobes/myevent4/enable
> -# Should only have one enabled
> -cnt=`cat enabled_functions | wc -l`
> -if [ $cnt -ne $((ocnt + 1)) ]; then
> +cnt2=`cat enabled_functions | wc -l`
> +if [ $cnt2 -ne $cnt ]; then
>  	exit_fail
>  fi
>  
> -- 
> 2.39.5 (Apple Git-154)
> 


-- 
Masami Hiramatsu (Google) <mhiramat@kernel.org>
Re: [PATCH] selftests/ftrace: Account for fprobe attachment at creation
Posted by Cao Ruichuang 2 months ago
Hi Masami,

I reran this in a clean QEMU boot environment with only the minimal
tracefs setup and the add_remove_fprobe sequence.

I saw the same behavior there: after creating myevent1/2/3,
enabled_functions went from 2 to 4 before any event was enabled, and
enabling myevent1/2/3 did not increase the count further. After cleanup,
it returned to the original baseline again.

So this does not look like a dirty tracing environment issue. The
failing testcase assumption appears to be that the attachment happens on
enable, while on the current kernel it is already visible after create.

Thanks,
Cao Ruichuang
Re: [PATCH] selftests/ftrace: Account for fprobe attachment at creation
Posted by Masami Hiramatsu (Google) 2 months ago
On Thu, 09 Apr 2026 13:18:19 +0800
Cao Ruichuang <create0818@163.com> wrote:

> Hi Masami,
> 
> I reran this in a clean QEMU boot environment with only the minimal
> tracefs setup and the add_remove_fprobe sequence.
> 
> I saw the same behavior there: after creating myevent1/2/3,
> enabled_functions went from 2 to 4 before any event was enabled, and
> enabling myevent1/2/3 did not increase the count further. After cleanup,
> it returned to the original baseline again.

Hmm, that does not mean test failure but could be a bug.
Can you dump the enabled_functions and share it when the
test failed?

> So this does not look like a dirty tracing environment issue. The
> failing testcase assumption appears to be that the attachment happens on
> enable, while on the current kernel it is already visible after create.

What kernel are you using? I need to check why this happens.
I guess there is a change which introduces this issue.

Thank you,

> 
> Thanks,
> Cao Ruichuang
> 


-- 
Masami Hiramatsu (Google) <mhiramat@kernel.org>
Re: [PATCH] selftests/ftrace: Account for fprobe attachment at creation
Posted by Cao Ruichuang 2 months ago
Hi Masami,

I reran this in clean QEMU on two kernels and got different results.

1. Ubuntu distro kernel:
   Linux 6.8.0-100-generic #100-Ubuntu SMP PREEMPT_DYNAMIC
   Tue Jan 13 16:40:06 UTC 2026

   baseline count=2
   after_create123 count=4
   after_enable1/2/3 count=4

   baseline enabled_functions:
     __hid_bpf_tail_call ...

   after_create123 enabled_functions:
     kernel_clone (2) R ->arch_ftrace_ops_list_func+0x0/0x280
     kmem_cache_free (1) R tramp: ... ->fprobe_handler+0x0/0x40
     __hid_bpf_tail_call ...

2. Current source-tree kernel built from the clean snapshot of my patch
   branch:
   Linux 7.0.0-rc6 #2 SMP PREEMPT_DYNAMIC Fri Apr 10 12:19:39 CST 2026

   baseline count=0
   after_create123 count=0
   after_enable1 count=1
   after_enable2 count=1
   after_enable3 count=2

   after_create123 enabled_functions:
     <empty>

   after_enable3 enabled_functions:
     kernel_clone (2) ->arch_ftrace_ops_list_func+0x0/0x200
     kmem_cache_free (1) tramp: ... ->fprobe_ftrace_entry+0x0/0x220

So the behavior I reported earlier reproduces on that Ubuntu 6.8 kernel,
but not on the current source-tree kernel. I think my earlier conclusion
was too broad.

I will stop pushing this testcase change for now unless I can narrow down
which kernel change caused the difference.

Thanks,
Cao Ruichuang
Re: [PATCH] selftests/ftrace: Account for fprobe attachment at creation
Posted by Masami Hiramatsu (Google) 2 months ago
On Fri, 10 Apr 2026 12:32:43 +0800
Cao Ruichuang <create0818@163.com> wrote:

> Hi Masami,
> 
> I reran this in clean QEMU on two kernels and got different results.
> 
> 1. Ubuntu distro kernel:
>    Linux 6.8.0-100-generic #100-Ubuntu SMP PREEMPT_DYNAMIC
>    Tue Jan 13 16:40:06 UTC 2026
> 
>    baseline count=2
>    after_create123 count=4
>    after_enable1/2/3 count=4
> 
>    baseline enabled_functions:
>      __hid_bpf_tail_call ...
> 
>    after_create123 enabled_functions:
>      kernel_clone (2) R ->arch_ftrace_ops_list_func+0x0/0x280
>      kmem_cache_free (1) R tramp: ... ->fprobe_handler+0x0/0x40
>      __hid_bpf_tail_call ...
> 
> 2. Current source-tree kernel built from the clean snapshot of my patch
>    branch:
>    Linux 7.0.0-rc6 #2 SMP PREEMPT_DYNAMIC Fri Apr 10 12:19:39 CST 2026
> 
>    baseline count=0
>    after_create123 count=0
>    after_enable1 count=1
>    after_enable2 count=1
>    after_enable3 count=2
> 
>    after_create123 enabled_functions:
>      <empty>
> 
>    after_enable3 enabled_functions:
>      kernel_clone (2) ->arch_ftrace_ops_list_func+0x0/0x200
>      kmem_cache_free (1) tramp: ... ->fprobe_ftrace_entry+0x0/0x220
> 
> So the behavior I reported earlier reproduces on that Ubuntu 6.8 kernel,
> but not on the current source-tree kernel. I think my earlier conclusion
> was too broad.

Thanks for reporting the difference of behaviors.

> 
> I will stop pushing this testcase change for now unless I can narrow down
> which kernel change caused the difference.

OK.

Thanks!

> 
> Thanks,
> Cao Ruichuang
> 


-- 
Masami Hiramatsu (Google) <mhiramat@kernel.org>
Re: [PATCH] selftests/ftrace: Account for fprobe attachment at creation
Posted by Cao Ruichuang 2 months, 1 week ago
Thanks, that is a good point.

I need to separate a clean source tree from a clean tracing runtime
environment. I will rerun this in a fresh tracing environment and
re-evaluate the testcase assumption before pushing anything further.

If this testcase is only valid for a clean environment, then an
unresolved check or a different precondition will make more sense
than the current patch.

Thanks,
Cao Ruichuang
[PATCH v2] selftests/ftrace: Account for fprobe attachment before enable
Posted by Cao Ruichuang 2 months, 1 week ago
add_remove_fprobe.tc assumes that enabling an fprobe event is what
adds its target function to enabled_functions.

After resetting tracing state with initialize_system(), the current
kernel still attaches the target functions as soon as the probe events
are created. Enabling the three events does not increase the
enabled_functions count further, and the original testcase fails on the
check after enabling myevent3.

Record the baseline after creating the fprobe events and verify that
subsequent enables keep that attachment set unchanged.

Signed-off-by: Cao Ruichuang <create0818@163.com>
---
v2:
- rerun in a reset tracing environment after initialize_system()
- keep the fix focused on the failing myevent3 assumption and drop the
  unnecessary myevent4 changes

 .../ftrace/test.d/dynevent/add_remove_fprobe.tc | 17 ++++++++++++-----
 1 file changed, 12 insertions(+), 5 deletions(-)

diff --git a/tools/testing/selftests/ftrace/test.d/dynevent/add_remove_fprobe.tc b/tools/testing/selftests/ftrace/test.d/dynevent/add_remove_fprobe.tc
index 47067a5e3..a55179f5d 100644
--- a/tools/testing/selftests/ftrace/test.d/dynevent/add_remove_fprobe.tc
+++ b/tools/testing/selftests/ftrace/test.d/dynevent/add_remove_fprobe.tc
@@ -27,22 +27,29 @@ grep -q myevent3 dynamic_events
 test -d events/fprobes/myevent1
 test -d events/fprobes/myevent2
 
-echo 1 > events/fprobes/myevent1/enable
-# Make sure the event is attached.
 grep -q $PLACE enabled_functions
+grep -q $PLACE2 enabled_functions
 cnt=`cat enabled_functions | wc -l`
-if [ $cnt -eq $ocnt ]; then
+if [ $cnt -le $ocnt ]; then
+	exit_fail
+fi
+
+echo 1 > events/fprobes/myevent1/enable
+cnt1=`cat enabled_functions | wc -l`
+if [ $cnt1 -ne $cnt ]; then
 	exit_fail
 fi
 
 echo 1 > events/fprobes/myevent2/enable
 cnt2=`cat enabled_functions | wc -l`
+if [ $cnt2 -ne $cnt1 ]; then
+	exit_fail
+fi
 
 echo 1 > events/fprobes/myevent3/enable
-# If the function is different, the attached function should be increased
 grep -q $PLACE2 enabled_functions
 cnt=`cat enabled_functions | wc -l`
-if [ $cnt -eq $cnt2 ]; then
+if [ $cnt -ne $cnt2 ]; then
 	exit_fail
 fi
 
-- 
2.39.5 (Apple Git-154)