Skip to contentSkip to conversation

drm/amdgpu: keep PTL disable requests across GPU reset and resume

PATCH In review Perry Yuan 3 participants 5 messages Dec 1

Subsystems (5)
lore
Share & tools
lore

Perry Yuan

author
#0 lore raw

PATCH 1/3 drm/amdgpu: keep PTL disable requests across GPU reset and resume

gfx_v9_4_3_perf_monitor_ptl_init() sets ptl->disable_ref to 0 every time it runs. Besides the first init, that is late_init after a GPU reset and on resume, and hw_fini on suspend. The KFD processes holding a PTL disable request survive all of these, and so do their pdd->ptl_disable_req and the AMDGPU_PTL_DISABLE_PROFILER bit.

If a process holds the profiler lock across a GPU reset, its release afterwards, by unlock, exit or kill, takes disable_ref from 0 to -1. The 1 -> 0 transition that clears the PROFILER bit and enables PTL never happens, neither for this process nor for later profilers, which move the counter between -1 and 0. A write to ptl_enable is blocked by the PROFILER bit. PTL stays disabled until amdgpu is reloaded.

disable_ref starts at 0 in the zeroed amdgpu_device and is balanced by the request/release pairs, so there is nothing to reset. Drop the reset so that the counter keeps matching the requests that are held.

Fixes dd61e27535a6 ("drm/amdkfd: Add PTL control IOCTL Option and unify refcount logic")
---
 drivers/gpu/drm/amd/amdgpu/gfx_v9_4_3.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/gfx_v9_4_3.c +5 -1
index e140430ce059..78ce980ac319 100644
@@ -2463,7 +2463,11 @@ static int gfx_v9_4_3_perf_monitor_ptl_init(struct amdgpu_device *adev, bool ena
 
 	ptl->hw_supported_state = AMDGPU_PTL_HW_SUPPORTED;
 
-	atomic_set(&ptl->disable_ref, 0);
+	/*
+	 * disable_ref counts the KFD processes holding a PTL disable request.
+	 * They survive GPU reset and suspend/resume, so it must not be reset
+	 * here.
+	 */
 	if (!enable && !amdgpu_in_reset(adev) && !adev->in_suspend) {
 		dev_dbg(adev->dev,
 			"PTL disabled (amdgpu.ptl=%d)\
Signature · 1 line
2.34.1
author
#1 lore raw

PATCH 2/3 drm/amdkfd: Fix leaked PTL disable request on process exit

When the last profiler releases its PTL disable request, KFD tries to enable PTL. If that fails, KFD restores the request so the owner can retry. At process teardown, however, kfd_process_destroy_pdds() frees the owner immediately after the release. The restored request then has no owner. Subsequent profiler sessions never drop the reference count to zero, and the profiler bit blocks attempts to enable PTL via sysfs.

On the final release, report the enable error but do not restore the request. The next profiler release or sysfs enable can then retry the hardware operation. Keep the existing rollback for explicit releases.

Fixes dd61e27535a6 ("drm/amdkfd: Add PTL control IOCTL Option and unify refcount logic")
---
 drivers/gpu/drm/amd/amdkfd/kfd_chardev.c | 16 +++++++++-------
 drivers/gpu/drm/amd/amdkfd/kfd_priv.h    |  3 +--
 drivers/gpu/drm/amd/amdkfd/kfd_process.c |  2 +-
 3 files changed, 11 insertions(+), 10 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +9 -7
index ae5b430a0031..43cb0af5e527 100644
@@ -1882,8 +1882,7 @@ int kfd_ptl_disable_request(struct kfd_process_device *pdd,
 	return ret;
 }
 
-int kfd_ptl_disable_release(struct kfd_process_device *pdd,
-		struct kfd_process *p)
+int kfd_ptl_disable_release(struct kfd_process_device *pdd, bool final)
 {
 	struct amdgpu_device *adev = pdd->dev->adev;
 	struct amdgpu_ptl *ptl = &adev->psp.ptl;
@@ -1901,10 +1900,13 @@ int kfd_ptl_disable_release(struct kfd_process_device *pdd,
 		clear_bit(AMDGPU_PTL_DISABLE_PROFILER, ptl->disable_bitmap);
 		ret = kfd_ptl_control(pdd, true);
 		if (ret) {
-			atomic_inc(&ptl->disable_ref);
-			set_bit(AMDGPU_PTL_DISABLE_PROFILER, ptl->disable_bitmap);
 			dev_warn(adev->dev, "Failed to enable PTL on release: %d\n", ret);
-			goto out;
+			/* Only a live pdd can retry the release. */
+			if (!final) {
+				atomic_inc(&ptl->disable_ref);
+				set_bit(AMDGPU_PTL_DISABLE_PROFILER, ptl->disable_bitmap);
+				goto out;
+			}
 		}
 	}
 	pdd->ptl_disable_req = false;
@@ -1930,7 +1932,7 @@ static int kfd_profiler_ptl_control(struct kfd_process *p,
 	if (args->enable == 0)
 		ret = kfd_ptl_disable_request(pdd, p);
 	else
-		ret = kfd_ptl_disable_release(pdd, p);
+		ret = kfd_ptl_disable_release(pdd, false);
 
 	return ret;
 }
@@ -3444,7 +3446,7 @@ static inline int profile_lock_device(struct kfd_process *p,
 		up_read(&kfd->adev->reset_domain->sem);
 
 		if (ptl->hw_supported_state == AMDGPU_PTL_HW_SUPPORTED) {
-			status = kfd_ptl_disable_release(pdd, p);
+			status = kfd_ptl_disable_release(pdd, false);
 			if (status)
 				dev_err(kfd_device,
 						"Failed to unlock device %d for profiling, error %d\n",
drivers/gpu/drm/amd/amdkfd/kfd_priv.h +1 -2
index 8eac086aa108..4950a8b9d162 100644
@@ -1661,8 +1661,7 @@ static inline bool kfd_is_first_node(struct kfd_node *node)
 /* PTL support */
 int kfd_ptl_disable_request(struct kfd_process_device *pdd,
 		struct kfd_process *p);
-int kfd_ptl_disable_release(struct kfd_process_device *pdd,
-		struct kfd_process *p);
+int kfd_ptl_disable_release(struct kfd_process_device *pdd, bool final);
 
 /* Debugfs */
 #if defined(CONFIG_DEBUG_FS)
drivers/gpu/drm/amd/amdkfd/kfd_process.c +1 -1
index 5dc657f32d38..a5ec822077b6 100644
@@ -1258,7 +1258,7 @@ static void kfd_process_destroy_pdds(struct kfd_process *p)
 		kfd_process_profiler_release(p, pdd);
 
 		if (pdd->ptl_disable_req)
-			kfd_ptl_disable_release(pdd, p);
+			kfd_ptl_disable_release(pdd, true);
 
 		kfd_process_device_destroy_cwsr_dgpu(pdd);
 		kfd_process_device_destroy_ib_mem(pdd);
Signature · 1 line
2.34.1
author
#2 lore raw

PATCH 3/3 drm/ttm: Narrow the eviction lock scope when adding move fences

In reply to Perry Yuan

The resource manager's eviction_lock protects its eviction_fences array. When attaching those fences to a BO, TTM also holds eviction_lock while dma_resv_add_fence() scans the BO's reservation list for an entry to replace. That list is protected by the reservation lock, not by the manager lock.

Take references to pending fences under eviction_lock and add them to the reservation object after dropping it. This keeps the list scan out of the critical section shared with other allocators using the same manager. Skip completed fences, which are no longer dependencies, and keep returning -EBUSY for a pending fence in the no-wait path.

---
 drivers/gpu/drm/ttm/ttm_bo.c | 25 +++++++++++++++----------
 1 file changed, 15 insertions(+), 10 deletions(-)
drivers/gpu/drm/ttm/ttm_bo.c +15 -10
index ef56c18ded1b..45adcd2678a3 100644
@@ -909,33 +909,38 @@ void ttm_bo_unpin(struct ttm_buffer_object *bo)
 EXPORT_SYMBOL(ttm_bo_unpin);
 
 /*
- * Add the pipelined eviction fencesto the BO as kernel dependency and reserve new
- * fence slots.
+ * Add pending eviction fences to the BO as kernel dependencies and reserve
+ * new fence slots.
  */
 static int ttm_bo_add_pipelined_eviction_fences(struct ttm_buffer_object *bo,
 						struct ttm_resource_manager *man,
 						bool no_wait_gpu)
 {
+	struct dma_fence *fences[TTM_NUM_MOVE_FENCES];
 	struct dma_fence *fence;
-	int i;
+	int i, count = 0;
 
 	spin_lock(&man->eviction_lock);
 	for (i = 0; i < TTM_NUM_MOVE_FENCES; i++) {
 		fence = man->eviction_fences[i];
-		if (!fence)
+		if (!fence || dma_fence_is_signaled(fence))
 			continue;
 
 		if (no_wait_gpu) {
-			if (!dma_fence_is_signaled(fence)) {
-				spin_unlock(&man->eviction_lock);
-				return -EBUSY;
-			}
-		} else {
-			dma_resv_add_fence(bo->base.resv, fence, DMA_RESV_USAGE_KERNEL);
+			spin_unlock(&man->eviction_lock);
+			return -EBUSY;
 		}
+
+		/* The manager may replace this fence once we drop the lock. */
+		fences[count++] = dma_fence_get(fence);
 	}
 	spin_unlock(&man->eviction_lock);
 
+	for (i = 0; i < count; i++) {
+		dma_resv_add_fence(bo->base.resv, fences[i], DMA_RESV_USAGE_KERNEL);
+		dma_fence_put(fences[i]);
+	}
+
 	/* TODO: this call should be removed. */
 	return dma_resv_reserve_fences(bo->base.resv, 1);
 }
Signature · 1 line
2.34.1
#3 lore raw

Re: [PATCH 3/3] drm/ttm: Narrow the eviction lock scope when adding move fences

Thank you for your contribution! Sashiko AI review found no new issues in this patch, but noticed 1 related pre-existing issue it does not introduce: - [High] drm/amdgpu: insufficient fence slot…

Thank you for your contribution! Sashiko AI review found no new issues in this patch, but noticed 1 related pre-existing issue it does not introduce:
- [High] drm/amdgpu: insufficient fence slot reservation in amdgpu_dma_buf_move_notify() [1]
--

commit d22882cc6158883831770886db4d0852012b8d1a
Author: Perry Yuan <[email protected]>

drm/ttm: Narrow the eviction lock scope when adding move fences

This commit snapshots pending eviction fences under man->eviction_lock and adds them to the BO's reservation object after releasing the lock.

Quoted 43 lines
drivers/gpu/drm/ttm/ttm_bo.c +10 -10
index 53c68e29ebc21..f833982073c6a 100644
@@ -908,33 +908,38 @@ void ttm_bo_unpin(struct ttm_buffer_object *bo)
 EXPORT_SYMBOL(ttm_bo_unpin);
 
 /*
- * Add the pipelined eviction fencesto the BO as kernel dependency and reserve new
- * fence slots.
+ * Add pending eviction fences to the BO as kernel dependencies and reserve
+ * new fence slots.
  */
 static int ttm_bo_add_pipelined_eviction_fences(struct ttm_buffer_object *bo,
 						struct ttm_resource_manager *man,
 						bool no_wait_gpu)
 {
+	struct dma_fence *fences[TTM_NUM_MOVE_FENCES];
 	struct dma_fence *fence;
-	int i;
+	int i, count = 0;
 
 	spin_lock(&man->eviction_lock);
 	for (i = 0; i < TTM_NUM_MOVE_FENCES; i++) {
 		fence = man->eviction_fences[i];
-		if (!fence)
+		if (!fence || dma_fence_is_signaled(fence))
 			continue;
 
 		if (no_wait_gpu) {
-			if (!dma_fence_is_signaled(fence)) {
-				spin_unlock(&man->eviction_lock);
-				return -EBUSY;
-			}
-		} else {
-			dma_resv_add_fence(bo->base.resv, fence, DMA_RESV_USAGE_KERNEL);
+			spin_unlock(&man->eviction_lock);
+			return -EBUSY;
 		}
+
+		/* The manager may replace this fence once we drop the lock. */
+		fences[count++] = dma_fence_get(fence);
 	}

drivers/gpu/drm/ttm/ttm_bo.c void ttm_bo_unpin(struct ttm_buffer_object *bo) · line 945

Quoted 10 lines
drivers/gpu/drm/ttm/ttm_bo.c +5 -0
 	spin_unlock(&man->eviction_lock);
 
+	for (i = 0; i < count; i++) {
+		dma_resv_add_fence(bo->base.resv, fences[i], DMA_RESV_USAGE_KERNEL);
+		dma_fence_put(fences[i]);
+	}
+
 	/* TODO: this call should be removed. */
 	return dma_resv_reserve_fences(bo->base.resv, 1);
 }

[Severity: High]
This problem wasn't introduced by this patch, but can reserving only 1 fence slot here after consuming up to TTM_NUM_MOVE_FENCES slots hit the BUG_ON() in dma_resv_add_fence() during amdgpu_dma_buf_move_notify() [1]?

amdgpu_dma_buf_move_notify() reserves 2 fence slots on the VM root BO reservation object for the page table update fence and the TLB flush fence (when vm->need_tlb_fence is enabled). If amdgpu_vm_update_range() allocates a new page table BO sharing that reservation object, ttm_bo_alloc_resource() reserves TTM_NUM_MOVE_FENCES slots and ttm_bo_add_pipelined_eviction_fences() can consume all TTM_NUM_MOVE_FENCES of them before calling dma_resv_reserve_fences(bo->base.resv, 1).

Because dma_resv_reserve_fences() is not additive, if the reservation object started with TTM_NUM_MOVE_FENCES + 1 free slots, neither reservation call reallocates the list and only 1 free slot remains after BO creation. amdgpu_vm_sdma_commit() takes that slot for vm->delayed, and amdgpu_vm_tlb_flush() then overflows fobj->max_fences when adding vm->last_tlb_inv.

[1] https://sashiko.dev/#/bug/linux-8e6fd858-557e-4ca0-9558-be9bf089c7d4

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

Re: [PATCH 3/3] drm/ttm: Narrow the eviction lock scope when adding move fences

In reply to Perry Yuan

Hi Perry,

first of all TTM patches needs to be send to Natalie, Arun and the dri-devel mailing list.

I'm also adding Pierre-Eric since we came up with the TTM_NUM_MOVE_FENCES together.

Quoted 34 lines · Perry Yuan

The resource manager's eviction_lock protects its eviction_fences array. When attaching those fences to a BO, TTM also holds eviction_lock while dma_resv_add_fence() scans the BO's reservation list for an entry to replace. That list is protected by the reservation lock, not by the manager lock.

Take references to pending fences under eviction_lock and add them to the reservation object after dropping it. This keeps the list scan out of the critical section shared with other allocators using the same manager. Skip completed fences, which are no longer dependencies, and keep returning -EBUSY for a pending fence in the no-wait path.

---
 drivers/gpu/drm/ttm/ttm_bo.c | 25 +++++++++++++++----------
 1 file changed, 15 insertions(+), 10 deletions(-)
drivers/gpu/drm/ttm/ttm_bo.c +2 -2
index ef56c18ded1b..45adcd2678a3 100644
@@ -909,33 +909,38 @@ void ttm_bo_unpin(struct ttm_buffer_object *bo)
 EXPORT_SYMBOL(ttm_bo_unpin);
 
 /*
- * Add the pipelined eviction fencesto the BO as kernel dependency and reserve new
- * fence slots.
+ * Add pending eviction fences to the BO as kernel dependencies and reserve
+ * new fence slots.
  */
 static int ttm_bo_add_pipelined_eviction_fences(struct ttm_buffer_object *bo,
 						struct ttm_resource_manager *man,
 						bool no_wait_gpu)
 {

drivers/gpu/drm/ttm/ttm_bo.c void ttm_bo_unpin(struct ttm_buffer_object *bo) · line 926

Quoted 10 lines
drivers/gpu/drm/ttm/ttm_bo.c +3 -2
+	struct dma_fence *fences[TTM_NUM_MOVE_FENCES];
 	struct dma_fence *fence;
-	int i;
+	int i, count = 0;
 
 	spin_lock(&man->eviction_lock);
 	for (i = 0; i < TTM_NUM_MOVE_FENCES; i++) {
 		fence = man->eviction_fences[i];
-		if (!fence)
+		if (!fence || dma_fence_is_signaled(fence))

I think that should be dropped, skipping adding signaled fences doesn't make things much faster but can hide bugs.

Apart from that looks like a valid improvement to me, but did that really show up in some benchmark? That sounds quite unlikely for real world use cases.

Regards,
Christian.

Quoted 26 lines
drivers/gpu/drm/ttm/ttm_bo.c +10 -6
 			continue;
 
 		if (no_wait_gpu) {
-			if (!dma_fence_is_signaled(fence)) {
-				spin_unlock(&man->eviction_lock);
-				return -EBUSY;
-			}
-		} else {
-			dma_resv_add_fence(bo->base.resv, fence, DMA_RESV_USAGE_KERNEL);
+			spin_unlock(&man->eviction_lock);
+			return -EBUSY;
 		}
+
+		/* The manager may replace this fence once we drop the lock. */
+		fences[count++] = dma_fence_get(fence);
 	}
 	spin_unlock(&man->eviction_lock);
 
+	for (i = 0; i < count; i++) {
+		dma_resv_add_fence(bo->base.resv, fences[i], DMA_RESV_USAGE_KERNEL);
+		dma_fence_put(fences[i]);
+	}
+
 	/* TODO: this call should be removed. */
 	return dma_resv_reserve_fences(bo->base.resv, 1);
 }