Skip to contentSkip to conversation

selftests/ftrace: Fix trace_marker_raw test on 64K page kernels

PATCH Superseded Tianchen Ding 2 participants 8 messages May 27 – Jun 1

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 selftests/ftrace: Fix trace_marker_raw test on 64K page kernels

On ARM64 kernels with 64K pages, the trace_marker_raw test fails because bash's printf builtin uses stdio buffering which splits output into multiple small write() calls to the tracefs file. Since each individual write is within TRACE_MARKER_MAX_SIZE (4096), they all succeed, causing the "too big" write test to incorrectly pass.

Fix by piping make_str output through dd with iflag=fullblock to guarantee a single atomic write() syscall to trace_marker_raw.

Fixes 37f46601383a ("selftests/tracing: Add basic test for trace_marker_raw file")
---
 .../selftests/ftrace/test.d/00basic/trace_marker_raw.tc     | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
tools/testing/selftests/ftrace/test.d/00basic/trace_marker_raw.tc +4 -2
index 8e905d4fe6dd..efd8263e6087 100644
@@ -43,8 +43,10 @@ write_buffer() {
 	id=$1
 	size=$2
 
-	# write the string into the raw marker
-	make_str $id $size > trace_marker_raw
+	# Pipe through dd to ensure a single atomic write() syscall.
+	# Shell's printf builtin uses stdio buffering which may split the
+	# output into multiple writes.
+	make_str $id $size | dd of=trace_marker_raw bs=`expr $size + 4` iflag=fullblock
 }
 
 
Signature · 1 line
2.39.3
maintainer
#1 lore raw
Quoted 20 lines · On Wed, 27 May 2026 17:54:37 +0800 Tianchen Ding <[email protected]>…

On ARM64 kernels with 64K pages, the trace_marker_raw test fails because bash's printf builtin uses stdio buffering which splits output into multiple small write() calls to the tracefs file. Since each individual write is within TRACE_MARKER_MAX_SIZE (4096), they all succeed, causing the "too big" write test to incorrectly pass.

Fix by piping make_str output through dd with iflag=fullblock to guarantee a single atomic write() syscall to trace_marker_raw.

Fixes 37f46601383a ("selftests/tracing: Add basic test for trace_marker_raw file")
---
 .../selftests/ftrace/test.d/00basic/trace_marker_raw.tc     | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
tools/testing/selftests/ftrace/test.d/00basic/trace_marker_raw.tc +0 -0
index 8e905d4fe6dd..efd8263e6087 100644

tools/testing/selftests/ftrace/test.d/00basic/trace_marker_raw.tc write_buffer() { · line 48

Quoted 8 lines
tools/testing/selftests/ftrace/test.d/00basic/trace_marker_raw.tc +3 -2
@@ -43,8 +43,10 @@ write_buffer() {
 	id=$1
 	size=$2
 
-	# write the string into the raw marker
-	make_str $id $size > trace_marker_raw
+	# Pipe through dd to ensure a single atomic write() syscall.
+	# Shell's printf builtin uses stdio buffering which may split the
+	# output into multiple writes.

Could you comment that this is for architectures with 64K pages too.

Thanks for fixing this,

-- Steve

Quoted 4 lines
tools/testing/selftests/ftrace/test.d/00basic/trace_marker_raw.tc +1 -0
+	make_str $id $size | dd of=trace_marker_raw bs=`expr $size + 4` iflag=fullblock
 }
 
 
#2 lore raw

PATCH v2 1/1 selftests/ftrace: Fix trace_marker_raw test on 64K page kernels

In reply to Tianchen Ding

On ARM64 kernels with 64K pages, the trace_marker_raw test fails because bash's printf builtin uses stdio buffering which splits output into multiple small write() calls to the tracefs file. Since each individual write is within TRACE_MARKER_MAX_SIZE (4096), they all succeed, causing the "too big" write test to incorrectly pass.

Fix by piping make_str output through dd with iflag=fullblock to guarantee a single atomic write() syscall to trace_marker_raw.

Fixes 37f46601383a ("selftests/tracing: Add basic test for trace_marker_raw file")

---
v2:
Update comment about 64K pages.

---
 .../selftests/ftrace/test.d/00basic/trace_marker_raw.tc    | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
tools/testing/selftests/ftrace/test.d/00basic/trace_marker_raw.tc +5 -2
index 8e905d4fe6dd..f68f1901f65f 100644
@@ -43,8 +43,11 @@ write_buffer() {
 	id=$1
 	size=$2
 
-	# write the string into the raw marker
-	make_str $id $size > trace_marker_raw
+	# Pipe through dd to ensure a single atomic write() syscall
+	# on architectures with 64K pages, where shell's printf builtin
+	# uses stdio buffering which may split the output into multiple
+	# writes.
+	make_str $id $size | dd of=trace_marker_raw bs=`expr $size + 4` iflag=fullblock
 }
 
 
Signature · 1 line
2.39.3
maintainer
#3 lore raw
Quoted 6 lines · On Thu, 28 May 2026 10:24:17 +0800 Tianchen Ding <[email protected]>…
tools/testing/selftests/ftrace/test.d/00basic/trace_marker_raw.tc +0 -0
index 8e905d4fe6dd..f68f1901f65f 100644

tools/testing/selftests/ftrace/test.d/00basic/trace_marker_raw.tc write_buffer() { · line 50

Quoted 10 lines
tools/testing/selftests/ftrace/test.d/00basic/trace_marker_raw.tc +5 -2
@@ -43,8 +43,11 @@ write_buffer() {
 	id=$1
 	size=$2
 
-	# write the string into the raw marker
-	make_str $id $size > trace_marker_raw
+	# Pipe through dd to ensure a single atomic write() syscall
+	# on architectures with 64K pages, where shell's printf builtin
+	# uses stdio buffering which may split the output into multiple
+	# writes.
+	make_str $id $size | dd of=trace_marker_raw bs=`expr $size + 4` iflag=fullblock

I was looking at this more, and I'm not comfortable with the hard coded 4 above. I rather use the length of the string. Something like:

str=make_str $id $size
len=${#str}
echo "$str" | dd of=trace_marker_raw bs=$len iflag=fullblock

-- Steve

Quoted 3 lines
tools/testing/selftests/ftrace/test.d/00basic/trace_marker_raw.tc +0 -0
 }
 
 
#4 lore raw
Quoted 23 lines · Steven Rostedt
Quoted 6 lines · On Thu, 28 May 2026 10:24:17 +0800 Tianchen Ding <[email protected]>…
tools/testing/selftests/ftrace/test.d/00basic/trace_marker_raw.tc +0 -0
index 8e905d4fe6dd..f68f1901f65f 100644

tools/testing/selftests/ftrace/test.d/00basic/trace_marker_raw.tc write_buffer() { · line 50

Quoted 10 lines
tools/testing/selftests/ftrace/test.d/00basic/trace_marker_raw.tc +5 -2
@@ -43,8 +43,11 @@ write_buffer() {
  	id=$1
  	size=$2
 
-	# write the string into the raw marker
-	make_str $id $size > trace_marker_raw
+	# Pipe through dd to ensure a single atomic write() syscall
+	# on architectures with 64K pages, where shell's printf builtin
+	# uses stdio buffering which may split the output into multiple
+	# writes.
+	make_str $id $size | dd of=trace_marker_raw bs=`expr $size + 4` iflag=fullblock

I was looking at this more, and I'm not comfortable with the hard coded 4 above. I rather use the length of the string. Something like:

Quoted 6 lines

str=make_str $id $size
len=${#str}
echo "$str" | dd of=trace_marker_raw bs=$len iflag=fullblock

-- Steve

Capturing make_str output into a shell variable doesn't work because make_str outputs raw binary that may contain NUL bytes, and shell command substitution silently strips them.

However, the val variable inside make_str doesn't hold actual NUL bytes — it holds the text of escape sequences (e.g., the literal characters \003\000\000\000). The binary conversion only happens at the final printf "${val}${data}".

We can take advantage of this by having make_str return the escape-sequence text instead of binary, and letting write_buffer handle the conversion:

  make_str() {
        ...
        printf '%s' "${val}${data}"
  }

  write_buffer() {
        id=$1
        size=$2

str=make_str $id $size
len=$(printf "$str" | wc -c)
printf "$str" | dd of=trace_marker_raw bs=$len iflag=fullblock
}

This way str holds only printable escape-sequence text (no NUL), printf "$str" converts it to real binary through the pipe, and wc -c measures the true binary length.

Quoted 3 lines
Quoted 3 lines
tools/testing/selftests/ftrace/test.d/00basic/trace_marker_raw.tc +0 -0
  }
 
 
maintainer
#5 lore raw
Quoted 12 lines · On Fri, 29 May 2026 10:59:34 +0800 Tianchen Ding <[email protected]>…

We can take advantage of this by having make_str return the escape-sequence text instead of binary, and letting write_buffer handle the conversion:

   make_str() {
         ...
         printf '%s' "${val}${data}"
   }

   write_buffer() {
         id=$1
         size=$2
Quoted 8 lines

str=make_str $id $size
len=$(printf "$str" | wc -c)
printf "$str" | dd of=trace_marker_raw bs=$len iflag=fullblock
}

This way str holds only printable escape-sequence text (no NUL), printf "$str" converts it to real binary through the pipe, and wc -c measures the true binary length.

This is quite hacky, but at least it removes the hardcoded assumptions.

OK, you can send a v3 that does that.

Thanks,

-- Steve

#6 lore raw

PATCH v3 1/1 selftests/ftrace: Fix trace_marker_raw test on 64K page kernels

In reply to Tianchen Ding

On ARM64 kernels with 64K pages, the trace_marker_raw test fails because bash's printf builtin uses stdio buffering which splits output into multiple small write() calls to the tracefs file. Since each individual write is within TRACE_MARKER_MAX_SIZE (4096), they all succeed, causing the "too big" write test to incorrectly pass.

Fix by writing through dd with iflag=fullblock to guarantee a single atomic write() syscall to trace_marker_raw.

Fixes 37f46601383a ("selftests/tracing: Add basic test for trace_marker_raw file")

---
v3:
Measure the binary length via wc -c instead of hard-coding "size + 4".

v2:
Update comment about 64K pages.

---
 .../ftrace/test.d/00basic/trace_marker_raw.tc      | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)
tools/testing/selftests/ftrace/test.d/00basic/trace_marker_raw.tc +11 -3
index 8e905d4fe6dd..f985ff391463 100644
@@ -36,15 +36,23 @@ make_str() {
 
 	data=`printf -- 'X%.0s' $(seq $cnt)`
 
-	printf "${val}${data}"
+	# Return escape-sequence text (e.g. "\003\000..."); the caller
+	# converts to binary. Shell command substitution strips NUL bytes,
+	# so the binary form cannot survive being captured into a variable.
+	printf '%s' "${val}${data}"
 }
 
 write_buffer() {
 	id=$1
 	size=$2
 
-	# write the string into the raw marker
-	make_str $id $size > trace_marker_raw
+	str=`make_str $id $size`
+	len=`printf "$str" | wc -c`
+	# Pipe through dd to ensure a single atomic write() syscall
+	# on architectures with 64K pages, where shell's printf builtin
+	# uses stdio buffering which may split the output into multiple
+	# writes.
+	printf "$str" | dd of=trace_marker_raw bs=$len iflag=fullblock
 }
 
 
Signature · 1 line
2.39.3
maintainer Reviewed-by on v3 1/1
#7 lore raw

Shuah,

can you take this?

Tianchen,

for future patches, please send new versions as a separate thread and not a reply to the thread of the previous version.

Thanks,

-- Steve

Quoted 52 lines · On Mon, 1 Jun 2026 10:32:51 +0800 Tianchen Ding <[email protected]>…

On ARM64 kernels with 64K pages, the trace_marker_raw test fails because bash's printf builtin uses stdio buffering which splits output into multiple small write() calls to the tracefs file. Since each individual write is within TRACE_MARKER_MAX_SIZE (4096), they all succeed, causing the "too big" write test to incorrectly pass.

Fix by writing through dd with iflag=fullblock to guarantee a single atomic write() syscall to trace_marker_raw.

Fixes 37f46601383a ("selftests/tracing: Add basic test for trace_marker_raw file")

---
v3:
Measure the binary length via wc -c instead of hard-coding "size + 4".

v2:
Update comment about 64K pages.

---
 .../ftrace/test.d/00basic/trace_marker_raw.tc      | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)
tools/testing/selftests/ftrace/test.d/00basic/trace_marker_raw.tc +11 -3
index 8e905d4fe6dd..f985ff391463 100644
@@ -36,15 +36,23 @@ make_str() {
 
 	data=`printf -- 'X%.0s' $(seq $cnt)`
 
-	printf "${val}${data}"
+	# Return escape-sequence text (e.g. "\003\000..."); the caller
+	# converts to binary. Shell command substitution strips NUL bytes,
+	# so the binary form cannot survive being captured into a variable.
+	printf '%s' "${val}${data}"
 }
 
 write_buffer() {
 	id=$1
 	size=$2
 
-	# write the string into the raw marker
-	make_str $id $size > trace_marker_raw
+	str=`make_str $id $size`
+	len=`printf "$str" | wc -c`
+	# Pipe through dd to ensure a single atomic write() syscall
+	# on architectures with 64K pages, where shell's printf builtin
+	# uses stdio buffering which may split the output into multiple
+	# writes.
+	printf "$str" | dd of=trace_marker_raw bs=$len iflag=fullblock
 }