All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Christian König" <ckoenig.leichtzumerken@gmail.com>
To: dri-devel@lists.freedesktop.org, chris@chris-wilson.co.uk,
	daniel.vetter@ffwll.ch, sumit.semwal@linaro.org,
	linux-media@vger.kernel.org, linaro-mm-sig@lists.linaro.org
Subject: [PATCH 6/9] dma-buf/resv: add dma_resv_prune_fences v2
Date: Mon, 26 Aug 2019 16:57:28 +0200	[thread overview]
Message-ID: <20190826145731.1725-7-christian.koenig@amd.com> (raw)
In-Reply-To: <20190826145731.1725-1-christian.koenig@amd.com>

Add a new dma_resv_prune_fences() function to improve memory management.

v2: fix potential NULL deref

Signed-off-by: Christian König <christian.koenig@amd.com>
---
 drivers/dma-buf/dma-resv.c                 | 37 ++++++++++++++++++++++
 drivers/gpu/drm/i915/gem/i915_gem_wait.c   |  3 +-
 drivers/gpu/drm/i915/i915_gem_batch_pool.c |  2 +-
 drivers/gpu/drm/i915/i915_vma.c            |  3 +-
 drivers/gpu/drm/ttm/ttm_bo.c               |  2 +-
 include/linux/dma-resv.h                   |  1 +
 6 files changed, 42 insertions(+), 6 deletions(-)

diff --git a/drivers/dma-buf/dma-resv.c b/drivers/dma-buf/dma-resv.c
index 42a8f3f11681..59fbcd9f4b01 100644
--- a/drivers/dma-buf/dma-resv.c
+++ b/drivers/dma-buf/dma-resv.c
@@ -301,6 +301,43 @@ void dma_resv_add_excl_fence(struct dma_resv *obj, struct dma_fence *fence)
 }
 EXPORT_SYMBOL(dma_resv_add_excl_fence);
 
+/**
+ * dma_resv_prune_fences - prune signaled fences from the resv object
+ * @obj: the reservation object to prune
+ *
+ * Prune all signaled fences from the reservation object.
+ */
+void dma_resv_prune_fences(struct dma_resv *obj)
+{
+	struct dma_resv_list *list;
+	struct dma_fence *fence;
+	unsigned int i;
+
+	dma_resv_assert_held(obj);
+
+	fence = dma_resv_get_excl(obj);
+	if (fence && dma_fence_is_signaled(fence)) {
+		RCU_INIT_POINTER(obj->fence_excl, NULL);
+		dma_fence_put(fence);
+	}
+
+	list = dma_resv_get_list(obj);
+	if (!list)
+		return;
+
+	for (i = 0; i < list->shared_count; ++i) {
+		fence = rcu_dereference_protected(list->shared[i],
+						  dma_resv_held(obj));
+
+		if (!dma_fence_is_signaled(fence))
+			continue;
+
+		RCU_INIT_POINTER(list->shared[i], dma_fence_get_stub());
+		dma_fence_put(fence);
+	}
+}
+EXPORT_SYMBOL(dma_resv_prune_fences);
+
 /**
 * dma_resv_copy_fences - Copy all fences from src to dst.
 * @dst: the destination reservation object
diff --git a/drivers/gpu/drm/i915/gem/i915_gem_wait.c b/drivers/gpu/drm/i915/gem/i915_gem_wait.c
index 8af55cd3e690..a76d36f8fb77 100644
--- a/drivers/gpu/drm/i915/gem/i915_gem_wait.c
+++ b/drivers/gpu/drm/i915/gem/i915_gem_wait.c
@@ -85,8 +85,7 @@ i915_gem_object_wait_reservation(struct dma_resv *resv,
 	 * signaled.
 	 */
 	if (prune_fences && dma_resv_trylock(resv)) {
-		if (dma_resv_test_signaled_rcu(resv, true))
-			dma_resv_add_excl_fence(resv, NULL);
+		dma_resv_prune_fences(resv);
 		dma_resv_unlock(resv);
 	}
 
diff --git a/drivers/gpu/drm/i915/i915_gem_batch_pool.c b/drivers/gpu/drm/i915/i915_gem_batch_pool.c
index 5f82a763e64c..274cf5b19fc9 100644
--- a/drivers/gpu/drm/i915/i915_gem_batch_pool.c
+++ b/drivers/gpu/drm/i915/i915_gem_batch_pool.c
@@ -114,7 +114,7 @@ i915_gem_batch_pool_get(struct i915_gem_batch_pool *pool,
 			 */
 			if (rcu_access_pointer(resv->fence)) {
 				dma_resv_lock(resv, NULL);
-				dma_resv_add_excl_fence(resv, NULL);
+				dma_resv_prune_fences(resv);
 				dma_resv_unlock(resv);
 			}
 		}
diff --git a/drivers/gpu/drm/i915/i915_vma.c b/drivers/gpu/drm/i915/i915_vma.c
index ebfd03d117cd..fcbe433a968c 100644
--- a/drivers/gpu/drm/i915/i915_vma.c
+++ b/drivers/gpu/drm/i915/i915_vma.c
@@ -100,8 +100,7 @@ static void __i915_vma_retire(struct i915_active *ref)
 
 	/* Prune the shared fence arrays iff completely idle (inc. external) */
 	if (dma_resv_trylock(obj->base.resv)) {
-		if (dma_resv_test_signaled_rcu(obj->base.resv, true))
-			dma_resv_add_excl_fence(obj->base.resv, NULL);
+		dma_resv_prune_fences(obj->base.resv);
 		dma_resv_unlock(obj->base.resv);
 	}
 
diff --git a/drivers/gpu/drm/ttm/ttm_bo.c b/drivers/gpu/drm/ttm/ttm_bo.c
index 58d1f2b28132..f78f52cc2e6d 100644
--- a/drivers/gpu/drm/ttm/ttm_bo.c
+++ b/drivers/gpu/drm/ttm/ttm_bo.c
@@ -1818,7 +1818,7 @@ int ttm_bo_wait(struct ttm_buffer_object *bo,
 	if (timeout == 0)
 		return -EBUSY;
 
-	dma_resv_add_excl_fence(bo->base.resv, NULL);
+	dma_resv_prune_fences(bo->base.resv);
 	return 0;
 }
 EXPORT_SYMBOL(ttm_bo_wait);
diff --git a/include/linux/dma-resv.h b/include/linux/dma-resv.h
index ee50d10f052b..03b0f95682b0 100644
--- a/include/linux/dma-resv.h
+++ b/include/linux/dma-resv.h
@@ -279,6 +279,7 @@ int dma_resv_reserve_shared(struct dma_resv *obj, unsigned int num_fences);
 void dma_resv_add_shared_fence(struct dma_resv *obj, struct dma_fence *fence);
 
 void dma_resv_add_excl_fence(struct dma_resv *obj, struct dma_fence *fence);
+void dma_resv_prune_fences(struct dma_resv *obj);
 
 int dma_resv_get_fences_rcu(struct dma_resv *obj,
 			    struct dma_fence **pfence_excl,
-- 
2.17.1


WARNING: multiple messages have this Message-ID (diff)
From: "Christian König" <ckoenig.leichtzumerken@gmail.com>
To: dri-devel@lists.freedesktop.org, chris@chris-wilson.co.uk,
	daniel.vetter@ffwll.ch, sumit.semwal@linaro.org,
	linux-media@vger.kernel.org, linaro-mm-sig@lists.linaro.org
Subject: [PATCH 6/9] dma-buf/resv: add dma_resv_prune_fences v2
Date: Mon, 26 Aug 2019 16:57:28 +0200	[thread overview]
Message-ID: <20190826145731.1725-7-christian.koenig@amd.com> (raw)
In-Reply-To: <20190826145731.1725-1-christian.koenig@amd.com>

Add a new dma_resv_prune_fences() function to improve memory management.

v2: fix potential NULL deref

Signed-off-by: Christian König <christian.koenig@amd.com>
---
 drivers/dma-buf/dma-resv.c                 | 37 ++++++++++++++++++++++
 drivers/gpu/drm/i915/gem/i915_gem_wait.c   |  3 +-
 drivers/gpu/drm/i915/i915_gem_batch_pool.c |  2 +-
 drivers/gpu/drm/i915/i915_vma.c            |  3 +-
 drivers/gpu/drm/ttm/ttm_bo.c               |  2 +-
 include/linux/dma-resv.h                   |  1 +
 6 files changed, 42 insertions(+), 6 deletions(-)

diff --git a/drivers/dma-buf/dma-resv.c b/drivers/dma-buf/dma-resv.c
index 42a8f3f11681..59fbcd9f4b01 100644
--- a/drivers/dma-buf/dma-resv.c
+++ b/drivers/dma-buf/dma-resv.c
@@ -301,6 +301,43 @@ void dma_resv_add_excl_fence(struct dma_resv *obj, struct dma_fence *fence)
 }
 EXPORT_SYMBOL(dma_resv_add_excl_fence);
 
+/**
+ * dma_resv_prune_fences - prune signaled fences from the resv object
+ * @obj: the reservation object to prune
+ *
+ * Prune all signaled fences from the reservation object.
+ */
+void dma_resv_prune_fences(struct dma_resv *obj)
+{
+	struct dma_resv_list *list;
+	struct dma_fence *fence;
+	unsigned int i;
+
+	dma_resv_assert_held(obj);
+
+	fence = dma_resv_get_excl(obj);
+	if (fence && dma_fence_is_signaled(fence)) {
+		RCU_INIT_POINTER(obj->fence_excl, NULL);
+		dma_fence_put(fence);
+	}
+
+	list = dma_resv_get_list(obj);
+	if (!list)
+		return;
+
+	for (i = 0; i < list->shared_count; ++i) {
+		fence = rcu_dereference_protected(list->shared[i],
+						  dma_resv_held(obj));
+
+		if (!dma_fence_is_signaled(fence))
+			continue;
+
+		RCU_INIT_POINTER(list->shared[i], dma_fence_get_stub());
+		dma_fence_put(fence);
+	}
+}
+EXPORT_SYMBOL(dma_resv_prune_fences);
+
 /**
 * dma_resv_copy_fences - Copy all fences from src to dst.
 * @dst: the destination reservation object
diff --git a/drivers/gpu/drm/i915/gem/i915_gem_wait.c b/drivers/gpu/drm/i915/gem/i915_gem_wait.c
index 8af55cd3e690..a76d36f8fb77 100644
--- a/drivers/gpu/drm/i915/gem/i915_gem_wait.c
+++ b/drivers/gpu/drm/i915/gem/i915_gem_wait.c
@@ -85,8 +85,7 @@ i915_gem_object_wait_reservation(struct dma_resv *resv,
 	 * signaled.
 	 */
 	if (prune_fences && dma_resv_trylock(resv)) {
-		if (dma_resv_test_signaled_rcu(resv, true))
-			dma_resv_add_excl_fence(resv, NULL);
+		dma_resv_prune_fences(resv);
 		dma_resv_unlock(resv);
 	}
 
diff --git a/drivers/gpu/drm/i915/i915_gem_batch_pool.c b/drivers/gpu/drm/i915/i915_gem_batch_pool.c
index 5f82a763e64c..274cf5b19fc9 100644
--- a/drivers/gpu/drm/i915/i915_gem_batch_pool.c
+++ b/drivers/gpu/drm/i915/i915_gem_batch_pool.c
@@ -114,7 +114,7 @@ i915_gem_batch_pool_get(struct i915_gem_batch_pool *pool,
 			 */
 			if (rcu_access_pointer(resv->fence)) {
 				dma_resv_lock(resv, NULL);
-				dma_resv_add_excl_fence(resv, NULL);
+				dma_resv_prune_fences(resv);
 				dma_resv_unlock(resv);
 			}
 		}
diff --git a/drivers/gpu/drm/i915/i915_vma.c b/drivers/gpu/drm/i915/i915_vma.c
index ebfd03d117cd..fcbe433a968c 100644
--- a/drivers/gpu/drm/i915/i915_vma.c
+++ b/drivers/gpu/drm/i915/i915_vma.c
@@ -100,8 +100,7 @@ static void __i915_vma_retire(struct i915_active *ref)
 
 	/* Prune the shared fence arrays iff completely idle (inc. external) */
 	if (dma_resv_trylock(obj->base.resv)) {
-		if (dma_resv_test_signaled_rcu(obj->base.resv, true))
-			dma_resv_add_excl_fence(obj->base.resv, NULL);
+		dma_resv_prune_fences(obj->base.resv);
 		dma_resv_unlock(obj->base.resv);
 	}
 
diff --git a/drivers/gpu/drm/ttm/ttm_bo.c b/drivers/gpu/drm/ttm/ttm_bo.c
index 58d1f2b28132..f78f52cc2e6d 100644
--- a/drivers/gpu/drm/ttm/ttm_bo.c
+++ b/drivers/gpu/drm/ttm/ttm_bo.c
@@ -1818,7 +1818,7 @@ int ttm_bo_wait(struct ttm_buffer_object *bo,
 	if (timeout == 0)
 		return -EBUSY;
 
-	dma_resv_add_excl_fence(bo->base.resv, NULL);
+	dma_resv_prune_fences(bo->base.resv);
 	return 0;
 }
 EXPORT_SYMBOL(ttm_bo_wait);
diff --git a/include/linux/dma-resv.h b/include/linux/dma-resv.h
index ee50d10f052b..03b0f95682b0 100644
--- a/include/linux/dma-resv.h
+++ b/include/linux/dma-resv.h
@@ -279,6 +279,7 @@ int dma_resv_reserve_shared(struct dma_resv *obj, unsigned int num_fences);
 void dma_resv_add_shared_fence(struct dma_resv *obj, struct dma_fence *fence);
 
 void dma_resv_add_excl_fence(struct dma_resv *obj, struct dma_fence *fence);
+void dma_resv_prune_fences(struct dma_resv *obj);
 
 int dma_resv_get_fences_rcu(struct dma_resv *obj,
 			    struct dma_fence **pfence_excl,
-- 
2.17.1

_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/dri-devel

  parent reply	other threads:[~2019-08-26 14:57 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-08-26 14:57 Use dma_fence_array for implementing shared dma_resv fences Christian König
2019-08-26 14:57 ` Christian König
2019-08-26 14:57 ` [PATCH 1/9] dma-buf: fix dma_fence_array_signaled Christian König
2019-08-26 14:57   ` Christian König
2019-08-27 11:44   ` Chris Wilson
2019-08-27 11:44     ` Chris Wilson
2019-08-26 14:57 ` [PATCH 2/9] dma-buf: make to_dma_fence_array NULL safe Christian König
2019-08-26 14:57   ` Christian König
2019-08-30 16:07   ` Ruhl, Michael J
2019-08-30 16:07     ` Ruhl, Michael J
2019-08-26 14:57 ` [PATCH 3/9] dma-buf: add dma_fence_array_alloc/free Christian König
2019-08-26 14:57   ` Christian König
2019-08-26 14:57 ` [PATCH 4/9] dma-buf: add dma_fence_array_recycle v2 Christian König
2019-08-26 14:57   ` Christian König
2019-08-26 14:57 ` [PATCH 5/9] dma-buf: add dma_fence_array_for_each Christian König
2019-08-26 14:57   ` Christian König
2019-08-26 14:57 ` Christian König [this message]
2019-08-26 14:57   ` [PATCH 6/9] dma-buf/resv: add dma_resv_prune_fences v2 Christian König
2019-08-26 14:57 ` [PATCH 7/9] dma-buf/resv: add new fences container implementation Christian König
2019-08-26 14:57   ` Christian König
2019-08-26 14:57 ` [PATCH 8/9] dma-buf/resv: use new dma_fence_array based implementation Christian König
2019-08-26 14:57   ` Christian König
2019-08-26 14:57 ` [PATCH 9/9] dma-buf/resv: drop the sequence count Christian König
2019-08-26 14:57   ` Christian König
2019-08-27 16:37 ` Use dma_fence_array for implementing shared dma_resv fences Daniel Vetter
2019-08-27 16:37   ` Daniel Vetter

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20190826145731.1725-7-christian.koenig@amd.com \
    --to=ckoenig.leichtzumerken@gmail.com \
    --cc=chris@chris-wilson.co.uk \
    --cc=daniel.vetter@ffwll.ch \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=linaro-mm-sig@lists.linaro.org \
    --cc=linux-media@vger.kernel.org \
    --cc=sumit.semwal@linaro.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.