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.

Signed-off-by: William Palacek <[email protected]>
---
 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(-)

diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.c 
b/drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.c
index a7d13e337d84..9f80ffaa0607 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.c
@@ -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);
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.h 
b/drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.h
index 140bc9cd57b4..4d1c96ad2704 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.h
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.h
@@ -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)
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c 
b/drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c
index b0672f1747ee..fae845632169 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c
@@ -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)
diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_svm.c 
b/drivers/gpu/drm/amd/amdkfd/kfd_svm.c
index 3f32507e2aae..8d7de7bb39e0 100644
--- a/drivers/gpu/drm/amd/amdkfd/kfd_svm.c
+++ b/drivers/gpu/drm/amd/amdkfd/kfd_svm.c
@@ -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);
-- 
2.43.0

Reply via email to