Skip to contentSkip to conversation

drm/amd/amdgpu: remove unnecessary flush when enable gart

PATCH In review Yunxiang Li 3 participants 13 messages May 22, 2024 – May 24, 2024

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

PATCH 1/4 drm/amd/amdgpu: remove unnecessary flush when enable gart

From: Likun Gao <[email protected]>

Remove hdp flush for gc v11/12 when enable gart. Remove flush tlb for gc v10/11/12 when enable gart.

---
 drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c | 3 ---
 drivers/gpu/drm/amd/amdgpu/gfx_v12_0.c | 3 ---
 drivers/gpu/drm/amd/amdgpu/gmc_v10_0.c | 3 ---
 drivers/gpu/drm/amd/amdgpu/gmc_v11_0.c | 3 ---
 drivers/gpu/drm/amd/amdgpu/gmc_v12_0.c | 4 ----
 5 files changed, 16 deletions(-)
drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c +0 -3
index e5882da8332a..f9932b063855 100644
@@ -4204,13 +4204,10 @@ static int gfx_v11_0_gfxhub_enable(struct amdgpu_device *adev)
 	if (r)
 		return r;
 
-	adev->hdp.funcs->flush_hdp(adev, NULL);
-
 	value = (amdgpu_vm_fault_stop == AMDGPU_VM_FAULT_STOP_ALWAYS) ?
 		false : true;
 
 	adev->gfxhub.funcs->set_fault_enable_default(adev, value);
-	amdgpu_gmc_flush_gpu_tlb(adev, 0, AMDGPU_GFXHUB(0), 0);
 
 	return 0;
 }
drivers/gpu/drm/amd/amdgpu/gfx_v12_0.c +0 -3
index 6419f98e32b6..88ca252251c7 100644
@@ -3186,13 +3186,10 @@ static int gfx_v12_0_gfxhub_enable(struct amdgpu_device *adev)
 	if (r)
 		return r;
 
-	adev->hdp.funcs->flush_hdp(adev, NULL);
-
 	value = (amdgpu_vm_fault_stop == AMDGPU_VM_FAULT_STOP_ALWAYS) ?
 		false : true;
 
 	adev->gfxhub.funcs->set_fault_enable_default(adev, value);
-	amdgpu_gmc_flush_gpu_tlb(adev, 0, AMDGPU_GFXHUB(0), 0);
 
 	return 0;
 }
drivers/gpu/drm/amd/amdgpu/gmc_v10_0.c +0 -3
index d933e19e0cf5..3e0ebe25a80f 100644
@@ -974,9 +974,6 @@ static int gmc_v10_0_gart_enable(struct amdgpu_device *adev)
 	if (!adev->in_s0ix)
 		adev->gfxhub.funcs->set_fault_enable_default(adev, value);
 	adev->mmhub.funcs->set_fault_enable_default(adev, value);
-	gmc_v10_0_flush_gpu_tlb(adev, 0, AMDGPU_MMHUB0(0), 0);
-	if (!adev->in_s0ix)
-		gmc_v10_0_flush_gpu_tlb(adev, 0, AMDGPU_GFXHUB(0), 0);
 
 	DRM_INFO("PCIE GART of %uM enabled (table at 0x%016llX).\n",
 		 (unsigned int)(adev->gmc.gart_size >> 20),
drivers/gpu/drm/amd/amdgpu/gmc_v11_0.c +0 -3
index 527dc917e049..cadbe55f0c8f 100644
@@ -891,9 +891,6 @@ static int gmc_v11_0_gart_enable(struct amdgpu_device *adev)
 	if (r)
 		return r;
 
-	/* Flush HDP after it is initialized */
-	adev->hdp.funcs->flush_hdp(adev, NULL);
-
 	value = (amdgpu_vm_fault_stop == AMDGPU_VM_FAULT_STOP_ALWAYS) ?
 		false : true;
 
drivers/gpu/drm/amd/amdgpu/gmc_v12_0.c +0 -4
index e2c6ec3cc4f3..a677aca69a06 100644
@@ -861,14 +861,10 @@ static int gmc_v12_0_gart_enable(struct amdgpu_device *adev)
 	if (r)
 		return r;
 
-	/* Flush HDP after it is initialized */
-	adev->hdp.funcs->flush_hdp(adev, NULL);
-
 	value = (amdgpu_vm_fault_stop == AMDGPU_VM_FAULT_STOP_ALWAYS) ?
 		false : true;
 
 	adev->mmhub.funcs->set_fault_enable_default(adev, value);
-	gmc_v12_0_flush_gpu_tlb(adev, 0, AMDGPU_MMHUB0(0), 0);
 
 	dev_info(adev->dev, "PCIE GART of %uM enabled (table at 0x%016llX).\n",
 		 (unsigned)(adev->gmc.gart_size >> 20),
Signature · 1 line
2.34.1
#1 lore raw

PATCH 2/4 drm/amdgpu: use helper in amdgpu_gart_unbind

When amdgpu_gart_invalidate_tlb helper is introduced this part was left out of the conversion. Avoid the code duplication here.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_gart.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_gart.c +1 -4
index c623e23049d1..eb172388d99e 100644
@@ -325,10 +325,7 @@ void amdgpu_gart_unbind(struct amdgpu_device *adev, uint64_t offset,
 			page_base += AMDGPU_GPU_PAGE_SIZE;
 		}
 	}
-	mb();
-	amdgpu_device_flush_hdp(adev, NULL);
-	for_each_set_bit(i, adev->vmhubs_mask, AMDGPU_MAX_VMHUBS)
-		amdgpu_gmc_flush_gpu_tlb(adev, 0, i, 0);
+	amdgpu_gart_invalidate_tlb(adev);
 
 	drm_dev_exit(idx);
 }
Signature · 1 line
2.34.1
#2 lore raw

PATCH 3/4 drm/amdgpu: remove tlb flush in amdgpu_gtt_mgr_recover

In reply to Yunxiang Li

At this point the gart is not set up, there's no point to invalidate tlb here and it could even be harmful.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_gtt_mgr.c | 2 --
 1 file changed, 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_gtt_mgr.c +0 -2
index 44367f03316f..0760e70402ec 100644
@@ -200,8 +200,6 @@ void amdgpu_gtt_mgr_recover(struct amdgpu_gtt_mgr *mgr)
 		amdgpu_ttm_recover_gart(node->base.bo);
 	}
 	spin_unlock(&mgr->lock);
-
-	amdgpu_gart_invalidate_tlb(adev);
 }
 
 /**
Signature · 1 line
2.34.1
#3 lore raw

PATCH 4/4 drm/amdgpu: prevent gpu access during reset recovery

In reply to Yunxiang Li

Random accesses to the GPU while it is not re-initialized can lead to a bad time. So add a rwsem to prevent such accesses. Normal accesses will now take the read lock for shared GPU access, reset takes the write lock for exclusive GPU access.

Care need to be taken so that the recovery thread does not take the read lock and deadlock itself, and normal access should avoid waiting on the reset to finish and should instead treat the hardware access as failed.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu.h           |  5 ++
 drivers/gpu/drm/amd/amdgpu/amdgpu_device.c    | 22 ++++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c       | 74 ++++++++++---------
 drivers/gpu/drm/amd/amdgpu/amdgpu_reset.c     |  1 +
 drivers/gpu/drm/amd/amdgpu/amdgpu_reset.h     |  1 +
 drivers/gpu/drm/amd/amdgpu/mxgpu_ai.c         |  7 +-
 drivers/gpu/drm/amd/amdgpu/mxgpu_nv.c         |  7 +-
 .../drm/amd/amdkfd/kfd_device_queue_manager.c |  3 +-
 .../amd/amdkfd/kfd_process_queue_manager.c    |  8 +-
 9 files changed, 79 insertions(+), 49 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu.h +5 -0
index 1f71c7b98d77..5a089e2dec2a 100644
@@ -1632,6 +1632,11 @@ static inline bool amdgpu_is_tmz(struct amdgpu_device *adev)
 
 int amdgpu_in_reset(struct amdgpu_device *adev);
 
+void amdgpu_lock_hw_access(struct amdgpu_device *adev);
+void amdgpu_unlock_hw_access(struct amdgpu_device *adev);
+int amdgpu_begin_hw_access(struct amdgpu_device *adev);
+void amdgpu_end_hw_access(struct amdgpu_device *adev);
+
 extern const struct attribute_group amdgpu_vram_mgr_attr_group;
 extern const struct attribute_group amdgpu_gtt_mgr_attr_group;
 extern const struct attribute_group amdgpu_flash_attr_group;
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +22 -0
index e74789691070..057d735c7cae 100644
@@ -5816,6 +5816,7 @@ int amdgpu_device_gpu_recover(struct amdgpu_device *adev,
 		goto skip_hw_reset;
 	}
 
+	amdgpu_lock_hw_access(adev);
 retry:	/* Rest of adevs pre asic reset from XGMI hive. */
 	list_for_each_entry(tmp_adev, device_list_handle, reset_list) {
 		r = amdgpu_device_pre_asic_reset(tmp_adev, reset_context);
@@ -5852,6 +5853,7 @@ int amdgpu_device_gpu_recover(struct amdgpu_device *adev,
 		 */
 		amdgpu_device_stop_pending_resets(tmp_adev);
 	}
+	amdgpu_unlock_hw_access(adev);
 
 skip_hw_reset:
 
@@ -6449,6 +6451,26 @@ int amdgpu_in_reset(struct amdgpu_device *adev)
 	return atomic_read(&adev->reset_domain->in_gpu_reset);
 }
 
+void amdgpu_lock_hw_access(struct amdgpu_device *adev)
+{
+	down_write(&adev->reset_domain->gpu_sem);
+}
+
+void amdgpu_unlock_hw_access(struct amdgpu_device *adev)
+{
+	up_write(&adev->reset_domain->gpu_sem);
+}
+
+int amdgpu_begin_hw_access(struct amdgpu_device *adev)
+{
+	return down_read_trylock(&adev->reset_domain->gpu_sem);
+}
+
+void amdgpu_end_hw_access(struct amdgpu_device *adev)
+{
+	up_read(&adev->reset_domain->gpu_sem);
+}
+
 /**
  * amdgpu_device_halt() - bring hardware to some kind of halt state
  *
drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c +38 -36

115 lines not on this page · raw message

drivers/gpu/drm/amd/amdgpu/amdgpu_reset.c +1 -0
index bfdde772b7ee..01b109ec705b 100644
@@ -144,6 +144,7 @@ struct amdgpu_reset_domain *amdgpu_reset_create_reset_domain(enum amdgpu_reset_d
 	atomic_set(&reset_domain->in_gpu_reset, 0);
 	atomic_set(&reset_domain->reset_res, 0);
 	init_rwsem(&reset_domain->sem);
+	init_rwsem(&reset_domain->gpu_sem);
 
 	return reset_domain;
 }
drivers/gpu/drm/amd/amdgpu/amdgpu_reset.h +1 -0
index 5a9cc043b858..a8ea493ef0e4 100644
@@ -86,6 +86,7 @@ struct amdgpu_reset_domain {
 	struct workqueue_struct *wq;
 	enum amdgpu_reset_domain_type type;
 	struct rw_semaphore sem;
+	struct rw_semaphore gpu_sem;
 	atomic_t in_gpu_reset;
 	atomic_t reset_res;
 };
drivers/gpu/drm/amd/amdgpu/mxgpu_ai.c +2 -5
index f4c47492e0cd..38bfd1bb1192 100644
@@ -259,11 +259,9 @@ static void xgpu_ai_mailbox_flr_work(struct work_struct *work)
 	 * otherwise the mailbox msg will be ruined/reseted by
 	 * the VF FLR.
 	 */
-	if (atomic_cmpxchg(&adev->reset_domain->in_gpu_reset, 0, 1) != 0)
+	if (!amdgpu_begin_hw_access(adev))
 		return;
 
-	down_write(&adev->reset_domain->sem);
-
 	amdgpu_virt_fini_data_exchange(adev);
 
 	xgpu_ai_mailbox_trans_msg(adev, IDH_READY_TO_RESET, 0, 0, 0);
@@ -279,8 +277,7 @@ static void xgpu_ai_mailbox_flr_work(struct work_struct *work)
 	dev_warn(adev->dev, "waiting IDH_FLR_NOTIFICATION_CMPL timeout\n");
 
 flr_done:
-	atomic_set(&adev->reset_domain->in_gpu_reset, 0);
-	up_write(&adev->reset_domain->sem);
+	amdgpu_end_hw_access(adev);
 
 	/* Trigger recovery for world switch failure if no TDR */
 	if (amdgpu_device_should_recover_gpu(adev)
drivers/gpu/drm/amd/amdgpu/mxgpu_nv.c +2 -5
index 37b49a5ed2a1..522198b511d7 100644
@@ -292,11 +292,9 @@ static void xgpu_nv_mailbox_flr_work(struct work_struct *work)
 	 * otherwise the mailbox msg will be ruined/reseted by
 	 * the VF FLR.
 	 */
-	if (atomic_cmpxchg(&adev->reset_domain->in_gpu_reset, 0, 1) != 0)
+	if (!amdgpu_begin_hw_access(adev))
 		return;
 
-	down_write(&adev->reset_domain->sem);
-
 	amdgpu_virt_fini_data_exchange(adev);
 
 	xgpu_nv_mailbox_trans_msg(adev, IDH_READY_TO_RESET, 0, 0, 0);
@@ -312,8 +310,7 @@ static void xgpu_nv_mailbox_flr_work(struct work_struct *work)
 	dev_warn(adev->dev, "waiting IDH_FLR_NOTIFICATION_CMPL timeout\n");
 
 flr_done:
-	atomic_set(&adev->reset_domain->in_gpu_reset, 0);
-	up_write(&adev->reset_domain->sem);
+	amdgpu_end_hw_access(adev);
 
 	/* Trigger recovery for world switch failure if no TDR */
 	if (amdgpu_device_should_recover_gpu(adev)
drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +2 -1
index 4721b2fccd06..ed96ed46d912 100644
@@ -262,7 +262,7 @@ static int remove_queue_mes(struct device_queue_manager *dqm, struct queue *q,
 	int r;
 	struct mes_remove_queue_input queue_input;
 
-	if (dqm->is_hws_hang)
+	if (dqm->is_hws_hang || !amdgpu_begin_hw_access(adev))
 		return -EIO;
 
 	memset(&queue_input, 0x0, sizeof(struct mes_remove_queue_input));
@@ -272,6 +272,7 @@ static int remove_queue_mes(struct device_queue_manager *dqm, struct queue *q,
 	amdgpu_mes_lock(&adev->mes);
 	r = adev->mes.funcs->remove_hw_queue(&adev->mes, &queue_input);
 	amdgpu_mes_unlock(&adev->mes);
+	amdgpu_end_hw_access(adev);
 
 	if (r) {
 		dev_err(adev->dev, "failed to remove hardware queue from MES, doorbell=0x%x\n",
drivers/gpu/drm/amd/amdkfd/kfd_process_queue_manager.c +6 -2
index 6bf79c435f2e..c2bccc78525c 100644
@@ -87,8 +87,12 @@ void kfd_process_dequeue_from_device(struct kfd_process_device *pdd)
 		return;
 
 	dev->dqm->ops.process_termination(dev->dqm, &pdd->qpd);
-	if (dev->kfd->shared_resources.enable_mes)
-		amdgpu_mes_flush_shader_debugger(dev->adev, pdd->proc_ctx_gpu_addr);
+	if (dev->kfd->shared_resources.enable_mes &&
+	    amdgpu_begin_hw_access(dev->adev)) {
+		amdgpu_mes_flush_shader_debugger(dev->adev,
+						 pdd->proc_ctx_gpu_addr);
+		amdgpu_end_hw_access(dev->adev);
+	}
 	pdd->already_dequeued = true;
 }
 
Signature · 1 line
2.34.1
maintainer
#4 lore raw

Re: [PATCH 4/4] drm/amdgpu: prevent gpu access during reset recovery

Am 22.05.24 um 19:27 schrieb Yunxiang Li:

Quoted 27 lines

Random accesses to the GPU while it is not re-initialized can lead to a bad time. So add a rwsem to prevent such accesses. Normal accesses will now take the read lock for shared GPU access, reset takes the write lock for exclusive GPU access.

Care need to be taken so that the recovery thread does not take the read lock and deadlock itself, and normal access should avoid waiting on the reset to finish and should instead treat the hardware access as failed.

---
  drivers/gpu/drm/amd/amdgpu/amdgpu.h           |  5 ++
  drivers/gpu/drm/amd/amdgpu/amdgpu_device.c    | 22 ++++++
  drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c       | 74 ++++++++++---------
  drivers/gpu/drm/amd/amdgpu/amdgpu_reset.c     |  1 +
  drivers/gpu/drm/amd/amdgpu/amdgpu_reset.h     |  1 +
  drivers/gpu/drm/amd/amdgpu/mxgpu_ai.c         |  7 +-
  drivers/gpu/drm/amd/amdgpu/mxgpu_nv.c         |  7 +-
  .../drm/amd/amdkfd/kfd_device_queue_manager.c |  3 +-
  .../amd/amdkfd/kfd_process_queue_manager.c    |  8 +-
  9 files changed, 79 insertions(+), 49 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu.h +0 -0
index 1f71c7b98d77..5a089e2dec2a 100644

drivers/gpu/drm/amd/amdgpu/amdgpu.h static inline bool amdgpu_is_tmz(struct amdgpu_device *adev) · line 1639

Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu.h +5 -0
@@ -1632,6 +1632,11 @@ static inline bool amdgpu_is_tmz(struct amdgpu_device *adev)
 
  int amdgpu_in_reset(struct amdgpu_device *adev);
 
+void amdgpu_lock_hw_access(struct amdgpu_device *adev);
+void amdgpu_unlock_hw_access(struct amdgpu_device *adev);
+int amdgpu_begin_hw_access(struct amdgpu_device *adev);
+void amdgpu_end_hw_access(struct amdgpu_device *adev);
+

Don't add anything to amdgpu.h. We are slowly decomposing that file.

drivers/gpu/drm/amd/amdgpu/amdgpu_device.c int amdgpu_device_gpu_recover(struct amdgpu_device *adev, · line 5819

Quoted 12 lines
drivers/gpu/drm/amd/amdgpu/amdgpu.h +0 -0
  extern const struct attribute_group amdgpu_vram_mgr_attr_group;
  extern const struct attribute_group amdgpu_gtt_mgr_attr_group;
  extern const struct attribute_group amdgpu_flash_attr_group;
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +1 -0
index e74789691070..057d735c7cae 100644
@@ -5816,6 +5816,7 @@ int amdgpu_device_gpu_recover(struct amdgpu_device *adev,
  		goto skip_hw_reset;
  	}
 
+	amdgpu_lock_hw_access(adev);

That should already be locked here. So this will most likely deadlock.

Quoted 25 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +11 -0
  retry:	/* Rest of adevs pre asic reset from XGMI hive. */
  	list_for_each_entry(tmp_adev, device_list_handle, reset_list) {
  		r = amdgpu_device_pre_asic_reset(tmp_adev, reset_context);
@@ -5852,6 +5853,7 @@ int amdgpu_device_gpu_recover(struct amdgpu_device *adev,
  		 */
  		amdgpu_device_stop_pending_resets(tmp_adev);
  	}
+	amdgpu_unlock_hw_access(adev);
 
  skip_hw_reset:
 
@@ -6449,6 +6451,26 @@ int amdgpu_in_reset(struct amdgpu_device *adev)
  	return atomic_read(&adev->reset_domain->in_gpu_reset);
  }
 
+void amdgpu_lock_hw_access(struct amdgpu_device *adev)
+{
+	down_write(&adev->reset_domain->gpu_sem);
+}
+
+void amdgpu_unlock_hw_access(struct amdgpu_device *adev)
+{
+	up_write(&adev->reset_domain->gpu_sem);
+}
+

drivers/gpu/drm/amd/amdgpu/amdgpu_device.c int amdgpu_in_reset(struct amdgpu_device *adev) · line 6473

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +10 -0
+int amdgpu_begin_hw_access(struct amdgpu_device *adev)
+{
+	return down_read_trylock(&adev->reset_domain->gpu_sem);
+}
+
+void amdgpu_end_hw_access(struct amdgpu_device *adev)
+{
+	up_read(&adev->reset_domain->gpu_sem);
+}
+

Don't add helpers for that, especially not with that name.

We don't block HW access, but just prevent concurrent resets.

Quoted 32 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +0 -0
  /**
   * amdgpu_device_halt() - bring hardware to some kind of halt state
   *
drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c +3 -3
index 603c0738fd03..098755db9d20 100644
@@ -623,12 +623,11 @@ void amdgpu_gmc_flush_gpu_tlb(struct amdgpu_device *adev, uint32_t vmid,
  	    !adev->mman.buffer_funcs_enabled ||
  	    !adev->ib_pool_ready || amdgpu_in_reset(adev) ||
  	    !ring->sched.ready) {
-
  		/*
  		 * A GPU reset should flush all TLBs anyway, so no need to do
  		 * this while one is ongoing.
  		 */
-		if (!down_read_trylock(&adev->reset_domain->sem))
+		if (!amdgpu_begin_hw_access(adev))
  			return;
 
  		if (adev->gmc.flush_tlb_needs_extra_type_2)
@@ -641,7 +640,8 @@ void amdgpu_gmc_flush_gpu_tlb(struct amdgpu_device *adev, uint32_t vmid,
 
  		adev->gmc.gmc_funcs->flush_gpu_tlb(adev, vmid, vmhub,
  						   flush_type);
-		up_read(&adev->reset_domain->sem);
+
+		amdgpu_end_hw_access(adev);
  		return;
  	}
 

drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c int amdgpu_gmc_flush_gpu_tlb_pasid(struct amdgpu_device *adev, uint16_t pasid, · line 687

Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c +1 -1
@@ -684,12 +684,18 @@ int amdgpu_gmc_flush_gpu_tlb_pasid(struct amdgpu_device *adev, uint16_t pasid,
  	struct amdgpu_ring *ring = &adev->gfx.kiq[inst].ring;
  	struct amdgpu_kiq *kiq = &adev->gfx.kiq[inst];
  	unsigned int ndw;
-	signed long r;
+	signed long r = 0;

Please don't initialize local variables if it isn't necessary.

drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c int amdgpu_gmc_flush_gpu_tlb_pasid(struct amdgpu_device *adev, uint16_t pasid, · line 698

Quoted 13 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c +8 -2
  	uint32_t seq;
 
-	if (!adev->gmc.flush_pasid_uses_kiq || !ring->sched.ready ||
-	    !down_read_trylock(&adev->reset_domain->sem)) {
+	/*
+	* A GPU reset should flush all TLBs anyway, so no need to do
+	* this while one is ongoing.
+	*/
+	if (!amdgpu_begin_hw_access(adev))
+		return 0;
 
+	if (!adev->gmc.flush_pasid_uses_kiq || !ring->sched.ready ||
+	    amdgpu_in_reset(adev)) {

That check doesn't makes sense now any more.

drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c int amdgpu_gmc_flush_gpu_tlb_pasid(struct amdgpu_device *adev, uint16_t pasid, · line 712

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c +1 -2
  		if (adev->gmc.flush_tlb_needs_extra_type_2)
  			adev->gmc.gmc_funcs->flush_gpu_tlb_pasid(adev, pasid,
  								 2, all_hub,
@@ -703,46 +709,42 @@ int amdgpu_gmc_flush_gpu_tlb_pasid(struct amdgpu_device *adev, uint16_t pasid,
  		adev->gmc.gmc_funcs->flush_gpu_tlb_pasid(adev, pasid,
  							 flush_type, all_hub,
  							 inst);
-		return 0;
-	}
+	} else {

That the locking is missing here should be a separate bug fix independent of other changes.

Regards,
Christian.

Quoted 184 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c +25 -28
+		/* 2 dwords flush + 8 dwords fence */
+		ndw = kiq->pmf->invalidate_tlbs_size + 8;
 
-	/* 2 dwords flush + 8 dwords fence */
-	ndw = kiq->pmf->invalidate_tlbs_size + 8;
+		if (adev->gmc.flush_tlb_needs_extra_type_2)
+			ndw += kiq->pmf->invalidate_tlbs_size;
 
-	if (adev->gmc.flush_tlb_needs_extra_type_2)
-		ndw += kiq->pmf->invalidate_tlbs_size;
+		if (adev->gmc.flush_tlb_needs_extra_type_0)
+			ndw += kiq->pmf->invalidate_tlbs_size;
 
-	if (adev->gmc.flush_tlb_needs_extra_type_0)
-		ndw += kiq->pmf->invalidate_tlbs_size;
+		spin_lock(&adev->gfx.kiq[inst].ring_lock);
+		amdgpu_ring_alloc(ring, ndw);
+		if (adev->gmc.flush_tlb_needs_extra_type_2)
+			kiq->pmf->kiq_invalidate_tlbs(ring, pasid, 2, all_hub);
 
-	spin_lock(&adev->gfx.kiq[inst].ring_lock);
-	amdgpu_ring_alloc(ring, ndw);
-	if (adev->gmc.flush_tlb_needs_extra_type_2)
-		kiq->pmf->kiq_invalidate_tlbs(ring, pasid, 2, all_hub);
+		if (flush_type == 2 && adev->gmc.flush_tlb_needs_extra_type_0)
+			kiq->pmf->kiq_invalidate_tlbs(ring, pasid, 0, all_hub);
 
-	if (flush_type == 2 && adev->gmc.flush_tlb_needs_extra_type_0)
-		kiq->pmf->kiq_invalidate_tlbs(ring, pasid, 0, all_hub);
+		kiq->pmf->kiq_invalidate_tlbs(ring, pasid, flush_type, all_hub);
+		r = amdgpu_fence_emit_polling(ring, &seq, MAX_KIQ_REG_WAIT);
+		if (r) {
+			amdgpu_ring_undo(ring);
+			spin_unlock(&adev->gfx.kiq[inst].ring_lock);
+			goto error_unlock_reset;
+		}
 
-	kiq->pmf->kiq_invalidate_tlbs(ring, pasid, flush_type, all_hub);
-	r = amdgpu_fence_emit_polling(ring, &seq, MAX_KIQ_REG_WAIT);
-	if (r) {
-		amdgpu_ring_undo(ring);
+		amdgpu_ring_commit(ring);
  		spin_unlock(&adev->gfx.kiq[inst].ring_lock);
-		goto error_unlock_reset;
-	}
-
-	amdgpu_ring_commit(ring);
-	spin_unlock(&adev->gfx.kiq[inst].ring_lock);
-	r = amdgpu_fence_wait_polling(ring, seq, usec_timeout);
-	if (r < 1) {
-		dev_err(adev->dev, "wait for kiq fence error: %ld.\n", r);
-		r = -ETIME;
-		goto error_unlock_reset;
+		if (amdgpu_fence_wait_polling(ring, seq, usec_timeout) < 1) {
+			dev_err(adev->dev, "timeout waiting for kiq fence\n");
+			r = -ETIME;
+		}
  	}
-	r = 0;
 
  error_unlock_reset:
-	up_read(&adev->reset_domain->sem);
+	amdgpu_end_hw_access(adev);
  	return r;
  }
 
drivers/gpu/drm/amd/amdgpu/amdgpu_reset.c +1 -0
index bfdde772b7ee..01b109ec705b 100644
@@ -144,6 +144,7 @@ struct amdgpu_reset_domain *amdgpu_reset_create_reset_domain(enum amdgpu_reset_d
  	atomic_set(&reset_domain->in_gpu_reset, 0);
  	atomic_set(&reset_domain->reset_res, 0);
  	init_rwsem(&reset_domain->sem);
+	init_rwsem(&reset_domain->gpu_sem);
 
  	return reset_domain;
  }
drivers/gpu/drm/amd/amdgpu/amdgpu_reset.h +1 -0
index 5a9cc043b858..a8ea493ef0e4 100644
@@ -86,6 +86,7 @@ struct amdgpu_reset_domain {
  	struct workqueue_struct *wq;
  	enum amdgpu_reset_domain_type type;
  	struct rw_semaphore sem;
+	struct rw_semaphore gpu_sem;
  	atomic_t in_gpu_reset;
  	atomic_t reset_res;
  };
drivers/gpu/drm/amd/amdgpu/mxgpu_ai.c +2 -5
index f4c47492e0cd..38bfd1bb1192 100644
@@ -259,11 +259,9 @@ static void xgpu_ai_mailbox_flr_work(struct work_struct *work)
  	 * otherwise the mailbox msg will be ruined/reseted by
  	 * the VF FLR.
  	 */
-	if (atomic_cmpxchg(&adev->reset_domain->in_gpu_reset, 0, 1) != 0)
+	if (!amdgpu_begin_hw_access(adev))
  		return;
 
-	down_write(&adev->reset_domain->sem);
-
  	amdgpu_virt_fini_data_exchange(adev);
 
  	xgpu_ai_mailbox_trans_msg(adev, IDH_READY_TO_RESET, 0, 0, 0);
@@ -279,8 +277,7 @@ static void xgpu_ai_mailbox_flr_work(struct work_struct *work)
  	dev_warn(adev->dev, "waiting IDH_FLR_NOTIFICATION_CMPL timeout\n");
 
  flr_done:
-	atomic_set(&adev->reset_domain->in_gpu_reset, 0);
-	up_write(&adev->reset_domain->sem);
+	amdgpu_end_hw_access(adev);
 
  	/* Trigger recovery for world switch failure if no TDR */
  	if (amdgpu_device_should_recover_gpu(adev)
drivers/gpu/drm/amd/amdgpu/mxgpu_nv.c +2 -5
index 37b49a5ed2a1..522198b511d7 100644
@@ -292,11 +292,9 @@ static void xgpu_nv_mailbox_flr_work(struct work_struct *work)
  	 * otherwise the mailbox msg will be ruined/reseted by
  	 * the VF FLR.
  	 */
-	if (atomic_cmpxchg(&adev->reset_domain->in_gpu_reset, 0, 1) != 0)
+	if (!amdgpu_begin_hw_access(adev))
  		return;
 
-	down_write(&adev->reset_domain->sem);
-
  	amdgpu_virt_fini_data_exchange(adev);
 
  	xgpu_nv_mailbox_trans_msg(adev, IDH_READY_TO_RESET, 0, 0, 0);
@@ -312,8 +310,7 @@ static void xgpu_nv_mailbox_flr_work(struct work_struct *work)
  	dev_warn(adev->dev, "waiting IDH_FLR_NOTIFICATION_CMPL timeout\n");
 
  flr_done:
-	atomic_set(&adev->reset_domain->in_gpu_reset, 0);
-	up_write(&adev->reset_domain->sem);
+	amdgpu_end_hw_access(adev);
 
  	/* Trigger recovery for world switch failure if no TDR */
  	if (amdgpu_device_should_recover_gpu(adev)
drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +2 -1
index 4721b2fccd06..ed96ed46d912 100644
@@ -262,7 +262,7 @@ static int remove_queue_mes(struct device_queue_manager *dqm, struct queue *q,
  	int r;
  	struct mes_remove_queue_input queue_input;
 
-	if (dqm->is_hws_hang)
+	if (dqm->is_hws_hang || !amdgpu_begin_hw_access(adev))
  		return -EIO;
 
  	memset(&queue_input, 0x0, sizeof(struct mes_remove_queue_input));
@@ -272,6 +272,7 @@ static int remove_queue_mes(struct device_queue_manager *dqm, struct queue *q,
  	amdgpu_mes_lock(&adev->mes);
  	r = adev->mes.funcs->remove_hw_queue(&adev->mes, &queue_input);
  	amdgpu_mes_unlock(&adev->mes);
+	amdgpu_end_hw_access(adev);
 
  	if (r) {
  		dev_err(adev->dev, "failed to remove hardware queue from MES, doorbell=0x%x\n",
drivers/gpu/drm/amd/amdkfd/kfd_process_queue_manager.c +6 -2
index 6bf79c435f2e..c2bccc78525c 100644
@@ -87,8 +87,12 @@ void kfd_process_dequeue_from_device(struct kfd_process_device *pdd)
  		return;
 
  	dev->dqm->ops.process_termination(dev->dqm, &pdd->qpd);
-	if (dev->kfd->shared_resources.enable_mes)
-		amdgpu_mes_flush_shader_debugger(dev->adev, pdd->proc_ctx_gpu_addr);
+	if (dev->kfd->shared_resources.enable_mes &&
+	    amdgpu_begin_hw_access(dev->adev)) {
+		amdgpu_mes_flush_shader_debugger(dev->adev,
+						 pdd->proc_ctx_gpu_addr);
+		amdgpu_end_hw_access(dev->adev);
+	}
  	pdd->already_dequeued = true;
  }
 
#5 lore raw

RE: [PATCH 4/4] drm/amdgpu: prevent gpu access during reset recovery

[AMD Official Use Only - AMD Internal Distribution Only]

Quoted 6 lines

Review

Quoted 4 lines
(quoted diff) +4 -0
+void amdgpu_lock_hw_access(struct amdgpu_device *adev); void
+amdgpu_unlock_hw_access(struct amdgpu_device *adev); int
+amdgpu_begin_hw_access(struct amdgpu_device *adev); void
+amdgpu_end_hw_access(struct amdgpu_device *adev);

Don't add anything to amdgpu.h. We are slowly decomposing that file.

Where would be a better place? I just wanted to have them next to amdgpu_in_reset

Quoted 17 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_device.c int amdgpu_device_gpu_recover(struct amdgpu_device *adev, · line 5819

Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +1 -0
@@ -5816,6 +5816,7 @@ int amdgpu_device_gpu_recover(struct amdgpu_device *adev,
            goto skip_hw_reset;
    }
 
+   amdgpu_lock_hw_access(adev);

That should already be locked here. So this will most likely deadlock.

Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +1 -1
  retry:    /* Rest of adevs pre asic reset from XGMI hive. */
    list_for_each_entry(tmp_adev, device_list_handle, reset_list) {
            r = amdgpu_device_pre_asic_reset(tmp_adev, reset_context); @@
-5852,6 +5853,7 @@ int amdgpu_device_gpu_recover(struct amdgpu_device *adev,
             */
            amdgpu_device_stop_pending_resets(tmp_adev);
    }
+   amdgpu_unlock_hw_access(adev);
Quoted 5 lines
Quoted 1 line

skip_hw_reset:

Don't add helpers for that, especially not with that name.

We don't block HW access, but just prevent concurrent resets.

Here is taking a different lock than the reset_domain->sem. It is a seperate reset_domain->gpu_sem that is only locked when we will actuall do reset, it is not taken in the skip_hw_reset path.

Quoted 11 lines
Quoted 11 lines
    uint32_t seq;
drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c +6 -2
-   if (!adev->gmc.flush_pasid_uses_kiq || !ring->sched.ready ||
-       !down_read_trylock(&adev->reset_domain->sem)) {
+   /*
+   * A GPU reset should flush all TLBs anyway, so no need to do
+   * this while one is ongoing.
+   */
+   if (!amdgpu_begin_hw_access(adev))
+           return 0;
Quoted 4 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c int amdgpu_gmc_flush_gpu_tlb_pasid(struct amdgpu_device *adev, uint16_t pasid, · line 698

Quoted 2 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c +2 -0
+   if (!adev->gmc.flush_pasid_uses_kiq || !ring->sched.ready ||
+       amdgpu_in_reset(adev)) {

That check doesn't makes sense now any more.

same here, the two checks are for different scope, although I wasn't sure if the original check is correct or not, is there a possibility that !adev->gmc.flush_pasid_uses_kiq and !ring->sched.ready are false but amdgpu_in_reset(adev) is true? and to we want to take this branch when that happens?

Quoted 10 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c int amdgpu_gmc_flush_gpu_tlb_pasid(struct amdgpu_device *adev, uint16_t pasid, · line 687

Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c +1 -1
@@ -684,12 +684,18 @@ int amdgpu_gmc_flush_gpu_tlb_pasid(struct amdgpu_device *adev, uint16_t pasid,
    struct amdgpu_ring *ring = &adev->gfx.kiq[inst].ring;
    struct amdgpu_kiq *kiq = &adev->gfx.kiq[inst];
    unsigned int ndw;
-   signed long r;
+   signed long r = 0;

Please don't initialize local variables if it isn't necessary.

Quoted 12 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c int amdgpu_gmc_flush_gpu_tlb_pasid(struct amdgpu_device *adev, uint16_t pasid, · line 712

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c +1 -2
            if (adev->gmc.flush_tlb_needs_extra_type_2)
                    adev->gmc.gmc_funcs->flush_gpu_tlb_pasid(adev, pasid,
                                                             2, all_hub,
@@ -703,46 +709,42 @@ int amdgpu_gmc_flush_gpu_tlb_pasid(struct amdgpu_device *adev, uint16_t pasid,
            adev->gmc.gmc_funcs->flush_gpu_tlb_pasid(adev, pasid,
                                                     flush_type, all_hub,
                                                     inst);
-           return 0;
-   }
+   } else {

That the locking is missing here should be a separate bug fix independent of other changes.

I will split this off into a seperate patch, initializing r is needed because I consolidated the return paths to drop the read lock.

maintainer
#6 lore raw

Re: [PATCH 4/4] drm/amdgpu: prevent gpu access during reset recovery

Am 23.05.24 um 13:36 schrieb Li, Yunxiang (Teddy):

Quoted 8 lines

[AMD Official Use Only - AMD Internal Distribution Only]

Quoted 5 lines

Review

Quoted 4 lines
(quoted diff) +4 -0
+void amdgpu_lock_hw_access(struct amdgpu_device *adev); void
+amdgpu_unlock_hw_access(struct amdgpu_device *adev); int
+amdgpu_begin_hw_access(struct amdgpu_device *adev); void
+amdgpu_end_hw_access(struct amdgpu_device *adev);

Don't add anything to amdgpu.h. We are slowly decomposing that file.

Where would be a better place? I just wanted to have them next to amdgpu_in_reset

amdgpu_reset.h if you have time feel free to move amdgpu_in_reset() over there as well.

Quoted 17 lines
Quoted 17 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_device.c int amdgpu_device_gpu_recover(struct amdgpu_device *adev, · line 5819

Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +1 -0
@@ -5816,6 +5816,7 @@ int amdgpu_device_gpu_recover(struct amdgpu_device *adev,
             goto skip_hw_reset;
     }
 
+   amdgpu_lock_hw_access(adev);

That should already be locked here. So this will most likely deadlock.

Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +1 -1
   retry:    /* Rest of adevs pre asic reset from XGMI hive. */
     list_for_each_entry(tmp_adev, device_list_handle, reset_list) {
             r = amdgpu_device_pre_asic_reset(tmp_adev, reset_context); @@
-5852,6 +5853,7 @@ int amdgpu_device_gpu_recover(struct amdgpu_device *adev,
              */
             amdgpu_device_stop_pending_resets(tmp_adev);
     }
+   amdgpu_unlock_hw_access(adev);
Quoted 5 lines
Quoted 4 lines
Quoted 1 line

skip_hw_reset:

Don't add helpers for that, especially not with that name.

We don't block HW access, but just prevent concurrent resets.

Here is taking a different lock than the reset_domain->sem. It is a seperate reset_domain->gpu_sem that is only locked when we will actuall do reset, it is not taken in the skip_hw_reset path.

Exactly that is what you should *not* do. Please don't add any new lock to the code. This is already complicated enough.

If you think that adding wrappers for reset lock makes sense then we can probably do that, bot don't add any lock for hw access.

Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
     uint32_t seq;
drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c +6 -2
-   if (!adev->gmc.flush_pasid_uses_kiq || !ring->sched.ready ||
-       !down_read_trylock(&adev->reset_domain->sem)) {
+   /*
+   * A GPU reset should flush all TLBs anyway, so no need to do
+   * this while one is ongoing.
+   */
+   if (!amdgpu_begin_hw_access(adev))
+           return 0;
Quoted 4 lines
Quoted 3 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c int amdgpu_gmc_flush_gpu_tlb_pasid(struct amdgpu_device *adev, uint16_t pasid, · line 698

Quoted 2 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c +2 -0
+   if (!adev->gmc.flush_pasid_uses_kiq || !ring->sched.ready ||
+       amdgpu_in_reset(adev)) {

That check doesn't makes sense now any more.

same here, the two checks are for different scope, although I wasn't sure if the original check is correct or not, is there a possibility that !adev->gmc.flush_pasid_uses_kiq and !ring->sched.ready are false but amdgpu_in_reset(adev) is true? and to we want to take this branch when that happens?

amdgpu_in_reset() in used incorrect in quite a lot of places. It should only be used inside the HW backend code to distinct between initial load and reset.

All other use cases especially the ones in the IOCTL front end functions as well as here in the midlayer which isn't used by GPU reset are incorrect.

Quoted 20 lines
Quoted 9 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c int amdgpu_gmc_flush_gpu_tlb_pasid(struct amdgpu_device *adev, uint16_t pasid, · line 687

Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c +1 -1
@@ -684,12 +684,18 @@ int amdgpu_gmc_flush_gpu_tlb_pasid(struct amdgpu_device *adev, uint16_t pasid,
     struct amdgpu_ring *ring = &adev->gfx.kiq[inst].ring;
     struct amdgpu_kiq *kiq = &adev->gfx.kiq[inst];
     unsigned int ndw;
-   signed long r;
+   signed long r = 0;

Please don't initialize local variables if it isn't necessary.

Quoted 11 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c int amdgpu_gmc_flush_gpu_tlb_pasid(struct amdgpu_device *adev, uint16_t pasid, · line 712

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c +1 -2
             if (adev->gmc.flush_tlb_needs_extra_type_2)
                     adev->gmc.gmc_funcs->flush_gpu_tlb_pasid(adev, pasid,
                                                              2, all_hub,
@@ -703,46 +709,42 @@ int amdgpu_gmc_flush_gpu_tlb_pasid(struct amdgpu_device *adev, uint16_t pasid,
             adev->gmc.gmc_funcs->flush_gpu_tlb_pasid(adev, pasid,
                                                      flush_type, all_hub,
                                                      inst);
-           return 0;
-   }
+   } else {

That the locking is missing here should be a separate bug fix independent of other changes.

Quoted 1 line

I will split this off into a seperate patch, initializing r is needed because I consolidated the return paths to drop the read lock.

In that case better set r when it's not initialized in some path.

Regards,
Christian.

#7 lore raw

RE: [PATCH 4/4] drm/amdgpu: prevent gpu access during reset recovery

[Public]

Quoted 5 lines
Quoted 1 line

Here is taking a different lock than the reset_domain->sem. It is a seperate reset_domain->gpu_sem that is only locked when we will actuall do reset, it is not taken in the skip_hw_reset path.

Exactly that is what you should *not* do. Please don't add any new lock to the code. This is already complicated enough.

If you think that adding wrappers for reset lock makes sense then we can probably do that, bot don't add any lock for hw access.

The two lock protects very different things though. The first case is we need to block two resets running in parallel, this does not only cover GPU reset itself but also any teardown that happens before GPU reset. The second case is we need to ensure exclusive access to the GPU between GPU reset and GPU init, concurrent access is fine before GPU is reset.

Theoretically, the second case happens within the first case, so locking the first case would protect against both. But with the current implementation this is infeasible, all the generic functions called between amdgpu_device_lock/unlock_reset_domain would need to be swapped out for special versions so the reset thread does not dead lock itself. It is much simpler to have a second, much narrower lock that only covers GPU reset<->GPU init because all the accesses there are very low level anyway.

Teddy

maintainer
#8 lore raw

Re: [PATCH 4/4] drm/amdgpu: prevent gpu access during reset recovery

Am 23.05.24 um 17:35 schrieb Li, Yunxiang (Teddy):

Quoted 7 lines

[Public]

Quoted 4 lines
Quoted 1 line

Here is taking a different lock than the reset_domain->sem. It is a seperate reset_domain->gpu_sem that is only locked when we will actuall do reset, it is not taken in the skip_hw_reset path.

Exactly that is what you should *not* do. Please don't add any new lock to the code. This is already complicated enough.

If you think that adding wrappers for reset lock makes sense then we can probably do that, bot don't add any lock for hw access.

The two lock protects very different things though. The first case is we need to block two resets running in parallel,

No, that's not correct. Two parallel resets are prevent by using a worker queue for the resets.

The reset lock is here exactly to provide external thread the opportunity to make their operation mutual exclusive with the reset.

Quoted 1 line

this does not only cover GPU reset itself but also any teardown that happens before GPU reset. The second case is we need to ensure exclusive access to the GPU between GPU reset and GPU init, concurrent access is fine before GPU is reset.

If that is true you could in theory lower the locked area of the existing lock, but adding a new one is strict no-go from my side.

Regards,
Christian.

Quoted 4 lines

Theoretically, the second case happens within the first case, so locking the first case would protect against both. But with the current implementation this is infeasible, all the generic functions called between amdgpu_device_lock/unlock_reset_domain would need to be swapped out for special versions so the reset thread does not dead lock itself. It is much simpler to have a second, much narrower lock that only covers GPU reset<->GPU init because all the accesses there are very low level anyway.

Teddy

#9 lore raw

RE: [PATCH 4/4] drm/amdgpu: prevent gpu access during reset recovery

[AMD Official Use Only - AMD Internal Distribution Only]

Quoted 1 line

If that is true you could in theory lower the locked area of the existing lock, but adding a new one is strict no-go from my side.

I'll try this, right now I see two places where this would be problematic because they are trying to suspend either gfx or kfd scheduler and later resume it, which would race with the same in reset. It is exactly this two functions I'm trying to avoid putting in the scope of the second lock, because they are common to both in and outside reset.

Teddy

maintainer
#10 lore raw

Re: [PATCH 4/4] drm/amdgpu: prevent gpu access during reset recovery

Am 24.05.24 um 15:35 schrieb Li, Yunxiang (Teddy):

Quoted 4 lines

[AMD Official Use Only - AMD Internal Distribution Only]

Quoted 1 line

If that is true you could in theory lower the locked area of the existing lock, but adding a new one is strict no-go from my side.

I'll try this, right now I see two places where this would be problematic because they are trying to suspend either gfx or kfd scheduler and later resume it, which would race with the same in reset. It is exactly this two functions I'm trying to avoid putting in the scope of the second lock, because they are common to both in and outside reset.

I don't think that avoiding the suspending and resuming the schedulers is worth moving the locks.

That is something so rarely done that it shouldn't matter.

Christian.

Quoted 2 lines

Teddy

#11 lore raw

RE: [PATCH 4/4] drm/amdgpu: prevent gpu access during reset recovery

[AMD Official Use Only - AMD Internal Distribution Only]

Yes, the two places are 1. In debugfs and 2. In MI100's en/disable_debug_trap, and evidently someone is testing the debugfs interface because there's a bug fix for a race condition of it.

Teddy

#12 lore raw

Re: [PATCH 4/4] drm/amdgpu: prevent gpu access during reset recovery

In reply to Christian König
Quoted 59 lines · Christian König

Am 23.05.24 um 13:36 schrieb Li, Yunxiang (Teddy):

Quoted 8 lines

[AMD Official Use Only - AMD Internal Distribution Only]

Quoted 5 lines

Review

Quoted 4 lines
(quoted diff) +4 -0
+void amdgpu_lock_hw_access(struct amdgpu_device *adev); void
+amdgpu_unlock_hw_access(struct amdgpu_device *adev); int
+amdgpu_begin_hw_access(struct amdgpu_device *adev); void
+amdgpu_end_hw_access(struct amdgpu_device *adev);

Don't add anything to amdgpu.h. We are slowly decomposing that file.

Where would be a better place? I just wanted to have them next to amdgpu_in_reset

amdgpu_reset.h if you have time feel free to move amdgpu_in_reset() over there as well.

Quoted 17 lines
Quoted 17 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_device.c int amdgpu_device_gpu_recover(struct amdgpu_device *adev, · line 5819

Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +1 -0
@@ -5816,6 +5816,7 @@ int amdgpu_device_gpu_recover(struct amdgpu_device *adev,
             goto skip_hw_reset;
     }
 
+   amdgpu_lock_hw_access(adev);

That should already be locked here. So this will most likely deadlock.

Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +1 -1
   retry:    /* Rest of adevs pre asic reset from XGMI hive. */
     list_for_each_entry(tmp_adev, device_list_handle, reset_list) {
             r = amdgpu_device_pre_asic_reset(tmp_adev, reset_context); @@
-5852,6 +5853,7 @@ int amdgpu_device_gpu_recover(struct amdgpu_device *adev,
              */
             amdgpu_device_stop_pending_resets(tmp_adev);
     }
+   amdgpu_unlock_hw_access(adev);
Quoted 5 lines
Quoted 4 lines
Quoted 1 line

skip_hw_reset:

Don't add helpers for that, especially not with that name.

We don't block HW access, but just prevent concurrent resets.

Here is taking a different lock than the reset_domain->sem. It is a seperate reset_domain->gpu_sem that is only locked when we will actuall do reset, it is not taken in the skip_hw_reset path.

Exactly that is what you should *not* do. Please don't add any new lock to the code. This is already complicated enough.

If you think that adding wrappers for reset lock makes sense then we can probably do that, bot don't add any lock for hw access.

Quoted 15 lines
Quoted 15 lines
Quoted 4 lines
     uint32_t seq;
drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c +0 -1
-   if (!adev->gmc.flush_pasid_uses_kiq || !ring->sched.ready ||

drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c int amdgpu_gmc_flush_gpu_tlb_pasid(struct amdgpu_device *adev, uint16_t pasid, · line 698

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c +8 -1
-       !down_read_trylock(&adev->reset_domain->sem)) {
+   /*
+   * A GPU reset should flush all TLBs anyway, so no need to do
+   * this while one is ongoing.
+   */
+   if (!amdgpu_begin_hw_access(adev))
+           return 0;
 
+   if (!adev->gmc.flush_pasid_uses_kiq || !ring->sched.ready ||
+       amdgpu_in_reset(adev)) {

That check doesn't makes sense now any more.

Quoted 8 lines
Quoted 1 line

same here, the two checks are for different scope, although I wasn't sure if the original check is correct or not, is there a possibility that !adev->gmc.flush_pasid_uses_kiq and !ring->sched.ready are false but amdgpu_in_reset(adev) is true? and to we want to take this branch when that happens?

amdgpu_in_reset() in used incorrect in quite a lot of places. It should only be used inside the HW backend code to distinct between initial load and reset.

All other use cases especially the ones in the IOCTL front end functions as well as here in the midlayer which isn't used by GPU reset are incorrect.

FWIW, I started to clean this up earlier this year, but never got around to finishing up the patches:
https://lists.freedesktop.org/archives/amd-gfx/2024-February/104582.html

Alex

Quoted 28 lines
Quoted 21 lines
Quoted 10 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c int amdgpu_gmc_flush_gpu_tlb_pasid(struct amdgpu_device *adev, uint16_t pasid, · line 687

Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c +1 -1
@@ -684,12 +684,18 @@ int amdgpu_gmc_flush_gpu_tlb_pasid(struct amdgpu_device *adev, uint16_t pasid,
     struct amdgpu_ring *ring = &adev->gfx.kiq[inst].ring;
     struct amdgpu_kiq *kiq = &adev->gfx.kiq[inst];
     unsigned int ndw;
-   signed long r;
+   signed long r = 0;

Please don't initialize local variables if it isn't necessary.

Quoted 11 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c int amdgpu_gmc_flush_gpu_tlb_pasid(struct amdgpu_device *adev, uint16_t pasid, · line 712

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c +1 -2
             if (adev->gmc.flush_tlb_needs_extra_type_2)
                     adev->gmc.gmc_funcs->flush_gpu_tlb_pasid(adev, pasid,
                                                              2, all_hub,
@@ -703,46 +709,42 @@ int amdgpu_gmc_flush_gpu_tlb_pasid(struct amdgpu_device *adev, uint16_t pasid,
             adev->gmc.gmc_funcs->flush_gpu_tlb_pasid(adev, pasid,
                                                      flush_type, all_hub,
                                                      inst);
-           return 0;
-   }
+   } else {

That the locking is missing here should be a separate bug fix independent of other changes.

Quoted 1 line

I will split this off into a seperate patch, initializing r is needed because I consolidated the return paths to drop the read lock.

In that case better set r when it's not initialized in some path.

Regards,
Christian.