Skip to contentSkip to conversation

rt-tests: rt-migrate-test: Report trace_marker write errno once and stop

PATCH New John Kacur 1 participant 1 message Sep 3

lore
Share & tools
lore

John Kacur

author
#0 lore raw

PATCH 1/1 rt-tests: rt-migrate-test: Report trace_marker write errno once and stop

Since commit 1fc77b9d100a ("rt-migrate-test.c: Check return values of write() and ftruncate()"), ftrace_write() prints "write failed" on every failed write to the trace_marker. Because ftrace_write() is called several times per loop, a single persistent failure (e.g. tracefs not writable under SELinux/lockdown, or the markers trace option disabled) floods stderr with ~150 messages and disrupts the progress bar, as reported against realtime-tests-2.10.

The trace marker is a best-effort diagnostic aid, so instead of retrying and spamming, report the failure once - including strerror(errno) so the actual cause is visible - then close the fd and disable further marker writes for the rest of the run.

Also clamp the vsnprintf() return value before passing it to write():
vsnprintf() returns the length it would have written, so an over-long format could otherwise hand write() a length past the end of buff.

Assisted-by Claude:claude-opus-4-8
---
 src/rt-migrate-test/rt-migrate-test.c | 22 ++++++++++++++++++++--
 1 file changed, 20 insertions(+), 2 deletions(-)
src/rt-migrate-test/rt-migrate-test.c +20 -2
index 1c60fe4d3a01..c33df58acf1f 100644
@@ -68,9 +68,27 @@ static void ftrace_write(const char *fmt, ...)
 	n = vsnprintf(buff, BUFSIZ, fmt, ap);
 	va_end(ap);
 
+	/*
+	 * vsnprintf() returns the number of characters it would have
+	 * written; clamp it so we never hand write() a length past the
+	 * end of buff.
+	 */
+	if (n >= BUFSIZ)
+		n = BUFSIZ - 1;
+
 	ret = write(mark_fd, buff, n);
-	if (ret < 0)
-		fprintf(stderr, "%s: write failed\n", __func__);
+	if (ret < 0) {
+		/*
+		 * The trace marker is a best-effort diagnostic aid. If the
+		 * write fails (e.g. tracefs not writable, markers disabled),
+		 * report it once and stop trying, rather than flooding stderr
+		 * for every marker for the rest of the run.
+		 */
+		fprintf(stderr, "%s: write to trace_marker failed: %s\n",
+			__func__, strerror(errno));
+		close(mark_fd);
+		mark_fd = -1;
+	}
 }
 
 #define nano2sec(nan) (nan / 1000000000ULL)
Signature · 1 line
2.55.0