Skip to contentSkip to conversation

drm/amdkfd: don't stall other threads during large userptr registration

PATCH In review William Palacek 3 participants 9 messages Nov 18 – Nov 20

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

cover drm/amdkfd: don't stall other threads during large userptr registration

Registering a large userptr BO (hipHostMalloc) holds mmap_lock, p->mutex and process_info->lock while every page of the range is faulted in. For 32 GiB on MI300X that takes seconds, and every other thread of the process that submits GPU work waits behind it for the whole time.

Patch 1 limits userptr sg segments to half of sg->length. Larger physically contiguous segments can overflow the padded segment length in iommu_dma_map_sg(), and the pin fails. This already happens without the series; patch 3 makes the pages contiguous enough that it hits on a freshly booted system, so it goes first.

Patch 2 drops p->mutex from AMDKFD_IOC_GET_CLOCK_COUNTERS, which other threads call while submitting work.

Patch 3 drops mmap_lock between 2 MiB chunks while faulting userptr pages in.

Patch 4 faults in and validates userptr pages without p->mutex and process_info->lock, and adds the BO to the valid list only if no invalidation happened meanwhile.

Tested with two 32 GiB hipHostMalloc/hipHostFree cycles next to 4 PyTorch streams on 2 GPUs, measuring the worst GPU submission latency of the other threads (limit 1 s), on the amdgpu 7.2 DKMS driver:

MI300X, IOMMU DMA-FQ unpatched 0/3 (6.1-6.2 s; on other boots
the 32 GiB pin itself fails)
series 3/3 (0.80-0.95 s)

  MI300X, IOMMU identity   unpatched    0/3 (9.6-10.1 s)
                           patches 2-3  0/3 (10.5-11.0 s)
                           patches 2-4  3/3 (0.81-0.93 s)
  MI350X, IOMMU identity   unpatched    0/4 (4.55-4.84 s)
                           series       7/7 (0.21-0.39 s)

kfdtest KFDMemoryTest.* and KFDEvictTest.* pass on both GPUs.

William Palacek (4):
drm/amdgpu: limit userptr sg segments to half of sg->length drm/amdkfd: don't take p->mutex in AMDKFD_IOC_GET_CLOCK_COUNTERS drm/amdgpu: drop mmap_lock between chunks when faulting userptr pages drm/amdkfd: register userptr pages without holding KFD process locks

 .../gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c  | 53 +++++++++++--------
 drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.c       | 14 ++++-
 drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.h       |  2 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c       |  9 ++--
 drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h       |  7 +++
 drivers/gpu/drm/amd/amdkfd/kfd_chardev.c      | 10 +++-
 drivers/gpu/drm/amd/amdkfd/kfd_svm.c          |  2 +-
 7 files changed, 66 insertions(+), 31 deletions(-)
Signature · 1 line
2.43.0
#1 lore raw

PATCH 1/4 drm/amdgpu: limit userptr sg segments to half of sg->length

Userptr sg tables are built with sg_alloc_table_from_pages(), which merges physically contiguous pages into segments of up to 0xfffff000 bytes. iommu_dma_map_sg() pads a segment so that the next one does not cross the DMA segment boundary, and adds the padding to the 32-bit sg->length. With two large contiguous segments the padded length reaches 4 GiB and wraps to 0.

On kernels without commit 1c8f6308e557 ("iommu/dma: Catch scatterlist length overflows") the wrapped length makes iommu_map_sg() map less than the allocated IOVA range. The IOVA is then freed without unmapping it, and a later map at that address fails with -EADDRINUSE. On kernels with that commit, iommu_dma_map_sg() returns -EOVERFLOW instead. In both cases a large userptr allocation fails to pin. On MI300X with the default DMA-FQ IOMMU domain, the first 32 GiB hipHostMalloc after boot built 16 segments of up to 3.7 GiB, mapped only 15.7 GiB and failed.

The padding is shorter than the next segment, so a padded segment still fits sg->length when no segment exceeds half of it. Limit both userptr sg tables to that, as xe does in commit 1b1d3710380d ("drm/xe: Apply upper limit to sg element size").

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c | 8 ++++----
 drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c          | 7 ++++---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h          | 7 +++++++
 3 files changed, 15 insertions(+), 7 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +4 -4
index f060b206c255..565ecd7e012a 100644
@@ -589,10 +589,10 @@ kfd_mem_dmamap_userptr(struct kgd_mem *mem,
 		return -ENOMEM;
 
 	/* Same sequence as in amdgpu_ttm_tt_pin_userptr */
-	ret = sg_alloc_table_from_pages(ttm->sg, src_ttm->pages,
-					ttm->num_pages, 0,
-					(u64)ttm->num_pages << PAGE_SHIFT,
-					GFP_KERNEL);
+	ret = sg_alloc_table_from_pages_segment(ttm->sg, src_ttm->pages,
+						ttm->num_pages, 0,
+						(u64)ttm->num_pages << PAGE_SHIFT,
+						AMDGPU_SG_MAX_SEGMENT, GFP_KERNEL);
 	if (unlikely(ret))
 		goto free_sg;
 
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +4 -3
index 8dfa92967b63..b0672f1747ee 100644
@@ -837,9 +837,10 @@ static int amdgpu_ttm_tt_pin_userptr(struct ttm_device *bdev,
 	int r;
 
 	/* Allocate an SG array and squash pages into it */
-	r = sg_alloc_table_from_pages(ttm->sg, ttm->pages, ttm->num_pages, 0,
-				      (u64)ttm->num_pages << PAGE_SHIFT,
-				      GFP_KERNEL);
+	r = sg_alloc_table_from_pages_segment(ttm->sg, ttm->pages,
+					      ttm->num_pages, 0,
+					      (u64)ttm->num_pages << PAGE_SHIFT,
+					      AMDGPU_SG_MAX_SEGMENT, GFP_KERNEL);
 	if (r)
 		goto release_sg;
 
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h +7 -0
index 9856a2fc2d08..da7d7621aad1 100644
@@ -44,6 +44,13 @@
 
 #define AMDGPU_GTT_MAX_TRANSFER_SIZE	(1ULL << 22)
 
+/*
+ * iommu_dma_map_sg() pads a segment so that the next one does not cross the
+ * DMA segment boundary. The padding is shorter than the next segment, so the
+ * padded length still fits sg->length if no segment exceeds half of it.
+ */
+#define AMDGPU_SG_MAX_SEGMENT	round_down(UINT_MAX / 2, PAGE_SIZE)
+
 extern const struct attribute_group amdgpu_vram_mgr_attr_group;
 extern const struct attribute_group amdgpu_gtt_mgr_attr_group;
 
Signature · 1 line
2.43.0
#5 lore raw

PATCH 3/4 drm/amdgpu: drop mmap_lock between chunks when faulting userptr pages

In reply to William Palacek

amdgpu_ttm_tt_get_user_pages() holds mmap_read_lock across hmm_range_fault() for the whole userptr range. Faulting in a 32 GiB range takes seconds, and every mmap_lock writer of the process (mmap, munmap, madvise, the KFD SVM flush at each alloc ioctl) waits that long.

Fault the range in 2 MiB chunks and drop mmap_read_lock between them when the caller passes its mm. A change to the range while the lock is dropped bumps the notifier sequence, which hmm_range_fault() checks before each chunk and callers check before using the pages. kfd_svm passes NULL and keeps holding the lock across the whole range.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.c | 14 ++++++++++++--
 drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.h |  2 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c |  2 +-
 drivers/gpu/drm/amd/amdkfd/kfd_svm.c    |  2 +-
 4 files changed, 15 insertions(+), 5 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.c +12 -2
index a7d13e337d84..9f80ffaa0607 100644
@@ -165,12 +165,17 @@ void amdgpu_hmm_unregister(struct amdgpu_bo *bo)
 	bo->notifier.mm = NULL;
 }
 
+/*
+ * The caller holds mmap_read_lock. If @mm is non-NULL it is dropped between
+ * 2 MiB chunks, so faulting a large range blocks mmap_lock writers for at
+ * most one chunk; the notifier sequence catches any change made meanwhile.
+ */
 int amdgpu_hmm_range_get_pages(struct mmu_interval_notifier *notifier,
 			       uint64_t start, uint64_t npages, bool readonly,
-			       void *owner,
+			       void *owner, struct mm_struct *mm,
 			       struct amdgpu_hmm_range *range)
 {
-	const u64 max_bytes = SZ_2G;
+	const u64 max_bytes = mm ? SZ_2M : SZ_2G;
 
 	struct hmm_range *hmm_range = &range->hmm_range;
 	unsigned long *pfns;
@@ -205,6 +210,11 @@ int amdgpu_hmm_range_get_pages(struct mmu_interval_notifier *notifier,
 
 		if (hmm_range->end == end)
 			break;
+		if (mm) {
+			mmap_read_unlock(mm);
+			cond_resched();
+			mmap_read_lock(mm);
+		}
 		hmm_range->hmm_pfns += max_bytes >> PAGE_SHIFT;
 		hmm_range->start = hmm_range->end;
 	} while (hmm_range->end < end);
drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.h +1 -1
index 140bc9cd57b4..4d1c96ad2704 100644
@@ -38,7 +38,7 @@ struct amdgpu_hmm_range {
 
 int amdgpu_hmm_range_get_pages(struct mmu_interval_notifier *notifier,
 			       uint64_t start, uint64_t npages, bool readonly,
-			       void *owner,
+			       void *owner, struct mm_struct *mm,
 			       struct amdgpu_hmm_range *range);
 
 #if defined(CONFIG_HMM_MIRROR)
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +1 -1
index b0672f1747ee..fae845632169 100644
@@ -793,7 +793,7 @@ int amdgpu_ttm_tt_get_user_pages(struct amdgpu_bo *bo,
 
 	readonly = amdgpu_ttm_tt_is_readonly(ttm);
 	r = amdgpu_hmm_range_get_pages(&bo->notifier, start, ttm->num_pages,
-				       readonly, NULL, range);
+				       readonly, NULL, mm, range);
 out_unlock:
 	mmap_read_unlock(mm);
 	if (r)
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +1 -1
index 3f32507e2aae..8d7de7bb39e0 100644
@@ -1892,7 +1892,7 @@ static int svm_range_validate_and_map(struct mm_struct *mm,
 			range = amdgpu_hmm_range_alloc(NULL);
 			if (likely(range))
 				r = amdgpu_hmm_range_get_pages(&prange->notifier, addr, npages,
-							       readonly, owner, range);
+							       readonly, owner, NULL, range);
 			else
 				r = -ENOMEM;
 			WRITE_ONCE(p->svms.faulting_task, NULL);
Signature · 1 line
2.43.0
#7 lore raw

PATCH 4/4 drm/amdkfd: register userptr pages without holding KFD process locks

In reply to William Palacek

Registering a userptr BO faults in its whole range while holding p->mutex (alloc ioctl) and process_info->lock (init_user_pages). For a 32 GiB hipHostMalloc this takes seconds, and other threads of the process block on both locks in their own alloc and map ioctls, stalling GPU submission on every stream.

Allocate the BO without p->mutex; it has no handle until the ioctl takes the lock again, so no other thread can reach it. In init_user_pages, fault in and validate the pages without process_info->lock, then add the BO to userptr_valid_list under process_info->lock and notifier_lock only if the range was not invalidated meanwhile. Otherwise return -EAGAIN, which user mode retries; an invalidation after that point is handled by the restore worker as before.

---
 .../gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c  | 45 ++++++++++++-------
 drivers/gpu/drm/amd/amdkfd/kfd_chardev.c      |  7 +++
 2 files changed, 35 insertions(+), 17 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +28 -17
index 565ecd7e012a..01a34ee5f316 100644
@@ -1062,12 +1062,12 @@ static void remove_kgd_mem_from_kfd_bo_list(struct kgd_mem *mem,
 /* Initializes user pages. It registers the MMU notifier and validates
  * the userptr BO in the GTT domain.
  *
- * The BO must already be on the userptr_valid_list. Otherwise an
- * eviction and restore may happen that leaves the new BO unmapped
- * with the user mode queues running.
- *
- * Takes the process_info->lock to protect against concurrent restore
- * workers.
+ * Pages are faulted in and validated without process_info->lock, so a
+ * large range does not block other threads of the process. The BO is
+ * added to the userptr_valid_list only if no invalidation happened since
+ * its pages were read; otherwise an eviction and restore could leave it
+ * stale with the user mode queues running. A lost race returns -EAGAIN
+ * and user mode retries the ioctl.
  *
  * Returns 0 for success, negative errno for errors.
  */
@@ -1080,19 +1080,17 @@ static int init_user_pages(struct kgd_mem *mem, uint64_t user_addr,
 	struct amdgpu_hmm_range *range;
 	int ret = 0;
 
-	mutex_lock(&process_info->lock);
-
 	ret = amdgpu_ttm_tt_set_userptr(&bo->tbo, user_addr, 0);
 	if (ret) {
 		pr_err("%s: Failed to set userptr: %d\n", __func__, ret);
-		goto out;
+		return ret;
 	}
 
 	ret = amdgpu_hmm_register(bo, user_addr);
 	if (ret) {
 		pr_err("%s: Failed to register MMU notifier: %d\n",
 		       __func__, ret);
-		goto out;
+		return ret;
 	}
 
 	if (criu_resume) {
@@ -1102,10 +1100,10 @@ static int init_user_pages(struct kgd_mem *mem, uint64_t user_addr,
 		 * later stage when it is scheduled by another ioctl called by
 		 * CRIU master process for the target pid for restore.
 		 */
+		add_kgd_mem_to_kfd_bo_list(mem, process_info, true);
 		mutex_lock(&process_info->notifier_lock);
 		mem->invalid++;
 		mutex_unlock(&process_info->notifier_lock);
-		mutex_unlock(&process_info->lock);
 		return 0;
 	}
 
@@ -1117,12 +1115,11 @@ static int init_user_pages(struct kgd_mem *mem, uint64_t user_addr,
 
 	ret = amdgpu_ttm_tt_get_user_pages(bo, range);
 	if (ret) {
-		amdgpu_hmm_range_free(range);
 		if (ret == -EAGAIN)
 			pr_debug("Failed to get user pages, try again\n");
 		else
 			pr_err("%s: Failed to get user pages: %d\n", __func__, ret);
-		goto unregister_out;
+		goto release_out;
 	}
 
 	ret = amdgpu_bo_reserve(bo, true);
@@ -1138,14 +1135,27 @@ static int init_user_pages(struct kgd_mem *mem, uint64_t user_addr,
 	if (ret)
 		pr_err("%s: failed to validate BO\n", __func__);
 	amdgpu_bo_unreserve(bo);
+	if (ret)
+		goto release_out;
+
+	mutex_lock(&process_info->lock);
+	mutex_lock(&process_info->notifier_lock);
+	if (amdgpu_hmm_range_valid(range)) {
+		/* Earlier invalidations completed before the pages were read */
+		mem->invalid = 0;
+		list_add_tail(&mem->validate_list,
+			      &process_info->userptr_valid_list);
+	} else {
+		ret = -EAGAIN;
+	}
+	mutex_unlock(&process_info->notifier_lock);
+	mutex_unlock(&process_info->lock);
 
 release_out:
 	amdgpu_hmm_range_free(range);
 unregister_out:
 	if (ret)
 		amdgpu_hmm_unregister(bo);
-out:
-	mutex_unlock(&process_info->lock);
 	return ret;
 }
 
@@ -1875,7 +1885,8 @@ int amdgpu_amdkfd_gpuvm_alloc_memory_of_gpu(
 	(*mem)->mapped_to_gpu_memory = 0;
 	(*mem)->process_info = avm->process_info;
 
-	add_kgd_mem_to_kfd_bo_list(*mem, avm->process_info, user_addr);
+	if (!user_addr)
+		add_kgd_mem_to_kfd_bo_list(*mem, avm->process_info, false);
 
 	if (user_addr) {
 		pr_debug("creating userptr BO for user_addr = %llx\n", user_addr);
@@ -1907,10 +1918,10 @@ int amdgpu_amdkfd_gpuvm_alloc_memory_of_gpu(
 
 	return 0;
 
-allocate_init_user_pages_failed:
 err_pin_bo:
 err_validate_bo:
 	remove_kgd_mem_from_kfd_bo_list(*mem, avm->process_info);
+allocate_init_user_pages_failed:
 	drm_gem_handle_delete(adev->kfd.client.file, (*mem)->gem_handle);
 err_gem_handle_create:
 	drm_vma_node_revoke(&gobj->vma_node, drm_priv);
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +7 -0
index 9886bbd28ea2..acd4dd0f2f06 100644
@@ -1228,10 +1228,17 @@ static int kfd_ioctl_alloc_memory_of_gpu(struct file *filep,
 		}
 	}
 
+	/*
+	 * Not under p->mutex: a userptr BO faults in its whole range here.
+	 * The BO has no handle yet, so nothing else can reach it, and pdd
+	 * lives until process teardown.
+	 */
+	mutex_unlock(&p->mutex);
 	err = amdgpu_amdkfd_gpuvm_alloc_memory_of_gpu(
 		dev->adev, args->va_addr, args->size,
 		pdd->drm_priv, (struct kgd_mem **) &mem, &offset,
 		flags, false);
+	mutex_lock(&p->mutex);
 
 	if (err)
 		goto err_unlock;
Signature · 1 line
2.43.0
#3 lore raw

PATCH 2/4 drm/amdkfd: don't take p->mutex in AMDKFD_IOC_GET_CLOCK_COUNTERS

In reply to William Palacek

The ioctl takes p->mutex only to look up the pdd. p->pdds[] is filled in create_process() before the process is published and freed only at teardown, and the clock read that follows already uses the pdd without the lock, so the lock protects nothing.

It does serialize every clock counter query of the process behind whatever holds p->mutex. Threads submitting GPU work call this ioctl (seen in a PyTorch workload on MI300X), so while one thread registered and mapped a 32 GiB userptr, the others waited in it (205 ms measured for one call).

---
 drivers/gpu/drm/amd/amdkfd/kfd_chardev.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +1 -2
index 3d2136adb62b..9886bbd28ea2 100644
@@ -745,9 +745,8 @@ static int kfd_ioctl_get_clock_counters(struct file *filep,
 	struct kfd_ioctl_get_clock_counters_args *args = data;
 	struct kfd_process_device *pdd;
 
-	mutex_lock(&p->mutex);
+	/* p->pdds[] is fixed from process creation to teardown, no lock needed */
 	pdd = kfd_process_device_data_by_id(p, args->gpu_id);
-	mutex_unlock(&p->mutex);
 	if (pdd)
 		/* Reading GPU clock counter from KGD */
 		args->gpu_clock_counter = amdgpu_amdkfd_get_gpu_clock_counter(pdd->dev->adev);
Signature · 1 line
2.43.0
maintainer
#8 lore raw
In reply to William Palacek
Quoted 14 lines · William Palacek

Registering a large userptr BO (hipHostMalloc) holds mmap_lock, p->mutex and process_info->lock while every page of the range is faulted in. For 32 GiB on MI300X that takes seconds, and every other thread of the process that submits GPU work waits behind it for the whole time.

Patch 1 limits userptr sg segments to half of sg->length. Larger physically contiguous segments can overflow the padded segment length in iommu_dma_map_sg(), and the pin fails. This already happens without the series; patch 3 makes the pages contiguous enough that it hits on a freshly booted system, so it goes first.

Patch 2 drops p->mutex from AMDKFD_IOC_GET_CLOCK_COUNTERS, which other threads call while submitting work.

Quoted 6 lines

Patch 3 drops mmap_lock between 2 MiB chunks while faulting userptr pages in.

Patch 4 faults in and validates userptr pages without p->mutex and process_info->lock, and adds the BO to the valid list only if no invalidation happened meanwhile.

Absolutely clear NAK to the whole approach! Userptrs are fragile enough without this.

That the performance of userptrs isn't the best is expected and won't be fixed.

Regards,
Christian.

Quoted 31 lines

Tested with two 32 GiB hipHostMalloc/hipHostFree cycles next to 4 PyTorch streams on 2 GPUs, measuring the worst GPU submission latency of the other threads (limit 1 s), on the amdgpu 7.2 DKMS driver:

MI300X, IOMMU DMA-FQ unpatched 0/3 (6.1-6.2 s; on other boots
the 32 GiB pin itself fails)
series 3/3 (0.80-0.95 s)

  MI300X, IOMMU identity   unpatched    0/3 (9.6-10.1 s)
                           patches 2-3  0/3 (10.5-11.0 s)
                           patches 2-4  3/3 (0.81-0.93 s)
  MI350X, IOMMU identity   unpatched    0/4 (4.55-4.84 s)
                           series       7/7 (0.21-0.39 s)

kfdtest KFDMemoryTest.* and KFDEvictTest.* pass on both GPUs.

William Palacek (4):
drm/amdgpu: limit userptr sg segments to half of sg->length drm/amdkfd: don't take p->mutex in AMDKFD_IOC_GET_CLOCK_COUNTERS drm/amdgpu: drop mmap_lock between chunks when faulting userptr pages drm/amdkfd: register userptr pages without holding KFD process locks

 .../gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c  | 53 +++++++++++--------
 drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.c       | 14 ++++-
 drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.h       |  2 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c       |  9 ++--
 drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h       |  7 +++
 drivers/gpu/drm/amd/amdkfd/kfd_chardev.c      | 10 +++-
 drivers/gpu/drm/amd/amdkfd/kfd_svm.c          |  2 +-
 7 files changed, 66 insertions(+), 31 deletions(-)
maintainer
#2 lore raw

Re: [PATCH 1/4] drm/amdgpu: limit userptr sg segments to half of sg->length

In reply to William Palacek
Quoted 9 lines · William Palacek

Userptr sg tables are built with sg_alloc_table_from_pages(), which merges physically contiguous pages into segments of up to 0xfffff000 bytes. iommu_dma_map_sg() pads a segment so that the next one does not cross the DMA segment boundary, and adds the padding to the 32-bit sg->length. With two large contiguous segments the padded length reaches 4 GiB and wraps to 0.

On kernels without commit 1c8f6308e557 ("iommu/dma: Catch scatterlist length overflows") the wrapped length makes iommu_map_sg() map less

I recommend you don't mention upstream bugs that are already fixed, since this commit is meant for upstream. Your patch seems to be applicable regardless.

Quoted 10 lines

than the allocated IOVA range. The IOVA is then freed without unmapping it, and a later map at that address fails with -EADDRINUSE. On kernels with that commit, iommu_dma_map_sg() returns -EOVERFLOW instead. In both cases a large userptr allocation fails to pin. On MI300X with the default DMA-FQ IOMMU domain, the first 32 GiB hipHostMalloc after boot built 16 segments of up to 3.7 GiB, mapped only 15.7 GiB and failed.

The padding is shorter than the next segment, so a padded segment still fits sg->length when no segment exceeds half of it.

This statement doesn't make sense. sg->length is per scatterlist entry. Do you mean "no unpadded segment exceeds half of the 4GB maximum size"?

The commit headline is equally confusing and should probably also be reworded.

Quoted 3 lines

Limit both
userptr sg tables to that, as xe does in commit 1b1d3710380d ("drm/xe: Apply upper limit to sg element size").

The XE commit also change a call to dma_set_max_seg_size. You're leaving the call in amdgpu_ttm_init unchanged with UINT_MAX. Is that an oversight?

Regards,
  Felix

Quoted 63 lines
---
  drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c | 8 ++++----
  drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c          | 7 ++++---
  drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h          | 7 +++++++
  3 files changed, 15 insertions(+), 7 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +4 -4
index f060b206c255..565ecd7e012a 100644
@@ -589,10 +589,10 @@ kfd_mem_dmamap_userptr(struct kgd_mem *mem,
  		return -ENOMEM;
 
  	/* Same sequence as in amdgpu_ttm_tt_pin_userptr */
-	ret = sg_alloc_table_from_pages(ttm->sg, src_ttm->pages,
-					ttm->num_pages, 0,
-					(u64)ttm->num_pages << PAGE_SHIFT,
-					GFP_KERNEL);
+	ret = sg_alloc_table_from_pages_segment(ttm->sg, src_ttm->pages,
+						ttm->num_pages, 0,
+						(u64)ttm->num_pages << PAGE_SHIFT,
+						AMDGPU_SG_MAX_SEGMENT, GFP_KERNEL);
  	if (unlikely(ret))
  		goto free_sg;
 
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +4 -3
index 8dfa92967b63..b0672f1747ee 100644
@@ -837,9 +837,10 @@ static int amdgpu_ttm_tt_pin_userptr(struct ttm_device *bdev,
  	int r;
 
  	/* Allocate an SG array and squash pages into it */
-	r = sg_alloc_table_from_pages(ttm->sg, ttm->pages, ttm->num_pages, 0,
-				      (u64)ttm->num_pages << PAGE_SHIFT,
-				      GFP_KERNEL);
+	r = sg_alloc_table_from_pages_segment(ttm->sg, ttm->pages,
+					      ttm->num_pages, 0,
+					      (u64)ttm->num_pages << PAGE_SHIFT,
+					      AMDGPU_SG_MAX_SEGMENT, GFP_KERNEL);
  	if (r)
  		goto release_sg;
 
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h +7 -0
index 9856a2fc2d08..da7d7621aad1 100644
@@ -44,6 +44,13 @@
 
  #define AMDGPU_GTT_MAX_TRANSFER_SIZE	(1ULL << 22)
 
+/*
+ * iommu_dma_map_sg() pads a segment so that the next one does not cross the
+ * DMA segment boundary. The padding is shorter than the next segment, so the
+ * padded length still fits sg->length if no segment exceeds half of it.
+ */
+#define AMDGPU_SG_MAX_SEGMENT	round_down(UINT_MAX / 2, PAGE_SIZE)
+
  extern const struct attribute_group amdgpu_vram_mgr_attr_group;
  extern const struct attribute_group amdgpu_gtt_mgr_attr_group;
 
maintainer
#4 lore raw

Re: [PATCH 2/4] drm/amdkfd: don't take p->mutex in AMDKFD_IOC_GET_CLOCK_COUNTERS

In reply to William Palacek
Quoted 4 lines · William Palacek

The ioctl takes p->mutex only to look up the pdd. p->pdds[] is filled in create_process() before the process is published and freed only at teardown, and the clock read that follows already uses the pdd without the lock, so the lock protects nothing.

That's not completely true. kfd_process_device_data_by_id compares the gpu_id with pdd->user_gpu_id, which is updated under the p->mutex by criu_restore.

Regards,
  Felix

Quoted 27 lines

It does serialize every clock counter query of the process behind whatever holds p->mutex. Threads submitting GPU work call this ioctl (seen in a PyTorch workload on MI300X), so while one thread registered and mapped a 32 GiB userptr, the others waited in it (205 ms measured for one call).

---
  drivers/gpu/drm/amd/amdkfd/kfd_chardev.c | 3 +--
  1 file changed, 1 insertion(+), 2 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +1 -2
index 3d2136adb62b..9886bbd28ea2 100644
@@ -745,9 +745,8 @@ static int kfd_ioctl_get_clock_counters(struct file *filep,
  	struct kfd_ioctl_get_clock_counters_args *args = data;
  	struct kfd_process_device *pdd;
 
-	mutex_lock(&p->mutex);
+	/* p->pdds[] is fixed from process creation to teardown, no lock needed */
  	pdd = kfd_process_device_data_by_id(p, args->gpu_id);
-	mutex_unlock(&p->mutex);
  	if (pdd)
  		/* Reading GPU clock counter from KGD */
  		args->gpu_clock_counter = amdgpu_amdkfd_get_gpu_clock_counter(pdd->dev->adev);
maintainer
#6 lore raw

Re: [PATCH 3/4] drm/amdgpu: drop mmap_lock between chunks when faulting userptr pages

In reply to William Palacek
Quoted 10 lines · William Palacek

amdgpu_ttm_tt_get_user_pages() holds mmap_read_lock across hmm_range_fault() for the whole userptr range. Faulting in a 32 GiB range takes seconds, and every mmap_lock writer of the process (mmap, munmap, madvise, the KFD SVM flush at each alloc ioctl) waits that long.

Fault the range in 2 MiB chunks and drop mmap_read_lock between them when the caller passes its mm. A change to the range while the lock is dropped bumps the notifier sequence, which hmm_range_fault() checks before each chunk and callers check before using the pages. kfd_svm passes NULL and keeps holding the lock across the whole range.

I agree with Christian, that this is the wrong approach. I think the root of the problem is that we're misusing the mmap write lock in the KFD SVM call, and that's the reason that those functions get stuck behind mmap read locks.

Another simple lever we have is max_svm_range_pages. That's currently clamped to 1GB in svm_range_set_max_pages. But it could be made smaller.

Regards,
  Felix

Quoted 84 lines
---
  drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.c | 14 ++++++++++++--
  drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.h |  2 +-
  drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c |  2 +-
  drivers/gpu/drm/amd/amdkfd/kfd_svm.c    |  2 +-
  4 files changed, 15 insertions(+), 5 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.c +12 -2
index a7d13e337d84..9f80ffaa0607 100644
@@ -165,12 +165,17 @@ void amdgpu_hmm_unregister(struct amdgpu_bo *bo)
  	bo->notifier.mm = NULL;
  }
 
+/*
+ * The caller holds mmap_read_lock. If @mm is non-NULL it is dropped between
+ * 2 MiB chunks, so faulting a large range blocks mmap_lock writers for at
+ * most one chunk; the notifier sequence catches any change made meanwhile.
+ */
  int amdgpu_hmm_range_get_pages(struct mmu_interval_notifier *notifier,
  			       uint64_t start, uint64_t npages, bool readonly,
-			       void *owner,
+			       void *owner, struct mm_struct *mm,
  			       struct amdgpu_hmm_range *range)
  {
-	const u64 max_bytes = SZ_2G;
+	const u64 max_bytes = mm ? SZ_2M : SZ_2G;
 
  	struct hmm_range *hmm_range = &range->hmm_range;
  	unsigned long *pfns;
@@ -205,6 +210,11 @@ int amdgpu_hmm_range_get_pages(struct mmu_interval_notifier *notifier,
 
  		if (hmm_range->end == end)
  			break;
+		if (mm) {
+			mmap_read_unlock(mm);
+			cond_resched();
+			mmap_read_lock(mm);
+		}
  		hmm_range->hmm_pfns += max_bytes >> PAGE_SHIFT;
  		hmm_range->start = hmm_range->end;
  	} while (hmm_range->end < end);
drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.h +1 -1
index 140bc9cd57b4..4d1c96ad2704 100644
@@ -38,7 +38,7 @@ struct amdgpu_hmm_range {
 
  int amdgpu_hmm_range_get_pages(struct mmu_interval_notifier *notifier,
  			       uint64_t start, uint64_t npages, bool readonly,
-			       void *owner,
+			       void *owner, struct mm_struct *mm,
  			       struct amdgpu_hmm_range *range);
 
  #if defined(CONFIG_HMM_MIRROR)
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +1 -1
index b0672f1747ee..fae845632169 100644
@@ -793,7 +793,7 @@ int amdgpu_ttm_tt_get_user_pages(struct amdgpu_bo *bo,
 
  	readonly = amdgpu_ttm_tt_is_readonly(ttm);
  	r = amdgpu_hmm_range_get_pages(&bo->notifier, start, ttm->num_pages,
-				       readonly, NULL, range);
+				       readonly, NULL, mm, range);
  out_unlock:
  	mmap_read_unlock(mm);
  	if (r)
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +1 -1
index 3f32507e2aae..8d7de7bb39e0 100644
@@ -1892,7 +1892,7 @@ static int svm_range_validate_and_map(struct mm_struct *mm,
  			range = amdgpu_hmm_range_alloc(NULL);
  			if (likely(range))
  				r = amdgpu_hmm_range_get_pages(&prange->notifier, addr, npages,
-							       readonly, owner, range);
+							       readonly, owner, NULL, range);
  			else
  				r = -ENOMEM;
  			WRITE_ONCE(p->svms.faulting_task, NULL);