Igt-dev Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Harry Wentland <harry.wentland@amd.com>
To: <igt-dev@lists.freedesktop.org>
Cc: Harry Wentland <harry.wentland@amd.com>
Subject: [PATCH] lib/igt_amd: don't rely on NUL separators parsing link_settings
Date: Tue, 23 Jun 2026 11:55:41 -0400	[thread overview]
Message-ID: <20260623155541.32145-1-harry.wentland@amd.com> (raw)

igt_amd_read_link_settings() parsed the dp_link_settings debugfs node by
replacing the NUL bytes that the kernel accidentally wrote between the
Current/Verified/Reported/Preferred records with ';' and using those as
strtok delimiters.

The kernel side has been fixed to emit a single, properly NUL-terminated
string with no embedded NULs (amdgpu: fix malformed link_settings debugfs
output). With that fix the buffer no longer contains internal separators,
so the old parser collapsed the whole string into a single token: only the
Current record was parsed while Verified, Reported and Preferred were left
unset (0). amd_ilr then wrote a lane count of 0 and asserted it against the
trained lane count, failing the ilr-link-training-configs subtest. The
NUL-scanning while loop also walked into uninitialized stack.

Parse the node by locating each label and reading the three values that
follow it, so it no longer depends on embedded NULs or any other record
separator and works with the contiguous string the kernel now produces.

Assisted-by: Copilot:claude-opus-4.8
Signed-off-by: Harry Wentland <harry.wentland@amd.com>
---
 lib/igt_amd.c | 38 ++++++++++++++++++++------------------
 1 file changed, 20 insertions(+), 18 deletions(-)

diff --git a/lib/igt_amd.c b/lib/igt_amd.c
index 47e587e54de0..7d33ab65858c 100644
--- a/lib/igt_amd.c
+++ b/lib/igt_amd.c
@@ -833,10 +833,13 @@ int igt_amd_trigger_hotplug(int drm_fd, char *connector_name)
 void igt_amd_read_link_settings(
 	int drm_fd, const char *connector_name, int *lane_count, int *link_rate, int *link_spread)
 {
+	static const char * const labels[] = {
+		"Current:", "Verified:", "Reported:", "Preferred:"
+	};
 	int fd, ret;
 	char buf[101];
-	int i = 0;
-	char *token_end, *val_token;
+	char *ptr;
+	int i;
 
 	fd = igt_debugfs_connector_dir(drm_fd, connector_name, O_RDONLY);
 	if (fd < 0) {
@@ -850,23 +853,22 @@ void igt_amd_read_link_settings(
 
 	close(fd);
 
-	/* Between current, verified, reported, and preferred are null terminators,
-	 * replace them with ';' to use as the delimiter for strtok. */
-	while (strlen(buf) < sizeof(buf) - 1 && buf[strlen(buf)] == '\0')
-		buf[strlen(buf)] = ';';
+	/* The debugfs node returns a single NUL-terminated string of the form:
+	 *   "Current:  %d  0x%x  %d  Verified:  %d  0x%x  %d  "
+	 *   "Reported:  %d  0x%x  %d  Preferred:  %d  0x%x  %d\n"
+	 * Locate each label and parse the three values that follow it. Do not
+	 * rely on embedded NUL bytes (or any other delimiter) between records,
+	 * as the kernel emits a single contiguous string. */
+	ptr = buf;
+	for (i = 0; i < 4; i++) {
+		ptr = strstr(ptr, labels[i]);
+		if (ptr == NULL)
+			break;
 
-	/* Parse values read from file. */
-	for (char *token = strtok_r(buf, ";", &token_end);
-	     token != NULL;
-	     token = strtok_r(NULL, ";", &token_end))
-	{
-		strtok_r(token, ": ", &val_token);
-		lane_count[i] = strtol(val_token, &val_token, 10);
-		link_rate[i] = strtol(val_token, &val_token, 16);
-		link_spread[i] = strtol(val_token, &val_token, 10);
-		i++;
-
-		if (i > 3) return;
+		ptr += strlen(labels[i]);
+		lane_count[i] = strtol(ptr, &ptr, 10);
+		link_rate[i] = strtol(ptr, &ptr, 16);
+		link_spread[i] = strtol(ptr, &ptr, 10);
 	}
 }
 
-- 
2.54.0


             reply	other threads:[~2026-06-23 15:56 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-06-23 15:55 Harry Wentland [this message]
2026-06-23 16:59 ` ✓ Xe.CI.BAT: success for lib/igt_amd: don't rely on NUL separators parsing link_settings Patchwork
2026-06-23 17:19 ` ✓ i915.CI.BAT: " Patchwork
2026-06-23 22:55 ` ✓ Xe.CI.FULL: " Patchwork
2026-06-24  2:23 ` ✗ i915.CI.Full: failure " Patchwork
2026-09-03 14:59 ` [PATCH] " Alex Hung

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260623155541.32145-1-harry.wentland@amd.com \
    --to=harry.wentland@amd.com \
    --cc=igt-dev@lists.freedesktop.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox