AMD-GFX Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 1/2] drm/amdgpu: reserve a fence slot before adding the eviction fence
@ 2026-11-19  9:56 Jesse Zhang
  2026-11-19  9:56 ` [PATCH 2/2] drm/amdgpu: don't unreserve the freed root PD in amdgpu_vm_init() Jesse Zhang
  2026-11-19 10:13 ` [PATCH 1/2] drm/amdgpu: reserve a fence slot before adding the eviction fence sashiko-bot
  0 siblings, 2 replies; 6+ messages in thread
From: Jesse Zhang @ 2026-11-19  9:56 UTC (permalink / raw)
  To: amd-gfx; +Cc: Alexander.Deucher, Christian Koenig, Sunil Khatri, Jesse Zhang

After lock contention, drm_exec may retain a BO removed from the VM
during the retry. The BO misses caller-side fence reservation, but
rearm still attaches a fence to it. If validation needs no move, this
can trigger BUG_ON() in dma_resv_add_fence().

 kernel BUG at drivers/dma-buf/dma-resv.c:319!
      Oops: invalid opcode: 0000 [#1] SMP KASAN NOPTI
      Workqueue: events_freezable amdgpu_userq_restore_worker [amdgpu]
      RIP: 0010:dma_resv_add_fence+0x833/0x920
       amdgpu_evf_mgr_attach_fence+0x2d3/0x4c0 [amdgpu]
       amdgpu_evf_mgr_rearm+0x279/0x370 [amdgpu]
       amdgpu_userq_vm_validate_and_restore_queue+0x166c/0x1dc0 [amdgpu]
       amdgpu_userq_restore_worker+0x6d/0x180 [amdgpu]

Reserve one slot before attaching the eviction fence.

Fixes: 2cd7284ba54b ("drm/amdgpu: completely rework eviction fence handling v2")
Signed-off-by: Jesse Zhang <Jesse.Zhang@amd.com>
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_eviction_fence.c | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)

diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_eviction_fence.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_eviction_fence.c
index f8652fd0525d..f01e08c4de4a 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_eviction_fence.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_eviction_fence.c
@@ -108,13 +108,21 @@ int amdgpu_evf_mgr_attach_fence(struct amdgpu_eviction_fence_mgr *evf_mgr,
 
 		amdgpu_bo_placement_from_domain(bo, bo->allowed_domains);
 		ret = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
-		if (!ret)
-			dma_resv_add_fence(resv, ev_fence,
-					   DMA_RESV_USAGE_BOOKKEEP);
+		if (ret)
+			goto out;
+
+		/* BOs prelocked by drm_exec may not have reserved fence slots. */
+		dma_resv_reserve_fences(resv, 1);
+		if (ret)
+			goto out;
+
+		dma_resv_add_fence(resv, ev_fence,
+				   DMA_RESV_USAGE_BOOKKEEP);
 	} else {
 		ret = 0;
 	}
 
+out:
 	dma_fence_put(ev_fence);
 	return ret;
 }
-- 
2.50.1


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

* [PATCH 2/2] drm/amdgpu: don't unreserve the freed root PD in amdgpu_vm_init()
  2026-11-19  9:56 [PATCH 1/2] drm/amdgpu: reserve a fence slot before adding the eviction fence Jesse Zhang
@ 2026-11-19  9:56 ` Jesse Zhang
  2026-11-19 14:46   ` Christian König
  2026-11-19 10:13 ` [PATCH 1/2] drm/amdgpu: reserve a fence slot before adding the eviction fence sashiko-bot
  1 sibling, 1 reply; 6+ messages in thread
From: Jesse Zhang @ 2026-11-19  9:56 UTC (permalink / raw)
  To: amd-gfx; +Cc: Alexander.Deucher, Christian Koenig, Sunil Khatri, Jesse Zhang

The error path of amdgpu_vm_init() frees the root PD, which clears
vm->root.bo, and then unreserves vm->root.bo. On navi48, with an
allocation after reserving the root PD failing through failslab while
opening the render node:

  Oops: general protection fault, probably for non-canonical address 0xdffffc000000005e: 0000 [#1] SMP KASAN NOPTI
  KASAN: null-ptr-deref in range [0x00000000000002f0-0x00000000000002f7]
  RIP: 0010:amdgpu_vm_init+0xa9f/0x1480 [amdgpu]
   amdgpu_driver_open_kms+0x2b1/0xd10 [amdgpu]
   drm_file_alloc+0x58f/0xac0 [drm]
   drm_open_helper+0x1b3/0x4b0 [drm]

Unreserve it through root_bo, which still holds a reference. With this
and the entity fix 4842 opens with allocations failing in turn don't
oops anymore.

Fixes: 8473bfdcb5b1 ("drm/amdgpu: fix error handling in amdgpu_vm_init")
Signed-off-by: Jesse Zhang <Jesse.Zhang@amd.com>
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c
index 8cc5964f29c0..272eac0ef317 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c
@@ -2718,8 +2718,9 @@ int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 	return 0;
 
 error_free_root:
+	/* This clears vm->root.bo, root_bo keeps the BO alive */
 	amdgpu_vm_pt_free_root(adev, vm);
-	amdgpu_bo_unreserve(vm->root.bo);
+	amdgpu_bo_unreserve(root_bo);
 	amdgpu_bo_unref(&root_bo);
 
 error_free_delayed:
-- 
2.50.1


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

* Re: [PATCH 1/2] drm/amdgpu: reserve a fence slot before adding the eviction fence
  2026-11-19  9:56 [PATCH 1/2] drm/amdgpu: reserve a fence slot before adding the eviction fence Jesse Zhang
  2026-11-19  9:56 ` [PATCH 2/2] drm/amdgpu: don't unreserve the freed root PD in amdgpu_vm_init() Jesse Zhang
@ 2026-11-19 10:13 ` sashiko-bot
  1 sibling, 0 replies; 6+ messages in thread
From: sashiko-bot @ 2026-11-19 10:13 UTC (permalink / raw)
  To: Jesse Zhang, amd-gfx

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [High] drm/amdgpu: missing return value assignment in
  amdgpu_evf_mgr_attach_fence()
It also noticed 1 related pre-existing issue this patch does not
introduce:
- [High] drm/amdgpu: infinite loop in amdgpu_evf_mgr_rearm() [1]
--

commit 48f419fb19e05e772f024f2aeb82afb2c8d3447c
Author: Jesse Zhang <Jesse.Zhang@amd.com>

drm/amdgpu: reserve a fence slot before adding the eviction fence

This commit calls dma_resv_reserve_fences() in amdgpu_evf_mgr_attach_fence()
before attaching the eviction fence to a BO's reservation object.

> diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_eviction_fence.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_eviction_fence.c
> index f8652fd0525df..f01e08c4de4a2 100644
> --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_eviction_fence.c
> +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_eviction_fence.c
> @@ -108,13 +108,21 @@ int amdgpu_evf_mgr_attach_fence(struct amdgpu_eviction_fence_mgr *evf_mgr,
>  
>  		amdgpu_bo_placement_from_domain(bo, bo->allowed_domains);
>  		ret = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
> -		if (!ret)
> -			dma_resv_add_fence(resv, ev_fence,
> -					   DMA_RESV_USAGE_BOOKKEEP);
> +		if (ret)
> +			goto out;
> +
> +		/* BOs prelocked by drm_exec may not have reserved fence slots. */
> +		dma_resv_reserve_fences(resv, 1);
> +		if (ret)
> +			goto out;

[Severity: High]
Should the return value of dma_resv_reserve_fences() be assigned to ret?
Because ret is already 0 from ttm_bo_validate(), an allocation failure in
dma_resv_reserve_fences() is ignored and execution proceeds to
dma_resv_add_fence() without a reserved slot, triggering the BUG_ON() in
dma_resv_add_fence() or a NULL pointer dereference if resv->fences is NULL.

> +
> +		dma_resv_add_fence(resv, ev_fence,
> +				   DMA_RESV_USAGE_BOOKKEEP);
>  	} else {
>  		ret = 0;
>  	}
>  
> +out:
>  	dma_fence_put(ev_fence);
>  	return ret;
>  }

[Severity: High]
This problem wasn't introduced by this patch, but can dropping the old
evf_mgr->ev_fence reference before updating the pointer in
amdgpu_evf_mgr_rearm() [1] trap a concurrent RCU reader in an
infinite loop?

drivers/gpu/drm/amd/amdgpu/amdgpu_eviction_fence.c:amdgpu_evf_mgr_rearm() {
    ...
	/* Remember it for newly added BOs */
	dma_fence_put(evf_mgr->ev_fence);
	evf_mgr->ev_fence = &ev_fence->base;
    ...
}

When dma_fence_put() drops the old fence's refcount to zero while
evf_mgr->ev_fence still points to it, a concurrent reader calling
dma_fence_get_rcu_safe() sees a zero refcount and an unchanged pointer:

CPU0                                CPU1
amdgpu_evf_mgr_rearm()
  dma_fence_put(evf_mgr->ev_fence)
                                    amdgpu_evf_mgr_get_fence()
                                      dma_fence_get_rcu_safe()
                                        fence = rcu_dereference(...)
                                        dma_fence_get_rcu(fence) -> NULL
                                        /* pointer unchanged, retries */
                                        <-- spins until pointer updates
  evf_mgr->ev_fence = &ev_fence->base

Because dma_fence_get_rcu_safe() loops until it either acquires a reference
or observes the pointer change, a reader that preempts CPU0 after
dma_fence_put() will spin indefinitely and cause a CPU lockup.  Updating
evf_mgr->ev_fence via rcu_replace_pointer() before calling dma_fence_put()
on the old fence would avoid this window.

[1] https://sashiko.dev/#/bug/linux-6debe0c7-cdae-40ca-8882-4e7c8c922581

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261119095652.3510341-1-Jesse.Zhang@amd.com?part=1

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

* Re: [PATCH 2/2] drm/amdgpu: don't unreserve the freed root PD in amdgpu_vm_init()
  2026-11-19  9:56 ` [PATCH 2/2] drm/amdgpu: don't unreserve the freed root PD in amdgpu_vm_init() Jesse Zhang
@ 2026-11-19 14:46   ` Christian König
  2026-11-19 15:54     ` Alex Deucher
  0 siblings, 1 reply; 6+ messages in thread
From: Christian König @ 2026-11-19 14:46 UTC (permalink / raw)
  To: Jesse Zhang, amd-gfx; +Cc: Alexander.Deucher, Sunil Khatri



On 11/19/26 10:56, Jesse Zhang wrote:
> The error path of amdgpu_vm_init() frees the root PD, which clears
> vm->root.bo, and then unreserves vm->root.bo. On navi48, with an
> allocation after reserving the root PD failing through failslab while
> opening the render node:
> 
>   Oops: general protection fault, probably for non-canonical address 0xdffffc000000005e: 0000 [#1] SMP KASAN NOPTI
>   KASAN: null-ptr-deref in range [0x00000000000002f0-0x00000000000002f7]
>   RIP: 0010:amdgpu_vm_init+0xa9f/0x1480 [amdgpu]
>    amdgpu_driver_open_kms+0x2b1/0xd10 [amdgpu]
>    drm_file_alloc+0x58f/0xac0 [drm]
>    drm_open_helper+0x1b3/0x4b0 [drm]
> 
> Unreserve it through root_bo, which still holds a reference. With this
> and the entity fix 4842 opens with allocations failing in turn don't
> oops anymore.
> 
> Fixes: 8473bfdcb5b1 ("drm/amdgpu: fix error handling in amdgpu_vm_init")
> Signed-off-by: Jesse Zhang <Jesse.Zhang@amd.com>

That is already fixed with:

commit 26401a4999f1596a515b2e704e7066ac09306488
Author: Christian König <ckoenig.leichtzumerken@gmail.com>
Date:   Wed Oct 7 14:46:57 2026 +0200

    drm/exec: don't immediately add prelocked obj to array of locked objs
    

Regards,
Christian

> ---
>  drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c
> index 8cc5964f29c0..272eac0ef317 100644
> --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c
> +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c
> @@ -2718,8 +2718,9 @@ int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm,
>  	return 0;
>  
>  error_free_root:
> +	/* This clears vm->root.bo, root_bo keeps the BO alive */
>  	amdgpu_vm_pt_free_root(adev, vm);
> -	amdgpu_bo_unreserve(vm->root.bo);
> +	amdgpu_bo_unreserve(root_bo);
>  	amdgpu_bo_unref(&root_bo);
>  
>  error_free_delayed:


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

* Re: [PATCH 2/2] drm/amdgpu: don't unreserve the freed root PD in amdgpu_vm_init()
  2026-11-19 14:46   ` Christian König
@ 2026-11-19 15:54     ` Alex Deucher
  2026-11-19 23:43       ` Zhang, Jesse(Jie)
  0 siblings, 1 reply; 6+ messages in thread
From: Alex Deucher @ 2026-11-19 15:54 UTC (permalink / raw)
  To: Christian König
  Cc: Jesse Zhang, amd-gfx, Alexander.Deucher, Sunil Khatri

On Thu, Nov 19, 2026 at 9:56 AM Christian König
<christian.koenig@amd.com> wrote:
>
>
>
> On 11/19/26 10:56, Jesse Zhang wrote:
> > The error path of amdgpu_vm_init() frees the root PD, which clears
> > vm->root.bo, and then unreserves vm->root.bo. On navi48, with an
> > allocation after reserving the root PD failing through failslab while
> > opening the render node:
> >
> >   Oops: general protection fault, probably for non-canonical address 0xdffffc000000005e: 0000 [#1] SMP KASAN NOPTI
> >   KASAN: null-ptr-deref in range [0x00000000000002f0-0x00000000000002f7]
> >   RIP: 0010:amdgpu_vm_init+0xa9f/0x1480 [amdgpu]
> >    amdgpu_driver_open_kms+0x2b1/0xd10 [amdgpu]
> >    drm_file_alloc+0x58f/0xac0 [drm]
> >    drm_open_helper+0x1b3/0x4b0 [drm]
> >
> > Unreserve it through root_bo, which still holds a reference. With this
> > and the entity fix 4842 opens with allocations failing in turn don't
> > oops anymore.
> >
> > Fixes: 8473bfdcb5b1 ("drm/amdgpu: fix error handling in amdgpu_vm_init")
> > Signed-off-by: Jesse Zhang <Jesse.Zhang@amd.com>
>
> That is already fixed with:
>
> commit 26401a4999f1596a515b2e704e7066ac09306488
> Author: Christian König <ckoenig.leichtzumerken@gmail.com>
> Date:   Wed Oct 7 14:46:57 2026 +0200
>
>     drm/exec: don't immediately add prelocked obj to array of locked objs
>

If this is missing from ASDN, feel free to cherry-pick.

Alex

>
> Regards,
> Christian
>
> > ---
> >  drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 3 ++-
> >  1 file changed, 2 insertions(+), 1 deletion(-)
> >
> > diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c
> > index 8cc5964f29c0..272eac0ef317 100644
> > --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c
> > +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c
> > @@ -2718,8 +2718,9 @@ int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm,
> >       return 0;
> >
> >  error_free_root:
> > +     /* This clears vm->root.bo, root_bo keeps the BO alive */
> >       amdgpu_vm_pt_free_root(adev, vm);
> > -     amdgpu_bo_unreserve(vm->root.bo);
> > +     amdgpu_bo_unreserve(root_bo);
> >       amdgpu_bo_unref(&root_bo);
> >
> >  error_free_delayed:
>

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

* RE: [PATCH 2/2] drm/amdgpu: don't unreserve the freed root PD in amdgpu_vm_init()
  2026-11-19 15:54     ` Alex Deucher
@ 2026-11-19 23:43       ` Zhang, Jesse(Jie)
  0 siblings, 0 replies; 6+ messages in thread
From: Zhang, Jesse(Jie) @ 2026-11-19 23:43 UTC (permalink / raw)
  To: Alex Deucher, Koenig, Christian
  Cc: amd-gfx@lists.freedesktop.org, Deucher,  Alexander, Khatri, Sunil

AMD General

> -----Original Message-----
> From: Alex Deucher <alexdeucher@gmail.com>
> Sent: Thursday, November 19, 2026 11:54 PM
> To: Koenig, Christian <Christian.Koenig@amd.com>
> Cc: Zhang, Jesse(Jie) <Jesse.Zhang@amd.com>; amd-
> gfx@lists.freedesktop.org; Deucher, Alexander
> <Alexander.Deucher@amd.com>; Khatri, Sunil <Sunil.Khatri@amd.com>
> Subject: Re: [PATCH 2/2] drm/amdgpu: don't unreserve the freed root PD in
> amdgpu_vm_init()
>
> On Thu, Nov 19, 2026 at 9:56 AM Christian König
> <christian.koenig@amd.com> wrote:
> >
> >
> >
> > On 11/19/26 10:56, Jesse Zhang wrote:
> > > The error path of amdgpu_vm_init() frees the root PD, which clears
> > > vm->root.bo, and then unreserves vm->root.bo. On navi48, with an
> > > allocation after reserving the root PD failing through failslab
> > > while opening the render node:
> > >
> > >   Oops: general protection fault, probably for non-canonical address
> 0xdffffc000000005e: 0000 [#1] SMP KASAN NOPTI
> > >   KASAN: null-ptr-deref in range [0x00000000000002f0-
> 0x00000000000002f7]
> > >   RIP: 0010:amdgpu_vm_init+0xa9f/0x1480 [amdgpu]
> > >    amdgpu_driver_open_kms+0x2b1/0xd10 [amdgpu]
> > >    drm_file_alloc+0x58f/0xac0 [drm]
> > >    drm_open_helper+0x1b3/0x4b0 [drm]
> > >
> > > Unreserve it through root_bo, which still holds a reference. With
> > > this and the entity fix 4842 opens with allocations failing in turn
> > > don't oops anymore.
> > >
> > > Fixes: 8473bfdcb5b1 ("drm/amdgpu: fix error handling in
> > > amdgpu_vm_init")
> > > Signed-off-by: Jesse Zhang <Jesse.Zhang@amd.com>
> >
> > That is already fixed with:
> >
> > commit 26401a4999f1596a515b2e704e7066ac09306488
> > Author: Christian König <ckoenig.leichtzumerken@gmail.com>
> > Date:   Wed Oct 7 14:46:57 2026 +0200
> >
> >     drm/exec: don't immediately add prelocked obj to array of locked
> > objs
> >
>
> If this is missing from ASDN, feel free to cherry-pick.

Hi Christian, Alex,

Thanks. As far as I can see, 26401a4999f1 ("drm/exec: don't immediately
add prelocked obj to array of locked objs") only changes drm_exec and
doesn't touch the amdgpu_vm_init() error path.

The same fix as this patch landed in amd-staging-drm-next yesterday:

  768e106833a9 ("drm/amdgpu: fix NULL deref in error path")

from Pierre-Eric, so please drop this patch. I'll cherry-pick
768e106833a9 to amd-unified-interface,

Thanks
Jesse
>
> Alex
>
> >
> > Regards,
> > Christian
> >
> > > ---
> > >  drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 3 ++-
> > >  1 file changed, 2 insertions(+), 1 deletion(-)
> > >
> > > diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c
> > > b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c
> > > index 8cc5964f29c0..272eac0ef317 100644
> > > --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c
> > > +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c
> > > @@ -2718,8 +2718,9 @@ int amdgpu_vm_init(struct amdgpu_device
> *adev, struct amdgpu_vm *vm,
> > >       return 0;
> > >
> > >  error_free_root:
> > > +     /* This clears vm->root.bo, root_bo keeps the BO alive */
> > >       amdgpu_vm_pt_free_root(adev, vm);
> > > -     amdgpu_bo_unreserve(vm->root.bo);
> > > +     amdgpu_bo_unreserve(root_bo);
> > >       amdgpu_bo_unref(&root_bo);
> > >
> > >  error_free_delayed:
> >

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

end of thread, other threads:[~2026-11-19 23:45 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-19  9:56 [PATCH 1/2] drm/amdgpu: reserve a fence slot before adding the eviction fence Jesse Zhang
2026-11-19  9:56 ` [PATCH 2/2] drm/amdgpu: don't unreserve the freed root PD in amdgpu_vm_init() Jesse Zhang
2026-11-19 14:46   ` Christian König
2026-11-19 15:54     ` Alex Deucher
2026-11-19 23:43       ` Zhang, Jesse(Jie)
2026-11-19 10:13 ` [PATCH 1/2] drm/amdgpu: reserve a fence slot before adding the eviction fence sashiko-bot

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