tracing: Use seq_buf for string concatenation
PATCH v2 1/1 tracing: Use seq_buf for string concatenation
In preparation for removing the strlcat API[1],
replace the string concatenation logic with a struct seq_buf, which tracks the current position and the remaining space internally.
Use seq_buf_str() to NUL-terminate before passing to early_enable_events().
---
v1 -> v2: Fixed WARN_ON when booting with empty trace_event.
v1: https://lore.kernel.org/all/[email protected]
kernel/trace/trace_events.c | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-)
kernel/trace/trace_events.c +13 -6
@@ -22,6 +22,7 @@#include <linux/sort.h> #include <linux/slab.h> #include <linux/delay.h> +#include <linux/seq_buf.h> #include <trace/events/sched.h> #include <trace/syscall.h>
@@ -4500,14 +4501,20 @@ static void __add_event_to_tracers(struct trace_event_call *call)extern struct trace_event_call *__start_ftrace_events[];extern struct trace_event_call *__stop_ftrace_events[]; -static char bootup_event_buf[COMMAND_LINE_SIZE ]__initdata ; +static struct seq_buf bootup_event_buf__initdata = { + .buffer = (char [COMMAND_LINE_SIZE ]) {}, + .size =COMMAND_LINE_SIZE , +};static __init int setup_trace_event (char *str) { -if (bootup_event_buf[0 ] !='\0' ) -strlcat (bootup_event_buf,"," ,COMMAND_LINE_SIZE ); +if (seq_buf_used (&bootup_event_buf) >0 ) +seq_buf_puts (&bootup_event_buf,"," ); + +seq_buf_puts (&bootup_event_buf, str); -strlcat (bootup_event_buf, str,COMMAND_LINE_SIZE ); +if (seq_buf_has_overflowed (&bootup_event_buf)) +return -ENOMEM ;trace_set_ring_buffer_expanded (NULL );disable_tracing_selftest ("running event tracing" );
@@ -4766,7 +4773,7 @@ static __init int event_trace_enable(void)*/ __trace_early_add_events (tr); -early_enable_events (tr, bootup_event_buf, false); +early_enable_events (tr, (char *)seq_buf_str (&bootup_event_buf), false);trace_printk_start_comm ();
@@ -4794,7 +4801,7 @@ static __init int event_trace_enable_again(void)if (!tr)return -ENODEV ; -early_enable_events (tr, bootup_event_buf, true); +early_enable_events (tr, (char *)seq_buf_str (&bootup_event_buf), true);return 0 ; }
Signature · 1 line
2.43.0
Steven Rostedt
maintainerkernel/trace/trace_events.c static void __add_event_to_tracers(struct trace_event_call *call) · line 4503
Quoted 8 lines · On Mon, 22 Jun 2026 16:46:23 +0700 Woradorn Laodhanadhaworn…
#include <
trace/events/sched.h>
#include <trace/syscall.h>kernel/trace/trace_events.c +0
-1@@ -4500,14 +4501,20 @@ static void __add_event_to_tracers(struct trace_event_call *call)extern struct trace_event_call *__start_ftrace_events[];extern struct trace_event_call *__stop_ftrace_events[]; -static char bootup_event_buf[COMMAND_LINE_SIZE ]__initdata ;
Keep the above string and just assign it.
kernel/trace/trace_events.c static void __add_event_to_tracers(struct trace_event_call *call) · line 4507
Quoted 4 lines
kernel/trace/trace_events.c +4
-0+static struct seq_buf bootup_event_buf__initdata = { + .buffer = (char [COMMAND_LINE_SIZE ]) {}, + .size =COMMAND_LINE_SIZE , +};
static struct seq_buf bootup_event_seq __initdata = {
.buffer = bootup_event_buf;
.size = sizeof(bootup_event_buf);
};Quoted 17 lines
kernel/trace/trace_events.c +6
-3static __init int setup_trace_event (char *str) { -if (bootup_event_buf[0 ] !='\0' ) -strlcat (bootup_event_buf,"," ,COMMAND_LINE_SIZE ); +if (seq_buf_used (&bootup_event_buf) >0 ) +seq_buf_puts (&bootup_event_buf,"," ); + +seq_buf_puts (&bootup_event_buf, str); -strlcat (bootup_event_buf, str,COMMAND_LINE_SIZE ); +if (seq_buf_has_overflowed (&bootup_event_buf)) +return -ENOMEM ;trace_set_ring_buffer_expanded (NULL );disable_tracing_selftest ("running event tracing" );
kernel/trace/trace_events.c static __init int event_trace_enable(void) · line 4776
Quoted 5 lines
kernel/trace/trace_events.c +1
-1@@ -4766,7 +4773,7 @@ static __init int event_trace_enable(void)*/ __trace_early_add_events (tr); -early_enable_events (tr, bootup_event_buf, false); +early_enable_events (tr, (char *)seq_buf_str (&bootup_event_buf), false);
The above then would be:
seq_buf_str(&bootup_event_seq); early_enable_events(tr, bootup_event_buf, false);
Don't typecast a const char* to non const.
kernel/trace/trace_events.c static __init int event_trace_enable_again(void) · line 4804
Quoted 9 lines
kernel/trace/trace_events.c +1
-1trace_printk_start_comm ();@@ -4794,7 +4801,7 @@ static __init int event_trace_enable_again(void)if (!tr)return -ENODEV ; -early_enable_events (tr, bootup_event_buf, true); +early_enable_events (tr, (char *)seq_buf_str (&bootup_event_buf), true);
Same here.
-- Steve
Quoted 3 lines
kernel/trace/trace_events.c +0
-0return 0 ; }
Quoted 61 lines · Steven Rostedt
kernel/trace/trace_events.c static void __add_event_to_tracers(struct trace_event_call *call) · line 4503
Quoted 8 lines · On Mon, 22 Jun 2026 16:46:23 +0700 Woradorn Laodhanadhaworn…
#include <
trace/events/sched.h>
#include <trace/syscall.h>kernel/trace/trace_events.c +0
-1@@ -4500,14 +4501,20 @@ static void __add_event_to_tracers(struct trace_event_call *call)extern struct trace_event_call *__start_ftrace_events[];extern struct trace_event_call *__stop_ftrace_events[]; -static char bootup_event_buf[COMMAND_LINE_SIZE ]__initdata ;Keep the above string and just assign it.
kernel/trace/trace_events.c static void __add_event_to_tracers(struct trace_event_call *call) · line 4507
Quoted 4 lines
kernel/trace/trace_events.c +4
-0+static struct seq_buf bootup_event_buf__initdata = { + .buffer = (char [COMMAND_LINE_SIZE ]) {}, + .size =COMMAND_LINE_SIZE , +};static struct seq_buf bootup_event_seq __initdata = { .buffer = bootup_event_buf; .size = sizeof(bootup_event_buf); };Quoted 17 lines
kernel/trace/trace_events.c +6
-3static __init int setup_trace_event (char *str) { -if (bootup_event_buf[0 ] !='\0' ) -strlcat (bootup_event_buf,"," ,COMMAND_LINE_SIZE ); +if (seq_buf_used (&bootup_event_buf) >0 ) +seq_buf_puts (&bootup_event_buf,"," ); + +seq_buf_puts (&bootup_event_buf, str); -strlcat (bootup_event_buf, str,COMMAND_LINE_SIZE ); +if (seq_buf_has_overflowed (&bootup_event_buf)) +return -ENOMEM ;trace_set_ring_buffer_expanded (NULL );disable_tracing_selftest ("running event tracing" );kernel/trace/trace_events.c static __init int event_trace_enable(void) · line 4776
Quoted 5 lines
kernel/trace/trace_events.c +1
-1@@ -4766,7 +4773,7 @@ static __init int event_trace_enable(void)*/ __trace_early_add_events (tr); -early_enable_events (tr, bootup_event_buf, false); +early_enable_events (tr, (char *)seq_buf_str (&bootup_event_buf), false);The above then would be:
seq_buf_str(&bootup_event_seq); early_enable_events(tr, bootup_event_buf, false);Don't typecast a const char* to non const.
Quoted 7 lines
kernel/trace/trace_events.c +0
-0trace_printk_start_comm ();@@ -4794,7 +4801,7 @@ static __init int event_trace_enable_again(void)if (!tr)return -ENODEV ;
Quoted 11 lines
kernel/trace/trace_events.c static __init int event_trace_enable_again(void) · line 4804
Quoted 2 lines
kernel/trace/trace_events.c +1
-1-early_enable_events (tr, bootup_event_buf, true); +early_enable_events (tr, (char *)seq_buf_str (&bootup_event_buf), true);Same here.
-- Steve
Quoted 3 lines
kernel/trace/trace_events.c +0
-0return 0 ; }
Thank you, Steven, for your review. I've sent v4:
https://lore.kernel.org/all/[email protected]
Thanks,
Woradorn
kernel test robot
build botkernel test robot noticed the following build warnings:
Hi Woradorn,
kernel test robot noticed the following build warnings:
[auto build test WARNING on v7.2-rc7]
[also build test WARNING on linus/master]
[cannot apply to linux-review/Woradorn-Laodhanadhaworn/tracing-Use-seq_buf-for-string-concatenation/20260813-224524 trace/for-next next-20260814] [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/Woradorn-Laodhanadhaworn/tracing-Use-seq_buf-for-string-concatenation/20260815-195403 base: v7.2-rc7 patch link: https://lore.kernel.org/r/20260622094623.18469-1-woradorn.laon%40gmail.com patch subject: [PATCH v2] tracing: Use seq_buf for string concatenation config: hexagon-randconfig-r123-20260816 (https://download.01.org/0day-ci/archive/20260817/[email protected]/config) compiler: clang version 18.1.8 (https://github.com/llvm/llvm-project 3b5b5c1ec4a3095ab096dd780e84d7ab81f3d7ff) sparse: v0.6.5-rc1 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260817/[email protected]/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 <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All warnings (new ones prefixed by >>, old ones prefixed by <<):
>> WARNING: modpost: vmlinux: section mismatch in reference: seq_buf_str+0x4 (section: .text) -> bootup_event_buf (section: .init.data) WARNING: modpost: vmlinux: section mismatch in reference: seq_buf_str+0x8 (section: .text) -> bootup_event_buf (section: .init.data) >> WARNING: modpost: vmlinux: section mismatch in reference: seq_buf_str+0xe0 (section: .text) -> __TRACE_SYSTEM_ERROR_DETECTOR_KFENCE (section: .init.data)
WARNING: modpost: vmlinux: section mismatch in reference: seq_buf_str+0xe4 (section: .text) -> __TRACE_SYSTEM_ERROR_DETECTOR_KFENCE (section: .init.data)
WARNING: modpost: vmlinux: section mismatch in reference: seq_buf_str+0xfc (section: .text) -> bootup_event_buf (section: .init.data)
WARNING: modpost: vmlinux: section mismatch in reference: seq_buf_str+0x100 (section: .text) -> bootup_event_buf (section: .init.data)
WARNING: modpost: vmlinux: section mismatch in reference: seq_buf_str+0x1cc (section: .text) -> bootup_event_buf (section: .init.data)
WARNING: modpost: vmlinux: section mismatch in reference: seq_buf_str+0x1d0 (section: .text) -> bootup_event_buf (section: .init.data)
WARNING: modpost: vmlinux: section mismatch in reference: seq_buf_str+0x1f4 (section: .text) -> bootup_event_buf (section: .init.data)
WARNING: modpost: vmlinux: section mismatch in reference: seq_buf_str+0x1f8 (section: .text) -> bootup_event_buf (section: .init.data)
WARNING: modpost: vmlinux: section mismatch in reference: seq_buf_str+0x228 (section: .text) -> bootup_event_buf (section: .init.data)
WARNING: modpost: vmlinux: section mismatch in reference: seq_buf_str+0x22c (section: .text) -> bootup_event_buf (section: .init.data)
--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki