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 v2 2/4] drm/amdgpu: Cleanup on ring failure
Date: Tue, 01 Dec 2026 10:57:26 +0000	[thread overview]
Message-ID: <sashiko-outbox-243105@kernel.org> (raw)
In-Reply-To: <20261201103953.74817-3-tvrtko.ursulin@igalia.com>

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] drm/amdgpu: fence array leak in
  amdgpu_fence_driver_fini_ring()
- [Medium] drm/amdgpu: invalid WB slot 0 free on fence init error in
  amdgpu_ring_init()
--

commit 3860c6ba520690a3a97d15071c514291b18c54d5
Author: Tvrtko Ursulin <tvrtko.ursulin@igalia.com>

drm/amdgpu: Cleanup on ring failure

This commit exports amdgpu_fence_driver_fini_ring(), marks unallocated
writeback slots as AMDGPU_RING_WB_INVALID, and calls amdgpu_ring_fini()
on failure in amdgpu_ring_init().

> diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c
> index d0a6901532fd6..141bf5503755f 100644
> --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c
> +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c
> @@ -577,31 +577,43 @@ void amdgpu_fence_driver_isr_toggle(struct amdgpu_device *adev, bool stop)
>  	}
>  }
>  
> -void amdgpu_fence_driver_sw_fini(struct amdgpu_device *adev)
> +/**
> + * amdgpu_fence_driver_fini_ring - fini the fence driver
> + * for the requested ring.
> + *
> + * @ring: ring to fini the fence driver on
> + *
> + * Cleanup the fence driver for the requested ring (all asics).
> + */
> +void amdgpu_fence_driver_fini_ring(struct amdgpu_ring *ring)
>  {
> -	unsigned int i, j;
> +	unsigned int i;
>  
> -	for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
> -		struct amdgpu_ring *ring = adev->rings[i];
> +	if (!ring || !ring->fence_drv.initialized)
> +		return;
>  
> -		if (!ring || !ring->fence_drv.initialized)
> -			continue;
> +	/*
> +	 * Notice we check for sched.ops since there's some
> +	 * override on the meaning of sched.ready by amdgpu.
> +	 * The natural check would be sched.ready, which is
> +	 * set as drm_sched_init() finishes...
> +	 */
> +	if (ring->sched.ops)
> +		drm_sched_fini(&ring->sched);
>  
> -		/*
> -		 * Notice we check for sched.ops since there's some
> -		 * override on the meaning of sched.ready by amdgpu.
> -		 * The natural check would be sched.ready, which is
> -		 * set as drm_sched_init() finishes...
> -		 */
> -		if (ring->sched.ops)
> -			drm_sched_fini(&ring->sched);
> -
> -		for (j = 0; j <= ring->fence_drv.num_fences_mask; ++j)
> -			dma_fence_put(ring->fence_drv.fences[j]);
> -		kfree(ring->fence_drv.fences);
> -		ring->fence_drv.fences = NULL;
> -		ring->fence_drv.initialized = false;
> -	}
> +	for (i = 0; i <= ring->fence_drv.num_fences_mask; ++i)
> +		dma_fence_put(ring->fence_drv.fences[i]);
> +	kfree(ring->fence_drv.fences);
> +	ring->fence_drv.fences = NULL;
> +	ring->fence_drv.initialized = false;
> +}

[Severity: Medium]
Does checking ring->fence_drv.initialized here leak ring->fence_drv.fences?

amdgpu_fence_driver_init_ring() allocates ring->fence_drv.fences with
ring->fence_drv.initialized set to false, and initialized is only set to
true later by amdgpu_fence_driver_start_ring() (which is never called for
AMDGPU_RING_TYPE_CPER rings).  If any of the five amdgpu_wb_get() calls in
amdgpu_ring_init() fails, or when a CPER ring is torn down,
amdgpu_fence_driver_fini_ring() returns early without freeing
ring->fence_drv.fences, and amdgpu_ring_fini() then clears
ring->adev->rings[ring->idx] and ring->adev.

[ ... ]

> diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c
> index 71cabb884ac20..2a2b4c39c2892 100644
> --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c
> +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c
> @@ -246,34 +246,40 @@ int amdgpu_ring_init(struct amdgpu_device *adev, struct amdgpu_ring *ring,
>  			return r;
>  	}
>  
> +	ring->rptr_offs = AMDGPU_RING_WB_INVALID;
> +	ring->wptr_offs = AMDGPU_RING_WB_INVALID;
> +	ring->fence_offs = AMDGPU_RING_WB_INVALID;
> +	ring->trail_fence_offs = AMDGPU_RING_WB_INVALID;
> +	ring->cond_exe_offs = AMDGPU_RING_WB_INVALID;

[Severity: Medium]
Can an error return from amdgpu_fence_driver_init_ring() right above this
cause amdgpu_ring_fini() to free writeback slot 0 five times?

Before calling amdgpu_fence_driver_init_ring(), amdgpu_ring_init() has
already set ring->adev and adev->rings[ring->idx]:

drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c:amdgpu_ring_init() {
	if (ring->adev == NULL) {
		if (adev->num_rings >= AMDGPU_MAX_RINGS)
			return -EINVAL;

		ring->adev = adev;
		...
		ring->idx = adev->num_rings++;
		adev->rings[ring->idx] = ring;

		r = amdgpu_fence_driver_init_ring(ring);
		if (r)
			return r;
	}
}

If amdgpu_fence_driver_init_ring() fails, the five writeback offsets are
left at 0 rather than AMDGPU_RING_WB_INVALID, so a subsequent
amdgpu_ring_fini() during teardown (for example via amdgpu_cper_fini())
passes the ring->adev and adev->rings[ring->idx] check and calls
amdgpu_wb_free(ring->adev, 0) for each of the five offsets.

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

  reply	other threads:[~2026-12-01 10:57 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-01 10:39 [RFC v2 0/4] Use high priority scheduler workqueue for high priority rings Tvrtko Ursulin
2026-12-01 10:39 ` [RFC v2 1/4] drm/amdgpu: Free trail_fence_offs when freeing rings Tvrtko Ursulin
2026-12-01 10:39 ` [RFC v2 2/4] drm/amdgpu: Cleanup on ring failure Tvrtko Ursulin
2026-12-01 10:57   ` sashiko-bot [this message]
2026-12-01 10:39 ` [RFC v2 3/4] drm/amdgpu: Unconfuse amdgpu_ring_init() Tvrtko Ursulin
2026-12-01 11:01   ` sashiko-bot
2026-12-01 10:39 ` [RFC v2 4/4] drm/amdgpu: Use high priority scheduler workqueue for high priority rings 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-243105@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