Skip to contentSkip to conversation

tracing: A couple more fixes to v6.18

GIT PULL Merged Steven Rostedt 3 participants 5 messages Oct 11, 2025 Tracing

lore
Share & tools
lore

Steven Rostedt

author maintainer
#0 lore raw

Linus,

tracing fixes for v6.18:

The previous fix to trace_marker required updating trace_marker_raw as well. The difference between trace_marker_raw from trace_marker is that the raw version is for applications to write binary structures directly into the ring buffer instead of writing ASCII strings. This is for applications that will read the raw data from the ring buffer and get the data structures directly. It's a bit quicker than using the ASCII version.

Unfortunately, it appears that our test suite has several tests that test writes to the trace_marker file, but lacks any tests to the trace_marker_raw file (this needs to be remedied). Two issues came about the update to the trace_marker_raw file that syzbot found:

- Fix tracing_mark_raw_write() to use per CPU buffer

The fix to use the per CPU buffer to copy from user space was needed for both the trace_maker and trace_maker_raw file.

The fix for reading from user space into per CPU buffers properly fixed the trace_marker write function, but the trace_marker_raw file wasn't fixed properly. The user space data was correctly written into the per CPU buffer, but the code that wrote into the ring buffer still used the user space pointer and not the per CPU buffer that had the user space data already written.

- Stop the fortify string warning from writing into trace_marker_raw

After converting the copy_from_user_nofault() into a memcpy(), another issue appeared. As writes to the trace_marker_raw expects binary data, the first entry is a 4 byte identifier. The entry structure is defined as:

  struct {
	struct trace_entry ent;
	int id;
	char buf[];

};

The size of this structure is reserved on the ring buffer with:

    size = sizeof(*entry) + cnt;

Then it is copied from the buffer into the ring buffer with:

    memcpy(&entry->id, buf, cnt);

This use to be a copy_from_user_nofault(), but now converting it to a memcpy() triggers the fortify-string code, and causes a warning.

The allocated space is actually more than what is copied, as the cnt used also includes the entry->id portion. Allocating sizeof(*entry) plus cnt is actually allocating 4 bytes more than what is needed.

Change the size function to:

    size = struct_size(entry, buf, cnt - sizeof(entry->id));

And update the memcpy() to unsafe_memcpy().

Please pull the latest trace-v6.18-3 tree, which can be found at:

git://git.kernel.org/pub/scm/linux/kernel/git/trace/linux-trace.git
trace-v6.18-3

Tag SHA1: 3697c271c3c640784960e2f66b787cb34b29bb1f Head SHA1: 54b91e54b113d4f15ab023a44f508251db6e22e7

Steven Rostedt (2):
tracing: Fix tracing_mark_raw_write() to use buf and not ubuf
tracing: Stop fortify-string from warning in tracing_mark_raw_write()

----

 kernel/trace/trace.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)

---------------------------

kernel/trace/trace.c +8 -4
index 0fd582651293..eb256378e65b 100644
@@ -7441,7 +7441,8 @@ static ssize_t write_raw_marker_to_buffer(struct trace_array *tr,
 	ssize_t written;
 	size_t size;
 
-	size = sizeof(*entry) + cnt;
+	/* cnt includes both the entry->id and the data behind it. */
+	size = struct_size(entry, buf, cnt - sizeof(entry->id));
 
 	buffer = tr->array_buffer.buffer;
 
@@ -7455,7 +7456,10 @@ static ssize_t write_raw_marker_to_buffer(struct trace_array *tr,
 		return -EBADF;
 
 	entry = ring_buffer_event_data(event);
-	memcpy(&entry->id, buf, cnt);
+	unsafe_memcpy(&entry->id, buf, cnt,
+		      "id and content already reserved on ring buffer"
+		      "'buf' includes the 'id' and the data."
+		      "'entry' was allocated with cnt from 'id'.");
 	written = cnt;
 
 	__buffer_unlock_commit(buffer, event);
@@ -7497,12 +7501,12 @@ tracing_mark_raw_write(struct file *filp, const char __user *ubuf,
 	if (tr == &global_trace) {
 		guard(rcu)();
 		list_for_each_entry_rcu(tr, &marker_copies, marker_list) {
-			written = write_raw_marker_to_buffer(tr, ubuf, cnt);
+			written = write_raw_marker_to_buffer(tr, buf, cnt);
 			if (written < 0)
 				break;
 		}
 	} else {
-		written = write_raw_marker_to_buffer(tr, ubuf, cnt);
+		written = write_raw_marker_to_buffer(tr, buf, cnt);
 	}
 
 	return written;
#1 lore raw

kernel/trace/trace.c static ssize_t write_raw_marker_to_buffer(struct trace_array *tr, · line 7445

Quoted 3 lines · Steven Rostedt
kernel/trace/trace.c +2 -0
+       /* cnt includes both the entry->id and the data behind it. */
+       size = struct_size(entry, buf, cnt - sizeof(entry->id));

This seems very non-intuitive.

Why isn't it just saying

    size = cnt + offsetof(struct raw_data_entry, id);

which would seem to be much more straightforward than saying "go to the end, and then subtract out the size of this entry" - which then relies on the entry being the last thing in the struct.

And if somebody wants to have a helper like

   #define struct_offset(s,memb) (offsetof(typeof(*(s)), memb))

in order to get some kind of "typesafe offsetof", to go with 'struct size' then by all means.. That would make it be just

    size = cnt + struct_offset(entry, id);

which looks fairly legible and logical, and would go with the comment (and would pair fairly well with our other "struct_xyz()" helpers, I think).

I've pulled this, I just reacted to how odd that calculation looked. It makes very little sense to me, since the calculation really has _nothing_ do with the size of the struct, and you explicitly have to play tricks to get the offset that way.

Linus

author maintainer
#2 lore raw
Quoted 24 lines · On Sat, 11 Oct 2025 16:09:18 -0700 Linus Torvalds…

kernel/trace/trace.c static ssize_t write_raw_marker_to_buffer(struct trace_array *tr, · line 7445

Quoted 3 lines · Steven Rostedt
kernel/trace/trace.c +2 -0
+       /* cnt includes both the entry->id and the data behind it. */
+       size = struct_size(entry, buf, cnt - sizeof(entry->id));  

This seems very non-intuitive.

Why isn't it just saying

    size = cnt + offsetof(struct raw_data_entry, id);

which would seem to be much more straightforward than saying "go to the end, and then subtract out the size of this entry" - which then relies on the entry being the last thing in the struct.

And if somebody wants to have a helper like

   #define struct_offset(s,memb) (offsetof(typeof(*(s)), memb))

in order to get some kind of "typesafe offsetof", to go with 'struct size' then by all means.. That would make it be just

    size = cnt + struct_offset(entry, id);
Quoted 8 lines

which looks fairly legible and logical, and would go with the comment (and would pair fairly well with our other "struct_xyz()" helpers, I think).

I've pulled this, I just reacted to how odd that calculation looked. It makes very little sense to me, since the calculation really has _nothing_ do with the size of the struct, and you explicitly have to play tricks to get the offset that way.

I could make this change for the next merge window.

I originally was going to change it to:

	size = sizeof(*entry) + cnt - sizeof(entry->id);

But figured using a macro would be better.

Honestly, I didn't like either solution. But having a:

size = struct_offset(entry, id) + cnt

Would probably look better.

But again, it can probably wait till 6.19.

-- Steve

#3 lore raw
Quoted 2 lines · Steven Rostedt

But again, it can probably wait till 6.19.

Definitely. I added Kees, because he did those other 'struct_xyz()' macros, but yeah, this is not one of those "last day of the 6.18 merge window" things.

Linus

merge bot Merged
#4 lore raw
In reply to Steven Rostedt
The pull request you sent on Sat, 11 Oct 2025 15:52:40 -0400:

The pull request you sent on Sat, 11 Oct 2025 15:52:40 -0400:

Quoted 1 line

git://git.kernel.org/pub/scm/linux/kernel/git/trace/linux-trace.git trace-v6.18-3

has been merged into torvalds/linux.git:
https://git.kernel.org/torvalds/c/67029a49db6c1f21106a1b5fcdd0ea234a6e0711

Thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/prtracker.html