Skip to contentSkip to conversation

drm/amdgpu: Acquire ttm locks for dmaunmap

PATCH In review David Francis 3 participants 7 messages Oct 24, 2023 – Oct 26, 2023

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

PATCH v3 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, two new numbers need to be tracked. 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.

Track those values in struct kgd_mem.

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    |  6 ++++-
 .../gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c  | 23 +++++++++++++++----
 drivers/gpu/drm/amd/amdkfd/kfd_chardev.c      | 19 ++++++++++-----
 3 files changed, 37 insertions(+), 11 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h +5 -1
index 3ad8dc523b42..c60564ec4312 100644
@@ -86,6 +86,10 @@ struct kgd_mem {
 
 	bool aql_queue;
 	bool is_imported;
+
+	/* Used to track successful dmaunmap across retries in unmap ioctl */
+	uint32_t n_dmaunmap_success;
+	uint32_t n_dmaunmap_bos;
 };
 
 /* KFD Memory Eviction */
@@ -302,7 +306,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 +19 -4
index 54f31a420229..c431132d7cc1 100644
@@ -2102,21 +2102,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)
 {
 	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 >= mem->n_dmaunmap_bos) {
+			ret = amdgpu_bo_reserve(entry->bo_va->base.bo, false);
+			if (ret) {
+				mem->n_dmaunmap_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++;
+	}
+	mem->n_dmaunmap_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 +13 -6
index 06988cf1db51..66dee67ad859 100644
@@ -1366,7 +1366,7 @@ static int kfd_ioctl_unmap_memory_from_gpu(struct file *filep,
 {
 	struct kfd_ioctl_unmap_memory_from_gpu_args *args = data;
 	struct kfd_process_device *pdd, *peer_pdd;
-	void *mem;
+	struct kgd_mem *mem;
 	long err = 0;
 	uint32_t *devices_arr = NULL, i;
 	bool flush_tlb;
@@ -1400,7 +1400,7 @@ static int kfd_ioctl_unmap_memory_from_gpu(struct file *filep,
 		goto bind_process_to_device_failed;
 	}
 
-	mem = kfd_process_device_translate_handle(pdd,
+	mem = (struct kgd_mem *)kfd_process_device_translate_handle(pdd,
 						GET_IDR_HANDLE(args->handle));
 	if (!mem) {
 		err = -ENOMEM;
@@ -1414,7 +1414,7 @@ static int kfd_ioctl_unmap_memory_from_gpu(struct file *filep,
 			goto get_mem_obj_from_handle_failed;
 		}
 		err = amdgpu_amdkfd_gpuvm_unmap_memory_from_gpu(
-			peer_pdd->dev->adev, (struct kgd_mem *)mem, peer_pdd->drm_priv);
+			peer_pdd->dev->adev, mem, peer_pdd->drm_priv);
 		if (err) {
 			pr_err("Failed to unmap from gpu %d/%d\n",
 			       i, args->n_devices);
@@ -1426,7 +1426,7 @@ static int kfd_ioctl_unmap_memory_from_gpu(struct file *filep,
 	flush_tlb = kfd_flush_tlb_after_unmap(pdd->dev->kfd);
 	if (flush_tlb) {
 		err = amdgpu_amdkfd_gpuvm_sync_memory(pdd->dev->adev,
-				(struct kgd_mem *) mem, true);
+				mem, true);
 		if (err) {
 			pr_debug("Sync memory failed, wait interrupted by user signal\n");
 			goto sync_memory_failed;
@@ -1434,7 +1434,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 = mem->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,8 +1442,14 @@ 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, acquire interrupted by user signal\n");
+			goto dmaunmap_failed;
+		}
+		mem->n_dmaunmap_success = i+1;
 	}
+	mem->n_dmaunmap_success = 0;
 
 	mutex_unlock(&p->mutex);
 
@@ -1455,6 +1461,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 v3 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 7ec32b44df05..a5c054f8a427 100644
@@ -5783,9 +5783,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
#3 lore raw
In reply to David Francis
Quoted 11 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, two new numbers need to be tracked. 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.

I think what you mean here is "tracks the number of attachments on that device".

Quoted 53 lines

Track those values in struct kgd_mem.

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    |  6 ++++-
  .../gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c  | 23 +++++++++++++++----
  drivers/gpu/drm/amd/amdkfd/kfd_chardev.c      | 19 ++++++++++-----
  3 files changed, 37 insertions(+), 11 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h +5 -1
index 3ad8dc523b42..c60564ec4312 100644
@@ -86,6 +86,10 @@ struct kgd_mem {
 
  	bool aql_queue;
  	bool is_imported;
+
+	/* Used to track successful dmaunmap across retries in unmap ioctl */
+	uint32_t n_dmaunmap_success;
+	uint32_t n_dmaunmap_bos;
  };
 
  /* KFD Memory Eviction */
@@ -302,7 +306,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 +3 -1
index 54f31a420229..c431132d7cc1 100644
@@ -2102,21 +2102,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)
  {
  	struct kfd_mem_attachment *entry;
  	struct amdgpu_vm *vm;
+	int ret;
+	int i = 0;
 

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

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +2 -3
  	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 >= mem->n_dmaunmap_bos) {
+			ret = amdgpu_bo_reserve(entry->bo_va->base.bo, false);

This will lock and unlock things that aren't needed. This should be inside the "if (entry->bo_va->base.vm == vm)" where it's actually calling the dmaunmap_attachment.

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

Quoted 2 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +2 -0
+			if (ret) {
+				mem->n_dmaunmap_bos = i;

This counting approach feels a bit fragile. Also, what you're counting with "i" is not the number of attachments per device, but the number of attachments overall. So this double counting with two counters in two places is probably redundant and could be simplified to using just one counter and in one place.

But this may still have issue in corner cases where multiple unmap ioctls are happening concurrently. I'm not sure if this happens in practice, but it's something that a robust implementation needs to handle. The consequences of getting this wrong would be resource leaks or DMA mappings or potentially double-frees of dma mappings.

What would be simpler and more robust is, to have a flag in struct kfd_mem_attachment that indicates whether it is currently dma-mapped or not. If it's not mapped, you assume it was already unmapped and you don't unmap it again. That way you wouldn't need to count at all and it handles concurrent calls without problems. There is already an is_mapped flag there. You could add an is_dmamapped flag.

Regards,
  Felix

Quoted 90 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +12 -0
+				goto out;
+			}
+
+			if (entry->bo_va->base.vm == vm)
+				kfd_mem_dmaunmap_attachment(mem, entry);
 
+			amdgpu_bo_unreserve(entry->bo_va->base.bo);
+		}
+		i++;
+	}
+	mem->n_dmaunmap_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 +13 -6
index 06988cf1db51..66dee67ad859 100644
@@ -1366,7 +1366,7 @@ static int kfd_ioctl_unmap_memory_from_gpu(struct file *filep,
  {
  	struct kfd_ioctl_unmap_memory_from_gpu_args *args = data;
  	struct kfd_process_device *pdd, *peer_pdd;
-	void *mem;
+	struct kgd_mem *mem;
  	long err = 0;
  	uint32_t *devices_arr = NULL, i;
  	bool flush_tlb;
@@ -1400,7 +1400,7 @@ static int kfd_ioctl_unmap_memory_from_gpu(struct file *filep,
  		goto bind_process_to_device_failed;
  	}
 
-	mem = kfd_process_device_translate_handle(pdd,
+	mem = (struct kgd_mem *)kfd_process_device_translate_handle(pdd,
  						GET_IDR_HANDLE(args->handle));
  	if (!mem) {
  		err = -ENOMEM;
@@ -1414,7 +1414,7 @@ static int kfd_ioctl_unmap_memory_from_gpu(struct file *filep,
  			goto get_mem_obj_from_handle_failed;
  		}
  		err = amdgpu_amdkfd_gpuvm_unmap_memory_from_gpu(
-			peer_pdd->dev->adev, (struct kgd_mem *)mem, peer_pdd->drm_priv);
+			peer_pdd->dev->adev, mem, peer_pdd->drm_priv);
  		if (err) {
  			pr_err("Failed to unmap from gpu %d/%d\n",
  			       i, args->n_devices);
@@ -1426,7 +1426,7 @@ static int kfd_ioctl_unmap_memory_from_gpu(struct file *filep,
  	flush_tlb = kfd_flush_tlb_after_unmap(pdd->dev->kfd);
  	if (flush_tlb) {
  		err = amdgpu_amdkfd_gpuvm_sync_memory(pdd->dev->adev,
-				(struct kgd_mem *) mem, true);
+				mem, true);
  		if (err) {
  			pr_debug("Sync memory failed, wait interrupted by user signal\n");
  			goto sync_memory_failed;
@@ -1434,7 +1434,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 = mem->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,8 +1442,14 @@ 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, acquire interrupted by user signal\n");
+			goto dmaunmap_failed;
+		}
+		mem->n_dmaunmap_success = i+1;
  	}
+	mem->n_dmaunmap_success = 0;
 
  	mutex_unlock(&p->mutex);
 
@@ -1455,6 +1461,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);
maintainer Reviewed-by on v3 2/2
#2 lore raw

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

In reply to David Francis
Quoted 10 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.

This patch is

Quoted 40 lines
---
  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 7ec32b44df05..a5c054f8a427 100644
@@ -5783,9 +5783,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:
#4 lore raw
In reply to David Francis

Am 24.10.23 um 21:20 schrieb David Francis:

Quoted 2 lines

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

Well first of all the dma_resv object isn't related to TTM.

Quoted 74 lines

Acquire the locks in amdgpu_amdkfd_gpuvm_dmaunmap_mem.

Because the dmaunmap step can now fail, two new numbers need to be tracked. 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.

Track those values in struct kgd_mem.

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    |  6 ++++-
  .../gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c  | 23 +++++++++++++++----
  drivers/gpu/drm/amd/amdkfd/kfd_chardev.c      | 19 ++++++++++-----
  3 files changed, 37 insertions(+), 11 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h +5 -1
index 3ad8dc523b42..c60564ec4312 100644
@@ -86,6 +86,10 @@ struct kgd_mem {
 
  	bool aql_queue;
  	bool is_imported;
+
+	/* Used to track successful dmaunmap across retries in unmap ioctl */
+	uint32_t n_dmaunmap_success;
+	uint32_t n_dmaunmap_bos;
  };
 
  /* KFD Memory Eviction */
@@ -302,7 +306,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 +7 -4
index 54f31a420229..c431132d7cc1 100644
@@ -2102,21 +2102,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)
  {
  	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 >= mem->n_dmaunmap_bos) {
+			ret = amdgpu_bo_reserve(entry->bo_va->base.bo, false);
+			if (ret) {
+				mem->n_dmaunmap_bos = i;

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

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +9 -0
+				goto out;
+			}
+
+			if (entry->bo_va->base.vm == vm)
+				kfd_mem_dmaunmap_attachment(mem, entry);
 
+			amdgpu_bo_unreserve(entry->bo_va->base.bo);
+		}
+		i++;
+	}

WOW, big NAK to that!

You are essentially re-inventing the locking multiple BOs at the same time, please don't do that. Instead use dma_exec or ttm_exec_buf for that.

This also avoids all the fail handling.

Regards,
Christian.

Quoted 80 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +3 -0
+	mem->n_dmaunmap_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 +13 -6
index 06988cf1db51..66dee67ad859 100644
@@ -1366,7 +1366,7 @@ static int kfd_ioctl_unmap_memory_from_gpu(struct file *filep,
  {
  	struct kfd_ioctl_unmap_memory_from_gpu_args *args = data;
  	struct kfd_process_device *pdd, *peer_pdd;
-	void *mem;
+	struct kgd_mem *mem;
  	long err = 0;
  	uint32_t *devices_arr = NULL, i;
  	bool flush_tlb;
@@ -1400,7 +1400,7 @@ static int kfd_ioctl_unmap_memory_from_gpu(struct file *filep,
  		goto bind_process_to_device_failed;
  	}
 
-	mem = kfd_process_device_translate_handle(pdd,
+	mem = (struct kgd_mem *)kfd_process_device_translate_handle(pdd,
  						GET_IDR_HANDLE(args->handle));
  	if (!mem) {
  		err = -ENOMEM;
@@ -1414,7 +1414,7 @@ static int kfd_ioctl_unmap_memory_from_gpu(struct file *filep,
  			goto get_mem_obj_from_handle_failed;
  		}
  		err = amdgpu_amdkfd_gpuvm_unmap_memory_from_gpu(
-			peer_pdd->dev->adev, (struct kgd_mem *)mem, peer_pdd->drm_priv);
+			peer_pdd->dev->adev, mem, peer_pdd->drm_priv);
  		if (err) {
  			pr_err("Failed to unmap from gpu %d/%d\n",
  			       i, args->n_devices);
@@ -1426,7 +1426,7 @@ static int kfd_ioctl_unmap_memory_from_gpu(struct file *filep,
  	flush_tlb = kfd_flush_tlb_after_unmap(pdd->dev->kfd);
  	if (flush_tlb) {
  		err = amdgpu_amdkfd_gpuvm_sync_memory(pdd->dev->adev,
-				(struct kgd_mem *) mem, true);
+				mem, true);
  		if (err) {
  			pr_debug("Sync memory failed, wait interrupted by user signal\n");
  			goto sync_memory_failed;
@@ -1434,7 +1434,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 = mem->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,8 +1442,14 @@ 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, acquire interrupted by user signal\n");
+			goto dmaunmap_failed;
+		}
+		mem->n_dmaunmap_success = i+1;
  	}
+	mem->n_dmaunmap_success = 0;
 
  	mutex_unlock(&p->mutex);
 
@@ -1455,6 +1461,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);
maintainer
#5 lore raw
Quoted 91 lines · Christian König

Am 24.10.23 um 21:20 schrieb David Francis:

Quoted 2 lines

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

Well first of all the dma_resv object isn't related to TTM.

Quoted 72 lines

Acquire the locks in amdgpu_amdkfd_gpuvm_dmaunmap_mem.

Because the dmaunmap step can now fail, two new numbers need to be tracked. 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.

Track those values in struct kgd_mem.

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    |  6 ++++- .../gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c  | 23 +++++++++++++++---- drivers/gpu/drm/amd/amdkfd/kfd_chardev.c      | 19 ++++++++++-----

  3 files changed, 37 insertions(+), 11 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h +2 -0
index 3ad8dc523b42..c60564ec4312 100644
@@ -86,6 +86,10 @@ struct kgd_mem {
         bool aql_queue;
       bool is_imported;
+
+    /* Used to track successful dmaunmap across retries in unmap 

ioctl */
+    uint32_t n_dmaunmap_success;
+    uint32_t n_dmaunmap_bos;
  };
    /* KFD Memory Eviction */

drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h +0 -0
@@ -302,7 +306,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 +6 -3
index 54f31a420229..c431132d7cc1 100644
@@ -2102,21 +2102,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)
   {
       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 >= mem->n_dmaunmap_bos) {
+            ret = amdgpu_bo_reserve(entry->bo_va->base.bo, false);
+            if (ret) {

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

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +9 -0
+                mem->n_dmaunmap_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++;
+    }

WOW, big NAK to that!

Quoted 1 line

You are essentially re-inventing the locking multiple BOs at the same time, please don't do that. Instead use dma_exec or ttm_exec_buf for that.

I don't think that's the intention here. We're not locking multiple BOs at the same time. The purpose of all this counting is to safely handle ioctl restart for signal handling without dmaunmapping the same thing twice.

That said, I'm not a fan of this counting approach either and suggested a simpler way of doing this by adding a flag in the kfd_mem_attachment structure.

Regards,
  Felix

Quoted 90 lines

This also avoids all the fail handling.

Regards,
Christian.

Quoted 83 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +3 -0
+    mem->n_dmaunmap_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 +1 -1
index 06988cf1db51..66dee67ad859 100644
@@ -1366,7 +1366,7 @@ static int 
 kfd_ioctl_unmap_memory_from_gpu(struct file *filep,
   {
       struct kfd_ioctl_unmap_memory_from_gpu_args *args = data;
       struct kfd_process_device *pdd, *peer_pdd;
-    void *mem;
+    struct kgd_mem *mem;
       long err = 0;
       uint32_t *devices_arr = NULL, i;

      bool flush_tlb;

drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +1 -0
@@ -1400,7 +1400,7 @@ static int 
 kfd_ioctl_unmap_memory_from_gpu(struct file *filep,
           goto bind_process_to_device_failed;
       }
   -    mem = kfd_process_device_translate_handle(pdd,
+    mem = (struct kgd_mem *)kfd_process_device_translate_handle(pdd,
                           GET_IDR_HANDLE(args->handle));
       if (!mem) {

          err = -ENOMEM;

drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +0 -0
@@ -1414,7 +1414,7 @@ static int 
 kfd_ioctl_unmap_memory_from_gpu(struct file *filep,
               goto get_mem_obj_from_handle_failed;
           }

          err = amdgpu_amdkfd_gpuvm_unmap_memory_from_gpu( -            peer_pdd->dev->adev, (struct kgd_mem *)mem, peer_pdd->drm_priv);
+            peer_pdd->dev->adev, mem, peer_pdd->drm_priv); if (err) {
              pr_err("Failed to unmap from gpu %d/%d\n", i, args->n_devices);

drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +1 -1
@@ -1426,7 +1426,7 @@ static int 
 kfd_ioctl_unmap_memory_from_gpu(struct file *filep,
       flush_tlb = kfd_flush_tlb_after_unmap(pdd->dev->kfd);
       if (flush_tlb) {
           err = amdgpu_amdkfd_gpuvm_sync_memory(pdd->dev->adev,
-                (struct kgd_mem *) mem, true);
+                mem, true);
           if (err) {
               pr_debug("Sync memory failed, wait interrupted by user signal\n");

              goto sync_memory_failed;

drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +8 -2
@@ -1434,7 +1434,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 = mem->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,8 +1442,14 @@ 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, acquire interrupted by 
 user signal\n");
+            goto dmaunmap_failed;
+        }
+        mem->n_dmaunmap_success = i+1;
       }
+    mem->n_dmaunmap_success = 0;
         mutex_unlock(&p->mutex);

  @@ -1455,6 +1461,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);

#6 lore raw

Am 25.10.23 um 20:45 schrieb Felix Kuehling:

Quoted 93 lines · Christian König
Quoted 92 lines

Am 24.10.23 um 21:20 schrieb David Francis:

Quoted 2 lines

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

Well first of all the dma_resv object isn't related to TTM.

Quoted 73 lines

Acquire the locks in amdgpu_amdkfd_gpuvm_dmaunmap_mem.

Because the dmaunmap step can now fail, two new numbers need to be tracked. 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.

Track those values in struct kgd_mem.

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    |  6 ++++- .../gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c  | 23
+++++++++++++++----
  drivers/gpu/drm/amd/amdkfd/kfd_chardev.c      | 19 ++++++++++-----

  3 files changed, 37 insertions(+), 11 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h +2 -0
index 3ad8dc523b42..c60564ec4312 100644
@@ -86,6 +86,10 @@ struct kgd_mem {
         bool aql_queue;
       bool is_imported;
+
+    /* Used to track successful dmaunmap across retries in unmap 

ioctl */
+    uint32_t n_dmaunmap_success;
+    uint32_t n_dmaunmap_bos;
  };
    /* KFD Memory Eviction */

drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h +0 -0
@@ -302,7 +306,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 +6 -3
index 54f31a420229..c431132d7cc1 100644
@@ -2102,21 +2102,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)
   {
       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 >= mem->n_dmaunmap_bos) {
+            ret = amdgpu_bo_reserve(entry->bo_va->base.bo, false);
+            if (ret) {

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

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +9 -0
+                mem->n_dmaunmap_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++;
+    }

WOW, big NAK to that!

Quoted 3 lines
Quoted 1 line

You are essentially re-inventing the locking multiple BOs at the same time, please don't do that. Instead use dma_exec or ttm_exec_buf for that.

I don't think that's the intention here. We're not locking multiple BOs at the same time. The purpose of all this counting is to safely handle ioctl restart for signal handling without dmaunmapping the same thing twice.

Well, it's not mandatory to lock multiple BOs at the same time but it solves the problem that this shouldn't be interrupted.

Quoted 2 lines

That said, I'm not a fan of this counting approach either and suggested a simpler way of doing this by adding a flag in the kfd_mem_attachment structure.

When we look strictly at the rules for IOCTLs and restarting them then that counting approach is not really allowed either.

Kernel operations should either completely fail, fully complete or explicitly return how much they completed (e.g. how many bytes transferred for example). That we only partially complete and track that state inside the kernel is usually a no-go.

That said is it possible that userspace tries to do this operation twice? If yes what happens?

For this local problem the alternative to using drm_exec or ttm_exec_* would be to use non-interruptible waits#.

Regards,
Christian.

Quoted 95 lines

Regards,
  Felix

Quoted 90 lines

This also avoids all the fail handling.

Regards,
Christian.

Quoted 83 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +3 -0
+    mem->n_dmaunmap_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 +1 -1
index 06988cf1db51..66dee67ad859 100644
@@ -1366,7 +1366,7 @@ static int 
 kfd_ioctl_unmap_memory_from_gpu(struct file *filep,
   {
       struct kfd_ioctl_unmap_memory_from_gpu_args *args = data;
       struct kfd_process_device *pdd, *peer_pdd;
-    void *mem;
+    struct kgd_mem *mem;
       long err = 0;
       uint32_t *devices_arr = NULL, i;

      bool flush_tlb;

drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +1 -0
@@ -1400,7 +1400,7 @@ static int 
 kfd_ioctl_unmap_memory_from_gpu(struct file *filep,
           goto bind_process_to_device_failed;
       }
   -    mem = kfd_process_device_translate_handle(pdd,
+    mem = (struct kgd_mem *)kfd_process_device_translate_handle(pdd,
                           GET_IDR_HANDLE(args->handle));
       if (!mem) {

          err = -ENOMEM;

drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +0 -0
@@ -1414,7 +1414,7 @@ static int 
 kfd_ioctl_unmap_memory_from_gpu(struct file *filep,
               goto get_mem_obj_from_handle_failed;
           }

          err = amdgpu_amdkfd_gpuvm_unmap_memory_from_gpu( -            peer_pdd->dev->adev, (struct kgd_mem *)mem, peer_pdd->drm_priv);
+            peer_pdd->dev->adev, mem, peer_pdd->drm_priv); if (err) {
              pr_err("Failed to unmap from gpu %d/%d\n", i, args->n_devices);

drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +1 -1
@@ -1426,7 +1426,7 @@ static int 
 kfd_ioctl_unmap_memory_from_gpu(struct file *filep,
       flush_tlb = kfd_flush_tlb_after_unmap(pdd->dev->kfd);
       if (flush_tlb) {
           err = amdgpu_amdkfd_gpuvm_sync_memory(pdd->dev->adev,
-                (struct kgd_mem *) mem, true);
+                mem, true);
           if (err) {
               pr_debug("Sync memory failed, wait interrupted by user signal\n");

              goto sync_memory_failed;

drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +8 -2
@@ -1434,7 +1434,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 = mem->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,8 +1442,14 @@ 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, acquire interrupted by 
 user signal\n");
+            goto dmaunmap_failed;
+        }
+        mem->n_dmaunmap_success = i+1;
       }
+    mem->n_dmaunmap_success = 0;
         mutex_unlock(&p->mutex);

  @@ -1455,6 +1461,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);