Skip to contentSkip to conversation

drm/amdgpu: protect kiq overrun

PATCH In review Yintian Tao 4 participants 7 messages Apr 22, 2020 – Apr 23, 2020

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

PATCH 1/1 drm/amdgpu: protect kiq overrun

Wait for the oldest sequence on the kiq ring
to be signaled in order to make sure there
will be no kiq overrun.

v2: remove unused the variable and correct
kiq max_sub_num value

---
 .../drm/amd/amdgpu/amdgpu_amdkfd_gfx_v10.c    |  6 ++++
 .../gpu/drm/amd/amdgpu/amdgpu_amdkfd_gfx_v9.c |  6 ++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c       | 30 +++++++++++++++++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.h       |  3 ++
 drivers/gpu/drm/amd/amdgpu/amdgpu_virt.c      |  6 ++++
 drivers/gpu/drm/amd/amdgpu/gfx_v9_0.c         |  6 ++++
 drivers/gpu/drm/amd/amdgpu/gmc_v10_0.c        |  7 +++++
 drivers/gpu/drm/amd/amdgpu/gmc_v9_0.c         |  7 +++++
 8 files changed, 71 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gfx_v10.c +6 -0
index 691c89705bcd..fac8b9713dfc 100644
@@ -325,6 +325,12 @@ static int kgd_hiq_mqd_load(struct kgd_dev *kgd, void *mqd,
 		 mec, pipe, queue_id);
 
 	spin_lock(&adev->gfx.kiq.ring_lock);
+	r = amdgpu_gfx_kiq_is_avail(&adev->gfx.kiq);
+	if (r) {
+		pr_err("critical bug! too many kiq submission\n");
+		goto out_unlock;
+	}
+
 	r = amdgpu_ring_alloc(kiq_ring, 7);
 	if (r) {
 		pr_err("Failed to alloc KIQ (%d).\n", r);
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gfx_v9.c +6 -0
index df841c2ac5e7..fd42c126510f 100644
@@ -323,6 +323,12 @@ int kgd_gfx_v9_hiq_mqd_load(struct kgd_dev *kgd, void *mqd,
 		 mec, pipe, queue_id);
 
 	spin_lock(&adev->gfx.kiq.ring_lock);
+	r = amdgpu_gfx_kiq_is_avail(&adev->gfx.kiq);
+	if (r) {
+		pr_err("critical bug! too many kiq submissions\n");
+		goto out_unlock;
+	}
+
 	r = amdgpu_ring_alloc(kiq_ring, 7);
 	if (r) {
 		pr_err("Failed to alloc KIQ (%d).\n", r);
drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c +30 -0
index a721b0e0ff69..84e66c45df37 100644
@@ -321,6 +321,9 @@ int amdgpu_gfx_kiq_init_ring(struct amdgpu_device *adev,
 			     AMDGPU_RING_PRIO_DEFAULT);
 	if (r)
 		dev_warn(adev->dev, "(%d) failed to init kiq ring\n", r);
+	else
+		kiq->max_sub_num = (ring->ring_size / 4) /
+				(ring->funcs->align_mask + 1);
 
 	return r;
 }
@@ -663,6 +666,21 @@ int amdgpu_gfx_cp_ecc_error_irq(struct amdgpu_device *adev,
 	return 0;
 }
 
+int amdgpu_gfx_kiq_is_avail(struct amdgpu_kiq *kiq)
+{
+	uint32_t seq = 0;
+	signed long r = 0;
+
+	seq = abs(kiq->ring.fence_drv.sync_seq - kiq->max_sub_num);
+	if (seq > kiq->max_sub_num) {
+		r = amdgpu_fence_wait_polling(&kiq->ring, seq,
+					      MAX_KIQ_REG_WAIT);
+		return r < 1 ? -ETIME : 0;
+	}
+
+	return 0;
+}
+
 uint32_t amdgpu_kiq_rreg(struct amdgpu_device *adev, uint32_t reg)
 {
 	signed long r, cnt = 0;
@@ -674,6 +692,12 @@ uint32_t amdgpu_kiq_rreg(struct amdgpu_device *adev, uint32_t reg)
 	BUG_ON(!ring->funcs->emit_rreg);
 
 	spin_lock_irqsave(&kiq->ring_lock, flags);
+	r = amdgpu_gfx_kiq_is_avail(&adev->gfx.kiq);
+	if (r) {
+		spin_unlock_irqrestore(&kiq->ring_lock, flags);
+		goto failed_kiq_read;
+	}
+
 	if (amdgpu_device_wb_get(adev, &reg_val_offs)) {
 		spin_unlock_irqrestore(&kiq->ring_lock, flags);
 		pr_err("critical bug! too many kiq readers\n");
@@ -728,6 +752,12 @@ void amdgpu_kiq_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v)
 	BUG_ON(!ring->funcs->emit_wreg);
 
 	spin_lock_irqsave(&kiq->ring_lock, flags);
+	r = amdgpu_gfx_kiq_is_avail(&adev->gfx.kiq);
+	if (r) {
+		spin_unlock_irqrestore(&kiq->ring_lock, flags);
+		goto failed_kiq_write;
+	}
+
 	amdgpu_ring_alloc(ring, 32);
 	amdgpu_ring_emit_wreg(ring, reg, v);
 	amdgpu_fence_emit_polling(ring, &seq);
drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.h +3 -0
index ee698f0246d8..1ee59a927bd9 100644
@@ -103,6 +103,7 @@ struct amdgpu_kiq {
 	struct amdgpu_ring	ring;
 	struct amdgpu_irq_src	irq;
 	const struct kiq_pm4_funcs *pmf;
+	uint32_t		max_sub_num;
 };
 
 /*
@@ -387,4 +388,6 @@ int amdgpu_gfx_cp_ecc_error_irq(struct amdgpu_device *adev,
 				  struct amdgpu_iv_entry *entry);
 uint32_t amdgpu_kiq_rreg(struct amdgpu_device *adev, uint32_t reg);
 void amdgpu_kiq_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v);
+
+int amdgpu_gfx_kiq_is_avail(struct amdgpu_kiq *kiq);
 #endif
drivers/gpu/drm/amd/amdgpu/amdgpu_virt.c +6 -0
index 8c10084f44ef..4b027006d072 100644
@@ -57,6 +57,12 @@ void amdgpu_virt_kiq_reg_write_reg_wait(struct amdgpu_device *adev,
 	uint32_t seq;
 
 	spin_lock_irqsave(&kiq->ring_lock, flags);
+	r = amdgpu_gfx_kiq_is_avail(&adev->gfx.kiq);
+	if (r) {
+		spin_unlock_irqrestore(&kiq->ring_lock, flags);
+		goto failed_kiq;
+	}
+
 	amdgpu_ring_alloc(ring, 32);
 	amdgpu_ring_emit_reg_write_reg_wait(ring, reg0, reg1,
 					    ref, mask);
drivers/gpu/drm/amd/amdgpu/gfx_v9_0.c +6 -0
index 5b1549f167b0..a136e2229f7a 100644
@@ -4051,6 +4051,12 @@ static uint64_t gfx_v9_0_kiq_read_clock(struct amdgpu_device *adev)
 	BUG_ON(!ring->funcs->emit_rreg);
 
 	spin_lock_irqsave(&kiq->ring_lock, flags);
+	r = amdgpu_gfx_kiq_is_avail(&adev->gfx.kiq);
+	if (r) {
+		spin_unlock_irqrestore(&kiq->ring_lock, flags);
+		goto failed_kiq_read;
+	}
+
 	if (amdgpu_device_wb_get(adev, &reg_val_offs)) {
 		spin_unlock_irqrestore(&kiq->ring_lock, flags);
 		pr_err("critical bug! too many kiq readers\n");
drivers/gpu/drm/amd/amdgpu/gmc_v10_0.c +7 -0
index 30b75d79efdb..77d8bc9c0111 100644
@@ -423,6 +423,13 @@ static int gmc_v10_0_flush_gpu_tlb_pasid(struct amdgpu_device *adev,
 
 	if (amdgpu_emu_mode == 0 && ring->sched.ready) {
 		spin_lock(&adev->gfx.kiq.ring_lock);
+		r = amdgpu_gfx_kiq_is_avail(&adev->gfx.kiq);
+		if (r) {
+			spin_unlock(&kiq->ring_lock);
+			DRM_ERROR("too many kiq submissions\n");
+			return -ETIME;
+		}
+
 		/* 2 dwords flush + 8 dwords fence */
 		amdgpu_ring_alloc(ring, kiq->pmf->invalidate_tlbs_size + 8);
 		kiq->pmf->kiq_invalidate_tlbs(ring,
drivers/gpu/drm/amd/amdgpu/gmc_v9_0.c +7 -0
index fecdbc471983..c429a2a5fe3d 100644
@@ -614,6 +614,13 @@ static int gmc_v9_0_flush_gpu_tlb_pasid(struct amdgpu_device *adev,
 			ndw += kiq->pmf->invalidate_tlbs_size;
 
 		spin_lock(&adev->gfx.kiq.ring_lock);
+		r = amdgpu_gfx_kiq_is_avail(&adev->gfx.kiq);
+		if (r) {
+			spin_unlock(&kiq->ring_lock);
+			DRM_ERROR("critical bug! too many kiq submissions\n");
+			return -ETIME;
+		}
+
 		/* 2 dwords flush + 8 dwords fence */
 		amdgpu_ring_alloc(ring, ndw);
 		if (vega20_xgmi_wa)
Signature · 6 lines
2.17.1

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx
#1 lore raw

[AMD Official Use Only - Internal Distribution Only]

I think each kiq operation will call ring_alloc for the package space , why not just check whether this allocation is succeed or not ?

Shaoyun.liu

-----Original Message-----
From: amd-gfx <[email protected]> On Behalf Of Yintian Tao
Sent: Wednesday, April 22, 2020 10:50 AM
To: Koenig, Christian <[email protected]>; Liu, Monk <[email protected]>; Kuehling, Felix <[email protected]>
Cc: [email protected]; Tao, Yintian <[email protected]>
Subject: [PATCH] drm/amdgpu: protect kiq overrun

Wait for the oldest sequence on the kiq ring to be signaled in order to make sure there will be no kiq overrun.

v2: remove unused the variable and correct
kiq max_sub_num value

---
 .../drm/amd/amdgpu/amdgpu_amdkfd_gfx_v10.c    |  6 ++++
 .../gpu/drm/amd/amdgpu/amdgpu_amdkfd_gfx_v9.c |  6 ++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c       | 30 +++++++++++++++++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.h       |  3 ++
 drivers/gpu/drm/amd/amdgpu/amdgpu_virt.c      |  6 ++++
 drivers/gpu/drm/amd/amdgpu/gfx_v9_0.c         |  6 ++++
 drivers/gpu/drm/amd/amdgpu/gmc_v10_0.c        |  7 +++++
 drivers/gpu/drm/amd/amdgpu/gmc_v9_0.c         |  7 +++++
 8 files changed, 71 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gfx_v10.c +6 -0
index 691c89705bcd..fac8b9713dfc 100644
@@ -325,6 +325,12 @@ static int kgd_hiq_mqd_load(struct kgd_dev *kgd, void *mqd,
 		 mec, pipe, queue_id);
 
 	spin_lock(&adev->gfx.kiq.ring_lock);
+	r = amdgpu_gfx_kiq_is_avail(&adev->gfx.kiq);
+	if (r) {
+		pr_err("critical bug! too many kiq submission\n");
+		goto out_unlock;
+	}
+
 	r = amdgpu_ring_alloc(kiq_ring, 7);
 	if (r) {
 		pr_err("Failed to alloc KIQ (%d).\n", r); diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gfx_v9.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gfx_v9.c

index df841c2ac5e7..fd42c126510f 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gfx_v9.c +6 -0
@@ -323,6 +323,12 @@ int kgd_gfx_v9_hiq_mqd_load(struct kgd_dev *kgd, void *mqd,
 		 mec, pipe, queue_id);
 
 	spin_lock(&adev->gfx.kiq.ring_lock);
+	r = amdgpu_gfx_kiq_is_avail(&adev->gfx.kiq);
+	if (r) {
+		pr_err("critical bug! too many kiq submissions\n");
+		goto out_unlock;
+	}
+
 	r = amdgpu_ring_alloc(kiq_ring, 7);
 	if (r) {
 		pr_err("Failed to alloc KIQ (%d).\n", r); diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c

index a721b0e0ff69..84e66c45df37 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c +23 -0
@@ -321,6 +321,9 @@ int amdgpu_gfx_kiq_init_ring(struct amdgpu_device *adev,
 			     AMDGPU_RING_PRIO_DEFAULT);
 	if (r)
 		dev_warn(adev->dev, "(%d) failed to init kiq ring\n", r);
+	else
+		kiq->max_sub_num = (ring->ring_size / 4) /
+				(ring->funcs->align_mask + 1);
 
 	return r;
 }
@@ -663,6 +666,21 @@ int amdgpu_gfx_cp_ecc_error_irq(struct amdgpu_device *adev,
 	return 0;
 }
 
+int amdgpu_gfx_kiq_is_avail(struct amdgpu_kiq *kiq) {
+	uint32_t seq = 0;
+	signed long r = 0;
+
+	seq = abs(kiq->ring.fence_drv.sync_seq - kiq->max_sub_num);
+	if (seq > kiq->max_sub_num) {
+		r = amdgpu_fence_wait_polling(&kiq->ring, seq,
+					      MAX_KIQ_REG_WAIT);
+		return r < 1 ? -ETIME : 0;
+	}
+
+	return 0;
+}
+
 uint32_t amdgpu_kiq_rreg(struct amdgpu_device *adev, uint32_t reg)  {
 	signed long r, cnt = 0;
@@ -674,6 +692,12 @@ uint32_t amdgpu_kiq_rreg(struct amdgpu_device *adev, uint32_t reg)
 	BUG_ON(!ring->funcs->emit_rreg);
 
 	spin_lock_irqsave(&kiq->ring_lock, flags);
+	r = amdgpu_gfx_kiq_is_avail(&adev->gfx.kiq);
+	if (r) {
+		spin_unlock_irqrestore(&kiq->ring_lock, flags);
+		goto failed_kiq_read;
+	}
+
 	if (amdgpu_device_wb_get(adev, &reg_val_offs)) {
 		spin_unlock_irqrestore(&kiq->ring_lock, flags);
 		pr_err("critical bug! too many kiq readers\n"); @@ -728,6 +752,12 @@ void amdgpu_kiq_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v)

BUG_ON(!ring->funcs->emit_wreg);

spin_lock_irqsave(&kiq->ring_lock, flags);
+ r = amdgpu_gfx_kiq_is_avail(&adev->gfx.kiq);
+ if (r) {
+ spin_unlock_irqrestore(&kiq->ring_lock, flags);
+ goto failed_kiq_write;
+ }
+
amdgpu_ring_alloc(ring, 32);
amdgpu_ring_emit_wreg(ring, reg, v);
amdgpu_fence_emit_polling(ring, &seq); diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.h b/drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.h
index ee698f0246d8..1ee59a927bd9 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.h +3 -0
@@ -103,6 +103,7 @@ struct amdgpu_kiq {
 	struct amdgpu_ring	ring;
 	struct amdgpu_irq_src	irq;
 	const struct kiq_pm4_funcs *pmf;
+	uint32_t		max_sub_num;
 };
 
 /*
@@ -387,4 +388,6 @@ int amdgpu_gfx_cp_ecc_error_irq(struct amdgpu_device *adev,
 				  struct amdgpu_iv_entry *entry);
 uint32_t amdgpu_kiq_rreg(struct amdgpu_device *adev, uint32_t reg);  void amdgpu_kiq_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v);
+
+int amdgpu_gfx_kiq_is_avail(struct amdgpu_kiq *kiq);
 #endif
drivers/gpu/drm/amd/amdgpu/amdgpu_virt.c +6 -0
index 8c10084f44ef..4b027006d072 100644
@@ -57,6 +57,12 @@ void amdgpu_virt_kiq_reg_write_reg_wait(struct amdgpu_device *adev,
 	uint32_t seq;
 
 	spin_lock_irqsave(&kiq->ring_lock, flags);
+	r = amdgpu_gfx_kiq_is_avail(&adev->gfx.kiq);
+	if (r) {
+		spin_unlock_irqrestore(&kiq->ring_lock, flags);
+		goto failed_kiq;
+	}
+
 	amdgpu_ring_alloc(ring, 32);
 	amdgpu_ring_emit_reg_write_reg_wait(ring, reg0, reg1,
 					    ref, mask);
drivers/gpu/drm/amd/amdgpu/gfx_v9_0.c +6 -0
index 5b1549f167b0..a136e2229f7a 100644
@@ -4051,6 +4051,12 @@ static uint64_t gfx_v9_0_kiq_read_clock(struct amdgpu_device *adev)
 	BUG_ON(!ring->funcs->emit_rreg);
 
 	spin_lock_irqsave(&kiq->ring_lock, flags);
+	r = amdgpu_gfx_kiq_is_avail(&adev->gfx.kiq);
+	if (r) {
+		spin_unlock_irqrestore(&kiq->ring_lock, flags);
+		goto failed_kiq_read;
+	}
+
 	if (amdgpu_device_wb_get(adev, &reg_val_offs)) {
 		spin_unlock_irqrestore(&kiq->ring_lock, flags);
 		pr_err("critical bug! too many kiq readers\n"); diff --git a/drivers/gpu/drm/amd/amdgpu/gmc_v10_0.c b/drivers/gpu/drm/amd/amdgpu/gmc_v10_0.c

index 30b75d79efdb..77d8bc9c0111 100644

drivers/gpu/drm/amd/amdgpu/gmc_v10_0.c +7 -0
@@ -423,6 +423,13 @@ static int gmc_v10_0_flush_gpu_tlb_pasid(struct amdgpu_device *adev,
 
 	if (amdgpu_emu_mode == 0 && ring->sched.ready) {
 		spin_lock(&adev->gfx.kiq.ring_lock);
+		r = amdgpu_gfx_kiq_is_avail(&adev->gfx.kiq);
+		if (r) {
+			spin_unlock(&kiq->ring_lock);
+			DRM_ERROR("too many kiq submissions\n");
+			return -ETIME;
+		}
+
 		/* 2 dwords flush + 8 dwords fence */
 		amdgpu_ring_alloc(ring, kiq->pmf->invalidate_tlbs_size + 8);
 		kiq->pmf->kiq_invalidate_tlbs(ring,
drivers/gpu/drm/amd/amdgpu/gmc_v9_0.c +7 -0
index fecdbc471983..c429a2a5fe3d 100644
@@ -614,6 +614,13 @@ static int gmc_v9_0_flush_gpu_tlb_pasid(struct amdgpu_device *adev,
 			ndw += kiq->pmf->invalidate_tlbs_size;
 
 		spin_lock(&adev->gfx.kiq.ring_lock);
+		r = amdgpu_gfx_kiq_is_avail(&adev->gfx.kiq);
+		if (r) {
+			spin_unlock(&kiq->ring_lock);
+			DRM_ERROR("critical bug! too many kiq submissions\n");
+			return -ETIME;
+		}
+
 		/* 2 dwords flush + 8 dwords fence */
 		amdgpu_ring_alloc(ring, ndw);
 		if (vega20_xgmi_wa)

--
2.17.1

_______________________________________________
amd-gfx mailing list
[email protected]
https://nam11.safelinks.protection.outlook.com/?url=https%3A%2F%2Flists.freedesktop.org%2Fmailman%2Flistinfo%2Famd-gfx&amp;data=02%7C01%7CShaoyun.Liu%40amd.com%7C0cd26307c0a149ebe0bd08d7e6cc84b1%7C3dd8961fe4884e608e11a82d994e183d%7C0%7C0%7C637231638419034830&amp;sdata=rxkhyMutFwbe5Nw%2BeBGiESW9wTdflDUo%2F4xEvbCbR6U%3D&amp;reserved=0
_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

maintainer
#2 lore raw

The amdgpu_ring_alloc() function checks if the requested number of DW don't exceed the maximum submission size.

It does NOT check if there is enough room on the ring. That would require MMIO access and that is what we want to avoid.

Regards,
Christian.

Am 22.04.20 um 16:54 schrieb Liu, Shaoyun:

Quoted 210 lines

[AMD Official Use Only - Internal Distribution Only]

I think each kiq operation will call ring_alloc for the package space , why not just check whether this allocation is succeed or not ?

Shaoyun.liu

-----Original Message-----
From: amd-gfx <[email protected]> On Behalf Of Yintian Tao
Sent: Wednesday, April 22, 2020 10:50 AM
To: Koenig, Christian <[email protected]>; Liu, Monk <[email protected]>; Kuehling, Felix <[email protected]>
Cc: [email protected]; Tao, Yintian <[email protected]>
Subject: [PATCH] drm/amdgpu: protect kiq overrun

Wait for the oldest sequence on the kiq ring to be signaled in order to make sure there will be no kiq overrun.

v2: remove unused the variable and correct
kiq max_sub_num value

---
  .../drm/amd/amdgpu/amdgpu_amdkfd_gfx_v10.c    |  6 ++++
  .../gpu/drm/amd/amdgpu/amdgpu_amdkfd_gfx_v9.c |  6 ++++
  drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c       | 30 +++++++++++++++++++
  drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.h       |  3 ++
  drivers/gpu/drm/amd/amdgpu/amdgpu_virt.c      |  6 ++++
  drivers/gpu/drm/amd/amdgpu/gfx_v9_0.c         |  6 ++++
  drivers/gpu/drm/amd/amdgpu/gmc_v10_0.c        |  7 +++++
  drivers/gpu/drm/amd/amdgpu/gmc_v9_0.c         |  7 +++++
  8 files changed, 71 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gfx_v10.c +6 -0
index 691c89705bcd..fac8b9713dfc 100644
@@ -325,6 +325,12 @@ static int kgd_hiq_mqd_load(struct kgd_dev *kgd, void *mqd,
  		 mec, pipe, queue_id);
 
  	spin_lock(&adev->gfx.kiq.ring_lock);
+	r = amdgpu_gfx_kiq_is_avail(&adev->gfx.kiq);
+	if (r) {
+		pr_err("critical bug! too many kiq submission\n");
+		goto out_unlock;
+	}
+
  	r = amdgpu_ring_alloc(kiq_ring, 7);
  	if (r) {
  		pr_err("Failed to alloc KIQ (%d).\n", r); diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gfx_v9.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gfx_v9.c

index df841c2ac5e7..fd42c126510f 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gfx_v9.c +6 -0
@@ -323,6 +323,12 @@ int kgd_gfx_v9_hiq_mqd_load(struct kgd_dev *kgd, void *mqd,
  		 mec, pipe, queue_id);
 
  	spin_lock(&adev->gfx.kiq.ring_lock);
+	r = amdgpu_gfx_kiq_is_avail(&adev->gfx.kiq);
+	if (r) {
+		pr_err("critical bug! too many kiq submissions\n");
+		goto out_unlock;
+	}
+
  	r = amdgpu_ring_alloc(kiq_ring, 7);
  	if (r) {
  		pr_err("Failed to alloc KIQ (%d).\n", r); diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c

index a721b0e0ff69..84e66c45df37 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c +23 -0
@@ -321,6 +321,9 @@ int amdgpu_gfx_kiq_init_ring(struct amdgpu_device *adev,
  			     AMDGPU_RING_PRIO_DEFAULT);
  	if (r)
  		dev_warn(adev->dev, "(%d) failed to init kiq ring\n", r);
+	else
+		kiq->max_sub_num = (ring->ring_size / 4) /
+				(ring->funcs->align_mask + 1);
 
  	return r;
  }
@@ -663,6 +666,21 @@ int amdgpu_gfx_cp_ecc_error_irq(struct amdgpu_device *adev,
  	return 0;
  }
 
+int amdgpu_gfx_kiq_is_avail(struct amdgpu_kiq *kiq) {
+	uint32_t seq = 0;
+	signed long r = 0;
+
+	seq = abs(kiq->ring.fence_drv.sync_seq - kiq->max_sub_num);
+	if (seq > kiq->max_sub_num) {
+		r = amdgpu_fence_wait_polling(&kiq->ring, seq,
+					      MAX_KIQ_REG_WAIT);
+		return r < 1 ? -ETIME : 0;
+	}
+
+	return 0;
+}
+
  uint32_t amdgpu_kiq_rreg(struct amdgpu_device *adev, uint32_t reg)  {
  	signed long r, cnt = 0;
@@ -674,6 +692,12 @@ uint32_t amdgpu_kiq_rreg(struct amdgpu_device *adev, uint32_t reg)
  	BUG_ON(!ring->funcs->emit_rreg);
 
  	spin_lock_irqsave(&kiq->ring_lock, flags);
+	r = amdgpu_gfx_kiq_is_avail(&adev->gfx.kiq);
+	if (r) {
+		spin_unlock_irqrestore(&kiq->ring_lock, flags);
+		goto failed_kiq_read;
+	}
+
  	if (amdgpu_device_wb_get(adev, &reg_val_offs)) {
  		spin_unlock_irqrestore(&kiq->ring_lock, flags);
  		pr_err("critical bug! too many kiq readers\n"); @@ -728,6 +752,12 @@ void amdgpu_kiq_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v)
  	BUG_ON(!ring->funcs->emit_wreg);
  
  	spin_lock_irqsave(&kiq->ring_lock, flags);
drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c +6 -0
+	r = amdgpu_gfx_kiq_is_avail(&adev->gfx.kiq);
+	if (r) {
+		spin_unlock_irqrestore(&kiq->ring_lock, flags);
+		goto failed_kiq_write;
+	}
+
  	amdgpu_ring_alloc(ring, 32);
  	amdgpu_ring_emit_wreg(ring, reg, v);
  	amdgpu_fence_emit_polling(ring, &seq); diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.h b/drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.h

index ee698f0246d8..1ee59a927bd9 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.h +3 -0
@@ -103,6 +103,7 @@ struct amdgpu_kiq {
  	struct amdgpu_ring	ring;
  	struct amdgpu_irq_src	irq;
  	const struct kiq_pm4_funcs *pmf;
+	uint32_t		max_sub_num;
  };
 
  /*
@@ -387,4 +388,6 @@ int amdgpu_gfx_cp_ecc_error_irq(struct amdgpu_device *adev,
  				  struct amdgpu_iv_entry *entry);
  uint32_t amdgpu_kiq_rreg(struct amdgpu_device *adev, uint32_t reg);  void amdgpu_kiq_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v);
+
+int amdgpu_gfx_kiq_is_avail(struct amdgpu_kiq *kiq);
  #endif
drivers/gpu/drm/amd/amdgpu/amdgpu_virt.c +6 -0
index 8c10084f44ef..4b027006d072 100644
@@ -57,6 +57,12 @@ void amdgpu_virt_kiq_reg_write_reg_wait(struct amdgpu_device *adev,
  	uint32_t seq;
 
  	spin_lock_irqsave(&kiq->ring_lock, flags);
+	r = amdgpu_gfx_kiq_is_avail(&adev->gfx.kiq);
+	if (r) {
+		spin_unlock_irqrestore(&kiq->ring_lock, flags);
+		goto failed_kiq;
+	}
+
  	amdgpu_ring_alloc(ring, 32);
  	amdgpu_ring_emit_reg_write_reg_wait(ring, reg0, reg1,
  					    ref, mask);
drivers/gpu/drm/amd/amdgpu/gfx_v9_0.c +6 -0
index 5b1549f167b0..a136e2229f7a 100644
@@ -4051,6 +4051,12 @@ static uint64_t gfx_v9_0_kiq_read_clock(struct amdgpu_device *adev)
  	BUG_ON(!ring->funcs->emit_rreg);
 
  	spin_lock_irqsave(&kiq->ring_lock, flags);
+	r = amdgpu_gfx_kiq_is_avail(&adev->gfx.kiq);
+	if (r) {
+		spin_unlock_irqrestore(&kiq->ring_lock, flags);
+		goto failed_kiq_read;
+	}
+
  	if (amdgpu_device_wb_get(adev, &reg_val_offs)) {
  		spin_unlock_irqrestore(&kiq->ring_lock, flags);
  		pr_err("critical bug! too many kiq readers\n"); diff --git a/drivers/gpu/drm/amd/amdgpu/gmc_v10_0.c b/drivers/gpu/drm/amd/amdgpu/gmc_v10_0.c

index 30b75d79efdb..77d8bc9c0111 100644

drivers/gpu/drm/amd/amdgpu/gmc_v10_0.c +7 -0
@@ -423,6 +423,13 @@ static int gmc_v10_0_flush_gpu_tlb_pasid(struct amdgpu_device *adev,
 
  	if (amdgpu_emu_mode == 0 && ring->sched.ready) {
  		spin_lock(&adev->gfx.kiq.ring_lock);
+		r = amdgpu_gfx_kiq_is_avail(&adev->gfx.kiq);
+		if (r) {
+			spin_unlock(&kiq->ring_lock);
+			DRM_ERROR("too many kiq submissions\n");
+			return -ETIME;
+		}
+
  		/* 2 dwords flush + 8 dwords fence */
  		amdgpu_ring_alloc(ring, kiq->pmf->invalidate_tlbs_size + 8);
  		kiq->pmf->kiq_invalidate_tlbs(ring,
drivers/gpu/drm/amd/amdgpu/gmc_v9_0.c +7 -1
index fecdbc471983..c429a2a5fe3d 100644
@@ -614,6 +614,13 @@ static int gmc_v9_0_flush_gpu_tlb_pasid(struct amdgpu_device *adev,
  			ndw += kiq->pmf->invalidate_tlbs_size;
 
  		spin_lock(&adev->gfx.kiq.ring_lock);
+		r = amdgpu_gfx_kiq_is_avail(&adev->gfx.kiq);
+		if (r) {
+			spin_unlock(&kiq->ring_lock);
+			DRM_ERROR("critical bug! too many kiq submissions\n");
+			return -ETIME;
+		}
+
  		/* 2 dwords flush + 8 dwords fence */
  		amdgpu_ring_alloc(ring, ndw);
  		if (vega20_xgmi_wa)
--

2.17.1

Quoted 4 lines

_______________________________________________
amd-gfx mailing list
[email protected]
https://nam11.safelinks.protection.outlook.com/?url=https%3A%2F%2Flists.freedesktop.org%2Fmailman%2Flistinfo%2Famd-gfx&amp;data=02%7C01%7CShaoyun.Liu%40amd.com%7C0cd26307c0a149ebe0bd08d7e6cc84b1%7C3dd8961fe4884e608e11a82d994e183d%7C0%7C0%7C637231638419034830&amp;sdata=rxkhyMutFwbe5Nw%2BeBGiESW9wTdflDUo%2F4xEvbCbR6U%3D&amp;reserved=0

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

#3 lore raw

[AMD Official Use Only - Internal Distribution Only]

I always has an impression for each submission, once the ring be allocated , before the fence be signed , this ring space will always be reserved . If this can not be guaranteed , it sound a big issue to me . Can't we check the rptr write back to determine the available room in the ring_alloc ?

Regards
Shaoyun.liu
-----Original Message-----
From: Koenig, Christian <[email protected]>
Sent: Wednesday, April 22, 2020 10:57 AM
To: Liu, Shaoyun <[email protected]>; Tao, Yintian <[email protected]>; Liu, Monk <[email protected]>; Kuehling, Felix <[email protected]>
Cc: [email protected]
Subject: Re: [PATCH] drm/amdgpu: protect kiq overrun

The amdgpu_ring_alloc() function checks if the requested number of DW don't exceed the maximum submission size.

It does NOT check if there is enough room on the ring. That would require MMIO access and that is what we want to avoid.

Regards,
Christian.

Am 22.04.20 um 16:54 schrieb Liu, Shaoyun:

Quoted 227 lines

[AMD Official Use Only - Internal Distribution Only]

I think each kiq operation will call ring_alloc for the package space , why not just check whether this allocation is succeed or not ?

Shaoyun.liu

-----Original Message-----
From: amd-gfx <[email protected]> On Behalf Of Yintian Tao
Sent: Wednesday, April 22, 2020 10:50 AM
To: Koenig, Christian <[email protected]>; Liu, Monk <[email protected]>; Kuehling, Felix <[email protected]>
Cc: [email protected]; Tao, Yintian <[email protected]>
Subject: [PATCH] drm/amdgpu: protect kiq overrun

Wait for the oldest sequence on the kiq ring to be signaled in order to make sure there will be no kiq overrun.

v2: remove unused the variable and correct
kiq max_sub_num value

---
  .../drm/amd/amdgpu/amdgpu_amdkfd_gfx_v10.c    |  6 ++++
  .../gpu/drm/amd/amdgpu/amdgpu_amdkfd_gfx_v9.c |  6 ++++
  drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c       | 30 +++++++++++++++++++
  drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.h       |  3 ++
  drivers/gpu/drm/amd/amdgpu/amdgpu_virt.c      |  6 ++++
  drivers/gpu/drm/amd/amdgpu/gfx_v9_0.c         |  6 ++++
  drivers/gpu/drm/amd/amdgpu/gmc_v10_0.c        |  7 +++++
  drivers/gpu/drm/amd/amdgpu/gmc_v9_0.c         |  7 +++++
  8 files changed, 71 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gfx_v10.c +6 -0
index 691c89705bcd..fac8b9713dfc 100644
@@ -325,6 +325,12 @@ static int kgd_hiq_mqd_load(struct kgd_dev *kgd, void *mqd,
  		 mec, pipe, queue_id);
 
  	spin_lock(&adev->gfx.kiq.ring_lock);
+	r = amdgpu_gfx_kiq_is_avail(&adev->gfx.kiq);
+	if (r) {
+		pr_err("critical bug! too many kiq submission\n");
+		goto out_unlock;
+	}
+
  	r = amdgpu_ring_alloc(kiq_ring, 7);
  	if (r) {
  		pr_err("Failed to alloc KIQ (%d).\n", r); diff --git 

a/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gfx_v9.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gfx_v9.c index df841c2ac5e7..fd42c126510f 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gfx_v9.c +6 -0
@@ -323,6 +323,12 @@ int kgd_gfx_v9_hiq_mqd_load(struct kgd_dev *kgd, void *mqd,
  		 mec, pipe, queue_id);
 
  	spin_lock(&adev->gfx.kiq.ring_lock);
+	r = amdgpu_gfx_kiq_is_avail(&adev->gfx.kiq);
+	if (r) {
+		pr_err("critical bug! too many kiq submissions\n");
+		goto out_unlock;
+	}
+
  	r = amdgpu_ring_alloc(kiq_ring, 7);
  	if (r) {
  		pr_err("Failed to alloc KIQ (%d).\n", r); diff --git 

a/drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c
b/drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c
index a721b0e0ff69..84e66c45df37 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c +23 -0
@@ -321,6 +321,9 @@ int amdgpu_gfx_kiq_init_ring(struct amdgpu_device *adev,
  			     AMDGPU_RING_PRIO_DEFAULT);
  	if (r)
  		dev_warn(adev->dev, "(%d) failed to init kiq ring\n", r);
+	else
+		kiq->max_sub_num = (ring->ring_size / 4) /
+				(ring->funcs->align_mask + 1);
 
  	return r;
  }
@@ -663,6 +666,21 @@ int amdgpu_gfx_cp_ecc_error_irq(struct amdgpu_device *adev,
  	return 0;
  }
 
+int amdgpu_gfx_kiq_is_avail(struct amdgpu_kiq *kiq) {
+	uint32_t seq = 0;
+	signed long r = 0;
+
+	seq = abs(kiq->ring.fence_drv.sync_seq - kiq->max_sub_num);
+	if (seq > kiq->max_sub_num) {
+		r = amdgpu_fence_wait_polling(&kiq->ring, seq,
+					      MAX_KIQ_REG_WAIT);
+		return r < 1 ? -ETIME : 0;
+	}
+
+	return 0;
+}
+
  uint32_t amdgpu_kiq_rreg(struct amdgpu_device *adev, uint32_t reg)  {
  	signed long r, cnt = 0;
@@ -674,6 +692,12 @@ uint32_t amdgpu_kiq_rreg(struct amdgpu_device *adev, uint32_t reg)
  	BUG_ON(!ring->funcs->emit_rreg);
 
  	spin_lock_irqsave(&kiq->ring_lock, flags);
+	r = amdgpu_gfx_kiq_is_avail(&adev->gfx.kiq);
+	if (r) {
+		spin_unlock_irqrestore(&kiq->ring_lock, flags);
+		goto failed_kiq_read;
+	}
+
  	if (amdgpu_device_wb_get(adev, &reg_val_offs)) {
  		spin_unlock_irqrestore(&kiq->ring_lock, flags);
  		pr_err("critical bug! too many kiq readers\n"); @@ -728,6 +752,12 @@ void amdgpu_kiq_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v)
  	BUG_ON(!ring->funcs->emit_wreg);
  
  	spin_lock_irqsave(&kiq->ring_lock, flags);
drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c +6 -0
+	r = amdgpu_gfx_kiq_is_avail(&adev->gfx.kiq);
+	if (r) {
+		spin_unlock_irqrestore(&kiq->ring_lock, flags);
+		goto failed_kiq_write;
+	}
+
  	amdgpu_ring_alloc(ring, 32);
  	amdgpu_ring_emit_wreg(ring, reg, v);
  	amdgpu_fence_emit_polling(ring, &seq); diff --git 

a/drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.h
b/drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.h
index ee698f0246d8..1ee59a927bd9 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.h +3 -0
@@ -103,6 +103,7 @@ struct amdgpu_kiq {
  	struct amdgpu_ring	ring;
  	struct amdgpu_irq_src	irq;
  	const struct kiq_pm4_funcs *pmf;
+	uint32_t		max_sub_num;
  };
 
  /*
@@ -387,4 +388,6 @@ int amdgpu_gfx_cp_ecc_error_irq(struct amdgpu_device *adev,
  				  struct amdgpu_iv_entry *entry);
  uint32_t amdgpu_kiq_rreg(struct amdgpu_device *adev, uint32_t reg);  
 void amdgpu_kiq_wreg(struct amdgpu_device *adev, uint32_t reg, 
 uint32_t v);
+
+int amdgpu_gfx_kiq_is_avail(struct amdgpu_kiq *kiq);
  #endif
drivers/gpu/drm/amd/amdgpu/amdgpu_virt.c +6 -0
index 8c10084f44ef..4b027006d072 100644
@@ -57,6 +57,12 @@ void amdgpu_virt_kiq_reg_write_reg_wait(struct amdgpu_device *adev,
  	uint32_t seq;
 
  	spin_lock_irqsave(&kiq->ring_lock, flags);
+	r = amdgpu_gfx_kiq_is_avail(&adev->gfx.kiq);
+	if (r) {
+		spin_unlock_irqrestore(&kiq->ring_lock, flags);
+		goto failed_kiq;
+	}
+
  	amdgpu_ring_alloc(ring, 32);
  	amdgpu_ring_emit_reg_write_reg_wait(ring, reg0, reg1,
  					    ref, mask);
drivers/gpu/drm/amd/amdgpu/gfx_v9_0.c +6 -0
index 5b1549f167b0..a136e2229f7a 100644
@@ -4051,6 +4051,12 @@ static uint64_t gfx_v9_0_kiq_read_clock(struct amdgpu_device *adev)
  	BUG_ON(!ring->funcs->emit_rreg);
 
  	spin_lock_irqsave(&kiq->ring_lock, flags);
+	r = amdgpu_gfx_kiq_is_avail(&adev->gfx.kiq);
+	if (r) {
+		spin_unlock_irqrestore(&kiq->ring_lock, flags);
+		goto failed_kiq_read;
+	}
+
  	if (amdgpu_device_wb_get(adev, &reg_val_offs)) {
  		spin_unlock_irqrestore(&kiq->ring_lock, flags);
  		pr_err("critical bug! too many kiq readers\n"); diff --git 

a/drivers/gpu/drm/amd/amdgpu/gmc_v10_0.c
b/drivers/gpu/drm/amd/amdgpu/gmc_v10_0.c
index 30b75d79efdb..77d8bc9c0111 100644

drivers/gpu/drm/amd/amdgpu/gmc_v10_0.c +7 -0
@@ -423,6 +423,13 @@ static int gmc_v10_0_flush_gpu_tlb_pasid(struct 
 amdgpu_device *adev,
 
  	if (amdgpu_emu_mode == 0 && ring->sched.ready) {
  		spin_lock(&adev->gfx.kiq.ring_lock);
+		r = amdgpu_gfx_kiq_is_avail(&adev->gfx.kiq);
+		if (r) {
+			spin_unlock(&kiq->ring_lock);
+			DRM_ERROR("too many kiq submissions\n");
+			return -ETIME;
+		}
+
  		/* 2 dwords flush + 8 dwords fence */
  		amdgpu_ring_alloc(ring, kiq->pmf->invalidate_tlbs_size + 8);

kiq->pmf->kiq_invalidate_tlbs(ring,

drivers/gpu/drm/amd/amdgpu/gmc_v9_0.c +7 -1
index fecdbc471983..c429a2a5fe3d 100644
@@ -614,6 +614,13 @@ static int gmc_v9_0_flush_gpu_tlb_pasid(struct amdgpu_device *adev,
  			ndw += kiq->pmf->invalidate_tlbs_size;
 
  		spin_lock(&adev->gfx.kiq.ring_lock);
+		r = amdgpu_gfx_kiq_is_avail(&adev->gfx.kiq);
+		if (r) {
+			spin_unlock(&kiq->ring_lock);
+			DRM_ERROR("critical bug! too many kiq submissions\n");
+			return -ETIME;
+		}
+
  		/* 2 dwords flush + 8 dwords fence */
  		amdgpu_ring_alloc(ring, ndw);
  		if (vega20_xgmi_wa)
--

2.17.1

Quoted 8 lines

_______________________________________________
amd-gfx mailing list
[email protected]
https://nam11.safelinks.protection.outlook.com/?url=https%3A%2F%2Flist
s.freedesktop.org%2Fmailman%2Flistinfo%2Famd-gfx&amp;data=02%7C01%7CSh aoyun.Liu%40amd.com%7C0cd26307c0a149ebe0bd08d7e6cc84b1%7C3dd8961fe4884 e608e11a82d994e183d%7C0%7C0%7C637231638419034830&amp;sdata=rxkhyMutFwb e5Nw%2BeBGiESW9wTdflDUo%2F4xEvbCbR6U%3D&amp;reserved=0

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

maintainer
#4 lore raw
Quoted 1 line

Can't we check the rptr write back to determine the available room in the ring_alloc ?

We removed that a couple of years ago.

One reason for this is that checking the rptr each time is quite a huge overhead since it is an MMIO register.

The other reason is that the rptr is no longer a secure way of figuring out if an engine has processed all data.

Some engines like the CP are pipelined and the rptr is only the beginning of the pipeline, subsequent pipeline steps might read from the ring buffer again.

If you want to figure out the processing status of ring allocations you need to take a look at the fences.

Regards,
Christian.

Am 22.04.20 um 17:43 schrieb Liu, Shaoyun:

Quoted 248 lines

[AMD Official Use Only - Internal Distribution Only]

I always has an impression for each submission, once the ring be allocated , before the fence be signed , this ring space will always be reserved . If this can not be guaranteed , it sound a big issue to me . Can't we check the rptr write back to determine the available room in the ring_alloc ?

Regards
Shaoyun.liu
-----Original Message-----
From: Koenig, Christian <[email protected]>
Sent: Wednesday, April 22, 2020 10:57 AM
To: Liu, Shaoyun <[email protected]>; Tao, Yintian <[email protected]>; Liu, Monk <[email protected]>; Kuehling, Felix <[email protected]>
Cc: [email protected]
Subject: Re: [PATCH] drm/amdgpu: protect kiq overrun

The amdgpu_ring_alloc() function checks if the requested number of DW don't exceed the maximum submission size.

It does NOT check if there is enough room on the ring. That would require MMIO access and that is what we want to avoid.

Regards,
Christian.

Am 22.04.20 um 16:54 schrieb Liu, Shaoyun:

Quoted 227 lines

[AMD Official Use Only - Internal Distribution Only]

I think each kiq operation will call ring_alloc for the package space , why not just check whether this allocation is succeed or not ?

Shaoyun.liu

-----Original Message-----
From: amd-gfx <[email protected]> On Behalf Of Yintian Tao
Sent: Wednesday, April 22, 2020 10:50 AM
To: Koenig, Christian <[email protected]>; Liu, Monk <[email protected]>; Kuehling, Felix <[email protected]>
Cc: [email protected]; Tao, Yintian <[email protected]>
Subject: [PATCH] drm/amdgpu: protect kiq overrun

Wait for the oldest sequence on the kiq ring to be signaled in order to make sure there will be no kiq overrun.

v2: remove unused the variable and correct
kiq max_sub_num value

---
   .../drm/amd/amdgpu/amdgpu_amdkfd_gfx_v10.c    |  6 ++++
   .../gpu/drm/amd/amdgpu/amdgpu_amdkfd_gfx_v9.c |  6 ++++
   drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c       | 30 +++++++++++++++++++
   drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.h       |  3 ++
   drivers/gpu/drm/amd/amdgpu/amdgpu_virt.c      |  6 ++++
   drivers/gpu/drm/amd/amdgpu/gfx_v9_0.c         |  6 ++++
   drivers/gpu/drm/amd/amdgpu/gmc_v10_0.c        |  7 +++++
   drivers/gpu/drm/amd/amdgpu/gmc_v9_0.c         |  7 +++++
   8 files changed, 71 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gfx_v10.c +6 -0
index 691c89705bcd..fac8b9713dfc 100644
@@ -325,6 +325,12 @@ static int kgd_hiq_mqd_load(struct kgd_dev *kgd, void *mqd,
   		 mec, pipe, queue_id);
 
   	spin_lock(&adev->gfx.kiq.ring_lock);
+	r = amdgpu_gfx_kiq_is_avail(&adev->gfx.kiq);
+	if (r) {
+		pr_err("critical bug! too many kiq submission\n");
+		goto out_unlock;
+	}
+
   	r = amdgpu_ring_alloc(kiq_ring, 7);
   	if (r) {
   		pr_err("Failed to alloc KIQ (%d).\n", r); diff --git

a/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gfx_v9.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gfx_v9.c index df841c2ac5e7..fd42c126510f 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gfx_v9.c +6 -0
@@ -323,6 +323,12 @@ int kgd_gfx_v9_hiq_mqd_load(struct kgd_dev *kgd, void *mqd,
   		 mec, pipe, queue_id);
 
   	spin_lock(&adev->gfx.kiq.ring_lock);
+	r = amdgpu_gfx_kiq_is_avail(&adev->gfx.kiq);
+	if (r) {
+		pr_err("critical bug! too many kiq submissions\n");
+		goto out_unlock;
+	}
+
   	r = amdgpu_ring_alloc(kiq_ring, 7);
   	if (r) {
   		pr_err("Failed to alloc KIQ (%d).\n", r); diff --git

a/drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c
b/drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c
index a721b0e0ff69..84e66c45df37 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c +23 -0
@@ -321,6 +321,9 @@ int amdgpu_gfx_kiq_init_ring(struct amdgpu_device *adev,
   			     AMDGPU_RING_PRIO_DEFAULT);
   	if (r)
   		dev_warn(adev->dev, "(%d) failed to init kiq ring\n", r);
+	else
+		kiq->max_sub_num = (ring->ring_size / 4) /
+				(ring->funcs->align_mask + 1);
 
   	return r;
   }
@@ -663,6 +666,21 @@ int amdgpu_gfx_cp_ecc_error_irq(struct amdgpu_device *adev,
   	return 0;
   }
 
+int amdgpu_gfx_kiq_is_avail(struct amdgpu_kiq *kiq) {
+	uint32_t seq = 0;
+	signed long r = 0;
+
+	seq = abs(kiq->ring.fence_drv.sync_seq - kiq->max_sub_num);
+	if (seq > kiq->max_sub_num) {
+		r = amdgpu_fence_wait_polling(&kiq->ring, seq,
+					      MAX_KIQ_REG_WAIT);
+		return r < 1 ? -ETIME : 0;
+	}
+
+	return 0;
+}
+
   uint32_t amdgpu_kiq_rreg(struct amdgpu_device *adev, uint32_t reg)  {
   	signed long r, cnt = 0;
@@ -674,6 +692,12 @@ uint32_t amdgpu_kiq_rreg(struct amdgpu_device *adev, uint32_t reg)
   	BUG_ON(!ring->funcs->emit_rreg);
 
   	spin_lock_irqsave(&kiq->ring_lock, flags);
+	r = amdgpu_gfx_kiq_is_avail(&adev->gfx.kiq);
+	if (r) {
+		spin_unlock_irqrestore(&kiq->ring_lock, flags);
+		goto failed_kiq_read;
+	}
+
   	if (amdgpu_device_wb_get(adev, &reg_val_offs)) {
   		spin_unlock_irqrestore(&kiq->ring_lock, flags);
   		pr_err("critical bug! too many kiq readers\n"); @@ -728,6 +752,12 @@ void amdgpu_kiq_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v)
   	BUG_ON(!ring->funcs->emit_wreg);
   
   	spin_lock_irqsave(&kiq->ring_lock, flags);
drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c +6 -0
+	r = amdgpu_gfx_kiq_is_avail(&adev->gfx.kiq);
+	if (r) {
+		spin_unlock_irqrestore(&kiq->ring_lock, flags);
+		goto failed_kiq_write;
+	}
+
   	amdgpu_ring_alloc(ring, 32);
   	amdgpu_ring_emit_wreg(ring, reg, v);
   	amdgpu_fence_emit_polling(ring, &seq); diff --git

a/drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.h
b/drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.h
index ee698f0246d8..1ee59a927bd9 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.h +3 -0
@@ -103,6 +103,7 @@ struct amdgpu_kiq {
   	struct amdgpu_ring	ring;
   	struct amdgpu_irq_src	irq;
   	const struct kiq_pm4_funcs *pmf;
+	uint32_t		max_sub_num;
   };
 
   /*
@@ -387,4 +388,6 @@ int amdgpu_gfx_cp_ecc_error_irq(struct amdgpu_device *adev,
   				  struct amdgpu_iv_entry *entry);
   uint32_t amdgpu_kiq_rreg(struct amdgpu_device *adev, uint32_t reg);
 void amdgpu_kiq_wreg(struct amdgpu_device *adev, uint32_t reg,
 uint32_t v);
+
+int amdgpu_gfx_kiq_is_avail(struct amdgpu_kiq *kiq);
   #endif
drivers/gpu/drm/amd/amdgpu/amdgpu_virt.c +6 -0
index 8c10084f44ef..4b027006d072 100644
@@ -57,6 +57,12 @@ void amdgpu_virt_kiq_reg_write_reg_wait(struct amdgpu_device *adev,
   	uint32_t seq;
 
   	spin_lock_irqsave(&kiq->ring_lock, flags);
+	r = amdgpu_gfx_kiq_is_avail(&adev->gfx.kiq);
+	if (r) {
+		spin_unlock_irqrestore(&kiq->ring_lock, flags);
+		goto failed_kiq;
+	}
+
   	amdgpu_ring_alloc(ring, 32);
   	amdgpu_ring_emit_reg_write_reg_wait(ring, reg0, reg1,
   					    ref, mask);
drivers/gpu/drm/amd/amdgpu/gfx_v9_0.c +6 -0
index 5b1549f167b0..a136e2229f7a 100644
@@ -4051,6 +4051,12 @@ static uint64_t gfx_v9_0_kiq_read_clock(struct amdgpu_device *adev)
   	BUG_ON(!ring->funcs->emit_rreg);
 
   	spin_lock_irqsave(&kiq->ring_lock, flags);
+	r = amdgpu_gfx_kiq_is_avail(&adev->gfx.kiq);
+	if (r) {
+		spin_unlock_irqrestore(&kiq->ring_lock, flags);
+		goto failed_kiq_read;
+	}
+
   	if (amdgpu_device_wb_get(adev, &reg_val_offs)) {
   		spin_unlock_irqrestore(&kiq->ring_lock, flags);
   		pr_err("critical bug! too many kiq readers\n"); diff --git

a/drivers/gpu/drm/amd/amdgpu/gmc_v10_0.c
b/drivers/gpu/drm/amd/amdgpu/gmc_v10_0.c
index 30b75d79efdb..77d8bc9c0111 100644

drivers/gpu/drm/amd/amdgpu/gmc_v10_0.c +7 -0
@@ -423,6 +423,13 @@ static int gmc_v10_0_flush_gpu_tlb_pasid(struct
 amdgpu_device *adev,
 
   	if (amdgpu_emu_mode == 0 && ring->sched.ready) {
   		spin_lock(&adev->gfx.kiq.ring_lock);
+		r = amdgpu_gfx_kiq_is_avail(&adev->gfx.kiq);
+		if (r) {
+			spin_unlock(&kiq->ring_lock);
+			DRM_ERROR("too many kiq submissions\n");
+			return -ETIME;
+		}
+
   		/* 2 dwords flush + 8 dwords fence */
   		amdgpu_ring_alloc(ring, kiq->pmf->invalidate_tlbs_size + 8);
   		kiq->pmf->kiq_invalidate_tlbs(ring,
drivers/gpu/drm/amd/amdgpu/gmc_v9_0.c +7 -1
index fecdbc471983..c429a2a5fe3d 100644
@@ -614,6 +614,13 @@ static int gmc_v9_0_flush_gpu_tlb_pasid(struct amdgpu_device *adev,
   			ndw += kiq->pmf->invalidate_tlbs_size;
 
   		spin_lock(&adev->gfx.kiq.ring_lock);
+		r = amdgpu_gfx_kiq_is_avail(&adev->gfx.kiq);
+		if (r) {
+			spin_unlock(&kiq->ring_lock);
+			DRM_ERROR("critical bug! too many kiq submissions\n");
+			return -ETIME;
+		}
+
   		/* 2 dwords flush + 8 dwords fence */
   		amdgpu_ring_alloc(ring, ndw);
   		if (vega20_xgmi_wa)
--

2.17.1

Quoted 8 lines
Quoted 8 lines

_______________________________________________
amd-gfx mailing list
[email protected]
https://nam11.safelinks.protection.outlook.com/?url=https%3A%2F%2Flist
s.freedesktop.org%2Fmailman%2Flistinfo%2Famd-gfx&amp;data=02%7C01%7CSh aoyun.Liu%40amd.com%7C0cd26307c0a149ebe0bd08d7e6cc84b1%7C3dd8961fe4884 e608e11a82d994e183d%7C0%7C0%7C637231638419034830&amp;sdata=rxkhyMutFwb e5Nw%2BeBGiESW9wTdflDUo%2F4xEvbCbR6U%3D&amp;reserved=0

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

maintainer
#5 lore raw
In reply to Yintian Tao

Am 22.04.20 um 16:50 schrieb Yintian Tao:

Quoted 6 lines

Wait for the oldest sequence on the kiq ring
to be signaled in order to make sure there
will be no kiq overrun.

v2: remove unused the variable and correct
kiq max_sub_num value

First of all this should probably be added to the fence handling code and not the kiq code.

Then you are kind of duplicating some of the functionality we have in the ring handling here. Probably better to avoid this, see amdgpu_fence_driver_init_ring() as well. That's also why I suggested to use the num_fences_mask value.

Regards,
Christian.

Quoted 188 lines
---
  .../drm/amd/amdgpu/amdgpu_amdkfd_gfx_v10.c    |  6 ++++
  .../gpu/drm/amd/amdgpu/amdgpu_amdkfd_gfx_v9.c |  6 ++++
  drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c       | 30 +++++++++++++++++++
  drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.h       |  3 ++
  drivers/gpu/drm/amd/amdgpu/amdgpu_virt.c      |  6 ++++
  drivers/gpu/drm/amd/amdgpu/gfx_v9_0.c         |  6 ++++
  drivers/gpu/drm/amd/amdgpu/gmc_v10_0.c        |  7 +++++
  drivers/gpu/drm/amd/amdgpu/gmc_v9_0.c         |  7 +++++
  8 files changed, 71 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gfx_v10.c +6 -0
index 691c89705bcd..fac8b9713dfc 100644
@@ -325,6 +325,12 @@ static int kgd_hiq_mqd_load(struct kgd_dev *kgd, void *mqd,
  		 mec, pipe, queue_id);
 
  	spin_lock(&adev->gfx.kiq.ring_lock);
+	r = amdgpu_gfx_kiq_is_avail(&adev->gfx.kiq);
+	if (r) {
+		pr_err("critical bug! too many kiq submission\n");
+		goto out_unlock;
+	}
+
  	r = amdgpu_ring_alloc(kiq_ring, 7);
  	if (r) {
  		pr_err("Failed to alloc KIQ (%d).\n", r);
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gfx_v9.c +6 -0
index df841c2ac5e7..fd42c126510f 100644
@@ -323,6 +323,12 @@ int kgd_gfx_v9_hiq_mqd_load(struct kgd_dev *kgd, void *mqd,
  		 mec, pipe, queue_id);
 
  	spin_lock(&adev->gfx.kiq.ring_lock);
+	r = amdgpu_gfx_kiq_is_avail(&adev->gfx.kiq);
+	if (r) {
+		pr_err("critical bug! too many kiq submissions\n");
+		goto out_unlock;
+	}
+
  	r = amdgpu_ring_alloc(kiq_ring, 7);
  	if (r) {
  		pr_err("Failed to alloc KIQ (%d).\n", r);
drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c +30 -0
index a721b0e0ff69..84e66c45df37 100644
@@ -321,6 +321,9 @@ int amdgpu_gfx_kiq_init_ring(struct amdgpu_device *adev,
  			     AMDGPU_RING_PRIO_DEFAULT);
  	if (r)
  		dev_warn(adev->dev, "(%d) failed to init kiq ring\n", r);
+	else
+		kiq->max_sub_num = (ring->ring_size / 4) /
+				(ring->funcs->align_mask + 1);
 
  	return r;
  }
@@ -663,6 +666,21 @@ int amdgpu_gfx_cp_ecc_error_irq(struct amdgpu_device *adev,
  	return 0;
  }
 
+int amdgpu_gfx_kiq_is_avail(struct amdgpu_kiq *kiq)
+{
+	uint32_t seq = 0;
+	signed long r = 0;
+
+	seq = abs(kiq->ring.fence_drv.sync_seq - kiq->max_sub_num);
+	if (seq > kiq->max_sub_num) {
+		r = amdgpu_fence_wait_polling(&kiq->ring, seq,
+					      MAX_KIQ_REG_WAIT);
+		return r < 1 ? -ETIME : 0;
+	}
+
+	return 0;
+}
+
  uint32_t amdgpu_kiq_rreg(struct amdgpu_device *adev, uint32_t reg)
  {
  	signed long r, cnt = 0;
@@ -674,6 +692,12 @@ uint32_t amdgpu_kiq_rreg(struct amdgpu_device *adev, uint32_t reg)
  	BUG_ON(!ring->funcs->emit_rreg);
 
  	spin_lock_irqsave(&kiq->ring_lock, flags);
+	r = amdgpu_gfx_kiq_is_avail(&adev->gfx.kiq);
+	if (r) {
+		spin_unlock_irqrestore(&kiq->ring_lock, flags);
+		goto failed_kiq_read;
+	}
+
  	if (amdgpu_device_wb_get(adev, &reg_val_offs)) {
  		spin_unlock_irqrestore(&kiq->ring_lock, flags);
  		pr_err("critical bug! too many kiq readers\n");
@@ -728,6 +752,12 @@ void amdgpu_kiq_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v)
  	BUG_ON(!ring->funcs->emit_wreg);
 
  	spin_lock_irqsave(&kiq->ring_lock, flags);
+	r = amdgpu_gfx_kiq_is_avail(&adev->gfx.kiq);
+	if (r) {
+		spin_unlock_irqrestore(&kiq->ring_lock, flags);
+		goto failed_kiq_write;
+	}
+
  	amdgpu_ring_alloc(ring, 32);
  	amdgpu_ring_emit_wreg(ring, reg, v);
  	amdgpu_fence_emit_polling(ring, &seq);
drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.h +3 -0
index ee698f0246d8..1ee59a927bd9 100644
@@ -103,6 +103,7 @@ struct amdgpu_kiq {
  	struct amdgpu_ring	ring;
  	struct amdgpu_irq_src	irq;
  	const struct kiq_pm4_funcs *pmf;
+	uint32_t		max_sub_num;
  };
 
  /*
@@ -387,4 +388,6 @@ int amdgpu_gfx_cp_ecc_error_irq(struct amdgpu_device *adev,
  				  struct amdgpu_iv_entry *entry);
  uint32_t amdgpu_kiq_rreg(struct amdgpu_device *adev, uint32_t reg);
  void amdgpu_kiq_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v);
+
+int amdgpu_gfx_kiq_is_avail(struct amdgpu_kiq *kiq);
  #endif
drivers/gpu/drm/amd/amdgpu/amdgpu_virt.c +6 -0
index 8c10084f44ef..4b027006d072 100644
@@ -57,6 +57,12 @@ void amdgpu_virt_kiq_reg_write_reg_wait(struct amdgpu_device *adev,
  	uint32_t seq;
 
  	spin_lock_irqsave(&kiq->ring_lock, flags);
+	r = amdgpu_gfx_kiq_is_avail(&adev->gfx.kiq);
+	if (r) {
+		spin_unlock_irqrestore(&kiq->ring_lock, flags);
+		goto failed_kiq;
+	}
+
  	amdgpu_ring_alloc(ring, 32);
  	amdgpu_ring_emit_reg_write_reg_wait(ring, reg0, reg1,
  					    ref, mask);
drivers/gpu/drm/amd/amdgpu/gfx_v9_0.c +6 -0
index 5b1549f167b0..a136e2229f7a 100644
@@ -4051,6 +4051,12 @@ static uint64_t gfx_v9_0_kiq_read_clock(struct amdgpu_device *adev)
  	BUG_ON(!ring->funcs->emit_rreg);
 
  	spin_lock_irqsave(&kiq->ring_lock, flags);
+	r = amdgpu_gfx_kiq_is_avail(&adev->gfx.kiq);
+	if (r) {
+		spin_unlock_irqrestore(&kiq->ring_lock, flags);
+		goto failed_kiq_read;
+	}
+
  	if (amdgpu_device_wb_get(adev, &reg_val_offs)) {
  		spin_unlock_irqrestore(&kiq->ring_lock, flags);
  		pr_err("critical bug! too many kiq readers\n");
drivers/gpu/drm/amd/amdgpu/gmc_v10_0.c +7 -0
index 30b75d79efdb..77d8bc9c0111 100644
@@ -423,6 +423,13 @@ static int gmc_v10_0_flush_gpu_tlb_pasid(struct amdgpu_device *adev,
 
  	if (amdgpu_emu_mode == 0 && ring->sched.ready) {
  		spin_lock(&adev->gfx.kiq.ring_lock);
+		r = amdgpu_gfx_kiq_is_avail(&adev->gfx.kiq);
+		if (r) {
+			spin_unlock(&kiq->ring_lock);
+			DRM_ERROR("too many kiq submissions\n");
+			return -ETIME;
+		}
+
  		/* 2 dwords flush + 8 dwords fence */
  		amdgpu_ring_alloc(ring, kiq->pmf->invalidate_tlbs_size + 8);
  		kiq->pmf->kiq_invalidate_tlbs(ring,
drivers/gpu/drm/amd/amdgpu/gmc_v9_0.c +0 -0
index fecdbc471983..c429a2a5fe3d 100644
@@ -614,6 +614,13 @@ static int gmc_v9_0_flush_gpu_tlb_pasid(struct amdgpu_device *adev,
  			ndw += kiq->pmf->invalidate_tlbs_size;
 
  		spin_lock(&adev->gfx.kiq.ring_lock);

drivers/gpu/drm/amd/amdgpu/gmc_v9_0.c static int gmc_v9_0_flush_gpu_tlb_pasid(struct amdgpu_device *adev, · line 626

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/gmc_v9_0.c +7 -0
+		r = amdgpu_gfx_kiq_is_avail(&adev->gfx.kiq);
+		if (r) {
+			spin_unlock(&kiq->ring_lock);
+			DRM_ERROR("critical bug! too many kiq submissions\n");
+			return -ETIME;
+		}
+
  		/* 2 dwords flush + 8 dwords fence */
  		amdgpu_ring_alloc(ring, ndw);
  		if (vega20_xgmi_wa)

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

#6 lore raw

Hi Christian

Ok , I got it. The real max number can be submitted to kiq ring buffer is 1024.
If we use the num_fneces_mask value then the max submission number will be reduced to 512, do you think whether it is ok?

Best Regards
Yintian Tao
-----Original Message-----
From: Koenig, Christian <[email protected]>
Sent: 2020年4月23日 2:43
To: Tao, Yintian <[email protected]>; Liu, Monk <[email protected]>; Kuehling, Felix <[email protected]>
Cc: [email protected]
Subject: Re: [PATCH] drm/amdgpu: protect kiq overrun

Am 22.04.20 um 16:50 schrieb Yintian Tao:

Quoted 5 lines

Wait for the oldest sequence on the kiq ring to be signaled in order to make sure there will be no kiq overrun.

v2: remove unused the variable and correct
kiq max_sub_num value

First of all this should probably be added to the fence handling code and not the kiq code.

Then you are kind of duplicating some of the functionality we have in the ring handling here. Probably better to avoid this, see amdgpu_fence_driver_init_ring() as well. That's also why I suggested to use the num_fences_mask value.

Regards,
Christian.

Quoted 197 lines
---
  .../drm/amd/amdgpu/amdgpu_amdkfd_gfx_v10.c    |  6 ++++
  .../gpu/drm/amd/amdgpu/amdgpu_amdkfd_gfx_v9.c |  6 ++++
  drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c       | 30 +++++++++++++++++++
  drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.h       |  3 ++
  drivers/gpu/drm/amd/amdgpu/amdgpu_virt.c      |  6 ++++
  drivers/gpu/drm/amd/amdgpu/gfx_v9_0.c         |  6 ++++
  drivers/gpu/drm/amd/amdgpu/gmc_v10_0.c        |  7 +++++
  drivers/gpu/drm/amd/amdgpu/gmc_v9_0.c         |  7 +++++
  8 files changed, 71 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gfx_v10.c +6 -0
index 691c89705bcd..fac8b9713dfc 100644
@@ -325,6 +325,12 @@ static int kgd_hiq_mqd_load(struct kgd_dev *kgd, void *mqd,
  		 mec, pipe, queue_id);
 
  	spin_lock(&adev->gfx.kiq.ring_lock);
+	r = amdgpu_gfx_kiq_is_avail(&adev->gfx.kiq);
+	if (r) {
+		pr_err("critical bug! too many kiq submission\n");
+		goto out_unlock;
+	}
+
  	r = amdgpu_ring_alloc(kiq_ring, 7);
  	if (r) {
  		pr_err("Failed to alloc KIQ (%d).\n", r); diff --git 

a/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gfx_v9.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gfx_v9.c index df841c2ac5e7..fd42c126510f 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gfx_v9.c +6 -0
@@ -323,6 +323,12 @@ int kgd_gfx_v9_hiq_mqd_load(struct kgd_dev *kgd, void *mqd,
  		 mec, pipe, queue_id);
 
  	spin_lock(&adev->gfx.kiq.ring_lock);
+	r = amdgpu_gfx_kiq_is_avail(&adev->gfx.kiq);
+	if (r) {
+		pr_err("critical bug! too many kiq submissions\n");
+		goto out_unlock;
+	}
+
  	r = amdgpu_ring_alloc(kiq_ring, 7);
  	if (r) {
  		pr_err("Failed to alloc KIQ (%d).\n", r); diff --git 

a/drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c
b/drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c
index a721b0e0ff69..84e66c45df37 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c +23 -0
@@ -321,6 +321,9 @@ int amdgpu_gfx_kiq_init_ring(struct amdgpu_device *adev,
  			     AMDGPU_RING_PRIO_DEFAULT);
  	if (r)
  		dev_warn(adev->dev, "(%d) failed to init kiq ring\n", r);
+	else
+		kiq->max_sub_num = (ring->ring_size / 4) /
+				(ring->funcs->align_mask + 1);
 
  	return r;
  }
@@ -663,6 +666,21 @@ int amdgpu_gfx_cp_ecc_error_irq(struct amdgpu_device *adev,
  	return 0;
  }
 
+int amdgpu_gfx_kiq_is_avail(struct amdgpu_kiq *kiq) {
+	uint32_t seq = 0;
+	signed long r = 0;
+
+	seq = abs(kiq->ring.fence_drv.sync_seq - kiq->max_sub_num);
+	if (seq > kiq->max_sub_num) {
+		r = amdgpu_fence_wait_polling(&kiq->ring, seq,
+					      MAX_KIQ_REG_WAIT);
+		return r < 1 ? -ETIME : 0;
+	}
+
+	return 0;
+}
+
  uint32_t amdgpu_kiq_rreg(struct amdgpu_device *adev, uint32_t reg)
  {
  	signed long r, cnt = 0;
@@ -674,6 +692,12 @@ uint32_t amdgpu_kiq_rreg(struct amdgpu_device *adev, uint32_t reg)
  	BUG_ON(!ring->funcs->emit_rreg);
 
  	spin_lock_irqsave(&kiq->ring_lock, flags);
+	r = amdgpu_gfx_kiq_is_avail(&adev->gfx.kiq);
+	if (r) {
+		spin_unlock_irqrestore(&kiq->ring_lock, flags);
+		goto failed_kiq_read;
+	}
+
  	if (amdgpu_device_wb_get(adev, &reg_val_offs)) {
  		spin_unlock_irqrestore(&kiq->ring_lock, flags);
  		pr_err("critical bug! too many kiq readers\n"); @@ -728,6 +752,12 

@@ void amdgpu_kiq_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v)
BUG_ON(!ring->funcs->emit_wreg);

  	spin_lock_irqsave(&kiq->ring_lock, flags);
drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c +6 -0
+	r = amdgpu_gfx_kiq_is_avail(&adev->gfx.kiq);
+	if (r) {
+		spin_unlock_irqrestore(&kiq->ring_lock, flags);
+		goto failed_kiq_write;
+	}
+
  	amdgpu_ring_alloc(ring, 32);
  	amdgpu_ring_emit_wreg(ring, reg, v);
  	amdgpu_fence_emit_polling(ring, &seq); diff --git 

a/drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.h
b/drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.h
index ee698f0246d8..1ee59a927bd9 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.h +3 -0
@@ -103,6 +103,7 @@ struct amdgpu_kiq {
  	struct amdgpu_ring	ring;
  	struct amdgpu_irq_src	irq;
  	const struct kiq_pm4_funcs *pmf;
+	uint32_t		max_sub_num;
  };
 
  /*
@@ -387,4 +388,6 @@ int amdgpu_gfx_cp_ecc_error_irq(struct amdgpu_device *adev,
  				  struct amdgpu_iv_entry *entry);
  uint32_t amdgpu_kiq_rreg(struct amdgpu_device *adev, uint32_t reg);
  void amdgpu_kiq_wreg(struct amdgpu_device *adev, uint32_t reg, 
 uint32_t v);
+
+int amdgpu_gfx_kiq_is_avail(struct amdgpu_kiq *kiq);
  #endif
drivers/gpu/drm/amd/amdgpu/amdgpu_virt.c +6 -0
index 8c10084f44ef..4b027006d072 100644
@@ -57,6 +57,12 @@ void amdgpu_virt_kiq_reg_write_reg_wait(struct amdgpu_device *adev,
  	uint32_t seq;
 
  	spin_lock_irqsave(&kiq->ring_lock, flags);
+	r = amdgpu_gfx_kiq_is_avail(&adev->gfx.kiq);
+	if (r) {
+		spin_unlock_irqrestore(&kiq->ring_lock, flags);
+		goto failed_kiq;
+	}
+
  	amdgpu_ring_alloc(ring, 32);
  	amdgpu_ring_emit_reg_write_reg_wait(ring, reg0, reg1,
  					    ref, mask);
drivers/gpu/drm/amd/amdgpu/gfx_v9_0.c +6 -0
index 5b1549f167b0..a136e2229f7a 100644
@@ -4051,6 +4051,12 @@ static uint64_t gfx_v9_0_kiq_read_clock(struct amdgpu_device *adev)
  	BUG_ON(!ring->funcs->emit_rreg);
 
  	spin_lock_irqsave(&kiq->ring_lock, flags);
+	r = amdgpu_gfx_kiq_is_avail(&adev->gfx.kiq);
+	if (r) {
+		spin_unlock_irqrestore(&kiq->ring_lock, flags);
+		goto failed_kiq_read;
+	}
+
  	if (amdgpu_device_wb_get(adev, &reg_val_offs)) {
  		spin_unlock_irqrestore(&kiq->ring_lock, flags);
  		pr_err("critical bug! too many kiq readers\n"); diff --git 

a/drivers/gpu/drm/amd/amdgpu/gmc_v10_0.c
b/drivers/gpu/drm/amd/amdgpu/gmc_v10_0.c
index 30b75d79efdb..77d8bc9c0111 100644

drivers/gpu/drm/amd/amdgpu/gmc_v10_0.c +7 -0
@@ -423,6 +423,13 @@ static int gmc_v10_0_flush_gpu_tlb_pasid(struct 
 amdgpu_device *adev,
 
  	if (amdgpu_emu_mode == 0 && ring->sched.ready) {
  		spin_lock(&adev->gfx.kiq.ring_lock);
+		r = amdgpu_gfx_kiq_is_avail(&adev->gfx.kiq);
+		if (r) {
+			spin_unlock(&kiq->ring_lock);
+			DRM_ERROR("too many kiq submissions\n");
+			return -ETIME;
+		}
+
  		/* 2 dwords flush + 8 dwords fence */
  		amdgpu_ring_alloc(ring, kiq->pmf->invalidate_tlbs_size + 8);

kiq->pmf->kiq_invalidate_tlbs(ring,

drivers/gpu/drm/amd/amdgpu/gmc_v9_0.c +0 -0
index fecdbc471983..c429a2a5fe3d 100644
@@ -614,6 +614,13 @@ static int gmc_v9_0_flush_gpu_tlb_pasid(struct amdgpu_device *adev,
  			ndw += kiq->pmf->invalidate_tlbs_size;
 
  		spin_lock(&adev->gfx.kiq.ring_lock);

drivers/gpu/drm/amd/amdgpu/gmc_v9_0.c static int gmc_v9_0_flush_gpu_tlb_pasid(struct amdgpu_device *adev, · line 626

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/gmc_v9_0.c +7 -0
+		r = amdgpu_gfx_kiq_is_avail(&adev->gfx.kiq);
+		if (r) {
+			spin_unlock(&kiq->ring_lock);
+			DRM_ERROR("critical bug! too many kiq submissions\n");
+			return -ETIME;
+		}
+
  		/* 2 dwords flush + 8 dwords fence */
  		amdgpu_ring_alloc(ring, ndw);
  		if (vega20_xgmi_wa)

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx