Skip to contentSkip to conversation

drm/amdgpu: only keep most recent fence for each context

PATCH Superseded Arvind Yadav 3 participants 3 messages Apr 15, 2025 – Apr 16, 2025

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 drm/amdgpu: only keep most recent fence for each context

Mesa passes shared bo, fence syncobj to userq_ioctl. There can be duplicates here or some fences that are old. This patch is remove duplicates fence and only keep the most recent fence for each context.

Cc Alex Deucher <[email protected]>
Cc Christian König <[email protected]>
Cc Sunil Khatri <[email protected]>
Cc Arunpravin Paneer Selvam <[email protected]>
---
 .../gpu/drm/amd/amdgpu/amdgpu_userq_fence.c   | 37 +++++++++++++++++++
 1 file changed, 37 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c +37 -0
index a4953d668972..9a09d545ceb5 100644
@@ -25,6 +25,7 @@
 #include <linux/kref.h>
 #include <linux/slab.h>
 #include <linux/dma-fence-unwrap.h>
+#include <linux/sort.h>
 
 #include <drm/drm_exec.h>
 #include <drm/drm_syncobj.h>
@@ -584,6 +585,24 @@ int amdgpu_userq_signal_ioctl(struct drm_device *dev, void *data,
 #endif
 
 #ifdef CONFIG_DRM_AMDGPU_NAVI3X_USERQ
+static int fence_cmp(const void *_a, const void *_b)
+{
+	struct dma_fence *a = *(struct dma_fence **)_a;
+	struct dma_fence *b = *(struct dma_fence **)_b;
+
+	if (a->context < b->context)
+		return -1;
+	else if (a->context > b->context)
+		return 1;
+
+	if (dma_fence_is_later(b, a))
+		return 1;
+	else if (dma_fence_is_later(a, b))
+		return -1;
+
+	return 0;
+}
+
 int amdgpu_userq_wait_ioctl(struct drm_device *dev, void *data,
 			    struct drm_file *filp)
 {
@@ -840,6 +859,24 @@ int amdgpu_userq_wait_ioctl(struct drm_device *dev, void *data,
 			fences[num_fences++] = fence;
 		}
 
+		if (num_fences > 1) {
+			int j = 0;
+
+			sort(fences, num_fences, sizeof(*fences), fence_cmp, NULL);
+
+			/*
+			 * Only keep the most recent fence for each context.
+			 */
+			for (int i = 1; i < num_fences; i++) {
+				if (fences[i]->context == fences[j]->context)
+					dma_fence_put(fences[i]);
+				else
+					fences[++j] = fences[i];
+			}
+
+			num_fences = ++j;
+		}
+
 		for (i = 0, cnt = 0; i < num_fences; i++) {
 			struct amdgpu_userq_fence_driver *fence_drv;
 			struct amdgpu_userq_fence *userq_fence;
Signature · 1 line
2.34.1
maintainer
#1 lore raw

Am 15.04.25 um 16:18 schrieb Arvind Yadav:

Quoted 63 lines

Mesa passes shared bo, fence syncobj to userq_ioctl. There can be duplicates here or some fences that are old. This patch is remove duplicates fence and only keep the most recent fence for each context.

Cc Alex Deucher <[email protected]>
Cc Christian König <[email protected]>
Cc Sunil Khatri <[email protected]>
Cc Arunpravin Paneer Selvam <[email protected]>
---
 .../gpu/drm/amd/amdgpu/amdgpu_userq_fence.c   | 37 +++++++++++++++++++
 1 file changed, 37 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c +27 -0
index a4953d668972..9a09d545ceb5 100644
@@ -25,6 +25,7 @@
 #include <linux/kref.h>
 #include <linux/slab.h>
 #include <linux/dma-fence-unwrap.h>
+#include <linux/sort.h>
 
 #include <drm/drm_exec.h>
 #include <drm/drm_syncobj.h>
@@ -584,6 +585,24 @@ int amdgpu_userq_signal_ioctl(struct drm_device *dev, void *data,
 #endif
 
 #ifdef CONFIG_DRM_AMDGPU_NAVI3X_USERQ
+static int fence_cmp(const void *_a, const void *_b)
+{
+	struct dma_fence *a = *(struct dma_fence **)_a;
+	struct dma_fence *b = *(struct dma_fence **)_b;
+
+	if (a->context < b->context)
+		return -1;
+	else if (a->context > b->context)
+		return 1;
+
+	if (dma_fence_is_later(b, a))
+		return 1;
+	else if (dma_fence_is_later(a, b))
+		return -1;
+
+	return 0;
+}
+
 int amdgpu_userq_wait_ioctl(struct drm_device *dev, void *data,
 			    struct drm_file *filp)
 {
@@ -840,6 +859,24 @@ int amdgpu_userq_wait_ioctl(struct drm_device *dev, void *data,
 			fences[num_fences++] = fence;
 		}
 
+		if (num_fences > 1) {
+			int j = 0;
+
+			sort(fences, num_fences, sizeof(*fences), fence_cmp, NULL);
+
+			/*
+			 * Only keep the most recent fence for each context.
+			 */

drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c int amdgpu_userq_wait_ioctl(struct drm_device *dev, void *data, · line 879

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c +10 -0
+			for (int i = 1; i < num_fences; i++) {
+				if (fences[i]->context == fences[j]->context)
+					dma_fence_put(fences[i]);
+				else
+					fences[++j] = fences[i];
+			}
+
+			num_fences = ++j;
+		}
+

We already have the same code in drivers/dma-buf/dma-fence-unwrap.c.

I suggest to export this code from drivers/dma-buf/dma-fence-unwrap.c and just use it here as well.

Something like num_fences = dma_fence_shrink_array(fences, num_fences)

Regards,
Christian.

Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c +0 -0
 		for (i = 0, cnt = 0; i < num_fences; i++) {
 			struct amdgpu_userq_fence_driver *fence_drv;
 			struct amdgpu_userq_fence *userq_fence;
#2 lore raw
Quoted 76 lines · Christian König

Am 15.04.25 um 16:18 schrieb Arvind Yadav:

Quoted 63 lines

Mesa passes shared bo, fence syncobj to userq_ioctl. There can be duplicates here or some fences that are old. This patch is remove duplicates fence and only keep the most recent fence for each context.

Cc Alex Deucher <[email protected]>
Cc Christian König <[email protected]>
Cc Sunil Khatri <[email protected]>
Cc Arunpravin Paneer Selvam <[email protected]>
---
  .../gpu/drm/amd/amdgpu/amdgpu_userq_fence.c   | 37 +++++++++++++++++++
  1 file changed, 37 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c +27 -0
index a4953d668972..9a09d545ceb5 100644
@@ -25,6 +25,7 @@
  #include <linux/kref.h>
  #include <linux/slab.h>
  #include <linux/dma-fence-unwrap.h>
+#include <linux/sort.h>
 
  #include <drm/drm_exec.h>
  #include <drm/drm_syncobj.h>
@@ -584,6 +585,24 @@ int amdgpu_userq_signal_ioctl(struct drm_device *dev, void *data,
  #endif
 
  #ifdef CONFIG_DRM_AMDGPU_NAVI3X_USERQ
+static int fence_cmp(const void *_a, const void *_b)
+{
+	struct dma_fence *a = *(struct dma_fence **)_a;
+	struct dma_fence *b = *(struct dma_fence **)_b;
+
+	if (a->context < b->context)
+		return -1;
+	else if (a->context > b->context)
+		return 1;
+
+	if (dma_fence_is_later(b, a))
+		return 1;
+	else if (dma_fence_is_later(a, b))
+		return -1;
+
+	return 0;
+}
+
  int amdgpu_userq_wait_ioctl(struct drm_device *dev, void *data,
  			    struct drm_file *filp)
  {
@@ -840,6 +859,24 @@ int amdgpu_userq_wait_ioctl(struct drm_device *dev, void *data,
  			fences[num_fences++] = fence;
  		}
 
+		if (num_fences > 1) {
+			int j = 0;
+
+			sort(fences, num_fences, sizeof(*fences), fence_cmp, NULL);
+
+			/*
+			 * Only keep the most recent fence for each context.
+			 */

drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c int amdgpu_userq_wait_ioctl(struct drm_device *dev, void *data, · line 879

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c +10 -0
+			for (int i = 1; i < num_fences; i++) {
+				if (fences[i]->context == fences[j]->context)
+					dma_fence_put(fences[i]);
+				else
+					fences[++j] = fences[i];
+			}
+
+			num_fences = ++j;
+		}
+

We already have the same code in drivers/dma-buf/dma-fence-unwrap.c.

Quoted 3 lines

I suggest to export this code from drivers/dma-buf/dma-fence-unwrap.c and just use it here as well.

Something like num_fences = dma_fence_shrink_array(fences, num_fences)

Noted.

Quoted 7 lines

Regards,
Christian.

Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c +0 -0
  		for (i = 0, cnt = 0; i < num_fences; i++) {
  			struct amdgpu_userq_fence_driver *fence_drv;
  			struct amdgpu_userq_fence *userq_fence;