Skip to contentSkip to conversation

Improve reservation object shared slot function

PATCH In review Christian König 6 participants 13 messages Sep 24, 2018 4 messages not in the archive

Subsystems (12)
lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): 20180924115819.2116-1-christian.koenig-5C7GfCeVMHo@public.gmane.org

#2 lore raw

PATCH 1/7 dma-buf: remove shared fence staging in reservation object

No need for that any more. Just replace the list when there isn't enough room any more for the additional fence.

---
 drivers/dma-buf/reservation.c | 178 ++++++++++++++----------------------------
 include/linux/reservation.h   |   4 -
 2 files changed, 58 insertions(+), 124 deletions(-)
drivers/dma-buf/reservation.c +58 -120
index 6c95f61a32e7..5825fc336a13 100644
@@ -68,105 +68,23 @@ EXPORT_SYMBOL(reservation_seqcount_string);
  */
 int reservation_object_reserve_shared(struct reservation_object *obj)
 {
-	struct reservation_object_list *fobj, *old;
-	u32 max;
+	struct reservation_object_list *old, *new;
+	unsigned int i, j, k, max;
 
 	old = reservation_object_get_list(obj);
 
 	if (old && old->shared_max) {
-		if (old->shared_count < old->shared_max) {
-			/* perform an in-place update */
-			kfree(obj->staged);
-			obj->staged = NULL;
+		if (old->shared_count < old->shared_max)
 			return 0;
-		} else
+		else
 			max = old->shared_max * 2;
-	} else
-		max = 4;
-
-	/*
-	 * resize obj->staged or allocate if it doesn't exist,
-	 * noop if already correct size
-	 */
-	fobj = krealloc(obj->staged, offsetof(typeof(*fobj), shared[max]),
-			GFP_KERNEL);
-	if (!fobj)
-		return -ENOMEM;
-
-	obj->staged = fobj;
-	fobj->shared_max = max;
-	return 0;
-}
-EXPORT_SYMBOL(reservation_object_reserve_shared);
-
-static void
-reservation_object_add_shared_inplace(struct reservation_object *obj,
-				      struct reservation_object_list *fobj,
-				      struct dma_fence *fence)
-{
-	struct dma_fence *signaled = NULL;
-	u32 i, signaled_idx;
-
-	dma_fence_get(fence);
-
-	preempt_disable();
-	write_seqcount_begin(&obj->seq);
-
-	for (i = 0; i < fobj->shared_count; ++i) {
-		struct dma_fence *old_fence;
-
-		old_fence = rcu_dereference_protected(fobj->shared[i],
-						reservation_object_held(obj));
-
-		if (old_fence->context == fence->context) {
-			/* memory barrier is added by write_seqcount_begin */
-			RCU_INIT_POINTER(fobj->shared[i], fence);
-			write_seqcount_end(&obj->seq);
-			preempt_enable();
-
-			dma_fence_put(old_fence);
-			return;
-		}
-
-		if (!signaled && dma_fence_is_signaled(old_fence)) {
-			signaled = old_fence;
-			signaled_idx = i;
-		}
-	}
-
-	/*
-	 * memory barrier is added by write_seqcount_begin,
-	 * fobj->shared_count is protected by this lock too
-	 */
-	if (signaled) {
-		RCU_INIT_POINTER(fobj->shared[signaled_idx], fence);
 	} else {
-		BUG_ON(fobj->shared_count >= fobj->shared_max);
-		RCU_INIT_POINTER(fobj->shared[fobj->shared_count], fence);
-		fobj->shared_count++;
+		max = 4;
 	}
 
-	write_seqcount_end(&obj->seq);
-	preempt_enable();
-
-	dma_fence_put(signaled);
-}
-
-static void
-reservation_object_add_shared_replace(struct reservation_object *obj,
-				      struct reservation_object_list *old,
-				      struct reservation_object_list *fobj,
-				      struct dma_fence *fence)
-{
-	unsigned i, j, k;
-
-	dma_fence_get(fence);
-
-	if (!old) {
-		RCU_INIT_POINTER(fobj->shared[0], fence);
-		fobj->shared_count = 1;
-		goto done;
-	}
+	new = kmalloc(offsetof(typeof(*new), shared[max]), GFP_KERNEL);
+	if (!new)
+		return -ENOMEM;
 
 	/*
 	 * no need to bump fence refcounts, rcu_read access
@@ -174,46 +92,45 @@ reservation_object_add_shared_replace(struct reservation_object *obj,
 	 * references from the old struct are carried over to
 	 * the new.
 	 */
-	for (i = 0, j = 0, k = fobj->shared_max; i < old->shared_count; ++i) {
-		struct dma_fence *check;
+	for (i = 0, j = 0, k = max; i < (old ? old->shared_count : 0); ++i) {
+		struct dma_fence *fence;
 
-		check = rcu_dereference_protected(old->shared[i],
-						reservation_object_held(obj));
-
-		if (check->context == fence->context ||
-		    dma_fence_is_signaled(check))
-			RCU_INIT_POINTER(fobj->shared[--k], check);
+		fence = rcu_dereference_protected(old->shared[i],
+						  reservation_object_held(obj));
+		if (dma_fence_is_signaled(fence))
+			RCU_INIT_POINTER(new->shared[--k], fence);
 		else
-			RCU_INIT_POINTER(fobj->shared[j++], check);
+			RCU_INIT_POINTER(new->shared[j++], fence);
 	}
-	fobj->shared_count = j;
-	RCU_INIT_POINTER(fobj->shared[fobj->shared_count], fence);
-	fobj->shared_count++;
+	new->shared_count = j;
+	new->shared_max = max;
 
-done:
 	preempt_disable();
 	write_seqcount_begin(&obj->seq);
 	/*
 	 * RCU_INIT_POINTER can be used here,
 	 * seqcount provides the necessary barriers
 	 */
-	RCU_INIT_POINTER(obj->fence, fobj);
+	RCU_INIT_POINTER(obj->fence, new);
 	write_seqcount_end(&obj->seq);
 	preempt_enable();
 
 	if (!old)
-		return;
+		return 0;
 
 	/* Drop the references to the signaled fences */
-	for (i = k; i < fobj->shared_max; ++i) {
-		struct dma_fence *f;
+	for (i = k; i < new->shared_max; ++i) {
+		struct dma_fence *fence;
 
-		f = rcu_dereference_protected(fobj->shared[i],
-					      reservation_object_held(obj));
-		dma_fence_put(f);
+		fence = rcu_dereference_protected(new->shared[i],
+						  reservation_object_held(obj));
+		dma_fence_put(fence);
 	}
 	kfree_rcu(old, rcu);
+
+	return 0;
 }
+EXPORT_SYMBOL(reservation_object_reserve_shared);
 
 /**
  * reservation_object_add_shared_fence - Add a fence to a shared slot
@@ -226,15 +143,39 @@ reservation_object_add_shared_replace(struct reservation_object *obj,
 void reservation_object_add_shared_fence(struct reservation_object *obj,
 					 struct dma_fence *fence)
 {
-	struct reservation_object_list *old, *fobj = obj->staged;
+	struct reservation_object_list *fobj;
+	unsigned int i;
 
-	old = reservation_object_get_list(obj);
-	obj->staged = NULL;
+	dma_fence_get(fence);
+
+	fobj = reservation_object_get_list(obj);
 
-	if (!fobj)
-		reservation_object_add_shared_inplace(obj, old, fence);
-	else
-		reservation_object_add_shared_replace(obj, old, fobj, fence);
+	preempt_disable();
+	write_seqcount_begin(&obj->seq);
+
+	for (i = 0; i < fobj->shared_count; ++i) {
+		struct dma_fence *old_fence;
+
+		old_fence = rcu_dereference_protected(fobj->shared[i],
+						      reservation_object_held(obj));
+		if (old_fence->context == fence->context ||
+		    dma_fence_is_signaled(old_fence)) {
+			dma_fence_put(old_fence);
+			goto replace;
+		}
+	}
+
+	BUG_ON(fobj->shared_count >= fobj->shared_max);
+	fobj->shared_count++;
+
+replace:
+	/*
+	 * memory barrier is added by write_seqcount_begin,
+	 * fobj->shared_count is protected by this lock too
+	 */
+	RCU_INIT_POINTER(fobj->shared[i], fence);
+	write_seqcount_end(&obj->seq);
+	preempt_enable();
 }
 EXPORT_SYMBOL(reservation_object_add_shared_fence);
 
@@ -343,9 +284,6 @@ int reservation_object_copy_fences(struct reservation_object *dst,
 	new = dma_fence_get_rcu_safe(&src->fence_excl);
 	rcu_read_unlock();
 
-	kfree(dst->staged);
-	dst->staged = NULL;
-
 	src_list = reservation_object_get_list(dst);
 	old = reservation_object_get_excl(dst);
 
include/linux/reservation.h +0 -4
index 02166e815afb..54cf6773a14c 100644
@@ -68,7 +68,6 @@ struct reservation_object_list {
  * @seq: sequence count for managing RCU read-side synchronization
  * @fence_excl: the exclusive fence, if there is one currently
  * @fence: list of current shared fences
- * @staged: staged copy of shared fences for RCU updates
  */
 struct reservation_object {
 	struct ww_mutex lock;
@@ -76,7 +75,6 @@ struct reservation_object {
 
 	struct dma_fence __rcu *fence_excl;
 	struct reservation_object_list __rcu *fence;
-	struct reservation_object_list *staged;
 };
 
 #define reservation_object_held(obj) lockdep_is_held(&(obj)->lock.base)
@@ -95,7 +93,6 @@ reservation_object_init(struct reservation_object *obj)
 	__seqcount_init(&obj->seq, reservation_seqcount_string, &reservation_seqcount_class);
 	RCU_INIT_POINTER(obj->fence, NULL);
 	RCU_INIT_POINTER(obj->fence_excl, NULL);
-	obj->staged = NULL;
 }
 
 /**
@@ -124,7 +121,6 @@ reservation_object_fini(struct reservation_object *obj)
 
 		kfree(fobj);
 	}
-	kfree(obj->staged);
 
 	ww_mutex_destroy(&obj->lock);
 }
Signature · 6 lines
2.14.1

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

PATCH 2/7 dma-buf: allow reserving more than one shared fence slot

In reply to a message not in the archive

Let's support simultaneous submissions to multiple engines.

---
 drivers/dma-buf/reservation.c                | 13 ++++++++-----
 drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c       |  2 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_object.c   |  2 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c       |  4 ++--
 drivers/gpu/drm/etnaviv/etnaviv_gem_submit.c |  2 +-
 drivers/gpu/drm/i915/i915_vma.c              |  2 +-
 drivers/gpu/drm/msm/msm_gem_submit.c         |  3 ++-
 drivers/gpu/drm/nouveau/nouveau_fence.c      |  2 +-
 drivers/gpu/drm/qxl/qxl_release.c            |  2 +-
 drivers/gpu/drm/radeon/radeon_vm.c           |  2 +-
 drivers/gpu/drm/ttm/ttm_bo.c                 |  4 ++--
 drivers/gpu/drm/ttm/ttm_execbuf_util.c       |  4 ++--
 drivers/gpu/drm/v3d/v3d_gem.c                |  2 +-
 drivers/gpu/drm/vc4/vc4_gem.c                |  2 +-
 drivers/gpu/drm/vgem/vgem_fence.c            |  2 +-
 include/linux/reservation.h                  |  3 ++-
 16 files changed, 28 insertions(+), 23 deletions(-)
drivers/dma-buf/reservation.c +8 -5
index 5825fc336a13..5fb4fd461908 100644
@@ -56,9 +56,10 @@ const char reservation_seqcount_string[] = "reservation_seqcount";
 EXPORT_SYMBOL(reservation_seqcount_string);
 
 /**
- * reservation_object_reserve_shared - Reserve space to add a shared
- * fence to a reservation_object.
+ * reservation_object_reserve_shared - Reserve space to add shared fences to
+ * a reservation_object.
  * @obj: reservation object
+ * @num_fences: number of fences we want to add
  *
  * Should be called before reservation_object_add_shared_fence().  Must
  * be called with obj->lock held.
@@ -66,7 +67,8 @@ EXPORT_SYMBOL(reservation_seqcount_string);
  * RETURNS
  * Zero for success, or -errno
  */
-int reservation_object_reserve_shared(struct reservation_object *obj)
+int reservation_object_reserve_shared(struct reservation_object *obj,
+				      unsigned int num_fences)
 {
 	struct reservation_object_list *old, *new;
 	unsigned int i, j, k, max;
@@ -74,10 +76,11 @@ int reservation_object_reserve_shared(struct reservation_object *obj)
 	old = reservation_object_get_list(obj);
 
 	if (old && old->shared_max) {
-		if (old->shared_count < old->shared_max)
+		if ((old->shared_count + num_fences) <= old->shared_max)
 			return 0;
 		else
-			max = old->shared_max * 2;
+			max = max(old->shared_count + num_fences,
+				  old->shared_max * 2);
 	} else {
 		max = 4;
 	}
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +1 -1
index 8836186eb5ef..3243da67db9e 100644
@@ -955,7 +955,7 @@ static int amdgpu_cs_vm_handling(struct amdgpu_cs_parser *p)
 	if (r)
 		return r;
 
-	r = reservation_object_reserve_shared(vm->root.base.bo->tbo.resv);
+	r = reservation_object_reserve_shared(vm->root.base.bo->tbo.resv, 1);
 	if (r)
 		return r;
 
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +1 -1
index 904014dc5915..cf768acb51dc 100644
@@ -640,7 +640,7 @@ int amdgpu_bo_backup_to_shadow(struct amdgpu_device *adev,
 	bo_addr = amdgpu_bo_gpu_offset(bo);
 	shadow_addr = amdgpu_bo_gpu_offset(bo->shadow);
 
-	r = reservation_object_reserve_shared(bo->tbo.resv);
+	r = reservation_object_reserve_shared(bo->tbo.resv, 1);
 	if (r)
 		goto err;
 
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +2 -2
index 6904d794d60a..bdce05183edb 100644
@@ -772,7 +772,7 @@ static int amdgpu_vm_clear_bo(struct amdgpu_device *adev,
 
 	ring = container_of(vm->entity.rq->sched, struct amdgpu_ring, sched);
 
-	r = reservation_object_reserve_shared(bo->tbo.resv);
+	r = reservation_object_reserve_shared(bo->tbo.resv, 1);
 	if (r)
 		return r;
 
@@ -1839,7 +1839,7 @@ static int amdgpu_vm_bo_update_mapping(struct amdgpu_device *adev,
 	if (r)
 		goto error_free;
 
-	r = reservation_object_reserve_shared(vm->root.base.bo->tbo.resv);
+	r = reservation_object_reserve_shared(vm->root.base.bo->tbo.resv, 1);
 	if (r)
 		goto error_free;
 
drivers/gpu/drm/etnaviv/etnaviv_gem_submit.c +1 -1
index 983e67f19e45..30875f8f2933 100644
@@ -179,7 +179,7 @@ static int submit_fence_sync(struct etnaviv_gem_submit *submit)
 		struct reservation_object *robj = bo->obj->resv;
 
 		if (!(bo->flags & ETNA_SUBMIT_BO_WRITE)) {
-			ret = reservation_object_reserve_shared(robj);
+			ret = reservation_object_reserve_shared(robj, 1);
 			if (ret)
 				return ret;
 		}
drivers/gpu/drm/i915/i915_vma.c +1 -1
index 11d834f94220..f74c856858f9 100644
@@ -893,7 +893,7 @@ static void export_fence(struct i915_vma *vma,
 	reservation_object_lock(resv, NULL);
 	if (flags & EXEC_OBJECT_WRITE)
 		reservation_object_add_excl_fence(resv, &rq->fence);
-	else if (reservation_object_reserve_shared(resv) == 0)
+	else if (reservation_object_reserve_shared(resv, 1) == 0)
 		reservation_object_add_shared_fence(resv, &rq->fence);
 	reservation_object_unlock(resv);
 }
drivers/gpu/drm/msm/msm_gem_submit.c +2 -1
index 7bd83e0afa97..acc5da791e36 100644
@@ -241,7 +241,8 @@ static int submit_fence_sync(struct msm_gem_submit *submit, bool no_implicit)
 			 * strange place to call it.  OTOH this is a
 			 * convenient can-fail point to hook it in.
 			 */
-			ret = reservation_object_reserve_shared(msm_obj->resv);
+			ret = reservation_object_reserve_shared(msm_obj->resv,
+								1);
 			if (ret)
 				return ret;
 		}
drivers/gpu/drm/nouveau/nouveau_fence.c +1 -1
index 412d49bc6e56..bd58298afe12 100644
@@ -341,7 +341,7 @@ nouveau_fence_sync(struct nouveau_bo *nvbo, struct nouveau_channel *chan, bool e
 	int ret = 0, i;
 
 	if (!exclusive) {
-		ret = reservation_object_reserve_shared(resv);
+		ret = reservation_object_reserve_shared(resv, 1);
 
 		if (ret)
 			return ret;
drivers/gpu/drm/qxl/qxl_release.c +1 -1
index e37f0097f744..a8d5457a1af9 100644
@@ -234,7 +234,7 @@ static int qxl_release_validate_bo(struct qxl_bo *bo)
 			return ret;
 	}
 
-	ret = reservation_object_reserve_shared(bo->tbo.resv);
+	ret = reservation_object_reserve_shared(bo->tbo.resv, 1);
 	if (ret)
 		return ret;
 
drivers/gpu/drm/radeon/radeon_vm.c +1 -1
index 7f1a9c787bd1..fed11ece0de6 100644
@@ -831,7 +831,7 @@ static int radeon_vm_update_ptes(struct radeon_device *rdev,
 		int r;
 
 		radeon_sync_resv(rdev, &ib->sync, pt->tbo.resv, true);
-		r = reservation_object_reserve_shared(pt->tbo.resv);
+		r = reservation_object_reserve_shared(pt->tbo.resv, 1);
 		if (r)
 			return r;
 
drivers/gpu/drm/ttm/ttm_bo.c +2 -2
index b2a33bf1ef10..461b318ca2a6 100644
@@ -887,7 +887,7 @@ static int ttm_bo_add_move_fence(struct ttm_buffer_object *bo,
 	if (fence) {
 		reservation_object_add_shared_fence(bo->resv, fence);
 
-		ret = reservation_object_reserve_shared(bo->resv);
+		ret = reservation_object_reserve_shared(bo->resv, 1);
 		if (unlikely(ret))
 			return ret;
 
@@ -992,7 +992,7 @@ int ttm_bo_mem_space(struct ttm_buffer_object *bo,
 	bool has_erestartsys = false;
 	int i, ret;
 
-	ret = reservation_object_reserve_shared(bo->resv);
+	ret = reservation_object_reserve_shared(bo->resv, 1);
 	if (unlikely(ret))
 		return ret;
 
drivers/gpu/drm/ttm/ttm_execbuf_util.c +2 -2
index e73ae0d22897..e493edb0d3e7 100644
@@ -129,7 +129,7 @@ int ttm_eu_reserve_buffers(struct ww_acquire_ctx *ticket,
 			if (!entry->shared)
 				continue;
 
-			ret = reservation_object_reserve_shared(bo->resv);
+			ret = reservation_object_reserve_shared(bo->resv, 1);
 			if (!ret)
 				continue;
 		}
@@ -151,7 +151,7 @@ int ttm_eu_reserve_buffers(struct ww_acquire_ctx *ticket,
 		}
 
 		if (!ret && entry->shared)
-			ret = reservation_object_reserve_shared(bo->resv);
+			ret = reservation_object_reserve_shared(bo->resv, 1);
 
 		if (unlikely(ret != 0)) {
 			if (ret == -EINTR)
drivers/gpu/drm/v3d/v3d_gem.c +1 -1
index 5ce24098a5fd..4a46376ff795 100644
@@ -305,7 +305,7 @@ v3d_lock_bo_reservations(struct drm_device *dev,
 	for (i = 0; i < exec->bo_count; i++) {
 		bo = to_v3d_bo(&exec->bo[i]->base);
 
-		ret = reservation_object_reserve_shared(bo->resv);
+		ret = reservation_object_reserve_shared(bo->resv, 1);
 		if (ret) {
 			v3d_unlock_bo_reservations(dev, exec, acquire_ctx);
 			return ret;
drivers/gpu/drm/vc4/vc4_gem.c +1 -1
index 7910b9acedd6..5f768d886cb2 100644
@@ -635,7 +635,7 @@ vc4_lock_bo_reservations(struct drm_device *dev,
 	for (i = 0; i < exec->bo_count; i++) {
 		bo = to_vc4_bo(&exec->bo[i]->base);
 
-		ret = reservation_object_reserve_shared(bo->resv);
+		ret = reservation_object_reserve_shared(bo->resv, 1);
 		if (ret) {
 			vc4_unlock_bo_reservations(dev, exec, acquire_ctx);
 			return ret;
drivers/gpu/drm/vgem/vgem_fence.c +1 -1
index b28876c222b4..f2692e5abac2 100644
@@ -193,7 +193,7 @@ int vgem_fence_attach_ioctl(struct drm_device *dev,
 	reservation_object_lock(resv, NULL);
 	if (arg->flags & VGEM_FENCE_WRITE)
 		reservation_object_add_excl_fence(resv, fence);
-	else if ((ret = reservation_object_reserve_shared(resv)) == 0)
+	else if ((ret = reservation_object_reserve_shared(resv, 1)) == 0)
 		reservation_object_add_shared_fence(resv, fence);
 	reservation_object_unlock(resv);
 
include/linux/reservation.h +2 -1
index 54cf6773a14c..5ddb0e143721 100644
@@ -261,7 +261,8 @@ reservation_object_get_excl_rcu(struct reservation_object *obj)
 	return fence;
 }
 
-int reservation_object_reserve_shared(struct reservation_object *obj);
+int reservation_object_reserve_shared(struct reservation_object *obj,
+				      unsigned int num_fences);
 void reservation_object_add_shared_fence(struct reservation_object *obj,
 					 struct dma_fence *fence);
 
Signature · 6 lines
2.14.1

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

PATCH 3/7 drm/ttm: allow reserve more than one shared slot

In reply to a message not in the archive

Let's support simultaneous submissions to multiple engines.

---
 drivers/gpu/drm/ttm/ttm_execbuf_util.c | 6 ++++--
 include/drm/ttm/ttm_execbuf_util.h     | 2 +-
 2 files changed, 5 insertions(+), 3 deletions(-)
drivers/gpu/drm/ttm/ttm_execbuf_util.c +4 -2
index e493edb0d3e7..9b842884530a 100644
@@ -129,7 +129,8 @@ int ttm_eu_reserve_buffers(struct ww_acquire_ctx *ticket,
 			if (!entry->shared)
 				continue;
 
-			ret = reservation_object_reserve_shared(bo->resv, 1);
+			ret = reservation_object_reserve_shared(bo->resv,
+								entry->shared);
 			if (!ret)
 				continue;
 		}
@@ -151,7 +152,8 @@ int ttm_eu_reserve_buffers(struct ww_acquire_ctx *ticket,
 		}
 
 		if (!ret && entry->shared)
-			ret = reservation_object_reserve_shared(bo->resv, 1);
+			ret = reservation_object_reserve_shared(bo->resv,
+								entry->shared);
 
 		if (unlikely(ret != 0)) {
 			if (ret == -EINTR)
include/drm/ttm/ttm_execbuf_util.h +1 -1
index b0fdd1980034..3b16dda9bd08 100644
@@ -46,7 +46,7 @@
 struct ttm_validate_buffer {
 	struct list_head head;
 	struct ttm_buffer_object *bo;
-	bool shared;
+	unsigned int shared;
 };
 
 /**
Signature · 6 lines
2.14.1

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

Re: [PATCH 3/7] drm/ttm: allow reserve more than one shared slot

The shortlog should say "allow reserving more than one shared fence slot", same as patch 2.

Quoted 38 lines · Christian König

Let's support simultaneous submissions to multiple engines.

---
 drivers/gpu/drm/ttm/ttm_execbuf_util.c | 6 ++++--
 include/drm/ttm/ttm_execbuf_util.h     | 2 +-
 2 files changed, 5 insertions(+), 3 deletions(-)
drivers/gpu/drm/ttm/ttm_execbuf_util.c +4 -2
index e493edb0d3e7..9b842884530a 100644
@@ -129,7 +129,8 @@ int ttm_eu_reserve_buffers(struct ww_acquire_ctx *ticket,
 			if (!entry->shared)
 				continue;
 
-			ret = reservation_object_reserve_shared(bo->resv, 1);
+			ret = reservation_object_reserve_shared(bo->resv,
+								entry->shared);
 			if (!ret)
 				continue;
 		}
@@ -151,7 +152,8 @@ int ttm_eu_reserve_buffers(struct ww_acquire_ctx *ticket,
 		}
 
 		if (!ret && entry->shared)
-			ret = reservation_object_reserve_shared(bo->resv, 1);
+			ret = reservation_object_reserve_shared(bo->resv,
+								entry->shared);
 
 		if (unlikely(ret != 0)) {
 			if (ret == -EINTR)
include/drm/ttm/ttm_execbuf_util.h +0 -0
index b0fdd1980034..3b16dda9bd08 100644

include/drm/ttm/ttm_execbuf_util.h line 52

Quoted 8 lines
include/drm/ttm/ttm_execbuf_util.h +1 -1
@@ -46,7 +46,7 @@
 struct ttm_validate_buffer {
 	struct list_head head;
 	struct ttm_buffer_object *bo;
-	bool shared;
+	unsigned int shared;
 };
 
 /**

It would be better to change the name of the field to something more descriptive, e.g. "num_shared_fence_contexts", and fix up all users accordingly in this patch (which will get rid of one hunk in patch 4).

Signature · 6 lines
Earthling Michel Dänzer               |               http://www.amd.com
Libre software enthusiast             |             Mesa and X developer
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
#6 lore raw

PATCH 4/7 drm/amdgpu: fix using shared fence for exported BOs

In reply to a message not in the archive

It is perfectly possible that the BO list is created before the BO is exported. While at it cleanup setting shared to one instead of true.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.c |  1 -
 drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c      | 13 ++++++++++---
 2 files changed, 10 insertions(+), 4 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.c +0 -1
index 14d2982a47cc..5c79da8e1150 100644
@@ -118,7 +118,6 @@ int amdgpu_bo_list_create(struct amdgpu_device *adev, struct drm_file *filp,
 		entry->priority = min(info[i].bo_priority,
 				      AMDGPU_BO_LIST_MAX_PRIORITY);
 		entry->tv.bo = &bo->tbo;
-		entry->tv.shared = !bo->prime_shared_count;
 
 		if (bo->preferred_domains == AMDGPU_GEM_DOMAIN_GDS)
 			list->gds_obj = bo;
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +10 -3
index 3243da67db9e..1a2ea2e931e3 100644
@@ -50,7 +50,7 @@ static int amdgpu_cs_user_fence_chunk(struct amdgpu_cs_parser *p,
 	bo = amdgpu_bo_ref(gem_to_amdgpu_bo(gobj));
 	p->uf_entry.priority = 0;
 	p->uf_entry.tv.bo = &bo->tbo;
-	p->uf_entry.tv.shared = true;
+	p->uf_entry.tv.shared = 1;
 	p->uf_entry.user_pages = NULL;
 
 	drm_gem_object_put_unlocked(gobj);
@@ -598,6 +598,9 @@ static int amdgpu_cs_parser_bos(struct amdgpu_cs_parser *p,
 			return r;
 	}
 
+	amdgpu_bo_list_for_each_entry(e, p->bo_list)
+		e->tv.shared = 1;
+
 	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, AMDGPU_MN_TYPE_GFX);
@@ -717,8 +720,12 @@ static int amdgpu_cs_parser_bos(struct amdgpu_cs_parser *p,
 	gws = p->bo_list->gws_obj;
 	oa = p->bo_list->oa_obj;
 
-	amdgpu_bo_list_for_each_entry(e, p->bo_list)
-		e->bo_va = amdgpu_vm_bo_find(vm, ttm_to_amdgpu_bo(e->tv.bo));
+	amdgpu_bo_list_for_each_entry(e, p->bo_list) {
+		struct amdgpu_bo *bo = ttm_to_amdgpu_bo(e->tv.bo);
+
+		e->tv.shared = bo->prime_shared_count ? 1 : 0;
+		e->bo_va = amdgpu_vm_bo_find(vm, bo);
+	}
 
 	if (gds) {
 		p->job->gds_base = amdgpu_bo_gpu_offset(gds) >> PAGE_SHIFT;
Signature · 6 lines
2.14.1

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

PATCH 6/7 drm/amdgpu: always reserve one more shared slot for pipelines BO moves

In reply to a message not in the archive

This allows us to drop the extra reserve in TTM.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c | 6 +++---
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 2 +-
 2 files changed, 4 insertions(+), 4 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +3 -3
index cda8cf90777c..3e31561d141b 100644
@@ -50,7 +50,7 @@ static int amdgpu_cs_user_fence_chunk(struct amdgpu_cs_parser *p,
 	bo = amdgpu_bo_ref(gem_to_amdgpu_bo(gobj));
 	p->uf_entry.priority = 0;
 	p->uf_entry.tv.bo = &bo->tbo;
-	p->uf_entry.tv.shared = 1;
+	p->uf_entry.tv.shared = 2;
 	p->uf_entry.user_pages = NULL;
 
 	drm_gem_object_put_unlocked(gobj);
@@ -599,7 +599,7 @@ static int amdgpu_cs_parser_bos(struct amdgpu_cs_parser *p,
 	}
 
 	amdgpu_bo_list_for_each_entry(e, p->bo_list)
-		e->tv.shared = 1;
+		e->tv.shared = 2;
 
 	amdgpu_bo_list_get_list(p->bo_list, &p->validated);
 	if (p->bo_list->first_userptr != p->bo_list->num_entries)
@@ -723,7 +723,7 @@ static int amdgpu_cs_parser_bos(struct amdgpu_cs_parser *p,
 	amdgpu_bo_list_for_each_entry(e, p->bo_list) {
 		struct amdgpu_bo *bo = ttm_to_amdgpu_bo(e->tv.bo);
 
-		e->tv.shared = bo->prime_shared_count ? 1 : 0;
+		e->tv.shared = bo->prime_shared_count ? 2 : 0;
 		e->bo_va = amdgpu_vm_bo_find(vm, bo);
 	}
 
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +1 -1
index 353367382874..96fcb33774b7 100644
@@ -616,7 +616,7 @@ void amdgpu_vm_get_pd_bo(struct amdgpu_vm *vm,
 {
 	entry->priority = 0;
 	entry->tv.bo = &vm->root.base.bo->tbo;
-	entry->tv.shared = 2;
+	entry->tv.shared = 3;
 	entry->user_pages = NULL;
 	list_add(&entry->tv.head, validated);
 }
Signature · 6 lines
2.14.1

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

PATCH 7/7 drm/ttm: drop the extra reservation for pipelines BO moves

In reply to a message not in the archive

The driver is now responsible to allocate enough shared slots.

---
 drivers/gpu/drm/ttm/ttm_bo.c | 27 ++++++---------------------
 1 file changed, 6 insertions(+), 21 deletions(-)
drivers/gpu/drm/ttm/ttm_bo.c +6 -21
index 461b318ca2a6..7cf6c9d06364 100644
@@ -873,12 +873,11 @@ EXPORT_SYMBOL(ttm_bo_mem_put);
 /**
  * Add the last move fence to the BO and reserve a new shared slot.
  */
-static int ttm_bo_add_move_fence(struct ttm_buffer_object *bo,
-				 struct ttm_mem_type_manager *man,
-				 struct ttm_mem_reg *mem)
+static void ttm_bo_add_move_fence(struct ttm_buffer_object *bo,
+				  struct ttm_mem_type_manager *man,
+				  struct ttm_mem_reg *mem)
 {
 	struct dma_fence *fence;
-	int ret;
 
 	spin_lock(&man->move_lock);
 	fence = dma_fence_get(man->move);
@@ -886,16 +885,9 @@ static int ttm_bo_add_move_fence(struct ttm_buffer_object *bo,
 
 	if (fence) {
 		reservation_object_add_shared_fence(bo->resv, fence);
-
-		ret = reservation_object_reserve_shared(bo->resv, 1);
-		if (unlikely(ret))
-			return ret;
-
 		dma_fence_put(bo->moving);
 		bo->moving = fence;
 	}
-
-	return 0;
 }
 
 /**
@@ -923,7 +915,8 @@ static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo,
 			return ret;
 	} while (1);
 	mem->mem_type = mem_type;
-	return ttm_bo_add_move_fence(bo, man, mem);
+	ttm_bo_add_move_fence(bo, man, mem);
+	return 0;
 }
 
 static uint32_t ttm_bo_select_caching(struct ttm_mem_type_manager *man,
@@ -992,10 +985,6 @@ int ttm_bo_mem_space(struct ttm_buffer_object *bo,
 	bool has_erestartsys = false;
 	int i, ret;
 
-	ret = reservation_object_reserve_shared(bo->resv, 1);
-	if (unlikely(ret))
-		return ret;
-
 	mem->mm_node = NULL;
 	for (i = 0; i < placement->num_placement; ++i) {
 		const struct ttm_place *place = &placement->placement[i];
@@ -1031,11 +1020,7 @@ int ttm_bo_mem_space(struct ttm_buffer_object *bo,
 			return ret;
 
 		if (mem->mm_node) {
-			ret = ttm_bo_add_move_fence(bo, man, mem);
-			if (unlikely(ret)) {
-				(*man->func->put_node)(man, mem);
-				return ret;
-			}
+			ttm_bo_add_move_fence(bo, man, mem);
 			break;
 		}
 	}
Signature · 6 lines
2.14.1

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

PATCH 5/7 drm/amdgpu: always reserve two slots for the VM

In reply to Christian König

And drop the now superflous extra reservations.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c |  4 ----
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 14 +++++---------
 2 files changed, 5 insertions(+), 13 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +0 -4
index 1a2ea2e931e3..cda8cf90777c 100644
@@ -962,10 +962,6 @@ static int amdgpu_cs_vm_handling(struct amdgpu_cs_parser *p)
 	if (r)
 		return r;
 
-	r = reservation_object_reserve_shared(vm->root.base.bo->tbo.resv, 1);
-	if (r)
-		return r;
-
 	p->job->vm_pd_addr = amdgpu_gmc_pd_addr(vm->root.base.bo);
 
 	if (amdgpu_vm_debug) {
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +5 -9
index bdce05183edb..353367382874 100644
@@ -616,7 +616,7 @@ void amdgpu_vm_get_pd_bo(struct amdgpu_vm *vm,
 {
 	entry->priority = 0;
 	entry->tv.bo = &vm->root.base.bo->tbo;
-	entry->tv.shared = true;
+	entry->tv.shared = 2;
 	entry->user_pages = NULL;
 	list_add(&entry->tv.head, validated);
 }
@@ -772,10 +772,6 @@ static int amdgpu_vm_clear_bo(struct amdgpu_device *adev,
 
 	ring = container_of(vm->entity.rq->sched, struct amdgpu_ring, sched);
 
-	r = reservation_object_reserve_shared(bo->tbo.resv, 1);
-	if (r)
-		return r;
-
 	r = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
 	if (r)
 		goto error;
@@ -1839,10 +1835,6 @@ static int amdgpu_vm_bo_update_mapping(struct amdgpu_device *adev,
 	if (r)
 		goto error_free;
 
-	r = reservation_object_reserve_shared(vm->root.base.bo->tbo.resv, 1);
-	if (r)
-		goto error_free;
-
 	r = amdgpu_vm_update_ptes(&params, start, last + 1, addr, flags);
 	if (r)
 		goto error_free;
@@ -3023,6 +3015,10 @@ int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 	if (r)
 		goto error_free_root;
 
+	r = reservation_object_reserve_shared(root->tbo.resv, 1);
+	if (r)
+		goto error_unreserve;
+
 	r = amdgpu_vm_clear_bo(adev, vm, root,
 			       adev->vm_manager.root_level,
 			       vm->pte_support_ats);
Signature · 6 lines
2.14.1

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

A message that is not in the archive (sent off-list or not yet received): 20180924115819.2116-6-christian.koenig-5C7GfCeVMHo@public.gmane.org

#11 lore raw

Re: [PATCH 5/7] drm/amdgpu: always reserve two slots for the VM

Quoted 28 lines · Christian König

And drop the now superflous extra reservations.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c |  4 ----
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 14 +++++---------
 2 files changed, 5 insertions(+), 13 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +0 -4
index 1a2ea2e931e3..cda8cf90777c 100644
@@ -962,10 +962,6 @@ static int amdgpu_cs_vm_handling(struct amdgpu_cs_parser *p)
 	if (r)
 		return r;
 
-	r = reservation_object_reserve_shared(vm->root.base.bo->tbo.resv, 1);
-	if (r)
-		return r;
-
 	p->job->vm_pd_addr = amdgpu_gmc_pd_addr(vm->root.base.bo);
 
 	if (amdgpu_vm_debug) {
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +0 -0
index bdce05183edb..353367382874 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c void amdgpu_vm_get_pd_bo(struct amdgpu_vm *vm, · line 619

Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +1 -1
@@ -616,7 +616,7 @@ void amdgpu_vm_get_pd_bo(struct amdgpu_vm *vm,
 {
 	entry->priority = 0;
 	entry->tv.bo = &vm->root.base.bo->tbo;
-	entry->tv.shared = true;
+	entry->tv.shared = 2;

Where assigning values > 1 to this field, it would be good to have a comment explaining what the multiple shared fence slots correspond to. Same for patch 6.

Signature · 6 lines
Earthling Michel Dänzer               |               http://www.amd.com
Libre software enthusiast             |             Mesa and X developer
_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx
#12 lore raw
In reply to Christian König
Quoted 3 lines · Christian König

The reservation object shared slot function only allowed to reserve one slot at a time.

Improve that and allow to reserve multiple slots to support atomically submission to multiple engines.

I think you can do this already, just don't drop the ww_mutex lock. And I also think that invariant still holds, if you drop the ww_mutex lock your fence slot reservation evaporates. Your new code is just a bit more convenient.

Could we check/enforce this somehow when WW_MUTEX debugging is enabled? E.g. store the ww_mutex ctx in the reservation (we can dig it out from under the lock), and then check that the lock holder/ctx hasn't changed when adding all the fences? I think with multiple fences getting added atomically some more debug checks here would be good.

Another one: Do we want to insist that you either add all the fences, or none, to make this fully atomic? We could check this when unlocking the reservation. Kinda hard to guess what your exact use-case here is.

All this would ofc be compiled out for !WW_MUTEX_DEBUG kernels.

Cheers, Daniel

Signature · 7 lines
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

A message that is not in the archive (sent off-list or not yet received): 20180924150319.GV11082-dv86pmgwkMBes7Z6vYuT8azUEOm+Xw19@public.gmane.org

#14 lore raw

Am 24.09.2018 um 17:03 schrieb Daniel Vetter:

Quoted 8 lines · Christian König
Quoted 3 lines

The reservation object shared slot function only allowed to reserve one slot at a time.

Improve that and allow to reserve multiple slots to support atomically submission to multiple engines.

I think you can do this already, just don't drop the ww_mutex lock. And I also think that invariant still holds, if you drop the ww_mutex lock your fence slot reservation evaporates. Your new code is just a bit more convenient.

The problem is that allocating a slot could in theory fail with -ENOMEM.

And at the point we add the fence the hardware is already using the memory, so failure is not on option.

Key feature is that we are getting submissions to multiple engines which either needs to be submitted together or not at all.

Quoted 5 lines

Could we check/enforce this somehow when WW_MUTEX debugging is enabled? E.g. store the ww_mutex ctx in the reservation (we can dig it out from under the lock), and then check that the lock holder/ctx hasn't changed when adding all the fences? I think with multiple fences getting added atomically some more debug checks here would be good.

Yeah, I was already thinking about something similar.

We wouldn't need to remember the context or anything, but rather just set shared_max=shared_count in reservation_object_unlock().

Quoted 3 lines

Another one: Do we want to insist that you either add all the fences, or none, to make this fully atomic? We could check this when unlocking the reservation. Kinda hard to guess what your exact use-case here is.

No, some fence (like VM updates) are perfectly optional.

Quoted 1 line

All this would ofc be compiled out for !WW_MUTEX_DEBUG kernels.

Those extra checks sounds like a good idea to me, going to add that.

Thanks,
Christian.

Quoted 2 lines

Cheers, Daniel

_______________________________________________
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): b84dbae6-12ce-5632-d80f-567cb7fe678b-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org

#16 lore raw
Quoted 26 lines · Christian König

Am 24.09.2018 um 17:03 schrieb Daniel Vetter:

Quoted 8 lines · Christian König
Quoted 3 lines

The reservation object shared slot function only allowed to reserve one slot at a time.

Improve that and allow to reserve multiple slots to support atomically submission to multiple engines.

I think you can do this already, just don't drop the ww_mutex lock. And I also think that invariant still holds, if you drop the ww_mutex lock your fence slot reservation evaporates. Your new code is just a bit more convenient.

The problem is that allocating a slot could in theory fail with -ENOMEM.

And at the point we add the fence the hardware is already using the memory, so failure is not on option.

Key feature is that we are getting submissions to multiple engines which either needs to be submitted together or not at all.

Quoted 5 lines

Could we check/enforce this somehow when WW_MUTEX debugging is enabled? E.g. store the ww_mutex ctx in the reservation (we can dig it out from under the lock), and then check that the lock holder/ctx hasn't changed when adding all the fences? I think with multiple fences getting added atomically some more debug checks here would be good.

Yeah, I was already thinking about something similar.

Quoted 8 lines

We wouldn't need to remember the context or anything, but rather just set shared_max=shared_count in reservation_object_unlock().

Quoted 3 lines

Another one: Do we want to insist that you either add all the fences, or none, to make this fully atomic? We could check this when unlocking the reservation. Kinda hard to guess what your exact use-case here is.

No, some fence (like VM updates) are perfectly optional.

Ah right, vm moves might already be underway (even if you end up throwing all the actual CS stuff from userspace away), or might not happen.

Quoted 3 lines
Quoted 1 line

All this would ofc be compiled out for !WW_MUTEX_DEBUG kernels.

Those extra checks sounds like a good idea to me, going to add that.

Yeah that'd be great, just figured "this has lots of potential for nasty to debug bugs". The patches themselves look good to me, at least from skimming.
-Daniel

Signature · 7 lines
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx