public inbox for drm-ai-reviews@public-inbox.freedesktop.org
 help / color / mirror / Atom feed
* [PATCH] drm/amdgpu: switch XGMI sysfs show helpers to sysfs_emit_at()
@ 2026-03-13  1:52 David Baum
  2026-03-13 14:04 ` Alex Deucher
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: David Baum @ 2026-03-13  1:52 UTC (permalink / raw)
  To: alex.deucher, christian.koenig
  Cc: lijo.lazar, airlied, simona, amd-gfx, dri-devel, David Baum

The XGMI sysfs show helpers amdgpu_xgmi_show_num_hops() and
amdgpu_xgmi_show_num_links() currently populate the output buffer with
sprintf() and then call sysfs_emit(buf, "%s\n", buf) to append the final
newline.

Convert both helpers to use sysfs_emit_at() while tracking the current
offset. This keeps buffer construction in the sysfs helpers, avoids
feeding the output buffer back into the final formatted write, and
matches the style already used by
amdgpu_xgmi_show_connected_port_num().

Signed-off-by: David Baum <davidbaum461@gmail.com>
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_xgmi.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_xgmi.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_xgmi.c
index 11e56df1d91b..13fc7247fe23 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_xgmi.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_xgmi.c
@@ -469,12 +469,12 @@ static ssize_t amdgpu_xgmi_show_num_hops(struct device *dev,
 	struct drm_device *ddev = dev_get_drvdata(dev);
 	struct amdgpu_device *adev = drm_to_adev(ddev);
 	struct psp_xgmi_topology_info *top = &adev->psp.xgmi_context.top_info;
-	int i;
+	int i, offset = 0;
 
 	for (i = 0; i < top->num_nodes; i++)
-		sprintf(buf + 3 * i, "%02x ", top->nodes[i].num_hops);
+		offset += sysfs_emit_at(buf, offset, "%02x ", top->nodes[i].num_hops);
 
-	return sysfs_emit(buf, "%s\n", buf);
+	return offset + sysfs_emit_at(buf, offset, "\n");
 }
 
 static ssize_t amdgpu_xgmi_show_num_links(struct device *dev,
@@ -484,12 +484,12 @@ static ssize_t amdgpu_xgmi_show_num_links(struct device *dev,
 	struct drm_device *ddev = dev_get_drvdata(dev);
 	struct amdgpu_device *adev = drm_to_adev(ddev);
 	struct psp_xgmi_topology_info *top = &adev->psp.xgmi_context.top_info;
-	int i;
+	int i, offset = 0;
 
 	for (i = 0; i < top->num_nodes; i++)
-		sprintf(buf + 3 * i, "%02x ", top->nodes[i].num_links);
+		offset += sysfs_emit_at(buf, offset, "%02x ", top->nodes[i].num_links);
 
-	return sysfs_emit(buf, "%s\n", buf);
+	return offset + sysfs_emit_at(buf, offset, "\n");
 }
 
 static ssize_t amdgpu_xgmi_show_connected_port_num(struct device *dev,
-- 
2.50.1 (Apple Git-155)


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] drm/amdgpu: switch XGMI sysfs show helpers to sysfs_emit_at()
  2026-03-13  1:52 [PATCH] drm/amdgpu: switch XGMI sysfs show helpers to sysfs_emit_at() David Baum
@ 2026-03-13 14:04 ` Alex Deucher
  2026-03-13 21:16 ` Claude review: " Claude Code Review Bot
  2026-03-13 21:16 ` Claude Code Review Bot
  2 siblings, 0 replies; 4+ messages in thread
From: Alex Deucher @ 2026-03-13 14:04 UTC (permalink / raw)
  To: David Baum
  Cc: alex.deucher, christian.koenig, lijo.lazar, airlied, simona,
	amd-gfx, dri-devel

Applied.  Thanks!

Alex

On Fri, Mar 13, 2026 at 4:55 AM David Baum <davidbaum461@gmail.com> wrote:
>
> The XGMI sysfs show helpers amdgpu_xgmi_show_num_hops() and
> amdgpu_xgmi_show_num_links() currently populate the output buffer with
> sprintf() and then call sysfs_emit(buf, "%s\n", buf) to append the final
> newline.
>
> Convert both helpers to use sysfs_emit_at() while tracking the current
> offset. This keeps buffer construction in the sysfs helpers, avoids
> feeding the output buffer back into the final formatted write, and
> matches the style already used by
> amdgpu_xgmi_show_connected_port_num().
>
> Signed-off-by: David Baum <davidbaum461@gmail.com>
> ---
>  drivers/gpu/drm/amd/amdgpu/amdgpu_xgmi.c | 12 ++++++------
>  1 file changed, 6 insertions(+), 6 deletions(-)
>
> diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_xgmi.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_xgmi.c
> index 11e56df1d91b..13fc7247fe23 100644
> --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_xgmi.c
> +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_xgmi.c
> @@ -469,12 +469,12 @@ static ssize_t amdgpu_xgmi_show_num_hops(struct device *dev,
>         struct drm_device *ddev = dev_get_drvdata(dev);
>         struct amdgpu_device *adev = drm_to_adev(ddev);
>         struct psp_xgmi_topology_info *top = &adev->psp.xgmi_context.top_info;
> -       int i;
> +       int i, offset = 0;
>
>         for (i = 0; i < top->num_nodes; i++)
> -               sprintf(buf + 3 * i, "%02x ", top->nodes[i].num_hops);
> +               offset += sysfs_emit_at(buf, offset, "%02x ", top->nodes[i].num_hops);
>
> -       return sysfs_emit(buf, "%s\n", buf);
> +       return offset + sysfs_emit_at(buf, offset, "\n");
>  }
>
>  static ssize_t amdgpu_xgmi_show_num_links(struct device *dev,
> @@ -484,12 +484,12 @@ static ssize_t amdgpu_xgmi_show_num_links(struct device *dev,
>         struct drm_device *ddev = dev_get_drvdata(dev);
>         struct amdgpu_device *adev = drm_to_adev(ddev);
>         struct psp_xgmi_topology_info *top = &adev->psp.xgmi_context.top_info;
> -       int i;
> +       int i, offset = 0;
>
>         for (i = 0; i < top->num_nodes; i++)
> -               sprintf(buf + 3 * i, "%02x ", top->nodes[i].num_links);
> +               offset += sysfs_emit_at(buf, offset, "%02x ", top->nodes[i].num_links);
>
> -       return sysfs_emit(buf, "%s\n", buf);
> +       return offset + sysfs_emit_at(buf, offset, "\n");
>  }
>
>  static ssize_t amdgpu_xgmi_show_connected_port_num(struct device *dev,
> --
> 2.50.1 (Apple Git-155)
>

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Claude review: drm/amdgpu: switch XGMI sysfs show helpers to sysfs_emit_at()
  2026-03-13  1:52 [PATCH] drm/amdgpu: switch XGMI sysfs show helpers to sysfs_emit_at() David Baum
  2026-03-13 14:04 ` Alex Deucher
  2026-03-13 21:16 ` Claude review: " Claude Code Review Bot
@ 2026-03-13 21:16 ` Claude Code Review Bot
  2 siblings, 0 replies; 4+ messages in thread
From: Claude Code Review Bot @ 2026-03-13 21:16 UTC (permalink / raw)
  To: dri-devel-reviews

Overall Series Review

Subject: drm/amdgpu: switch XGMI sysfs show helpers to sysfs_emit_at()
Author: David Baum <davidbaum461@gmail.com>
Patches: 2
Reviewed: 2026-03-14T07:16:57.684961

---

This is a single, well-motivated cleanup patch that converts two sysfs show helpers from using `sprintf()` + a self-referencing `sysfs_emit(buf, "%s\n", buf)` pattern to the proper `sysfs_emit_at()` API. The existing code has a real bug: passing `buf` as both the destination and a `%s` argument to `sysfs_emit()` is undefined behavior (overlapping source and destination in a formatted print). The fix is correct and follows the pattern already established by the neighboring `amdgpu_xgmi_show_connected_port_num()` function.

**Verdict: Looks good.** One minor concern noted below, but it's not a blocker.

---
Generated by Claude Code Patch Reviewer

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Claude review: drm/amdgpu: switch XGMI sysfs show helpers to sysfs_emit_at()
  2026-03-13  1:52 [PATCH] drm/amdgpu: switch XGMI sysfs show helpers to sysfs_emit_at() David Baum
  2026-03-13 14:04 ` Alex Deucher
@ 2026-03-13 21:16 ` Claude Code Review Bot
  2026-03-13 21:16 ` Claude Code Review Bot
  2 siblings, 0 replies; 4+ messages in thread
From: Claude Code Review Bot @ 2026-03-13 21:16 UTC (permalink / raw)
  To: dri-devel-reviews

Patch Review

**Positive aspects:**

1. Fixes a real (albeit likely benign in practice) undefined behavior issue where the old code passes `buf` as both output buffer and `%s` format argument in `sysfs_emit(buf, "%s\n", buf)`.
2. The conversion to `sysfs_emit_at()` is the correct modern kernel API for building up sysfs output incrementally.
3. Matches the existing style of `amdgpu_xgmi_show_connected_port_num()` at line 505 of the same file, which already uses `sysfs_emit_at()` with a `size` offset variable.

**Minor concern:**

The return value calculation on the final line:

```c
return offset + sysfs_emit_at(buf, offset, "\n");
```

This works correctly — `offset` holds the bytes written so far, and `sysfs_emit_at()` returns the number of bytes written for the newline (1), so the total returned is the full length. However, there is an edge case: if `sysfs_emit_at()` were to return a negative error value, this addition would produce an incorrect result. In practice, `sysfs_emit_at()` only returns negative if `offset` is already beyond `PAGE_SIZE`, which won't happen here with a reasonable `num_nodes` count. The neighboring `amdgpu_xgmi_show_connected_port_num()` uses the same `size +=` pattern, so this is consistent.

**Overall:** The patch is clean, correct, and well-scoped. No issues that would block merging.

Reviewed-by: looks good to me.

---
Generated by Claude Code Patch Reviewer

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-03-13 21:16 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-03-13  1:52 [PATCH] drm/amdgpu: switch XGMI sysfs show helpers to sysfs_emit_at() David Baum
2026-03-13 14:04 ` Alex Deucher
2026-03-13 21:16 ` Claude review: " Claude Code Review Bot
2026-03-13 21:16 ` Claude Code Review Bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox