Skip to contentSkip to conversation

drm/amdgpu: Acquire ttm locks for dmaunmap

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

Subsystems (3)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 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.

Because the dmaunmap step can now fail, the unmap ioctl UAPI needs two new arguments. n_dmaunmap_success tracks the number of devices that have completed dmaunmap. If a device fails to dmaunmap due to a signal interrupt, n_dmaunmap_bos tracks the number of bos on that device that were successfully dmaunmapped.

This failure can also cause the sync_memory step of the ioctl to be repeated; it is idempotent, so this should not cause any issues.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h    |  2 +-
 .../gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c  | 23 +++++++++++++++----
 drivers/gpu/drm/amd/amdkfd/kfd_chardev.c      | 14 +++++++++--
 include/uapi/linux/kfd_ioctl.h                |  2 ++
 4 files changed, 34 insertions(+), 7 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h +1 -1
index 3ad8dc523b42..781642871900 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, uint32_t *num_bos);
 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 +19 -4
index a15e59abe70a..cbd6032f3d39 100644
@@ -2094,21 +2094,36 @@ 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, uint32_t *num_bos)
 {
 	struct kfd_mem_attachment *entry;
 	struct amdgpu_vm *vm;
+	int ret;
+	int i = 0;
 
 	vm = drm_priv_to_vm(drm_priv);
 
 	mutex_lock(&mem->lock);
 
 	list_for_each_entry(entry, &mem->attachments, list) {
-		if (entry->bo_va->base.vm == vm)
-			kfd_mem_dmaunmap_attachment(mem, entry);
-	}
+		if (i >= *num_bos) {
+			ret = amdgpu_bo_reserve(entry->bo_va->base.bo, false);
+			if (ret) {
+				*num_bos = i;
+				goto out;
+			}
+
+			if (entry->bo_va->base.vm == vm)
+				kfd_mem_dmaunmap_attachment(mem, entry);
 
+			amdgpu_bo_unreserve(entry->bo_va->base.bo);
+		}
+		i++;
+	}
+	*num_bos = 0;
+out:
 	mutex_unlock(&mem->lock);
+	return ret;
 }
 
 int amdgpu_amdkfd_gpuvm_unmap_memory_from_gpu(
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +12 -2
index 06988cf1db51..a944e255de4a 100644
@@ -1379,6 +1379,10 @@ static int kfd_ioctl_unmap_memory_from_gpu(struct file *filep,
 		pr_debug("n_success exceeds n_devices\n");
 		return -EINVAL;
 	}
+	if (args->n_dmaunmap_success > args->n_devices) {
+		pr_debug("n_dmaunmap_success exceeds n_devices\n");
+		return -EINVAL;
+	}
 
 	devices_arr = kmalloc_array(args->n_devices, sizeof(*devices_arr),
 				    GFP_KERNEL);
@@ -1434,7 +1438,7 @@ static int kfd_ioctl_unmap_memory_from_gpu(struct file *filep,
 	}
 
 	/* Flush TLBs after waiting for the page table updates to complete */
-	for (i = 0; i < args->n_devices; i++) {
+	for (i = args->n_dmaunmap_success; i < args->n_devices; i++) {
 		peer_pdd = kfd_process_device_data_by_id(p, devices_arr[i]);
 		if (WARN_ON_ONCE(!peer_pdd))
 			continue;
@@ -1442,7 +1446,12 @@ 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, &args->n_dmaunmap_bos);
+		if (err) {
+			pr_debug("DMA unmapping failed, acquire interrupted by user signal\n");
+			goto dmaunmap_failed;
+		}
+		args->n_dmaunmap_success = i+1;
 	}
 
 	mutex_unlock(&p->mutex);
@@ -1455,6 +1464,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);
include/uapi/linux/kfd_ioctl.h +2 -0
index f0ed68974c54..edeeac370201 100644
@@ -467,6 +467,8 @@ struct kfd_ioctl_unmap_memory_from_gpu_args {
 	__u64 device_ids_array_ptr;	/* to KFD */
 	__u32 n_devices;		/* to KFD */
 	__u32 n_success;		/* to/from KFD */
+	__u32 n_dmaunmap_success;	/* to/from KFD */
+	__u32 n_dmaunmap_bos;		/* to/from KFD */
 };
 
 /* Allocate GWS for specific queue
Signature · 1 line
2.34.1
#1 lore raw

PATCH v2 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 remote doorbells and MMIO mappings.

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  | 6 ++++--
 2 files changed, 5 insertions(+), 5 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 +4 -2
index 4e530791507e..cb64c19482f3 100644
@@ -1514,11 +1514,13 @@ static int kfd_dev_create_p2p_links(void)
 			goto next;
 
 		/* check if node(s) is/are peer accessible in one direction or bi-direction */
-		ret = kfd_add_peer_prop(new_dev, dev, i, k);
+		if (!new_dev->gpu->adev->gmc.xgmi.connected_to_cpu)
+			ret = kfd_add_peer_prop(new_dev, dev, i, k);
 		if (ret < 0)
 			goto out;
 
-		ret = kfd_add_peer_prop(dev, new_dev, k, i);
+		if (!dev->gpu->adev->gmc.xgmi.connected_to_cpu)
+			ret = kfd_add_peer_prop(dev, new_dev, k, i);
 		if (ret < 0)
 			goto out;
 next:
Signature · 1 line
2.34.1
maintainer
#2 lore raw

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

Quoted 35 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 remote doorbells and MMIO mappings.

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  | 6 ++++--
  2 files changed, 5 insertions(+), 5 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..cb64c19482f3 100644

drivers/gpu/drm/amd/amdkfd/kfd_topology.c static int kfd_dev_create_p2p_links(void) · line 1517

Quoted 5 lines
drivers/gpu/drm/amd/amdkfd/kfd_topology.c +1 -1
@@ -1514,11 +1514,13 @@ static int kfd_dev_create_p2p_links(void)
  			goto next;
 
  		/* check if node(s) is/are peer accessible in one direction or bi-direction */
-		ret = kfd_add_peer_prop(new_dev, dev, i, k);
+		if (!new_dev->gpu->adev->gmc.xgmi.connected_to_cpu)

Yikes. I was thinking this should be something like

	if (... xgmi.connected_to_cpu)
		goto next;

I didn't consider that the check needs to be separate for each GPU. I mean, it's not exactly the same thing, but would it make sense to have a check like this?

	if (new_dev->gpu->adev->gmc.xgmi.connected_to_cpu &&
	    dev->gpu->adev->gmc.xgmi.connected_to_cpu)
		goto next;

I don't see why this should depend on the direction of the link. We don't want to advertise PCI P2P links between pairs of GPUs that are both connected to the CPU via XGMI. We don't currently support mixed systems with both XGMI and PCIe connected GPUs. But if that ever existed, I think we would want to allow P2P links between those, regardless of the direction.

Regards,
  Felix

Quoted 10 lines
drivers/gpu/drm/amd/amdkfd/kfd_topology.c +3 -1
+			ret = kfd_add_peer_prop(new_dev, dev, i, k);
  		if (ret < 0)
  			goto out;
 
-		ret = kfd_add_peer_prop(dev, new_dev, k, i);
+		if (!dev->gpu->adev->gmc.xgmi.connected_to_cpu)
+			ret = kfd_add_peer_prop(dev, new_dev, k, i);
  		if (ret < 0)
  			goto out;
  next:
maintainer
#3 lore raw
In reply to David Francis
Quoted 132 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.

Because the dmaunmap step can now fail, the unmap ioctl UAPI needs two new arguments. n_dmaunmap_success tracks the number of devices that have completed dmaunmap. If a device fails to dmaunmap due to a signal interrupt, n_dmaunmap_bos tracks the number of bos on that device that were successfully dmaunmapped.

This failure can also cause the sync_memory step of the ioctl to be repeated; it is idempotent, so this should not cause any issues.

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h    |  2 +-
  .../gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c  | 23 +++++++++++++++----
  drivers/gpu/drm/amd/amdkfd/kfd_chardev.c      | 14 +++++++++--
  include/uapi/linux/kfd_ioctl.h                |  2 ++
  4 files changed, 34 insertions(+), 7 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h +1 -1
index 3ad8dc523b42..781642871900 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, uint32_t *num_bos);
  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 +19 -4
index a15e59abe70a..cbd6032f3d39 100644
@@ -2094,21 +2094,36 @@ 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, uint32_t *num_bos)
  {
  	struct kfd_mem_attachment *entry;
  	struct amdgpu_vm *vm;
+	int ret;
+	int i = 0;
 
  	vm = drm_priv_to_vm(drm_priv);
 
  	mutex_lock(&mem->lock);
 
  	list_for_each_entry(entry, &mem->attachments, list) {
-		if (entry->bo_va->base.vm == vm)
-			kfd_mem_dmaunmap_attachment(mem, entry);
-	}
+		if (i >= *num_bos) {
+			ret = amdgpu_bo_reserve(entry->bo_va->base.bo, false);
+			if (ret) {
+				*num_bos = i;
+				goto out;
+			}
+
+			if (entry->bo_va->base.vm == vm)
+				kfd_mem_dmaunmap_attachment(mem, entry);
 
+			amdgpu_bo_unreserve(entry->bo_va->base.bo);
+		}
+		i++;
+	}
+	*num_bos = 0;
+out:
  	mutex_unlock(&mem->lock);
+	return ret;
  }
 
  int amdgpu_amdkfd_gpuvm_unmap_memory_from_gpu(
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +12 -2
index 06988cf1db51..a944e255de4a 100644
@@ -1379,6 +1379,10 @@ static int kfd_ioctl_unmap_memory_from_gpu(struct file *filep,
  		pr_debug("n_success exceeds n_devices\n");
  		return -EINVAL;
  	}
+	if (args->n_dmaunmap_success > args->n_devices) {
+		pr_debug("n_dmaunmap_success exceeds n_devices\n");
+		return -EINVAL;
+	}
 
  	devices_arr = kmalloc_array(args->n_devices, sizeof(*devices_arr),
  				    GFP_KERNEL);
@@ -1434,7 +1438,7 @@ static int kfd_ioctl_unmap_memory_from_gpu(struct file *filep,
  	}
 
  	/* Flush TLBs after waiting for the page table updates to complete */
-	for (i = 0; i < args->n_devices; i++) {
+	for (i = args->n_dmaunmap_success; i < args->n_devices; i++) {
  		peer_pdd = kfd_process_device_data_by_id(p, devices_arr[i]);
  		if (WARN_ON_ONCE(!peer_pdd))
  			continue;
@@ -1442,7 +1446,12 @@ 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, &args->n_dmaunmap_bos);
+		if (err) {
+			pr_debug("DMA unmapping failed, acquire interrupted by user signal\n");
+			goto dmaunmap_failed;
+		}
+		args->n_dmaunmap_success = i+1;
  	}
 
  	mutex_unlock(&p->mutex);
@@ -1455,6 +1464,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);
include/uapi/linux/kfd_ioctl.h +0 -0
index f0ed68974c54..edeeac370201 100644

include/uapi/linux/kfd_ioctl.h struct kfd_ioctl_unmap_memory_from_gpu_args { · line 471

Quoted 5 lines
include/uapi/linux/kfd_ioctl.h +2 -0
@@ -467,6 +467,8 @@ struct kfd_ioctl_unmap_memory_from_gpu_args {
  	__u64 device_ids_array_ptr;	/* to KFD */
  	__u32 n_devices;		/* to KFD */
  	__u32 n_success;		/* to/from KFD */
+	__u32 n_dmaunmap_success;	/* to/from KFD */
+	__u32 n_dmaunmap_bos;		/* to/from KFD */

This breaks the ABI with existing user mode, because this is the argument structure being passed back and forth with user mode. You can't change this without a very good reason and a strategy for maintaining backwards compatibility with existing user mode.

Regards,
  Felix

Quoted 3 lines
include/uapi/linux/kfd_ioctl.h +0 -0
  };
 
  /* Allocate GWS for specific queue