[PATCH] trace: make ftrace likely counters atomic

Khaled Saleh posted 1 patch 1 month, 2 weeks ago
kernel/trace/trace_branch.c | 9 +++++----
1 file changed, 5 insertions(+), 4 deletions(-)
[PATCH] trace: make ftrace likely counters atomic
Posted by Khaled Saleh 1 month, 2 weeks ago
The correct and incorrect counters in ftrace_likely_update() are
updated without synchronization and may race on SMP systems.

Convert them to atomic counters to address the existing FIXME
without changing behavior.

Signed-off-by: Khaled Saleh <khaled.saleh.req@gmail.com>
---
 kernel/trace/trace_branch.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)

diff --git a/kernel/trace/trace_branch.c b/kernel/trace/trace_branch.c
index 6809b370e991..31a144fa975f 100644
--- a/kernel/trace/trace_branch.c
+++ b/kernel/trace/trace_branch.c
@@ -215,10 +215,11 @@ void ftrace_likely_update(struct ftrace_likely_data *f, int val,
 	trace_likely_condition(f, val, expect);
 
 	/* FIXME: Make this atomic! */
-	if (val == expect)
-		f->data.correct++;
-	else
-		f->data.incorrect++;
+	if (val == expect) {
+		atomic_long_inc(&f->data.correct);
+	} else {
+	atomic_long_inc(&f->data.incorrect);
+	}
 
 	user_access_restore(flags);
 }
-- 
2.43.0
Re: [PATCH] trace: make ftrace likely counters atomic
Posted by kernel test robot 1 month, 2 weeks ago
Hi Khaled,

kernel test robot noticed the following build errors:

[auto build test ERROR on trace/for-next]
[also build test ERROR on linus/master v6.19-rc2 next-20251219]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Khaled-Saleh/trace-make-ftrace-likely-counters-atomic/20251224-044106
base:   https://git.kernel.org/pub/scm/linux/kernel/git/trace/linux-trace for-next
patch link:    https://lore.kernel.org/r/20251223203715.10954-1-khaled.saleh.req%40gmail.com
patch subject: [PATCH] trace: make ftrace likely counters atomic
config: hexagon-randconfig-002-20251224 (https://download.01.org/0day-ci/archive/20251224/202512242156.fyUcX7CR-lkp@intel.com/config)
compiler: clang version 22.0.0git (https://github.com/llvm/llvm-project 4ef602d446057dabf5f61fb221669ecbeda49279)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20251224/202512242156.fyUcX7CR-lkp@intel.com/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202512242156.fyUcX7CR-lkp@intel.com/

All errors (new ones prefixed by >>):

>> kernel/trace/trace_branch.c:219:19: error: incompatible pointer types passing 'unsigned long *' to parameter of type 'atomic_long_t *' (aka 'atomic_t *') [-Wincompatible-pointer-types]
     219 |                 atomic_long_inc(&f->data.correct);
         |                                 ^~~~~~~~~~~~~~~~
   include/linux/atomic/atomic-instrumented.h:3589:32: note: passing argument to parameter 'v' here
    3589 | atomic_long_inc(atomic_long_t *v)
         |                                ^
   kernel/trace/trace_branch.c:221:18: error: incompatible pointer types passing 'unsigned long *' to parameter of type 'atomic_long_t *' (aka 'atomic_t *') [-Wincompatible-pointer-types]
     221 |         atomic_long_inc(&f->data.incorrect);
         |                         ^~~~~~~~~~~~~~~~~~
   include/linux/atomic/atomic-instrumented.h:3589:32: note: passing argument to parameter 'v' here
    3589 | atomic_long_inc(atomic_long_t *v)
         |                                ^
   2 errors generated.


vim +219 kernel/trace/trace_branch.c

   198	
   199	void ftrace_likely_update(struct ftrace_likely_data *f, int val,
   200				  int expect, int is_constant)
   201	{
   202		unsigned long flags = user_access_save();
   203	
   204		/* A constant is always correct */
   205		if (is_constant) {
   206			f->constant++;
   207			val = expect;
   208		}
   209		/*
   210		 * I would love to have a trace point here instead, but the
   211		 * trace point code is so inundated with unlikely and likely
   212		 * conditions that the recursive nightmare that exists is too
   213		 * much to try to get working. At least for now.
   214		 */
   215		trace_likely_condition(f, val, expect);
   216	
   217		/* FIXME: Make this atomic! */
   218		if (val == expect) {
 > 219			atomic_long_inc(&f->data.correct);
   220		} else {
   221		atomic_long_inc(&f->data.incorrect);
   222		}
   223	
   224		user_access_restore(flags);
   225	}
   226	EXPORT_SYMBOL(ftrace_likely_update);
   227	

-- 
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
Re: [PATCH] trace: make ftrace likely counters atomic
Posted by kernel test robot 1 month, 2 weeks ago
Hi Khaled,

kernel test robot noticed the following build errors:

[auto build test ERROR on trace/for-next]
[also build test ERROR on linus/master v6.19-rc2 next-20251219]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Khaled-Saleh/trace-make-ftrace-likely-counters-atomic/20251224-044106
base:   https://git.kernel.org/pub/scm/linux/kernel/git/trace/linux-trace for-next
patch link:    https://lore.kernel.org/r/20251223203715.10954-1-khaled.saleh.req%40gmail.com
patch subject: [PATCH] trace: make ftrace likely counters atomic
config: i386-randconfig-003-20251224 (https://download.01.org/0day-ci/archive/20251224/202512242037.SviPrpZt-lkp@intel.com/config)
compiler: gcc-14 (Debian 14.2.0-19) 14.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20251224/202512242037.SviPrpZt-lkp@intel.com/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202512242037.SviPrpZt-lkp@intel.com/

All errors (new ones prefixed by >>):

   kernel/trace/trace_branch.c: In function 'ftrace_likely_update':
>> kernel/trace/trace_branch.c:219:33: error: passing argument 1 of 'atomic_long_inc' from incompatible pointer type [-Wincompatible-pointer-types]
     219 |                 atomic_long_inc(&f->data.correct);
         |                                 ^~~~~~~~~~~~~~~~
         |                                 |
         |                                 long unsigned int *
   In file included from include/linux/atomic.h:82,
                    from include/linux/jump_label.h:257,
                    from include/linux/static_key.h:1,
                    from arch/x86/include/asm/nospec-branch.h:6,
                    from arch/x86/include/asm/irqflags.h:9,
                    from include/linux/irqflags.h:18,
                    from include/linux/spinlock.h:59,
                    from include/linux/mmzone.h:8,
                    from include/linux/gfp.h:7,
                    from include/linux/mm.h:7,
                    from include/linux/kallsyms.h:13,
                    from kernel/trace/trace_branch.c:7:
   include/linux/atomic/atomic-instrumented.h:3589:32: note: expected 'atomic_long_t *' {aka 'atomic_t *'} but argument is of type 'long unsigned int *'
    3589 | atomic_long_inc(atomic_long_t *v)
         |                 ~~~~~~~~~~~~~~~^
   kernel/trace/trace_branch.c:221:25: error: passing argument 1 of 'atomic_long_inc' from incompatible pointer type [-Wincompatible-pointer-types]
     221 |         atomic_long_inc(&f->data.incorrect);
         |                         ^~~~~~~~~~~~~~~~~~
         |                         |
         |                         long unsigned int *
   include/linux/atomic/atomic-instrumented.h:3589:32: note: expected 'atomic_long_t *' {aka 'atomic_t *'} but argument is of type 'long unsigned int *'
    3589 | atomic_long_inc(atomic_long_t *v)
         |                 ~~~~~~~~~~~~~~~^


vim +/atomic_long_inc +219 kernel/trace/trace_branch.c

   198	
   199	void ftrace_likely_update(struct ftrace_likely_data *f, int val,
   200				  int expect, int is_constant)
   201	{
   202		unsigned long flags = user_access_save();
   203	
   204		/* A constant is always correct */
   205		if (is_constant) {
   206			f->constant++;
   207			val = expect;
   208		}
   209		/*
   210		 * I would love to have a trace point here instead, but the
   211		 * trace point code is so inundated with unlikely and likely
   212		 * conditions that the recursive nightmare that exists is too
   213		 * much to try to get working. At least for now.
   214		 */
   215		trace_likely_condition(f, val, expect);
   216	
   217		/* FIXME: Make this atomic! */
   218		if (val == expect) {
 > 219			atomic_long_inc(&f->data.correct);
   220		} else {
   221		atomic_long_inc(&f->data.incorrect);
   222		}
   223	
   224		user_access_restore(flags);
   225	}
   226	EXPORT_SYMBOL(ftrace_likely_update);
   227	

-- 
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
Re: [PATCH] trace: make ftrace likely counters atomic
Posted by Steven Rostedt 1 month, 2 weeks ago
On Tue, 23 Dec 2025 22:37:14 +0200
Khaled Saleh <khaled.saleh.req@gmail.com> wrote:

> The correct and incorrect counters in ftrace_likely_update() are
> updated without synchronization and may race on SMP systems.

Yes, it can race, but its mostly just heuristics anyway, so missed counters
are not a problem.

> 
> Convert them to atomic counters to address the existing FIXME
> without changing behavior.

There's a reason why they are not atomic. You think I would put in a
"FIXME" if it was that easy?

Sorry, NAK.

-- Steve