Skip to contentSkip to conversation

drm/amdgpu: move taking mmap_sem into get_user_pages v2

PATCH In review Christian König 3 participants 10 messages Sep 6, 2017 5 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 v2

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

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

v2: unlock mmap_sem in the error path as well

---
 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 |  8 +++++++-
 3 files changed, 8 insertions(+), 16 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 +7 -1
index 07c3f11..facea17 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 */
@@ -629,8 +631,10 @@ int amdgpu_ttm_tt_get_user_pages(struct ttm_tt *ttm, struct page **pages)
 		struct vm_area_struct *vma;
 
 		vma = find_vma(gtt->usermm, gtt->userptr);
-		if (!vma || vma->vm_file || vma->vm_end < end)
+		if (!vma || vma->vm_file || vma->vm_end < end) {
+			up_read(&current->mm->mmap_sem);
 			return -EPERM;
+		}
 	}
 
 	do {
@@ -657,10 +661,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);
 	return 0;
 
 release_pages:
 	release_pages(pages, pinned, 0);
+	up_read(&current->mm->mmap_sem);
 	return r;
 }
 
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): 1504701283-3317-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 facea17..21aaf03 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;
 };
 
@@ -672,8 +673,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]);
@@ -1025,6 +1028,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;
 }
@@ -1077,6 +1081,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 v2

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.

v2: still mark pages as accessed/dirty

---
 drivers/gpu/drm/amd/amdgpu/amdgpu.h     |  1 +
 drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c  |  6 ++++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_mn.c  |  5 +----
 drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c | 27 +++++++++++++++++++--------
 4 files changed, 27 insertions(+), 12 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu.h +1 -0
index 5d1f9cc..c9f1f995 100644
@@ -1803,6 +1803,7 @@ void amdgpu_ttm_placement_from_domain(struct amdgpu_bo *abo, u32 domain);
 bool amdgpu_ttm_bo_is_amdgpu_bo(struct ttm_buffer_object *bo);
 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);
+void amdgpu_ttm_tt_mark_user_pages(struct ttm_tt *ttm);
 int amdgpu_ttm_tt_set_userptr(struct ttm_tt *ttm, uint64_t addr,
 				     uint32_t flags);
 bool amdgpu_ttm_tt_has_userptr(struct ttm_tt *ttm);
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 +1 -4
index 6558a3e..df85a1314 100644
@@ -137,10 +137,7 @@ 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_ttm_tt_mark_user_pages(bo->tbo.ttm);
 
 		amdgpu_bo_unreserve(bo);
 	}
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +19 -8
index 21aaf03..0d4c2c5 100644
@@ -685,6 +685,24 @@ void amdgpu_ttm_tt_set_user_pages(struct ttm_tt *ttm, struct page **pages)
 	}
 }
 
+void amdgpu_ttm_tt_mark_user_pages(struct ttm_tt *ttm)
+{
+	struct amdgpu_ttm_tt *gtt = (void *)ttm;
+	unsigned i;
+
+	for (i = 0; i < ttm->num_pages; ++i) {
+		struct page *page = ttm->pages[i];
+
+		if (!page)
+			continue;
+
+		if (!(gtt->userflags & AMDGPU_GEM_USERPTR_READONLY))
+			set_page_dirty(page);
+
+		mark_page_accessed(page);
+	}
+}
+
 static void amdgpu_trace_dma_map(struct ttm_tt *ttm)
 {
 	struct amdgpu_device *adev = amdgpu_ttm_adev(ttm->bdev);
@@ -740,7 +758,6 @@ static void amdgpu_ttm_tt_unpin_userptr(struct ttm_tt *ttm)
 {
 	struct amdgpu_device *adev = amdgpu_ttm_adev(ttm->bdev);
 	struct amdgpu_ttm_tt *gtt = (void *)ttm;
-	struct sg_page_iter sg_iter;
 
 	int write = !(gtt->userflags & AMDGPU_GEM_USERPTR_READONLY);
 	enum dma_data_direction direction = write ?
@@ -753,13 +770,7 @@ static void amdgpu_ttm_tt_unpin_userptr(struct ttm_tt *ttm)
 	/* free the sg table and pages again */
 	dma_unmap_sg(adev->dev, ttm->sg->sgl, ttm->sg->nents, direction);
 
-	for_each_sg_page(ttm->sg->sgl, &sg_iter, ttm->sg->nents, 0) {
-		struct page *page = sg_page_iter_page(&sg_iter);
-		if (!(gtt->userflags & AMDGPU_GEM_USERPTR_READONLY))
-			set_page_dirty(page);
-
-		mark_page_accessed(page);
-	}
+	amdgpu_ttm_tt_mark_user_pages(ttm);
 
 	amdgpu_trace_dma_unmap(ttm);
 
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 df85a1314..6d216ab 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);
@@ -160,7 +160,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) {
@@ -170,7 +170,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);
 }
 
 /**
@@ -195,7 +195,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) {
@@ -207,7 +207,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 = {
@@ -248,7 +248,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);
@@ -295,7 +295,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);
@@ -309,7 +309,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;
 		}
 	}
@@ -324,7 +324,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;
 }
@@ -350,7 +350,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;
@@ -365,6 +365,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 v2

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.

v2: rebase, make dummy functions static inline

---
 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 c9f1f995..480ee2f 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) {}
+static inline void amdgpu_mn_lock(struct amdgpu_mn *mn) {}
+static inline 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 6d216ab..459f02e 100644
@@ -126,20 +126,12 @@ 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_ttm_tt_mark_user_pages(bo->tbo.ttm);
-
-		amdgpu_bo_unreserve(bo);
 	}
 }
 
@@ -223,7 +215,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;
@@ -368,3 +360,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): 1504701283-3317-5-git-send-email-deathsimple-ANTagKRnAhcb1SvskN2V4Q@public.gmane.org

#8 lore raw

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

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.

v2: allow multiple updates to be in flight at the same time

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_mn.c | 108 +++++++++++++++++++++++++++++++--
 1 file changed, 104 insertions(+), 4 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_mn.c +104 -4
index 459f02e..333521e 100644
@@ -52,6 +52,10 @@ struct amdgpu_mn {
 	/* objects protected by lock */
 	struct rw_semaphore	lock;
 	struct rb_root		objects;
+
+	/* protected by task_lock */
+	spinlock_t		task_lock;
+	DECLARE_HASHTABLE(task_hash, 7);
 };
 
 struct amdgpu_mn_node {
@@ -59,6 +63,12 @@ struct amdgpu_mn_node {
 	struct list_head		bos;
 };
 
+struct amdgpu_mn_task {
+	struct hlist_node	node;
+	struct task_struct	*task;
+	unsigned		recursion;
+};
+
 /**
  * amdgpu_mn_destroy - destroy the rmn
  *
@@ -107,6 +117,75 @@ static void amdgpu_mn_release(struct mmu_notifier *mn,
 }
 
 /**
+ * amdgpu_mn_read_lock - take the rmn read lock
+ *
+ * @rmn: our notifier
+ *
+ * Take the rmn read side lock.
+ */
+static void amdgpu_mn_read_lock(struct amdgpu_mn *rmn)
+{
+	struct amdgpu_mn_task *task;
+
+	spin_lock(&rmn->task_lock);
+	hash_for_each_possible(rmn->task_hash, task, node,
+			       (unsigned long)current) {
+		if (task->task != current)
+			continue;
+
+		spin_unlock(&rmn->task_lock);
+		++task->recursion;
+		return;
+	}
+	spin_unlock(&rmn->task_lock);
+
+	task = kmalloc(sizeof(*task), GFP_KERNEL);
+	task->task = current;
+	task->recursion = 1;
+
+	down_read(&rmn->lock);
+
+	spin_lock(&rmn->task_lock);
+	hash_add(rmn->task_hash, &task->node, (unsigned long)current);
+	spin_unlock(&rmn->task_lock);
+}
+
+/**
+ * amdgpu_mn_read_unlock - drop the rmn read lock
+ *
+ * @rmn: our notifier
+ *
+ * Drop the rmn read side lock.
+ */
+static void amdgpu_mn_read_unlock(struct amdgpu_mn *rmn)
+{
+	struct amdgpu_mn_task *task;
+
+	spin_lock(&rmn->task_lock);
+	hash_for_each_possible(rmn->task_hash, task, node,
+			       (unsigned long)current) {
+		if (task->task != current)
+			continue;
+
+		spin_unlock(&rmn->task_lock);
+		if (--task->recursion) {
+			return;
+		}
+
+		spin_lock(&rmn->task_lock);
+		hash_del(&task->node);
+		spin_unlock(&rmn->task_lock);
+		kfree(task);
+
+		up_read(&rmn->lock);
+		return;
+	}
+	spin_unlock(&rmn->task_lock);
+
+	BUG();
+}
+
+/**
  * amdgpu_mn_invalidate_node - unmap all BOs of a node
  *
  * @node: the node with the BOs to unmap
@@ -152,7 +231,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;
 
-	down_read(&rmn->lock);
+	amdgpu_mn_read_lock(rmn);
 
 	it = interval_tree_iter_first(&rmn->objects, address, address);
 	if (it) {
@@ -162,7 +241,7 @@ static void amdgpu_mn_invalidate_page(struct mmu_notifier *mn,
 		amdgpu_mn_invalidate_node(node, address, address);
 	}
 
-	up_read(&rmn->lock);
+	amdgpu_mn_read_unlock(rmn);
 }
 
 /**
@@ -187,7 +266,7 @@ static void amdgpu_mn_invalidate_range_start(struct mmu_notifier *mn,
 	/* notification is exclusive, but interval is inclusive */
 	end -= 1;
 
-	down_read(&rmn->lock);
+	amdgpu_mn_read_lock(rmn);
 
 	it = interval_tree_iter_first(&rmn->objects, start, end);
 	while (it) {
@@ -198,14 +277,33 @@ 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);
+	amdgpu_mn_read_unlock(rmn);
 }
 
 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,
 };
 
 /**
@@ -242,6 +340,8 @@ struct amdgpu_mn *amdgpu_mn_get(struct amdgpu_device *adev)
 	rmn->mn.ops = &amdgpu_mn_ops;
 	init_rwsem(&rmn->lock);
 	rmn->objects = RB_ROOT;
+	spin_lock_init(&rmn->task_lock);
+	hash_init(rmn->task_hash);
 
 	r = __mmu_notifier_register(&rmn->mn, mm);
 	if (r)
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): 1504701283-3317-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 v2

I think all this does (compared to v1) is, avoid taking the same read lock twice, but keep track of how many readers there are outside the rw_semaphore to avoid recursive lock warnings.

I don't really understand why you need to count readers per task (using the task hashtable in the amdgpu_mn struct). The amdgpu_mn structure is already per process. Each instance should only be used by one process (as identified by its mm_struct). So the counting of readers could be done with a simple atomic counter in the amdgpu_mn struct:

lock:

    if (atomic_inc_return(mn->read_count) == 1)
        down_read(&mn->lock);

unlock:

    if (atomic_dec_return(mn->read_count) == 0)
        up_read(&mn->lock);

Am I missing something? Even if there are multiple threads in the process, it shouldn't matter because they all need to use the same read lock anyway and as long as any thread holds the read lock, any other thread is safe to use it. Maybe there is a concern about locking a semaphore in one thread and unlocking it on a different thread. I know that mutexes have quite strict semantics in this regard. But I think semaphores are less strict.

Regards,
Felix

Quoted 180 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.

v2: allow multiple updates to be in flight at the same time

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_mn.c | 108 +++++++++++++++++++++++++++++++--
 1 file changed, 104 insertions(+), 4 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_mn.c +102 -4
index 459f02e..333521e 100644
@@ -52,6 +52,10 @@ struct amdgpu_mn {
 	/* objects protected by lock */
 	struct rw_semaphore	lock;
 	struct rb_root		objects;
+
+	/* protected by task_lock */
+	spinlock_t		task_lock;
+	DECLARE_HASHTABLE(task_hash, 7);
 };
 
 struct amdgpu_mn_node {
@@ -59,6 +63,12 @@ struct amdgpu_mn_node {
 	struct list_head		bos;
 };
 
+struct amdgpu_mn_task {
+	struct hlist_node	node;
+	struct task_struct	*task;
+	unsigned		recursion;
+};
+
 /**
  * amdgpu_mn_destroy - destroy the rmn
  *
@@ -107,6 +117,75 @@ static void amdgpu_mn_release(struct mmu_notifier *mn,
 }
 
 /**
+ * amdgpu_mn_read_lock - take the rmn read lock
+ *
+ * @rmn: our notifier
+ *
+ * Take the rmn read side lock.
+ */
+static void amdgpu_mn_read_lock(struct amdgpu_mn *rmn)
+{
+	struct amdgpu_mn_task *task;
+
+	spin_lock(&rmn->task_lock);
+	hash_for_each_possible(rmn->task_hash, task, node,
+			       (unsigned long)current) {
+		if (task->task != current)
+			continue;
+
+		spin_unlock(&rmn->task_lock);
+		++task->recursion;
+		return;
+	}
+	spin_unlock(&rmn->task_lock);
+
+	task = kmalloc(sizeof(*task), GFP_KERNEL);
+	task->task = current;
+	task->recursion = 1;
+
+	down_read(&rmn->lock);
+
+	spin_lock(&rmn->task_lock);
+	hash_add(rmn->task_hash, &task->node, (unsigned long)current);
+	spin_unlock(&rmn->task_lock);
+}
+
+/**
+ * amdgpu_mn_read_unlock - drop the rmn read lock
+ *
+ * @rmn: our notifier
+ *
+ * Drop the rmn read side lock.
+ */
+static void amdgpu_mn_read_unlock(struct amdgpu_mn *rmn)
+{
+	struct amdgpu_mn_task *task;
+
+	spin_lock(&rmn->task_lock);
+	hash_for_each_possible(rmn->task_hash, task, node,
+			       (unsigned long)current) {
+		if (task->task != current)
+			continue;
+
+		spin_unlock(&rmn->task_lock);
+		if (--task->recursion) {
+			return;
+		}
+
+		spin_lock(&rmn->task_lock);
+		hash_del(&task->node);
+		spin_unlock(&rmn->task_lock);
+		kfree(task);
+
+		up_read(&rmn->lock);
+		return;
+	}
+	spin_unlock(&rmn->task_lock);
+
+	BUG();
+}
+
+/**
  * amdgpu_mn_invalidate_node - unmap all BOs of a node
  *
  * @node: the node with the BOs to unmap
@@ -152,7 +231,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;
 
-	down_read(&rmn->lock);
+	amdgpu_mn_read_lock(rmn);
 
 	it = interval_tree_iter_first(&rmn->objects, address, address);
 	if (it) {
@@ -162,7 +241,7 @@ static void amdgpu_mn_invalidate_page(struct mmu_notifier *mn,
 		amdgpu_mn_invalidate_node(node, address, address);
 	}
 
-	up_read(&rmn->lock);
+	amdgpu_mn_read_unlock(rmn);
 }
 
 /**
@@ -187,7 +266,7 @@ static void amdgpu_mn_invalidate_range_start(struct mmu_notifier *mn,
 	/* notification is exclusive, but interval is inclusive */
 	end -= 1;
 
-	down_read(&rmn->lock);
+	amdgpu_mn_read_lock(rmn);
 
 	it = interval_tree_iter_first(&rmn->objects, start, end);
 	while (it) {
@@ -198,14 +277,33 @@ 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);
+	amdgpu_mn_read_unlock(rmn);
 }
 
 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,
 };
 
 /**

drivers/gpu/drm/amd/amdgpu/amdgpu_mn.c struct amdgpu_mn *amdgpu_mn_get(struct amdgpu_device *adev) · line 347

Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_mn.c +2 -0
@@ -242,6 +340,8 @@ struct amdgpu_mn *amdgpu_mn_get(struct amdgpu_device *adev)
 	rmn->mn.ops = &amdgpu_mn_ops;
 	init_rwsem(&rmn->lock);
 	rmn->objects = RB_ROOT;
+	spin_lock_init(&rmn->task_lock);
+	hash_init(rmn->task_hash);
 
 	r = __mmu_notifier_register(&rmn->mn, mm);
 	if (r)

_______________________________________________
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): 20805313-b6a7-dd67-9953-32d7843142d7-5C7GfCeVMHo@public.gmane.org

#12 lore raw

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

Quoted 3 lines

I think all this does (compared to v1) is, avoid taking the same read lock twice, but keep track of how many readers there are outside the rw_semaphore to avoid recursive lock warnings.

Well the primary problem is that taking a read side twice in the same thread can cause a deadlock and is forbidden. That's what lockdep is complaining about.

Quoted 1 line

Am I missing something?

When the first thread is waiting for the lock to be acquired other threads would just continue without waiting for the write side to end.

The alternative would be to use recursion counter + another lock + non owner release.

Quoted 1 line

Maybe there is a concern about locking a semaphore in one thread and unlocking it on a different thread.

It is discouraged, but we could use down_read_non_owner()/up_read_non_owner() for this.

I'm going to give that a try,
Christian.

Am 06.09.2017 um 18:00 schrieb Felix Kuehling:

Quoted 219 lines

I think all this does (compared to v1) is, avoid taking the same read lock twice, but keep track of how many readers there are outside the rw_semaphore to avoid recursive lock warnings.

I don't really understand why you need to count readers per task (using the task hashtable in the amdgpu_mn struct). The amdgpu_mn structure is already per process. Each instance should only be used by one process (as identified by its mm_struct). So the counting of readers could be done with a simple atomic counter in the amdgpu_mn struct:

lock:

     if (atomic_inc_return(mn->read_count) == 1)
         down_read(&mn->lock);

unlock:

     if (atomic_dec_return(mn->read_count) == 0)
         up_read(&mn->lock);

Am I missing something? Even if there are multiple threads in the process, it shouldn't matter because they all need to use the same read lock anyway and as long as any thread holds the read lock, any other thread is safe to use it. Maybe there is a concern about locking a semaphore in one thread and unlocking it on a different thread. I know that mutexes have quite strict semantics in this regard. But I think semaphores are less strict.

Regards,
Felix

Quoted 186 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.

v2: allow multiple updates to be in flight at the same time

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_mn.c | 108 +++++++++++++++++++++++++++++++--
  1 file changed, 104 insertions(+), 4 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_mn.c +104 -4
index 459f02e..333521e 100644
@@ -52,6 +52,10 @@ struct amdgpu_mn {
  	/* objects protected by lock */
  	struct rw_semaphore	lock;
  	struct rb_root		objects;
+
+	/* protected by task_lock */
+	spinlock_t		task_lock;
+	DECLARE_HASHTABLE(task_hash, 7);
  };
 
  struct amdgpu_mn_node {
@@ -59,6 +63,12 @@ struct amdgpu_mn_node {
  	struct list_head		bos;
  };
 
+struct amdgpu_mn_task {
+	struct hlist_node	node;
+	struct task_struct	*task;
+	unsigned		recursion;
+};
+
  /**
   * amdgpu_mn_destroy - destroy the rmn
   *
@@ -107,6 +117,75 @@ static void amdgpu_mn_release(struct mmu_notifier *mn,
  }
 
  /**
+ * amdgpu_mn_read_lock - take the rmn read lock
+ *
+ * @rmn: our notifier
+ *
+ * Take the rmn read side lock.
+ */
+static void amdgpu_mn_read_lock(struct amdgpu_mn *rmn)
+{
+	struct amdgpu_mn_task *task;
+
+	spin_lock(&rmn->task_lock);
+	hash_for_each_possible(rmn->task_hash, task, node,
+			       (unsigned long)current) {
+		if (task->task != current)
+			continue;
+
+		spin_unlock(&rmn->task_lock);
+		++task->recursion;
+		return;
+	}
+	spin_unlock(&rmn->task_lock);
+
+	task = kmalloc(sizeof(*task), GFP_KERNEL);
+	task->task = current;
+	task->recursion = 1;
+
+	down_read(&rmn->lock);
+
+	spin_lock(&rmn->task_lock);
+	hash_add(rmn->task_hash, &task->node, (unsigned long)current);
+	spin_unlock(&rmn->task_lock);
+}
+
+/**
+ * amdgpu_mn_read_unlock - drop the rmn read lock
+ *
+ * @rmn: our notifier
+ *
+ * Drop the rmn read side lock.
+ */
+static void amdgpu_mn_read_unlock(struct amdgpu_mn *rmn)
+{
+	struct amdgpu_mn_task *task;
+
+	spin_lock(&rmn->task_lock);
+	hash_for_each_possible(rmn->task_hash, task, node,
+			       (unsigned long)current) {
+		if (task->task != current)
+			continue;
+
+		spin_unlock(&rmn->task_lock);
+		if (--task->recursion) {
+			return;
+		}
+
+		spin_lock(&rmn->task_lock);
+		hash_del(&task->node);
+		spin_unlock(&rmn->task_lock);
+		kfree(task);
+
+		up_read(&rmn->lock);
+		return;
+	}
+	spin_unlock(&rmn->task_lock);
+
+	BUG();
+}
+
+/**
   * amdgpu_mn_invalidate_node - unmap all BOs of a node
   *
   * @node: the node with the BOs to unmap
@@ -152,7 +231,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;
 
-	down_read(&rmn->lock);
+	amdgpu_mn_read_lock(rmn);
 
  	it = interval_tree_iter_first(&rmn->objects, address, address);
  	if (it) {
@@ -162,7 +241,7 @@ static void amdgpu_mn_invalidate_page(struct mmu_notifier *mn,
  		amdgpu_mn_invalidate_node(node, address, address);
  	}
 
-	up_read(&rmn->lock);
+	amdgpu_mn_read_unlock(rmn);
  }
 
  /**
@@ -187,7 +266,7 @@ static void amdgpu_mn_invalidate_range_start(struct mmu_notifier *mn,
  	/* notification is exclusive, but interval is inclusive */
  	end -= 1;
 
-	down_read(&rmn->lock);
+	amdgpu_mn_read_lock(rmn);
 
  	it = interval_tree_iter_first(&rmn->objects, start, end);
  	while (it) {
@@ -198,14 +277,33 @@ 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);
+	amdgpu_mn_read_unlock(rmn);
  }
 
  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,
  };
 
  /**
@@ -242,6 +340,8 @@ struct amdgpu_mn *amdgpu_mn_get(struct amdgpu_device *adev)
  	rmn->mn.ops = &amdgpu_mn_ops;
  	init_rwsem(&rmn->lock);
  	rmn->objects = RB_ROOT;
+	spin_lock_init(&rmn->task_lock);
+	hash_init(rmn->task_hash);
 
Quoted 2 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_mn.c struct amdgpu_mn *amdgpu_mn_get(struct amdgpu_device *adev) · line 347

Quoted 2 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_mn.c +0 -0
  	r = __mmu_notifier_register(&rmn->mn, mm);
  	if (r)

_______________________________________________
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): 2e67b5f4-1e8f-aa9f-7dd9-3354880a89ad-5C7GfCeVMHo@public.gmane.org

#14 lore raw

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

Quoted 10 lines · Christian König
Quoted 3 lines

I think all this does (compared to v1) is, avoid taking the same read lock twice, but keep track of how many readers there are outside the rw_semaphore to avoid recursive lock warnings.

Well the primary problem is that taking a read side twice in the same thread can cause a deadlock and is forbidden. That's what lockdep is complaining about.

Quoted 1 line

Am I missing something?

When the first thread is waiting for the lock to be acquired other threads would just continue without waiting for the write side to end.

Yeah, OK, that was pretty obvious. So we still need a spinlock ...

Quoted 8 lines

The alternative would be to use recursion counter + another lock + non owner release.

Quoted 2 lines

Maybe there is a concern about locking a semaphore in one thread and unlocking it on a different thread.

It is discouraged, but we could use down_read_non_owner()/up_read_non_owner() for this.

Sounds good.

rwsem.h has a comment that the proper abstraction to use here would be a completion. If you want to avoid a hack, a completion could do the job. Instead of holding the rwsem until the range_end notifier, we could signal a completion in range_end (if it's the last range_end).

The writers would have to wait for the completion after taking the write side lock before proceeding.

I'm not quite sure about how to do that without race conditions or deadlocks between the lock and the completion, though.

Regards,
Felix

Quoted 213 lines

I'm going to give that a try,
Christian.

Am 06.09.2017 um 18:00 schrieb Felix Kuehling:

Quoted 208 lines

I think all this does (compared to v1) is, avoid taking the same read lock twice, but keep track of how many readers there are outside the rw_semaphore to avoid recursive lock warnings.

I don't really understand why you need to count readers per task (using the task hashtable in the amdgpu_mn struct). The amdgpu_mn structure is already per process. Each instance should only be used by one process (as identified by its mm_struct). So the counting of readers could be done with a simple atomic counter in the amdgpu_mn struct:

lock:

     if (atomic_inc_return(mn->read_count) == 1)
         down_read(&mn->lock);

unlock:

     if (atomic_dec_return(mn->read_count) == 0)
         up_read(&mn->lock);

Am I missing something? Even if there are multiple threads in the process, it shouldn't matter because they all need to use the same read lock anyway and as long as any thread holds the read lock, any other thread is safe to use it. Maybe there is a concern about locking a semaphore in one thread and unlocking it on a different thread. I know that mutexes have quite strict semantics in this regard. But I think semaphores are less strict.

Regards,
Felix

Quoted 175 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.

v2: allow multiple updates to be in flight at the same time

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_mn.c | 108
+++++++++++++++++++++++++++++++--
  1 file changed, 104 insertions(+), 4 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_mn.c +101 -0
index 459f02e..333521e 100644
@@ -52,6 +52,10 @@ struct amdgpu_mn {
      /* objects protected by lock */
      struct rw_semaphore    lock;
      struct rb_root        objects;
+
+    /* protected by task_lock */
+    spinlock_t        task_lock;
+    DECLARE_HASHTABLE(task_hash, 7);
  };
    struct amdgpu_mn_node {
@@ -59,6 +63,12 @@ struct amdgpu_mn_node {
      struct list_head        bos;
  };
  +struct amdgpu_mn_task {
+    struct hlist_node    node;
+    struct task_struct    *task;
+    unsigned        recursion;
+};
+
  /**
   * amdgpu_mn_destroy - destroy the rmn
   *
@@ -107,6 +117,75 @@ static void amdgpu_mn_release(struct
 mmu_notifier *mn,
  }
    /**
+ * amdgpu_mn_read_lock - take the rmn read lock
+ *
+ * @rmn: our notifier
+ *
+ * Take the rmn read side lock.
+ */
+static void amdgpu_mn_read_lock(struct amdgpu_mn *rmn)
+{
+    struct amdgpu_mn_task *task;
+
+    spin_lock(&rmn->task_lock);
+    hash_for_each_possible(rmn->task_hash, task, node,
+                   (unsigned long)current) {
+        if (task->task != current)
+            continue;
+
+        spin_unlock(&rmn->task_lock);
+        ++task->recursion;
+        return;
+    }
+    spin_unlock(&rmn->task_lock);
+
+    task = kmalloc(sizeof(*task), GFP_KERNEL);
+    task->task = current;
+    task->recursion = 1;
+
+    down_read(&rmn->lock);
+
+    spin_lock(&rmn->task_lock);
+    hash_add(rmn->task_hash, &task->node, (unsigned long)current);
+    spin_unlock(&rmn->task_lock);
+}
+
+/**
+ * amdgpu_mn_read_unlock - drop the rmn read lock
+ *
+ * @rmn: our notifier
+ *
+ * Drop the rmn read side lock.
+ */
+static void amdgpu_mn_read_unlock(struct amdgpu_mn *rmn)
+{
+    struct amdgpu_mn_task *task;
+
+    spin_lock(&rmn->task_lock);
+    hash_for_each_possible(rmn->task_hash, task, node,
+                   (unsigned long)current) {
+        if (task->task != current)
+            continue;
+
+        spin_unlock(&rmn->task_lock);
+        if (--task->recursion) {
+            return;
+        }
+
+        spin_lock(&rmn->task_lock);
+        hash_del(&task->node);
+        spin_unlock(&rmn->task_lock);
+        kfree(task);
+
+        up_read(&rmn->lock);
+        return;
+    }
+    spin_unlock(&rmn->task_lock);
+
+    BUG();
+}
+
+/**
   * amdgpu_mn_invalidate_node - unmap all BOs of a node
   *
   * @node: the node with the BOs to unmap
@@ -152,7 +231,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;
  -    down_read(&rmn->lock);
+    amdgpu_mn_read_lock(rmn);
        it = interval_tree_iter_first(&rmn->objects, address, address);
      if (it) {
@@ -162,7 +241,7 @@ static void amdgpu_mn_invalidate_page(struct
 mmu_notifier *mn,
          amdgpu_mn_invalidate_node(node, address, address);
      }
  -    up_read(&rmn->lock);
+    amdgpu_mn_read_unlock(rmn);
  }
    /**
@@ -187,7 +266,7 @@ static void
 amdgpu_mn_invalidate_range_start(struct mmu_notifier *mn,
      /* notification is exclusive, but interval is inclusive */
      end -= 1;
  -    down_read(&rmn->lock);
+    amdgpu_mn_read_lock(rmn);
        it = interval_tree_iter_first(&rmn->objects, start, end);
      while (it) {
@@ -198,14 +277,33 @@ 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);
+    amdgpu_mn_read_unlock(rmn);
  }
    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,
  };
    /**
@@ -242,6 +340,8 @@ struct amdgpu_mn *amdgpu_mn_get(struct
Quoted 10 lines
Quoted 8 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_mn.c struct amdgpu_mn *amdgpu_mn_get(struct amdgpu_device *adev) · line 347

Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_mn.c +2 -0
 amdgpu_device *adev)
      rmn->mn.ops = &amdgpu_mn_ops;
      init_rwsem(&rmn->lock);
      rmn->objects = RB_ROOT;
+    spin_lock_init(&rmn->task_lock);
+    hash_init(rmn->task_hash);
        r = __mmu_notifier_register(&rmn->mn, mm);
      if (r)

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