Skip to contentSkip to conversation

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

PATCH In review Jesse Zhang 4 participants 7 messages Nov 19 – Nov 20

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

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

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")
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_eviction_fence.c | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_eviction_fence.c +11 -3
index f8652fd0525d..f01e08c4de4a 100644
@@ -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;
 }
Signature · 1 line
2.50.1
#1 lore raw

PATCH 2/2 drm/amdgpu: don't unreserve the freed root PD in amdgpu_vm_init()

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")
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +2 -1
index 8cc5964f29c0..272eac0ef317 100644
@@ -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:
Signature · 1 line
2.50.1
maintainer
#2 lore raw

Re: [PATCH 2/2] drm/amdgpu: don't unreserve the freed root PD in amdgpu_vm_init()

Quoted 12 lines · 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]
Quoted 6 lines

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")

That is already fixed with:

commit 26401a4999f1596a515b2e704e7066ac09306488

Author: Christian König <[email protected]>
Date:   Wed Oct 7 14:46:57 2026 +0200

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

Regards,
Christian

Quoted 19 lines
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +2 -1
index 8cc5964f29c0..272eac0ef317 100644
@@ -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:
#3 lore raw

Re: [PATCH 2/2] drm/amdgpu: don't unreserve the freed root PD in amdgpu_vm_init()

Quoted 23 lines · Christian König
Quoted 12 lines · 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]
Quoted 6 lines

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")
Quoted 8 lines

That is already fixed with:

commit 26401a4999f1596a515b2e704e7066ac09306488

Author: Christian König <[email protected]>
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

Quoted 24 lines

Regards,
Christian

Quoted 19 lines
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +2 -1
index 8cc5964f29c0..272eac0ef317 100644
@@ -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:
#4 lore raw

RE: [PATCH 2/2] drm/amdgpu: don't unreserve the freed root PD in amdgpu_vm_init()

AMD General

Quoted 44 lines

-----Original Message-----
From: Alex Deucher <[email protected]>
Sent: Thursday, November 19, 2026 11:54 PM
To: Koenig, Christian <[email protected]>
Cc: Zhang, Jesse(Jie) <[email protected]>; amd- [email protected]; Deucher, Alexander <[email protected]>; Khatri, Sunil <[email protected]>
Subject: Re: [PATCH 2/2] drm/amdgpu: don't unreserve the freed root PD in amdgpu_vm_init()

Quoted 10 lines · Christian König
Quoted 6 lines · 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

Quoted 1 line
Quoted 1 line
  KASAN: null-ptr-deref in range [0x00000000000002f0-

0x00000000000002f7]

Quoted 19 lines
Quoted 5 lines
  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]
Quoted 7 lines

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")

That is already fixed with:

commit 26401a4999f1596a515b2e704e7066ac09306488

Author: Christian König <[email protected]>
Date:   Wed Oct 7 14:46:57 2026 +0200
Quoted 5 lines
Quoted 3 lines

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

Quoted 29 lines

Alex

Quoted 8 lines

Regards,
Christian

Quoted 4 lines
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
Quoted 6 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c int amdgpu_vm_init(struct amdgpu_device

Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +0 -0
index 8cc5964f29c0..272eac0ef317 100644
@@ -2718,8 +2718,9 @@ int amdgpu_vm_init(struct amdgpu_device

*adev, struct amdgpu_vm *vm,

Quoted 11 lines
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +2 -1
      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:
maintainer
#5 lore raw

Re: [PATCH 2/2] drm/amdgpu: don't unreserve the freed root PD in amdgpu_vm_init()

Quoted 50 lines · Jesse(Jie)

AMD General

Quoted 48 lines

-----Original Message-----
From: Alex Deucher <[email protected]>
Sent: Thursday, November 19, 2026 11:54 PM
To: Koenig, Christian <[email protected]>
Cc: Zhang, Jesse(Jie) <[email protected]>; amd- [email protected]; Deucher, Alexander <[email protected]>; Khatri, Sunil <[email protected]>
Subject: Re: [PATCH 2/2] drm/amdgpu: don't unreserve the freed root PD in amdgpu_vm_init()

Quoted 10 lines · Christian König
Quoted 6 lines · 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

Quoted 1 line
Quoted 1 line
  KASAN: null-ptr-deref in range [0x00000000000002f0-

0x00000000000002f7]

Quoted 22 lines
Quoted 5 lines
  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]
Quoted 7 lines

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")

That is already fixed with:

commit 26401a4999f1596a515b2e704e7066ac09306488

Author: Christian König <[email protected]>
Date:   Wed Oct 7 14:46:57 2026 +0200

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

Quoted 7 lines
Quoted 1 line

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.

Sorry that was my fault I replied to the wrong mail.

The patch 26401a4999f1 ("drm/exec: don't immediately add prelocked obj to array of locked objs") fixes the issue patch #1 in this series tries to address and not patch #2.

So yeah patch #2 is perfectly correct, but we should just cherry pick the drm/exec fix into amd-staging-drm-next, going to take a look at that today.

Thanks,
Christian.

Quoted 39 lines

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

Quoted 29 lines

Alex

Quoted 8 lines

Regards,
Christian

Quoted 4 lines
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
Quoted 6 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c int amdgpu_vm_init(struct amdgpu_device

Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +0 -0
index 8cc5964f29c0..272eac0ef317 100644
@@ -2718,8 +2718,9 @@ int amdgpu_vm_init(struct amdgpu_device

*adev, struct amdgpu_vm *vm,

Quoted 11 lines
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +2 -1
      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:
#6 lore raw
In reply to Jesse Zhang
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…

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 <[email protected]>

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.

Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_eviction_fence.c +0 -0
index f8652fd0525df..f01e08c4de4a2 100644
@@ -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);

drivers/gpu/drm/amd/amdgpu/amdgpu_eviction_fence.c int amdgpu_evf_mgr_attach_fence(struct amdgpu_eviction_fence_mgr *evf_mgr, · line 117

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_eviction_fence.c +7 -3
-		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.

drivers/gpu/drm/amd/amdgpu/amdgpu_eviction_fence.c int amdgpu_evf_mgr_attach_fence(struct amdgpu_eviction_fence_mgr *evf_mgr, · line 128

Quoted 11 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_eviction_fence.c +4 -0
+
+		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

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1