Skip to contentSkip to conversation

drm/amdgpu: allow function to allocate normal GTT memory

PATCH In review Victor Zhao 6 participants 21 messages Nov 6, 2024 – Nov 14, 2024

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

PATCH 1/2 drm/amdgpu: allow function to allocate normal GTT memory

From: Gang Ba <[email protected]>

amdgpu_amdkfd_alloc_gtt_mem currently allocates USWC memory. It uses write-combining for CPU access, which is slow for reading. Add a new parameter to amdgpu_amdkfd_alloc_gtt_mem to allocate normal GTT memory.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.c             | 7 +++++--
 drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h             | 2 +-
 drivers/gpu/drm/amd/amdkfd/kfd_device.c                | 2 +-
 drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c  | 2 +-
 drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_v9.c        | 2 +-
 drivers/gpu/drm/amd/amdkfd/kfd_process.c               | 2 +-
 drivers/gpu/drm/amd/amdkfd/kfd_process_queue_manager.c | 2 +-
 7 files changed, 11 insertions(+), 8 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.c +5 -2
index 713be49540d4..acb762855c24 100644
@@ -294,7 +294,7 @@ void amdgpu_amdkfd_gpu_reset(struct amdgpu_device *adev)
 
 int amdgpu_amdkfd_alloc_gtt_mem(struct amdgpu_device *adev, size_t size,
 				void **mem_obj, uint64_t *gpu_addr,
-				void **cpu_ptr, bool cp_mqd_gfx9)
+				void **cpu_ptr, bool cp_mqd_gfx9, bool is_uswc_mode)
 {
 	struct amdgpu_bo *bo = NULL;
 	struct amdgpu_bo_param bp;
@@ -305,7 +305,10 @@ int amdgpu_amdkfd_alloc_gtt_mem(struct amdgpu_device *adev, size_t size,
 	bp.size = size;
 	bp.byte_align = PAGE_SIZE;
 	bp.domain = AMDGPU_GEM_DOMAIN_GTT;
-	bp.flags = AMDGPU_GEM_CREATE_CPU_GTT_USWC;
+	if (is_uswc_mode)
+		bp.flags = AMDGPU_GEM_CREATE_CPU_GTT_USWC;
+	else
+		bp.flags = 0;
 	bp.type = ttm_bo_type_kernel;
 	bp.resv = NULL;
 	bp.bo_ptr_size = sizeof(struct amdgpu_bo);
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h +1 -1
index 4b80ad860639..745121e0dd8e 100644
@@ -234,7 +234,7 @@ int amdgpu_amdkfd_bo_validate_and_fence(struct amdgpu_bo *bo,
 /* Shared API */
 int amdgpu_amdkfd_alloc_gtt_mem(struct amdgpu_device *adev, size_t size,
 				void **mem_obj, uint64_t *gpu_addr,
-				void **cpu_ptr, bool mqd_gfx9);
+				void **cpu_ptr, bool mqd_gfx9, bool is_uswc_mode);
 void amdgpu_amdkfd_free_gtt_mem(struct amdgpu_device *adev, void **mem_obj);
 int amdgpu_amdkfd_alloc_gws(struct amdgpu_device *adev, size_t size,
 				void **mem_obj);
drivers/gpu/drm/amd/amdkfd/kfd_device.c +1 -1
index 956198da7859..1f1d79ac5e6c 100644
@@ -779,7 +779,7 @@ bool kgd2kfd_device_init(struct kfd_dev *kfd,
 	if (amdgpu_amdkfd_alloc_gtt_mem(
 			kfd->adev, size, &kfd->gtt_mem,
 			&kfd->gtt_start_gpu_addr, &kfd->gtt_start_cpu_ptr,
-			false)) {
+			false, true)) {
 		dev_err(kfd_device, "Could not allocate %d bytes\n", size);
 		goto alloc_gtt_mem_failure;
 	}
drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +1 -1
index 38c19dc8311d..9672542578d4 100644
@@ -2783,7 +2783,7 @@ static int allocate_hiq_sdma_mqd(struct device_queue_manager *dqm)
 
 	retval = amdgpu_amdkfd_alloc_gtt_mem(dev->adev, size,
 		&(mem_obj->gtt_mem), &(mem_obj->gpu_addr),
-		(void *)&(mem_obj->cpu_ptr), false);
+		(void *)&(mem_obj->cpu_ptr), false, true);
 
 	return retval;
 }
drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_v9.c +1 -1
index 84e8ea3a8a0c..c9882f1d4419 100644
@@ -140,7 +140,7 @@ static struct kfd_mem_obj *allocate_mqd(struct kfd_node *node,
 			NUM_XCC(node->xcc_mask),
 			&(mqd_mem_obj->gtt_mem),
 			&(mqd_mem_obj->gpu_addr),
-			(void *)&(mqd_mem_obj->cpu_ptr), true);
+			(void *)&(mqd_mem_obj->cpu_ptr), true, true);
 
 		if (retval) {
 			kfree(mqd_mem_obj);
drivers/gpu/drm/amd/amdkfd/kfd_process.c +1 -1
index 6bab6fc6a35d..e1d8fd11d2b1 100644
@@ -1639,7 +1639,7 @@ struct kfd_process_device *kfd_create_process_device_data(struct kfd_node *dev,
 						&pdd->proc_ctx_bo,
 						&pdd->proc_ctx_gpu_addr,
 						&pdd->proc_ctx_cpu_ptr,
-						false);
+						false, true);
 		if (retval) {
 			dev_err(dev->adev->dev,
 				"failed to allocate process context bo\n");
drivers/gpu/drm/amd/amdkfd/kfd_process_queue_manager.c +1 -1
index c76db22a1000..3a3be0e19fb9 100644
@@ -260,7 +260,7 @@ static int init_user_queue(struct process_queue_manager *pqm,
 						&(*q)->gang_ctx_bo,
 						&(*q)->gang_ctx_gpu_addr,
 						&(*q)->gang_ctx_cpu_ptr,
-						false);
+						false, true);
 		if (retval) {
 			pr_err("failed to allocate gang context bo\n");
 			goto cleanup;
Signature · 1 line
2.34.1
#1 lore raw

PATCH 2/2 drm/amdkfd: use cache GTT buffer for PQ and wb pool

From: Monk Liu <[email protected]>

As cache GTT buffer is snooped, this way the coherence between CPU write and GPU fetch is guaranteed, but original code uses WC + unsnooped for HIQ PQ(ring buffer) which introduces coherency issues:
MEC fetches a stall data from PQ and leads to MEC hang.

---
 drivers/gpu/drm/amd/amdkfd/kfd_device.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/amd/amdkfd/kfd_device.c +1 -1
index 1f1d79ac5e6c..fb087a0ff5bc 100644
@@ -779,7 +779,7 @@ bool kgd2kfd_device_init(struct kfd_dev *kfd,
 	if (amdgpu_amdkfd_alloc_gtt_mem(
 			kfd->adev, size, &kfd->gtt_mem,
 			&kfd->gtt_start_gpu_addr, &kfd->gtt_start_cpu_ptr,
-			false, true)) {
+			false, false)) {
 		dev_err(kfd_device, "Could not allocate %d bytes\n", size);
 		goto alloc_gtt_mem_failure;
 	}
Signature · 1 line
2.34.1
#2 lore raw

Re: [PATCH 2/2] drm/amdkfd: use cache GTT buffer for PQ and wb pool

Quoted 7 lines · Victor Zhao

From: Monk Liu <[email protected]>

As cache GTT buffer is snooped, this way the coherence between CPU write and GPU fetch is guaranteed, but original code uses WC + unsnooped for HIQ PQ(ring buffer) which introduces coherency issues:
MEC fetches a stall data from PQ and leads to MEC hang.

Can you elaborate on this? I can see CPU reads being slower because the memory is uncached, but the ring buffer is mostly writes anyway. IIRC, the driver uses USWC for most if not all of the other ring buffers managed by the kernel. Why aren't those a problem?

Alex

Quoted 22 lines
---
 drivers/gpu/drm/amd/amdkfd/kfd_device.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/amd/amdkfd/kfd_device.c +1 -2
index 1f1d79ac5e6c..fb087a0ff5bc 100644
@@ -779,7 +779,7 @@ bool kgd2kfd_device_init(struct kfd_dev *kfd,
        if (amdgpu_amdkfd_alloc_gtt_mem(
                        kfd->adev, size, &kfd->gtt_mem,
                        &kfd->gtt_start_gpu_addr, &kfd->gtt_start_cpu_ptr,
-                       false, true)) {
+                       false, false)) {
                dev_err(kfd_device, "Could not allocate %d bytes\n", size);
                goto alloc_gtt_mem_failure;
        }
--

2.34.1

#3 lore raw

Re: [PATCH 2/2] drm/amdkfd: use cache GTT buffer for PQ and wb pool

Quoted 9 lines · Alex Deucher
Quoted 7 lines · Victor Zhao

From: Monk Liu <[email protected]>

As cache GTT buffer is snooped, this way the coherence between CPU write and GPU fetch is guaranteed, but original code uses WC + unsnooped for HIQ PQ(ring buffer) which introduces coherency issues:
MEC fetches a stall data from PQ and leads to MEC hang.

Quoted 4 lines

Can you elaborate on this? I can see CPU reads being slower because the memory is uncached, but the ring buffer is mostly writes anyway. IIRC, the driver uses USWC for most if not all of the other ring buffers managed by the kernel. Why aren't those a problem?

We have this on other rings -

        mb();
        amdgpu_ring_set_wptr(ring);

I think the solution should be to use barrier before write pointer updates rather than relying on PCIe snooping.

Thanks,
Lijo

Quoted 25 lines

Alex

Quoted 22 lines
---
 drivers/gpu/drm/amd/amdkfd/kfd_device.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/amd/amdkfd/kfd_device.c +1 -2
index 1f1d79ac5e6c..fb087a0ff5bc 100644
@@ -779,7 +779,7 @@ bool kgd2kfd_device_init(struct kfd_dev *kfd,
        if (amdgpu_amdkfd_alloc_gtt_mem(
                        kfd->adev, size, &kfd->gtt_mem,
                        &kfd->gtt_start_gpu_addr, &kfd->gtt_start_cpu_ptr,
-                       false, true)) {
+                       false, false)) {
                dev_err(kfd_device, "Could not allocate %d bytes\n", size);
                goto alloc_gtt_mem_failure;
        }
--

2.34.1

maintainer
#4 lore raw

Re: [PATCH 2/2] drm/amdkfd: use cache GTT buffer for PQ and wb pool

Am 07.11.24 um 06:58 schrieb Lazar, Lijo:

Quoted 12 lines · Alex Deucher
Quoted 11 lines · Victor Zhao
Quoted 6 lines

From: Monk Liu <[email protected]>

As cache GTT buffer is snooped, this way the coherence between CPU write and GPU fetch is guaranteed, but original code uses WC + unsnooped for HIQ PQ(ring buffer) which introduces coherency issues:
MEC fetches a stall data from PQ and leads to MEC hang.

Can you elaborate on this? I can see CPU reads being slower because the memory is uncached, but the ring buffer is mostly writes anyway. IIRC, the driver uses USWC for most if not all of the other ring buffers managed by the kernel. Why aren't those a problem?

Quoted 6 lines

We have this on other rings -

         mb();
         amdgpu_ring_set_wptr(ring);

I think the solution should be to use barrier before write pointer updates rather than relying on PCIe snooping.

Yeah, completely agree as well. The barrier also takes care of preventing the compiler from re-ordering writes.

Regards,
Christian.

Quoted 27 lines

Thanks,
Lijo

Quoted 23 lines

Alex

Quoted 21 lines
---
  drivers/gpu/drm/amd/amdkfd/kfd_device.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/amd/amdkfd/kfd_device.c +1 -2
index 1f1d79ac5e6c..fb087a0ff5bc 100644
@@ -779,7 +779,7 @@ bool kgd2kfd_device_init(struct kfd_dev *kfd,
         if (amdgpu_amdkfd_alloc_gtt_mem(
                         kfd->adev, size, &kfd->gtt_mem,
                         &kfd->gtt_start_gpu_addr, &kfd->gtt_start_cpu_ptr,
-                       false, true)) {
+                       false, false)) {
                 dev_err(kfd_device, "Could not allocate %d bytes\n", size);
                 goto alloc_gtt_mem_failure;
         }
--

2.34.1

#5 lore raw

RE: [PATCH 2/2] drm/amdkfd: use cache GTT buffer for PQ and wb pool

[AMD Official Use Only - AMD Internal Distribution Only]

Christian/Lijo

We verified all approaches, and we know what works and not works, obviously the mb() doesn't work.

The way of mb() between set wptr_polling and kicking off doorbell is theoretically correct, and I'm not object to do so... but this won't resolve the issue we hit. First of all, USWC will have some chance that the data is still in CPU's WC storage place and not flushed to the memory and even with MB() get rid of re-ordering GPU might still have a chance to read stalled data from ring buffer as long as it is mapped USWC.

This is why only cache plus snoop memory can get rid of inconsistence issues.

Besides, do you know what's the rational to keep all GFX KCQ cache+snoop but only HIQ/KIQ from KFD configured to USWC ?

For performance concern that looks to me always the second priority compared to "correct" especially under the case HIQ contributes very little to ROCM performance when switching to cache mapping.

Monk Liu | Cloud GPU & Virtualization | AMD

-----Original Message-----
From: Koenig, Christian <[email protected]>
Sent: Thursday, November 7, 2024 7:57 PM
To: Lazar, Lijo <[email protected]>; Alex Deucher <[email protected]>; Zhao, Victor <[email protected]>
Cc: [email protected]; Liu, Monk <[email protected]>; Yang, Philip <[email protected]>; Kuehling, Felix <[email protected]>
Subject: Re: [PATCH 2/2] drm/amdkfd: use cache GTT buffer for PQ and wb pool

Am 07.11.24 um 06:58 schrieb Lazar, Lijo:

Quoted 12 lines · Alex Deucher
Quoted 11 lines · Victor Zhao
Quoted 6 lines

From: Monk Liu <[email protected]>

As cache GTT buffer is snooped, this way the coherence between CPU write and GPU fetch is guaranteed, but original code uses WC + unsnooped for HIQ PQ(ring buffer) which introduces coherency issues:
MEC fetches a stall data from PQ and leads to MEC hang.

Can you elaborate on this? I can see CPU reads being slower because the memory is uncached, but the ring buffer is mostly writes anyway. IIRC, the driver uses USWC for most if not all of the other ring buffers managed by the kernel. Why aren't those a problem?

Quoted 6 lines

We have this on other rings -

         mb();
         amdgpu_ring_set_wptr(ring);

I think the solution should be to use barrier before write pointer updates rather than relying on PCIe snooping.

Yeah, completely agree as well. The barrier also takes care of preventing the compiler from re-ordering writes.

Regards,
Christian.

Quoted 28 lines

Thanks,
Lijo

Quoted 24 lines

Alex

Quoted 22 lines
---
  drivers/gpu/drm/amd/amdkfd/kfd_device.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/amd/amdkfd/kfd_device.c +1 -2
index 1f1d79ac5e6c..fb087a0ff5bc 100644
@@ -779,7 +779,7 @@ bool kgd2kfd_device_init(struct kfd_dev *kfd,
         if (amdgpu_amdkfd_alloc_gtt_mem(
                         kfd->adev, size, &kfd->gtt_mem,
                         &kfd->gtt_start_gpu_addr, &kfd->gtt_start_cpu_ptr,
-                       false, true)) {
+                       false, false)) {
                 dev_err(kfd_device, "Could not allocate %d bytes\n", size);
                 goto alloc_gtt_mem_failure;
         }
--

2.34.1

#6 lore raw

Re: [PATCH 2/2] drm/amdkfd: use cache GTT buffer for PQ and wb pool

Quoted 8 lines · Monk

[AMD Official Use Only - AMD Internal Distribution Only]

Christian/Lijo

We verified all approaches, and we know what works and not works, obviously the mb() doesn't work.

The way of mb() between set wptr_polling and kicking off doorbell is theoretically correct, and I'm not object to do so... but this won't resolve the issue we hit. First of all, USWC will have some chance that the data is still in CPU's WC storage place and not flushed to the memory and even with MB() get rid of re-ordering

This is the wrong premise.

For x86, I see this -

	#define mb()    asm volatile("mfence" : : : "memory")

Below is Intel Arch Manual which clearly mentions any prior buffered writes are *drained to memory* with mfence (AMD arch manual has a much clearer definition about WC itself).

Program synchronization can also be carried out with serializing instructions (see Section 10.3). These instructions
are typically used at critical procedure or task boundaries to force completion of all previous instructions before a
jump to a new section of code or a context switch occurs. " Like the I/O instructions, the processor waits until all
previous instructions have been completed and all buffered writes have been drained to memory before executing
the serializing instruction"

MFENCE — Serializes all store and load operations that occurred prior to the MFENCE instruction in the
program instruction stream.

Just because it worked because of memory type change doesn't imply that it is the right way to solve this. You could be looking at a system config bug.

Thanks,
Lijo

Quoted 78 lines

GPU might still have a chance to read stalled data from ring buffer as long as it is mapped USWC.

This is why only cache plus snoop memory can get rid of inconsistence issues.

Besides, do you know what's the rational to keep all GFX KCQ cache+snoop but only HIQ/KIQ from KFD configured to USWC ?

For performance concern that looks to me always the second priority compared to "correct" especially under the case HIQ contributes very little to ROCM performance when switching to cache mapping.

Monk Liu | Cloud GPU & Virtualization | AMD

-----Original Message-----
From: Koenig, Christian <[email protected]>
Sent: Thursday, November 7, 2024 7:57 PM
To: Lazar, Lijo <[email protected]>; Alex Deucher <[email protected]>; Zhao, Victor <[email protected]>
Cc: [email protected]; Liu, Monk <[email protected]>; Yang, Philip <[email protected]>; Kuehling, Felix <[email protected]>
Subject: Re: [PATCH 2/2] drm/amdkfd: use cache GTT buffer for PQ and wb pool

Am 07.11.24 um 06:58 schrieb Lazar, Lijo:

Quoted 12 lines · Alex Deucher
Quoted 11 lines · Victor Zhao
Quoted 6 lines

From: Monk Liu <[email protected]>

As cache GTT buffer is snooped, this way the coherence between CPU write and GPU fetch is guaranteed, but original code uses WC + unsnooped for HIQ PQ(ring buffer) which introduces coherency issues:
MEC fetches a stall data from PQ and leads to MEC hang.

Can you elaborate on this? I can see CPU reads being slower because the memory is uncached, but the ring buffer is mostly writes anyway. IIRC, the driver uses USWC for most if not all of the other ring buffers managed by the kernel. Why aren't those a problem?

Quoted 6 lines

We have this on other rings -

         mb();
         amdgpu_ring_set_wptr(ring);

I think the solution should be to use barrier before write pointer updates rather than relying on PCIe snooping.

Yeah, completely agree as well. The barrier also takes care of preventing the compiler from re-ordering writes.

Regards,
Christian.

Quoted 28 lines

Thanks,
Lijo

Quoted 24 lines

Alex

Quoted 22 lines
---
  drivers/gpu/drm/amd/amdkfd/kfd_device.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/amd/amdkfd/kfd_device.c +1 -2
index 1f1d79ac5e6c..fb087a0ff5bc 100644
@@ -779,7 +779,7 @@ bool kgd2kfd_device_init(struct kfd_dev *kfd,
         if (amdgpu_amdkfd_alloc_gtt_mem(
                         kfd->adev, size, &kfd->gtt_mem,
                         &kfd->gtt_start_gpu_addr, &kfd->gtt_start_cpu_ptr,
-                       false, true)) {
+                       false, false)) {
                 dev_err(kfd_device, "Could not allocate %d bytes\n", size);
                 goto alloc_gtt_mem_failure;
         }
--

2.34.1

#7 lore raw

RE: [PATCH 2/2] drm/amdkfd: use cache GTT buffer for PQ and wb pool

In reply to Liu, Monk

[AMD Official Use Only - AMD Internal Distribution Only]

To be clear for the mb() approach: Even if we insert mb() in prior to amdgpu_ring_set_wptr(ring), GPU might still fetch stalled data from PQ due to USWC attributes.

The issue here is not the re-ordering but the stalled PQ.

Monk Liu | Cloud GPU & Virtualization | AMD

-----Original Message-----
From: Liu, Monk
Sent: Friday, November 8, 2024 6:22 PM
To: Koenig, Christian <[email protected]>; Lazar, Lijo <[email protected]>; Alex Deucher <[email protected]>; Zhao, Victor <[email protected]>
Cc: [email protected]; Yang, Philip <[email protected]>; Kuehling, Felix <[email protected]>
Subject: RE: [PATCH 2/2] drm/amdkfd: use cache GTT buffer for PQ and wb pool

Christian/Lijo

We verified all approaches, and we know what works and not works, obviously the mb() doesn't work.

The way of mb() between set wptr_polling and kicking off doorbell is theoretically correct, and I'm not object to do so... but this won't resolve the issue we hit.
First of all, USWC will have some chance that the data is still in CPU's WC storage place and not flushed to the memory and even with MB() get rid of re-ordering GPU might still have a chance to read stalled data from ring buffer as long as it is mapped USWC.

This is why only cache plus snoop memory can get rid of inconsistence issues.

Besides, do you know what's the rational to keep all GFX KCQ cache+snoop but only HIQ/KIQ from KFD configured to USWC ?

For performance concern that looks to me always the second priority compared to "correct" especially under the case HIQ contributes very little to ROCM performance when switching to cache mapping.

Monk Liu | Cloud GPU & Virtualization | AMD

-----Original Message-----
From: Koenig, Christian <[email protected]>
Sent: Thursday, November 7, 2024 7:57 PM
To: Lazar, Lijo <[email protected]>; Alex Deucher <[email protected]>; Zhao, Victor <[email protected]>
Cc: [email protected]; Liu, Monk <[email protected]>; Yang, Philip <[email protected]>; Kuehling, Felix <[email protected]>
Subject: Re: [PATCH 2/2] drm/amdkfd: use cache GTT buffer for PQ and wb pool

Am 07.11.24 um 06:58 schrieb Lazar, Lijo:

Quoted 12 lines · Alex Deucher
Quoted 11 lines · Victor Zhao
Quoted 6 lines

From: Monk Liu <[email protected]>

As cache GTT buffer is snooped, this way the coherence between CPU write and GPU fetch is guaranteed, but original code uses WC + unsnooped for HIQ PQ(ring buffer) which introduces coherency issues:
MEC fetches a stall data from PQ and leads to MEC hang.

Can you elaborate on this? I can see CPU reads being slower because the memory is uncached, but the ring buffer is mostly writes anyway. IIRC, the driver uses USWC for most if not all of the other ring buffers managed by the kernel. Why aren't those a problem?

Quoted 6 lines

We have this on other rings -

         mb();
         amdgpu_ring_set_wptr(ring);

I think the solution should be to use barrier before write pointer updates rather than relying on PCIe snooping.

Yeah, completely agree as well. The barrier also takes care of preventing the compiler from re-ordering writes.

Regards,
Christian.

Quoted 28 lines

Thanks,
Lijo

Quoted 24 lines

Alex

Quoted 22 lines
---
  drivers/gpu/drm/amd/amdkfd/kfd_device.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/amd/amdkfd/kfd_device.c +1 -2
index 1f1d79ac5e6c..fb087a0ff5bc 100644
@@ -779,7 +779,7 @@ bool kgd2kfd_device_init(struct kfd_dev *kfd,
         if (amdgpu_amdkfd_alloc_gtt_mem(
                         kfd->adev, size, &kfd->gtt_mem,
                         &kfd->gtt_start_gpu_addr, &kfd->gtt_start_cpu_ptr,
-                       false, true)) {
+                       false, false)) {
                 dev_err(kfd_device, "Could not allocate %d bytes\n", size);
                 goto alloc_gtt_mem_failure;
         }
--

2.34.1

#8 lore raw

Re: [PATCH 2/2] drm/amdkfd: use cache GTT buffer for PQ and wb pool

Quoted 4 lines · Monk

[AMD Official Use Only - AMD Internal Distribution Only]

To be clear for the mb() approach: Even if we insert mb() in prior to amdgpu_ring_set_wptr(ring), GPU might still fetch stalled data from PQ due to USWC attributes.

Inserting an mb() doesn't cause WC buffer flush is a wrong assumption.

All prior loads/stores are supposed to be globally visible. Hence mb() followed by a write pointer update also should guarantee the same (From Arch manual).

The MFENCE instruction establishes a memory fence for both loads and
stores. The processor ensures that no load
or store after MFENCE will become globally visible *until all loads and stores before MFENCE are globally visible.*

Ignoring the amdgpu driver part of it - if mb() is not working as expected as you claim that means something is wrong with the system.

USWC or WB for ring type may still be a separate discussion.

Thanks,
Lijo

Quoted 96 lines

The issue here is not the re-ordering but the stalled PQ.

Monk Liu | Cloud GPU & Virtualization | AMD

-----Original Message-----
From: Liu, Monk
Sent: Friday, November 8, 2024 6:22 PM
To: Koenig, Christian <[email protected]>; Lazar, Lijo <[email protected]>; Alex Deucher <[email protected]>; Zhao, Victor <[email protected]>
Cc: [email protected]; Yang, Philip <[email protected]>; Kuehling, Felix <[email protected]>
Subject: RE: [PATCH 2/2] drm/amdkfd: use cache GTT buffer for PQ and wb pool

Christian/Lijo

We verified all approaches, and we know what works and not works, obviously the mb() doesn't work.

The way of mb() between set wptr_polling and kicking off doorbell is theoretically correct, and I'm not object to do so... but this won't resolve the issue we hit.
First of all, USWC will have some chance that the data is still in CPU's WC storage place and not flushed to the memory and even with MB() get rid of re-ordering GPU might still have a chance to read stalled data from ring buffer as long as it is mapped USWC.

This is why only cache plus snoop memory can get rid of inconsistence issues.

Besides, do you know what's the rational to keep all GFX KCQ cache+snoop but only HIQ/KIQ from KFD configured to USWC ?

For performance concern that looks to me always the second priority compared to "correct" especially under the case HIQ contributes very little to ROCM performance when switching to cache mapping.

Monk Liu | Cloud GPU & Virtualization | AMD

-----Original Message-----
From: Koenig, Christian <[email protected]>
Sent: Thursday, November 7, 2024 7:57 PM
To: Lazar, Lijo <[email protected]>; Alex Deucher <[email protected]>; Zhao, Victor <[email protected]>
Cc: [email protected]; Liu, Monk <[email protected]>; Yang, Philip <[email protected]>; Kuehling, Felix <[email protected]>
Subject: Re: [PATCH 2/2] drm/amdkfd: use cache GTT buffer for PQ and wb pool

Am 07.11.24 um 06:58 schrieb Lazar, Lijo:

Quoted 12 lines · Alex Deucher
Quoted 11 lines · Victor Zhao
Quoted 6 lines

From: Monk Liu <[email protected]>

As cache GTT buffer is snooped, this way the coherence between CPU write and GPU fetch is guaranteed, but original code uses WC + unsnooped for HIQ PQ(ring buffer) which introduces coherency issues:
MEC fetches a stall data from PQ and leads to MEC hang.

Can you elaborate on this? I can see CPU reads being slower because the memory is uncached, but the ring buffer is mostly writes anyway. IIRC, the driver uses USWC for most if not all of the other ring buffers managed by the kernel. Why aren't those a problem?

Quoted 6 lines

We have this on other rings -

         mb();
         amdgpu_ring_set_wptr(ring);

I think the solution should be to use barrier before write pointer updates rather than relying on PCIe snooping.

Yeah, completely agree as well. The barrier also takes care of preventing the compiler from re-ordering writes.

Regards,
Christian.

Quoted 28 lines

Thanks,
Lijo

Quoted 24 lines

Alex

Quoted 22 lines
---
  drivers/gpu/drm/amd/amdkfd/kfd_device.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/amd/amdkfd/kfd_device.c +1 -2
index 1f1d79ac5e6c..fb087a0ff5bc 100644
@@ -779,7 +779,7 @@ bool kgd2kfd_device_init(struct kfd_dev *kfd,
         if (amdgpu_amdkfd_alloc_gtt_mem(
                         kfd->adev, size, &kfd->gtt_mem,
                         &kfd->gtt_start_gpu_addr, &kfd->gtt_start_cpu_ptr,
-                       false, true)) {
+                       false, false)) {
                 dev_err(kfd_device, "Could not allocate %d bytes\n", size);
                 goto alloc_gtt_mem_failure;
         }
--

2.34.1

#9 lore raw

Re: [PATCH 2/2] drm/amdkfd: use cache GTT buffer for PQ and wb pool

Am 08.11.24 um 12:26 schrieb Lazar, Lijo:

Quoted 17 lines · Monk
Quoted 4 lines

[AMD Official Use Only - AMD Internal Distribution Only]

To be clear for the mb() approach: Even if we insert mb() in prior to amdgpu_ring_set_wptr(ring), GPU might still fetch stalled data from PQ due to USWC attributes.

Inserting an mb() doesn't cause WC buffer flush is a wrong assumption.

All prior loads/stores are supposed to be globally visible. Hence mb() followed by a write pointer update also should guarantee the same (From Arch manual).

The MFENCE instruction establishes a memory fence for both loads and
stores. The processor ensures that no load
or store after MFENCE will become globally visible *until all loads and stores before MFENCE are globally visible.*

Quoted 4 lines

Ignoring the amdgpu driver part of it - if mb() is not working as expected as you claim that means something is wrong with the system.

USWC or WB for ring type may still be a separate discussion.

Yeah completely agree. Additional to that the combine behavior between USWC and WB is the same, so the argumentation is clearly not correct.

What happens instead is that the broken KVM patch applies the guest caching attributes to the ring buffer instead of the host ones.

Making an educated guess I would say that the root of the problem is that the guest is using some incorrect caching attributes for some reason, which is exactly the same reason why that KVM patch was revert upstream as well.

Regards,
Christian.

Quoted 97 lines

Thanks,
Lijo

Quoted 93 lines

The issue here is not the re-ordering but the stalled PQ.

Monk Liu | Cloud GPU & Virtualization | AMD

-----Original Message-----
From: Liu, Monk
Sent: Friday, November 8, 2024 6:22 PM
To: Koenig, Christian <[email protected]>; Lazar, Lijo <[email protected]>; Alex Deucher <[email protected]>; Zhao, Victor <[email protected]>
Cc: [email protected]; Yang, Philip <[email protected]>; Kuehling, Felix <[email protected]>
Subject: RE: [PATCH 2/2] drm/amdkfd: use cache GTT buffer for PQ and wb pool

Christian/Lijo

We verified all approaches, and we know what works and not works, obviously the mb() doesn't work.

The way of mb() between set wptr_polling and kicking off doorbell is theoretically correct, and I'm not object to do so... but this won't resolve the issue we hit.
First of all, USWC will have some chance that the data is still in CPU's WC storage place and not flushed to the memory and even with MB() get rid of re-ordering GPU might still have a chance to read stalled data from ring buffer as long as it is mapped USWC.

This is why only cache plus snoop memory can get rid of inconsistence issues.

Besides, do you know what's the rational to keep all GFX KCQ cache+snoop but only HIQ/KIQ from KFD configured to USWC ?

For performance concern that looks to me always the second priority compared to "correct" especially under the case HIQ contributes very little to ROCM performance when switching to cache mapping.

Monk Liu | Cloud GPU & Virtualization | AMD

-----Original Message-----
From: Koenig, Christian <[email protected]>
Sent: Thursday, November 7, 2024 7:57 PM
To: Lazar, Lijo <[email protected]>; Alex Deucher <[email protected]>; Zhao, Victor <[email protected]>
Cc: [email protected]; Liu, Monk <[email protected]>; Yang, Philip <[email protected]>; Kuehling, Felix <[email protected]>
Subject: Re: [PATCH 2/2] drm/amdkfd: use cache GTT buffer for PQ and wb pool

Am 07.11.24 um 06:58 schrieb Lazar, Lijo:

Quoted 12 lines · Alex Deucher
Quoted 11 lines · Victor Zhao
Quoted 6 lines

From: Monk Liu <[email protected]>

As cache GTT buffer is snooped, this way the coherence between CPU write and GPU fetch is guaranteed, but original code uses WC + unsnooped for HIQ PQ(ring buffer) which introduces coherency issues:
MEC fetches a stall data from PQ and leads to MEC hang.

Can you elaborate on this? I can see CPU reads being slower because the memory is uncached, but the ring buffer is mostly writes anyway. IIRC, the driver uses USWC for most if not all of the other ring buffers managed by the kernel. Why aren't those a problem?

Quoted 6 lines

We have this on other rings -

          mb();
          amdgpu_ring_set_wptr(ring);

I think the solution should be to use barrier before write pointer updates rather than relying on PCIe snooping.

Yeah, completely agree as well. The barrier also takes care of preventing the compiler from re-ordering writes.

Regards,
Christian.

Quoted 27 lines

Thanks,
Lijo

Quoted 24 lines

Alex

Quoted 22 lines
---
   drivers/gpu/drm/amd/amdkfd/kfd_device.c | 2 +-
   1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/amd/amdkfd/kfd_device.c +1 -2
index 1f1d79ac5e6c..fb087a0ff5bc 100644
@@ -779,7 +779,7 @@ bool kgd2kfd_device_init(struct kfd_dev *kfd,
          if (amdgpu_amdkfd_alloc_gtt_mem(
                          kfd->adev, size, &kfd->gtt_mem,
                          &kfd->gtt_start_gpu_addr, &kfd->gtt_start_cpu_ptr,
-                       false, true)) {
+                       false, false)) {
                  dev_err(kfd_device, "Could not allocate %d bytes\n", size);
                  goto alloc_gtt_mem_failure;
          }
--

2.34.1

#10 lore raw

RE: [PATCH 2/2] drm/amdkfd: use cache GTT buffer for PQ and wb pool

[AMD Official Use Only - AMD Internal Distribution Only]

Christian

What do you mean "What happens instead is that the broken KVM patch applies the guest caching attributes to the ring buffer instead of the host ones."

From what I heard there was a KVM patch to correct the mapping behavior -- previously the mapping is forced to cache mode even guest KMD maps the buffer with "WC". But after the patch the buffer will be really "WC" if guest maps it with WC"... and this reveals the bug and hit our issue.

Can you explain why USWC for ring buffer is safe, why it will not hit coherence issue, why you don't make all ring buffers with USWC even for amdgpu side if you really believe USWC is innocent ...

Thanks

Monk Liu | Cloud GPU & Virtualization | AMD

-----Original Message-----
From: Christian König <[email protected]>
Sent: Friday, November 8, 2024 10:09 PM
To: Lazar, Lijo <[email protected]>; Liu, Monk <[email protected]>; Koenig, Christian <[email protected]>; Alex Deucher <[email protected]>; Zhao, Victor <[email protected]>
Cc: [email protected]; Yang, Philip <[email protected]>; Kuehling, Felix <[email protected]>
Subject: Re: [PATCH 2/2] drm/amdkfd: use cache GTT buffer for PQ and wb pool

Am 08.11.24 um 12:26 schrieb Lazar, Lijo:

Quoted 17 lines · Monk
Quoted 4 lines

[AMD Official Use Only - AMD Internal Distribution Only]

To be clear for the mb() approach: Even if we insert mb() in prior to amdgpu_ring_set_wptr(ring), GPU might still fetch stalled data from PQ due to USWC attributes.

Inserting an mb() doesn't cause WC buffer flush is a wrong assumption.

All prior loads/stores are supposed to be globally visible. Hence mb() followed by a write pointer update also should guarantee the same (From Arch manual).

The MFENCE instruction establishes a memory fence for both loads and
stores. The processor ensures that no load or store after MFENCE will become globally visible *until all loads and stores before MFENCE are globally visible.*

Quoted 4 lines

Ignoring the amdgpu driver part of it - if mb() is not working as expected as you claim that means something is wrong with the system.

USWC or WB for ring type may still be a separate discussion.

Yeah completely agree. Additional to that the combine behavior between USWC and WB is the same, so the argumentation is clearly not correct.

What happens instead is that the broken KVM patch applies the guest caching attributes to the ring buffer instead of the host ones.

Making an educated guess I would say that the root of the problem is that the guest is using some incorrect caching attributes for some reason, which is exactly the same reason why that KVM patch was revert upstream as well.

Regards,
Christian.

Quoted 105 lines

Thanks,
Lijo

Quoted 101 lines

The issue here is not the re-ordering but the stalled PQ.

Monk Liu | Cloud GPU & Virtualization | AMD

-----Original Message-----
From: Liu, Monk
Sent: Friday, November 8, 2024 6:22 PM
To: Koenig, Christian <[email protected]>; Lazar, Lijo <[email protected]>; Alex Deucher <[email protected]>; Zhao, Victor <[email protected]>
Cc: [email protected]; Yang, Philip <[email protected]>; Kuehling, Felix <[email protected]>
Subject: RE: [PATCH 2/2] drm/amdkfd: use cache GTT buffer for PQ and wb pool

Christian/Lijo

We verified all approaches, and we know what works and not works, obviously the mb() doesn't work.

The way of mb() between set wptr_polling and kicking off doorbell is theoretically correct, and I'm not object to do so... but this won't resolve the issue we hit.
First of all, USWC will have some chance that the data is still in CPU's WC storage place and not flushed to the memory and even with MB() get rid of re-ordering GPU might still have a chance to read stalled data from ring buffer as long as it is mapped USWC.

This is why only cache plus snoop memory can get rid of inconsistence issues.

Besides, do you know what's the rational to keep all GFX KCQ cache+snoop but only HIQ/KIQ from KFD configured to USWC ?

For performance concern that looks to me always the second priority compared to "correct" especially under the case HIQ contributes very little to ROCM performance when switching to cache mapping.

Monk Liu | Cloud GPU & Virtualization | AMD

-----Original Message-----
From: Koenig, Christian <[email protected]>
Sent: Thursday, November 7, 2024 7:57 PM
To: Lazar, Lijo <[email protected]>; Alex Deucher <[email protected]>; Zhao, Victor <[email protected]>
Cc: [email protected]; Liu, Monk <[email protected]>; Yang, Philip <[email protected]>; Kuehling, Felix <[email protected]>
Subject: Re: [PATCH 2/2] drm/amdkfd: use cache GTT buffer for PQ and wb pool

Am 07.11.24 um 06:58 schrieb Lazar, Lijo:

Quoted 12 lines · Alex Deucher
Quoted 11 lines · Victor Zhao
Quoted 6 lines

From: Monk Liu <[email protected]>

As cache GTT buffer is snooped, this way the coherence between CPU write and GPU fetch is guaranteed, but original code uses WC + unsnooped for HIQ PQ(ring buffer) which introduces coherency issues:
MEC fetches a stall data from PQ and leads to MEC hang.

Can you elaborate on this? I can see CPU reads being slower because the memory is uncached, but the ring buffer is mostly writes anyway. IIRC, the driver uses USWC for most if not all of the other ring buffers managed by the kernel. Why aren't those a problem?

Quoted 6 lines

We have this on other rings -

          mb();
          amdgpu_ring_set_wptr(ring);

I think the solution should be to use barrier before write pointer updates rather than relying on PCIe snooping.

Yeah, completely agree as well. The barrier also takes care of preventing the compiler from re-ordering writes.

Regards,
Christian.

Quoted 27 lines

Thanks,
Lijo

Quoted 24 lines

Alex

Quoted 22 lines
---
   drivers/gpu/drm/amd/amdkfd/kfd_device.c | 2 +-
   1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/amd/amdkfd/kfd_device.c +1 -2
index 1f1d79ac5e6c..fb087a0ff5bc 100644
@@ -779,7 +779,7 @@ bool kgd2kfd_device_init(struct kfd_dev *kfd,
          if (amdgpu_amdkfd_alloc_gtt_mem(
                          kfd->adev, size, &kfd->gtt_mem,
                          &kfd->gtt_start_gpu_addr, &kfd->gtt_start_cpu_ptr,
-                       false, true)) {
+                       false, false)) {
                  dev_err(kfd_device, "Could not allocate %d bytes\n", size);
                  goto alloc_gtt_mem_failure;
          }
--

2.34.1

#11 lore raw

RE: [PATCH 2/2] drm/amdkfd: use cache GTT buffer for PQ and wb pool

In reply to Lazar, Lijo

[AMD Official Use Only - AMD Internal Distribution Only]

Quoted 1 line
Quoted 1 line

All prior loads/stores are supposed to be globally visible. Hence mb() followed by a write pointer update also should guarantee the same (From Arch manual).

The MFENCE instruction establishes a memory fence for both loads and stores. The processor ensures that no load or store after MFENCE will become globally visible *until all loads and stores before MFENCE are globally visible.*

<<

Lijo, two things I shall state here:

1) from our experience it is not true, the WC storage buffer from CPU is not flushed when mfence presents, like I said, we did many .. many experiment before submitting this patch
2) from Intel's CPU spec (Customer's platform is with Intel CPU), the WC storage doesn't belong to the cache nor to the regular CPU internal storage place. (I can help to get more literal and formal descriptions later for you for double confirm, this is what I heard from customer during on-site support)
3) without this patch, when the issues happens, we can see the "last header dump" is 0, and you can see in KFD code the HIQ 's PQ was previously cleared by "memset()" with 0 and later followed the PM4 fulfilling, so the thing is the 0 is fetched by MEC but the corrected PM4 was somehow stuck in WC storage buffer occasionally.

At last, from any theorical perspective the ring buffer should be snooped to guarantee the coherence between CPU and GPU, again, I don't see any reason not to honor this point for HIQ given the statues of all GFX queues, and even the user queues allocated by H-S-A runtime is also cache mapping.

Monk Liu | Cloud GPU & Virtualization | AMD

-----Original Message-----
From: Lazar, Lijo <[email protected]>
Sent: Friday, November 8, 2024 7:26 PM
To: Liu, Monk <[email protected]>; Koenig, Christian <[email protected]>; Alex Deucher <[email protected]>; Zhao, Victor <[email protected]>
Cc: [email protected]; Yang, Philip <[email protected]>; Kuehling, Felix <[email protected]>
Subject: Re: [PATCH 2/2] drm/amdkfd: use cache GTT buffer for PQ and wb pool

Quoted 4 lines · Monk

[AMD Official Use Only - AMD Internal Distribution Only]

To be clear for the mb() approach: Even if we insert mb() in prior to amdgpu_ring_set_wptr(ring), GPU might still fetch stalled data from PQ due to USWC attributes.

Inserting an mb() doesn't cause WC buffer flush is a wrong assumption.

All prior loads/stores are supposed to be globally visible. Hence mb() followed by a write pointer update also should guarantee the same (From Arch manual).

The MFENCE instruction establishes a memory fence for both loads and stores. The processor ensures that no load or store after MFENCE will become globally visible *until all loads and stores before MFENCE are globally visible.*

Ignoring the amdgpu driver part of it - if mb() is not working as expected as you claim that means something is wrong with the system.

USWC or WB for ring type may still be a separate discussion.

Thanks,
Lijo

Quoted 103 lines

The issue here is not the re-ordering but the stalled PQ.

Monk Liu | Cloud GPU & Virtualization | AMD

-----Original Message-----
From: Liu, Monk
Sent: Friday, November 8, 2024 6:22 PM
To: Koenig, Christian <[email protected]>; Lazar, Lijo <[email protected]>; Alex Deucher <[email protected]>; Zhao, Victor <[email protected]>
Cc: [email protected]; Yang, Philip <[email protected]>; Kuehling, Felix <[email protected]>
Subject: RE: [PATCH 2/2] drm/amdkfd: use cache GTT buffer for PQ and wb pool

Christian/Lijo

We verified all approaches, and we know what works and not works, obviously the mb() doesn't work.

The way of mb() between set wptr_polling and kicking off doorbell is theoretically correct, and I'm not object to do so... but this won't resolve the issue we hit.
First of all, USWC will have some chance that the data is still in CPU's WC storage place and not flushed to the memory and even with MB() get rid of re-ordering GPU might still have a chance to read stalled data from ring buffer as long as it is mapped USWC.

This is why only cache plus snoop memory can get rid of inconsistence issues.

Besides, do you know what's the rational to keep all GFX KCQ cache+snoop but only HIQ/KIQ from KFD configured to USWC ?

For performance concern that looks to me always the second priority compared to "correct" especially under the case HIQ contributes very little to ROCM performance when switching to cache mapping.

Monk Liu | Cloud GPU & Virtualization | AMD

-----Original Message-----
From: Koenig, Christian <[email protected]>
Sent: Thursday, November 7, 2024 7:57 PM
To: Lazar, Lijo <[email protected]>; Alex Deucher <[email protected]>; Zhao, Victor <[email protected]>
Cc: [email protected]; Liu, Monk <[email protected]>; Yang, Philip <[email protected]>; Kuehling, Felix <[email protected]>
Subject: Re: [PATCH 2/2] drm/amdkfd: use cache GTT buffer for PQ and wb pool

Am 07.11.24 um 06:58 schrieb Lazar, Lijo:

Quoted 12 lines · Alex Deucher
Quoted 11 lines · Victor Zhao
Quoted 6 lines

From: Monk Liu <[email protected]>

As cache GTT buffer is snooped, this way the coherence between CPU write and GPU fetch is guaranteed, but original code uses WC + unsnooped for HIQ PQ(ring buffer) which introduces coherency issues:
MEC fetches a stall data from PQ and leads to MEC hang.

Can you elaborate on this? I can see CPU reads being slower because the memory is uncached, but the ring buffer is mostly writes anyway. IIRC, the driver uses USWC for most if not all of the other ring buffers managed by the kernel. Why aren't those a problem?

Quoted 6 lines

We have this on other rings -

         mb();
         amdgpu_ring_set_wptr(ring);

I think the solution should be to use barrier before write pointer updates rather than relying on PCIe snooping.

Yeah, completely agree as well. The barrier also takes care of preventing the compiler from re-ordering writes.

Regards,
Christian.

Quoted 28 lines

Thanks,
Lijo

Quoted 24 lines

Alex

Quoted 22 lines
---
  drivers/gpu/drm/amd/amdkfd/kfd_device.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/amd/amdkfd/kfd_device.c +1 -2
index 1f1d79ac5e6c..fb087a0ff5bc 100644
@@ -779,7 +779,7 @@ bool kgd2kfd_device_init(struct kfd_dev *kfd,
         if (amdgpu_amdkfd_alloc_gtt_mem(
                         kfd->adev, size, &kfd->gtt_mem,
                         &kfd->gtt_start_gpu_addr, &kfd->gtt_start_cpu_ptr,
-                       false, true)) {
+                       false, false)) {
                 dev_err(kfd_device, "Could not allocate %d bytes\n", size);
                 goto alloc_gtt_mem_failure;
         }
--

2.34.1

#12 lore raw

RE: [PATCH 2/2] drm/amdkfd: use cache GTT buffer for PQ and wb pool

In reply to Lazar, Lijo

[AMD Official Use Only - AMD Internal Distribution Only]

Hi Lijo

This is the patch we verified before:

drivers/gpu/drm/amd/amdkfd/kfd_kernel_queue.c +2 -0
index 4843dcb9a5f7..39553c7648eb 100644
@@ -308,10 +308,12 @@ int kq_submit_packet(struct kernel_queue *kq)
 
        if (kq->dev->kfd->device_info.doorbell_size == 8) {
                *kq->wptr64_kernel = kq->pending_wptr64;
+               mb();
                write_kernel_doorbell64(kq->queue->properties.doorbell_ptr,
                                        kq->pending_wptr64);
        } else {
                *kq->wptr_kernel = kq->pending_wptr;
+               mb();
                write_kernel_doorbell(kq->queue->properties.doorbell_ptr,
                                        kq->pending_wptr);
        }

This mb() doesn't resolve the problem during customer's testing, I also thought of MB() first in beginning like you and Christian ...
The mb() here shall resolve the re-ordering between WPTR and doorbell kicking so GPU won't fetch stalled data from WPTR polling once it receives notification from doorbell kicking. (in SR-IOV we set doorbell mode to force GPU still fetch from WPTR polling area, doorbell kicking is just to notify GPU)

And by your theory: mb() shall flush the WC storage buffer to memory, thus, this mb() shall also make sure that the ring buffer is not holding stalled data anymore, right ? But we still hit hang and get stalled data from dump.

Maybe we need to put mb() in another place ? can you proposal if you insist the cache mapping is not acceptable to you and we can ask customer to verify again.

Thanks

Monk Liu | Cloud GPU & Virtualization | AMD

-----Original Message-----
From: Lazar, Lijo <[email protected]>
Sent: Friday, November 8, 2024 7:26 PM
To: Liu, Monk <[email protected]>; Koenig, Christian <[email protected]>; Alex Deucher <[email protected]>; Zhao, Victor <[email protected]>
Cc: [email protected]; Yang, Philip <[email protected]>; Kuehling, Felix <[email protected]>
Subject: Re: [PATCH 2/2] drm/amdkfd: use cache GTT buffer for PQ and wb pool

Quoted 4 lines · Monk

[AMD Official Use Only - AMD Internal Distribution Only]

To be clear for the mb() approach: Even if we insert mb() in prior to amdgpu_ring_set_wptr(ring), GPU might still fetch stalled data from PQ due to USWC attributes.

Inserting an mb() doesn't cause WC buffer flush is a wrong assumption.

All prior loads/stores are supposed to be globally visible. Hence mb() followed by a write pointer update also should guarantee the same (From Arch manual).

The MFENCE instruction establishes a memory fence for both loads and stores. The processor ensures that no load or store after MFENCE will become globally visible *until all loads and stores before MFENCE are globally visible.*

Ignoring the amdgpu driver part of it - if mb() is not working as expected as you claim that means something is wrong with the system.

USWC or WB for ring type may still be a separate discussion.

Thanks,
Lijo

Quoted 103 lines

The issue here is not the re-ordering but the stalled PQ.

Monk Liu | Cloud GPU & Virtualization | AMD

-----Original Message-----
From: Liu, Monk
Sent: Friday, November 8, 2024 6:22 PM
To: Koenig, Christian <[email protected]>; Lazar, Lijo <[email protected]>; Alex Deucher <[email protected]>; Zhao, Victor <[email protected]>
Cc: [email protected]; Yang, Philip <[email protected]>; Kuehling, Felix <[email protected]>
Subject: RE: [PATCH 2/2] drm/amdkfd: use cache GTT buffer for PQ and wb pool

Christian/Lijo

We verified all approaches, and we know what works and not works, obviously the mb() doesn't work.

The way of mb() between set wptr_polling and kicking off doorbell is theoretically correct, and I'm not object to do so... but this won't resolve the issue we hit.
First of all, USWC will have some chance that the data is still in CPU's WC storage place and not flushed to the memory and even with MB() get rid of re-ordering GPU might still have a chance to read stalled data from ring buffer as long as it is mapped USWC.

This is why only cache plus snoop memory can get rid of inconsistence issues.

Besides, do you know what's the rational to keep all GFX KCQ cache+snoop but only HIQ/KIQ from KFD configured to USWC ?

For performance concern that looks to me always the second priority compared to "correct" especially under the case HIQ contributes very little to ROCM performance when switching to cache mapping.

Monk Liu | Cloud GPU & Virtualization | AMD

-----Original Message-----
From: Koenig, Christian <[email protected]>
Sent: Thursday, November 7, 2024 7:57 PM
To: Lazar, Lijo <[email protected]>; Alex Deucher <[email protected]>; Zhao, Victor <[email protected]>
Cc: [email protected]; Liu, Monk <[email protected]>; Yang, Philip <[email protected]>; Kuehling, Felix <[email protected]>
Subject: Re: [PATCH 2/2] drm/amdkfd: use cache GTT buffer for PQ and wb pool

Am 07.11.24 um 06:58 schrieb Lazar, Lijo:

Quoted 12 lines · Alex Deucher
Quoted 11 lines · Victor Zhao
Quoted 6 lines

From: Monk Liu <[email protected]>

As cache GTT buffer is snooped, this way the coherence between CPU write and GPU fetch is guaranteed, but original code uses WC + unsnooped for HIQ PQ(ring buffer) which introduces coherency issues:
MEC fetches a stall data from PQ and leads to MEC hang.

Can you elaborate on this? I can see CPU reads being slower because the memory is uncached, but the ring buffer is mostly writes anyway. IIRC, the driver uses USWC for most if not all of the other ring buffers managed by the kernel. Why aren't those a problem?

Quoted 6 lines

We have this on other rings -

         mb();
         amdgpu_ring_set_wptr(ring);

I think the solution should be to use barrier before write pointer updates rather than relying on PCIe snooping.

Yeah, completely agree as well. The barrier also takes care of preventing the compiler from re-ordering writes.

Regards,
Christian.

Quoted 28 lines

Thanks,
Lijo

Quoted 24 lines

Alex

Quoted 22 lines
---
  drivers/gpu/drm/amd/amdkfd/kfd_device.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/amd/amdkfd/kfd_device.c +1 -2
index 1f1d79ac5e6c..fb087a0ff5bc 100644
@@ -779,7 +779,7 @@ bool kgd2kfd_device_init(struct kfd_dev *kfd,
         if (amdgpu_amdkfd_alloc_gtt_mem(
                         kfd->adev, size, &kfd->gtt_mem,
                         &kfd->gtt_start_gpu_addr, &kfd->gtt_start_cpu_ptr,
-                       false, true)) {
+                       false, false)) {
                 dev_err(kfd_device, "Could not allocate %d bytes\n", size);
                 goto alloc_gtt_mem_failure;
         }
--

2.34.1

#13 lore raw

Re: [PATCH 2/2] drm/amdkfd: use cache GTT buffer for PQ and wb pool

Quoted 25 lines · Monk

[AMD Official Use Only - AMD Internal Distribution Only]

Hi Lijo

This is the patch we verified before:

drivers/gpu/drm/amd/amdkfd/kfd_kernel_queue.c +2 -0
index 4843dcb9a5f7..39553c7648eb 100644
@@ -308,10 +308,12 @@ int kq_submit_packet(struct kernel_queue *kq)
 
        if (kq->dev->kfd->device_info.doorbell_size == 8) {
                *kq->wptr64_kernel = kq->pending_wptr64;
+               mb();
                write_kernel_doorbell64(kq->queue->properties.doorbell_ptr,
                                        kq->pending_wptr64);
        } else {
                *kq->wptr_kernel = kq->pending_wptr;
+               mb();
                write_kernel_doorbell(kq->queue->properties.doorbell_ptr,
                                        kq->pending_wptr);
        }
Quoted 5 lines

This mb() doesn't resolve the problem during customer's testing, I also thought of MB() first in beginning like you and Christian ...
The mb() here shall resolve the re-ordering between WPTR and doorbell kicking so GPU won't fetch stalled data from WPTR polling once it receives notification from doorbell kicking. (in SR-IOV we set doorbell mode to force GPU still fetch from WPTR polling area, doorbell kicking is just to notify GPU)

And by your theory: mb() shall flush the WC storage buffer to memory, thus, this mb() shall also make sure that the ring buffer is not holding stalled data anymore, right ?

This is not my theory. All the quotes in my earlier mails are from "Intel® 64 and IA-32 Architectures Software Developer’s Manual".

Yet another one -

<snip>

Writes may be delayed and combined in the write combining buffer (WC buffer) to reduce memory accesses.

"If the WC buffer is partially filled, the writes may be delayed until the next occurrence of a serializing event; such as an SFENCE or MFENCE instruction, CPUID or other serializing instruction, a read or write to uncached memory, an
interrupt occurrence, or an execution of a LOCK instruction (including one with an XACQUIRE or XRELEASE
prefix)."

</snip>

Quoted 4 lines

But we still hit hang and get stalled data from dump.

Maybe we need to put mb() in another place ? can you proposal if you insist the cache mapping is not acceptable to you and we can ask customer to verify again.

There are a couple of things which are still working strangely, they will need some explanation as well -

Even though write pointer allocations are also to WC region, and are
correctly seen by CP every time. Since it needs to get wptr updates from memory rather than doorbell value, don't know how your snooping theory works for this. Also, it is weird that WC writes to those pass MQD buffer writes even with fence.

MQD allocation for user queues are still from USWC as per your latest
patch and they still work correctly.

For debug -
Is the WPTR reg value correctly reflecting the memory value? Have you
tried initializing MQD to a known pattern (memset32/64) like a series of 0xcafedead and verified what is seen at the CP side?

Thanks,
Lijo

Quoted 141 lines

Thanks

Monk Liu | Cloud GPU & Virtualization | AMD

-----Original Message-----
From: Lazar, Lijo <[email protected]>
Sent: Friday, November 8, 2024 7:26 PM
To: Liu, Monk <[email protected]>; Koenig, Christian <[email protected]>; Alex Deucher <[email protected]>; Zhao, Victor <[email protected]>
Cc: [email protected]; Yang, Philip <[email protected]>; Kuehling, Felix <[email protected]>
Subject: Re: [PATCH 2/2] drm/amdkfd: use cache GTT buffer for PQ and wb pool

Quoted 4 lines · Monk

[AMD Official Use Only - AMD Internal Distribution Only]

To be clear for the mb() approach: Even if we insert mb() in prior to amdgpu_ring_set_wptr(ring), GPU might still fetch stalled data from PQ due to USWC attributes.

Inserting an mb() doesn't cause WC buffer flush is a wrong assumption.

All prior loads/stores are supposed to be globally visible. Hence mb() followed by a write pointer update also should guarantee the same (From Arch manual).

The MFENCE instruction establishes a memory fence for both loads and stores. The processor ensures that no load or store after MFENCE will become globally visible *until all loads and stores before MFENCE are globally visible.*

Ignoring the amdgpu driver part of it - if mb() is not working as expected as you claim that means something is wrong with the system.

USWC or WB for ring type may still be a separate discussion.

Thanks,
Lijo

Quoted 103 lines

The issue here is not the re-ordering but the stalled PQ.

Monk Liu | Cloud GPU & Virtualization | AMD

-----Original Message-----
From: Liu, Monk
Sent: Friday, November 8, 2024 6:22 PM
To: Koenig, Christian <[email protected]>; Lazar, Lijo <[email protected]>; Alex Deucher <[email protected]>; Zhao, Victor <[email protected]>
Cc: [email protected]; Yang, Philip <[email protected]>; Kuehling, Felix <[email protected]>
Subject: RE: [PATCH 2/2] drm/amdkfd: use cache GTT buffer for PQ and wb pool

Christian/Lijo

We verified all approaches, and we know what works and not works, obviously the mb() doesn't work.

The way of mb() between set wptr_polling and kicking off doorbell is theoretically correct, and I'm not object to do so... but this won't resolve the issue we hit.
First of all, USWC will have some chance that the data is still in CPU's WC storage place and not flushed to the memory and even with MB() get rid of re-ordering GPU might still have a chance to read stalled data from ring buffer as long as it is mapped USWC.

This is why only cache plus snoop memory can get rid of inconsistence issues.

Besides, do you know what's the rational to keep all GFX KCQ cache+snoop but only HIQ/KIQ from KFD configured to USWC ?

For performance concern that looks to me always the second priority compared to "correct" especially under the case HIQ contributes very little to ROCM performance when switching to cache mapping.

Monk Liu | Cloud GPU & Virtualization | AMD

-----Original Message-----
From: Koenig, Christian <[email protected]>
Sent: Thursday, November 7, 2024 7:57 PM
To: Lazar, Lijo <[email protected]>; Alex Deucher <[email protected]>; Zhao, Victor <[email protected]>
Cc: [email protected]; Liu, Monk <[email protected]>; Yang, Philip <[email protected]>; Kuehling, Felix <[email protected]>
Subject: Re: [PATCH 2/2] drm/amdkfd: use cache GTT buffer for PQ and wb pool

Am 07.11.24 um 06:58 schrieb Lazar, Lijo:

Quoted 12 lines · Alex Deucher
Quoted 11 lines · Victor Zhao
Quoted 6 lines

From: Monk Liu <[email protected]>

As cache GTT buffer is snooped, this way the coherence between CPU write and GPU fetch is guaranteed, but original code uses WC + unsnooped for HIQ PQ(ring buffer) which introduces coherency issues:
MEC fetches a stall data from PQ and leads to MEC hang.

Can you elaborate on this? I can see CPU reads being slower because the memory is uncached, but the ring buffer is mostly writes anyway. IIRC, the driver uses USWC for most if not all of the other ring buffers managed by the kernel. Why aren't those a problem?

Quoted 6 lines

We have this on other rings -

         mb();
         amdgpu_ring_set_wptr(ring);

I think the solution should be to use barrier before write pointer updates rather than relying on PCIe snooping.

Yeah, completely agree as well. The barrier also takes care of preventing the compiler from re-ordering writes.

Regards,
Christian.

Quoted 28 lines

Thanks,
Lijo

Quoted 24 lines

Alex

Quoted 22 lines
---
  drivers/gpu/drm/amd/amdkfd/kfd_device.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/amd/amdkfd/kfd_device.c +1 -2
index 1f1d79ac5e6c..fb087a0ff5bc 100644
@@ -779,7 +779,7 @@ bool kgd2kfd_device_init(struct kfd_dev *kfd,
         if (amdgpu_amdkfd_alloc_gtt_mem(
                         kfd->adev, size, &kfd->gtt_mem,
                         &kfd->gtt_start_gpu_addr, &kfd->gtt_start_cpu_ptr,
-                       false, true)) {
+                       false, false)) {
                 dev_err(kfd_device, "Could not allocate %d bytes\n", size);
                 goto alloc_gtt_mem_failure;
         }
--

2.34.1

#14 lore raw

Re: [PATCH 2/2] drm/amdkfd: use cache GTT buffer for PQ and wb pool

Hi guys,

trying to merge multiple responses back into one mail thread.

Lijo, you're completely right, but there is one really strong argument you are missing:

The patch here doesn't change the write combine behavior, it changes the caching behavior!

So when the patch works and fixes the issue, then the issue is 100% certain not a write combine issue but rather an incorrectly applied caching :)

Quoted 2 lines

From what I heard there was a KVM patch to correct the mapping behavior -- previously the mapping is forced to cache mode even guest KMD maps the buffer with "WC".
But after the patch the buffer will be really "WC" if guest maps it with WC"... and this reveals the bug and hit our issue.

Yeah, exactly that's the problem. The guest doesn't know if WB, USWC or uncached should be used!

The guest sees driver exposed memory as "emulated" MMIO BAR of a PCIe device, that's just how QEMU is designed. Because of this the guest maps the memory USWC or uncached. But in reality the memory can as well be GTT memory which is cached.

So forcing the cache mode even if the guest KMD maps the buffer with "WC" is intentional behavior, that's the correct approach.

The upstream people realized that as well. That's one major reason why the patch was reverted.

Quoted 1 line

Can you explain why USWC for ring buffer is safe, why it will not hit coherence issue, why you don't make all ring buffers with USWC even for amdgpu side if you really believe USWC is innocent ...

We already tried this, but there is simply no benefit for it.

Regards,
Christian.

Am 11.11.24 um 06:58 schrieb Lazar, Lijo:

Quoted 215 lines · Monk
Quoted 25 lines

[AMD Official Use Only - AMD Internal Distribution Only]

Hi Lijo

This is the patch we verified before:

drivers/gpu/drm/amd/amdkfd/kfd_kernel_queue.c +2 -0
index 4843dcb9a5f7..39553c7648eb 100644
@@ -308,10 +308,12 @@ int kq_submit_packet(struct kernel_queue *kq)
 
         if (kq->dev->kfd->device_info.doorbell_size == 8) {
                 *kq->wptr64_kernel = kq->pending_wptr64;
+               mb();
                 write_kernel_doorbell64(kq->queue->properties.doorbell_ptr,
                                         kq->pending_wptr64);
         } else {
                 *kq->wptr_kernel = kq->pending_wptr;
+               mb();
                 write_kernel_doorbell(kq->queue->properties.doorbell_ptr,
                                         kq->pending_wptr);
         }
Quoted 5 lines

This mb() doesn't resolve the problem during customer's testing, I also thought of MB() first in beginning like you and Christian ...
The mb() here shall resolve the re-ordering between WPTR and doorbell kicking so GPU won't fetch stalled data from WPTR polling once it receives notification from doorbell kicking. (in SR-IOV we set doorbell mode to force GPU still fetch from WPTR polling area, doorbell kicking is just to notify GPU)

And by your theory: mb() shall flush the WC storage buffer to memory, thus, this mb() shall also make sure that the ring buffer is not holding stalled data anymore, right ?

This is not my theory. All the quotes in my earlier mails are from "Intel® 64 and IA-32 Architectures Software Developer’s Manual".

Yet another one -

<snip>

Writes may be delayed and combined in the write combining buffer (WC buffer) to reduce memory accesses.

"If the WC buffer is partially filled, the writes may be delayed until the next occurrence of a serializing event; such as an SFENCE or MFENCE instruction, CPUID or other serializing instruction, a read or write to uncached memory, an
interrupt occurrence, or an execution of a LOCK instruction (including one with an XACQUIRE or XRELEASE
prefix)."

</snip>

Quoted 4 lines

But we still hit hang and get stalled data from dump.

Maybe we need to put mb() in another place ? can you proposal if you insist the cache mapping is not acceptable to you and we can ask customer to verify again.

There are a couple of things which are still working strangely, they will need some explanation as well -

Even though write pointer allocations are also to WC region, and are
correctly seen by CP every time. Since it needs to get wptr updates from memory rather than doorbell value, don't know how your snooping theory works for this. Also, it is weird that WC writes to those pass MQD buffer writes even with fence.

MQD allocation for user queues are still from USWC as per your latest
patch and they still work correctly.

For debug -
Is the WPTR reg value correctly reflecting the memory value? Have you
tried initializing MQD to a known pattern (memset32/64) like a series of 0xcafedead and verified what is seen at the CP side?

Thanks,
Lijo

Quoted 138 lines

Thanks

Monk Liu | Cloud GPU & Virtualization | AMD

-----Original Message-----
From: Lazar, Lijo <[email protected]>
Sent: Friday, November 8, 2024 7:26 PM
To: Liu, Monk <[email protected]>; Koenig, Christian <[email protected]>; Alex Deucher <[email protected]>; Zhao, Victor <[email protected]>
Cc: [email protected]; Yang, Philip <[email protected]>; Kuehling, Felix <[email protected]>
Subject: Re: [PATCH 2/2] drm/amdkfd: use cache GTT buffer for PQ and wb pool

Quoted 4 lines · Monk

[AMD Official Use Only - AMD Internal Distribution Only]

To be clear for the mb() approach: Even if we insert mb() in prior to amdgpu_ring_set_wptr(ring), GPU might still fetch stalled data from PQ due to USWC attributes.

Inserting an mb() doesn't cause WC buffer flush is a wrong assumption.

All prior loads/stores are supposed to be globally visible. Hence mb() followed by a write pointer update also should guarantee the same (From Arch manual).

The MFENCE instruction establishes a memory fence for both loads and stores. The processor ensures that no load or store after MFENCE will become globally visible *until all loads and stores before MFENCE are globally visible.*

Ignoring the amdgpu driver part of it - if mb() is not working as expected as you claim that means something is wrong with the system.

USWC or WB for ring type may still be a separate discussion.

Thanks,
Lijo

Quoted 100 lines

The issue here is not the re-ordering but the stalled PQ.

Monk Liu | Cloud GPU & Virtualization | AMD

-----Original Message-----
From: Liu, Monk
Sent: Friday, November 8, 2024 6:22 PM
To: Koenig, Christian <[email protected]>; Lazar, Lijo <[email protected]>; Alex Deucher <[email protected]>; Zhao, Victor <[email protected]>
Cc: [email protected]; Yang, Philip <[email protected]>; Kuehling, Felix <[email protected]>
Subject: RE: [PATCH 2/2] drm/amdkfd: use cache GTT buffer for PQ and wb pool

Christian/Lijo

We verified all approaches, and we know what works and not works, obviously the mb() doesn't work.

The way of mb() between set wptr_polling and kicking off doorbell is theoretically correct, and I'm not object to do so... but this won't resolve the issue we hit.
First of all, USWC will have some chance that the data is still in CPU's WC storage place and not flushed to the memory and even with MB() get rid of re-ordering GPU might still have a chance to read stalled data from ring buffer as long as it is mapped USWC.

This is why only cache plus snoop memory can get rid of inconsistence issues.

Besides, do you know what's the rational to keep all GFX KCQ cache+snoop but only HIQ/KIQ from KFD configured to USWC ?

For performance concern that looks to me always the second priority compared to "correct" especially under the case HIQ contributes very little to ROCM performance when switching to cache mapping.

Monk Liu | Cloud GPU & Virtualization | AMD

-----Original Message-----
From: Koenig, Christian <[email protected]>
Sent: Thursday, November 7, 2024 7:57 PM
To: Lazar, Lijo <[email protected]>; Alex Deucher <[email protected]>; Zhao, Victor <[email protected]>
Cc: [email protected]; Liu, Monk <[email protected]>; Yang, Philip <[email protected]>; Kuehling, Felix <[email protected]>
Subject: Re: [PATCH 2/2] drm/amdkfd: use cache GTT buffer for PQ and wb pool

Am 07.11.24 um 06:58 schrieb Lazar, Lijo:

Quoted 12 lines · Alex Deucher
Quoted 11 lines · Victor Zhao
Quoted 6 lines

From: Monk Liu <[email protected]>

As cache GTT buffer is snooped, this way the coherence between CPU write and GPU fetch is guaranteed, but original code uses WC + unsnooped for HIQ PQ(ring buffer) which introduces coherency issues:
MEC fetches a stall data from PQ and leads to MEC hang.

Can you elaborate on this? I can see CPU reads being slower because the memory is uncached, but the ring buffer is mostly writes anyway. IIRC, the driver uses USWC for most if not all of the other ring buffers managed by the kernel. Why aren't those a problem?

Quoted 6 lines

We have this on other rings -

          mb();
          amdgpu_ring_set_wptr(ring);

I think the solution should be to use barrier before write pointer updates rather than relying on PCIe snooping.

Yeah, completely agree as well. The barrier also takes care of preventing the compiler from re-ordering writes.

Regards,
Christian.

Quoted 27 lines

Thanks,
Lijo

Quoted 24 lines

Alex

Quoted 22 lines
---
   drivers/gpu/drm/amd/amdkfd/kfd_device.c | 2 +-
   1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/amd/amdkfd/kfd_device.c +1 -2
index 1f1d79ac5e6c..fb087a0ff5bc 100644
@@ -779,7 +779,7 @@ bool kgd2kfd_device_init(struct kfd_dev *kfd,
          if (amdgpu_amdkfd_alloc_gtt_mem(
                          kfd->adev, size, &kfd->gtt_mem,
                          &kfd->gtt_start_gpu_addr, &kfd->gtt_start_cpu_ptr,
-                       false, true)) {
+                       false, false)) {
                  dev_err(kfd_device, "Could not allocate %d bytes\n", size);
                  goto alloc_gtt_mem_failure;
          }
--

2.34.1

#15 lore raw

RE: [PATCH 2/2] drm/amdkfd: use cache GTT buffer for PQ and wb pool

[AMD Official Use Only - AMD Internal Distribution Only]

Hi Lijo

Thanks for your quote to the SPEC, yes I read that as well, supposedly WC storage buffer shall be flushed to memory with mfence, but it just doesn't work after applied that KVM patch which honor the WC attributes from guest.

This means the WC storage buffer from a KVM guest couldn’t be flushed to memory even with mb inserted... I don't know why. (is there any inconsistencies between nested page tables and host page tables?)

Hi Christian

Quoted 1 line
Quoted 1 line

The guest sees driver exposed memory as "emulated" MMIO BAR of a PCIe device, that's just how QEMU is designed. Because of this the guest maps the memory USWC or uncached. But in reality the memory can as well be GTT memory which is cached.

The memory we are talking about is pure guest system memory not FB BAR right? Therefore, I don't see why in reality it has to be cached, you see that only guest vCPU is accessing this buffer.

BTW: we should aways insert that MB() in our KFD part:

Quoted 12 lines

drivers/gpu/drm/amd/amdkfd/kfd_kernel_queue.c int kq_submit_packet(struct kernel_queue *kq) · line 319

Quoted 12 lines
         if (kq->dev->kfd->device_info.doorbell_size == 8) {
                 *kq->wptr64_kernel = kq->pending_wptr64;
drivers/gpu/drm/amd/amdkfd/kfd_kernel_queue.c +2 -0
+               mb();
                 write_kernel_doorbell64(kq->queue->properties.doorbell_ptr,
                                         kq->pending_wptr64);
         } else {
                 *kq->wptr_kernel = kq->pending_wptr;
+               mb();
                 write_kernel_doorbell(kq->queue->properties.doorbell_ptr,
                                         kq->pending_wptr);
         }

Thanks

Monk Liu | Cloud GPU & Virtualization | AMD

-----Original Message-----
From: Christian König <[email protected]>
Sent: Monday, November 11, 2024 8:40 PM
To: Lazar, Lijo <[email protected]>; Liu, Monk <[email protected]>; Koenig, Christian <[email protected]>; Alex Deucher <[email protected]>; Zhao, Victor <[email protected]>
Cc: [email protected]; Yang, Philip <[email protected]>; Kuehling, Felix <[email protected]>
Subject: Re: [PATCH 2/2] drm/amdkfd: use cache GTT buffer for PQ and wb pool

Hi guys,

trying to merge multiple responses back into one mail thread.

Lijo, you're completely right, but there is one really strong argument you are missing:

The patch here doesn't change the write combine behavior, it changes the caching behavior!

So when the patch works and fixes the issue, then the issue is 100% certain not a write combine issue but rather an incorrectly applied caching :)

Quoted 2 lines

From what I heard there was a KVM patch to correct the mapping behavior -- previously the mapping is forced to cache mode even guest KMD maps the buffer with "WC".
But after the patch the buffer will be really "WC" if guest maps it with WC"... and this reveals the bug and hit our issue.

Yeah, exactly that's the problem. The guest doesn't know if WB, USWC or uncached should be used!

The guest sees driver exposed memory as "emulated" MMIO BAR of a PCIe device, that's just how QEMU is designed. Because of this the guest maps the memory USWC or uncached. But in reality the memory can as well be GTT memory which is cached.

So forcing the cache mode even if the guest KMD maps the buffer with "WC" is intentional behavior, that's the correct approach.

The upstream people realized that as well. That's one major reason why the patch was reverted.

Quoted 1 line

Can you explain why USWC for ring buffer is safe, why it will not hit coherence issue, why you don't make all ring buffers with USWC even for amdgpu side if you really believe USWC is innocent ...

We already tried this, but there is simply no benefit for it.

Regards,
Christian.

Am 11.11.24 um 06:58 schrieb Lazar, Lijo:

Quoted 224 lines · Monk
Quoted 26 lines

[AMD Official Use Only - AMD Internal Distribution Only]

Hi Lijo

This is the patch we verified before:

drivers/gpu/drm/amd/amdkfd/kfd_kernel_queue.c +2 -0
index 4843dcb9a5f7..39553c7648eb 100644
@@ -308,10 +308,12 @@ int kq_submit_packet(struct kernel_queue *kq)
 
         if (kq->dev->kfd->device_info.doorbell_size == 8) {
                 *kq->wptr64_kernel = kq->pending_wptr64;
+               mb();
                 write_kernel_doorbell64(kq->queue->properties.doorbell_ptr,
                                         kq->pending_wptr64);
         } else {
                 *kq->wptr_kernel = kq->pending_wptr;
+               mb();
                 write_kernel_doorbell(kq->queue->properties.doorbell_ptr,
                                         kq->pending_wptr);
         }
Quoted 6 lines

This mb() doesn't resolve the problem during customer's testing, I also thought of MB() first in beginning like you and Christian ...
The mb() here shall resolve the re-ordering between WPTR and doorbell kicking so GPU won't fetch stalled data from WPTR polling once it receives notification from doorbell kicking. (in SR-IOV we set doorbell mode to force GPU still fetch from WPTR
polling area, doorbell kicking is just to notify GPU)

And by your theory: mb() shall flush the WC storage buffer to memory, thus, this mb() shall also make sure that the ring buffer is not holding stalled data anymore, right ?

This is not my theory. All the quotes in my earlier mails are from "Intel® 64 and IA-32 Architectures Software Developer’s Manual".

Yet another one -

<snip>

Writes may be delayed and combined in the write combining buffer (WC buffer) to reduce memory accesses.

"If the WC buffer is partially filled, the writes may be delayed until the next occurrence of a serializing event; such as an SFENCE or MFENCE instruction, CPUID or other serializing instruction, a read or write to uncached memory, an interrupt occurrence, or an execution of a LOCK instruction (including one with an XACQUIRE or XRELEASE prefix)."

</snip>

Quoted 4 lines

But we still hit hang and get stalled data from dump.

Maybe we need to put mb() in another place ? can you proposal if you insist the cache mapping is not acceptable to you and we can ask customer to verify again.

There are a couple of things which are still working strangely, they will need some explanation as well -

Even though write pointer allocations are also to WC region, and are
correctly seen by CP every time. Since it needs to get wptr updates from memory rather than doorbell value, don't know how your snooping theory works for this. Also, it is weird that WC writes to those pass MQD buffer writes even with fence.

MQD allocation for user queues are still from USWC as per your latest
patch and they still work correctly.

For debug -
Is the WPTR reg value correctly reflecting the memory value? Have you
tried initializing MQD to a known pattern (memset32/64) like a series of 0xcafedead and verified what is seen at the CP side?

Thanks,
Lijo

Quoted 146 lines

Thanks

Monk Liu | Cloud GPU & Virtualization | AMD

-----Original Message-----
From: Lazar, Lijo <[email protected]>
Sent: Friday, November 8, 2024 7:26 PM
To: Liu, Monk <[email protected]>; Koenig, Christian <[email protected]>; Alex Deucher <[email protected]>; Zhao, Victor <[email protected]>
Cc: [email protected]; Yang, Philip <[email protected]>; Kuehling, Felix <[email protected]>
Subject: Re: [PATCH 2/2] drm/amdkfd: use cache GTT buffer for PQ and wb pool

Quoted 4 lines · Monk

[AMD Official Use Only - AMD Internal Distribution Only]

To be clear for the mb() approach: Even if we insert mb() in prior to amdgpu_ring_set_wptr(ring), GPU might still fetch stalled data from PQ due to USWC attributes.

Inserting an mb() doesn't cause WC buffer flush is a wrong assumption.

All prior loads/stores are supposed to be globally visible. Hence mb() followed by a write pointer update also should guarantee the same (From Arch manual).

The MFENCE instruction establishes a memory fence for both
loads and stores. The processor ensures that no load or store after MFENCE will become globally visible *until all loads and stores before MFENCE are globally visible.*

Ignoring the amdgpu driver part of it - if mb() is not working as expected as you claim that means something is wrong with the system.

USWC or WB for ring type may still be a separate discussion.

Thanks,
Lijo

Quoted 101 lines

The issue here is not the re-ordering but the stalled PQ.

Monk Liu | Cloud GPU & Virtualization | AMD

-----Original Message-----
From: Liu, Monk
Sent: Friday, November 8, 2024 6:22 PM
To: Koenig, Christian <[email protected]>; Lazar, Lijo <[email protected]>; Alex Deucher <[email protected]>; Zhao, Victor <[email protected]>
Cc: [email protected]; Yang, Philip <[email protected]>; Kuehling, Felix <[email protected]>
Subject: RE: [PATCH 2/2] drm/amdkfd: use cache GTT buffer for PQ and wb pool

Christian/Lijo

We verified all approaches, and we know what works and not works, obviously the mb() doesn't work.

The way of mb() between set wptr_polling and kicking off doorbell is theoretically correct, and I'm not object to do so... but this won't resolve the issue we hit.
First of all, USWC will have some chance that the data is still in CPU's WC storage place and not flushed to the memory and even with MB() get rid of re-ordering GPU might still have a chance to read stalled data from ring buffer as long as it is mapped USWC.

This is why only cache plus snoop memory can get rid of inconsistence issues.

Besides, do you know what's the rational to keep all GFX KCQ cache+snoop but only HIQ/KIQ from KFD configured to USWC ?

For performance concern that looks to me always the second priority compared to "correct" especially under the case HIQ contributes very little to ROCM performance when switching to cache mapping.

Monk Liu | Cloud GPU & Virtualization | AMD

-----Original Message-----
From: Koenig, Christian <[email protected]>
Sent: Thursday, November 7, 2024 7:57 PM
To: Lazar, Lijo <[email protected]>; Alex Deucher <[email protected]>; Zhao, Victor <[email protected]>
Cc: [email protected]; Liu, Monk <[email protected]>; Yang, Philip <[email protected]>; Kuehling, Felix <[email protected]>
Subject: Re: [PATCH 2/2] drm/amdkfd: use cache GTT buffer for PQ and wb pool

Am 07.11.24 um 06:58 schrieb Lazar, Lijo:

Quoted 12 lines · Alex Deucher
Quoted 11 lines · Victor Zhao
Quoted 6 lines

From: Monk Liu <[email protected]>

As cache GTT buffer is snooped, this way the coherence between CPU write and GPU fetch is guaranteed, but original code uses WC
+ unsnooped for HIQ PQ(ring buffer) which introduces coherency issues:
MEC fetches a stall data from PQ and leads to MEC hang.

Can you elaborate on this? I can see CPU reads being slower because the memory is uncached, but the ring buffer is mostly writes anyway. IIRC, the driver uses USWC for most if not all of the other ring buffers managed by the kernel. Why aren't those a problem?

Quoted 6 lines

We have this on other rings -

          mb();
          amdgpu_ring_set_wptr(ring);

I think the solution should be to use barrier before write pointer updates rather than relying on PCIe snooping.

Yeah, completely agree as well. The barrier also takes care of preventing the compiler from re-ordering writes.

Regards,
Christian.

Quoted 27 lines

Thanks,
Lijo

Quoted 24 lines

Alex

Quoted 22 lines
---
   drivers/gpu/drm/amd/amdkfd/kfd_device.c | 2 +-
   1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/amd/amdkfd/kfd_device.c +1 -2
index 1f1d79ac5e6c..fb087a0ff5bc 100644
@@ -779,7 +779,7 @@ bool kgd2kfd_device_init(struct kfd_dev *kfd,
          if (amdgpu_amdkfd_alloc_gtt_mem(
                          kfd->adev, size, &kfd->gtt_mem,
                          &kfd->gtt_start_gpu_addr, &kfd->gtt_start_cpu_ptr,
-                       false, true)) {
+                       false, false)) {
                  dev_err(kfd_device, "Could not allocate %d bytes\n", size);
                  goto alloc_gtt_mem_failure;
          }
--

2.34.1

maintainer
#16 lore raw

Re: [PATCH 2/2] drm/amdkfd: use cache GTT buffer for PQ and wb pool

Hi Monk,

Quoted 1 line

The memory we are talking about is pure guest system memory not FB BAR right?

No, that works a bit differently.

QEMU and their virtual drivers uses a virtual PCI MMIO BAR to map buffers from the host into the guest. Those buffers can be both point to the FB BAR as well as system memory.

Only the host knows if the buffer is backed by MMIO (FB) or system memory, so only the host can setup the tables to have the correct caching and WC attributes.

The KVM patch completely breaks that because they suddenly say that not the host setups the caching and WC attributes but the guest.

Because of that this KVM patch not only breaks amdgpu, but also other drivers which do the same thing. The general approach of the patch seems to be broken.

drivers/gpu/drm/amd/amdkfd/kfd_kernel_queue.c int kq_submit_packet(struct kernel_queue *kq) · line 318

Quoted 10 lines
          if (kq->dev->kfd->device_info.doorbell_size == 8) {
                  *kq->wptr64_kernel = kq->pending_wptr64;
drivers/gpu/drm/amd/amdkfd/kfd_kernel_queue.c +2 -0
+               mb();
                  write_kernel_doorbell64(kq->queue->properties.doorbell_ptr,
                                          kq->pending_wptr64);
          } else {
                  *kq->wptr_kernel = kq->pending_wptr;
+               mb();
                  write_kernel_doorbell(kq->queue->properties.doorbell_ptr,
                                          kq->pending_wptr);

We should probably move that into write_kernel_doorbell()/write_kernel_doorbell64() but in general looks correct to me as well.

Regards,
Christian.

Am 12.11.24 um 04:50 schrieb Liu, Monk:

Quoted 291 lines

[AMD Official Use Only - AMD Internal Distribution Only]

Hi Lijo

Thanks for your quote to the SPEC, yes I read that as well, supposedly WC storage buffer shall be flushed to memory with mfence, but it just doesn't work after applied that KVM patch which honor the WC attributes from guest.

This means the WC storage buffer from a KVM guest couldn’t be flushed to memory even with mb inserted... I don't know why. (is there any inconsistencies between nested page tables and host page tables?)

Hi Christian

Quoted 1 line
Quoted 1 line

The guest sees driver exposed memory as "emulated" MMIO BAR of a PCIe device, that's just how QEMU is designed. Because of this the guest maps the memory USWC or uncached. But in reality the memory can as well be GTT memory which is cached.

The memory we are talking about is pure guest system memory not FB BAR right? Therefore, I don't see why in reality it has to be cached, you see that only guest vCPU is accessing this buffer.

BTW: we should aways insert that MB() in our KFD part:

Quoted 11 lines

drivers/gpu/drm/amd/amdkfd/kfd_kernel_queue.c int kq_submit_packet(struct kernel_queue *kq) · line 319

Quoted 11 lines
          if (kq->dev->kfd->device_info.doorbell_size == 8) {
                  *kq->wptr64_kernel = kq->pending_wptr64;
drivers/gpu/drm/amd/amdkfd/kfd_kernel_queue.c +2 -0
+               mb();
                  write_kernel_doorbell64(kq->queue->properties.doorbell_ptr,
                                          kq->pending_wptr64);
          } else {
                  *kq->wptr_kernel = kq->pending_wptr;
+               mb();
                  write_kernel_doorbell(kq->queue->properties.doorbell_ptr,
                                          kq->pending_wptr);
          }

Thanks

Monk Liu | Cloud GPU & Virtualization | AMD

-----Original Message-----
From: Christian König <[email protected]>
Sent: Monday, November 11, 2024 8:40 PM
To: Lazar, Lijo <[email protected]>; Liu, Monk <[email protected]>; Koenig, Christian <[email protected]>; Alex Deucher <[email protected]>; Zhao, Victor <[email protected]>
Cc: [email protected]; Yang, Philip <[email protected]>; Kuehling, Felix <[email protected]>
Subject: Re: [PATCH 2/2] drm/amdkfd: use cache GTT buffer for PQ and wb pool

Hi guys,

trying to merge multiple responses back into one mail thread.

Lijo, you're completely right, but there is one really strong argument you are missing:

The patch here doesn't change the write combine behavior, it changes the caching behavior!

So when the patch works and fixes the issue, then the issue is 100% certain not a write combine issue but rather an incorrectly applied caching :)

Quoted 2 lines

From what I heard there was a KVM patch to correct the mapping behavior -- previously the mapping is forced to cache mode even guest KMD maps the buffer with "WC".
But after the patch the buffer will be really "WC" if guest maps it with WC"... and this reveals the bug and hit our issue.

Yeah, exactly that's the problem. The guest doesn't know if WB, USWC or uncached should be used!

The guest sees driver exposed memory as "emulated" MMIO BAR of a PCIe device, that's just how QEMU is designed. Because of this the guest maps the memory USWC or uncached. But in reality the memory can as well be GTT memory which is cached.

So forcing the cache mode even if the guest KMD maps the buffer with "WC" is intentional behavior, that's the correct approach.

The upstream people realized that as well. That's one major reason why the patch was reverted.

Quoted 1 line

Can you explain why USWC for ring buffer is safe, why it will not hit coherence issue, why you don't make all ring buffers with USWC even for amdgpu side if you really believe USWC is innocent ...

We already tried this, but there is simply no benefit for it.

Regards,
Christian.

Am 11.11.24 um 06:58 schrieb Lazar, Lijo:

Quoted 223 lines · Monk
Quoted 26 lines

[AMD Official Use Only - AMD Internal Distribution Only]

Hi Lijo

This is the patch we verified before:

drivers/gpu/drm/amd/amdkfd/kfd_kernel_queue.c +2 -0
index 4843dcb9a5f7..39553c7648eb 100644
@@ -308,10 +308,12 @@ int kq_submit_packet(struct kernel_queue *kq)
 
          if (kq->dev->kfd->device_info.doorbell_size == 8) {
                  *kq->wptr64_kernel = kq->pending_wptr64;
+               mb();
                  write_kernel_doorbell64(kq->queue->properties.doorbell_ptr,
                                          kq->pending_wptr64);
          } else {
                  *kq->wptr_kernel = kq->pending_wptr;
+               mb();
                  write_kernel_doorbell(kq->queue->properties.doorbell_ptr,
                                          kq->pending_wptr);
          }
Quoted 6 lines

This mb() doesn't resolve the problem during customer's testing, I also thought of MB() first in beginning like you and Christian ...
The mb() here shall resolve the re-ordering between WPTR and doorbell kicking so GPU won't fetch stalled data from WPTR polling once it receives notification from doorbell kicking. (in SR-IOV we set doorbell mode to force GPU still fetch from WPTR
polling area, doorbell kicking is just to notify GPU)

And by your theory: mb() shall flush the WC storage buffer to memory, thus, this mb() shall also make sure that the ring buffer is not holding stalled data anymore, right ?

This is not my theory. All the quotes in my earlier mails are from "Intel® 64 and IA-32 Architectures Software Developer’s Manual".

Yet another one -

<snip>

Writes may be delayed and combined in the write combining buffer (WC buffer) to reduce memory accesses.

"If the WC buffer is partially filled, the writes may be delayed until the next occurrence of a serializing event; such as an SFENCE or MFENCE instruction, CPUID or other serializing instruction, a read or write to uncached memory, an interrupt occurrence, or an execution of a LOCK instruction (including one with an XACQUIRE or XRELEASE prefix)."

</snip>

Quoted 4 lines

But we still hit hang and get stalled data from dump.

Maybe we need to put mb() in another place ? can you proposal if you insist the cache mapping is not acceptable to you and we can ask customer to verify again.

There are a couple of things which are still working strangely, they will need some explanation as well -

Even though write pointer allocations are also to WC region, and are
correctly seen by CP every time. Since it needs to get wptr updates from memory rather than doorbell value, don't know how your snooping theory works for this. Also, it is weird that WC writes to those pass MQD buffer writes even with fence.

MQD allocation for user queues are still from USWC as per your latest
patch and they still work correctly.

For debug -
Is the WPTR reg value correctly reflecting the memory value? Have you
tried initializing MQD to a known pattern (memset32/64) like a series of 0xcafedead and verified what is seen at the CP side?

Thanks,
Lijo

Quoted 146 lines

Thanks

Monk Liu | Cloud GPU & Virtualization | AMD

-----Original Message-----
From: Lazar, Lijo <[email protected]>
Sent: Friday, November 8, 2024 7:26 PM
To: Liu, Monk <[email protected]>; Koenig, Christian <[email protected]>; Alex Deucher <[email protected]>; Zhao, Victor <[email protected]>
Cc: [email protected]; Yang, Philip <[email protected]>; Kuehling, Felix <[email protected]>
Subject: Re: [PATCH 2/2] drm/amdkfd: use cache GTT buffer for PQ and wb pool

Quoted 4 lines · Monk

[AMD Official Use Only - AMD Internal Distribution Only]

To be clear for the mb() approach: Even if we insert mb() in prior to amdgpu_ring_set_wptr(ring), GPU might still fetch stalled data from PQ due to USWC attributes.

Inserting an mb() doesn't cause WC buffer flush is a wrong assumption.

All prior loads/stores are supposed to be globally visible. Hence mb() followed by a write pointer update also should guarantee the same (From Arch manual).

The MFENCE instruction establishes a memory fence for both
loads and stores. The processor ensures that no load or store after MFENCE will become globally visible *until all loads and stores before MFENCE are globally visible.*

Ignoring the amdgpu driver part of it - if mb() is not working as expected as you claim that means something is wrong with the system.

USWC or WB for ring type may still be a separate discussion.

Thanks,
Lijo

Quoted 101 lines

The issue here is not the re-ordering but the stalled PQ.

Monk Liu | Cloud GPU & Virtualization | AMD

-----Original Message-----
From: Liu, Monk
Sent: Friday, November 8, 2024 6:22 PM
To: Koenig, Christian <[email protected]>; Lazar, Lijo <[email protected]>; Alex Deucher <[email protected]>; Zhao, Victor <[email protected]>
Cc: [email protected]; Yang, Philip <[email protected]>; Kuehling, Felix <[email protected]>
Subject: RE: [PATCH 2/2] drm/amdkfd: use cache GTT buffer for PQ and wb pool

Christian/Lijo

We verified all approaches, and we know what works and not works, obviously the mb() doesn't work.

The way of mb() between set wptr_polling and kicking off doorbell is theoretically correct, and I'm not object to do so... but this won't resolve the issue we hit.
First of all, USWC will have some chance that the data is still in CPU's WC storage place and not flushed to the memory and even with MB() get rid of re-ordering GPU might still have a chance to read stalled data from ring buffer as long as it is mapped USWC.

This is why only cache plus snoop memory can get rid of inconsistence issues.

Besides, do you know what's the rational to keep all GFX KCQ cache+snoop but only HIQ/KIQ from KFD configured to USWC ?

For performance concern that looks to me always the second priority compared to "correct" especially under the case HIQ contributes very little to ROCM performance when switching to cache mapping.

Monk Liu | Cloud GPU & Virtualization | AMD

-----Original Message-----
From: Koenig, Christian <[email protected]>
Sent: Thursday, November 7, 2024 7:57 PM
To: Lazar, Lijo <[email protected]>; Alex Deucher <[email protected]>; Zhao, Victor <[email protected]>
Cc: [email protected]; Liu, Monk <[email protected]>; Yang, Philip <[email protected]>; Kuehling, Felix <[email protected]>
Subject: Re: [PATCH 2/2] drm/amdkfd: use cache GTT buffer for PQ and wb pool

Am 07.11.24 um 06:58 schrieb Lazar, Lijo:

Quoted 12 lines · Alex Deucher
Quoted 11 lines · Victor Zhao
Quoted 6 lines

From: Monk Liu <[email protected]>

As cache GTT buffer is snooped, this way the coherence between CPU write and GPU fetch is guaranteed, but original code uses WC
+ unsnooped for HIQ PQ(ring buffer) which introduces coherency issues:
MEC fetches a stall data from PQ and leads to MEC hang.

Can you elaborate on this? I can see CPU reads being slower because the memory is uncached, but the ring buffer is mostly writes anyway. IIRC, the driver uses USWC for most if not all of the other ring buffers managed by the kernel. Why aren't those a problem?

Quoted 6 lines

We have this on other rings -

           mb();
           amdgpu_ring_set_wptr(ring);

I think the solution should be to use barrier before write pointer updates rather than relying on PCIe snooping.

Yeah, completely agree as well. The barrier also takes care of preventing the compiler from re-ordering writes.

Regards,
Christian.

Quoted 27 lines

Thanks,
Lijo

Quoted 24 lines

Alex

Quoted 22 lines
---
    drivers/gpu/drm/amd/amdkfd/kfd_device.c | 2 +-
    1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/amd/amdkfd/kfd_device.c +1 -2
index 1f1d79ac5e6c..fb087a0ff5bc 100644
@@ -779,7 +779,7 @@ bool kgd2kfd_device_init(struct kfd_dev *kfd,
           if (amdgpu_amdkfd_alloc_gtt_mem(
                           kfd->adev, size, &kfd->gtt_mem,
                           &kfd->gtt_start_gpu_addr, &kfd->gtt_start_cpu_ptr,
-                       false, true)) {
+                       false, false)) {
                   dev_err(kfd_device, "Could not allocate %d bytes\n", size);
                   goto alloc_gtt_mem_failure;
           }
--

2.34.1

#17 lore raw

RE: [PATCH 2/2] drm/amdkfd: use cache GTT buffer for PQ and wb pool

[AMD Official Use Only - AMD Internal Distribution Only]

Quoted 1 line
Quoted 1 line

QEMU and their virtual drivers uses a virtual PCI MMIO BAR to map buffers from the host into the guest. Those buffers can be both point to the FB BAR as well as system memory.

Well, you are talking about virt-io GL use case? Or mDev solution ? because those conventional device like QXL, NIC, soundcard are purely software emulated and no way to point to the FB BAR ...

I can understand that Host make the final decision on the memory if it is FB can be leveraged by guest as some system memory, but brutely treat all system memory as cache no matter what guest do is ugly ...

Back to our topic, I propagated you and Lijo's statement to customer and they didn't buy it, rational as:

1) they don't think let host honor the real mapping is correct as they don't have anything like virt-io GL solution in use...
2) they cannot suffer risk to revert that KVM patch on their server.

So the thing is they will live with a hotfix patch for now, but they are also asking:

Why cannot AMD make all ring buffer (managed by KMD/KFD) to the same type which is cache, as they know that GFX ring from amdgpu is cache type.

Besides, both customer and CVS team will also keep investigate why after the map is honored by guest there is still coherence issue (type is USWC, mfence is inserted between doorbell and wptr_polling updates)

Anyway, Lijo, Christian, thanks for your explain and insight !

Monk Liu | Cloud GPU & Virtualization | AMD

-----Original Message-----
From: Koenig, Christian <[email protected]>
Sent: Tuesday, November 12, 2024 5:41 PM
To: Liu, Monk <[email protected]>; Christian König <[email protected]>; Lazar, Lijo <[email protected]>; Alex Deucher <[email protected]>; Zhao, Victor <[email protected]>
Cc: [email protected]; Yang, Philip <[email protected]>; Kuehling, Felix <[email protected]>
Subject: Re: [PATCH 2/2] drm/amdkfd: use cache GTT buffer for PQ and wb pool

Hi Monk,

Quoted 1 line

The memory we are talking about is pure guest system memory not FB BAR right?

No, that works a bit differently.

QEMU and their virtual drivers uses a virtual PCI MMIO BAR to map buffers from the host into the guest. Those buffers can be both point to the FB BAR as well as system memory.

Only the host knows if the buffer is backed by MMIO (FB) or system memory, so only the host can setup the tables to have the correct caching and WC attributes.

The KVM patch completely breaks that because they suddenly say that not the host setups the caching and WC attributes but the guest.

Because of that this KVM patch not only breaks amdgpu, but also other drivers which do the same thing. The general approach of the patch seems to be broken.

drivers/gpu/drm/amd/amdkfd/kfd_kernel_queue.c int kq_submit_packet(struct kernel_queue *kq) · line 318

Quoted 10 lines
          if (kq->dev->kfd->device_info.doorbell_size == 8) {
                  *kq->wptr64_kernel = kq->pending_wptr64;
drivers/gpu/drm/amd/amdkfd/kfd_kernel_queue.c +2 -0
+               mb();
                  write_kernel_doorbell64(kq->queue->properties.doorbell_ptr,
                                          kq->pending_wptr64);
          } else {
                  *kq->wptr_kernel = kq->pending_wptr;
+               mb();
                  write_kernel_doorbell(kq->queue->properties.doorbell_ptr,
                                          kq->pending_wptr);

We should probably move that into write_kernel_doorbell()/write_kernel_doorbell64() but in general looks correct to me as well.

Regards,
Christian.

Am 12.11.24 um 04:50 schrieb Liu, Monk:

Quoted 299 lines

[AMD Official Use Only - AMD Internal Distribution Only]

Hi Lijo

Thanks for your quote to the SPEC, yes I read that as well, supposedly WC storage buffer shall be flushed to memory with mfence, but it just doesn't work after applied that KVM patch which honor the WC attributes from guest.

This means the WC storage buffer from a KVM guest couldn’t be flushed to memory even with mb inserted... I don't know why. (is there any inconsistencies between nested page tables and host page tables?)

Hi Christian

Quoted 1 line
Quoted 1 line

The guest sees driver exposed memory as "emulated" MMIO BAR of a PCIe device, that's just how QEMU is designed. Because of this the guest maps the memory USWC or uncached. But in reality the memory can as well be GTT memory which is cached.

The memory we are talking about is pure guest system memory not FB BAR right? Therefore, I don't see why in reality it has to be cached, you see that only guest vCPU is accessing this buffer.

BTW: we should aways insert that MB() in our KFD part:

Quoted 11 lines

drivers/gpu/drm/amd/amdkfd/kfd_kernel_queue.c int kq_submit_packet(struct kernel_queue *kq) · line 319

Quoted 11 lines
          if (kq->dev->kfd->device_info.doorbell_size == 8) {
                  *kq->wptr64_kernel = kq->pending_wptr64;
drivers/gpu/drm/amd/amdkfd/kfd_kernel_queue.c +2 -0
+               mb();
                  write_kernel_doorbell64(kq->queue->properties.doorbell_ptr,
                                          kq->pending_wptr64);
          } else {
                  *kq->wptr_kernel = kq->pending_wptr;
+               mb();
                  write_kernel_doorbell(kq->queue->properties.doorbell_ptr,
                                          kq->pending_wptr);
          }

Thanks

Monk Liu | Cloud GPU & Virtualization | AMD

-----Original Message-----
From: Christian König <[email protected]>
Sent: Monday, November 11, 2024 8:40 PM
To: Lazar, Lijo <[email protected]>; Liu, Monk <[email protected]>; Koenig, Christian <[email protected]>; Alex Deucher <[email protected]>; Zhao, Victor <[email protected]>
Cc: [email protected]; Yang, Philip <[email protected]>; Kuehling, Felix <[email protected]>
Subject: Re: [PATCH 2/2] drm/amdkfd: use cache GTT buffer for PQ and wb pool

Hi guys,

trying to merge multiple responses back into one mail thread.

Lijo, you're completely right, but there is one really strong argument you are missing:

The patch here doesn't change the write combine behavior, it changes the caching behavior!

So when the patch works and fixes the issue, then the issue is 100% certain not a write combine issue but rather an incorrectly applied caching :)

Quoted 2 lines

From what I heard there was a KVM patch to correct the mapping behavior -- previously the mapping is forced to cache mode even guest KMD maps the buffer with "WC".
But after the patch the buffer will be really "WC" if guest maps it with WC"... and this reveals the bug and hit our issue.

Yeah, exactly that's the problem. The guest doesn't know if WB, USWC or uncached should be used!

The guest sees driver exposed memory as "emulated" MMIO BAR of a PCIe device, that's just how QEMU is designed. Because of this the guest maps the memory USWC or uncached. But in reality the memory can as well be GTT memory which is cached.

So forcing the cache mode even if the guest KMD maps the buffer with "WC" is intentional behavior, that's the correct approach.

The upstream people realized that as well. That's one major reason why the patch was reverted.

Quoted 1 line

Can you explain why USWC for ring buffer is safe, why it will not hit coherence issue, why you don't make all ring buffers with USWC even for amdgpu side if you really believe USWC is innocent ...

We already tried this, but there is simply no benefit for it.

Regards,
Christian.

Am 11.11.24 um 06:58 schrieb Lazar, Lijo:

Quoted 223 lines · Monk
Quoted 26 lines

[AMD Official Use Only - AMD Internal Distribution Only]

Hi Lijo

This is the patch we verified before:

drivers/gpu/drm/amd/amdkfd/kfd_kernel_queue.c +2 -0
index 4843dcb9a5f7..39553c7648eb 100644
@@ -308,10 +308,12 @@ int kq_submit_packet(struct kernel_queue *kq)
 
          if (kq->dev->kfd->device_info.doorbell_size == 8) {
                  *kq->wptr64_kernel = kq->pending_wptr64;
+               mb();
                  write_kernel_doorbell64(kq->queue->properties.doorbell_ptr,
                                          kq->pending_wptr64);
          } else {
                  *kq->wptr_kernel = kq->pending_wptr;
+               mb();
                  write_kernel_doorbell(kq->queue->properties.doorbell_ptr,
                                          kq->pending_wptr);
          }
Quoted 6 lines

This mb() doesn't resolve the problem during customer's testing, I also thought of MB() first in beginning like you and Christian ...
The mb() here shall resolve the re-ordering between WPTR and doorbell kicking so GPU won't fetch stalled data from WPTR polling once it receives notification from doorbell kicking. (in SR-IOV we set doorbell mode to force GPU still fetch from WPTR
polling area, doorbell kicking is just to notify GPU)

And by your theory: mb() shall flush the WC storage buffer to memory, thus, this mb() shall also make sure that the ring buffer is not holding stalled data anymore, right ?

This is not my theory. All the quotes in my earlier mails are from "Intel® 64 and IA-32 Architectures Software Developer’s Manual".

Yet another one -

<snip>

Writes may be delayed and combined in the write combining buffer (WC buffer) to reduce memory accesses.

"If the WC buffer is partially filled, the writes may be delayed until the next occurrence of a serializing event; such as an SFENCE or MFENCE instruction, CPUID or other serializing instruction, a read or write to uncached memory, an interrupt occurrence, or an execution of a LOCK instruction (including one with an XACQUIRE or XRELEASE prefix)."

</snip>

Quoted 4 lines

But we still hit hang and get stalled data from dump.

Maybe we need to put mb() in another place ? can you proposal if you insist the cache mapping is not acceptable to you and we can ask customer to verify again.

There are a couple of things which are still working strangely, they will need some explanation as well -

Even though write pointer allocations are also to WC region,
and are correctly seen by CP every time. Since it needs to get wptr updates from memory rather than doorbell value, don't know how your snooping theory works for this. Also, it is weird that WC writes to those pass MQD buffer writes even with fence.

MQD allocation for user queues are still from USWC as per your
latest patch and they still work correctly.

For debug -
Is the WPTR reg value correctly reflecting the memory value?
Have you tried initializing MQD to a known pattern (memset32/64) like a series of 0xcafedead and verified what is seen at the CP side?

Thanks,
Lijo

Quoted 146 lines

Thanks

Monk Liu | Cloud GPU & Virtualization | AMD

-----Original Message-----
From: Lazar, Lijo <[email protected]>
Sent: Friday, November 8, 2024 7:26 PM
To: Liu, Monk <[email protected]>; Koenig, Christian <[email protected]>; Alex Deucher <[email protected]>; Zhao, Victor <[email protected]>
Cc: [email protected]; Yang, Philip <[email protected]>; Kuehling, Felix <[email protected]>
Subject: Re: [PATCH 2/2] drm/amdkfd: use cache GTT buffer for PQ and wb pool

Quoted 4 lines · Monk

[AMD Official Use Only - AMD Internal Distribution Only]

To be clear for the mb() approach: Even if we insert mb() in prior to amdgpu_ring_set_wptr(ring), GPU might still fetch stalled data from PQ due to USWC attributes.

Inserting an mb() doesn't cause WC buffer flush is a wrong assumption.

All prior loads/stores are supposed to be globally visible. Hence mb() followed by a write pointer update also should guarantee the same (From Arch manual).

The MFENCE instruction establishes a memory fence for both
loads and stores. The processor ensures that no load or store after MFENCE will become globally visible *until all loads and stores before MFENCE are globally visible.*

Ignoring the amdgpu driver part of it - if mb() is not working as expected as you claim that means something is wrong with the system.

USWC or WB for ring type may still be a separate discussion.

Thanks,
Lijo

Quoted 101 lines

The issue here is not the re-ordering but the stalled PQ.

Monk Liu | Cloud GPU & Virtualization | AMD

-----Original Message-----
From: Liu, Monk
Sent: Friday, November 8, 2024 6:22 PM
To: Koenig, Christian <[email protected]>; Lazar, Lijo <[email protected]>; Alex Deucher <[email protected]>; Zhao, Victor <[email protected]>
Cc: [email protected]; Yang, Philip <[email protected]>; Kuehling, Felix <[email protected]>
Subject: RE: [PATCH 2/2] drm/amdkfd: use cache GTT buffer for PQ and wb pool

Christian/Lijo

We verified all approaches, and we know what works and not works, obviously the mb() doesn't work.

The way of mb() between set wptr_polling and kicking off doorbell is theoretically correct, and I'm not object to do so... but this won't resolve the issue we hit.
First of all, USWC will have some chance that the data is still in CPU's WC storage place and not flushed to the memory and even with MB() get rid of re-ordering GPU might still have a chance to read stalled data from ring buffer as long as it is mapped USWC.

This is why only cache plus snoop memory can get rid of inconsistence issues.

Besides, do you know what's the rational to keep all GFX KCQ cache+snoop but only HIQ/KIQ from KFD configured to USWC ?

For performance concern that looks to me always the second priority compared to "correct" especially under the case HIQ contributes very little to ROCM performance when switching to cache mapping.

Monk Liu | Cloud GPU & Virtualization | AMD

-----Original Message-----
From: Koenig, Christian <[email protected]>
Sent: Thursday, November 7, 2024 7:57 PM
To: Lazar, Lijo <[email protected]>; Alex Deucher <[email protected]>; Zhao, Victor <[email protected]>
Cc: [email protected]; Liu, Monk <[email protected]>; Yang, Philip <[email protected]>; Kuehling, Felix <[email protected]>
Subject: Re: [PATCH 2/2] drm/amdkfd: use cache GTT buffer for PQ and wb pool

Am 07.11.24 um 06:58 schrieb Lazar, Lijo:

Quoted 12 lines · Alex Deucher
Quoted 11 lines · Victor Zhao
Quoted 6 lines

From: Monk Liu <[email protected]>

As cache GTT buffer is snooped, this way the coherence between CPU write and GPU fetch is guaranteed, but original code uses WC
+ unsnooped for HIQ PQ(ring buffer) which introduces coherency issues:
MEC fetches a stall data from PQ and leads to MEC hang.

Can you elaborate on this? I can see CPU reads being slower because the memory is uncached, but the ring buffer is mostly writes anyway. IIRC, the driver uses USWC for most if not all of the other ring buffers managed by the kernel. Why aren't those a problem?

Quoted 6 lines

We have this on other rings -

           mb();
           amdgpu_ring_set_wptr(ring);

I think the solution should be to use barrier before write pointer updates rather than relying on PCIe snooping.

Yeah, completely agree as well. The barrier also takes care of preventing the compiler from re-ordering writes.

Regards,
Christian.

Quoted 27 lines

Thanks,
Lijo

Quoted 24 lines

Alex

Quoted 22 lines
---
    drivers/gpu/drm/amd/amdkfd/kfd_device.c | 2 +-
    1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/amd/amdkfd/kfd_device.c +1 -2
index 1f1d79ac5e6c..fb087a0ff5bc 100644
@@ -779,7 +779,7 @@ bool kgd2kfd_device_init(struct kfd_dev *kfd,
           if (amdgpu_amdkfd_alloc_gtt_mem(
                           kfd->adev, size, &kfd->gtt_mem,
                           &kfd->gtt_start_gpu_addr, &kfd->gtt_start_cpu_ptr,
-                       false, true)) {
+                       false, false)) {
                   dev_err(kfd_device, "Could not allocate %d bytes\n", size);
                   goto alloc_gtt_mem_failure;
           }
--

2.34.1

#18 lore raw

Re: [PATCH 2/2] drm/amdkfd: use cache GTT buffer for PQ and wb pool

Quoted 10 lines · Monk

[AMD Official Use Only - AMD Internal Distribution Only]

Quoted 1 line
Quoted 1 line

QEMU and their virtual drivers uses a virtual PCI MMIO BAR to map buffers from the host into the guest. Those buffers can be both point to the FB BAR as well as system memory.

Well, you are talking about virt-io GL use case? Or mDev solution ? because those conventional device like QXL, NIC, soundcard are purely software emulated and no way to point to the FB BAR ...

I can understand that Host make the final decision on the memory if it is FB can be leveraged by guest as some system memory, but brutely treat all system memory as cache no matter what guest do is ugly ...

Back to our topic, I propagated you and Lijo's statement to customer and they didn't buy it, rational as:

Quoted 9 lines

1) they don't think let host honor the real mapping is correct as they don't have anything like virt-io GL solution in use...
2) they cannot suffer risk to revert that KVM patch on their server.

So the thing is they will live with a hotfix patch for now, but they are also asking:

Why cannot AMD make all ring buffer (managed by KMD/KFD) to the same type which is cache, as they know that GFX ring from amdgpu is cache type.

Besides, both customer and CVS team will also keep investigate why after the map is honored by guest there is still coherence issue (type is USWC, mfence is inserted between doorbell and wptr_polling updates)

If this is truly mfence-not-working-as-expected, wondering how any user app which relies on a similar mechanism can reliably work on their systems.

Had already mentioned this before -

Even by the assumption that mfence fixed ordering but data stayed in WC buffer, there is one other hole in this theory. With DOORBELL_MODE=1, any write pointer update is fetched from memory. That write pointer is also maintained in WC type memory. If CP is correctly able to see WPTR update, that means that part of write got updated in system memory.

Anyway, please keep us posted.

Thanks,
Lijo

Quoted 344 lines

Anyway, Lijo, Christian, thanks for your explain and insight !

Monk Liu | Cloud GPU & Virtualization | AMD

-----Original Message-----
From: Koenig, Christian <[email protected]>
Sent: Tuesday, November 12, 2024 5:41 PM
To: Liu, Monk <[email protected]>; Christian König <[email protected]>; Lazar, Lijo <[email protected]>; Alex Deucher <[email protected]>; Zhao, Victor <[email protected]>
Cc: [email protected]; Yang, Philip <[email protected]>; Kuehling, Felix <[email protected]>
Subject: Re: [PATCH 2/2] drm/amdkfd: use cache GTT buffer for PQ and wb pool

Hi Monk,

Quoted 1 line

The memory we are talking about is pure guest system memory not FB BAR right?

No, that works a bit differently.

QEMU and their virtual drivers uses a virtual PCI MMIO BAR to map buffers from the host into the guest. Those buffers can be both point to the FB BAR as well as system memory.

Only the host knows if the buffer is backed by MMIO (FB) or system memory, so only the host can setup the tables to have the correct caching and WC attributes.

The KVM patch completely breaks that because they suddenly say that not the host setups the caching and WC attributes but the guest.

Because of that this KVM patch not only breaks amdgpu, but also other drivers which do the same thing. The general approach of the patch seems to be broken.

drivers/gpu/drm/amd/amdkfd/kfd_kernel_queue.c int kq_submit_packet(struct kernel_queue *kq) · line 318

Quoted 10 lines
          if (kq->dev->kfd->device_info.doorbell_size == 8) {
                  *kq->wptr64_kernel = kq->pending_wptr64;
drivers/gpu/drm/amd/amdkfd/kfd_kernel_queue.c +2 -0
+               mb();
                  write_kernel_doorbell64(kq->queue->properties.doorbell_ptr,
                                          kq->pending_wptr64);
          } else {
                  *kq->wptr_kernel = kq->pending_wptr;
+               mb();
                  write_kernel_doorbell(kq->queue->properties.doorbell_ptr,
                                          kq->pending_wptr);

We should probably move that into write_kernel_doorbell()/write_kernel_doorbell64() but in general looks correct to me as well.

Regards,
Christian.

Am 12.11.24 um 04:50 schrieb Liu, Monk:

Quoted 299 lines

[AMD Official Use Only - AMD Internal Distribution Only]

Hi Lijo

Thanks for your quote to the SPEC, yes I read that as well, supposedly WC storage buffer shall be flushed to memory with mfence, but it just doesn't work after applied that KVM patch which honor the WC attributes from guest.

This means the WC storage buffer from a KVM guest couldn’t be flushed to memory even with mb inserted... I don't know why. (is there any inconsistencies between nested page tables and host page tables?)

Hi Christian

Quoted 1 line
Quoted 1 line

The guest sees driver exposed memory as "emulated" MMIO BAR of a PCIe device, that's just how QEMU is designed. Because of this the guest maps the memory USWC or uncached. But in reality the memory can as well be GTT memory which is cached.

The memory we are talking about is pure guest system memory not FB BAR right? Therefore, I don't see why in reality it has to be cached, you see that only guest vCPU is accessing this buffer.

BTW: we should aways insert that MB() in our KFD part:

Quoted 11 lines

drivers/gpu/drm/amd/amdkfd/kfd_kernel_queue.c int kq_submit_packet(struct kernel_queue *kq) · line 319

Quoted 11 lines
          if (kq->dev->kfd->device_info.doorbell_size == 8) {
                  *kq->wptr64_kernel = kq->pending_wptr64;
drivers/gpu/drm/amd/amdkfd/kfd_kernel_queue.c +2 -0
+               mb();
                  write_kernel_doorbell64(kq->queue->properties.doorbell_ptr,
                                          kq->pending_wptr64);
          } else {
                  *kq->wptr_kernel = kq->pending_wptr;
+               mb();
                  write_kernel_doorbell(kq->queue->properties.doorbell_ptr,
                                          kq->pending_wptr);
          }

Thanks

Monk Liu | Cloud GPU & Virtualization | AMD

-----Original Message-----
From: Christian König <[email protected]>
Sent: Monday, November 11, 2024 8:40 PM
To: Lazar, Lijo <[email protected]>; Liu, Monk <[email protected]>; Koenig, Christian <[email protected]>; Alex Deucher <[email protected]>; Zhao, Victor <[email protected]>
Cc: [email protected]; Yang, Philip <[email protected]>; Kuehling, Felix <[email protected]>
Subject: Re: [PATCH 2/2] drm/amdkfd: use cache GTT buffer for PQ and wb pool

Hi guys,

trying to merge multiple responses back into one mail thread.

Lijo, you're completely right, but there is one really strong argument you are missing:

The patch here doesn't change the write combine behavior, it changes the caching behavior!

So when the patch works and fixes the issue, then the issue is 100% certain not a write combine issue but rather an incorrectly applied caching :)

Quoted 2 lines

From what I heard there was a KVM patch to correct the mapping behavior -- previously the mapping is forced to cache mode even guest KMD maps the buffer with "WC".
But after the patch the buffer will be really "WC" if guest maps it with WC"... and this reveals the bug and hit our issue.

Yeah, exactly that's the problem. The guest doesn't know if WB, USWC or uncached should be used!

The guest sees driver exposed memory as "emulated" MMIO BAR of a PCIe device, that's just how QEMU is designed. Because of this the guest maps the memory USWC or uncached. But in reality the memory can as well be GTT memory which is cached.

So forcing the cache mode even if the guest KMD maps the buffer with "WC" is intentional behavior, that's the correct approach.

The upstream people realized that as well. That's one major reason why the patch was reverted.

Quoted 1 line

Can you explain why USWC for ring buffer is safe, why it will not hit coherence issue, why you don't make all ring buffers with USWC even for amdgpu side if you really believe USWC is innocent ...

We already tried this, but there is simply no benefit for it.

Regards,
Christian.

Am 11.11.24 um 06:58 schrieb Lazar, Lijo:

Quoted 223 lines · Monk
Quoted 26 lines

[AMD Official Use Only - AMD Internal Distribution Only]

Hi Lijo

This is the patch we verified before:

drivers/gpu/drm/amd/amdkfd/kfd_kernel_queue.c +2 -0
index 4843dcb9a5f7..39553c7648eb 100644
@@ -308,10 +308,12 @@ int kq_submit_packet(struct kernel_queue *kq)
 
          if (kq->dev->kfd->device_info.doorbell_size == 8) {
                  *kq->wptr64_kernel = kq->pending_wptr64;
+               mb();
                  write_kernel_doorbell64(kq->queue->properties.doorbell_ptr,
                                          kq->pending_wptr64);
          } else {
                  *kq->wptr_kernel = kq->pending_wptr;
+               mb();
                  write_kernel_doorbell(kq->queue->properties.doorbell_ptr,
                                          kq->pending_wptr);
          }
Quoted 6 lines

This mb() doesn't resolve the problem during customer's testing, I also thought of MB() first in beginning like you and Christian ...
The mb() here shall resolve the re-ordering between WPTR and doorbell kicking so GPU won't fetch stalled data from WPTR polling once it receives notification from doorbell kicking. (in SR-IOV we set doorbell mode to force GPU still fetch from WPTR
polling area, doorbell kicking is just to notify GPU)

And by your theory: mb() shall flush the WC storage buffer to memory, thus, this mb() shall also make sure that the ring buffer is not holding stalled data anymore, right ?

This is not my theory. All the quotes in my earlier mails are from "Intel® 64 and IA-32 Architectures Software Developer’s Manual".

Yet another one -

<snip>

Writes may be delayed and combined in the write combining buffer (WC buffer) to reduce memory accesses.

"If the WC buffer is partially filled, the writes may be delayed until the next occurrence of a serializing event; such as an SFENCE or MFENCE instruction, CPUID or other serializing instruction, a read or write to uncached memory, an interrupt occurrence, or an execution of a LOCK instruction (including one with an XACQUIRE or XRELEASE prefix)."

</snip>

Quoted 4 lines

But we still hit hang and get stalled data from dump.

Maybe we need to put mb() in another place ? can you proposal if you insist the cache mapping is not acceptable to you and we can ask customer to verify again.

There are a couple of things which are still working strangely, they will need some explanation as well -

Even though write pointer allocations are also to WC region,
and are correctly seen by CP every time. Since it needs to get wptr updates from memory rather than doorbell value, don't know how your snooping theory works for this. Also, it is weird that WC writes to those pass MQD buffer writes even with fence.

MQD allocation for user queues are still from USWC as per your
latest patch and they still work correctly.

For debug -
Is the WPTR reg value correctly reflecting the memory value?
Have you tried initializing MQD to a known pattern (memset32/64) like a series of 0xcafedead and verified what is seen at the CP side?

Thanks,
Lijo

Quoted 146 lines

Thanks

Monk Liu | Cloud GPU & Virtualization | AMD

-----Original Message-----
From: Lazar, Lijo <[email protected]>
Sent: Friday, November 8, 2024 7:26 PM
To: Liu, Monk <[email protected]>; Koenig, Christian <[email protected]>; Alex Deucher <[email protected]>; Zhao, Victor <[email protected]>
Cc: [email protected]; Yang, Philip <[email protected]>; Kuehling, Felix <[email protected]>
Subject: Re: [PATCH 2/2] drm/amdkfd: use cache GTT buffer for PQ and wb pool

Quoted 4 lines · Monk

[AMD Official Use Only - AMD Internal Distribution Only]

To be clear for the mb() approach: Even if we insert mb() in prior to amdgpu_ring_set_wptr(ring), GPU might still fetch stalled data from PQ due to USWC attributes.

Inserting an mb() doesn't cause WC buffer flush is a wrong assumption.

All prior loads/stores are supposed to be globally visible. Hence mb() followed by a write pointer update also should guarantee the same (From Arch manual).

The MFENCE instruction establishes a memory fence for both
loads and stores. The processor ensures that no load or store after MFENCE will become globally visible *until all loads and stores before MFENCE are globally visible.*

Ignoring the amdgpu driver part of it - if mb() is not working as expected as you claim that means something is wrong with the system.

USWC or WB for ring type may still be a separate discussion.

Thanks,
Lijo

Quoted 101 lines

The issue here is not the re-ordering but the stalled PQ.

Monk Liu | Cloud GPU & Virtualization | AMD

-----Original Message-----
From: Liu, Monk
Sent: Friday, November 8, 2024 6:22 PM
To: Koenig, Christian <[email protected]>; Lazar, Lijo <[email protected]>; Alex Deucher <[email protected]>; Zhao, Victor <[email protected]>
Cc: [email protected]; Yang, Philip <[email protected]>; Kuehling, Felix <[email protected]>
Subject: RE: [PATCH 2/2] drm/amdkfd: use cache GTT buffer for PQ and wb pool

Christian/Lijo

We verified all approaches, and we know what works and not works, obviously the mb() doesn't work.

The way of mb() between set wptr_polling and kicking off doorbell is theoretically correct, and I'm not object to do so... but this won't resolve the issue we hit.
First of all, USWC will have some chance that the data is still in CPU's WC storage place and not flushed to the memory and even with MB() get rid of re-ordering GPU might still have a chance to read stalled data from ring buffer as long as it is mapped USWC.

This is why only cache plus snoop memory can get rid of inconsistence issues.

Besides, do you know what's the rational to keep all GFX KCQ cache+snoop but only HIQ/KIQ from KFD configured to USWC ?

For performance concern that looks to me always the second priority compared to "correct" especially under the case HIQ contributes very little to ROCM performance when switching to cache mapping.

Monk Liu | Cloud GPU & Virtualization | AMD

-----Original Message-----
From: Koenig, Christian <[email protected]>
Sent: Thursday, November 7, 2024 7:57 PM
To: Lazar, Lijo <[email protected]>; Alex Deucher <[email protected]>; Zhao, Victor <[email protected]>
Cc: [email protected]; Liu, Monk <[email protected]>; Yang, Philip <[email protected]>; Kuehling, Felix <[email protected]>
Subject: Re: [PATCH 2/2] drm/amdkfd: use cache GTT buffer for PQ and wb pool

Am 07.11.24 um 06:58 schrieb Lazar, Lijo:

Quoted 12 lines · Alex Deucher
Quoted 11 lines · Victor Zhao
Quoted 6 lines

From: Monk Liu <[email protected]>

As cache GTT buffer is snooped, this way the coherence between CPU write and GPU fetch is guaranteed, but original code uses WC
+ unsnooped for HIQ PQ(ring buffer) which introduces coherency issues:
MEC fetches a stall data from PQ and leads to MEC hang.

Can you elaborate on this? I can see CPU reads being slower because the memory is uncached, but the ring buffer is mostly writes anyway. IIRC, the driver uses USWC for most if not all of the other ring buffers managed by the kernel. Why aren't those a problem?

Quoted 6 lines

We have this on other rings -

           mb();
           amdgpu_ring_set_wptr(ring);

I think the solution should be to use barrier before write pointer updates rather than relying on PCIe snooping.

Yeah, completely agree as well. The barrier also takes care of preventing the compiler from re-ordering writes.

Regards,
Christian.

Quoted 27 lines

Thanks,
Lijo

Quoted 24 lines

Alex

Quoted 22 lines
---
    drivers/gpu/drm/amd/amdkfd/kfd_device.c | 2 +-
    1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/amd/amdkfd/kfd_device.c +1 -2
index 1f1d79ac5e6c..fb087a0ff5bc 100644
@@ -779,7 +779,7 @@ bool kgd2kfd_device_init(struct kfd_dev *kfd,
           if (amdgpu_amdkfd_alloc_gtt_mem(
                           kfd->adev, size, &kfd->gtt_mem,
                           &kfd->gtt_start_gpu_addr, &kfd->gtt_start_cpu_ptr,
-                       false, true)) {
+                       false, false)) {
                   dev_err(kfd_device, "Could not allocate %d bytes\n", size);
                   goto alloc_gtt_mem_failure;
           }
--

2.34.1

#19 lore raw

RE: [PATCH 2/2] drm/amdkfd: use cache GTT buffer for PQ and wb pool

[AMD Official Use Only - AMD Internal Distribution Only]

For WPTR polling mem we didn't find it is stalled data, but for ring buffer we are pretty sure,

We use "0" or "ffffffff" to memset() HIQ before we fulfill the QUERY_STATUS pm4, and we can see when issue hit CP do get the either 0 or "ffffffff" .

Monk Liu | Cloud GPU & Virtualization | AMD

-----Original Message-----
From: Lazar, Lijo <[email protected]>
Sent: Thursday, November 14, 2024 3:13 PM
To: Liu, Monk <[email protected]>; Koenig, Christian <[email protected]>; Christian König <[email protected]>; Alex Deucher <[email protected]>; Zhao, Victor <[email protected]>
Cc: [email protected]; Yang, Philip <[email protected]>; Kuehling, Felix <[email protected]>
Subject: Re: [PATCH 2/2] drm/amdkfd: use cache GTT buffer for PQ and wb pool

Quoted 13 lines · Monk

[AMD Official Use Only - AMD Internal Distribution Only]

Quoted 1 line
Quoted 1 line

QEMU and their virtual drivers uses a virtual PCI MMIO BAR to map buffers from the host into the guest. Those buffers can be both point to the FB BAR as well as system memory.

Well, you are talking about virt-io GL use case? Or mDev solution ? because those conventional device like QXL, NIC, soundcard are purely software emulated and no way to point to the FB BAR ...

I can understand that Host make the final decision on the memory if it is FB can be leveraged by guest as some system memory, but brutely treat all system memory as cache no matter what guest do is ugly ...

Back to our topic, I propagated you and Lijo's statement to customer and they didn't buy it, rational as:

1) they don't think let host honor the real mapping is correct as they don't have anything like virt-io GL solution in use...
2) they cannot suffer risk to revert that KVM patch on their server.

Quoted 8 lines

So the thing is they will live with a hotfix patch for now, but they are also asking:

Why cannot AMD make all ring buffer (managed by KMD/KFD) to the same type which is cache, as they know that GFX ring from amdgpu is cache type.

Besides, both customer and CVS team will also keep investigate why after the map is honored by guest there is still coherence issue (type is USWC, mfence is inserted between doorbell and wptr_polling updates)

If this is truly mfence-not-working-as-expected, wondering how any user app which relies on a similar mechanism can reliably work on their systems.

Had already mentioned this before -

Even by the assumption that mfence fixed ordering but data stayed in WC buffer, there is one other hole in this theory. With DOORBELL_MODE=1, any write pointer update is fetched from memory. That write pointer is also maintained in WC type memory. If CP is correctly able to see WPTR update, that means that part of write got updated in system memory.

Anyway, please keep us posted.

Thanks,
Lijo

Quoted 350 lines

Anyway, Lijo, Christian, thanks for your explain and insight !

Monk Liu | Cloud GPU & Virtualization | AMD

-----Original Message-----
From: Koenig, Christian <[email protected]>
Sent: Tuesday, November 12, 2024 5:41 PM
To: Liu, Monk <[email protected]>; Christian König <[email protected]>; Lazar, Lijo <[email protected]>; Alex Deucher <[email protected]>; Zhao, Victor <[email protected]>
Cc: [email protected]; Yang, Philip <[email protected]>; Kuehling, Felix <[email protected]>
Subject: Re: [PATCH 2/2] drm/amdkfd: use cache GTT buffer for PQ and wb pool

Hi Monk,

Quoted 1 line

The memory we are talking about is pure guest system memory not FB BAR right?

No, that works a bit differently.

QEMU and their virtual drivers uses a virtual PCI MMIO BAR to map buffers from the host into the guest. Those buffers can be both point to the FB BAR as well as system memory.

Only the host knows if the buffer is backed by MMIO (FB) or system memory, so only the host can setup the tables to have the correct caching and WC attributes.

The KVM patch completely breaks that because they suddenly say that not the host setups the caching and WC attributes but the guest.

Because of that this KVM patch not only breaks amdgpu, but also other drivers which do the same thing. The general approach of the patch seems to be broken.

Review

Quoted 10 lines
          if (kq->dev->kfd->device_info.doorbell_size == 8) {
                  *kq->wptr64_kernel = kq->pending_wptr64;
(quoted diff) +2 -0
+               mb();
                  write_kernel_doorbell64(kq->queue->properties.doorbell_ptr,
                                          kq->pending_wptr64);
          } else {
                  *kq->wptr_kernel = kq->pending_wptr;
+               mb();
                  write_kernel_doorbell(kq->queue->properties.doorbell_ptr,
                                          kq->pending_wptr);

We should probably move that into write_kernel_doorbell()/write_kernel_doorbell64() but in general looks correct to me as well.

Regards,
Christian.

Am 12.11.24 um 04:50 schrieb Liu, Monk:

Quoted 300 lines

[AMD Official Use Only - AMD Internal Distribution Only]

Hi Lijo

Thanks for your quote to the SPEC, yes I read that as well, supposedly WC storage buffer shall be flushed to memory with mfence, but it just doesn't work after applied that KVM patch which honor the WC attributes from guest.

This means the WC storage buffer from a KVM guest couldn’t be flushed to memory even with mb inserted... I don't know why. (is there any inconsistencies between nested page tables and host page tables?)

Hi Christian

Quoted 1 line
Quoted 1 line

The guest sees driver exposed memory as "emulated" MMIO BAR of a PCIe device, that's just how QEMU is designed. Because of this the guest maps the memory USWC or uncached. But in reality the memory can as well be GTT memory which is cached.

The memory we are talking about is pure guest system memory not FB BAR right? Therefore, I don't see why in reality it has to be cached, you see that only guest vCPU is accessing this buffer.

BTW: we should aways insert that MB() in our KFD part:

Quoted 11 lines

Review

Quoted 11 lines
          if (kq->dev->kfd->device_info.doorbell_size == 8) {
                  *kq->wptr64_kernel = kq->pending_wptr64;
(quoted diff) +2 -0
+               mb();
                  write_kernel_doorbell64(kq->queue->properties.doorbell_ptr,
                                          kq->pending_wptr64);
          } else {
                  *kq->wptr_kernel = kq->pending_wptr;
+               mb();
                  write_kernel_doorbell(kq->queue->properties.doorbell_ptr,
                                          kq->pending_wptr);
          }

Thanks

Monk Liu | Cloud GPU & Virtualization | AMD

-----Original Message-----
From: Christian König <[email protected]>
Sent: Monday, November 11, 2024 8:40 PM
To: Lazar, Lijo <[email protected]>; Liu, Monk <[email protected]>; Koenig, Christian <[email protected]>; Alex Deucher <[email protected]>; Zhao, Victor <[email protected]>
Cc: [email protected]; Yang, Philip <[email protected]>; Kuehling, Felix <[email protected]>
Subject: Re: [PATCH 2/2] drm/amdkfd: use cache GTT buffer for PQ and wb pool

Hi guys,

trying to merge multiple responses back into one mail thread.

Lijo, you're completely right, but there is one really strong argument you are missing:

The patch here doesn't change the write combine behavior, it changes the caching behavior!

So when the patch works and fixes the issue, then the issue is 100% certain not a write combine issue but rather an incorrectly applied caching :)

Quoted 2 lines

From what I heard there was a KVM patch to correct the mapping behavior -- previously the mapping is forced to cache mode even guest KMD maps the buffer with "WC".
But after the patch the buffer will be really "WC" if guest maps it with WC"... and this reveals the bug and hit our issue.

Yeah, exactly that's the problem. The guest doesn't know if WB, USWC or uncached should be used!

The guest sees driver exposed memory as "emulated" MMIO BAR of a PCIe device, that's just how QEMU is designed. Because of this the guest maps the memory USWC or uncached. But in reality the memory can as well be GTT memory which is cached.

So forcing the cache mode even if the guest KMD maps the buffer with "WC" is intentional behavior, that's the correct approach.

The upstream people realized that as well. That's one major reason why the patch was reverted.

Quoted 1 line

Can you explain why USWC for ring buffer is safe, why it will not hit coherence issue, why you don't make all ring buffers with USWC even for amdgpu side if you really believe USWC is innocent ...

We already tried this, but there is simply no benefit for it.

Regards,
Christian.

Am 11.11.24 um 06:58 schrieb Lazar, Lijo:

Quoted 224 lines · Monk
Quoted 26 lines

[AMD Official Use Only - AMD Internal Distribution Only]

Hi Lijo

This is the patch we verified before:

drivers/gpu/drm/amd/amdkfd/kfd_kernel_queue.c +2 -0
index 4843dcb9a5f7..39553c7648eb 100644
@@ -308,10 +308,12 @@ int kq_submit_packet(struct kernel_queue *kq)
 
          if (kq->dev->kfd->device_info.doorbell_size == 8) {
                  *kq->wptr64_kernel = kq->pending_wptr64;
+               mb();
                  write_kernel_doorbell64(kq->queue->properties.doorbell_ptr,
                                          kq->pending_wptr64);
          } else {
                  *kq->wptr_kernel = kq->pending_wptr;
+               mb();
                  write_kernel_doorbell(kq->queue->properties.doorbell_ptr,
                                          kq->pending_wptr);
          }
Quoted 6 lines

This mb() doesn't resolve the problem during customer's testing, I also thought of MB() first in beginning like you and Christian ...
The mb() here shall resolve the re-ordering between WPTR and doorbell kicking so GPU won't fetch stalled data from WPTR polling once it receives notification from doorbell kicking. (in SR-IOV we set doorbell mode to force GPU still fetch from WPTR
polling area, doorbell kicking is just to notify GPU)

And by your theory: mb() shall flush the WC storage buffer to memory, thus, this mb() shall also make sure that the ring buffer is not holding stalled data anymore, right ?

This is not my theory. All the quotes in my earlier mails are from "Intel® 64 and IA-32 Architectures Software Developer’s Manual".

Yet another one -

<snip>

Writes may be delayed and combined in the write combining buffer (WC buffer) to reduce memory accesses.

"If the WC buffer is partially filled, the writes may be delayed until the next occurrence of a serializing event; such as an SFENCE or MFENCE instruction, CPUID or other serializing instruction, a read or write to uncached memory, an interrupt occurrence, or an execution of a LOCK instruction (including one with an XACQUIRE or XRELEASE prefix)."

</snip>

Quoted 4 lines

But we still hit hang and get stalled data from dump.

Maybe we need to put mb() in another place ? can you proposal if you insist the cache mapping is not acceptable to you and we can ask customer to verify again.

There are a couple of things which are still working strangely, they will need some explanation as well -

Even though write pointer allocations are also to WC region,
and are correctly seen by CP every time. Since it needs to get wptr updates from memory rather than doorbell value, don't know how your snooping theory works for this. Also, it is weird that WC writes to those pass MQD buffer writes even with fence.

MQD allocation for user queues are still from USWC as per
your latest patch and they still work correctly.

For debug -
Is the WPTR reg value correctly reflecting the memory value?
Have you tried initializing MQD to a known pattern (memset32/64) like a series of 0xcafedead and verified what is seen at the CP side?

Thanks,
Lijo

Quoted 147 lines

Thanks

Monk Liu | Cloud GPU & Virtualization | AMD

-----Original Message-----
From: Lazar, Lijo <[email protected]>
Sent: Friday, November 8, 2024 7:26 PM
To: Liu, Monk <[email protected]>; Koenig, Christian <[email protected]>; Alex Deucher <[email protected]>; Zhao, Victor <[email protected]>
Cc: [email protected]; Yang, Philip <[email protected]>; Kuehling, Felix <[email protected]>
Subject: Re: [PATCH 2/2] drm/amdkfd: use cache GTT buffer for PQ and wb pool

Quoted 4 lines · Monk

[AMD Official Use Only - AMD Internal Distribution Only]

To be clear for the mb() approach: Even if we insert mb() in prior to amdgpu_ring_set_wptr(ring), GPU might still fetch stalled data from PQ due to USWC attributes.

Inserting an mb() doesn't cause WC buffer flush is a wrong assumption.

All prior loads/stores are supposed to be globally visible. Hence mb() followed by a write pointer update also should guarantee the same (From Arch manual).

The MFENCE instruction establishes a memory fence for
both loads and stores. The processor ensures that no load or store after MFENCE will become globally visible *until all loads and stores before MFENCE are globally visible.*

Ignoring the amdgpu driver part of it - if mb() is not working as expected as you claim that means something is wrong with the system.

USWC or WB for ring type may still be a separate discussion.

Thanks,
Lijo

Quoted 102 lines

The issue here is not the re-ordering but the stalled PQ.

Monk Liu | Cloud GPU & Virtualization | AMD

-----Original Message-----
From: Liu, Monk
Sent: Friday, November 8, 2024 6:22 PM
To: Koenig, Christian <[email protected]>; Lazar, Lijo <[email protected]>; Alex Deucher <[email protected]>; Zhao, Victor <[email protected]>
Cc: [email protected]; Yang, Philip <[email protected]>; Kuehling, Felix <[email protected]>
Subject: RE: [PATCH 2/2] drm/amdkfd: use cache GTT buffer for PQ and wb pool

Christian/Lijo

We verified all approaches, and we know what works and not works, obviously the mb() doesn't work.

The way of mb() between set wptr_polling and kicking off doorbell is theoretically correct, and I'm not object to do so... but this won't resolve the issue we hit.
First of all, USWC will have some chance that the data is still in CPU's WC storage place and not flushed to the memory and even with MB() get rid of re-ordering GPU might still have a chance to read stalled data from ring buffer as long as it is mapped USWC.

This is why only cache plus snoop memory can get rid of inconsistence issues.

Besides, do you know what's the rational to keep all GFX KCQ cache+snoop but only HIQ/KIQ from KFD configured to USWC ?

For performance concern that looks to me always the second priority compared to "correct" especially under the case HIQ contributes very little to ROCM performance when switching to cache mapping.

Monk Liu | Cloud GPU & Virtualization | AMD

-----Original Message-----
From: Koenig, Christian <[email protected]>
Sent: Thursday, November 7, 2024 7:57 PM
To: Lazar, Lijo <[email protected]>; Alex Deucher <[email protected]>; Zhao, Victor <[email protected]>
Cc: [email protected]; Liu, Monk <[email protected]>; Yang, Philip <[email protected]>; Kuehling, Felix <[email protected]>
Subject: Re: [PATCH 2/2] drm/amdkfd: use cache GTT buffer for PQ and wb pool

Am 07.11.24 um 06:58 schrieb Lazar, Lijo:

Quoted 13 lines · Alex Deucher
Quoted 8 lines · Victor Zhao
Quoted 7 lines

From: Monk Liu <[email protected]>

As cache GTT buffer is snooped, this way the coherence between CPU write and GPU fetch is guaranteed, but original code uses WC
+ unsnooped for HIQ PQ(ring buffer) which introduces coherency issues:
MEC fetches a stall data from PQ and leads to MEC hang.

Quoted 4 lines

Can you elaborate on this? I can see CPU reads being slower because the memory is uncached, but the ring buffer is mostly writes anyway. IIRC, the driver uses USWC for most if not all of the other ring buffers managed by the kernel. Why aren't those a problem?

Quoted 6 lines

We have this on other rings -

           mb();
           amdgpu_ring_set_wptr(ring);

I think the solution should be to use barrier before write pointer updates rather than relying on PCIe snooping.

Yeah, completely agree as well. The barrier also takes care of preventing the compiler from re-ordering writes.

Regards,
Christian.

Quoted 27 lines

Thanks,
Lijo

Quoted 24 lines

Alex

Quoted 22 lines
---
    drivers/gpu/drm/amd/amdkfd/kfd_device.c | 2 +-
    1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/amd/amdkfd/kfd_device.c +1 -2
index 1f1d79ac5e6c..fb087a0ff5bc 100644
@@ -779,7 +779,7 @@ bool kgd2kfd_device_init(struct kfd_dev *kfd,
           if (amdgpu_amdkfd_alloc_gtt_mem(
                           kfd->adev, size, &kfd->gtt_mem,
                           &kfd->gtt_start_gpu_addr, &kfd->gtt_start_cpu_ptr,
-                       false, true)) {
+                       false, false)) {
                   dev_err(kfd_device, "Could not allocate %d bytes\n", size);
                   goto alloc_gtt_mem_failure;
           }
--

2.34.1

Reviewed-by on v1 1/2
#20 lore raw
In reply to Victor Zhao
Quoted 11 lines · Victor Zhao

From: Gang Ba <[email protected]>

amdgpu_amdkfd_alloc_gtt_mem currently allocates USWC memory. It uses write-combining for CPU access, which is slow for reading. Add a new parameter to amdgpu_amdkfd_alloc_gtt_mem to allocate normal GTT memory.

Quoted 116 lines
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.c             | 7 +++++--
 drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h             | 2 +-
 drivers/gpu/drm/amd/amdkfd/kfd_device.c                | 2 +-
 drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c  | 2 +-
 drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_v9.c        | 2 +-
 drivers/gpu/drm/amd/amdkfd/kfd_process.c               | 2 +-
 drivers/gpu/drm/amd/amdkfd/kfd_process_queue_manager.c | 2 +-
 7 files changed, 11 insertions(+), 8 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.c +5 -2
index 713be49540d4..acb762855c24 100644
@@ -294,7 +294,7 @@ void amdgpu_amdkfd_gpu_reset(struct amdgpu_device *adev)
 
 int amdgpu_amdkfd_alloc_gtt_mem(struct amdgpu_device *adev, size_t size,
                                void **mem_obj, uint64_t *gpu_addr,
-                               void **cpu_ptr, bool cp_mqd_gfx9)
+                               void **cpu_ptr, bool cp_mqd_gfx9, bool is_uswc_mode)
 {
        struct amdgpu_bo *bo = NULL;
        struct amdgpu_bo_param bp;
@@ -305,7 +305,10 @@ int amdgpu_amdkfd_alloc_gtt_mem(struct amdgpu_device *adev, size_t size,
        bp.size = size;
        bp.byte_align = PAGE_SIZE;
        bp.domain = AMDGPU_GEM_DOMAIN_GTT;
-       bp.flags = AMDGPU_GEM_CREATE_CPU_GTT_USWC;
+       if (is_uswc_mode)
+               bp.flags = AMDGPU_GEM_CREATE_CPU_GTT_USWC;
+       else
+               bp.flags = 0;
        bp.type = ttm_bo_type_kernel;
        bp.resv = NULL;
        bp.bo_ptr_size = sizeof(struct amdgpu_bo);
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h +1 -1
index 4b80ad860639..745121e0dd8e 100644
@@ -234,7 +234,7 @@ int amdgpu_amdkfd_bo_validate_and_fence(struct amdgpu_bo *bo,
 /* Shared API */
 int amdgpu_amdkfd_alloc_gtt_mem(struct amdgpu_device *adev, size_t size,
                                void **mem_obj, uint64_t *gpu_addr,
-                               void **cpu_ptr, bool mqd_gfx9);
+                               void **cpu_ptr, bool mqd_gfx9, bool is_uswc_mode);
 void amdgpu_amdkfd_free_gtt_mem(struct amdgpu_device *adev, void **mem_obj);
 int amdgpu_amdkfd_alloc_gws(struct amdgpu_device *adev, size_t size,
                                void **mem_obj);
drivers/gpu/drm/amd/amdkfd/kfd_device.c +1 -1
index 956198da7859..1f1d79ac5e6c 100644
@@ -779,7 +779,7 @@ bool kgd2kfd_device_init(struct kfd_dev *kfd,
        if (amdgpu_amdkfd_alloc_gtt_mem(
                        kfd->adev, size, &kfd->gtt_mem,
                        &kfd->gtt_start_gpu_addr, &kfd->gtt_start_cpu_ptr,
-                       false)) {
+                       false, true)) {
                dev_err(kfd_device, "Could not allocate %d bytes\n", size);
                goto alloc_gtt_mem_failure;
        }
drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +1 -1
index 38c19dc8311d..9672542578d4 100644
@@ -2783,7 +2783,7 @@ static int allocate_hiq_sdma_mqd(struct device_queue_manager *dqm)
 
        retval = amdgpu_amdkfd_alloc_gtt_mem(dev->adev, size,
                &(mem_obj->gtt_mem), &(mem_obj->gpu_addr),
-               (void *)&(mem_obj->cpu_ptr), false);
+               (void *)&(mem_obj->cpu_ptr), false, true);
 
        return retval;
 }
drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_v9.c +1 -1
index 84e8ea3a8a0c..c9882f1d4419 100644
@@ -140,7 +140,7 @@ static struct kfd_mem_obj *allocate_mqd(struct kfd_node *node,
                        NUM_XCC(node->xcc_mask),
                        &(mqd_mem_obj->gtt_mem),
                        &(mqd_mem_obj->gpu_addr),
-                       (void *)&(mqd_mem_obj->cpu_ptr), true);
+                       (void *)&(mqd_mem_obj->cpu_ptr), true, true);
 
                if (retval) {
                        kfree(mqd_mem_obj);
drivers/gpu/drm/amd/amdkfd/kfd_process.c +1 -1
index 6bab6fc6a35d..e1d8fd11d2b1 100644
@@ -1639,7 +1639,7 @@ struct kfd_process_device *kfd_create_process_device_data(struct kfd_node *dev,
                                                &pdd->proc_ctx_bo,
                                                &pdd->proc_ctx_gpu_addr,
                                                &pdd->proc_ctx_cpu_ptr,
-                                               false);
+                                               false, true);
                if (retval) {
                        dev_err(dev->adev->dev,
                                "failed to allocate process context bo\n");
drivers/gpu/drm/amd/amdkfd/kfd_process_queue_manager.c +1 -2
index c76db22a1000..3a3be0e19fb9 100644
@@ -260,7 +260,7 @@ static int init_user_queue(struct process_queue_manager *pqm,
                                                &(*q)->gang_ctx_bo,
                                                &(*q)->gang_ctx_gpu_addr,
                                                &(*q)->gang_ctx_cpu_ptr,
-                                               false);
+                                               false, true);
                if (retval) {
                        pr_err("failed to allocate gang context bo\n");
                        goto cleanup;
--

2.34.1