Skip to contentSkip to conversation

drm/amdgpu: move taking mmap_sem into get_user_pages

PATCH In review Christian König 2 participants 10 messages Sep 5, 2017 3 messages not in the archive

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

PATCH 1/6 drm/amdgpu: move taking mmap_sem into get_user_pages

From: Christian König <[email protected]>

This didn't helped as intended, just simplify the code.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c  | 12 +-----------
 drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c |  4 ----
 drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c |  4 ++++
 3 files changed, 5 insertions(+), 15 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +1 -11
index 4ab35db..e6e04d3 100644
@@ -497,18 +497,14 @@ static int amdgpu_cs_parser_bos(struct amdgpu_cs_parser *p,
 	struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
 	struct amdgpu_bo_list_entry *e;
 	struct list_head duplicates;
-	bool need_mmap_lock = false;
 	unsigned i, tries = 10;
 	int r;
 
 	INIT_LIST_HEAD(&p->validated);
 
 	p->bo_list = amdgpu_bo_list_get(fpriv, cs->in.bo_list_handle);
-	if (p->bo_list) {
-		need_mmap_lock = p->bo_list->first_userptr !=
-			p->bo_list->num_entries;
+	if (p->bo_list)
 		amdgpu_bo_list_get_list(p->bo_list, &p->validated);
-	}
 
 	INIT_LIST_HEAD(&duplicates);
 	amdgpu_vm_get_pd_bo(&fpriv->vm, &p->validated, &p->vm_pd);
@@ -516,9 +512,6 @@ static int amdgpu_cs_parser_bos(struct amdgpu_cs_parser *p,
 	if (p->uf_entry.robj)
 		list_add(&p->uf_entry.tv.head, &p->validated);
 
-	if (need_mmap_lock)
-		down_read(&current->mm->mmap_sem);
-
 	while (1) {
 		struct list_head need_pages;
 		unsigned i;
@@ -670,9 +663,6 @@ static int amdgpu_cs_parser_bos(struct amdgpu_cs_parser *p,
 
 error_free_pages:
 
-	if (need_mmap_lock)
-		up_read(&current->mm->mmap_sem);
-
 	if (p->bo_list) {
 		for (i = p->bo_list->first_userptr;
 		     i < p->bo_list->num_entries; ++i) {
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +0 -4
index f1e61b3..b0d45c8 100644
@@ -318,8 +318,6 @@ int amdgpu_gem_userptr_ioctl(struct drm_device *dev, void *data,
 	}
 
 	if (args->flags & AMDGPU_GEM_USERPTR_VALIDATE) {
-		down_read(&current->mm->mmap_sem);
-
 		r = amdgpu_ttm_tt_get_user_pages(bo->tbo.ttm,
 						 bo->tbo.ttm->pages);
 		if (r)
@@ -334,8 +332,6 @@ int amdgpu_gem_userptr_ioctl(struct drm_device *dev, void *data,
 		amdgpu_bo_unreserve(bo);
 		if (r)
 			goto free_pages;
-
-		up_read(&current->mm->mmap_sem);
 	}
 
 	r = drm_gem_handle_create(filp, gobj, &handle);
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +4 -0
index 07c3f11..d2ce1a7 100644
@@ -622,6 +622,8 @@ int amdgpu_ttm_tt_get_user_pages(struct ttm_tt *ttm, struct page **pages)
 	if (!(gtt->userflags & AMDGPU_GEM_USERPTR_READONLY))
 		flags |= FOLL_WRITE;
 
+	down_read(&current->mm->mmap_sem);
+
 	if (gtt->userflags & AMDGPU_GEM_USERPTR_ANONONLY) {
 		/* check that we only use anonymous memory
 		   to prevent problems with writeback */
@@ -657,6 +659,8 @@ 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);
+
 	return 0;
 
 release_pages:
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): 1504625871-1756-1-git-send-email-deathsimple-ANTagKRnAhcb1SvskN2V4Q@public.gmane.org

#2 lore raw

PATCH 2/6 drm/amdgpu: stop using BO status for user pages

From: Christian König <[email protected]>

Instead use a counter to figure out if we need to set new pages or not.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu.h     |  1 +
 drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c  | 11 +++++++----
 drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c | 14 ++++++++++++++
 3 files changed, 22 insertions(+), 4 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu.h +1 -0
index 4dc9744..5d1f9cc 100644
@@ -1811,6 +1811,7 @@ bool amdgpu_ttm_tt_affect_userptr(struct ttm_tt *ttm, unsigned long start,
 				  unsigned long end);
 bool amdgpu_ttm_tt_userptr_invalidated(struct ttm_tt *ttm,
 				       int *last_invalidated);
+bool amdgpu_ttm_tt_userptr_needs_pages(struct ttm_tt *ttm);
 bool amdgpu_ttm_tt_is_readonly(struct ttm_tt *ttm);
 uint64_t amdgpu_ttm_tt_pte_flags(struct amdgpu_device *adev, struct ttm_tt *ttm,
 				 struct ttm_mem_reg *mem);
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +7 -4
index e6e04d3..19c5649 100644
@@ -470,7 +470,8 @@ static int amdgpu_cs_list_validate(struct amdgpu_cs_parser *p,
 			return -EPERM;
 
 		/* Check if we have user pages and nobody bound the BO already */
-		if (lobj->user_pages && bo->tbo.ttm->state != tt_bound) {
+		if (amdgpu_ttm_tt_userptr_needs_pages(bo->tbo.ttm) &&
+		    lobj->user_pages) {
 			amdgpu_ttm_tt_set_user_pages(bo->tbo.ttm,
 						     lobj->user_pages);
 			binding_userptr = true;
@@ -531,23 +532,25 @@ static int amdgpu_cs_parser_bos(struct amdgpu_cs_parser *p,
 		INIT_LIST_HEAD(&need_pages);
 		for (i = p->bo_list->first_userptr;
 		     i < p->bo_list->num_entries; ++i) {
+			struct amdgpu_bo *bo;
 
 			e = &p->bo_list->array[i];
+			bo = e->robj;
 
-			if (amdgpu_ttm_tt_userptr_invalidated(e->robj->tbo.ttm,
+			if (amdgpu_ttm_tt_userptr_invalidated(bo->tbo.ttm,
 				 &e->user_invalidated) && e->user_pages) {
 
 				/* We acquired a page array, but somebody
 				 * invalidated it. Free it and try again
 				 */
 				release_pages(e->user_pages,
-					      e->robj->tbo.ttm->num_pages,
+					      bo->tbo.ttm->num_pages,
 					      false);
 				drm_free_large(e->user_pages);
 				e->user_pages = NULL;
 			}
 
-			if (e->robj->tbo.ttm->state != tt_bound &&
+			if (amdgpu_ttm_tt_userptr_needs_pages(bo->tbo.ttm) &&
 			    !e->user_pages) {
 				list_del(&e->tv.head);
 				list_add(&e->tv.head, &need_pages);
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +14 -0
index d2ce1a7..0ed1725 100644
@@ -609,6 +609,7 @@ struct amdgpu_ttm_tt {
 	spinlock_t              guptasklock;
 	struct list_head        guptasks;
 	atomic_t		mmu_invalidations;
+	uint32_t		last_set_pages;
 	struct list_head        list;
 };
 
@@ -670,8 +671,10 @@ int amdgpu_ttm_tt_get_user_pages(struct ttm_tt *ttm, struct page **pages)
 
 void amdgpu_ttm_tt_set_user_pages(struct ttm_tt *ttm, struct page **pages)
 {
+	struct amdgpu_ttm_tt *gtt = (void *)ttm;
 	unsigned i;
 
+	gtt->last_set_pages = atomic_read(&gtt->mmu_invalidations);
 	for (i = 0; i < ttm->num_pages; ++i) {
 		if (ttm->pages[i])
 			put_page(ttm->pages[i]);
@@ -1023,6 +1026,7 @@ int amdgpu_ttm_tt_set_userptr(struct ttm_tt *ttm, uint64_t addr,
 	spin_lock_init(&gtt->guptasklock);
 	INIT_LIST_HEAD(&gtt->guptasks);
 	atomic_set(&gtt->mmu_invalidations, 0);
+	gtt->last_set_pages = 0;
 
 	return 0;
 }
@@ -1075,6 +1079,16 @@ bool amdgpu_ttm_tt_userptr_invalidated(struct ttm_tt *ttm,
 	return prev_invalidated != *last_invalidated;
 }
 
+bool amdgpu_ttm_tt_userptr_needs_pages(struct ttm_tt *ttm)
+{
+	struct amdgpu_ttm_tt *gtt = (void *)ttm;
+
+	if (gtt == NULL || !gtt->userptr)
+		return false;
+
+	return atomic_read(&gtt->mmu_invalidations) != gtt->last_set_pages;
+}
+
 bool amdgpu_ttm_tt_is_readonly(struct ttm_tt *ttm)
 {
 	struct amdgpu_ttm_tt *gtt = (void *)ttm;
Signature · 6 lines
2.7.4

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

PATCH 3/6 drm/amdgpu: move userptr BOs to CPU domain during CS

In reply to a message not in the archive

From: Christian König <[email protected]>

Instead of moving them in the MMU notifier move them during CS.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c | 6 ++++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_mn.c | 5 -----
 2 files changed, 6 insertions(+), 5 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +6 -0
index 19c5649..aa45e25f 100644
@@ -472,6 +472,12 @@ static int amdgpu_cs_list_validate(struct amdgpu_cs_parser *p,
 		/* Check if we have user pages and nobody bound the BO already */
 		if (amdgpu_ttm_tt_userptr_needs_pages(bo->tbo.ttm) &&
 		    lobj->user_pages) {
+			amdgpu_ttm_placement_from_domain(bo,
+							 AMDGPU_GEM_DOMAIN_CPU);
+			r = ttm_bo_validate(&bo->tbo, &bo->placement, true,
+					    false);
+			if (r)
+				return r;
 			amdgpu_ttm_tt_set_user_pages(bo->tbo.ttm,
 						     lobj->user_pages);
 			binding_userptr = true;
drivers/gpu/drm/amd/amdgpu/amdgpu_mn.c +0 -5
index 6558a3e..47800e4 100644
@@ -137,11 +137,6 @@ static void amdgpu_mn_invalidate_node(struct amdgpu_mn_node *node,
 		if (r <= 0)
 			DRM_ERROR("(%ld) failed to wait for user bo\n", r);
 
-		amdgpu_ttm_placement_from_domain(bo, AMDGPU_GEM_DOMAIN_CPU);
-		r = ttm_bo_validate(&bo->tbo, &bo->placement, false, false);
-		if (r)
-			DRM_ERROR("(%ld) failed to validate user bo\n", r);
-
 		amdgpu_bo_unreserve(bo);
 	}
 }
Signature · 6 lines
2.7.4

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

PATCH 4/6 drm/amdgpu: use a rw_semaphore for MMU notifiers

In reply to a message not in the archive

From: Christian König <[email protected]>

Allow at least some parallel processing.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_mn.c | 26 +++++++++++++-------------
 1 file changed, 13 insertions(+), 13 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_mn.c +13 -13
index 47800e4..cb10073 100644
@@ -50,7 +50,7 @@ struct amdgpu_mn {
 	struct hlist_node	node;
 
 	/* objects protected by lock */
-	struct mutex		lock;
+	struct rw_semaphore	lock;
 	struct rb_root		objects;
 };
 
@@ -74,7 +74,7 @@ static void amdgpu_mn_destroy(struct work_struct *work)
 	struct amdgpu_bo *bo, *next_bo;
 
 	mutex_lock(&adev->mn_lock);
-	mutex_lock(&rmn->lock);
+	down_write(&rmn->lock);
 	hash_del(&rmn->node);
 	rbtree_postorder_for_each_entry_safe(node, next_node, &rmn->objects,
 					     it.rb) {
@@ -84,7 +84,7 @@ static void amdgpu_mn_destroy(struct work_struct *work)
 		}
 		kfree(node);
 	}
-	mutex_unlock(&rmn->lock);
+	up_write(&rmn->lock);
 	mutex_unlock(&adev->mn_lock);
 	mmu_notifier_unregister_no_release(&rmn->mn, rmn->mm);
 	kfree(rmn);
@@ -158,7 +158,7 @@ static void amdgpu_mn_invalidate_page(struct mmu_notifier *mn,
 	struct amdgpu_mn *rmn = container_of(mn, struct amdgpu_mn, mn);
 	struct interval_tree_node *it;
 
-	mutex_lock(&rmn->lock);
+	down_read(&rmn->lock);
 
 	it = interval_tree_iter_first(&rmn->objects, address, address);
 	if (it) {
@@ -168,7 +168,7 @@ static void amdgpu_mn_invalidate_page(struct mmu_notifier *mn,
 		amdgpu_mn_invalidate_node(node, address, address);
 	}
 
-	mutex_unlock(&rmn->lock);
+	up_read(&rmn->lock);
 }
 
 /**
@@ -193,7 +193,7 @@ static void amdgpu_mn_invalidate_range_start(struct mmu_notifier *mn,
 	/* notification is exclusive, but interval is inclusive */
 	end -= 1;
 
-	mutex_lock(&rmn->lock);
+	down_read(&rmn->lock);
 
 	it = interval_tree_iter_first(&rmn->objects, start, end);
 	while (it) {
@@ -205,7 +205,7 @@ static void amdgpu_mn_invalidate_range_start(struct mmu_notifier *mn,
 		amdgpu_mn_invalidate_node(node, start, end);
 	}
 
-	mutex_unlock(&rmn->lock);
+	up_read(&rmn->lock);
 }
 
 static const struct mmu_notifier_ops amdgpu_mn_ops = {
@@ -246,7 +246,7 @@ static struct amdgpu_mn *amdgpu_mn_get(struct amdgpu_device *adev)
 	rmn->adev = adev;
 	rmn->mm = mm;
 	rmn->mn.ops = &amdgpu_mn_ops;
-	mutex_init(&rmn->lock);
+	init_rwsem(&rmn->lock);
 	rmn->objects = RB_ROOT;
 
 	r = __mmu_notifier_register(&rmn->mn, mm);
@@ -293,7 +293,7 @@ int amdgpu_mn_register(struct amdgpu_bo *bo, unsigned long addr)
 
 	INIT_LIST_HEAD(&bos);
 
-	mutex_lock(&rmn->lock);
+	down_write(&rmn->lock);
 
 	while ((it = interval_tree_iter_first(&rmn->objects, addr, end))) {
 		kfree(node);
@@ -307,7 +307,7 @@ int amdgpu_mn_register(struct amdgpu_bo *bo, unsigned long addr)
 	if (!node) {
 		node = kmalloc(sizeof(struct amdgpu_mn_node), GFP_KERNEL);
 		if (!node) {
-			mutex_unlock(&rmn->lock);
+			up_write(&rmn->lock);
 			return -ENOMEM;
 		}
 	}
@@ -322,7 +322,7 @@ int amdgpu_mn_register(struct amdgpu_bo *bo, unsigned long addr)
 
 	interval_tree_insert(&node->it, &rmn->objects);
 
-	mutex_unlock(&rmn->lock);
+	up_write(&rmn->lock);
 
 	return 0;
 }
@@ -348,7 +348,7 @@ void amdgpu_mn_unregister(struct amdgpu_bo *bo)
 		return;
 	}
 
-	mutex_lock(&rmn->lock);
+	down_write(&rmn->lock);
 
 	/* save the next list entry for later */
 	head = bo->mn_list.next;
@@ -363,6 +363,6 @@ void amdgpu_mn_unregister(struct amdgpu_bo *bo)
 		kfree(node);
 	}
 
-	mutex_unlock(&rmn->lock);
+	up_write(&rmn->lock);
 	mutex_unlock(&adev->mn_lock);
 }
Signature · 6 lines
2.7.4

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

PATCH 5/6 drm/amdgpu: stop reserving the BO in the MMU callback

In reply to a message not in the archive

From: Christian König <[email protected]>

Instead take the callback lock during the final parts of CS.

This should solve the last remaining locking order problems with BO reservations.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu.h    |  8 ++++++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c | 29 +++++++++++++++++++++++------
 drivers/gpu/drm/amd/amdgpu/amdgpu_mn.c | 22 +++++++++++++---------
 3 files changed, 44 insertions(+), 15 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu.h +8 -0
index 5d1f9cc..466330e 100644
@@ -180,6 +180,7 @@ struct amdgpu_cs_parser;
 struct amdgpu_job;
 struct amdgpu_irq_src;
 struct amdgpu_fpriv;
+struct amdgpu_mn;
 
 enum amdgpu_cp_irq {
 	AMDGPU_CP_IRQ_GFX_EOP = 0,
@@ -1059,6 +1060,7 @@ struct amdgpu_cs_parser {
 	/* buffer objects */
 	struct ww_acquire_ctx		ticket;
 	struct amdgpu_bo_list		*bo_list;
+	struct amdgpu_mn		*mn;
 	struct amdgpu_bo_list_entry	vm_pd;
 	struct list_head		validated;
 	struct dma_fence		*fence;
@@ -1200,14 +1202,20 @@ void amdgpu_test_moves(struct amdgpu_device *adev);
  * MMU Notifier
  */
 #if defined(CONFIG_MMU_NOTIFIER)
+struct amdgpu_mn *amdgpu_mn_get(struct amdgpu_device *adev);
 int amdgpu_mn_register(struct amdgpu_bo *bo, unsigned long addr);
 void amdgpu_mn_unregister(struct amdgpu_bo *bo);
+void amdgpu_mn_lock(struct amdgpu_mn *mn);
+void amdgpu_mn_unlock(struct amdgpu_mn *mn);
 #else
+struct amdgpu_mn *amdgpu_mn_get(struct amdgpu_device *adev) { return NULL; }
 static inline int amdgpu_mn_register(struct amdgpu_bo *bo, unsigned long addr)
 {
 	return -ENODEV;
 }
 static inline void amdgpu_mn_unregister(struct amdgpu_bo *bo) {}
+void amdgpu_mn_lock(struct amdgpu_mn *mn) {}
+void amdgpu_mn_unlock(struct amdgpu_mn *mn) {}
 #endif
 
 /*
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +23 -6
index aa45e25f..2857eb8 100644
@@ -510,8 +510,11 @@ static int amdgpu_cs_parser_bos(struct amdgpu_cs_parser *p,
 	INIT_LIST_HEAD(&p->validated);
 
 	p->bo_list = amdgpu_bo_list_get(fpriv, cs->in.bo_list_handle);
-	if (p->bo_list)
+	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);
+	}
 
 	INIT_LIST_HEAD(&duplicates);
 	amdgpu_vm_get_pd_bo(&fpriv->vm, &p->validated, &p->vm_pd);
@@ -718,11 +721,7 @@ static void amdgpu_cs_parser_fini(struct amdgpu_cs_parser *parser, int error,
 {
 	unsigned i;
 
-	if (!error)
-		ttm_eu_fence_buffer_objects(&parser->ticket,
-					    &parser->validated,
-					    parser->fence);
-	else if (backoff)
+	if (error && backoff)
 		ttm_eu_backoff_reservation(&parser->ticket,
 					   &parser->validated);
 	dma_fence_put(parser->fence);
@@ -1041,14 +1040,29 @@ static int amdgpu_cs_submit(struct amdgpu_cs_parser *p,
 	struct amdgpu_ring *ring = p->job->ring;
 	struct amd_sched_entity *entity = &p->ctx->rings[ring->idx].entity;
 	struct amdgpu_job *job;
+	unsigned i;
 	int r;
 
+	amdgpu_mn_lock(p->mn);
+	if (p->bo_list) {
+		for (i = p->bo_list->first_userptr;
+		     i < p->bo_list->num_entries; ++i) {
+			struct amdgpu_bo *bo = p->bo_list->array[i].robj;
+
+			if (amdgpu_ttm_tt_userptr_needs_pages(bo->tbo.ttm)) {
+				amdgpu_mn_unlock(p->mn);
+				return -ERESTARTSYS;
+			}
+		}
+	}
+
 	job = p->job;
 	p->job = NULL;
 
 	r = amd_sched_job_init(&job->base, &ring->sched, entity, p->filp);
 	if (r) {
 		amdgpu_job_free(job);
+		amdgpu_mn_unlock(p->mn);
 		return r;
 	}
 
@@ -1062,6 +1076,9 @@ static int amdgpu_cs_submit(struct amdgpu_cs_parser *p,
 	trace_amdgpu_cs_ioctl(job);
 	amd_sched_entity_push_job(&job->base);
 
+	ttm_eu_fence_buffer_objects(&p->ticket, &p->validated, p->fence);
+	amdgpu_mn_unlock(p->mn);
+
 	return 0;
 }
 
drivers/gpu/drm/amd/amdgpu/amdgpu_mn.c +13 -9
index cb10073..651a746 100644
@@ -126,18 +126,10 @@ static void amdgpu_mn_invalidate_node(struct amdgpu_mn_node *node,
 		if (!amdgpu_ttm_tt_affect_userptr(bo->tbo.ttm, start, end))
 			continue;
 
-		r = amdgpu_bo_reserve(bo, true);
-		if (r) {
-			DRM_ERROR("(%ld) failed to reserve user bo\n", r);
-			continue;
-		}
-
 		r = reservation_object_wait_timeout_rcu(bo->tbo.resv,
 			true, false, MAX_SCHEDULE_TIMEOUT);
 		if (r <= 0)
 			DRM_ERROR("(%ld) failed to wait for user bo\n", r);
-
-		amdgpu_bo_unreserve(bo);
 	}
 }
 
@@ -221,7 +213,7 @@ static const struct mmu_notifier_ops amdgpu_mn_ops = {
  *
  * Creates a notifier context for current->mm.
  */
-static struct amdgpu_mn *amdgpu_mn_get(struct amdgpu_device *adev)
+struct amdgpu_mn *amdgpu_mn_get(struct amdgpu_device *adev)
 {
 	struct mm_struct *mm = current->mm;
 	struct amdgpu_mn *rmn;
@@ -366,3 +358,15 @@ void amdgpu_mn_unregister(struct amdgpu_bo *bo)
 	up_write(&rmn->lock);
 	mutex_unlock(&adev->mn_lock);
 }
+
+void amdgpu_mn_lock(struct amdgpu_mn *mn)
+{
+	if (mn)
+		down_write(&mn->lock);
+}
+
+void amdgpu_mn_unlock(struct amdgpu_mn *mn)
+{
+	if (mn)
+		up_write(&mn->lock);
+}
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): 1504625871-1756-5-git-send-email-deathsimple-ANTagKRnAhcb1SvskN2V4Q@public.gmane.org

#7 lore raw

Re: [PATCH 5/6] drm/amdgpu: stop reserving the BO in the MMU callback

drivers/gpu/drm/amd/amdgpu/amdgpu.h void amdgpu_test_moves(struct amdgpu_device *adev); · line 1218

Quoted 8 lines · Christian König
drivers/gpu/drm/amd/amdgpu/amdgpu.h +3 -0
+struct amdgpu_mn *amdgpu_mn_get(struct amdgpu_device *adev) { return NULL; }
 static inline int amdgpu_mn_register(struct amdgpu_bo *bo, unsigned long addr)
 {
 	return -ENODEV;
 }
 static inline void amdgpu_mn_unregister(struct amdgpu_bo *bo) {}
+void amdgpu_mn_lock(struct amdgpu_mn *mn) {}
+void amdgpu_mn_unlock(struct amdgpu_mn *mn) {}

The added dummy unctions here should be static inline.

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

#8 lore raw

PATCH 6/6 drm/amdgpu: keep the MMU lock until the update ends

In reply to a message not in the archive

From: Christian König <[email protected]>

This is quite controversial because it adds another lock which is held during page table updates, but I don't see much other option.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_mn.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_mn.c +19 -0
index 651a746..a5a560b 100644
@@ -196,6 +196,24 @@ static void amdgpu_mn_invalidate_range_start(struct mmu_notifier *mn,
 
 		amdgpu_mn_invalidate_node(node, start, end);
 	}
+}
+
+/**
+ * amdgpu_mn_invalidate_range_end - callback to notify about mm change
+ *
+ * @mn: our notifier
+ * @mn: the mm this callback is about
+ * @start: start of updated range
+ * @end: end of updated range
+ *
+ * Release the lock again to allow new command submissions.
+ */
+static void amdgpu_mn_invalidate_range_end(struct mmu_notifier *mn,
+					   struct mm_struct *mm,
+					   unsigned long start,
+					   unsigned long end)
+{
+	struct amdgpu_mn *rmn = container_of(mn, struct amdgpu_mn, mn);
 
 	up_read(&rmn->lock);
 }
@@ -204,6 +222,7 @@ static const struct mmu_notifier_ops amdgpu_mn_ops = {
 	.release = amdgpu_mn_release,
 	.invalidate_page = amdgpu_mn_invalidate_page,
 	.invalidate_range_start = amdgpu_mn_invalidate_range_start,
+	.invalidate_range_end = amdgpu_mn_invalidate_range_end,
 };
 
 /**
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): 1504625871-1756-6-git-send-email-deathsimple-ANTagKRnAhcb1SvskN2V4Q@public.gmane.org

#10 lore raw

Re: [PATCH 6/6] drm/amdgpu: keep the MMU lock until the update ends

I believe this can lead to lock warnings when multiple MMU notifier pairs are happening at the same time. They're probably harmless, because read locks are not exclusive. Below is an example I got during process termination of an HSA process.

Is there some kind of lock annotation that can prevent this message?

Regards,
Felix

[  232.510984] ============================================
[  232.510984] WARNING: possible recursive locking detected
[  232.510985] 4.12.0-kfd-fkuehlin #1 Tainted: G            E  
[  232.510986] --------------------------------------------
[  232.510986] test_integer_op/2692 is trying to acquire lock:
[  232.510987]  (&rmn->lock){++++.+}, at: [<ffffffffc01bf368>] amdgpu_mn_invalidate_range_start_hsa+0x28/0xc0 [amdgpu]
[  232.511029] 
               but task is already holding lock:
[  232.511029]  (&rmn->lock){++++.+}, at: [<ffffffffc01bf368>] amdgpu_mn_invalidate_range_start_hsa+0x28/0xc0 [amdgpu]
[  232.511052] 
               other info that might help us debug this:
[  232.511052]  Possible unsafe locking scenario:
[  232.511053]        CPU0
[  232.511053]        ----
[  232.511053]   lock(&rmn->lock);
[  232.511053]   lock(&rmn->lock);
[  232.511054] 
                *** DEADLOCK ***
[  232.511054]  May be due to missing lock nesting notation
[  232.511055] 3 locks held by test_integer_op/2692:
[  232.511055]  #0:  (&mm->mmap_sem){++++++}, at: [<ffffffff8d1a34e1>] SyS_madvise+0x361/0x880
[  232.511058]  #1:  (&rmn->lock){++++.+}, at: [<ffffffffc01bf368>] amdgpu_mn_invalidate_range_start_hsa+0x28/0xc0 [amdgpu]
[  232.511080]  #2:  (srcu){......}, at: [<ffffffff8d1b6d90>] __mmu_notifier_invalidate_range_start+0x0/0xb0
[  232.511083] 
               stack backtrace:
[  232.511084] CPU: 4 PID: 2692 Comm: test_integer_op Tainted: G            E   4.12.0-kfd-fkuehlin #1
[  232.511085] Hardware name: ASUS All Series/Z97-PRO(Wi-Fi ac)/USB 3.1, BIOS 2401 04/27/2015
[  232.511085] Call Trace:
[  232.511088]  dump_stack+0x85/0xc7
[  232.511090]  __lock_acquire+0x14da/0x1540
[  232.511091]  ? __lock_acquire+0x28d/0x1540
[  232.511093]  lock_acquire+0x59/0x80
[  232.511093]  ? lock_acquire+0x59/0x80
[  232.511115]  ? amdgpu_mn_invalidate_range_start_hsa+0x28/0xc0 [amdgpu]
[  232.511117]  down_read+0x3e/0x70
[  232.511139]  ? amdgpu_mn_invalidate_range_start_hsa+0x28/0xc0 [amdgpu]
[  232.511161]  amdgpu_mn_invalidate_range_start_hsa+0x28/0xc0 [amdgpu]
[  232.511162]  __mmu_notifier_invalidate_range_start+0x71/0xb0
[  232.511164]  __split_huge_pmd+0x296/0x890
[  232.511166]  unmap_page_range+0x353/0x930
[  232.511167]  ? reacquire_held_locks+0x67/0x170
[  232.511168]  unmap_single_vma+0x54/0xd0
[  232.511170]  zap_page_range+0xac/0x110
[  232.511170]  ? lock_acquire+0x59/0x80
[  232.511171]  ? find_vma+0x63/0x70
[  232.511172]  SyS_madvise+0x40a/0x880
[  232.511173]  ? trace_hardirqs_on_caller+0x11f/0x190
[  232.511174]  entry_SYSCALL_64_fastpath+0x1f/0xbe
[  232.511175] RIP: 0033:0x7f35d0e827c7
[  232.511176] RSP: 002b:00007f35c3ffef18 EFLAGS: 00000206 ORIG_RAX: 000000000000001c
[  232.511176] RAX: ffffffffffffffda RBX: 00007f35970028b0 RCX: 00007f35d0e827c7
[  232.511177] RDX: 0000000000000004 RSI: 00000000007fb000 RDI: 00007f35c37ff000
[  232.511177] RBP: 00007f35970028e8 R08: 0000000000000000 R09: 000000000000001e
[  232.511177] R10: 0000000000000000 R11: 0000000000000206 R12: 0000000000000004
[  232.511178] R13: 00007f35970028c0 R14: 0000000000630250 R15: 0000000000000007
[  232.511179]  ? entry_SYSCALL_64_fastpath+0x1f/0xbe
Quoted 40 lines · Christian König

From: Christian König <[email protected]>

This is quite controversial because it adds another lock which is held during page table updates, but I don't see much other option.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_mn.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_mn.c +18 -0
index 651a746..a5a560b 100644
@@ -196,6 +196,24 @@ static void amdgpu_mn_invalidate_range_start(struct mmu_notifier *mn,
 
 		amdgpu_mn_invalidate_node(node, start, end);
 	}
+}
+
+/**
+ * amdgpu_mn_invalidate_range_end - callback to notify about mm change
+ *
+ * @mn: our notifier
+ * @mn: the mm this callback is about
+ * @start: start of updated range
+ * @end: end of updated range
+ *
+ * Release the lock again to allow new command submissions.
+ */
+static void amdgpu_mn_invalidate_range_end(struct mmu_notifier *mn,
+					   struct mm_struct *mm,
+					   unsigned long start,
+					   unsigned long end)
+{
+	struct amdgpu_mn *rmn = container_of(mn, struct amdgpu_mn, mn);
 
 	up_read(&rmn->lock);
 }

drivers/gpu/drm/amd/amdgpu/amdgpu_mn.c static const struct mmu_notifier_ops amdgpu_mn_ops = { · line 228

Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_mn.c +1 -0
@@ -204,6 +222,7 @@ static const struct mmu_notifier_ops amdgpu_mn_ops = {
 	.release = amdgpu_mn_release,
 	.invalidate_page = amdgpu_mn_invalidate_page,
 	.invalidate_range_start = amdgpu_mn_invalidate_range_start,
+	.invalidate_range_end = amdgpu_mn_invalidate_range_end,
 };
 
 /**

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

Reviewed-by
#11 lore raw
In reply to a message not in the archive

I made one comment on patch 5. Other than that, this series is

This is the second week I'll get very little productive work done, because I'm busy debugging memory manager changes and patching KFD to keep it working. This series will require some changes to userptr handling in KFD. Once KFD upstreaming is done, you'll have to change amdgpu_amdkfd_gpuvm code when you're messing around with the memory manager code. Feels like you're trying to get in all your changes as long as that's still my problem. :P

The more you keep changing the memory manager, the longer this whole upstreaming exercise will take me. My biggest motivation to finish upstreaming is, so that I can stop adapting your memory manager patches. This is not helping! ;)

Cheers,
Felix

Quoted 91 lines · Christian König

From: Christian König <[email protected]>

This didn't helped as intended, just simplify the code.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c  | 12 +-----------
 drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c |  4 ----
 drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c |  4 ++++
 3 files changed, 5 insertions(+), 15 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +1 -11
index 4ab35db..e6e04d3 100644
@@ -497,18 +497,14 @@ static int amdgpu_cs_parser_bos(struct amdgpu_cs_parser *p,
 	struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
 	struct amdgpu_bo_list_entry *e;
 	struct list_head duplicates;
-	bool need_mmap_lock = false;
 	unsigned i, tries = 10;
 	int r;
 
 	INIT_LIST_HEAD(&p->validated);
 
 	p->bo_list = amdgpu_bo_list_get(fpriv, cs->in.bo_list_handle);
-	if (p->bo_list) {
-		need_mmap_lock = p->bo_list->first_userptr !=
-			p->bo_list->num_entries;
+	if (p->bo_list)
 		amdgpu_bo_list_get_list(p->bo_list, &p->validated);
-	}
 
 	INIT_LIST_HEAD(&duplicates);
 	amdgpu_vm_get_pd_bo(&fpriv->vm, &p->validated, &p->vm_pd);
@@ -516,9 +512,6 @@ static int amdgpu_cs_parser_bos(struct amdgpu_cs_parser *p,
 	if (p->uf_entry.robj)
 		list_add(&p->uf_entry.tv.head, &p->validated);
 
-	if (need_mmap_lock)
-		down_read(&current->mm->mmap_sem);
-
 	while (1) {
 		struct list_head need_pages;
 		unsigned i;
@@ -670,9 +663,6 @@ static int amdgpu_cs_parser_bos(struct amdgpu_cs_parser *p,
 
 error_free_pages:
 
-	if (need_mmap_lock)
-		up_read(&current->mm->mmap_sem);
-
 	if (p->bo_list) {
 		for (i = p->bo_list->first_userptr;
 		     i < p->bo_list->num_entries; ++i) {
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +0 -4
index f1e61b3..b0d45c8 100644
@@ -318,8 +318,6 @@ int amdgpu_gem_userptr_ioctl(struct drm_device *dev, void *data,
 	}
 
 	if (args->flags & AMDGPU_GEM_USERPTR_VALIDATE) {
-		down_read(&current->mm->mmap_sem);
-
 		r = amdgpu_ttm_tt_get_user_pages(bo->tbo.ttm,
 						 bo->tbo.ttm->pages);
 		if (r)
@@ -334,8 +332,6 @@ int amdgpu_gem_userptr_ioctl(struct drm_device *dev, void *data,
 		amdgpu_bo_unreserve(bo);
 		if (r)
 			goto free_pages;
-
-		up_read(&current->mm->mmap_sem);
 	}
 
 	r = drm_gem_handle_create(filp, gobj, &handle);
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +2 -0
index 07c3f11..d2ce1a7 100644
@@ -622,6 +622,8 @@ int amdgpu_ttm_tt_get_user_pages(struct ttm_tt *ttm, struct page **pages)
 	if (!(gtt->userflags & AMDGPU_GEM_USERPTR_READONLY))
 		flags |= FOLL_WRITE;
 
+	down_read(&current->mm->mmap_sem);
+
 	if (gtt->userflags & AMDGPU_GEM_USERPTR_ANONONLY) {
 		/* check that we only use anonymous memory
 		   to prevent problems with writeback */

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c int amdgpu_ttm_tt_get_user_pages(struct ttm_tt *ttm, struct page **pages) · line 666

Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +2 -0
@@ -657,6 +659,8 @@ 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);
+
 	return 0;
 
 release_pages:

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

#12 lore raw
In reply to a message not in the archive

I found another problem as I was looking at adapting KFD's userptr code.

Quoted 12 lines · Christian König
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +2 -0
@@ -622,6 +622,8 @@ int amdgpu_ttm_tt_get_user_pages(struct ttm_tt *ttm, struct page **pages)
 	if (!(gtt->userflags & AMDGPU_GEM_USERPTR_READONLY))
 		flags |= FOLL_WRITE;
 
+	down_read(&current->mm->mmap_sem);
+
 	if (gtt->userflags & AMDGPU_GEM_USERPTR_ANONONLY) {
 		/* check that we only use anonymous memory
 		   to prevent problems with writeback */

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c int amdgpu_ttm_tt_get_user_pages(struct ttm_tt *ttm, struct page **pages) · line 666

Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +2 -0
@@ -657,6 +659,8 @@ 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);
+
 	return 0;
 
 release_pages:

You need another up_read in the release_pages error handing path.

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