Skip to contentSkip to conversation

tracing/synthetic: use union instead of casts

PATCH Superseded Sven Schnelle 2 participants 5 messages Aug 8, 2023 – Aug 9, 2023 Tracing

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 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 introduced an endianness bug. Use a union and struct instead.

---
 kernel/trace/trace.h              | 18 +++++++
 kernel/trace/trace_events_synth.c | 87 +++++++++++++------------------
 2 files changed, 55 insertions(+), 50 deletions(-)
kernel/trace/trace.h +18 -0
index e1edc2197fc8..8ff1c193eadb 100644
@@ -1295,6 +1295,24 @@ 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 {
+#ifdef CONFIG_CPU_BIG_ENDIAN
+			u16 offset;
+			u16 len;
+#else
+			u16 len;
+			u16 offset;
+#endif
+		};
+	};
+};
+
 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
maintainer
#1 lore raw
Quoted 8 lines · On Tue, 8 Aug 2023 16:21:48 +0200 Sven Schnelle <[email protected]> wrote:
kernel/trace/trace.h +8 -0
+struct trace_dynamic {
+	union {
+		u8 as_u8;
+		u16 as_u16;
+		u32 as_u32;
+		u64 as_u64;
+		struct {
+#ifdef CONFIG_CPU_BIG_ENDIAN

kernel/trace/trace.h static inline void trace_branch_disable(void) · line 1315

Quoted 10 lines
kernel/trace/trace.h +10 -0
+			u16 offset;
+			u16 len;
+#else
+			u16 len;
+			u16 offset;
+#endif
+		};
+	};
+};
+

The "dynamic" I was using wasn't about the fields were dynamic (union), but because the field the offset/len combo represents is of dynamic size. It's used all over the trace_events code.

I would have in include/linux/trace_events.h (right above struct trace_entry):

/* 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
};

And then it kernel/trace/trace.h:

union trace_synthetic_field {
	u8				as_u8;
	u16				as_u16;
	u32				as_u32;
	u64				as_u64;
	struct trace_dynamic_info	as_dynamic;
};

I could work on the part of the trace_dynamic_info if you want.

-- Steve

#2 lore raw
Quoted 19 lines · Steven Rostedt

The "dynamic" I was using wasn't about the fields were dynamic (union), but because the field the offset/len combo represents is of dynamic size. It's used all over the trace_events code.

I would have in include/linux/trace_events.h (right above struct trace_entry):

/* 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
};

And then it kernel/trace/trace.h:

Quoted 7 lines
union trace_synthetic_field {
	u8				as_u8;
	u16				as_u16;
	u32				as_u32;
	u64				as_u64;
	struct trace_dynamic_info	as_dynamic;
};

Ok.

Quoted 1 line

I could work on the part of the trace_dynamic_info if you want.

Whatever you prefer. Should i update my patch and send it again, or do you want to adjust it?

Thanks
Sven

maintainer
#3 lore raw
Quoted 29 lines · On Tue, 08 Aug 2023 19:33:06 +0200 Sven Schnelle <[email protected]> wrote:
Quoted 19 lines · Steven Rostedt

The "dynamic" I was using wasn't about the fields were dynamic (union), but because the field the offset/len combo represents is of dynamic size. It's used all over the trace_events code.

I would have in include/linux/trace_events.h (right above struct trace_entry):

/* 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
};

And then it kernel/trace/trace.h:

Quoted 7 lines
union trace_synthetic_field {
	u8				as_u8;
	u16				as_u16;
	u32				as_u32;
	u64				as_u64;
	struct trace_dynamic_info	as_dynamic;
};  
Quoted 7 lines

Ok.

Quoted 1 line

I could work on the part of the trace_dynamic_info if you want.

Whatever you prefer. Should i update my patch and send it again, or do you want to adjust it?

How quickly do you need it. I can hopefully start working on it this week.

-- Steve

#4 lore raw
Quoted 34 lines · Steven Rostedt
Quoted 31 lines · On Tue, 08 Aug 2023 19:33:06 +0200 Sven Schnelle <[email protected]> wrote:
Quoted 19 lines · Steven Rostedt

The "dynamic" I was using wasn't about the fields were dynamic (union), but because the field the offset/len combo represents is of dynamic size. It's used all over the trace_events code.

I would have in include/linux/trace_events.h (right above struct trace_entry):

/* 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
};

And then it kernel/trace/trace.h:

Quoted 7 lines
union trace_synthetic_field {
	u8				as_u8;
	u16				as_u16;
	u32				as_u32;
	u64				as_u64;
	struct trace_dynamic_info	as_dynamic;
};  

Ok.

Quoted 7 lines
Quoted 5 lines
Quoted 1 line

I could work on the part of the trace_dynamic_info if you want.

Whatever you prefer. Should i update my patch and send it again, or do you want to adjust it?

How quickly do you need it. I can hopefully start working on it this week.

I would like to have the bugfix in as soon as possible, as this is triggering a lot of KASAN warnings in our CI (besides breaking the synthetic events on our platform). I sent a v2 of my patch, so we apply that. The rest of the changes are not urgent.

Thanks
Sven