Skip to contentSkip to conversation

drm/amdkfd: Wait vm update fence after retry fault recovered

PATCH In review Philip Yang 3 participants 13 messages Oct 10, 2023 – Oct 17, 2023

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

PATCH v2 1/7 drm/amdkfd: Wait vm update fence after retry fault recovered

If using sdma update GPU page table, kfd flush tlb does nothing if vm update fence callback doesn't update vm->tlb_seq. This works now because retry fault keep coming and will be handled to update page table again after AMDGPU_SVM_RANGE_RETRY_FAULT_PENDING timeout and flush tlb.

With the following patch to add bitmap_mapped flag, the retry fault recover will only update GPU page table once, need wait vm udate fence and then flush tlb.

No change if using CPU update GPU page table because no vm update fence.

Remove wait parameter in svm_range_validate_and_map because it is always called with true now.

---
 drivers/gpu/drm/amd/amdkfd/kfd_svm.c | 15 +++++++--------
 1 file changed, 7 insertions(+), 8 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +7 -8
index 2b4ccc0531e8..e195106a77cb 100644
@@ -1454,7 +1454,7 @@ svm_range_map_to_gpu(struct kfd_process_device *pdd, struct svm_range *prange,
 static int
 svm_range_map_to_gpus(struct svm_range *prange, unsigned long offset,
 		      unsigned long npages, bool readonly,
-		      unsigned long *bitmap, bool wait, bool flush_tlb)
+		      unsigned long *bitmap, bool flush_tlb)
 {
 	struct kfd_process_device *pdd;
 	struct amdgpu_device *bo_adev = NULL;
@@ -1487,8 +1487,7 @@ svm_range_map_to_gpus(struct svm_range *prange, unsigned long offset,
 
 		r = svm_range_map_to_gpu(pdd, prange, offset, npages, readonly,
 					 prange->dma_addr[gpuidx],
-					 bo_adev, wait ? &fence : NULL,
-					 flush_tlb);
+					 bo_adev, &fence, flush_tlb);
 		if (r)
 			break;
 
@@ -1612,7 +1611,7 @@ static void *kfd_svm_page_owner(struct kfd_process *p, int32_t gpuidx)
  */
 static int svm_range_validate_and_map(struct mm_struct *mm,
 				      struct svm_range *prange, int32_t gpuidx,
-				      bool intr, bool wait, bool flush_tlb)
+				      bool intr, bool flush_tlb)
 {
 	struct svm_validate_context *ctx;
 	unsigned long start, end, addr;
@@ -1741,7 +1740,7 @@ static int svm_range_validate_and_map(struct mm_struct *mm,
 
 		if (!r)
 			r = svm_range_map_to_gpus(prange, offset, npages, readonly,
-						  ctx->bitmap, wait, flush_tlb);
+						  ctx->bitmap, flush_tlb);
 
 		if (!r && next == end)
 			prange->mapped_to_gpu = true;
@@ -1848,7 +1847,7 @@ static void svm_range_restore_work(struct work_struct *work)
 		mutex_lock(&prange->migrate_mutex);
 
 		r = svm_range_validate_and_map(mm, prange, MAX_GPU_INSTANCE,
-					       false, true, false);
+					       false, false);
 		if (r)
 			pr_debug("failed %d to map 0x%lx to gpus\n", r,
 				 prange->start);
@@ -3094,7 +3093,7 @@ svm_range_restore_pages(struct amdgpu_device *adev, unsigned int pasid,
 		}
 	}
 
-	r = svm_range_validate_and_map(mm, prange, gpuidx, false, false, false);
+	r = svm_range_validate_and_map(mm, prange, gpuidx, false, false);
 	if (r)
 		pr_debug("failed %d to map svms 0x%p [0x%lx 0x%lx] to gpus\n",
 			 r, svms, prange->start, prange->last);
@@ -3643,7 +3642,7 @@ svm_range_set_attr(struct kfd_process *p, struct mm_struct *mm,
 		flush_tlb = !migrated && update_mapping && prange->mapped_to_gpu;
 
 		r = svm_range_validate_and_map(mm, prange, MAX_GPU_INSTANCE,
-					       true, true, flush_tlb);
+					       true, flush_tlb);
 		if (r)
 			pr_debug("failed %d to map svm range\n", r);
 
Signature · 1 line
2.35.1
#3 lore raw

PATCH v2 3/7 amd/amdkfd: Add granularity bitmap mapped to gpu flag

Replace prange->mapped_to_gpu with prange->bitmap_mapped[], which is per GPU flag and based on prange granularity, updated when map to GPUS or unmap from GPUs, to optimize multiple GPU map, unmap and retry fault recover.

svm_range_partial_mapped is false only if no part of the range mapping on any GPUs.

---
 drivers/gpu/drm/amd/amdkfd/kfd_svm.c | 256 +++++++++++++++++++++------
 drivers/gpu/drm/amd/amdkfd/kfd_svm.h |   8 +-
 2 files changed, 213 insertions(+), 51 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +206 -50
index fb22b857adbc..4e1af4b181ea 100644
@@ -296,12 +296,12 @@ static void svm_range_free(struct svm_range *prange, bool do_unmap)
 					KFD_IOC_ALLOC_MEM_FLAGS_USERPTR, 0);
 	}
 
-	/* free dma_addr array for each gpu */
+	/* free dma_addr array, bitmap_mapped for each gpu */
 	for (gpuidx = 0; gpuidx < MAX_GPU_INSTANCE; gpuidx++) {
-		if (prange->dma_addr[gpuidx]) {
+		if (prange->dma_addr[gpuidx])
 			kvfree(prange->dma_addr[gpuidx]);
-				prange->dma_addr[gpuidx] = NULL;
-		}
+		if (prange->bitmap_mapped[gpuidx])
+			bitmap_free(prange->bitmap_mapped[gpuidx]);
 	}
 
 	mutex_destroy(&prange->lock);
@@ -327,19 +327,38 @@ svm_range *svm_range_new(struct svm_range_list *svms, uint64_t start,
 	uint64_t size = last - start + 1;
 	struct svm_range *prange;
 	struct kfd_process *p;
-
-	prange = kzalloc(sizeof(*prange), GFP_KERNEL);
-	if (!prange)
-		return NULL;
+	unsigned int nbits;
+	uint32_t gpuidx;
 
 	p = container_of(svms, struct kfd_process, svms);
 	if (!p->xnack_enabled && update_mem_usage &&
 	    amdgpu_amdkfd_reserve_mem_limit(NULL, size << PAGE_SHIFT,
 				    KFD_IOC_ALLOC_MEM_FLAGS_USERPTR, 0)) {
 		pr_info("SVM mapping failed, exceeds resident system memory limit\n");
-		kfree(prange);
 		return NULL;
 	}
+
+	prange = kzalloc(sizeof(*prange), GFP_KERNEL);
+	if (!prange)
+		return NULL;
+
+	svm_range_set_default_attributes(&prange->preferred_loc,
+					 &prange->prefetch_loc,
+					 &prange->granularity, &prange->flags);
+
+	nbits = svm_range_mapped_nbits(start, last, prange->granularity);
+	pr_debug("prange 0x%p [0x%llx 0x%llx] bitmap_mapped nbits %d\n", prange,
+		 start, last, nbits);
+	for_each_set_bit(gpuidx, p->svms.bitmap_supported, p->n_pdds) {
+		prange->bitmap_mapped[gpuidx] = bitmap_zalloc(nbits, GFP_KERNEL);
+		if (!prange->bitmap_mapped[gpuidx]) {
+			while (gpuidx--)
+				bitmap_free(prange->bitmap_mapped[gpuidx]);
+			kfree(prange);
+			return NULL;
+		}
+	}
+
 	prange->npages = size;
 	prange->svms = svms;
 	prange->start = start;
@@ -359,10 +378,6 @@ svm_range *svm_range_new(struct svm_range_list *svms, uint64_t start,
 		bitmap_copy(prange->bitmap_access, svms->bitmap_supported,
 			    MAX_GPU_INSTANCE);
 
-	svm_range_set_default_attributes(&prange->preferred_loc,
-					 &prange->prefetch_loc,
-					 &prange->granularity, &prange->flags);
-
 	pr_debug("svms 0x%p [0x%llx 0x%llx]\n", svms, start, last);
 
 	return prange;
@@ -984,6 +999,47 @@ svm_range_split_nodes(struct svm_range *new, struct svm_range *old,
 	return 0;
 }
 
+static int
+svm_range_split_bitmap_mapped(struct svm_range *new, struct svm_range *old,
+			      uint64_t start, uint64_t last)
+{
+	struct kfd_process *p = container_of(new->svms, struct kfd_process, svms);
+	unsigned int nbits, old_nbits, old_nbits2;
+	unsigned long *bits;
+	uint32_t gpuidx;
+
+	nbits = svm_range_mapped_nbits(new->start, new->last, new->granularity);
+	old_nbits = svm_range_mapped_nbits(old->start, old->last, old->granularity);
+	old_nbits2 = svm_range_mapped_nbits(start, last, old->granularity);
+
+	pr_debug("old 0x%p [0x%lx 0x%lx] => [0x%llx 0x%llx] nbits %d => %d\n",
+		 old, old->start, old->last, start, last, old_nbits, old_nbits2);
+	pr_debug("new 0x%p [0x%lx 0x%lx] nbits %d\n", new, new->start, new->last,
+		 nbits);
+
+	for_each_set_bit(gpuidx, p->svms.bitmap_supported, p->n_pdds) {
+		bits = bitmap_alloc(old_nbits2, GFP_KERNEL);
+		if (!bits)
+			return -ENOMEM;
+
+		if (start == old->start) {
+			bitmap_shift_right(new->bitmap_mapped[gpuidx],
+					   old->bitmap_mapped[gpuidx],
+					   old_nbits2 - 1, old_nbits);
+			bitmap_copy(bits, old->bitmap_mapped[gpuidx], old_nbits2);
+		} else {
+			bitmap_copy(new->bitmap_mapped[gpuidx],
+				    old->bitmap_mapped[gpuidx], nbits);
+			bitmap_shift_right(bits, old->bitmap_mapped[gpuidx],
+					   nbits - 1, old_nbits);
+		}
+		bitmap_free(old->bitmap_mapped[gpuidx]);
+		old->bitmap_mapped[gpuidx] = bits;
+	}
+
+	return 0;
+}
+
 /**
  * svm_range_split_adjust - split range and adjust
  *
@@ -1024,6 +1080,10 @@ svm_range_split_adjust(struct svm_range *new, struct svm_range *old,
 			return r;
 	}
 
+	r = svm_range_split_bitmap_mapped(new, old, start, last);
+	if (r)
+		return r;
+
 	old->npages = last - start + 1;
 	old->start = start;
 	old->last = last;
@@ -1032,7 +1092,6 @@ svm_range_split_adjust(struct svm_range *new, struct svm_range *old,
 	new->prefetch_loc = old->prefetch_loc;
 	new->actual_loc = old->actual_loc;
 	new->granularity = old->granularity;
-	new->mapped_to_gpu = old->mapped_to_gpu;
 	bitmap_copy(new->bitmap_access, old->bitmap_access, MAX_GPU_INSTANCE);
 	bitmap_copy(new->bitmap_aip, old->bitmap_aip, MAX_GPU_INSTANCE);
 
@@ -1346,6 +1405,108 @@ svm_range_unmap_from_gpu(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 				      fence);
 }
 
+/**
+ * svm_range_partial_mapped_dev - check if prange mapped on the specific GPU
+ *
+ * @gpuidx: the GPU index to check
+ * @prange: prange to check
+ * @start: the start address in pages
+ * @last: the last address in pages
+ *
+ * Return:
+ * true: if any part of the range within [start, last] mapped on the GPU
+ * false: if the entire range [start, last] not mapped on the GPU
+ */
+static bool
+svm_range_partial_mapped_dev(uint32_t gpuidx, struct svm_range *prange,
+			     unsigned long start, unsigned long last)
+{
+	unsigned long start_index, last_index;
+
+	start = max(start, prange->start);
+	last = min(last, prange->last);
+	if (last < start)
+		return false;
+
+	start_index = (start - prange->start) >> prange->granularity;
+	last_index = (last - prange->start) >> prange->granularity;
+	return find_next_bit(prange->bitmap_mapped[gpuidx], last_index + 1,
+			     start_index) <= last_index;
+}
+
+/**
+ * svm_range_partial_mapped - check if prange mapped on any GPU
+ *
+ * @prange: prange to check
+ * @start: the start address in pages
+ * @last: the last address in pages
+ *
+ * Return:
+ * true: if any part of prange mapped on any GPU currently
+ * false: if the entire range is not mapped on any GPU
+ */
+static bool
+svm_range_partial_mapped(struct svm_range *prange, unsigned long start,
+			 unsigned long last)
+{
+	struct kfd_process *p = container_of(prange->svms, struct kfd_process, svms);
+	struct svm_range *pchild;
+	uint32_t gpuidx;
+
+	for_each_set_bit(gpuidx, p->svms.bitmap_supported, p->n_pdds) {
+		list_for_each_entry(pchild, &prange->child_list, child_list) {
+			if (svm_range_partial_mapped_dev(gpuidx, pchild, start, last))
+				return true;
+		}
+
+		if (svm_range_partial_mapped_dev(gpuidx, prange, start, last))
+			return true;
+	}
+	return false;
+}
+
+/**
+ * svm_range_complete_mapped - check if prange mapped on all GPUs completely
+ *
+ * @prange: prange to check
+ *
+ * Return:
+ * true: if the entire prange mapped completely on all GPUs that need access
+ * otherwise return false
+ */
+static bool svm_range_complete_mapped(struct svm_range *prange)
+{
+	int nbits = svm_range_mapped_nbits(prange->start, prange->last, prange->granularity);
+	DECLARE_BITMAP(bitmap, MAX_GPU_INSTANCE);
+	uint32_t gpuidx;
+	int r;
+
+	r = svm_range_need_access_gpus(bitmap, prange);
+	if (r)
+		return false;
+
+	for_each_set_bit(gpuidx, bitmap, MAX_GPU_INSTANCE)
+		if (!bitmap_full(prange->bitmap_mapped[gpuidx], nbits))
+			return false;
+	return true;
+}
+
+static void
+svm_range_update_mapped(uint32_t gpuidx, struct svm_range *prange,
+			unsigned long start, unsigned long last, bool mapped)
+{
+	unsigned long index, nbits;
+
+	index = (start - prange->start) >> prange->granularity;
+	nbits = svm_range_mapped_nbits(start, last, prange->granularity);
+	if (mapped)
+		bitmap_set(prange->bitmap_mapped[gpuidx], index, nbits);
+	else
+		bitmap_clear(prange->bitmap_mapped[gpuidx], index, nbits);
+	pr_debug("prange 0x%p [0x%lx 0x%lx] update mapped %d nbits %ld gpu %d\n",
+		 prange, start, last, mapped, nbits, gpuidx);
+}
+
 static int
 svm_range_unmap_from_gpus(struct svm_range *prange, unsigned long start,
 			  unsigned long last, uint32_t trigger)
@@ -1357,29 +1518,28 @@ svm_range_unmap_from_gpus(struct svm_range *prange, unsigned long start,
 	uint32_t gpuidx;
 	int r = 0;
 
-	if (!prange->mapped_to_gpu) {
-		pr_debug("prange 0x%p [0x%lx 0x%lx] not mapped to GPU\n",
-			 prange, prange->start, prange->last);
-		return 0;
-	}
-
-	if (prange->start == start && prange->last == last) {
-		pr_debug("unmap svms 0x%p prange 0x%p\n", prange->svms, prange);
-		prange->mapped_to_gpu = false;
-	}
-
 	bitmap_or(bitmap, prange->bitmap_access, prange->bitmap_aip,
 		  MAX_GPU_INSTANCE);
 	p = container_of(prange->svms, struct kfd_process, svms);
 
 	for_each_set_bit(gpuidx, bitmap, MAX_GPU_INSTANCE) {
-		pr_debug("unmap from gpu idx 0x%x\n", gpuidx);
 		pdd = kfd_process_device_from_gpuidx(p, gpuidx);
 		if (!pdd) {
 			pr_debug("failed to find device idx %d\n", gpuidx);
-			return -EINVAL;
+			continue;
 		}
 
+		if (!svm_range_partial_mapped_dev(gpuidx, prange, start, last)) {
+			pr_debug("svms 0x%p prange 0x%p [0x%lx 0x%lx] not mapped on gpu %d\n",
+				 prange->svms, prange, start, last, gpuidx);
+			continue;
+		}
+
+		svm_range_update_mapped(gpuidx, prange, start, last, false);
+
+		pr_debug("unmap svms 0x%p prange 0x%p [0x%lx 0x%lx] from gpu %d\n",
+			 prange->svms, prange, start, last, gpuidx);
+
 		kfd_smi_event_unmap_from_gpu(pdd->dev, p->lead_thread->pid,
 					     start, last, trigger);
 
@@ -1519,6 +1679,10 @@ svm_range_map_to_gpus(struct svm_range *prange, unsigned long offset,
 		if (r)
 			break;
 
+		if (!r)
+			svm_range_update_mapped(gpuidx, prange, prange->start + offset,
+						prange->start + offset + npages - 1, true);
+
 		if (fence) {
 			r = dma_fence_wait(fence, false);
 			dma_fence_put(fence);
@@ -1670,7 +1834,7 @@ static int svm_range_validate_and_map(struct mm_struct *mm,
 
 	if (bitmap_empty(ctx->bitmap, MAX_GPU_INSTANCE)) {
 		bitmap_copy(ctx->bitmap, prange->bitmap_access, MAX_GPU_INSTANCE);
-		if (!prange->mapped_to_gpu ||
+		if (!svm_range_partial_mapped(prange, prange->start, prange->last) ||
 		    bitmap_empty(ctx->bitmap, MAX_GPU_INSTANCE)) {
 			r = 0;
 			goto free_ctx;
@@ -1755,9 +1919,6 @@ static int svm_range_validate_and_map(struct mm_struct *mm,
 			r = svm_range_map_to_gpus(prange, offset, npages, readonly,
 						  ctx->bitmap, flush_tlb);
 
-		if (!r && next == end)
-			prange->mapped_to_gpu = true;
-
 		svm_range_unlock(prange);
 
 		addr = next;
@@ -1939,22 +2100,8 @@ svm_range_evict(struct svm_range *prange, struct mm_struct *mm,
 	if (!p->xnack_enabled ||
 	    (prange->flags & KFD_IOCTL_SVM_FLAG_GPU_ALWAYS_MAPPED)) {
 		int evicted_ranges;
-		bool mapped = prange->mapped_to_gpu;
-
-		list_for_each_entry(pchild, &prange->child_list, child_list) {
-			if (!pchild->mapped_to_gpu)
-				continue;
-			mapped = true;
-			mutex_lock_nested(&pchild->lock, 1);
-			if (pchild->start <= last && pchild->last >= start) {
-				pr_debug("increment pchild invalid [0x%lx 0x%lx]\n",
-					 pchild->start, pchild->last);
-				atomic_inc(&pchild->invalid);
-			}
-			mutex_unlock(&pchild->lock);
-		}
 
-		if (!mapped)
+		if (!svm_range_partial_mapped(prange, start, last))
 			return r;
 
 		if (prange->start <= last && prange->last >= start)
@@ -2005,7 +2152,10 @@ svm_range_evict(struct svm_range *prange, struct mm_struct *mm,
 
 static struct svm_range *svm_range_clone(struct svm_range *old)
 {
+	struct kfd_process *p = container_of(old->svms, struct kfd_process, svms);
 	struct svm_range *new;
+	uint32_t gpuidx;
+	int nbits;
 
 	new = svm_range_new(old->svms, old->start, old->last, false);
 	if (!new)
@@ -2027,8 +2177,13 @@ static struct svm_range *svm_range_clone(struct svm_range *old)
 	new->prefetch_loc = old->prefetch_loc;
 	new->actual_loc = old->actual_loc;
 	new->granularity = old->granularity;
-	new->mapped_to_gpu = old->mapped_to_gpu;
 	new->vram_pages = old->vram_pages;
+	nbits = svm_range_mapped_nbits(new->start, new->last, new->granularity);
+	for_each_set_bit(gpuidx, p->svms.bitmap_supported, p->n_pdds) {
+		bitmap_copy(new->bitmap_mapped[gpuidx],
+			    old->bitmap_mapped[gpuidx],
+			    nbits);
+	};
 	bitmap_copy(new->bitmap_access, old->bitmap_access, MAX_GPU_INSTANCE);
 	bitmap_copy(new->bitmap_aip, old->bitmap_aip, MAX_GPU_INSTANCE);
 
@@ -2147,7 +2302,7 @@ svm_range_add(struct kfd_process *p, uint64_t start, uint64_t size,
 		next_start = min(node->last, last) + 1;
 
 		if (svm_range_is_same_attrs(p, prange, nattr, attrs) &&
-		    prange->mapped_to_gpu) {
+		    svm_range_complete_mapped(prange)) {
 			/* nothing to do */
 		} else if (node->start < start || node->last > last) {
 			/* node intersects the update range and its attributes
@@ -3641,7 +3796,7 @@ svm_range_set_attr(struct kfd_process *p, struct mm_struct *mm,
 
 		if (migrated && (!p->xnack_enabled ||
 		    (prange->flags & KFD_IOCTL_SVM_FLAG_GPU_ALWAYS_MAPPED)) &&
-		    prange->mapped_to_gpu) {
+		    svm_range_partial_mapped(prange, prange->start, prange->last)) {
 			pr_debug("restore_work will update mappings of GPUs\n");
 			mutex_unlock(&prange->migrate_mutex);
 			continue;
@@ -3652,7 +3807,8 @@ svm_range_set_attr(struct kfd_process *p, struct mm_struct *mm,
 			continue;
 		}
 
-		flush_tlb = !migrated && update_mapping && prange->mapped_to_gpu;
+		flush_tlb = !migrated && update_mapping &&
+			    svm_range_partial_mapped(prange, prange->start, prange->last);
 
 		r = svm_range_validate_and_map(mm, prange, MAX_GPU_INSTANCE,
 					       true, flush_tlb);
drivers/gpu/drm/amd/amdkfd/kfd_svm.h +7 -1
index bf2fde008115..7e165854bc0e 100644
@@ -136,7 +136,7 @@ struct svm_range {
 	struct list_head		child_list;
 	DECLARE_BITMAP(bitmap_access, MAX_GPU_INSTANCE);
 	DECLARE_BITMAP(bitmap_aip, MAX_GPU_INSTANCE);
-	bool				mapped_to_gpu;
+	unsigned long			*bitmap_mapped[MAX_GPU_INSTANCE];
 };
 
 static inline void svm_range_lock(struct svm_range *prange)
@@ -167,6 +167,12 @@ static inline struct svm_range_bo *svm_range_bo_ref(struct svm_range_bo *svm_bo)
 		return NULL;
 }
 
+static inline int svm_range_mapped_nbits(uint64_t start, uint64_t last,
+					 uint8_t granularity)
+{
+	return (last >> granularity) - (start >> granularity) + 1;
+}
+
 int svm_range_list_init(struct kfd_process *p);
 void svm_range_list_fini(struct kfd_process *p);
 int svm_ioctl(struct kfd_process *p, enum kfd_ioctl_svm_op op, uint64_t start,
Signature · 1 line
2.35.1
#9 lore raw

PATCH v2 6/7 drm/amdkfd: Unmap from cpu split the range and bitmap_mapped

In reply to Philip Yang

Unmap the svm range from cpu will unmap and remove ranges, this cannot align the start and last address to range granularity. If we unmap from GPUs first, the bitmap_mapped flag is updated, split may get incorrect bitmap_mapped for the remaining ranges.

We should split the range and bitmap_mapped first, then unmap the range from GPUs.

---
 drivers/gpu/drm/amd/amdkfd/kfd_svm.c | 26 +++++++++++++++++---------
 1 file changed, 17 insertions(+), 9 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +17 -9
index 0ee5633c8972..e1392b277399 100644
@@ -2672,24 +2672,32 @@ svm_range_unmap_from_cpu(struct mm_struct *mm, struct svm_range *prange,
 
 	list_for_each_entry(pchild, &prange->child_list, child_list) {
 		mutex_lock_nested(&pchild->lock, 1);
-		s = max(start, pchild->start);
-		l = min(last, pchild->last);
-		if (l >= s)
-			svm_range_unmap_from_gpus(pchild, s, l, trigger);
 		svm_range_unmap_split(mm, prange, pchild, start, last);
 		mutex_unlock(&pchild->lock);
 	}
-	s = max(start, prange->start);
-	l = min(last, prange->last);
-	if (l >= s)
-		svm_range_unmap_from_gpus(prange, s, l, trigger);
 	svm_range_unmap_split(mm, prange, prange, start, last);
-
 	if (unmap_parent)
 		svm_range_add_list_work(svms, prange, mm, SVM_OP_UNMAP_RANGE);
 	else
 		svm_range_add_list_work(svms, prange, mm,
 					SVM_OP_UPDATE_RANGE_NOTIFIER);
+
+	list_for_each_entry(pchild, &prange->child_list, child_list) {
+		if (pchild->work_item.op != SVM_OP_UNMAP_RANGE)
+			continue;
+
+		s = max(start, pchild->start);
+		l = min(last, pchild->last);
+		if (l >= s)
+			svm_range_unmap_from_gpus(pchild, s, l, trigger);
+	}
+	if (prange->work_item.op == SVM_OP_UNMAP_RANGE) {
+		s = max(start, prange->start);
+		l = min(last, prange->last);
+		if (l >= s)
+			svm_range_unmap_from_gpus(prange, s, l, trigger);
+	}
+
 	schedule_deferred_list_work(svms);
 
 	kfd_unref_process(p);
Signature · 1 line
2.35.1
#10 lore raw

PATCH v2 7/7 drm/amdkfd: Changing range granularity update bitmap_mapped

In reply to Philip Yang

When changing the svm range granularity, update the svm range bitmap_mapped accordingly.

---
 drivers/gpu/drm/amd/amdkfd/kfd_svm.c | 49 +++++++++++++++++++++++++++-
 1 file changed, 48 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +48 -1
index e1392b277399..55b806048925 100644
@@ -739,6 +739,53 @@ svm_range_check_attr(struct kfd_process *p,
 	return 0;
 }
 
+static void
+svm_range_change_granularity(struct svm_range *prange, uint8_t value)
+{
+	struct kfd_process *p = container_of(prange->svms, struct kfd_process, svms);
+	unsigned int nbits_new, nbits_old, i, n;
+	unsigned long *bits_new, *bits_old;
+	int gpuidx;
+
+	if (prange->granularity == value)
+		return;
+
+	nbits_old = svm_range_mapped_nbits(prange->start, prange->last, prange->granularity);
+	nbits_new = svm_range_mapped_nbits(prange->start, prange->last, value);
+	if (nbits_new > nbits_old) {
+		n = nbits_new / nbits_old;
+		if (nbits_new % nbits_old)
+			n++;
+	} else {
+		n = nbits_old / nbits_new;
+		if (nbits_old % nbits_new)
+			n++;
+	}
+
+	pr_debug("prange 0x%p [0x%lx 0x%lx] bitmap_mapped nbits %d -> %d\n",
+		 prange, prange->start, prange->last, nbits_old, nbits_new);
+
+	for_each_set_bit(gpuidx, p->svms.bitmap_supported, p->n_pdds) {
+		bits_old = prange->bitmap_mapped[gpuidx];
+		if (bitmap_empty(bits_old, nbits_old))
+			continue;
+
+		bits_new = bitmap_zalloc(nbits_new, GFP_KERNEL);
+		if (!bits_new)
+			return;
+
+		for_each_set_bit(i, bits_old, nbits_old) {
+			if (nbits_new > nbits_old)
+				bitmap_set(bits_new, i * n, n);
+			else
+				bitmap_set(bits_new, i / n, 1);
+		}
+		bitmap_free(bits_old);
+		prange->bitmap_mapped[gpuidx] = bits_new;
+	}
+	prange->granularity = value;
+}
+
 static void
 svm_range_apply_attrs(struct kfd_process *p, struct svm_range *prange,
 		      uint32_t nattr, struct kfd_ioctl_svm_attribute *attrs,
@@ -783,7 +830,7 @@ svm_range_apply_attrs(struct kfd_process *p, struct svm_range *prange,
 			prange->flags &= ~attrs[i].value;
 			break;
 		case KFD_IOCTL_SVM_ATTR_GRANULARITY:
-			prange->granularity = attrs[i].value;
+			svm_range_change_granularity(prange, attrs[i].value);
 			break;
 		default:
 			WARN_ONCE(1, "svm_range_check_attrs wasn't called?");
Signature · 1 line
2.35.1
#7 lore raw

PATCH v2 5/7 drm/amdkfd: Check bitmap_mapped flag to skip retry fault

In reply to Philip Yang

Remove prange validate_timestamp which is not accurate for multiple GPUs.

Use the bitmap_mapped flag to skip the retry fault from different pages of the same range if the range is already mapped on the specific GPU.

---
 drivers/gpu/drm/amd/amdkfd/kfd_svm.c | 24 ++++++++----------------
 drivers/gpu/drm/amd/amdkfd/kfd_svm.h |  1 -
 2 files changed, 8 insertions(+), 17 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +8 -16
index daa996d7039d..0ee5633c8972 100644
@@ -43,10 +43,6 @@
 
 #define AMDGPU_SVM_RANGE_RESTORE_DELAY_MS 1
 
-/* Long enough to ensure no retry fault comes after svm range is restored and
- * page table is updated.
- */
-#define AMDGPU_SVM_RANGE_RETRY_FAULT_PENDING	(2UL * NSEC_PER_MSEC)
 #if IS_ENABLED(CONFIG_DYNAMIC_DEBUG)
 #define dynamic_svm_range_dump(svms) \
 	_dynamic_func_call_no_desc("svm_range_dump", svm_range_debug_dump, svms)
@@ -369,7 +365,6 @@ svm_range *svm_range_new(struct svm_range_list *svms, uint64_t start,
 	INIT_LIST_HEAD(&prange->deferred_list);
 	INIT_LIST_HEAD(&prange->child_list);
 	atomic_set(&prange->invalid, 0);
-	prange->validate_timestamp = 0;
 	prange->vram_pages = 0;
 	mutex_init(&prange->migrate_mutex);
 	mutex_init(&prange->lock);
@@ -1938,8 +1933,6 @@ static int svm_range_validate_and_map(struct mm_struct *mm,
 	}
 
 	svm_range_unreserve_bos(ctx);
-	if (!r)
-		prange->validate_timestamp = ktime_get_boottime();
 
 free_ctx:
 	kfree(ctx);
@@ -3214,15 +3207,6 @@ svm_range_restore_pages(struct amdgpu_device *adev, unsigned int pasid,
 		goto out_unlock_range;
 	}
 
-	/* skip duplicate vm fault on different pages of same range */
-	if (ktime_before(timestamp, ktime_add_ns(prange->validate_timestamp,
-				AMDGPU_SVM_RANGE_RETRY_FAULT_PENDING))) {
-		pr_debug("svms 0x%p [0x%lx %lx] already restored\n",
-			 svms, prange->start, prange->last);
-		r = 0;
-		goto out_unlock_range;
-	}
-
 	/* __do_munmap removed VMA, return success as we are handling stale
 	 * retry fault.
 	 */
@@ -3248,6 +3232,14 @@ svm_range_restore_pages(struct amdgpu_device *adev, unsigned int pasid,
 		goto out_unlock_range;
 	}
 
+	/* skip duplicate vm fault on different pages of same granularity range */
+	if (svm_range_partial_mapped_dev(gpuidx, prange, addr, addr)) {
+		pr_debug("svms 0x%p [0x%lx %lx] addr 0x%llx already mapped on gpu %d\n",
+			 svms, prange->start, prange->last, addr, gpuidx);
+		r = 0;
+		goto out_unlock_range;
+	}
+
 	pr_debug("svms %p [0x%lx 0x%lx] best restore 0x%x, actual loc 0x%x\n",
 		 svms, prange->start, prange->last, best_loc,
 		 prange->actual_loc);
drivers/gpu/drm/amd/amdkfd/kfd_svm.h +0 -1
index 7e165854bc0e..7a12be42cf16 100644
@@ -129,7 +129,6 @@ struct svm_range {
 	uint32_t			actual_loc;
 	uint8_t				granularity;
 	atomic_t			invalid;
-	ktime_t				validate_timestamp;
 	struct mmu_interval_notifier	notifier;
 	struct svm_work_list_item	work_item;
 	struct list_head		deferred_list;
Signature · 1 line
2.35.1
#6 lore raw

PATCH v2 4/7 amd/amdkfd: Unmap range from GPUs based on granularity

In reply to Philip Yang

When migrating the range to unmap the range from GPUs, align unmap start and last address to the range granularity boundary. Skip unmap from GPU if range is already unmapped from GPUs based on bitmap_mapped flag.

This optimizes the TLB flush and also solve the rocgdb CWSR migration related issue.

---
 drivers/gpu/drm/amd/amdkfd/kfd_svm.c | 35 ++++++++++++++++++++++++----
 1 file changed, 31 insertions(+), 4 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +31 -4
index 4e1af4b181ea..daa996d7039d 100644
@@ -2066,6 +2066,26 @@ static void svm_range_restore_work(struct work_struct *work)
 	mmput(mm);
 }
 
+static unsigned long
+svm_range_align_start(struct svm_range *prange, unsigned long start)
+{
+	unsigned long start_align;
+
+	start_align = ALIGN_DOWN(start, 1UL << prange->granularity);
+	start_align = max_t(unsigned long, start_align, prange->start);
+	return start_align;
+}
+
+static unsigned long
+svm_range_align_last(struct svm_range *prange, unsigned long last)
+{
+	unsigned long last_align;
+
+	last_align = ALIGN(last + 1, 1UL << prange->granularity) - 1;
+	last_align = min_t(unsigned long, last_align, prange->last);
+	return last_align;
+}
+
 /**
  * svm_range_evict - evict svm range
  * @prange: svm range structure
@@ -2126,6 +2146,12 @@ svm_range_evict(struct svm_range *prange, struct mm_struct *mm,
 		unsigned long s, l;
 		uint32_t trigger;
 
+		if (!svm_range_partial_mapped(prange, start, last)) {
+			pr_debug("svms 0x%p [0x%lx 0x%lx] unmapped already\n",
+				prange->svms, start, last);
+			return 0;
+		}
+
 		if (event == MMU_NOTIFY_MIGRATE)
 			trigger = KFD_SVM_UNMAP_TRIGGER_MMU_NOTIFY_MIGRATE;
 		else
@@ -2133,16 +2159,17 @@ svm_range_evict(struct svm_range *prange, struct mm_struct *mm,
 
 		pr_debug("invalidate unmap svms 0x%p [0x%lx 0x%lx] from GPUs\n",
 			 prange->svms, start, last);
+
 		list_for_each_entry(pchild, &prange->child_list, child_list) {
 			mutex_lock_nested(&pchild->lock, 1);
-			s = max(start, pchild->start);
-			l = min(last, pchild->last);
+			s = svm_range_align_start(pchild, start);
+			l = svm_range_align_last(pchild, last);
 			if (l >= s)
 				svm_range_unmap_from_gpus(pchild, s, l, trigger);
 			mutex_unlock(&pchild->lock);
 		}
-		s = max(start, prange->start);
-		l = min(last, prange->last);
+		s = svm_range_align_start(prange, start);
+		l = svm_range_align_last(prange, last);
 		if (l >= s)
 			svm_range_unmap_from_gpus(prange, s, l, trigger);
 	}
Signature · 1 line
2.35.1
#1 lore raw

PATCH v2 2/7 drm/amdkfd: Add helper function svm_range_need_access_gpus

In reply to Philip Yang

Refactor svm_range_validate_and_map to add and use the helper function to get all GPUs bitmap that need access the svm range. No functional change. This helper will be used in the following patch.

---
 drivers/gpu/drm/amd/amdkfd/kfd_svm.c | 55 +++++++++++++++++-----------
 1 file changed, 34 insertions(+), 21 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +34 -21
index e195106a77cb..fb22b857adbc 100644
@@ -1191,6 +1191,34 @@ svm_range_split_by_granularity(struct kfd_process *p, struct mm_struct *mm,
 	}
 	return 0;
 }
+
+static int
+svm_range_need_access_gpus(unsigned long *bitmap, struct svm_range *prange)
+{
+	struct kfd_process *p = container_of(prange->svms, struct kfd_process, svms);
+	uint32_t gpuidx;
+
+	if (p->xnack_enabled) {
+		bitmap_copy(bitmap, prange->bitmap_aip, MAX_GPU_INSTANCE);
+
+		/* If prefetch range to GPU, or GPU retry fault migrate range to
+		 * GPU, which has ACCESS attribute to the range, create mapping
+		 * on that GPU.
+		 */
+		if (prange->actual_loc) {
+			gpuidx = kfd_process_gpuidx_from_gpuid(p, prange->actual_loc);
+			if (gpuidx < 0)
+				return -EINVAL;
+			if (test_bit(gpuidx, prange->bitmap_access))
+				bitmap_set(bitmap, gpuidx, 1);
+		}
+	} else {
+		bitmap_or(bitmap, prange->bitmap_access, prange->bitmap_aip,
+			  MAX_GPU_INSTANCE);
+	}
+	return 0;
+}
+
 static bool
 svm_nodes_in_same_hive(struct kfd_node *node_a, struct kfd_node *node_b)
 {
@@ -1631,28 +1659,13 @@ static int svm_range_validate_and_map(struct mm_struct *mm,
 	if (gpuidx < MAX_GPU_INSTANCE) {
 		bitmap_zero(ctx->bitmap, MAX_GPU_INSTANCE);
 		bitmap_set(ctx->bitmap, gpuidx, 1);
-	} else if (ctx->process->xnack_enabled) {
-		bitmap_copy(ctx->bitmap, prange->bitmap_aip, MAX_GPU_INSTANCE);
-
-		/* If prefetch range to GPU, or GPU retry fault migrate range to
-		 * GPU, which has ACCESS attribute to the range, create mapping
-		 * on that GPU.
-		 */
-		if (prange->actual_loc) {
-			gpuidx = kfd_process_gpuidx_from_gpuid(ctx->process,
-							prange->actual_loc);
-			if (gpuidx < 0) {
-				WARN_ONCE(1, "failed get device by id 0x%x\n",
-					 prange->actual_loc);
-				r = -EINVAL;
-				goto free_ctx;
-			}
-			if (test_bit(gpuidx, prange->bitmap_access))
-				bitmap_set(ctx->bitmap, gpuidx, 1);
-		}
 	} else {
-		bitmap_or(ctx->bitmap, prange->bitmap_access,
-			  prange->bitmap_aip, MAX_GPU_INSTANCE);
+		r = svm_range_need_access_gpus(ctx->bitmap, prange);
+		if (r) {
+			WARN_ONCE(1, "failed get device by id 0x%x\n",
+				 prange->actual_loc);
+			goto free_ctx;
+		}
 	}
 
 	if (bitmap_empty(ctx->bitmap, MAX_GPU_INSTANCE)) {
Signature · 1 line
2.35.1
#11 lore raw
In reply to Philip Yang
Quoted 8 lines · Philip Yang

Caution: This message originated from an External Source. Use proper caution when opening attachments, clicking links, or responding.

If using sdma update GPU page table, kfd flush tlb does nothing if vm update fence callback doesn't update vm->tlb_seq. This works now because retry fault keep coming and will be handled to update page table again after AMDGPU_SVM_RANGE_RETRY_FAULT_PENDING timeout and flush tlb.

Quoted 8 lines

With the following patch to add bitmap_mapped flag, the retry fault recover will only update GPU page table once, need wait vm udate fence and then flush tlb.

No change if using CPU update GPU page table because no vm update fence.

Remove wait parameter in svm_range_validate_and_map because it is always called with true now.

I think it is better to add sdma at this patch title, like " Wait sdma vm update fence after retry fault recovered" as this patch only applies to using sdma to update page table in page fault recovery. It can improve performance in this case.

Waiting fence would delay page fault recovery process, we may need to look how it affects IH sw ring overflow in future.

Regards

Xiaogang

Quoted 77 lines
---
  drivers/gpu/drm/amd/amdkfd/kfd_svm.c | 15 +++++++--------
  1 file changed, 7 insertions(+), 8 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +7 -9
index 2b4ccc0531e8..e195106a77cb 100644
@@ -1454,7 +1454,7 @@ svm_range_map_to_gpu(struct kfd_process_device *pdd, struct svm_range *prange,
  static int
  svm_range_map_to_gpus(struct svm_range *prange, unsigned long offset,
                       unsigned long npages, bool readonly,
-                     unsigned long *bitmap, bool wait, bool flush_tlb)
+                     unsigned long *bitmap, bool flush_tlb)
  {
         struct kfd_process_device *pdd;
         struct amdgpu_device *bo_adev = NULL;
@@ -1487,8 +1487,7 @@ svm_range_map_to_gpus(struct svm_range *prange, unsigned long offset,
 
                 r = svm_range_map_to_gpu(pdd, prange, offset, npages, readonly,
                                          prange->dma_addr[gpuidx],
-                                        bo_adev, wait ? &fence : NULL,
-                                        flush_tlb);
+                                        bo_adev, &fence, flush_tlb);
                 if (r)
                         break;
 
@@ -1612,7 +1611,7 @@ static void *kfd_svm_page_owner(struct kfd_process *p, int32_t gpuidx)
   */
  static int svm_range_validate_and_map(struct mm_struct *mm,
                                       struct svm_range *prange, int32_t gpuidx,
-                                     bool intr, bool wait, bool flush_tlb)
+                                     bool intr, bool flush_tlb)
  {
         struct svm_validate_context *ctx;
         unsigned long start, end, addr;
@@ -1741,7 +1740,7 @@ static int svm_range_validate_and_map(struct mm_struct *mm,
 
                 if (!r)
                         r = svm_range_map_to_gpus(prange, offset, npages, readonly,
-                                                 ctx->bitmap, wait, flush_tlb);
+                                                 ctx->bitmap, flush_tlb);
 
                 if (!r && next == end)
                         prange->mapped_to_gpu = true;
@@ -1848,7 +1847,7 @@ static void svm_range_restore_work(struct work_struct *work)
                 mutex_lock(&prange->migrate_mutex);
 
                 r = svm_range_validate_and_map(mm, prange, MAX_GPU_INSTANCE,
-                                              false, true, false);
+                                              false, false);
                 if (r)
                         pr_debug("failed %d to map 0x%lx to gpus\n", r,
                                  prange->start);
@@ -3094,7 +3093,7 @@ svm_range_restore_pages(struct amdgpu_device *adev, unsigned int pasid,
                 }
         }
 
-       r = svm_range_validate_and_map(mm, prange, gpuidx, false, false, false);
+       r = svm_range_validate_and_map(mm, prange, gpuidx, false, false);
         if (r)
                 pr_debug("failed %d to map svms 0x%p [0x%lx 0x%lx] to gpus\n",
                          r, svms, prange->start, prange->last);
@@ -3643,7 +3642,7 @@ svm_range_set_attr(struct kfd_process *p, struct mm_struct *mm,
                 flush_tlb = !migrated && update_mapping && prange->mapped_to_gpu;
 
                 r = svm_range_validate_and_map(mm, prange, MAX_GPU_INSTANCE,
-                                              true, true, flush_tlb);
+                                              true, flush_tlb);
                 if (r)
                         pr_debug("failed %d to map svm range\n", r);
 
--

2.35.1

#4 lore raw

Re: [PATCH v2 3/7] amd/amdkfd: Add granularity bitmap mapped to gpu flag

In reply to Philip Yang
Quoted 22 lines · Philip Yang

Caution: This message originated from an External Source. Use proper caution when opening attachments, clicking links, or responding.

Replace prange->mapped_to_gpu with prange->bitmap_mapped[], which is per GPU flag and based on prange granularity, updated when map to GPUS or unmap from GPUs, to optimize multiple GPU map, unmap and retry fault recover.

svm_range_partial_mapped is false only if no part of the range mapping on any GPUs.

---
  drivers/gpu/drm/amd/amdkfd/kfd_svm.c | 256 +++++++++++++++++++++------
  drivers/gpu/drm/amd/amdkfd/kfd_svm.h |   8 +-
  2 files changed, 213 insertions(+), 51 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +0 -0
index fb22b857adbc..4e1af4b181ea 100644

drivers/gpu/drm/amd/amdkfd/kfd_svm.c static void svm_range_free(struct svm_range *prange, bool do_unmap) · line 303

Quoted 10 lines
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +2 -3
@@ -296,12 +296,12 @@ static void svm_range_free(struct svm_range *prange, bool do_unmap)
                                         KFD_IOC_ALLOC_MEM_FLAGS_USERPTR, 0);
         }
 
-       /* free dma_addr array for each gpu */
+       /* free dma_addr array, bitmap_mapped for each gpu */
         for (gpuidx = 0; gpuidx < MAX_GPU_INSTANCE; gpuidx++) {
-               if (prange->dma_addr[gpuidx]) {
+               if (prange->dma_addr[gpuidx])
                         kvfree(prange->dma_addr[gpuidx]);
-                               prange->dma_addr[gpuidx] = NULL;

I do not know why remove this line.

Quoted 149 lines
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +88 -11
-               }
+               if (prange->bitmap_mapped[gpuidx])
+                       bitmap_free(prange->bitmap_mapped[gpuidx]);
         }
 
         mutex_destroy(&prange->lock);
@@ -327,19 +327,38 @@ svm_range *svm_range_new(struct svm_range_list *svms, uint64_t start,
         uint64_t size = last - start + 1;
         struct svm_range *prange;
         struct kfd_process *p;
-
-       prange = kzalloc(sizeof(*prange), GFP_KERNEL);
-       if (!prange)
-               return NULL;
+       unsigned int nbits;
+       uint32_t gpuidx;
 
         p = container_of(svms, struct kfd_process, svms);
         if (!p->xnack_enabled && update_mem_usage &&
             amdgpu_amdkfd_reserve_mem_limit(NULL, size << PAGE_SHIFT,
                                     KFD_IOC_ALLOC_MEM_FLAGS_USERPTR, 0)) {
                 pr_info("SVM mapping failed, exceeds resident system memory limit\n");
-               kfree(prange);
                 return NULL;
         }
+
+       prange = kzalloc(sizeof(*prange), GFP_KERNEL);
+       if (!prange)
+               return NULL;
+
+       svm_range_set_default_attributes(&prange->preferred_loc,
+                                        &prange->prefetch_loc,
+                                        &prange->granularity, &prange->flags);
+
+       nbits = svm_range_mapped_nbits(start, last, prange->granularity);
+       pr_debug("prange 0x%p [0x%llx 0x%llx] bitmap_mapped nbits %d\n", prange,
+                start, last, nbits);
+       for_each_set_bit(gpuidx, p->svms.bitmap_supported, p->n_pdds) {
+               prange->bitmap_mapped[gpuidx] = bitmap_zalloc(nbits, GFP_KERNEL);
+               if (!prange->bitmap_mapped[gpuidx]) {
+                       while (gpuidx--)
+                               bitmap_free(prange->bitmap_mapped[gpuidx]);
+                       kfree(prange);
+                       return NULL;
+               }
+       }
+
         prange->npages = size;
         prange->svms = svms;
         prange->start = start;
@@ -359,10 +378,6 @@ svm_range *svm_range_new(struct svm_range_list *svms, uint64_t start,
                 bitmap_copy(prange->bitmap_access, svms->bitmap_supported,
                             MAX_GPU_INSTANCE);
 
-       svm_range_set_default_attributes(&prange->preferred_loc,
-                                        &prange->prefetch_loc,
-                                        &prange->granularity, &prange->flags);
-
         pr_debug("svms 0x%p [0x%llx 0x%llx]\n", svms, start, last);
 
         return prange;
@@ -984,6 +999,47 @@ svm_range_split_nodes(struct svm_range *new, struct svm_range *old,
         return 0;
  }
 
+static int
+svm_range_split_bitmap_mapped(struct svm_range *new, struct svm_range *old,
+                             uint64_t start, uint64_t last)
+{
+       struct kfd_process *p = container_of(new->svms, struct kfd_process, svms);
+       unsigned int nbits, old_nbits, old_nbits2;
+       unsigned long *bits;
+       uint32_t gpuidx;
+
+       nbits = svm_range_mapped_nbits(new->start, new->last, new->granularity);
+       old_nbits = svm_range_mapped_nbits(old->start, old->last, old->granularity);
+       old_nbits2 = svm_range_mapped_nbits(start, last, old->granularity);
+
+       pr_debug("old 0x%p [0x%lx 0x%lx] => [0x%llx 0x%llx] nbits %d => %d\n",
+                old, old->start, old->last, start, last, old_nbits, old_nbits2);
+       pr_debug("new 0x%p [0x%lx 0x%lx] nbits %d\n", new, new->start, new->last,
+                nbits);
+
+       for_each_set_bit(gpuidx, p->svms.bitmap_supported, p->n_pdds) {
+               bits = bitmap_alloc(old_nbits2, GFP_KERNEL);
+               if (!bits)
+                       return -ENOMEM;
+
+               if (start == old->start) {
+                       bitmap_shift_right(new->bitmap_mapped[gpuidx],
+                                          old->bitmap_mapped[gpuidx],
+                                          old_nbits2 - 1, old_nbits);
+                       bitmap_copy(bits, old->bitmap_mapped[gpuidx], old_nbits2);
+               } else {
+                       bitmap_copy(new->bitmap_mapped[gpuidx],
+                                   old->bitmap_mapped[gpuidx], nbits);
+                       bitmap_shift_right(bits, old->bitmap_mapped[gpuidx],
+                                          nbits - 1, old_nbits);
+               }
+               bitmap_free(old->bitmap_mapped[gpuidx]);
+               old->bitmap_mapped[gpuidx] = bits;
+       }
+
+       return 0;
+}
+
  /**
   * svm_range_split_adjust - split range and adjust
   *
@@ -1024,6 +1080,10 @@ svm_range_split_adjust(struct svm_range *new, struct svm_range *old,
                         return r;
         }
 
+       r = svm_range_split_bitmap_mapped(new, old, start, last);
+       if (r)
+               return r;
+
         old->npages = last - start + 1;
         old->start = start;
         old->last = last;
@@ -1032,7 +1092,6 @@ svm_range_split_adjust(struct svm_range *new, struct svm_range *old,
         new->prefetch_loc = old->prefetch_loc;
         new->actual_loc = old->actual_loc;
         new->granularity = old->granularity;
-       new->mapped_to_gpu = old->mapped_to_gpu;
         bitmap_copy(new->bitmap_access, old->bitmap_access, MAX_GPU_INSTANCE);
         bitmap_copy(new->bitmap_aip, old->bitmap_aip, MAX_GPU_INSTANCE);
 
@@ -1346,6 +1405,108 @@ svm_range_unmap_from_gpu(struct amdgpu_device *adev, struct amdgpu_vm *vm,
                                       fence);
  }
 
+/**
+ * svm_range_partial_mapped_dev - check if prange mapped on the specific GPU
+ *
+ * @gpuidx: the GPU index to check
+ * @prange: prange to check
+ * @start: the start address in pages
+ * @last: the last address in pages
+ *
+ * Return:
+ * true: if any part of the range within [start, last] mapped on the GPU
+ * false: if the entire range [start, last] not mapped on the GPU
+ */
+static bool
+svm_range_partial_mapped_dev(uint32_t gpuidx, struct svm_range *prange,
+                            unsigned long start, unsigned long last)
+{
+       unsigned long start_index, last_index;

drivers/gpu/drm/amd/amdkfd/kfd_svm.c svm_range_unmap_from_gpu(struct amdgpu_device *adev, struct amdgpu_vm *vm, · line 1434

Quoted 10 lines
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +10 -0
+
+       start = max(start, prange->start);
+       last = min(last, prange->last);
+       if (last < start)
+               return false;
+
+       start_index = (start - prange->start) >> prange->granularity;
+       last_index = (last - prange->start) >> prange->granularity;
+       return find_next_bit(prange->bitmap_mapped[gpuidx], last_index + 1,
+                            start_index) <= last_index;

The second parameter of find_next_bit is size that find_next_bit will look from bitmap offset, not end of bit. Look at: https://elixir.bootlin.com/linux/latest/source/include/linux/find.h#L54

Quoted 65 lines
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +65 -0
+}
+
+/**
+ * svm_range_partial_mapped - check if prange mapped on any GPU
+ *
+ * @prange: prange to check
+ * @start: the start address in pages
+ * @last: the last address in pages
+ *
+ * Return:
+ * true: if any part of prange mapped on any GPU currently
+ * false: if the entire range is not mapped on any GPU
+ */
+static bool
+svm_range_partial_mapped(struct svm_range *prange, unsigned long start,
+                        unsigned long last)
+{
+       struct kfd_process *p = container_of(prange->svms, struct kfd_process, svms);
+       struct svm_range *pchild;
+       uint32_t gpuidx;
+
+       for_each_set_bit(gpuidx, p->svms.bitmap_supported, p->n_pdds) {
+               list_for_each_entry(pchild, &prange->child_list, child_list) {
+                       if (svm_range_partial_mapped_dev(gpuidx, pchild, start, last))
+                               return true;
+               }
+
+               if (svm_range_partial_mapped_dev(gpuidx, prange, start, last))
+                       return true;
+       }
+       return false;
+}
+
+/**
+ * svm_range_complete_mapped - check if prange mapped on all GPUs completely
+ *
+ * @prange: prange to check
+ *
+ * Return:
+ * true: if the entire prange mapped completely on all GPUs that need access
+ * otherwise return false
+ */
+static bool svm_range_complete_mapped(struct svm_range *prange)
+{
+       int nbits = svm_range_mapped_nbits(prange->start, prange->last, prange->granularity);
+       DECLARE_BITMAP(bitmap, MAX_GPU_INSTANCE);
+       uint32_t gpuidx;
+       int r;
+
+       r = svm_range_need_access_gpus(bitmap, prange);
+       if (r)
+               return false;
+
+       for_each_set_bit(gpuidx, bitmap, MAX_GPU_INSTANCE)
+               if (!bitmap_full(prange->bitmap_mapped[gpuidx], nbits))
+                       return false;
+       return true;
+}
+
+static void
+svm_range_update_mapped(uint32_t gpuidx, struct svm_range *prange,
+                       unsigned long start, unsigned long last, bool mapped)
+{
+       unsigned long index, nbits;
+

drivers/gpu/drm/amd/amdkfd/kfd_svm.c svm_range_unmap_from_gpu(struct amdgpu_device *adev, struct amdgpu_vm *vm, · line 1509

Quoted 10 lines
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +10 -0
+       index = (start - prange->start) >> prange->granularity;
+       nbits = svm_range_mapped_nbits(start, last, prange->granularity);
+       if (mapped)
+               bitmap_set(prange->bitmap_mapped[gpuidx], index, nbits);
+       else
+               bitmap_clear(prange->bitmap_mapped[gpuidx], index, nbits);
+       pr_debug("prange 0x%p [0x%lx 0x%lx] update mapped %d nbits %ld gpu %d\n",
+                prange, start, last, mapped, nbits, gpuidx);
+}
+

prange->bitmap_mapped is based on prange->granularity, not same as [start, last]. The result is bitmap_mapped may cover bigger range than [start, last]. Is it ok?

Quoted 28 lines
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +0 -13
  static int
  svm_range_unmap_from_gpus(struct svm_range *prange, unsigned long start,
                           unsigned long last, uint32_t trigger)
@@ -1357,29 +1518,28 @@ svm_range_unmap_from_gpus(struct svm_range *prange, unsigned long start,
         uint32_t gpuidx;
         int r = 0;
 
-       if (!prange->mapped_to_gpu) {
-               pr_debug("prange 0x%p [0x%lx 0x%lx] not mapped to GPU\n",
-                        prange, prange->start, prange->last);
-               return 0;
-       }
-
-       if (prange->start == start && prange->last == last) {
-               pr_debug("unmap svms 0x%p prange 0x%p\n", prange->svms, prange);
-               prange->mapped_to_gpu = false;
-       }
-
         bitmap_or(bitmap, prange->bitmap_access, prange->bitmap_aip,
                   MAX_GPU_INSTANCE);
         p = container_of(prange->svms, struct kfd_process, svms);
 
         for_each_set_bit(gpuidx, bitmap, MAX_GPU_INSTANCE) {
-               pr_debug("unmap from gpu idx 0x%x\n", gpuidx);
                 pdd = kfd_process_device_from_gpuidx(p, gpuidx);
                 if (!pdd) {
                         pr_debug("failed to find device idx %d\n", gpuidx);
-                       return -EINVAL;

drivers/gpu/drm/amd/amdkfd/kfd_svm.c svm_range_unmap_from_gpus(struct svm_range *prange, unsigned long start, · line 1538

Quoted 10 lines
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +8 -0
+                       continue;
                 }
 
+               if (!svm_range_partial_mapped_dev(gpuidx, prange, start, last)) {
+                       pr_debug("svms 0x%p prange 0x%p [0x%lx 0x%lx] not mapped on gpu %d\n",
+                                prange->svms, prange, start, last, gpuidx);
+                       continue;
+               }
+
+               svm_range_update_mapped(gpuidx, prange, start, last, false);

svm_range_update_mapped should happen after svm_range_unmap_from_gpu, not before.

Quoted 8 lines
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +4 -0
+
+               pr_debug("unmap svms 0x%p prange 0x%p [0x%lx 0x%lx] from gpu %d\n",
+                        prange->svms, prange, start, last, gpuidx);
+
                 kfd_smi_event_unmap_from_gpu(pdd->dev, p->lead_thread->pid,
                                              start, last, trigger);
 

drivers/gpu/drm/amd/amdkfd/kfd_svm.c svm_range_map_to_gpus(struct svm_range *prange, unsigned long offset, · line 1685

Quoted 7 lines
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +4 -0
@@ -1519,6 +1679,10 @@ svm_range_map_to_gpus(struct svm_range *prange, unsigned long offset,
                 if (r)
                         break;
 
+               if (!r)
+                       svm_range_update_mapped(gpuidx, prange, prange->start + offset,
+                                               prange->start + offset + npages - 1, true);
+

Same as above that the mapping range is not same as the range that svm_range_update_mapped covers. This leds to that some pages are not mapped, but the bitmap marks they are mapped.

Regards

Xiaogang

Quoted 129 lines
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +15 -23
                 if (fence) {
                         r = dma_fence_wait(fence, false);
                         dma_fence_put(fence);
@@ -1670,7 +1834,7 @@ static int svm_range_validate_and_map(struct mm_struct *mm,
 
         if (bitmap_empty(ctx->bitmap, MAX_GPU_INSTANCE)) {
                 bitmap_copy(ctx->bitmap, prange->bitmap_access, MAX_GPU_INSTANCE);
-               if (!prange->mapped_to_gpu ||
+               if (!svm_range_partial_mapped(prange, prange->start, prange->last) ||
                     bitmap_empty(ctx->bitmap, MAX_GPU_INSTANCE)) {
                         r = 0;
                         goto free_ctx;
@@ -1755,9 +1919,6 @@ static int svm_range_validate_and_map(struct mm_struct *mm,
                         r = svm_range_map_to_gpus(prange, offset, npages, readonly,
                                                   ctx->bitmap, flush_tlb);
 
-               if (!r && next == end)
-                       prange->mapped_to_gpu = true;
-
                 svm_range_unlock(prange);
 
                 addr = next;
@@ -1939,22 +2100,8 @@ svm_range_evict(struct svm_range *prange, struct mm_struct *mm,
         if (!p->xnack_enabled ||
             (prange->flags & KFD_IOCTL_SVM_FLAG_GPU_ALWAYS_MAPPED)) {
                 int evicted_ranges;
-               bool mapped = prange->mapped_to_gpu;
-
-               list_for_each_entry(pchild, &prange->child_list, child_list) {
-                       if (!pchild->mapped_to_gpu)
-                               continue;
-                       mapped = true;
-                       mutex_lock_nested(&pchild->lock, 1);
-                       if (pchild->start <= last && pchild->last >= start) {
-                               pr_debug("increment pchild invalid [0x%lx 0x%lx]\n",
-                                        pchild->start, pchild->last);
-                               atomic_inc(&pchild->invalid);
-                       }
-                       mutex_unlock(&pchild->lock);
-               }
 
-               if (!mapped)
+               if (!svm_range_partial_mapped(prange, start, last))
                         return r;
 
                 if (prange->start <= last && prange->last >= start)
@@ -2005,7 +2152,10 @@ svm_range_evict(struct svm_range *prange, struct mm_struct *mm,
 
  static struct svm_range *svm_range_clone(struct svm_range *old)
  {
+       struct kfd_process *p = container_of(old->svms, struct kfd_process, svms);
         struct svm_range *new;
+       uint32_t gpuidx;
+       int nbits;
 
         new = svm_range_new(old->svms, old->start, old->last, false);
         if (!new)
@@ -2027,8 +2177,13 @@ static struct svm_range *svm_range_clone(struct svm_range *old)
         new->prefetch_loc = old->prefetch_loc;
         new->actual_loc = old->actual_loc;
         new->granularity = old->granularity;
-       new->mapped_to_gpu = old->mapped_to_gpu;
         new->vram_pages = old->vram_pages;
+       nbits = svm_range_mapped_nbits(new->start, new->last, new->granularity);
+       for_each_set_bit(gpuidx, p->svms.bitmap_supported, p->n_pdds) {
+               bitmap_copy(new->bitmap_mapped[gpuidx],
+                           old->bitmap_mapped[gpuidx],
+                           nbits);
+       };
         bitmap_copy(new->bitmap_access, old->bitmap_access, MAX_GPU_INSTANCE);
         bitmap_copy(new->bitmap_aip, old->bitmap_aip, MAX_GPU_INSTANCE);
 
@@ -2147,7 +2302,7 @@ svm_range_add(struct kfd_process *p, uint64_t start, uint64_t size,
                 next_start = min(node->last, last) + 1;
 
                 if (svm_range_is_same_attrs(p, prange, nattr, attrs) &&
-                   prange->mapped_to_gpu) {
+                   svm_range_complete_mapped(prange)) {
                         /* nothing to do */
                 } else if (node->start < start || node->last > last) {
                         /* node intersects the update range and its attributes
@@ -3641,7 +3796,7 @@ svm_range_set_attr(struct kfd_process *p, struct mm_struct *mm,
 
                 if (migrated && (!p->xnack_enabled ||
                     (prange->flags & KFD_IOCTL_SVM_FLAG_GPU_ALWAYS_MAPPED)) &&
-                   prange->mapped_to_gpu) {
+                   svm_range_partial_mapped(prange, prange->start, prange->last)) {
                         pr_debug("restore_work will update mappings of GPUs\n");
                         mutex_unlock(&prange->migrate_mutex);
                         continue;
@@ -3652,7 +3807,8 @@ svm_range_set_attr(struct kfd_process *p, struct mm_struct *mm,
                         continue;
                 }
 
-               flush_tlb = !migrated && update_mapping && prange->mapped_to_gpu;
+               flush_tlb = !migrated && update_mapping &&
+                           svm_range_partial_mapped(prange, prange->start, prange->last);
 
                 r = svm_range_validate_and_map(mm, prange, MAX_GPU_INSTANCE,
                                                true, flush_tlb);
drivers/gpu/drm/amd/amdkfd/kfd_svm.h +7 -2
index bf2fde008115..7e165854bc0e 100644
@@ -136,7 +136,7 @@ struct svm_range {
         struct list_head                child_list;
         DECLARE_BITMAP(bitmap_access, MAX_GPU_INSTANCE);
         DECLARE_BITMAP(bitmap_aip, MAX_GPU_INSTANCE);
-       bool                            mapped_to_gpu;
+       unsigned long                   *bitmap_mapped[MAX_GPU_INSTANCE];
  };
 
  static inline void svm_range_lock(struct svm_range *prange)
@@ -167,6 +167,12 @@ static inline struct svm_range_bo *svm_range_bo_ref(struct svm_range_bo *svm_bo)
                 return NULL;
  }
 
+static inline int svm_range_mapped_nbits(uint64_t start, uint64_t last,
+                                        uint8_t granularity)
+{
+       return (last >> granularity) - (start >> granularity) + 1;
+}
+
  int svm_range_list_init(struct kfd_process *p);
  void svm_range_list_fini(struct kfd_process *p);
  int svm_ioctl(struct kfd_process *p, enum kfd_ioctl_svm_op op, uint64_t start,
--

2.35.1

#8 lore raw

Re: [PATCH v2 5/7] drm/amdkfd: Check bitmap_mapped flag to skip retry fault

In reply to Philip Yang
Quoted 8 lines · Philip Yang

Caution: This message originated from an External Source. Use proper caution when opening attachments, clicking links, or responding.

Remove prange validate_timestamp which is not accurate for multiple GPUs.

Use the bitmap_mapped flag to skip the retry fault from different pages of the same range if the range is already mapped on the specific GPU.

This should be "different pages of same granularity range" as comments inside function svm_range_restore_page.

Quoted 56 lines
---
  drivers/gpu/drm/amd/amdkfd/kfd_svm.c | 24 ++++++++----------------
  drivers/gpu/drm/amd/amdkfd/kfd_svm.h |  1 -
  2 files changed, 8 insertions(+), 17 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +0 -16
index daa996d7039d..0ee5633c8972 100644
@@ -43,10 +43,6 @@
 
  #define AMDGPU_SVM_RANGE_RESTORE_DELAY_MS 1
 
-/* Long enough to ensure no retry fault comes after svm range is restored and
- * page table is updated.
- */
-#define AMDGPU_SVM_RANGE_RETRY_FAULT_PENDING   (2UL * NSEC_PER_MSEC)
  #if IS_ENABLED(CONFIG_DYNAMIC_DEBUG)
  #define dynamic_svm_range_dump(svms) \
         _dynamic_func_call_no_desc("svm_range_dump", svm_range_debug_dump, svms)
@@ -369,7 +365,6 @@ svm_range *svm_range_new(struct svm_range_list *svms, uint64_t start,
         INIT_LIST_HEAD(&prange->deferred_list);
         INIT_LIST_HEAD(&prange->child_list);
         atomic_set(&prange->invalid, 0);
-       prange->validate_timestamp = 0;
         prange->vram_pages = 0;
         mutex_init(&prange->migrate_mutex);
         mutex_init(&prange->lock);
@@ -1938,8 +1933,6 @@ static int svm_range_validate_and_map(struct mm_struct *mm,
         }
 
         svm_range_unreserve_bos(ctx);
-       if (!r)
-               prange->validate_timestamp = ktime_get_boottime();
 
  free_ctx:
         kfree(ctx);
@@ -3214,15 +3207,6 @@ svm_range_restore_pages(struct amdgpu_device *adev, unsigned int pasid,
                 goto out_unlock_range;
         }
 
-       /* skip duplicate vm fault on different pages of same range */
-       if (ktime_before(timestamp, ktime_add_ns(prange->validate_timestamp,
-                               AMDGPU_SVM_RANGE_RETRY_FAULT_PENDING))) {
-               pr_debug("svms 0x%p [0x%lx %lx] already restored\n",
-                        svms, prange->start, prange->last);
-               r = 0;
-               goto out_unlock_range;
-       }
-
         /* __do_munmap removed VMA, return success as we are handling stale
          * retry fault.
          */

drivers/gpu/drm/amd/amdkfd/kfd_svm.c svm_range_restore_pages(struct amdgpu_device *adev, unsigned int pasid, · line 3236

Quoted 5 lines
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +2 -0
@@ -3248,6 +3232,14 @@ svm_range_restore_pages(struct amdgpu_device *adev, unsigned int pasid,
                 goto out_unlock_range;
         }
 
+       /* skip duplicate vm fault on different pages of same granularity range */
+       if (svm_range_partial_mapped_dev(gpuidx, prange, addr, addr)) {

should use svm_range_complete_mapped on gpuidx, instead of svm_range_partial_mapped_dev?

Regards

Xiaogang

Quoted 24 lines
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +6 -0
+               pr_debug("svms 0x%p [0x%lx %lx] addr 0x%llx already mapped on gpu %d\n",
+                        svms, prange->start, prange->last, addr, gpuidx);
+               r = 0;
+               goto out_unlock_range;
+       }
+
         pr_debug("svms %p [0x%lx 0x%lx] best restore 0x%x, actual loc 0x%x\n",
                  svms, prange->start, prange->last, best_loc,
                  prange->actual_loc);
drivers/gpu/drm/amd/amdkfd/kfd_svm.h +0 -2
index 7e165854bc0e..7a12be42cf16 100644
@@ -129,7 +129,6 @@ struct svm_range {
         uint32_t                        actual_loc;
         uint8_t                         granularity;
         atomic_t                        invalid;
-       ktime_t                         validate_timestamp;
         struct mmu_interval_notifier    notifier;
         struct svm_work_list_item       work_item;
         struct list_head                deferred_list;
--

2.35.1

maintainer Reviewed-by on v2 1/7
#12 lore raw
In reply to Philip Yang
Quoted 9 lines · Philip Yang

If using sdma update GPU page table, kfd flush tlb does nothing if vm update fence callback doesn't update vm->tlb_seq. This works now because retry fault keep coming and will be handled to update page table again after AMDGPU_SVM_RANGE_RETRY_FAULT_PENDING timeout and flush tlb.

With the following patch to add bitmap_mapped flag, the retry fault recover will only update GPU page table once, need wait vm udate fence and then flush tlb.

Quoted 6 lines

No change if using CPU update GPU page table because no vm update fence.

Remove wait parameter in svm_range_validate_and_map because it is always called with true now.

Quoted 72 lines
---
  drivers/gpu/drm/amd/amdkfd/kfd_svm.c | 15 +++++++--------
  1 file changed, 7 insertions(+), 8 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +7 -8
index 2b4ccc0531e8..e195106a77cb 100644
@@ -1454,7 +1454,7 @@ svm_range_map_to_gpu(struct kfd_process_device *pdd, struct svm_range *prange,
  static int
  svm_range_map_to_gpus(struct svm_range *prange, unsigned long offset,
  		      unsigned long npages, bool readonly,
-		      unsigned long *bitmap, bool wait, bool flush_tlb)
+		      unsigned long *bitmap, bool flush_tlb)
  {
  	struct kfd_process_device *pdd;
  	struct amdgpu_device *bo_adev = NULL;
@@ -1487,8 +1487,7 @@ svm_range_map_to_gpus(struct svm_range *prange, unsigned long offset,
 
  		r = svm_range_map_to_gpu(pdd, prange, offset, npages, readonly,
  					 prange->dma_addr[gpuidx],
-					 bo_adev, wait ? &fence : NULL,
-					 flush_tlb);
+					 bo_adev, &fence, flush_tlb);
  		if (r)
  			break;
 
@@ -1612,7 +1611,7 @@ static void *kfd_svm_page_owner(struct kfd_process *p, int32_t gpuidx)
   */
  static int svm_range_validate_and_map(struct mm_struct *mm,
  				      struct svm_range *prange, int32_t gpuidx,
-				      bool intr, bool wait, bool flush_tlb)
+				      bool intr, bool flush_tlb)
  {
  	struct svm_validate_context *ctx;
  	unsigned long start, end, addr;
@@ -1741,7 +1740,7 @@ static int svm_range_validate_and_map(struct mm_struct *mm,
 
  		if (!r)
  			r = svm_range_map_to_gpus(prange, offset, npages, readonly,
-						  ctx->bitmap, wait, flush_tlb);
+						  ctx->bitmap, flush_tlb);
 
  		if (!r && next == end)
  			prange->mapped_to_gpu = true;
@@ -1848,7 +1847,7 @@ static void svm_range_restore_work(struct work_struct *work)
  		mutex_lock(&prange->migrate_mutex);
 
  		r = svm_range_validate_and_map(mm, prange, MAX_GPU_INSTANCE,
-					       false, true, false);
+					       false, false);
  		if (r)
  			pr_debug("failed %d to map 0x%lx to gpus\n", r,
  				 prange->start);
@@ -3094,7 +3093,7 @@ svm_range_restore_pages(struct amdgpu_device *adev, unsigned int pasid,
  		}
  	}
 
-	r = svm_range_validate_and_map(mm, prange, gpuidx, false, false, false);
+	r = svm_range_validate_and_map(mm, prange, gpuidx, false, false);
  	if (r)
  		pr_debug("failed %d to map svms 0x%p [0x%lx 0x%lx] to gpus\n",
  			 r, svms, prange->start, prange->last);
@@ -3643,7 +3642,7 @@ svm_range_set_attr(struct kfd_process *p, struct mm_struct *mm,
  		flush_tlb = !migrated && update_mapping && prange->mapped_to_gpu;
 
  		r = svm_range_validate_and_map(mm, prange, MAX_GPU_INSTANCE,
-					       true, true, flush_tlb);
+					       true, flush_tlb);
  		if (r)
  			pr_debug("failed %d to map svm range\n", r);
 
maintainer Reviewed-by on v2 2/7
#2 lore raw

Re: [PATCH v2 2/7] drm/amdkfd: Add helper function svm_range_need_access_gpus

In reply to Philip Yang
Quoted 5 lines · Philip Yang

Refactor svm_range_validate_and_map to add and use the helper function to get all GPUs bitmap that need access the svm range. No functional change. This helper will be used in the following patch.

Quoted 78 lines
---
  drivers/gpu/drm/amd/amdkfd/kfd_svm.c | 55 +++++++++++++++++-----------
  1 file changed, 34 insertions(+), 21 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +34 -21
index e195106a77cb..fb22b857adbc 100644
@@ -1191,6 +1191,34 @@ svm_range_split_by_granularity(struct kfd_process *p, struct mm_struct *mm,
  	}
  	return 0;
  }
+
+static int
+svm_range_need_access_gpus(unsigned long *bitmap, struct svm_range *prange)
+{
+	struct kfd_process *p = container_of(prange->svms, struct kfd_process, svms);
+	uint32_t gpuidx;
+
+	if (p->xnack_enabled) {
+		bitmap_copy(bitmap, prange->bitmap_aip, MAX_GPU_INSTANCE);
+
+		/* If prefetch range to GPU, or GPU retry fault migrate range to
+		 * GPU, which has ACCESS attribute to the range, create mapping
+		 * on that GPU.
+		 */
+		if (prange->actual_loc) {
+			gpuidx = kfd_process_gpuidx_from_gpuid(p, prange->actual_loc);
+			if (gpuidx < 0)
+				return -EINVAL;
+			if (test_bit(gpuidx, prange->bitmap_access))
+				bitmap_set(bitmap, gpuidx, 1);
+		}
+	} else {
+		bitmap_or(bitmap, prange->bitmap_access, prange->bitmap_aip,
+			  MAX_GPU_INSTANCE);
+	}
+	return 0;
+}
+
  static bool
  svm_nodes_in_same_hive(struct kfd_node *node_a, struct kfd_node *node_b)
  {
@@ -1631,28 +1659,13 @@ static int svm_range_validate_and_map(struct mm_struct *mm,
  	if (gpuidx < MAX_GPU_INSTANCE) {
  		bitmap_zero(ctx->bitmap, MAX_GPU_INSTANCE);
  		bitmap_set(ctx->bitmap, gpuidx, 1);
-	} else if (ctx->process->xnack_enabled) {
-		bitmap_copy(ctx->bitmap, prange->bitmap_aip, MAX_GPU_INSTANCE);
-
-		/* If prefetch range to GPU, or GPU retry fault migrate range to
-		 * GPU, which has ACCESS attribute to the range, create mapping
-		 * on that GPU.
-		 */
-		if (prange->actual_loc) {
-			gpuidx = kfd_process_gpuidx_from_gpuid(ctx->process,
-							prange->actual_loc);
-			if (gpuidx < 0) {
-				WARN_ONCE(1, "failed get device by id 0x%x\n",
-					 prange->actual_loc);
-				r = -EINVAL;
-				goto free_ctx;
-			}
-			if (test_bit(gpuidx, prange->bitmap_access))
-				bitmap_set(ctx->bitmap, gpuidx, 1);
-		}
  	} else {
-		bitmap_or(ctx->bitmap, prange->bitmap_access,
-			  prange->bitmap_aip, MAX_GPU_INSTANCE);
+		r = svm_range_need_access_gpus(ctx->bitmap, prange);
+		if (r) {
+			WARN_ONCE(1, "failed get device by id 0x%x\n",
+				 prange->actual_loc);
+			goto free_ctx;
+		}
  	}
 
  	if (bitmap_empty(ctx->bitmap, MAX_GPU_INSTANCE)) {
maintainer
#5 lore raw

Re: [PATCH v2 3/7] amd/amdkfd: Add granularity bitmap mapped to gpu flag

In reply to Philip Yang
Quoted 244 lines · Philip Yang

Replace prange->mapped_to_gpu with prange->bitmap_mapped[], which is per GPU flag and based on prange granularity, updated when map to GPUS or unmap from GPUs, to optimize multiple GPU map, unmap and retry fault recover.

svm_range_partial_mapped is false only if no part of the range mapping on any GPUs.

---
  drivers/gpu/drm/amd/amdkfd/kfd_svm.c | 256 +++++++++++++++++++++------
  drivers/gpu/drm/amd/amdkfd/kfd_svm.h |   8 +-
  2 files changed, 213 insertions(+), 51 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +156 -14
index fb22b857adbc..4e1af4b181ea 100644
@@ -296,12 +296,12 @@ static void svm_range_free(struct svm_range *prange, bool do_unmap)
  					KFD_IOC_ALLOC_MEM_FLAGS_USERPTR, 0);
  	}
 
-	/* free dma_addr array for each gpu */
+	/* free dma_addr array, bitmap_mapped for each gpu */
  	for (gpuidx = 0; gpuidx < MAX_GPU_INSTANCE; gpuidx++) {
-		if (prange->dma_addr[gpuidx]) {
+		if (prange->dma_addr[gpuidx])
  			kvfree(prange->dma_addr[gpuidx]);
-				prange->dma_addr[gpuidx] = NULL;
-		}
+		if (prange->bitmap_mapped[gpuidx])
+			bitmap_free(prange->bitmap_mapped[gpuidx]);
  	}
 
  	mutex_destroy(&prange->lock);
@@ -327,19 +327,38 @@ svm_range *svm_range_new(struct svm_range_list *svms, uint64_t start,
  	uint64_t size = last - start + 1;
  	struct svm_range *prange;
  	struct kfd_process *p;
-
-	prange = kzalloc(sizeof(*prange), GFP_KERNEL);
-	if (!prange)
-		return NULL;
+	unsigned int nbits;
+	uint32_t gpuidx;
 
  	p = container_of(svms, struct kfd_process, svms);
  	if (!p->xnack_enabled && update_mem_usage &&
  	    amdgpu_amdkfd_reserve_mem_limit(NULL, size << PAGE_SHIFT,
  				    KFD_IOC_ALLOC_MEM_FLAGS_USERPTR, 0)) {
  		pr_info("SVM mapping failed, exceeds resident system memory limit\n");
-		kfree(prange);
  		return NULL;
  	}
+
+	prange = kzalloc(sizeof(*prange), GFP_KERNEL);
+	if (!prange)
+		return NULL;
+
+	svm_range_set_default_attributes(&prange->preferred_loc,
+					 &prange->prefetch_loc,
+					 &prange->granularity, &prange->flags);
+
+	nbits = svm_range_mapped_nbits(start, last, prange->granularity);
+	pr_debug("prange 0x%p [0x%llx 0x%llx] bitmap_mapped nbits %d\n", prange,
+		 start, last, nbits);
+	for_each_set_bit(gpuidx, p->svms.bitmap_supported, p->n_pdds) {
+		prange->bitmap_mapped[gpuidx] = bitmap_zalloc(nbits, GFP_KERNEL);
+		if (!prange->bitmap_mapped[gpuidx]) {
+			while (gpuidx--)
+				bitmap_free(prange->bitmap_mapped[gpuidx]);
+			kfree(prange);
+			return NULL;
+		}
+	}
+
  	prange->npages = size;
  	prange->svms = svms;
  	prange->start = start;
@@ -359,10 +378,6 @@ svm_range *svm_range_new(struct svm_range_list *svms, uint64_t start,
  		bitmap_copy(prange->bitmap_access, svms->bitmap_supported,
  			    MAX_GPU_INSTANCE);
 
-	svm_range_set_default_attributes(&prange->preferred_loc,
-					 &prange->prefetch_loc,
-					 &prange->granularity, &prange->flags);
-
  	pr_debug("svms 0x%p [0x%llx 0x%llx]\n", svms, start, last);
 
  	return prange;
@@ -984,6 +999,47 @@ svm_range_split_nodes(struct svm_range *new, struct svm_range *old,
  	return 0;
  }
 
+static int
+svm_range_split_bitmap_mapped(struct svm_range *new, struct svm_range *old,
+			      uint64_t start, uint64_t last)
+{
+	struct kfd_process *p = container_of(new->svms, struct kfd_process, svms);
+	unsigned int nbits, old_nbits, old_nbits2;
+	unsigned long *bits;
+	uint32_t gpuidx;
+
+	nbits = svm_range_mapped_nbits(new->start, new->last, new->granularity);
+	old_nbits = svm_range_mapped_nbits(old->start, old->last, old->granularity);
+	old_nbits2 = svm_range_mapped_nbits(start, last, old->granularity);
+
+	pr_debug("old 0x%p [0x%lx 0x%lx] => [0x%llx 0x%llx] nbits %d => %d\n",
+		 old, old->start, old->last, start, last, old_nbits, old_nbits2);
+	pr_debug("new 0x%p [0x%lx 0x%lx] nbits %d\n", new, new->start, new->last,
+		 nbits);
+
+	for_each_set_bit(gpuidx, p->svms.bitmap_supported, p->n_pdds) {
+		bits = bitmap_alloc(old_nbits2, GFP_KERNEL);
+		if (!bits)
+			return -ENOMEM;
+
+		if (start == old->start) {
+			bitmap_shift_right(new->bitmap_mapped[gpuidx],
+					   old->bitmap_mapped[gpuidx],
+					   old_nbits2 - 1, old_nbits);
+			bitmap_copy(bits, old->bitmap_mapped[gpuidx], old_nbits2);
+		} else {
+			bitmap_copy(new->bitmap_mapped[gpuidx],
+				    old->bitmap_mapped[gpuidx], nbits);
+			bitmap_shift_right(bits, old->bitmap_mapped[gpuidx],
+					   nbits - 1, old_nbits);
+		}
+		bitmap_free(old->bitmap_mapped[gpuidx]);
+		old->bitmap_mapped[gpuidx] = bits;
+	}
+
+	return 0;
+}
+
  /**
   * svm_range_split_adjust - split range and adjust
   *
@@ -1024,6 +1080,10 @@ svm_range_split_adjust(struct svm_range *new, struct svm_range *old,
  			return r;
  	}
 
+	r = svm_range_split_bitmap_mapped(new, old, start, last);
+	if (r)
+		return r;
+
  	old->npages = last - start + 1;
  	old->start = start;
  	old->last = last;
@@ -1032,7 +1092,6 @@ svm_range_split_adjust(struct svm_range *new, struct svm_range *old,
  	new->prefetch_loc = old->prefetch_loc;
  	new->actual_loc = old->actual_loc;
  	new->granularity = old->granularity;
-	new->mapped_to_gpu = old->mapped_to_gpu;
  	bitmap_copy(new->bitmap_access, old->bitmap_access, MAX_GPU_INSTANCE);
  	bitmap_copy(new->bitmap_aip, old->bitmap_aip, MAX_GPU_INSTANCE);
 
@@ -1346,6 +1405,108 @@ svm_range_unmap_from_gpu(struct amdgpu_device *adev, struct amdgpu_vm *vm,
  				      fence);
  }
 
+/**
+ * svm_range_partial_mapped_dev - check if prange mapped on the specific GPU
+ *
+ * @gpuidx: the GPU index to check
+ * @prange: prange to check
+ * @start: the start address in pages
+ * @last: the last address in pages
+ *
+ * Return:
+ * true: if any part of the range within [start, last] mapped on the GPU
+ * false: if the entire range [start, last] not mapped on the GPU
+ */
+static bool
+svm_range_partial_mapped_dev(uint32_t gpuidx, struct svm_range *prange,
+			     unsigned long start, unsigned long last)
+{
+	unsigned long start_index, last_index;
+
+	start = max(start, prange->start);
+	last = min(last, prange->last);
+	if (last < start)
+		return false;
+
+	start_index = (start - prange->start) >> prange->granularity;
+	last_index = (last - prange->start) >> prange->granularity;
+	return find_next_bit(prange->bitmap_mapped[gpuidx], last_index + 1,
+			     start_index) <= last_index;
+}
+
+/**
+ * svm_range_partial_mapped - check if prange mapped on any GPU
+ *
+ * @prange: prange to check
+ * @start: the start address in pages
+ * @last: the last address in pages
+ *
+ * Return:
+ * true: if any part of prange mapped on any GPU currently
+ * false: if the entire range is not mapped on any GPU
+ */
+static bool
+svm_range_partial_mapped(struct svm_range *prange, unsigned long start,
+			 unsigned long last)
+{
+	struct kfd_process *p = container_of(prange->svms, struct kfd_process, svms);
+	struct svm_range *pchild;
+	uint32_t gpuidx;
+
+	for_each_set_bit(gpuidx, p->svms.bitmap_supported, p->n_pdds) {
+		list_for_each_entry(pchild, &prange->child_list, child_list) {
+			if (svm_range_partial_mapped_dev(gpuidx, pchild, start, last))
+				return true;
+		}
+
+		if (svm_range_partial_mapped_dev(gpuidx, prange, start, last))
+			return true;
+	}
+	return false;
+}
+
+/**
+ * svm_range_complete_mapped - check if prange mapped on all GPUs completely
+ *
+ * @prange: prange to check
+ *
+ * Return:
+ * true: if the entire prange mapped completely on all GPUs that need access
+ * otherwise return false
+ */
+static bool svm_range_complete_mapped(struct svm_range *prange)
+{
+	int nbits = svm_range_mapped_nbits(prange->start, prange->last, prange->granularity);
+	DECLARE_BITMAP(bitmap, MAX_GPU_INSTANCE);
+	uint32_t gpuidx;
+	int r;
+
+	r = svm_range_need_access_gpus(bitmap, prange);
+	if (r)
+		return false;
+
+	for_each_set_bit(gpuidx, bitmap, MAX_GPU_INSTANCE)
+		if (!bitmap_full(prange->bitmap_mapped[gpuidx], nbits))
+			return false;

drivers/gpu/drm/amd/amdkfd/kfd_svm.c svm_range_unmap_from_gpu(struct amdgpu_device *adev, struct amdgpu_vm *vm, · line 1500

Quoted 10 lines
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +10 -0
+	return true;
+}
+
+static void
+svm_range_update_mapped(uint32_t gpuidx, struct svm_range *prange,
+			unsigned long start, unsigned long last, bool mapped)
+{
+	unsigned long index, nbits;
+
+	index = (start - prange->start) >> prange->granularity;

This aligns the address "buckets" represented by bits in the bitmap with the start of the range. To be effective to protect page table fragments and PDE-PTEs, I think you need to align the buckets with the range granularity. That means that both the first and the last bucket may be less than the granularity size.

With that, I think the index calculation here should be:

    index = (start >> prange->granularity) - (prange->start >> prange->granularity);

This probably has more consequences throughout the rest of this patch, including calculating the size of the bitmap correctly.

Regards,
  Felix

Quoted 188 lines
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +40 -36
+	nbits = svm_range_mapped_nbits(start, last, prange->granularity);
+	if (mapped)
+		bitmap_set(prange->bitmap_mapped[gpuidx], index, nbits);
+	else
+		bitmap_clear(prange->bitmap_mapped[gpuidx], index, nbits);
+	pr_debug("prange 0x%p [0x%lx 0x%lx] update mapped %d nbits %ld gpu %d\n",
+		 prange, start, last, mapped, nbits, gpuidx);
+}
+
  static int
  svm_range_unmap_from_gpus(struct svm_range *prange, unsigned long start,
  			  unsigned long last, uint32_t trigger)
@@ -1357,29 +1518,28 @@ svm_range_unmap_from_gpus(struct svm_range *prange, unsigned long start,
  	uint32_t gpuidx;
  	int r = 0;
 
-	if (!prange->mapped_to_gpu) {
-		pr_debug("prange 0x%p [0x%lx 0x%lx] not mapped to GPU\n",
-			 prange, prange->start, prange->last);
-		return 0;
-	}
-
-	if (prange->start == start && prange->last == last) {
-		pr_debug("unmap svms 0x%p prange 0x%p\n", prange->svms, prange);
-		prange->mapped_to_gpu = false;
-	}
-
  	bitmap_or(bitmap, prange->bitmap_access, prange->bitmap_aip,
  		  MAX_GPU_INSTANCE);
  	p = container_of(prange->svms, struct kfd_process, svms);
 
  	for_each_set_bit(gpuidx, bitmap, MAX_GPU_INSTANCE) {
-		pr_debug("unmap from gpu idx 0x%x\n", gpuidx);
  		pdd = kfd_process_device_from_gpuidx(p, gpuidx);
  		if (!pdd) {
  			pr_debug("failed to find device idx %d\n", gpuidx);
-			return -EINVAL;
+			continue;
  		}
 
+		if (!svm_range_partial_mapped_dev(gpuidx, prange, start, last)) {
+			pr_debug("svms 0x%p prange 0x%p [0x%lx 0x%lx] not mapped on gpu %d\n",
+				 prange->svms, prange, start, last, gpuidx);
+			continue;
+		}
+
+		svm_range_update_mapped(gpuidx, prange, start, last, false);
+
+		pr_debug("unmap svms 0x%p prange 0x%p [0x%lx 0x%lx] from gpu %d\n",
+			 prange->svms, prange, start, last, gpuidx);
+
  		kfd_smi_event_unmap_from_gpu(pdd->dev, p->lead_thread->pid,
  					     start, last, trigger);
 
@@ -1519,6 +1679,10 @@ svm_range_map_to_gpus(struct svm_range *prange, unsigned long offset,
  		if (r)
  			break;
 
+		if (!r)
+			svm_range_update_mapped(gpuidx, prange, prange->start + offset,
+						prange->start + offset + npages - 1, true);
+
  		if (fence) {
  			r = dma_fence_wait(fence, false);
  			dma_fence_put(fence);
@@ -1670,7 +1834,7 @@ static int svm_range_validate_and_map(struct mm_struct *mm,
 
  	if (bitmap_empty(ctx->bitmap, MAX_GPU_INSTANCE)) {
  		bitmap_copy(ctx->bitmap, prange->bitmap_access, MAX_GPU_INSTANCE);
-		if (!prange->mapped_to_gpu ||
+		if (!svm_range_partial_mapped(prange, prange->start, prange->last) ||
  		    bitmap_empty(ctx->bitmap, MAX_GPU_INSTANCE)) {
  			r = 0;
  			goto free_ctx;
@@ -1755,9 +1919,6 @@ static int svm_range_validate_and_map(struct mm_struct *mm,
  			r = svm_range_map_to_gpus(prange, offset, npages, readonly,
  						  ctx->bitmap, flush_tlb);
 
-		if (!r && next == end)
-			prange->mapped_to_gpu = true;
-
  		svm_range_unlock(prange);
 
  		addr = next;
@@ -1939,22 +2100,8 @@ svm_range_evict(struct svm_range *prange, struct mm_struct *mm,
  	if (!p->xnack_enabled ||
  	    (prange->flags & KFD_IOCTL_SVM_FLAG_GPU_ALWAYS_MAPPED)) {
  		int evicted_ranges;
-		bool mapped = prange->mapped_to_gpu;
-
-		list_for_each_entry(pchild, &prange->child_list, child_list) {
-			if (!pchild->mapped_to_gpu)
-				continue;
-			mapped = true;
-			mutex_lock_nested(&pchild->lock, 1);
-			if (pchild->start <= last && pchild->last >= start) {
-				pr_debug("increment pchild invalid [0x%lx 0x%lx]\n",
-					 pchild->start, pchild->last);
-				atomic_inc(&pchild->invalid);
-			}
-			mutex_unlock(&pchild->lock);
-		}
 
-		if (!mapped)
+		if (!svm_range_partial_mapped(prange, start, last))
  			return r;
 
  		if (prange->start <= last && prange->last >= start)
@@ -2005,7 +2152,10 @@ svm_range_evict(struct svm_range *prange, struct mm_struct *mm,
 
  static struct svm_range *svm_range_clone(struct svm_range *old)
  {
+	struct kfd_process *p = container_of(old->svms, struct kfd_process, svms);
  	struct svm_range *new;
+	uint32_t gpuidx;
+	int nbits;
 
  	new = svm_range_new(old->svms, old->start, old->last, false);
  	if (!new)
@@ -2027,8 +2177,13 @@ static struct svm_range *svm_range_clone(struct svm_range *old)
  	new->prefetch_loc = old->prefetch_loc;
  	new->actual_loc = old->actual_loc;
  	new->granularity = old->granularity;
-	new->mapped_to_gpu = old->mapped_to_gpu;
  	new->vram_pages = old->vram_pages;
+	nbits = svm_range_mapped_nbits(new->start, new->last, new->granularity);
+	for_each_set_bit(gpuidx, p->svms.bitmap_supported, p->n_pdds) {
+		bitmap_copy(new->bitmap_mapped[gpuidx],
+			    old->bitmap_mapped[gpuidx],
+			    nbits);
+	};
  	bitmap_copy(new->bitmap_access, old->bitmap_access, MAX_GPU_INSTANCE);
  	bitmap_copy(new->bitmap_aip, old->bitmap_aip, MAX_GPU_INSTANCE);
 
@@ -2147,7 +2302,7 @@ svm_range_add(struct kfd_process *p, uint64_t start, uint64_t size,
  		next_start = min(node->last, last) + 1;
 
  		if (svm_range_is_same_attrs(p, prange, nattr, attrs) &&
-		    prange->mapped_to_gpu) {
+		    svm_range_complete_mapped(prange)) {
  			/* nothing to do */
  		} else if (node->start < start || node->last > last) {
  			/* node intersects the update range and its attributes
@@ -3641,7 +3796,7 @@ svm_range_set_attr(struct kfd_process *p, struct mm_struct *mm,
 
  		if (migrated && (!p->xnack_enabled ||
  		    (prange->flags & KFD_IOCTL_SVM_FLAG_GPU_ALWAYS_MAPPED)) &&
-		    prange->mapped_to_gpu) {
+		    svm_range_partial_mapped(prange, prange->start, prange->last)) {
  			pr_debug("restore_work will update mappings of GPUs\n");
  			mutex_unlock(&prange->migrate_mutex);
  			continue;
@@ -3652,7 +3807,8 @@ svm_range_set_attr(struct kfd_process *p, struct mm_struct *mm,
  			continue;
  		}
 
-		flush_tlb = !migrated && update_mapping && prange->mapped_to_gpu;
+		flush_tlb = !migrated && update_mapping &&
+			    svm_range_partial_mapped(prange, prange->start, prange->last);
 
  		r = svm_range_validate_and_map(mm, prange, MAX_GPU_INSTANCE,
  					       true, flush_tlb);
drivers/gpu/drm/amd/amdkfd/kfd_svm.h +7 -1
index bf2fde008115..7e165854bc0e 100644
@@ -136,7 +136,7 @@ struct svm_range {
  	struct list_head		child_list;
  	DECLARE_BITMAP(bitmap_access, MAX_GPU_INSTANCE);
  	DECLARE_BITMAP(bitmap_aip, MAX_GPU_INSTANCE);
-	bool				mapped_to_gpu;
+	unsigned long			*bitmap_mapped[MAX_GPU_INSTANCE];
  };
 
  static inline void svm_range_lock(struct svm_range *prange)
@@ -167,6 +167,12 @@ static inline struct svm_range_bo *svm_range_bo_ref(struct svm_range_bo *svm_bo)
  		return NULL;
  }
 
+static inline int svm_range_mapped_nbits(uint64_t start, uint64_t last,
+					 uint8_t granularity)
+{
+	return (last >> granularity) - (start >> granularity) + 1;
+}
+
  int svm_range_list_init(struct kfd_process *p);
  void svm_range_list_fini(struct kfd_process *p);
  int svm_ioctl(struct kfd_process *p, enum kfd_ioctl_svm_op op, uint64_t start,