Skip to contentSkip to conversation

tracing: move buffer in trace_seq to end of struct

PATCH In review Elijah Wright 3 participants 6 messages Aug 21, 2025 – Sep 2, 2025 Tracing

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tracing: move buffer in trace_seq to end of struct

TRACE_SEQ_BUFFER_SIZE is dependent on the architecture for its size. on 64-bit systems, it is 8148 bytes. forced 8-byte alignment in size_t and seq_buf means that trace_seq is 8200 bytes on 64-bit systems. moving the buffer to the end of the struct fixes the issue. there shouldn't be any side effects, i.e. pointer arithmetic on trace_seq

---
 include/linux/trace_seq.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/trace_seq.h +1 -1
index a93ed5ac3226..557780fe1c77 100644
@@ -21,10 +21,10 @@
 	(sizeof(struct seq_buf) + sizeof(size_t) + sizeof(int)))
 
 struct trace_seq {
-	char			buffer[TRACE_SEQ_BUFFER_SIZE];
 	struct seq_buf		seq;
 	size_t			readpos;
 	int			full;
+	char                    buffer[TRACE_SEQ_BUFFER_SIZE];
 };
 
 static inline void
Signature · 1 line
2.43.0
maintainer
#1 lore raw
Quoted 5 lines · On Wed, 20 Aug 2025 22:39:07 -0700 Elijah Wright <[email protected]> wrote:

TRACE_SEQ_BUFFER_SIZE is dependent on the architecture for its size. on 64-bit systems, it is 8148 bytes. forced 8-byte alignment in size_t and seq_buf means that trace_seq is 8200 bytes on 64-bit systems. moving the buffer to the end of the struct fixes the issue. there shouldn't be any side effects, i.e. pointer arithmetic on trace_seq

I don't remember why I had the buffer at the beginning. I think I did do some crazy typecasting when I first developed this, as the buffer field being first goes all the way back to the first commit (which doesn't include prototypes before it).

I guess I'm fine with the change. Probably even makes the cache better, as now the seq is now at the start of the buffer.

And that 'full' variable could actually be replaced as the seq does handle those cases too. But it can't just be removed. I think the patch below could be the answer to get rid of it.

Thanks,

-- Steve

include/linux/trace_seq.h +1 -3
index a93ed5ac3226..92364deb39a5 100644
@@ -24,14 +24,12 @@ struct trace_seq {
 	char			buffer[TRACE_SEQ_BUFFER_SIZE];
 	struct seq_buf		seq;
 	size_t			readpos;
-	int			full;
 };
 
 static inline void
 trace_seq_init(struct trace_seq *s)
 {
 	seq_buf_init(&s->seq, s->buffer, TRACE_SEQ_BUFFER_SIZE);
-	s->full = 0;
 	s->readpos = 0;
 }
 
@@ -77,7 +75,7 @@ trace_seq_buffer_ptr(struct trace_seq *s)
  */
 static inline bool trace_seq_has_overflowed(struct trace_seq *s)
 {
-	return s->full || seq_buf_has_overflowed(&s->seq);
+	return seq_buf_has_overflowed(&s->seq);
 }
 
 /*
kernel/trace/trace_seq.c +43 -67

213 lines not on this page · raw message

maintainer
#2 lore raw

include/linux/trace_seq.h struct trace_seq { · line 28

Quoted 11 lines · On Thu, 21 Aug 2025 11:43:55 -0400 Steven Rostedt <[email protected]> wrote:
include/linux/trace_seq.h +0 -1
index a93ed5ac3226..92364deb39a5 100644
@@ -24,14 +24,12 @@ struct trace_seq {
 	char			buffer[TRACE_SEQ_BUFFER_SIZE];
 	struct seq_buf		seq;
 	size_t			readpos;
-	int			full;
 };
 

I should have tried compiling it before posting. But trace.c has this:

		ret = print_trace_line(iter);
		if (ret == TRACE_TYPE_PARTIAL_LINE) {
			iter->seq.full = 0;
			trace_seq_puts(&iter->seq, "[LINE TOO BIG]\n");
		}

I need to figure out a clean way to fix that too :-p

-- Steve

#3 lore raw

can we maybe encode the overflow state in seq_buf? check if seq_buf_has_overflowed and clamp len back to the used size (seq_buf_used) in a helper?

Quoted 26 lines · Steven Rostedt

include/linux/trace_seq.h struct trace_seq { · line 28

Quoted 11 lines · On Thu, 21 Aug 2025 11:43:55 -0400 Steven Rostedt <[email protected]> wrote:
include/linux/trace_seq.h +0 -1
index a93ed5ac3226..92364deb39a5 100644
@@ -24,14 +24,12 @@ struct trace_seq {
  	char			buffer[TRACE_SEQ_BUFFER_SIZE];
  	struct seq_buf		seq;
  	size_t			readpos;
-	int			full;
  };
 

I should have tried compiling it before posting. But trace.c has this:

		ret = print_trace_line(iter);
		if (ret == TRACE_TYPE_PARTIAL_LINE) {
			iter->seq.full = 0;
			trace_seq_puts(&iter->seq, "[LINE TOO BIG]\n");
		}

I need to figure out a clean way to fix that too :-p

-- Steve

maintainer
#4 lore raw
Quoted 3 lines · On Thu, 21 Aug 2025 11:32:17 -0700 Elijah <[email protected]> wrote:

can we maybe encode the overflow state in seq_buf? check if seq_buf_has_overflowed and clamp len back to the used size (seq_buf_used) in a helper?

I could add a bit to the size?

include/linux/seq_buf.h +3 -2
index 52791e070506..ea4996851901 100644
@@ -20,8 +20,9 @@
  */
 struct seq_buf {
 	char			*buffer;
-	size_t			size;
-	size_t			len;
+	unsigned int		size;
+	unsigned int		len:31;
+	unsigned int		full:1;
 };
 
 #define DECLARE_SEQ_BUF(NAME, SIZE)			\

-- Steve

#5 lore raw

that could work yeah

Quoted 8 lines · Steven Rostedt
  struct seq_buf {
  	char			*buffer;
include/linux/seq_buf.h +3 -2
-	size_t			size;
-	size_t			len;
+	unsigned int		size;
+	unsigned int		len:31;
+	unsigned int		full:1;
  };