Skip to contentSkip to conversation

ring-buffer: Updates for 6.15

PATCH New Steven Rostedt 1 participant 5 messages Mar 23, 2025

Subsystems (2)
lore
Share & tools
lore

Steven Rostedt

author maintainer
#0 lore raw

cover ring-buffer: Updates for 6.15

git://git.kernel.org/pub/scm/linux/kernel/git/trace/linux-trace.git
ring-buffer/for-next

Head SHA1: 0e5d1a4b22bca3d9a9a2d2ba79ed67c5e74607d7

Jiapeng Chong (1):
ring-buffer: Remove the unused variable bmeta

Masami Hiramatsu (Google) (2):
tracing: Fix a compilation error without CONFIG_MODULES
tracing: Skip update_last_data() if cleared and remove active check for save_mod()

Steven Rostedt (1):
tracing: Initialize scratch_size to zero to prevent UB

----

 kernel/trace/ring_buffer.c |  3 ---
 kernel/trace/trace.c       | 35 ++++++++++++++++-------------------
 2 files changed, 16 insertions(+), 22 deletions(-)
author maintainer
#1 lore raw

PATCH 1/4 tracing: Fix a compilation error without CONFIG_MODULES

From: "Masami Hiramatsu (Google)" <[email protected]>

There are some code which depends on CONFIG_MODULES. #ifdef to enclose it.

---
 kernel/trace/trace.c | 7 +++++++
 1 file changed, 7 insertions(+)
kernel/trace/trace.c +7 -0
index c3c79908766e..68abea81e51a 100644
@@ -6001,6 +6001,7 @@ struct trace_scratch {
 
 static DEFINE_MUTEX(scratch_mutex);
 
+#ifdef CONFIG_MODULES
 static int save_mod(struct module *mod, void *data)
 {
 	struct trace_array *tr = data;
@@ -6025,6 +6026,12 @@ static int save_mod(struct module *mod, void *data)
 
 	return 0;
 }
+#else
+static int save_mod(struct module *mod, void *data)
+{
+	return 0;
+}
+#endif
 
 static void update_last_data(struct trace_array *tr)
 {
Signature · 1 line
2.47.2
author maintainer
#2 lore raw

PATCH 2/4 tracing: Initialize scratch_size to zero to prevent UB

In reply to Steven Rostedt

From: Steven Rostedt <[email protected]>

In allocate_trace_buffer() the following code:

buf->buffer = ring_buffer_alloc_range(size, rb_flags, 0,

				      tr->range_addr_start,
				      tr->range_addr_size,
				      struct_size(tscratch, entries, 128));
  tscratch = ring_buffer_meta_scratch(buf->buffer, &scratch_size);
  setup_trace_scratch(tr, tscratch, scratch_size);

Has undefined behavior if ring_buffer_alloc_range() fails because "scratch_size" is not initialize. If the allocation fails, then buf->buffer will be NULL. The ring_buffer_meta_scratch() will return NULL immediately if it is passed a NULL buffer and it will not update scratch_size. Then setup_trace_scratch() will return immediately if tscratch is NULL.

Although there's no real issue here, but it is considered undefined behavior to pass an uninitialized variable to a function as input, and UBSan may complain about it.

Just initialize scratch_size to zero to make the code defined behavior and a little more robust.

---
 kernel/trace/trace.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/trace/trace.c +1 -1
index 68abea81e51a..be97465485b3 100644
@@ -9394,7 +9394,7 @@ allocate_trace_buffer(struct trace_array *tr, struct array_buffer *buf, int size
 {
 	enum ring_buffer_flags rb_flags;
 	struct trace_scratch *tscratch;
-	unsigned int scratch_size;
+	unsigned int scratch_size = 0;
 
 	rb_flags = tr->trace_flags & TRACE_ITER_OVERWRITE ? RB_FL_OVERWRITE : 0;
 
Signature · 1 line
2.47.2
author maintainer
#3 lore raw

PATCH 3/4 tracing: Skip update_last_data() if cleared and remove active check for save_mod()

In reply to Steven Rostedt

From: "Masami Hiramatsu (Google)" <[email protected]>

If the last boot data is already cleared, there is no reason to update it again. Skip if the TRACE_ARRAY_FL_LAST_BOOT is cleared.
Also, for calling save_mod() when module loading, we don't need to check the trace is active or not because any module address can be on the stacktrace.

---
 kernel/trace/trace.c | 26 ++++++++------------------
 1 file changed, 8 insertions(+), 18 deletions(-)
kernel/trace/trace.c +8 -18
index be97465485b3..0be048fecc89 100644
@@ -6040,6 +6040,12 @@ static void update_last_data(struct trace_array *tr)
 	if (!(tr->flags & TRACE_ARRAY_FL_BOOT))
 		return;
 
+	if (!(tr->flags & TRACE_ARRAY_FL_LAST_BOOT))
+		return;
+
+	/* Only if the buffer has previous boot data clear and update it. */
+	tr->flags &= ~TRACE_ARRAY_FL_LAST_BOOT;
+
 	/* Reset the module list and reload them */
 	if (tr->scratch) {
 		struct trace_scratch *tscratch = tr->scratch;
@@ -6052,9 +6058,6 @@ static void update_last_data(struct trace_array *tr)
 		module_for_each_mod(save_mod, tr);
 	}
 
-	if (!(tr->flags & TRACE_ARRAY_FL_LAST_BOOT))
-		return;
-
 	/*
 	 * Need to clear all CPU buffers as there cannot be events
 	 * from the previous boot mixed with events with this boot
@@ -6077,7 +6080,6 @@ static void update_last_data(struct trace_array *tr)
 #else
 	tscratch->kaslr_addr = 0;
 #endif
-	tr->flags &= ~TRACE_ARRAY_FL_LAST_BOOT;
 }
 
 /**
@@ -10099,15 +10101,6 @@ static void trace_module_remove_evals(struct module *mod)
 static inline void trace_module_remove_evals(struct module *mod) { }
 #endif /* CONFIG_TRACE_EVAL_MAP_FILE */
 
-static bool trace_array_active(struct trace_array *tr)
-{
-	if (tr->current_trace != &nop_trace)
-		return true;
-
-	/* 0 is no events, 1 is all disabled */
-	return trace_events_enabled(tr, NULL) > 1;
-}
-
 static void trace_module_record(struct module *mod)
 {
 	struct trace_array *tr;
@@ -10116,11 +10109,8 @@ static void trace_module_record(struct module *mod)
 		/* Update any persistent trace array that has already been started */
 		if ((tr->flags & (TRACE_ARRAY_FL_BOOT | TRACE_ARRAY_FL_LAST_BOOT)) ==
 		    TRACE_ARRAY_FL_BOOT) {
-			/* Only update if the trace array is active */
-			if (trace_array_active(tr)) {
-				guard(mutex)(&scratch_mutex);
-				save_mod(mod, tr);
-			}
+			guard(mutex)(&scratch_mutex);
+			save_mod(mod, tr);
 		}
 	}
 }
Signature · 1 line
2.47.2
author maintainer
#4 lore raw

PATCH 4/4 ring-buffer: Remove the unused variable bmeta

In reply to Steven Rostedt

From: Jiapeng Chong <[email protected]>

Variable bmeta is not effectively used, so delete it.

kernel/trace/ring_buffer.c:1952:27: warning: variable ‘bmeta’ set but not used.

---
 kernel/trace/ring_buffer.c | 3 ---
 1 file changed, 3 deletions(-)
kernel/trace/ring_buffer.c +0 -3
index 94be81edffd2..302b1404213c 100644
@@ -1949,7 +1949,6 @@ static void rb_meta_validate_events(struct ring_buffer_per_cpu *cpu_buffer)
 static void rb_range_meta_init(struct trace_buffer *buffer, int nr_pages, int scratch_size)
 {
 	struct ring_buffer_cpu_meta *meta;
-	struct ring_buffer_meta *bmeta;
 	unsigned long *subbuf_mask;
 	unsigned long delta;
 	void *subbuf;
@@ -1964,8 +1963,6 @@ static void rb_range_meta_init(struct trace_buffer *buffer, int nr_pages, int sc
 	if (rb_meta_init(buffer, scratch_size))
 		valid = true;
 
-	bmeta = buffer->meta;
-
 	for (cpu = 0; cpu < nr_cpu_ids; cpu++) {
 		void *next_meta;
 
Signature · 1 line
2.47.2