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 3/4] drm/amdgpu: Unconfuse amdgpu_ring_init()
Date: Mon, 30 Nov 2026 16:28:31 +0000	[thread overview]
Message-ID: <sashiko-outbox-241582@kernel.org> (raw)
In-Reply-To: <20261130161016.64542-4-tvrtko.ursulin@igalia.com>

Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [Medium] drm/amdgpu: leak of ring->fence_drv.fences on
  amdgpu_ring_init() failure
- [Medium] drm/amdgpu: stale ring->adev allows repeated
  amdgpu_ring_fini() teardown
- [Medium] drm/amdgpu: amdgpu_ring_fini() frees WB slot 0 on partial WB
  init failure
- [Medium] drm/amdgpu: amdgpu_ring_init() returns 0 when ring_backup
  alloc fails
--

commit 30b04e0d84c8a20af2bf83c53f41bd8a634cd709
Author: Tvrtko Ursulin <tvrtko.ursulin@igalia.com>

drm/amdgpu: Unconfuse amdgpu_ring_init()

This commit removes the ring->adev == NULL check in amdgpu_ring_init() and
defers registering the ring in adev->rings[] until initialization
succeeds.

> diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c
> index 92eb00fdc2a15..2a96cb7d891b5 100644
> --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c
> +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c
> @@ -217,6 +217,9 @@ int amdgpu_ring_init(struct amdgpu_device *adev, struct amdgpu_ring *ring,
>  	unsigned int max_ibs_dw;
>  	int r;
>  
> +	if (adev->num_rings >= AMDGPU_MAX_RINGS)
> +		return -EINVAL;
> +
>  	/* Set the hw submission limit higher for KIQ because
>  	 * it's used for a number of gfx/compute tasks by both
>  	 * KFD and KGD which may have outstanding fences and
> @@ -230,21 +233,13 @@ int amdgpu_ring_init(struct amdgpu_device *adev, struct amdgpu_ring *ring,
>  	else if (ring == &adev->sdma.instance[0].page)
>  		sched_hw_submission = 256;
>  
> -	if (ring->adev == NULL) {
> -		if (adev->num_rings >= AMDGPU_MAX_RINGS)
> -			return -EINVAL;
> -
> -		ring->adev = adev;
> -		ring->num_hw_submission = sched_hw_submission;
> -		ring->sched_score = sched_score;
> -
> -		ring->idx = adev->num_rings++;
> -		adev->rings[ring->idx] = ring;
> +	ring->adev = adev;
> +	ring->num_hw_submission = sched_hw_submission;
> +	ring->sched_score = sched_score;
>  
> -		r = amdgpu_fence_driver_init_ring(ring);
> -		if (r)
> -			return r;
> -	}
> +	r = amdgpu_fence_driver_init_ring(ring);
> +	if (r)
> +		return r;
>  
>  	r = amdgpu_wb_get(adev, &ring->rptr_offs);
>  	if (r) {
> @@ -361,6 +356,9 @@ int amdgpu_ring_init(struct amdgpu_device *adev, struct amdgpu_ring *ring,
>  		gpu_sched->sched[gpu_sched->num_scheds++] = &ring->sched;
>  	}
>  
> +	ring->idx = adev->num_rings++;
> +	adev->rings[ring->idx] = ring;
> +
>  	return 0;
>  
>  err:

[Severity: Medium]
Does deferring adev->rings[ring->idx] = ring leak ring->fence_drv.fences
when amdgpu_ring_init() fails after amdgpu_fence_driver_init_ring()?

amdgpu_fence_driver_init_ring() allocates ring->fence_drv.fences, which is
only freed by amdgpu_fence_driver_sw_fini() while iterating over
adev->rings[].  Because a ring that fails at a later step (such as
amdgpu_bo_create_kernel()) is no longer added to adev->rings[], and
amdgpu_ring_fini() does not free ring->fence_drv.fences, that allocation
is leaked on error unwind.

[Severity: Medium]
Also in amdgpu_ring_init(), commit "drm/amdgpu: Cleanup on ring failure"
in this series changed the error paths to jump to err: and call
amdgpu_ring_fini().  Can a failure in one of the five amdgpu_wb_get()
calls free active writeback slot 0?

The first slot allocated by amdgpu_wb_get() has bit index 0 in
adev->wb.used and offset 0.  When an amdgpu_wb_get() call in
amdgpu_ring_init() fails, any remaining unallocated offset fields in ring
are still 0, and amdgpu_ring_fini() passes all five offsets to
amdgpu_wb_free(), which shifts 0 >> 3 and clears bit 0 of adev->wb.used.

[Severity: Medium]
Similarly in the err: path added by commit "drm/amdgpu: Cleanup on ring
failure", does amdgpu_ring_init() return 0 when kvzalloc() for
ring->ring_backup fails?

drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c:amdgpu_ring_init() {
    ...
	if (!ring->ring_backup) {
		ring->ring_backup = kvzalloc(ring->ring_size, GFP_KERNEL);
		if (!ring->ring_backup)
			goto err;
	}
    ...
err:
	amdgpu_ring_fini(ring);
	return r;
}

At that point r holds 0 from the preceding amdgpu_fence_driver_start_ring()
or amdgpu_wb_get() call, so jumping to err tears down the ring and returns
0 to the caller with ring->ring_obj and ring->ring left NULL.

> @@ -378,8 +376,7 @@ int amdgpu_ring_init(struct amdgpu_device *adev, struct amdgpu_ring *ring,
>  void amdgpu_ring_fini(struct amdgpu_ring *ring)
>  {
>  
> -	/* Not to finish a ring which is not initialized */
> -	if (!(ring->adev) || !(ring->adev->rings[ring->idx]))
> +	if (!ring->adev)
>  		return;
>  
>  	ring->sched.ready = false;

[Severity: Medium]
If ring->adev is neither cleared in amdgpu_ring_fini() nor rolled back
when amdgpu_fence_driver_init_ring() fails, can a subsequent
amdgpu_ring_fini() call during device teardown run on an uninitialized or
already finalized ring?

For example, amdgpu_device_ip_fini() unconditionally calls
amdgpu_cper_fini() -> amdgpu_ring_fini(&adev->cper.ring_buf).  If
amdgpu_ring_init() failed, ring->adev remains non-NULL and the second
amdgpu_ring_fini() call will re-run teardown and pass unallocated (0) or
already freed writeback offsets to amdgpu_wb_free().

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

  reply	other threads:[~2026-11-30 16:28 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
2026-11-30 16:10 ` [RFC 3/4] drm/amdgpu: Unconfuse amdgpu_ring_init() Tvrtko Ursulin
2026-11-30 16:28   ` sashiko-bot [this message]
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-241582@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