Skip to contentSkip to conversation

drm/amdgpu: Acquire ttm locks for dmaunmap

PATCH Superseded David Francis 2 participants 4 messages Oct 11, 2023

Subsystems (3)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 drm/amdgpu: Acquire ttm locks for dmaunmap

dmaunmap can call ttm_bo_validate, which expects the ttm dma_resv to be held.

Acquire the locks in amdgpu_amdkfd_gpuvm_dmaunmap_mem.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h       |  2 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c | 12 +++++++++++-
 drivers/gpu/drm/amd/amdkfd/kfd_chardev.c         |  7 ++++++-
 3 files changed, 18 insertions(+), 3 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h +1 -1
index 3ad8dc523b42..dba4f6b7a2f9 100644
@@ -302,7 +302,7 @@ int amdgpu_amdkfd_gpuvm_map_memory_to_gpu(struct amdgpu_device *adev,
 					  struct kgd_mem *mem, void *drm_priv);
 int amdgpu_amdkfd_gpuvm_unmap_memory_from_gpu(
 		struct amdgpu_device *adev, struct kgd_mem *mem, void *drm_priv);
-void amdgpu_amdkfd_gpuvm_dmaunmap_mem(struct kgd_mem *mem, void *drm_priv);
+int amdgpu_amdkfd_gpuvm_dmaunmap_mem(struct kgd_mem *mem, void *drm_priv);
 int amdgpu_amdkfd_gpuvm_sync_memory(
 		struct amdgpu_device *adev, struct kgd_mem *mem, bool intr);
 int amdgpu_amdkfd_gpuvm_map_gtt_bo_to_kernel(struct kgd_mem *mem,
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +11 -1
index a15e59abe70a..808deec8aa58 100644
@@ -2094,21 +2094,31 @@ int amdgpu_amdkfd_gpuvm_map_memory_to_gpu(
 	return ret;
 }
 
-void amdgpu_amdkfd_gpuvm_dmaunmap_mem(struct kgd_mem *mem, void *drm_priv)
+int amdgpu_amdkfd_gpuvm_dmaunmap_mem(struct kgd_mem *mem, void *drm_priv)
 {
 	struct kfd_mem_attachment *entry;
 	struct amdgpu_vm *vm;
+	struct bo_vm_reservation_context ctx;
+	int ret;
 
 	vm = drm_priv_to_vm(drm_priv);
 
 	mutex_lock(&mem->lock);
 
+	ret = reserve_bo_and_cond_vms(mem, vm, BO_VM_MAPPED, &ctx);
+	if (ret)
+		goto out;
+
 	list_for_each_entry(entry, &mem->attachments, list) {
 		if (entry->bo_va->base.vm == vm)
 			kfd_mem_dmaunmap_attachment(mem, entry);
 	}
 
+	unreserve_bo_and_vms(&ctx, false, false);
+
+out:
 	mutex_unlock(&mem->lock);
+	return ret;
 }
 
 int amdgpu_amdkfd_gpuvm_unmap_memory_from_gpu(
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +6 -1
index 06988cf1db51..21d4e7d46238 100644
@@ -1442,7 +1442,11 @@ static int kfd_ioctl_unmap_memory_from_gpu(struct file *filep,
 			kfd_flush_tlb(peer_pdd, TLB_FLUSH_HEAVYWEIGHT);
 
 		/* Remove dma mapping after tlb flush to avoid IO_PAGE_FAULT */
-		amdgpu_amdkfd_gpuvm_dmaunmap_mem(mem, peer_pdd->drm_priv);
+		err = amdgpu_amdkfd_gpuvm_dmaunmap_mem(mem, peer_pdd->drm_priv);
+		if (err) {
+			pr_debug("DMA unmapping failed\n");
+			goto dmaunmap_failed;
+		}
 	}
 
 	mutex_unlock(&p->mutex);
@@ -1455,6 +1459,7 @@ static int kfd_ioctl_unmap_memory_from_gpu(struct file *filep,
 get_mem_obj_from_handle_failed:
 unmap_memory_from_gpu_failed:
 sync_memory_failed:
+dmaunmap_failed:
 	mutex_unlock(&p->mutex);
 copy_from_user_failed:
 	kfree(devices_arr);
Signature · 1 line
2.34.1
#1 lore raw

PATCH 2/2 drm/amdgpu: Permit PCIe transfer over links with XGMI

When the CPU is XGMI connected, the PCIe links should not be enumerated for topology purposes. However, PCIe transfer should still be a valid option for memory attachment that requires it.

Move the XGMI connection check out of the shared helper function amdgpu_device_is_peer_accessible and into the topology path.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_device.c | 4 +---
 drivers/gpu/drm/amd/amdkfd/kfd_topology.c  | 3 +++
 2 files changed, 4 insertions(+), 3 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +1 -3
index bad2b5577e96..b47cb7f8cfbd 100644
@@ -5753,9 +5753,7 @@ bool amdgpu_device_is_peer_accessible(struct amdgpu_device *adev,
 		~*peer_adev->dev->dma_mask : ~((1ULL << 32) - 1);
 	resource_size_t aper_limit =
 		adev->gmc.aper_base + adev->gmc.aper_size - 1;
-	bool p2p_access =
-		!adev->gmc.xgmi.connected_to_cpu &&
-		!(pci_p2pdma_distance(adev->pdev, peer_adev->dev, false) < 0);
+	bool p2p_access = !(pci_p2pdma_distance(adev->pdev, peer_adev->dev, false) < 0);
 
 	return pcie_p2p && p2p_access && (adev->gmc.visible_vram_size &&
 		adev->gmc.real_vram_size == adev->gmc.visible_vram_size &&
drivers/gpu/drm/amd/amdkfd/kfd_topology.c +3 -0
index 4e530791507e..f0cff5072736 100644
@@ -1423,6 +1423,9 @@ static int kfd_add_peer_prop(struct kfd_topology_device *kdev,
 				peer->gpu->adev))
 		return ret;
 
+	if (kdev->gpu->adev->gmc.xgmi.connected_to_cpu)
+		return ret;
+
 	iolink1 = list_first_entry(&kdev->io_link_props,
 							struct kfd_iolink_properties, list);
 	if (!iolink1)
Signature · 1 line
2.34.1
maintainer
#2 lore raw

Re: [PATCH 2/2] drm/amdgpu: Permit PCIe transfer over links with XGMI

Quoted 4 lines · David Francis

When the CPU is XGMI connected, the PCIe links should not be enumerated for topology purposes. However, PCIe transfer should still be a valid option for memory attachment that requires it.

You could be more specific here. This is for remote doorbells and MMIO mappings.

Quoted 31 lines

Move the XGMI connection check out of the shared helper function amdgpu_device_is_peer_accessible and into the topology path.

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_device.c | 4 +---
  drivers/gpu/drm/amd/amdkfd/kfd_topology.c  | 3 +++
  2 files changed, 4 insertions(+), 3 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +1 -3
index bad2b5577e96..b47cb7f8cfbd 100644
@@ -5753,9 +5753,7 @@ bool amdgpu_device_is_peer_accessible(struct amdgpu_device *adev,
  		~*peer_adev->dev->dma_mask : ~((1ULL << 32) - 1);
  	resource_size_t aper_limit =
  		adev->gmc.aper_base + adev->gmc.aper_size - 1;
-	bool p2p_access =
-		!adev->gmc.xgmi.connected_to_cpu &&
-		!(pci_p2pdma_distance(adev->pdev, peer_adev->dev, false) < 0);
+	bool p2p_access = !(pci_p2pdma_distance(adev->pdev, peer_adev->dev, false) < 0);
 
  	return pcie_p2p && p2p_access && (adev->gmc.visible_vram_size &&
  		adev->gmc.real_vram_size == adev->gmc.visible_vram_size &&
drivers/gpu/drm/amd/amdkfd/kfd_topology.c +0 -0
index 4e530791507e..f0cff5072736 100644

drivers/gpu/drm/amd/amdkfd/kfd_topology.c static int kfd_add_peer_prop(struct kfd_topology_device *kdev, · line 1428

Quoted 6 lines
drivers/gpu/drm/amd/amdkfd/kfd_topology.c +3 -0
@@ -1423,6 +1423,9 @@ static int kfd_add_peer_prop(struct kfd_topology_device *kdev,
  				peer->gpu->adev))
  		return ret;
 
+	if (kdev->gpu->adev->gmc.xgmi.connected_to_cpu)
+		return ret;
+

I believe this is only needed for the case that XGMI is disabled via the module param. When XGMI is enabled, you shouldn't get here because kfd_dev_create_p2p_links doesn't call kfd_add_peer_prop if the GPUs are themselves in an XGMI hive. In fact, it may be clearer to move this condition into kfd_dev_create_p2p_links.

Regards,
  Felix

Quoted 3 lines
drivers/gpu/drm/amd/amdkfd/kfd_topology.c +0 -0
  	iolink1 = list_first_entry(&kdev->io_link_props,
  							struct kfd_iolink_properties, list);
  	if (!iolink1)
maintainer
#3 lore raw
In reply to David Francis
Quoted 36 lines · David Francis

dmaunmap can call ttm_bo_validate, which expects the ttm dma_resv to be held.

Acquire the locks in amdgpu_amdkfd_gpuvm_dmaunmap_mem.

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h       |  2 +-
  drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c | 12 +++++++++++-
  drivers/gpu/drm/amd/amdkfd/kfd_chardev.c         |  7 ++++++-
  3 files changed, 18 insertions(+), 3 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h +1 -1
index 3ad8dc523b42..dba4f6b7a2f9 100644
@@ -302,7 +302,7 @@ int amdgpu_amdkfd_gpuvm_map_memory_to_gpu(struct amdgpu_device *adev,
  					  struct kgd_mem *mem, void *drm_priv);
  int amdgpu_amdkfd_gpuvm_unmap_memory_from_gpu(
  		struct amdgpu_device *adev, struct kgd_mem *mem, void *drm_priv);
-void amdgpu_amdkfd_gpuvm_dmaunmap_mem(struct kgd_mem *mem, void *drm_priv);
+int amdgpu_amdkfd_gpuvm_dmaunmap_mem(struct kgd_mem *mem, void *drm_priv);
  int amdgpu_amdkfd_gpuvm_sync_memory(
  		struct amdgpu_device *adev, struct kgd_mem *mem, bool intr);
  int amdgpu_amdkfd_gpuvm_map_gtt_bo_to_kernel(struct kgd_mem *mem,
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +1 -1
index a15e59abe70a..808deec8aa58 100644
@@ -2094,21 +2094,31 @@ int amdgpu_amdkfd_gpuvm_map_memory_to_gpu(
  	return ret;
  }
 
-void amdgpu_amdkfd_gpuvm_dmaunmap_mem(struct kgd_mem *mem, void *drm_priv)
+int amdgpu_amdkfd_gpuvm_dmaunmap_mem(struct kgd_mem *mem, void *drm_priv)
  {

drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c int amdgpu_amdkfd_gpuvm_map_memory_to_gpu( · line 2108

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +3 -0
  	struct kfd_mem_attachment *entry;
  	struct amdgpu_vm *vm;
+	struct bo_vm_reservation_context ctx;
+	int ret;
 
  	vm = drm_priv_to_vm(drm_priv);
 
  	mutex_lock(&mem->lock);
 
+	ret = reserve_bo_and_cond_vms(mem, vm, BO_VM_MAPPED, &ctx);

Looks like you copied this from somewhere else. Do you really need to reserve the VM here, or just the BO? If you only need to reserve the BO, just use amdgpu_bo_reserve.

Quoted 21 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +7 -0
+	if (ret)
+		goto out;
+
  	list_for_each_entry(entry, &mem->attachments, list) {
  		if (entry->bo_va->base.vm == vm)
  			kfd_mem_dmaunmap_attachment(mem, entry);
  	}
 
+	unreserve_bo_and_vms(&ctx, false, false);
+
+out:
  	mutex_unlock(&mem->lock);
+	return ret;
  }
 
  int amdgpu_amdkfd_gpuvm_unmap_memory_from_gpu(
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +0 -0
index 06988cf1db51..21d4e7d46238 100644

drivers/gpu/drm/amd/amdkfd/kfd_chardev.c static int kfd_ioctl_unmap_memory_from_gpu(struct file *filep, · line 1446

Quoted 6 lines
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +2 -1
@@ -1442,7 +1442,11 @@ static int kfd_ioctl_unmap_memory_from_gpu(struct file *filep,
  			kfd_flush_tlb(peer_pdd, TLB_FLUSH_HEAVYWEIGHT);
 
  		/* Remove dma mapping after tlb flush to avoid IO_PAGE_FAULT */
-		amdgpu_amdkfd_gpuvm_dmaunmap_mem(mem, peer_pdd->drm_priv);
+		err = amdgpu_amdkfd_gpuvm_dmaunmap_mem(mem, peer_pdd->drm_priv);
+		if (err) {

This is probably not correct. The only failure case I know of is, that the reservation is interrupted by a signal and returns -ERESTARTSYS. When this happens, the ioctl will automatically get restarted after handling the signal. We need to make sure that we handle this restart case correctly. When this happens, all the unmapping is already done from the first time the function ran. All we need to repeat is the dmaunmap.

We already handled this scenario for amdgpu_amdkfd_gpuvm_sync_memory. Make sure it also works correctly for this new dmaunmap case. Basically, we don't want to accidentally dmaunmap the same thing twice. And we need to make sure that amdgpu_amdkfd_gpuvm_sync_memory is harmless if it's called multiple times due to restarts.

There is a test in KFDTest specifically to try to recreate this scenario where amdgpu_amdkfd_gpuvm_sync_memory gets interrupted by a signal: KFDMemoryTest.SignalHandling. It probably won't be able to trigger a signal interrupting an amdgpu_bo_reserve, though, because that's very fast if there is no lock contention. So we may just have to rely on code review. Or you can try to recreate the scenario manually by returning -ERESTARTSYS under some conditions.

Regards,
  Felix

Quoted 14 lines
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +4 -0
+			pr_debug("DMA unmapping failed\n");
+			goto dmaunmap_failed;
+		}
  	}
 
  	mutex_unlock(&p->mutex);
@@ -1455,6 +1459,7 @@ static int kfd_ioctl_unmap_memory_from_gpu(struct file *filep,
  get_mem_obj_from_handle_failed:
  unmap_memory_from_gpu_failed:
  sync_memory_failed:
+dmaunmap_failed:
  	mutex_unlock(&p->mutex);
  copy_from_user_failed:
  	kfree(devices_arr);