Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

tracing: Do not calculate strlen() twice for __string() fields

The TRACE_EVENT() macro handles dynamic strings by having:

TP_PROTO(struct some_struct *s),
TP_ARGS(s),
TP_STRUCT__entry(
__string(my_string, s->string)
),
TP_fast_assign(
__assign_str(my_string, s->string);
)
TP_printk("%s", __get_str(my_string))

There's even some code that may call a function helper to find the
s->string value. The problem with the above is that the work to get the
s->string is done twice. Once at the __string() and again in the
__assign_str().

The length of the string is calculated via a strlen(), not once, but
twice. Once during the __string() macro and again in __assign_str(). But
the length is actually already recorded in the data location and here's no
reason to call strlen() again.

Just use the saved length that was saved in the __string() code for the
__assign_str() code.

Link: https://lore.kernel.org/linux-trace-kernel/20240222211442.793074999@goodmis.org

Cc: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Ville Syrjälä <ville.syrjala@linux.intel.com>
Cc: Rodrigo Vivi <rodrigo.vivi@intel.com>
Cc: Chuck Lever <chuck.lever@oracle.com>
Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org>

+6 -4
+6 -4
include/trace/stages/stage6_event_callback.h
··· 32 32 33 33 #undef __assign_str 34 34 #define __assign_str(dst, src) \ 35 - strcpy(__get_str(dst), __data_offsets.dst##_ptr_ ? \ 36 - __data_offsets.dst##_ptr_ : "(null)") 35 + memcpy(__get_str(dst), __data_offsets.dst##_ptr_ ? \ 36 + __data_offsets.dst##_ptr_ : "(null)", \ 37 + __get_dynamic_array_len(dst)) 37 38 38 39 #undef __assign_str_len 39 40 #define __assign_str_len(dst, src, len) \ ··· 95 94 96 95 #undef __assign_rel_str 97 96 #define __assign_rel_str(dst, src) \ 98 - strcpy(__get_rel_str(dst), __data_offsets.dst##_ptr_ ? \ 99 - __data_offsets.dst##_ptr_ : "(null)") 97 + memcpy(__get_rel_str(dst), __data_offsets.dst##_ptr_ ? \ 98 + __data_offsets.dst##_ptr_ : "(null)", \ 99 + __get_rel_dynamic_array_len(dst)) 100 100 101 101 #undef __assign_rel_str_len 102 102 #define __assign_rel_str_len(dst, src, len) \