Skip to contentSkip to conversation

Userptr memory mapping support for KFD

PATCH In review Felix Kuehling 4 participants 16 messages Mar 23, 2018 – May 14, 2018 8 messages not in the archive

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

cover Userptr memory mapping support for KFD

Update of remaining patches from the GPUVM patch series. This should apply on top of the fixes I just sent out.

Felix Kuehling (6):
drm/amdgpu: Add MMU notifier type for KFD userptr drm/amdgpu: Enable amdgpu_ttm_tt_get_user_pages in worker threads drm/amdgpu: Avoid reclaim while holding locks taken in MMU notifier drm/amdkfd: GFP_NOIO while holding locks taken in MMU notifier drm/amdkfd: Add quiesce_mm and resume_mm to kgd2kfd_calls drm/amdgpu: Add userptr support for KFD

 drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h       |  12 +
 drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c | 572 ++++++++++++++++++++++-
 drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c           |   2 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_mn.c           | 111 ++++-
 drivers/gpu/drm/amd/amdgpu/amdgpu_mn.h           |  11 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c          |  38 +-
 drivers/gpu/drm/amd/amdkfd/kfd_device.c          |  40 +-
 drivers/gpu/drm/amd/amdkfd/kfd_module.c          |   2 +
 drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_cik.c |   2 +-
 drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_vi.c  |   2 +-
 drivers/gpu/drm/amd/amdkfd/kfd_priv.h            |   4 +
 drivers/gpu/drm/amd/amdkfd/kfd_process.c         |  10 +-
 drivers/gpu/drm/amd/include/kgd_kfd_interface.h  |   6 +
 13 files changed, 746 insertions(+), 66 deletions(-)
Signature · 6 lines
2.7.4

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

A message that is not in the archive (sent off-list or not yet received): 1521833553-31571-1-git-send-email-Felix.Kuehling-5C7GfCeVMHo@public.gmane.org

#2 lore raw

PATCH 1/6 drm/amdgpu: Add MMU notifier type for KFD userptr

This commit adds the notion of MMU notifier types GFX and HSA. GFX continues to work like MMU notifiers did before. HSA adds support for KFD userptr BOs. The implementation of KFD userptr eviction is a stub for now.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h       |  1 +
 drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c |  7 ++
 drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c           |  2 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_mn.c           | 94 ++++++++++++++++++++----
 drivers/gpu/drm/amd/amdgpu/amdgpu_mn.h           | 11 ++-
 5 files changed, 97 insertions(+), 18 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h +1 -0
index c2c2bea..83e0c5c 100644
@@ -104,6 +104,7 @@ void amdgpu_amdkfd_device_probe(struct amdgpu_device *adev);
 void amdgpu_amdkfd_device_init(struct amdgpu_device *adev);
 void amdgpu_amdkfd_device_fini(struct amdgpu_device *adev);
 
+int amdgpu_amdkfd_evict_userptr(struct kgd_mem *mem, struct mm_struct *mm);
 int amdgpu_amdkfd_submit_ib(struct kgd_dev *kgd, enum kgd_engine_type engine,
 				uint32_t vmid, uint64_t gpu_addr,
 				uint32_t *ib_cmd, uint32_t ib_len);
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +7 -0
index 2f42c60..2d6f13a 100644
@@ -1418,6 +1418,13 @@ int amdgpu_amdkfd_gpuvm_map_gtt_bo_to_kernel(struct kgd_dev *kgd,
 	return ret;
 }
 
+int amdgpu_amdkfd_evict_userptr(struct kgd_mem *mem,
+				struct mm_struct *mm)
+{
+	/* TODO */
+	return 0;
+}
+
 /** amdgpu_amdkfd_gpuvm_restore_process_bos - Restore all BOs for the given
  *   KFD process identified by process_info
  *
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +1 -1
index dc34b50..8e66f37 100644
@@ -536,7 +536,7 @@ static int amdgpu_cs_parser_bos(struct amdgpu_cs_parser *p,
 	if (p->bo_list) {
 		amdgpu_bo_list_get_list(p->bo_list, &p->validated);
 		if (p->bo_list->first_userptr != p->bo_list->num_entries)
-			p->mn = amdgpu_mn_get(p->adev);
+			p->mn = amdgpu_mn_get(p->adev, AMDGPU_MN_TYPE_GFX);
 	}
 
 	INIT_LIST_HEAD(&duplicates);
drivers/gpu/drm/amd/amdgpu/amdgpu_mn.c +79 -15

172 lines not on this page · raw message

drivers/gpu/drm/amd/amdgpu/amdgpu_mn.h +9 -2
index d0095a3..eb0f432 100644
@@ -29,16 +29,23 @@
  */
 struct amdgpu_mn;
 
+enum amdgpu_mn_type {
+	AMDGPU_MN_TYPE_GFX,
+	AMDGPU_MN_TYPE_HSA,
+};
+
 #if defined(CONFIG_MMU_NOTIFIER)
 void amdgpu_mn_lock(struct amdgpu_mn *mn);
 void amdgpu_mn_unlock(struct amdgpu_mn *mn);
-struct amdgpu_mn *amdgpu_mn_get(struct amdgpu_device *adev);
+struct amdgpu_mn *amdgpu_mn_get(struct amdgpu_device *adev,
+				enum amdgpu_mn_type type);
 int amdgpu_mn_register(struct amdgpu_bo *bo, unsigned long addr);
 void amdgpu_mn_unregister(struct amdgpu_bo *bo);
 #else
 static inline void amdgpu_mn_lock(struct amdgpu_mn *mn) {}
 static inline void amdgpu_mn_unlock(struct amdgpu_mn *mn) {}
-static inline struct amdgpu_mn *amdgpu_mn_get(struct amdgpu_device *adev)
+static inline struct amdgpu_mn *amdgpu_mn_get(struct amdgpu_device *adev,
+					      enum amdgpu_mn_type type)
 {
 	return NULL;
 }
Signature · 6 lines
2.7.4

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx
#3 lore raw

PATCH 2/6 drm/amdgpu: Enable amdgpu_ttm_tt_get_user_pages in worker threads

In reply to a message not in the archive

This commit allows amdgpu_ttm_tt_get_user_pages to work in a worker thread rather than regular process context. This will be used when KFD userptr BOs are restored after an MMU-notifier eviction.

v2: Manage task reference with get_task_struct/put_task_struct

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c | 38 +++++++++++++++++++++++++--------
 1 file changed, 29 insertions(+), 9 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +29 -9
index c2fae04..25490fe 100644
@@ -689,7 +689,7 @@ struct amdgpu_ttm_tt {
 	struct ttm_dma_tt	ttm;
 	u64			offset;
 	uint64_t		userptr;
-	struct mm_struct	*usermm;
+	struct task_struct	*usertask;
 	uint32_t		userflags;
 	spinlock_t              guptasklock;
 	struct list_head        guptasks;
@@ -700,14 +700,18 @@ struct amdgpu_ttm_tt {
 int amdgpu_ttm_tt_get_user_pages(struct ttm_tt *ttm, struct page **pages)
 {
 	struct amdgpu_ttm_tt *gtt = (void *)ttm;
+	struct mm_struct *mm = gtt->usertask->mm;
 	unsigned int flags = 0;
 	unsigned pinned = 0;
 	int r;
 
+	if (!mm) /* Happens during process shutdown */
+		return -ESRCH;
+
 	if (!(gtt->userflags & AMDGPU_GEM_USERPTR_READONLY))
 		flags |= FOLL_WRITE;
 
-	down_read(&current->mm->mmap_sem);
+	down_read(&mm->mmap_sem);
 
 	if (gtt->userflags & AMDGPU_GEM_USERPTR_ANONONLY) {
 		/* check that we only use anonymous memory
@@ -715,9 +719,9 @@ int amdgpu_ttm_tt_get_user_pages(struct ttm_tt *ttm, struct page **pages)
 		unsigned long end = gtt->userptr + ttm->num_pages * PAGE_SIZE;
 		struct vm_area_struct *vma;
 
-		vma = find_vma(gtt->usermm, gtt->userptr);
+		vma = find_vma(mm, gtt->userptr);
 		if (!vma || vma->vm_file || vma->vm_end < end) {
-			up_read(&current->mm->mmap_sem);
+			up_read(&mm->mmap_sem);
 			return -EPERM;
 		}
 	}
@@ -733,7 +737,12 @@ int amdgpu_ttm_tt_get_user_pages(struct ttm_tt *ttm, struct page **pages)
 		list_add(&guptask.list, &gtt->guptasks);
 		spin_unlock(&gtt->guptasklock);
 
-		r = get_user_pages(userptr, num_pages, flags, p, NULL);
+		if (mm == current->mm)
+			r = get_user_pages(userptr, num_pages, flags, p, NULL);
+		else
+			r = get_user_pages_remote(gtt->usertask,
+					mm, userptr, num_pages,
+					flags, p, NULL, NULL);
 
 		spin_lock(&gtt->guptasklock);
 		list_del(&guptask.list);
@@ -746,12 +755,12 @@ int amdgpu_ttm_tt_get_user_pages(struct ttm_tt *ttm, struct page **pages)
 
 	} while (pinned < ttm->num_pages);
 
-	up_read(&current->mm->mmap_sem);
+	up_read(&mm->mmap_sem);
 	return 0;
 
 release_pages:
 	release_pages(pages, pinned);
-	up_read(&current->mm->mmap_sem);
+	up_read(&mm->mmap_sem);
 	return r;
 }
 
@@ -972,6 +981,9 @@ static void amdgpu_ttm_backend_destroy(struct ttm_tt *ttm)
 {
 	struct amdgpu_ttm_tt *gtt = (void *)ttm;
 
+	if (gtt->usertask)
+		put_task_struct(gtt->usertask);
+
 	ttm_dma_tt_fini(&gtt->ttm);
 	kfree(gtt);
 }
@@ -1072,8 +1084,13 @@ int amdgpu_ttm_tt_set_userptr(struct ttm_tt *ttm, uint64_t addr,
 		return -EINVAL;
 
 	gtt->userptr = addr;
-	gtt->usermm = current->mm;
 	gtt->userflags = flags;
+
+	if (gtt->usertask)
+		put_task_struct(gtt->usertask);
+	gtt->usertask = current->group_leader;
+	get_task_struct(gtt->usertask);
+
 	spin_lock_init(&gtt->guptasklock);
 	INIT_LIST_HEAD(&gtt->guptasks);
 	atomic_set(&gtt->mmu_invalidations, 0);
@@ -1089,7 +1106,10 @@ struct mm_struct *amdgpu_ttm_tt_get_usermm(struct ttm_tt *ttm)
 	if (gtt == NULL)
 		return NULL;
 
-	return gtt->usermm;
+	if (gtt->usertask == NULL)
+		return NULL;
+
+	return gtt->usertask->mm;
 }
 
 bool amdgpu_ttm_tt_affect_userptr(struct ttm_tt *ttm, unsigned long start,
Signature · 6 lines
2.7.4

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

A message that is not in the archive (sent off-list or not yet received): 1521833553-31571-3-git-send-email-Felix.Kuehling-5C7GfCeVMHo@public.gmane.org

#5 lore raw

Re: [PATCH 2/6] drm/amdgpu: Enable amdgpu_ttm_tt_get_user_pages in worker threads

Hi Felix,
The patch looks fine to me and I can ACK it, but I would prefer that Alex or Christian will review it as well before pushing it. Thanks,
Oded

Quoted 123 lines · Felix Kuehling

This commit allows amdgpu_ttm_tt_get_user_pages to work in a worker thread rather than regular process context. This will be used when KFD userptr BOs are restored after an MMU-notifier eviction.

v2: Manage task reference with get_task_struct/put_task_struct

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c | 38 +++++++++++++++++++++++++--------
 1 file changed, 29 insertions(+), 9 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +29 -10
index c2fae04..25490fe 100644
@@ -689,7 +689,7 @@ struct amdgpu_ttm_tt {
        struct ttm_dma_tt       ttm;
        u64                     offset;
        uint64_t                userptr;
-       struct mm_struct        *usermm;
+       struct task_struct      *usertask;
        uint32_t                userflags;
        spinlock_t              guptasklock;
        struct list_head        guptasks;
@@ -700,14 +700,18 @@ struct amdgpu_ttm_tt {
 int amdgpu_ttm_tt_get_user_pages(struct ttm_tt *ttm, struct page **pages)
 {
        struct amdgpu_ttm_tt *gtt = (void *)ttm;
+       struct mm_struct *mm = gtt->usertask->mm;
        unsigned int flags = 0;
        unsigned pinned = 0;
        int r;
 
+       if (!mm) /* Happens during process shutdown */
+               return -ESRCH;
+
        if (!(gtt->userflags & AMDGPU_GEM_USERPTR_READONLY))
                flags |= FOLL_WRITE;
 
-       down_read(&current->mm->mmap_sem);
+       down_read(&mm->mmap_sem);
 
        if (gtt->userflags & AMDGPU_GEM_USERPTR_ANONONLY) {
                /* check that we only use anonymous memory
@@ -715,9 +719,9 @@ int amdgpu_ttm_tt_get_user_pages(struct ttm_tt *ttm, struct page **pages)
                unsigned long end = gtt->userptr + ttm->num_pages * PAGE_SIZE;
                struct vm_area_struct *vma;
 
-               vma = find_vma(gtt->usermm, gtt->userptr);
+               vma = find_vma(mm, gtt->userptr);
                if (!vma || vma->vm_file || vma->vm_end < end) {
-                       up_read(&current->mm->mmap_sem);
+                       up_read(&mm->mmap_sem);
                        return -EPERM;
                }
        }
@@ -733,7 +737,12 @@ int amdgpu_ttm_tt_get_user_pages(struct ttm_tt *ttm, struct page **pages)
                list_add(&guptask.list, &gtt->guptasks);
                spin_unlock(&gtt->guptasklock);
 
-               r = get_user_pages(userptr, num_pages, flags, p, NULL);
+               if (mm == current->mm)
+                       r = get_user_pages(userptr, num_pages, flags, p, NULL);
+               else
+                       r = get_user_pages_remote(gtt->usertask,
+                                       mm, userptr, num_pages,
+                                       flags, p, NULL, NULL);
 
                spin_lock(&gtt->guptasklock);
                list_del(&guptask.list);
@@ -746,12 +755,12 @@ int amdgpu_ttm_tt_get_user_pages(struct ttm_tt *ttm, struct page **pages)
 
        } while (pinned < ttm->num_pages);
 
-       up_read(&current->mm->mmap_sem);
+       up_read(&mm->mmap_sem);
        return 0;
 
 release_pages:
        release_pages(pages, pinned);
-       up_read(&current->mm->mmap_sem);
+       up_read(&mm->mmap_sem);
        return r;
 }
 
@@ -972,6 +981,9 @@ static void amdgpu_ttm_backend_destroy(struct ttm_tt *ttm)
 {
        struct amdgpu_ttm_tt *gtt = (void *)ttm;
 
+       if (gtt->usertask)
+               put_task_struct(gtt->usertask);
+
        ttm_dma_tt_fini(&gtt->ttm);
        kfree(gtt);
 }
@@ -1072,8 +1084,13 @@ int amdgpu_ttm_tt_set_userptr(struct ttm_tt *ttm, uint64_t addr,
                return -EINVAL;
 
        gtt->userptr = addr;
-       gtt->usermm = current->mm;
        gtt->userflags = flags;
+
+       if (gtt->usertask)
+               put_task_struct(gtt->usertask);
+       gtt->usertask = current->group_leader;
+       get_task_struct(gtt->usertask);
+
        spin_lock_init(&gtt->guptasklock);
        INIT_LIST_HEAD(&gtt->guptasks);
        atomic_set(&gtt->mmu_invalidations, 0);
@@ -1089,7 +1106,10 @@ struct mm_struct *amdgpu_ttm_tt_get_usermm(struct ttm_tt *ttm)
        if (gtt == NULL)
                return NULL;
 
-       return gtt->usermm;
+       if (gtt->usertask == NULL)
+               return NULL;
+
+       return gtt->usertask->mm;
 }
 
 bool amdgpu_ttm_tt_affect_userptr(struct ttm_tt *ttm, unsigned long start,
--
Quoted 2 lines

2.7.4

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

A message that is not in the archive (sent off-list or not yet received): CAFCwf114qcOrKVOrt0JvxShvXdA8tdAKDCSHDK0DAJjM5rAQjg-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org

#7 lore raw

Re: [PATCH 2/6] drm/amdgpu: Enable amdgpu_ttm_tt_get_user_pages in worker threads

Adding Alex, Christian

Quoted 128 lines · Oded Gabbay

Hi Felix,
The patch looks fine to me and I can ACK it, but I would prefer that Alex or Christian will review it as well before pushing it. Thanks,
Oded

Quoted 121 lines · Felix Kuehling

This commit allows amdgpu_ttm_tt_get_user_pages to work in a worker thread rather than regular process context. This will be used when KFD userptr BOs are restored after an MMU-notifier eviction.

v2: Manage task reference with get_task_struct/put_task_struct

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c | 38 +++++++++++++++++++++++++--------
 1 file changed, 29 insertions(+), 9 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +29 -9
index c2fae04..25490fe 100644
@@ -689,7 +689,7 @@ struct amdgpu_ttm_tt {
        struct ttm_dma_tt       ttm;
        u64                     offset;
        uint64_t                userptr;
-       struct mm_struct        *usermm;
+       struct task_struct      *usertask;
        uint32_t                userflags;
        spinlock_t              guptasklock;
        struct list_head        guptasks;
@@ -700,14 +700,18 @@ struct amdgpu_ttm_tt {
 int amdgpu_ttm_tt_get_user_pages(struct ttm_tt *ttm, struct page **pages)
 {
        struct amdgpu_ttm_tt *gtt = (void *)ttm;
+       struct mm_struct *mm = gtt->usertask->mm;
        unsigned int flags = 0;
        unsigned pinned = 0;
        int r;
 
+       if (!mm) /* Happens during process shutdown */
+               return -ESRCH;
+
        if (!(gtt->userflags & AMDGPU_GEM_USERPTR_READONLY))
                flags |= FOLL_WRITE;
 
-       down_read(&current->mm->mmap_sem);
+       down_read(&mm->mmap_sem);
 
        if (gtt->userflags & AMDGPU_GEM_USERPTR_ANONONLY) {
                /* check that we only use anonymous memory
@@ -715,9 +719,9 @@ int amdgpu_ttm_tt_get_user_pages(struct ttm_tt *ttm, struct page **pages)
                unsigned long end = gtt->userptr + ttm->num_pages * PAGE_SIZE;
                struct vm_area_struct *vma;
 
-               vma = find_vma(gtt->usermm, gtt->userptr);
+               vma = find_vma(mm, gtt->userptr);
                if (!vma || vma->vm_file || vma->vm_end < end) {
-                       up_read(&current->mm->mmap_sem);
+                       up_read(&mm->mmap_sem);
                        return -EPERM;
                }
        }
@@ -733,7 +737,12 @@ int amdgpu_ttm_tt_get_user_pages(struct ttm_tt *ttm, struct page **pages)
                list_add(&guptask.list, &gtt->guptasks);
                spin_unlock(&gtt->guptasklock);
 
-               r = get_user_pages(userptr, num_pages, flags, p, NULL);
+               if (mm == current->mm)
+                       r = get_user_pages(userptr, num_pages, flags, p, NULL);
+               else
+                       r = get_user_pages_remote(gtt->usertask,
+                                       mm, userptr, num_pages,
+                                       flags, p, NULL, NULL);
 
                spin_lock(&gtt->guptasklock);
                list_del(&guptask.list);
@@ -746,12 +755,12 @@ int amdgpu_ttm_tt_get_user_pages(struct ttm_tt *ttm, struct page **pages)
 
        } while (pinned < ttm->num_pages);
 
-       up_read(&current->mm->mmap_sem);
+       up_read(&mm->mmap_sem);
        return 0;
 
 release_pages:
        release_pages(pages, pinned);
-       up_read(&current->mm->mmap_sem);
+       up_read(&mm->mmap_sem);
        return r;
 }
 
@@ -972,6 +981,9 @@ static void amdgpu_ttm_backend_destroy(struct ttm_tt *ttm)
 {
        struct amdgpu_ttm_tt *gtt = (void *)ttm;
 
+       if (gtt->usertask)
+               put_task_struct(gtt->usertask);
+
        ttm_dma_tt_fini(&gtt->ttm);
        kfree(gtt);
 }
@@ -1072,8 +1084,13 @@ int amdgpu_ttm_tt_set_userptr(struct ttm_tt *ttm, uint64_t addr,
                return -EINVAL;
 
        gtt->userptr = addr;
-       gtt->usermm = current->mm;
        gtt->userflags = flags;
+
+       if (gtt->usertask)
+               put_task_struct(gtt->usertask);
+       gtt->usertask = current->group_leader;
+       get_task_struct(gtt->usertask);
+
        spin_lock_init(&gtt->guptasklock);
        INIT_LIST_HEAD(&gtt->guptasks);
        atomic_set(&gtt->mmu_invalidations, 0);
@@ -1089,7 +1106,10 @@ struct mm_struct *amdgpu_ttm_tt_get_usermm(struct ttm_tt *ttm)
        if (gtt == NULL)
                return NULL;
 
-       return gtt->usermm;
+       if (gtt->usertask == NULL)
+               return NULL;
+
+       return gtt->usertask->mm;
 }
 
Quoted 4 lines
Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +0 -1
 bool amdgpu_ttm_tt_affect_userptr(struct ttm_tt *ttm, unsigned long start,
--

2.7.4

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

Acked-by
#8 lore raw

Re: [PATCH 2/6] drm/amdgpu: Enable amdgpu_ttm_tt_get_user_pages in worker threads

In reply to a message not in the archive
Quoted 3 lines · Oded Gabbay

Hi Felix,
The patch looks fine to me and I can ACK it, but I would prefer that Alex or Christian will review it as well before pushing it.

Not my area of expertise, but it looks correct to me.

Quoted 125 lines

Thanks,
Oded

Quoted 121 lines · Felix Kuehling

This commit allows amdgpu_ttm_tt_get_user_pages to work in a worker thread rather than regular process context. This will be used when KFD userptr BOs are restored after an MMU-notifier eviction.

v2: Manage task reference with get_task_struct/put_task_struct

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c | 38 +++++++++++++++++++++++++--------
 1 file changed, 29 insertions(+), 9 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +29 -9
index c2fae04..25490fe 100644
@@ -689,7 +689,7 @@ struct amdgpu_ttm_tt {
        struct ttm_dma_tt       ttm;
        u64                     offset;
        uint64_t                userptr;
-       struct mm_struct        *usermm;
+       struct task_struct      *usertask;
        uint32_t                userflags;
        spinlock_t              guptasklock;
        struct list_head        guptasks;
@@ -700,14 +700,18 @@ struct amdgpu_ttm_tt {
 int amdgpu_ttm_tt_get_user_pages(struct ttm_tt *ttm, struct page **pages)
 {
        struct amdgpu_ttm_tt *gtt = (void *)ttm;
+       struct mm_struct *mm = gtt->usertask->mm;
        unsigned int flags = 0;
        unsigned pinned = 0;
        int r;
 
+       if (!mm) /* Happens during process shutdown */
+               return -ESRCH;
+
        if (!(gtt->userflags & AMDGPU_GEM_USERPTR_READONLY))
                flags |= FOLL_WRITE;
 
-       down_read(&current->mm->mmap_sem);
+       down_read(&mm->mmap_sem);
 
        if (gtt->userflags & AMDGPU_GEM_USERPTR_ANONONLY) {
                /* check that we only use anonymous memory
@@ -715,9 +719,9 @@ int amdgpu_ttm_tt_get_user_pages(struct ttm_tt *ttm, struct page **pages)
                unsigned long end = gtt->userptr + ttm->num_pages * PAGE_SIZE;
                struct vm_area_struct *vma;
 
-               vma = find_vma(gtt->usermm, gtt->userptr);
+               vma = find_vma(mm, gtt->userptr);
                if (!vma || vma->vm_file || vma->vm_end < end) {
-                       up_read(&current->mm->mmap_sem);
+                       up_read(&mm->mmap_sem);
                        return -EPERM;
                }
        }
@@ -733,7 +737,12 @@ int amdgpu_ttm_tt_get_user_pages(struct ttm_tt *ttm, struct page **pages)
                list_add(&guptask.list, &gtt->guptasks);
                spin_unlock(&gtt->guptasklock);
 
-               r = get_user_pages(userptr, num_pages, flags, p, NULL);
+               if (mm == current->mm)
+                       r = get_user_pages(userptr, num_pages, flags, p, NULL);
+               else
+                       r = get_user_pages_remote(gtt->usertask,
+                                       mm, userptr, num_pages,
+                                       flags, p, NULL, NULL);
 
                spin_lock(&gtt->guptasklock);
                list_del(&guptask.list);
@@ -746,12 +755,12 @@ int amdgpu_ttm_tt_get_user_pages(struct ttm_tt *ttm, struct page **pages)
 
        } while (pinned < ttm->num_pages);
 
-       up_read(&current->mm->mmap_sem);
+       up_read(&mm->mmap_sem);
        return 0;
 
 release_pages:
        release_pages(pages, pinned);
-       up_read(&current->mm->mmap_sem);
+       up_read(&mm->mmap_sem);
        return r;
 }
 
@@ -972,6 +981,9 @@ static void amdgpu_ttm_backend_destroy(struct ttm_tt *ttm)
 {
        struct amdgpu_ttm_tt *gtt = (void *)ttm;
 
+       if (gtt->usertask)
+               put_task_struct(gtt->usertask);
+
        ttm_dma_tt_fini(&gtt->ttm);
        kfree(gtt);
 }
@@ -1072,8 +1084,13 @@ int amdgpu_ttm_tt_set_userptr(struct ttm_tt *ttm, uint64_t addr,
                return -EINVAL;
 
        gtt->userptr = addr;
-       gtt->usermm = current->mm;
        gtt->userflags = flags;
+
+       if (gtt->usertask)
+               put_task_struct(gtt->usertask);
+       gtt->usertask = current->group_leader;
+       get_task_struct(gtt->usertask);
+
        spin_lock_init(&gtt->guptasklock);
        INIT_LIST_HEAD(&gtt->guptasks);
        atomic_set(&gtt->mmu_invalidations, 0);
@@ -1089,7 +1106,10 @@ struct mm_struct *amdgpu_ttm_tt_get_usermm(struct ttm_tt *ttm)
        if (gtt == NULL)
                return NULL;
 
-       return gtt->usermm;
+       if (gtt->usertask == NULL)
+               return NULL;
+
+       return gtt->usertask->mm;
 }
 
Quoted 8 lines
Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +0 -1
 bool amdgpu_ttm_tt_affect_userptr(struct ttm_tt *ttm, unsigned long start,
--

2.7.4

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

#9 lore raw

PATCH 3/6 drm/amdgpu: Avoid reclaim while holding locks taken in MMU notifier

In reply to a message not in the archive

When an MMU notifier runs in memory reclaim context, it can deadlock trying to take locks that are already held in the thread causing the memory reclaim. The solution is to avoid memory reclaim while holding locks that are taken in MMU notifiers.

This commit fixes kmalloc while holding rmn->lock by moving the call outside the lock. The GFX MMU notifier also locks reservation objects. I have no good solution for avoiding reclaim while holding reservation objects. The HSA MMU notifier will not lock any reservation objects.

v2: Moved allocation outside lock instead of using GFP_NOIO

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_mn.c | 17 +++++++++--------
 1 file changed, 9 insertions(+), 8 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_mn.c +9 -8
index f2ed18e..83e344f 100644
@@ -380,7 +380,7 @@ int amdgpu_mn_register(struct amdgpu_bo *bo, unsigned long addr)
 	enum amdgpu_mn_type type =
 		bo->kfd_bo ? AMDGPU_MN_TYPE_HSA : AMDGPU_MN_TYPE_GFX;
 	struct amdgpu_mn *rmn;
-	struct amdgpu_mn_node *node = NULL;
+	struct amdgpu_mn_node *node = NULL, *new_node;
 	struct list_head bos;
 	struct interval_tree_node *it;
 
@@ -388,6 +388,10 @@ int amdgpu_mn_register(struct amdgpu_bo *bo, unsigned long addr)
 	if (IS_ERR(rmn))
 		return PTR_ERR(rmn);
 
+	new_node = kmalloc(sizeof(*new_node), GFP_KERNEL);
+	if (!new_node)
+		return -ENOMEM;
+
 	INIT_LIST_HEAD(&bos);
 
 	down_write(&rmn->lock);
@@ -401,13 +405,10 @@ int amdgpu_mn_register(struct amdgpu_bo *bo, unsigned long addr)
 		list_splice(&node->bos, &bos);
 	}
 
-	if (!node) {
-		node = kmalloc(sizeof(struct amdgpu_mn_node), GFP_KERNEL);
-		if (!node) {
-			up_write(&rmn->lock);
-			return -ENOMEM;
-		}
-	}
+	if (!node)
+		node = new_node;
+	else
+		kfree(new_node);
 
 	bo->mn = rmn;
 
Signature · 6 lines
2.7.4

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

A message that is not in the archive (sent off-list or not yet received): 1521833553-31571-4-git-send-email-Felix.Kuehling-5C7GfCeVMHo@public.gmane.org

#11 lore raw

Re: [PATCH 3/6] drm/amdgpu: Avoid reclaim while holding locks taken in MMU notifier

Hi Felix,
Same as patch 2. ACK but I want an extra review from amdgpu people. Thanks,
Oded

Quoted 60 lines · Felix Kuehling

When an MMU notifier runs in memory reclaim context, it can deadlock trying to take locks that are already held in the thread causing the memory reclaim. The solution is to avoid memory reclaim while holding locks that are taken in MMU notifiers.

This commit fixes kmalloc while holding rmn->lock by moving the call outside the lock. The GFX MMU notifier also locks reservation objects. I have no good solution for avoiding reclaim while holding reservation objects. The HSA MMU notifier will not lock any reservation objects.

v2: Moved allocation outside lock instead of using GFP_NOIO

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_mn.c | 17 +++++++++--------
 1 file changed, 9 insertions(+), 8 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_mn.c +9 -9
index f2ed18e..83e344f 100644
@@ -380,7 +380,7 @@ int amdgpu_mn_register(struct amdgpu_bo *bo, unsigned long addr)
        enum amdgpu_mn_type type =
                bo->kfd_bo ? AMDGPU_MN_TYPE_HSA : AMDGPU_MN_TYPE_GFX;
        struct amdgpu_mn *rmn;
-       struct amdgpu_mn_node *node = NULL;
+       struct amdgpu_mn_node *node = NULL, *new_node;
        struct list_head bos;
        struct interval_tree_node *it;
 
@@ -388,6 +388,10 @@ int amdgpu_mn_register(struct amdgpu_bo *bo, unsigned long addr)
        if (IS_ERR(rmn))
                return PTR_ERR(rmn);
 
+       new_node = kmalloc(sizeof(*new_node), GFP_KERNEL);
+       if (!new_node)
+               return -ENOMEM;
+
        INIT_LIST_HEAD(&bos);
 
        down_write(&rmn->lock);
@@ -401,13 +405,10 @@ int amdgpu_mn_register(struct amdgpu_bo *bo, unsigned long addr)
                list_splice(&node->bos, &bos);
        }
 
-       if (!node) {
-               node = kmalloc(sizeof(struct amdgpu_mn_node), GFP_KERNEL);
-               if (!node) {
-                       up_write(&rmn->lock);
-                       return -ENOMEM;
-               }
-       }
+       if (!node)
+               node = new_node;
+       else
+               kfree(new_node);
 
        bo->mn = rmn;
 
--
Quoted 2 lines

2.7.4

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

A message that is not in the archive (sent off-list or not yet received): CAFCwf13ihv88TGtSWXovmOawkcboMRuVOxih-R_-CnJC5h1=Qw-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org

#13 lore raw

Re: [PATCH 3/6] drm/amdgpu: Avoid reclaim while holding locks taken in MMU notifier

Hi Oded,

it looks sane to me and is Reviewed-by: Christian König <[email protected]>.

Christian.

Am 11.05.2018 um 09:38 schrieb Oded Gabbay:

Quoted 63 lines

Hi Felix,
Same as patch 2. ACK but I want an extra review from amdgpu people. Thanks,
Oded

Quoted 57 lines · Felix Kuehling

When an MMU notifier runs in memory reclaim context, it can deadlock trying to take locks that are already held in the thread causing the memory reclaim. The solution is to avoid memory reclaim while holding locks that are taken in MMU notifiers.

This commit fixes kmalloc while holding rmn->lock by moving the call outside the lock. The GFX MMU notifier also locks reservation objects. I have no good solution for avoiding reclaim while holding reservation objects. The HSA MMU notifier will not lock any reservation objects.

v2: Moved allocation outside lock instead of using GFP_NOIO

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_mn.c | 17 +++++++++--------
  1 file changed, 9 insertions(+), 8 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_mn.c +9 -8
index f2ed18e..83e344f 100644
@@ -380,7 +380,7 @@ int amdgpu_mn_register(struct amdgpu_bo *bo, unsigned long addr)
         enum amdgpu_mn_type type =
                 bo->kfd_bo ? AMDGPU_MN_TYPE_HSA : AMDGPU_MN_TYPE_GFX;
         struct amdgpu_mn *rmn;
-       struct amdgpu_mn_node *node = NULL;
+       struct amdgpu_mn_node *node = NULL, *new_node;
         struct list_head bos;
         struct interval_tree_node *it;
 
@@ -388,6 +388,10 @@ int amdgpu_mn_register(struct amdgpu_bo *bo, unsigned long addr)
         if (IS_ERR(rmn))
                 return PTR_ERR(rmn);
 
+       new_node = kmalloc(sizeof(*new_node), GFP_KERNEL);
+       if (!new_node)
+               return -ENOMEM;
+
         INIT_LIST_HEAD(&bos);
 
         down_write(&rmn->lock);
@@ -401,13 +405,10 @@ int amdgpu_mn_register(struct amdgpu_bo *bo, unsigned long addr)
                 list_splice(&node->bos, &bos);
         }
 
-       if (!node) {
-               node = kmalloc(sizeof(struct amdgpu_mn_node), GFP_KERNEL);
-               if (!node) {
-                       up_write(&rmn->lock);
-                       return -ENOMEM;
-               }
-       }
+       if (!node)
+               node = new_node;
+       else
+               kfree(new_node);
 
Quoted 5 lines
Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_mn.c +0 -1
         bo->mn = rmn;
 
--

2.7.4

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

#14 lore raw

PATCH 4/6 drm/amdkfd: GFP_NOIO while holding locks taken in MMU notifier

In reply to a message not in the archive

When an MMU notifier runs in memory reclaim context, it can deadlock trying to take locks that are already held in the thread causing the memory reclaim. The solution is to avoid memory reclaim while holding locks that are taken in MMU notifiers by using GFP_NOIO.

This commit fixes memory allocations done while holding the dqm->lock which is needed in the MMU notifier (dqm->ops.evict_process_queues).

---
 drivers/gpu/drm/amd/amdkfd/kfd_device.c          | 2 +-
 drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_cik.c | 2 +-
 drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_vi.c  | 2 +-
 3 files changed, 3 insertions(+), 3 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_device.c +1 -1
index 334669996..0434f65 100644
@@ -652,7 +652,7 @@ int kfd_gtt_sa_allocate(struct kfd_dev *kfd, unsigned int size,
 	if (size > kfd->gtt_sa_num_of_chunks * kfd->gtt_sa_chunk_size)
 		return -ENOMEM;
 
-	*mem_obj = kmalloc(sizeof(struct kfd_mem_obj), GFP_KERNEL);
+	*mem_obj = kmalloc(sizeof(struct kfd_mem_obj), GFP_NOIO);
 	if ((*mem_obj) == NULL)
 		return -ENOMEM;
 
drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_cik.c +1 -1
index c00c325..2bc49c6 100644
@@ -412,7 +412,7 @@ struct mqd_manager *mqd_manager_init_cik(enum KFD_MQD_TYPE type,
 	if (WARN_ON(type >= KFD_MQD_TYPE_MAX))
 		return NULL;
 
-	mqd = kzalloc(sizeof(*mqd), GFP_KERNEL);
+	mqd = kzalloc(sizeof(*mqd), GFP_NOIO);
 	if (!mqd)
 		return NULL;
 
drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_vi.c +1 -1
index 89e4242..481307b 100644
@@ -394,7 +394,7 @@ struct mqd_manager *mqd_manager_init_vi(enum KFD_MQD_TYPE type,
 	if (WARN_ON(type >= KFD_MQD_TYPE_MAX))
 		return NULL;
 
-	mqd = kzalloc(sizeof(*mqd), GFP_KERNEL);
+	mqd = kzalloc(sizeof(*mqd), GFP_NOIO);
 	if (!mqd)
 		return NULL;
 
Signature · 6 lines
2.7.4

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

A message that is not in the archive (sent off-list or not yet received): 1521833553-31571-5-git-send-email-Felix.Kuehling-5C7GfCeVMHo@public.gmane.org

#16 lore raw

Re: [PATCH 4/6] drm/amdkfd: GFP_NOIO while holding locks taken in MMU notifier

Quoted 4 lines · Felix Kuehling

When an MMU notifier runs in memory reclaim context, it can deadlock trying to take locks that are already held in the thread causing the memory reclaim. The solution is to avoid memory reclaim while holding locks that are taken in MMU notifiers by using GFP_NOIO.

Which locks are problematic ?

The kernel recommendation is to use "memalloc_noio_{save,restore} to mark the whole scope which cannot perform any IO with a short explanation why"

By using the scope functions, you protect against future allocation code that will be written in the critical path, without worrying about the developer using the correct GFP_NOIO flag.

Oded

Quoted 51 lines

This commit fixes memory allocations done while holding the dqm->lock which is needed in the MMU notifier (dqm->ops.evict_process_queues).

---
 drivers/gpu/drm/amd/amdkfd/kfd_device.c          | 2 +-
 drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_cik.c | 2 +-
 drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_vi.c  | 2 +-
 3 files changed, 3 insertions(+), 3 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_device.c +1 -1
index 334669996..0434f65 100644
@@ -652,7 +652,7 @@ int kfd_gtt_sa_allocate(struct kfd_dev *kfd, unsigned int size,
        if (size > kfd->gtt_sa_num_of_chunks * kfd->gtt_sa_chunk_size)
                return -ENOMEM;
 
-       *mem_obj = kmalloc(sizeof(struct kfd_mem_obj), GFP_KERNEL);
+       *mem_obj = kmalloc(sizeof(struct kfd_mem_obj), GFP_NOIO);
        if ((*mem_obj) == NULL)
                return -ENOMEM;
 
drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_cik.c +1 -1
index c00c325..2bc49c6 100644
@@ -412,7 +412,7 @@ struct mqd_manager *mqd_manager_init_cik(enum KFD_MQD_TYPE type,
        if (WARN_ON(type >= KFD_MQD_TYPE_MAX))
                return NULL;
 
-       mqd = kzalloc(sizeof(*mqd), GFP_KERNEL);
+       mqd = kzalloc(sizeof(*mqd), GFP_NOIO);
        if (!mqd)
                return NULL;
 
drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_vi.c +1 -2
index 89e4242..481307b 100644
@@ -394,7 +394,7 @@ struct mqd_manager *mqd_manager_init_vi(enum KFD_MQD_TYPE type,
        if (WARN_ON(type >= KFD_MQD_TYPE_MAX))
                return NULL;
 
-       mqd = kzalloc(sizeof(*mqd), GFP_KERNEL);
+       mqd = kzalloc(sizeof(*mqd), GFP_NOIO);
        if (!mqd)
                return NULL;
 
--
Quoted 2 lines

2.7.4

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

A message that is not in the archive (sent off-list or not yet received): CAFCwf13=swLVZ-FeHtsV9wUxpbA0ghZ2enSLvfZG8vA4g_Jc3Q-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org

#18 lore raw

Re: [PATCH 4/6] drm/amdkfd: GFP_NOIO while holding locks taken in MMU notifier

Quoted 6 lines · Oded Gabbay
Quoted 4 lines · Felix Kuehling

When an MMU notifier runs in memory reclaim context, it can deadlock trying to take locks that are already held in the thread causing the memory reclaim. The solution is to avoid memory reclaim while holding locks that are taken in MMU notifiers by using GFP_NOIO.

Which locks are problematic ?

The only lock I need to take in our MMU notifier is the DQM lock.

Quoted 4 lines

The kernel recommendation is to use "memalloc_noio_{save,restore} to mark the whole scope which cannot perform any IO with a short explanation why"

Yeah. Looking at it more, I think the correct one to use is actually memalloc_nofs_{save,restore}.

Quoted 4 lines

By using the scope functions, you protect against future allocation code that will be written in the critical path, without worrying about the developer using the correct GFP_NOIO flag.

Yes. Last time I looked into this it was broken and didn't properly handle kmalloc allocations. It looks like this was fixed by this commit:

commit 6d7225f0cc1a1fc32cf5dd01b4ab4b8a34c7cdb4
Author: Nikolay Borisov <[email protected]> Date:   Wed May 3 14:53:05 2017 -0700

    lockdep: teach lockdep about memalloc_noio_save

Later NOFS was introduced, which is now used by the lockdep checker to detect reclaim deadlocks.

Regards,
  Felix

Quoted 47 lines

Oded

Quoted 44 lines

This commit fixes memory allocations done while holding the dqm->lock which is needed in the MMU notifier (dqm->ops.evict_process_queues).

---
 drivers/gpu/drm/amd/amdkfd/kfd_device.c          | 2 +-
 drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_cik.c | 2 +-
 drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_vi.c  | 2 +-
 3 files changed, 3 insertions(+), 3 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_device.c +1 -1
index 334669996..0434f65 100644
@@ -652,7 +652,7 @@ int kfd_gtt_sa_allocate(struct kfd_dev *kfd, unsigned int size,
        if (size > kfd->gtt_sa_num_of_chunks * kfd->gtt_sa_chunk_size)
                return -ENOMEM;
 
-       *mem_obj = kmalloc(sizeof(struct kfd_mem_obj), GFP_KERNEL);
+       *mem_obj = kmalloc(sizeof(struct kfd_mem_obj), GFP_NOIO);
        if ((*mem_obj) == NULL)
                return -ENOMEM;
 
drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_cik.c +1 -1
index c00c325..2bc49c6 100644
@@ -412,7 +412,7 @@ struct mqd_manager *mqd_manager_init_cik(enum KFD_MQD_TYPE type,
        if (WARN_ON(type >= KFD_MQD_TYPE_MAX))
                return NULL;
 
-       mqd = kzalloc(sizeof(*mqd), GFP_KERNEL);
+       mqd = kzalloc(sizeof(*mqd), GFP_NOIO);
        if (!mqd)
                return NULL;
 
drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_vi.c +0 -0
index 89e4242..481307b 100644
@@ -394,7 +394,7 @@ struct mqd_manager *mqd_manager_init_vi(enum KFD_MQD_TYPE type,
        if (WARN_ON(type >= KFD_MQD_TYPE_MAX))
                return NULL;
 
Quoted 8 lines
Quoted 8 lines
drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_vi.c +1 -2
-       mqd = kzalloc(sizeof(*mqd), GFP_KERNEL);
+       mqd = kzalloc(sizeof(*mqd), GFP_NOIO);
        if (!mqd)
                return NULL;
 
--

2.7.4

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

A message that is not in the archive (sent off-list or not yet received): 1f89f053-4d78-3a13-aa28-3826d858182b-5C7GfCeVMHo@public.gmane.org

#20 lore raw

Re: [PATCH 4/6] drm/amdkfd: GFP_NOIO while holding locks taken in MMU notifier

Cool, thanks!

Quoted 83 lines · On Mon, 14 May 2018, 19:07 Felix Kuehling…
Quoted 1 line · Oded Gabbay

On Fri, Mar 23, 2018 at 10:32 PM, Felix Kuehling <[email protected]>

Quoted 5 lines · wrote:
Quoted 4 lines

When an MMU notifier runs in memory reclaim context, it can deadlock trying to take locks that are already held in the thread causing the memory reclaim. The solution is to avoid memory reclaim while holding locks that are taken in MMU notifiers by using GFP_NOIO.

Which locks are problematic ?

The only lock I need to take in our MMU notifier is the DQM lock.

Quoted 4 lines

The kernel recommendation is to use "memalloc_noio_{save,restore} to mark the whole scope which cannot perform any IO with a short explanation why"

Yeah. Looking at it more, I think the correct one to use is actually memalloc_nofs_{save,restore}.

Quoted 4 lines

By using the scope functions, you protect against future allocation code that will be written in the critical path, without worrying about the developer using the correct GFP_NOIO flag.

Yes. Last time I looked into this it was broken and didn't properly handle kmalloc allocations. It looks like this was fixed by this commit:

commit 6d7225f0cc1a1fc32cf5dd01b4ab4b8a34c7cdb4
Author: Nikolay Borisov <nborisov-IBi9RG/[email protected]>
Date: Wed May 3 14:53:05 2017 -0700

lockdep: teach lockdep about memalloc_noio_save

Later NOFS was introduced, which is now used by the lockdep checker to detect reclaim deadlocks.

Regards,
Felix

Quoted 6 lines

Oded

Quoted 3 lines

This commit fixes memory allocations done while holding the dqm->lock which is needed in the MMU notifier (dqm->ops.evict_process_queues).

Quoted 8 lines

Review

Quoted 8 lines
---
 drivers/gpu/drm/amd/amdkfd/kfd_device.c          | 2 +-
 drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_cik.c | 2 +-
 drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_vi.c  | 2 +-
 3 files changed, 3 insertions(+), 3 deletions(-)
(quoted diff) +0 -0

b/drivers/gpu/drm/amd/amdkfd/kfd_device.c

Quoted 4 lines

drivers/gpu/drm/amd/amdkfd/kfd_device.c int kfd_gtt_sa_allocate(struct kfd_dev *kfd,

Quoted 4 lines

index 334669996..0434f65 100644

drivers/gpu/drm/amd/amdkfd/kfd_device.c +0 -0
@@ -652,7 +652,7 @@ int kfd_gtt_sa_allocate(struct kfd_dev *kfd,

unsigned int size,

Quoted 9 lines

Review

Quoted 9 lines
drivers/gpu/drm/amd/amdkfd/kfd_device.c +1 -1
        if (size > kfd->gtt_sa_num_of_chunks * kfd->gtt_sa_chunk_size)
                return -ENOMEM;
 
-       *mem_obj = kmalloc(sizeof(struct kfd_mem_obj), GFP_KERNEL);
+       *mem_obj = kmalloc(sizeof(struct kfd_mem_obj), GFP_NOIO);
        if ((*mem_obj) == NULL)
                return -ENOMEM;
 
(quoted diff) +0 -0

b/drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_cik.c

Quoted 4 lines

drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_cik.c struct mqd_manager *mqd_manager_init_cik(enum

Quoted 4 lines

index c00c325..2bc49c6 100644

drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_cik.c +0 -0
@@ -412,7 +412,7 @@ struct mqd_manager *mqd_manager_init_cik(enum

KFD_MQD_TYPE type,

Quoted 8 lines
Quoted 8 lines
drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_cik.c +1 -1
        if (WARN_ON(type >= KFD_MQD_TYPE_MAX))
                return NULL;
 
-       mqd = kzalloc(sizeof(*mqd), GFP_KERNEL);
+       mqd = kzalloc(sizeof(*mqd), GFP_NOIO);
        if (!mqd)
                return NULL;
 
Quoted 20 lines
Quoted 1 line

Review

Quoted 1 line
(quoted diff) +0 -0

b/drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_vi.c

Quoted 4 lines

drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_vi.c struct mqd_manager *mqd_manager_init_vi(enum

Quoted 4 lines

index 89e4242..481307b 100644

drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_vi.c +0 -0
@@ -394,7 +394,7 @@ struct mqd_manager *mqd_manager_init_vi(enum

KFD_MQD_TYPE type,

Quoted 11 lines
Quoted 11 lines
drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_vi.c +1 -2
        if (WARN_ON(type >= KFD_MQD_TYPE_MAX))
                return NULL;
 
-       mqd = kzalloc(sizeof(*mqd), GFP_KERNEL);
+       mqd = kzalloc(sizeof(*mqd), GFP_NOIO);
        if (!mqd)
                return NULL;
 
--

2.7.4

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

#21 lore raw

PATCH 5/6 drm/amdkfd: Add quiesce_mm and resume_mm to kgd2kfd_calls

In reply to a message not in the archive

These interfaces allow KGD to stop and resume all GPU user mode queue access to a process address space. This is needed for handling MMU notifiers of userptrs mapped for GPU access in KFD VMs.

---
 drivers/gpu/drm/amd/amdkfd/kfd_device.c         | 38 +++++++++++++++++++++++++
 drivers/gpu/drm/amd/amdkfd/kfd_module.c         |  2 ++
 drivers/gpu/drm/amd/amdkfd/kfd_priv.h           |  4 +++
 drivers/gpu/drm/amd/amdkfd/kfd_process.c        | 10 +++----
 drivers/gpu/drm/amd/include/kgd_kfd_interface.h |  6 ++++
 5 files changed, 55 insertions(+), 5 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_device.c +38 -0
index 0434f65..7b57995 100644
@@ -541,6 +541,44 @@ void kgd2kfd_interrupt(struct kfd_dev *kfd, const void *ih_ring_entry)
 	spin_unlock(&kfd->interrupt_lock);
 }
 
+int kgd2kfd_quiesce_mm(struct mm_struct *mm)
+{
+	struct kfd_process *p;
+	int r;
+
+	/* Because we are called from arbitrary context (workqueue) as opposed
+	 * to process context, kfd_process could attempt to exit while we are
+	 * running so the lookup function increments the process ref count.
+	 */
+	p = kfd_lookup_process_by_mm(mm);
+	if (!p)
+		return -ESRCH;
+
+	r = kfd_process_evict_queues(p);
+
+	kfd_unref_process(p);
+	return r;
+}
+
+int kgd2kfd_resume_mm(struct mm_struct *mm)
+{
+	struct kfd_process *p;
+	int r;
+
+	/* Because we are called from arbitrary context (workqueue) as opposed
+	 * to process context, kfd_process could attempt to exit while we are
+	 * running so the lookup function increments the process ref count.
+	 */
+	p = kfd_lookup_process_by_mm(mm);
+	if (!p)
+		return -ESRCH;
+
+	r = kfd_process_restore_queues(p);
+
+	kfd_unref_process(p);
+	return r;
+}
+
 /** kgd2kfd_schedule_evict_and_restore_process - Schedules work queue that will
  *   prepare for safe eviction of KFD BOs that belong to the specified
  *   process.
drivers/gpu/drm/amd/amdkfd/kfd_module.c +2 -0
index 2237332..a8e92ce 100644
@@ -43,6 +43,8 @@ static const struct kgd2kfd_calls kgd2kfd = {
 	.interrupt	= kgd2kfd_interrupt,
 	.suspend	= kgd2kfd_suspend,
 	.resume		= kgd2kfd_resume,
+	.quiesce_mm	= kgd2kfd_quiesce_mm,
+	.resume_mm	= kgd2kfd_resume_mm,
 	.schedule_evict_and_restore_process =
 			  kgd2kfd_schedule_evict_and_restore_process,
 };
drivers/gpu/drm/amd/amdkfd/kfd_priv.h +4 -0
index 96a9cc0..4d5c49e 100644
@@ -512,6 +512,8 @@ struct qcm_process_device {
 /* Approx. time before evicting the process again */
 #define PROCESS_ACTIVE_TIME_MS 10
 
+int kgd2kfd_quiesce_mm(struct mm_struct *mm);
+int kgd2kfd_resume_mm(struct mm_struct *mm);
 int kgd2kfd_schedule_evict_and_restore_process(struct mm_struct *mm,
 					       struct dma_fence *fence);
 
@@ -681,6 +683,8 @@ struct kfd_process *kfd_get_process(const struct task_struct *);
 struct kfd_process *kfd_lookup_process_by_pasid(unsigned int pasid);
 struct kfd_process *kfd_lookup_process_by_mm(const struct mm_struct *mm);
 void kfd_unref_process(struct kfd_process *p);
+int kfd_process_evict_queues(struct kfd_process *p);
+int kfd_process_restore_queues(struct kfd_process *p);
 void kfd_suspend_all_processes(void);
 int kfd_resume_all_processes(void);
 
drivers/gpu/drm/amd/amdkfd/kfd_process.c +5 -5
index 75fdc18..ee43b2f 100644
@@ -808,7 +808,7 @@ struct kfd_process *kfd_lookup_process_by_mm(const struct mm_struct *mm)
  * Eviction is reference-counted per process-device. This means multiple
  * evictions from different sources can be nested safely.
  */
-static int process_evict_queues(struct kfd_process *p)
+int kfd_process_evict_queues(struct kfd_process *p)
 {
 	struct kfd_process_device *pdd;
 	int r = 0;
@@ -844,7 +844,7 @@ static int process_evict_queues(struct kfd_process *p)
 }
 
 /* process_restore_queues - Restore all user queues of a process */
-static  int process_restore_queues(struct kfd_process *p)
+int kfd_process_restore_queues(struct kfd_process *p)
 {
 	struct kfd_process_device *pdd;
 	int r, ret = 0;
@@ -886,7 +886,7 @@ static void evict_process_worker(struct work_struct *work)
 	flush_delayed_work(&p->restore_work);
 
 	pr_debug("Started evicting pasid %d\n", p->pasid);
-	ret = process_evict_queues(p);
+	ret = kfd_process_evict_queues(p);
 	if (!ret) {
 		dma_fence_signal(p->ef);
 		dma_fence_put(p->ef);
@@ -946,7 +946,7 @@ static void restore_process_worker(struct work_struct *work)
 		return;
 	}
 
-	ret = process_restore_queues(p);
+	ret = kfd_process_restore_queues(p);
 	if (!ret)
 		pr_debug("Finished restoring pasid %d\n", p->pasid);
 	else
@@ -963,7 +963,7 @@ void kfd_suspend_all_processes(void)
 		cancel_delayed_work_sync(&p->eviction_work);
 		cancel_delayed_work_sync(&p->restore_work);
 
-		if (process_evict_queues(p))
+		if (kfd_process_evict_queues(p))
 			pr_err("Failed to suspend process %d\n", p->pasid);
 		dma_fence_signal(p->ef);
 		dma_fence_put(p->ef);
drivers/gpu/drm/amd/include/kgd_kfd_interface.h +6 -0
index 237289a..286cfe7 100644
@@ -382,6 +382,10 @@ struct kfd2kgd_calls {
  *
  * @resume: Notifies amdkfd about a resume action done to a kgd device
  *
+ * @quiesce_mm: Quiesce all user queue access to specified MM address space
+ *
+ * @resume_mm: Resume user queue access to specified MM address space
+ *
  * @schedule_evict_and_restore_process: Schedules work queue that will prepare
  * for safe eviction of KFD BOs that belong to the specified process.
  *
@@ -399,6 +403,8 @@ struct kgd2kfd_calls {
 	void (*interrupt)(struct kfd_dev *kfd, const void *ih_ring_entry);
 	void (*suspend)(struct kfd_dev *kfd);
 	int (*resume)(struct kfd_dev *kfd);
+	int (*quiesce_mm)(struct mm_struct *mm);
+	int (*resume_mm)(struct mm_struct *mm);
 	int (*schedule_evict_and_restore_process)(struct mm_struct *mm,
 			struct dma_fence *fence);
 };
Signature · 6 lines
2.7.4

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx
#22 lore raw

PATCH 6/6 drm/amdgpu: Add userptr support for KFD

In reply to a message not in the archive

This adds support for allocating, mapping, unmapping and freeing userptr BOs, and for handling MMU notifiers.

v2: updated a comment

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h       |  11 +
 drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c | 567 ++++++++++++++++++++++-
 2 files changed, 554 insertions(+), 24 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h +11 -0
index 83e0c5c..c3024b1 100644
@@ -28,6 +28,7 @@
 #include <linux/types.h>
 #include <linux/mm.h>
 #include <linux/mmu_context.h>
+#include <linux/workqueue.h>
 #include <kgd_kfd_interface.h>
 #include <drm/ttm/ttm_execbuf_util.h>
 #include "amdgpu_sync.h"
@@ -59,7 +60,9 @@ struct kgd_mem {
 
 	uint32_t mapping_flags;
 
+	atomic_t invalid;
 	struct amdkfd_process_info *process_info;
+	struct page **user_pages;
 
 	struct amdgpu_sync sync;
 
@@ -84,6 +87,9 @@ struct amdkfd_process_info {
 	struct list_head vm_list_head;
 	/* List head for all KFD BOs that belong to a KFD process. */
 	struct list_head kfd_bo_list;
+	/* List of userptr BOs that are valid or invalid */
+	struct list_head userptr_valid_list;
+	struct list_head userptr_inval_list;
 	/* Lock to protect kfd_bo_list */
 	struct mutex lock;
 
@@ -91,6 +97,11 @@ struct amdkfd_process_info {
 	unsigned int n_vms;
 	/* Eviction Fence */
 	struct amdgpu_amdkfd_fence *eviction_fence;
+
+	/* MMU-notifier related fields */
+	atomic_t evicted_bos;
+	struct delayed_work restore_userptr_work;
+	struct pid *pid;
 };
 
 int amdgpu_amdkfd_init(void);
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +543 -24

768 lines not on this page · raw message

Signature · 6 lines
2.7.4

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx
#23 lore raw
In reply to a message not in the archive
Quoted 26 lines · Felix Kuehling

Update of remaining patches from the GPUVM patch series. This should apply on top of the fixes I just sent out.

Felix Kuehling (6):
drm/amdgpu: Add MMU notifier type for KFD userptr drm/amdgpu: Enable amdgpu_ttm_tt_get_user_pages in worker threads drm/amdgpu: Avoid reclaim while holding locks taken in MMU notifier drm/amdkfd: GFP_NOIO while holding locks taken in MMU notifier drm/amdkfd: Add quiesce_mm and resume_mm to kgd2kfd_calls drm/amdgpu: Add userptr support for KFD

 drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h       |  12 +
 drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c | 572 ++++++++++++++++++++++-
 drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c           |   2 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_mn.c           | 111 ++++-
 drivers/gpu/drm/amd/amdgpu/amdgpu_mn.h           |  11 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c          |  38 +-
 drivers/gpu/drm/amd/amdkfd/kfd_device.c          |  40 +-
 drivers/gpu/drm/amd/amdkfd/kfd_module.c          |   2 +
 drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_cik.c |   2 +-
 drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_vi.c  |   2 +-
 drivers/gpu/drm/amd/amdkfd/kfd_priv.h            |   4 +
 drivers/gpu/drm/amd/amdkfd/kfd_process.c         |  10 +-
 drivers/gpu/drm/amd/include/kgd_kfd_interface.h  |   6 +
 13 files changed, 746 insertions(+), 66 deletions(-)
Quoted 3 lines

--
2.7.4

Hi Felix,

Patch 1, 5 & 6 are reviewed by me.
Patches 2 & 3 are fine but I would like approval from Alex/Christian Patch 4 - I think we can do better. See my comments there.

Thanks,
Oded
_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx