AMD-GFX Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Tvrtko Ursulin" <tvrtko.ursulin@igalia.com>,
	amd-gfx@lists.freedesktop.org
Subject: Re: [RFC 2/4] drm/amdgpu: Cleanup on ring failure
Date: Mon, 30 Nov 2026 16:32:32 +0000	[thread overview]
Message-ID: <sashiko-outbox-241584@kernel.org> (raw)
In-Reply-To: <20261130161016.64542-3-tvrtko.ursulin@igalia.com>

Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [High] drm/amdgpu: amdgpu_ring_init() returns 0 on ring_backup alloc
  failure
- [Medium] drm/amdgpu: amdgpu_ring_fini() frees WB slot 0 and
  double-frees WB slots
- [Medium] drm/amdgpu: incomplete fence_drv cleanup on error in
  amdgpu_ring_init()
--

commit 52137188f323e99fb1cb2c51c462dcc3580d61b7
Author: Tvrtko Ursulin <tvrtko.ursulin@igalia.com>

drm/amdgpu: Cleanup on ring failure

This commit calls amdgpu_ring_fini() on the error path of
amdgpu_ring_init() to clean up partially constructed rings, and clears
ring->ring_obj in amdgpu_ring_fini().

> diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c
> index 71cabb884ac20..92eb00fdc2a15 100644
> --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c
> +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c
> @@ -249,31 +249,31 @@ int amdgpu_ring_init(struct amdgpu_device *adev, struct amdgpu_ring *ring,
>  	r = amdgpu_wb_get(adev, &ring->rptr_offs);
>  	if (r) {
>  		dev_err(adev->dev, "(%d) ring rptr_offs wb alloc failed\n", r);
> -		return r;
> +		goto err;
>  	}
>  
>  	r = amdgpu_wb_get(adev, &ring->wptr_offs);
>  	if (r) {
>  		dev_err(adev->dev, "(%d) ring wptr_offs wb alloc failed\n", r);
> -		return r;
> +		goto err;
>  	}
>  
>  	r = amdgpu_wb_get(adev, &ring->fence_offs);
>  	if (r) {
>  		dev_err(adev->dev, "(%d) ring fence_offs wb alloc failed\n", r);
> -		return r;
> +		goto err;
>  	}
>  
>  	r = amdgpu_wb_get(adev, &ring->trail_fence_offs);
>  	if (r) {
>  		dev_err(adev->dev, "(%d) ring trail_fence_offs wb alloc failed\n", r);
> -		return r;
> +		goto err;
>  	}
>  
>  	r = amdgpu_wb_get(adev, &ring->cond_exe_offs);
>  	if (r) {
>  		dev_err(adev->dev, "(%d) ring cond_exec_polling wb alloc failed\n", r);
> -		return r;
> +		goto err;
>  	}

[Severity: Medium]
Can jumping to err when one of the amdgpu_wb_get() calls fails free
writeback slot 0 out from under its owner, and double free all five slots
when amdgpu_ring_fini() is called again during teardown?

Slot 0 in adev->wb.used is a valid writeback slot with offset 0, so when
amdgpu_ring_fini() calls amdgpu_wb_free() on all five offsets, any offset
that was not yet allocated is still 0 and clears bit 0 of adev->wb.used.
Also, amdgpu_ring_fini() does not clear ring->adev or
ring->adev->rings[ring->idx], so a later teardown call such as
amdgpu_cper_fini() -> amdgpu_ring_fini() will run a second time and free
all five writeback offsets again.

[ ... ]

> @@ -333,7 +333,7 @@ int amdgpu_ring_init(struct amdgpu_device *adev, struct amdgpu_ring *ring,
>  	if (!ring->ring_backup) {
>  		ring->ring_backup = kvzalloc(ring->ring_size, GFP_KERNEL);
>  		if (!ring->ring_backup)
> -			return -ENOMEM;
> +			goto err;
>  	}

[Severity: High]
Does amdgpu_ring_init() return 0 when allocating ring->ring_backup fails?
At this point r holds 0 from the preceding call, so jumping to err tears
down the ring via amdgpu_ring_fini() and returns success to the caller,
leading to a NULL pointer dereference when commands are emitted to
ring->ring.

>  
>  	/* Allocate ring buffer */
> @@ -344,11 +344,9 @@ int amdgpu_ring_init(struct amdgpu_device *adev, struct amdgpu_ring *ring,
>  					    &ring->ring_obj,
>  					    &ring->gpu_addr,
>  					    (void **)&ring->ring);
> -		if (r) {
> -			dev_err(adev->dev, "(%d) ring create failed\n", r);
> -			kvfree(ring->ring_backup);
> -			return r;
> -		}
> +		if (r)
> +			goto err;
> +
>  		amdgpu_ring_clear_ring(ring);
>  	}
>  
> @@ -364,6 +362,10 @@ int amdgpu_ring_init(struct amdgpu_device *adev, struct amdgpu_ring *ring,
>  	}
>  
>  	return 0;
> +
> +err:
> +	amdgpu_ring_fini(ring);
> +	return r;
>  }

[Severity: Medium]
Does the err path leave ring->fence_drv uncleaned when amdgpu_ring_init()
fails after amdgpu_fence_driver_init_ring()?

amdgpu_ring_fini() does not free ring->fence_drv.fences or reset
ring->fence_drv.initialized.  If amdgpu_ring_init() fails before
amdgpu_fence_driver_start_ring() (or after commit "drm/amdgpu: Unconfuse
amdgpu_ring_init()" moves the adev->rings[ring->idx] assignment to the end
of the function), amdgpu_fence_driver_sw_fini() skips the ring and leaks
ring->fence_drv.fences; if it fails after amdgpu_fence_driver_start_ring()
in this commit, the ring remains in adev->rings[] with
fence_drv.initialized set and fence_drv.cpu_addr pointing to the freed
ring->fence_offs writeback slot.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261130161016.64542-1-tvrtko.ursulin@igalia.com?part=2

  reply	other threads:[~2026-11-30 16:32 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-30 16:10 [RFC 0/4] Use high priority scheduler workqueue for high priority rings Tvrtko Ursulin
2026-11-30 16:10 ` [RFC 1/4] drm/amdgpu: Free trail_fence_offs when freeing rings Tvrtko Ursulin
2026-11-30 16:10 ` [RFC 2/4] drm/amdgpu: Cleanup on ring failure Tvrtko Ursulin
2026-11-30 16:32   ` sashiko-bot [this message]
2026-11-30 16:10 ` [RFC 3/4] drm/amdgpu: Unconfuse amdgpu_ring_init() Tvrtko Ursulin
2026-11-30 16:28   ` sashiko-bot
2026-11-30 16:10 ` [RFC 4/4] drm/amdgpu: Use high priority scheduler workqueue for high priority rings Tvrtko Ursulin
2026-11-30 16:26   ` sashiko-bot
2026-11-30 19:50 ` [RFC 0/4] " Tvrtko Ursulin

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=sashiko-outbox-241584@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=amd-gfx@lists.freedesktop.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=tvrtko.ursulin@igalia.com \
    /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