tracing/synthetic: use union instead of casts
PATCH v2 1/1 tracing/synthetic: use union instead of casts
The current code uses a lot of casts to access the fields member in struct synth_trace_events with different sizes. This makes the code hard to read, and had already introduced an endianness bug. Use a union and struct instead.
---
Changes in v2:
- cosmetic changes
- add struct trace_dynamic_info to include/linux/trace_events.h
include/linux/trace_events.h | 11 ++++ kernel/trace/trace.h | 10 ++++ kernel/trace/trace_events_synth.c | 87 +++++++++++++------------------ 3 files changed, 58 insertions(+), 50 deletions(-)
include/linux/trace_events.h +11 -0
@@ -59,6 +59,17 @@ int trace_raw_output_prep(struct trace_iterator *iter, extern __printf(2, 3) void trace_event_printf(struct trace_iterator *iter, const char *fmt, ...); +/* Used to find the offset and length of dynamic fields in trace events */ +struct trace_dynamic_info { +#ifdef CONFIG_CPU_BIG_ENDIAN + u16 offset; + u16 len; +#else + u16 len; + u16 offset; +#endif +}; + /* * The trace entry - the most basic unit of tracing. This is what * is printed in the end as a single line in the trace output, such as:
kernel/trace/trace.h +10 -0
@@ -1295,6 +1295,16 @@ static inline void trace_branch_disable(void)/* set ring buffers to default size if not already done so */ int tracing_update_buffers (void ); +struct trace_dynamic { +union { +u8 as_u8; +u16 as_u16; +u32 as_u32; +u64 as_u64; +struct trace_dynamic_info as_dynamic; + }; +}; +struct ftrace_event_field {struct list_head link;const char *name;
kernel/trace/trace_events_synth.c +37 -50
223 lines not on this page · raw message
Signature · 1 line
2.39.2
Steven Rostedt
maintainerQuoted 6 lines · On Wed, 9 Aug 2023 09:14:59 +0200 Sven Schnelle <[email protected]> wrote:
kernel/trace/trace.h +0
-0@@ -1295,6 +1295,16 @@ static inline void trace_branch_disable(void)/* set ring buffers to default size if not already done so */ int tracing_update_buffers (void );
kernel/trace/trace.h static inline void trace_branch_disable(void) · line 1307
Quoted 10 lines
kernel/trace/trace.h +10
-0+struct trace_dynamic { +union { +u8 as_u8; +u16 as_u16; +u32 as_u32; +u64 as_u64; +struct trace_dynamic_info as_dynamic; + }; +}; +
No need to create a structure around a single element union. Also, I would like to name it for what it is for.
union trace_synth_field {
u8 as_u8;
u16 as_u16;
u32 as_u32;
u64 as_u64;
struct trace_dynamic_info as_dynamic;
};Other than that, the patch looks good. Although I still need to test it.
-- Steve
From: Steven Rostedt
Quoted 32 lines
Sent: 09 August 2023 13:55
Quoted 6 lines · On Wed, 9 Aug 2023 09:14:59 +0200 Sven Schnelle <[email protected]> wrote:
kernel/trace/trace.h +0
-0@@ -1295,6 +1295,16 @@ static inline void trace_branch_disable(void)/* set ring buffers to default size if not already done so */ int tracing_update_buffers (void );kernel/trace/trace.h static inline void trace_branch_disable(void) · line 1307
Quoted 10 lines
kernel/trace/trace.h +10
-0+struct trace_dynamic { +union { +u8 as_u8; +u16 as_u16; +u32 as_u32; +u64 as_u64; +struct trace_dynamic_info as_dynamic; + }; +}; +No need to create a structure around a single element union. Also, I would like to name it for what it is for.
union trace_synth_field { u8 as_u8; u16 as_u16; u32 as_u32; u64 as_u64; struct trace_dynamic_info as_dynamic; };
Quoted 1 line
Other than that, the patch looks good. Although I still need to test it.
I was wondering if you need the u8 and u16 members at all? Can't the values just be treated as 32bit?
Both char and short aren't really 'proper' arithmetic types.
David
-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)
Steven Rostedt
maintainerQuoted 11 lines · On Mon, 14 Aug 2023 11:34:20 +0000 David Laight <[email protected]> wrote:
Quoted 11 lines
No need to create a structure around a single element union. Also, I would like to name it for what it is for.
union trace_synth_field { u8 as_u8; u16 as_u16; u32 as_u32; u64 as_u64; struct trace_dynamic_info as_dynamic; };
Quoted 5 lines
Quoted 1 line
Other than that, the patch looks good. Although I still need to test it.
I was wondering if you need the u8 and u16 members at all? Can't the values just be treated as 32bit?
Both char and short aren't really 'proper' arithmetic types.
Not sure what you mean by "arithmatic types".
If you do something like:
~# echo 's:skb u16 protocol; u64 delta;' >> /sys/kernel/tracing/dynamic_events
~# echo 'hist:keys=skbaddr:ts=common_timestamp.usecs' >> /sys/kernel/tracing/events/net/netif_receive_skb/trigger
~# echo 'hist:keys=skbaddr:delta=common_timestamp.usecs-$ts:onmatch(net.netif_receive_skb).trace(skb,protocol,$delta)' >> /sys/kernel/tracing/events/skb/kfree_skb/trigger
Where it records the protocol as u16, I'm guessing that if we didn't use this union, it might break on BIG_ENDIAN machines.
For reference, the kfree_skb event looks like:
system: skb
name: kfree_skb
ID: 1735
format:
field:unsigned short common_type; offset:0; size:2; signed:0;
field:unsigned char common_flags; offset:2; size:1; signed:0;
field:unsigned char common_preempt_count; offset:3; size:1; signed:0;
field:int common_pid; offset:4; size:4; signed:1;
field:void * skbaddr; offset:8; size:8; signed:0;
field:void * location; offset:16; size:8; signed:0;
field:unsigned short protocol; offset:24; size:2; signed:0;
field:enum skb_drop_reason reason; offset:28; size:4; signed:0;-- Steve