Skip to contentSkip to conversation

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

PATCH In review Arvind Yadav 4 participants 4 messages Apr 23, 2025

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

PATCH v2 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.

v2: Export this code from dma-fence-unwrap.c(by Christian).

Cc Alex Deucher <[email protected]>
Cc Christian König <[email protected]>
Cc Sunil Khatri <[email protected]>
Cc Arunpravin Paneer Selvam <[email protected]>
---
 drivers/dma-buf/dma-fence-unwrap.c            | 29 +++++++++++++++++++
 .../gpu/drm/amd/amdgpu/amdgpu_userq_fence.c   |  2 ++
 include/linux/dma-fence-unwrap.h              |  3 ++
 3 files changed, 34 insertions(+)
drivers/dma-buf/dma-fence-unwrap.c +29 -0
index 2a059ac0ed27..18874eedf091 100644
@@ -179,3 +179,32 @@ struct dma_fence *__dma_fence_unwrap_merge(unsigned int num_fences,
 	return tmp;
 }
 EXPORT_SYMBOL_GPL(__dma_fence_unwrap_merge);
+
+/**
+ * dma_fence_shrink_array - return the shrink fences from fence containers
+ * @fences: array of fence
+ * @num_fences: number of fence in fence array @fences
+ *
+ * only keep most recent fence for each context. It will remove duplicate
+ * and old fence of same context.
+ */
+unsigned short dma_fence_shrink_array(struct dma_fence **fences,
+				      unsigned short num_fences)
+{
+	unsigned short count = 0;
+
+	if (num_fences <= 1)
+		return num_fences;
+
+	sort(fences, num_fences, sizeof(*fences), fence_cmp, NULL);
+
+	for (int i = 1; i < num_fences; i++) {
+		if (fences[i]->context == fences[count]->context)
+			dma_fence_put(fences[i]);
+		else
+			fences[++count] = fences[i];
+	}
+
+	return ++count;
+}
+EXPORT_SYMBOL_GPL(dma_fence_shrink_array);
drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c +2 -0
index 3288c2ff692e..cc74bb0ed06f 100644
@@ -851,6 +851,8 @@ int amdgpu_userq_wait_ioctl(struct drm_device *dev, void *data,
 			fences[num_fences++] = fence;
 		}
 
+		num_fences = dma_fence_shrink_array(fences, num_fences);
+
 		waitq = idr_find(&userq_mgr->userq_idr, wait_info->waitq_id);
 		if (!waitq)
 			goto free_fences;
include/linux/dma-fence-unwrap.h +3 -0
index 66b1e56fbb81..94801feeee4b 100644
@@ -72,4 +72,7 @@ struct dma_fence *__dma_fence_unwrap_merge(unsigned int num_fences,
 		__dma_fence_unwrap_merge(ARRAY_SIZE(__f), __f, __c);	\
 	})
 
+
+unsigned short dma_fence_shrink_array(struct dma_fence **fences,
+				      unsigned short num_fences);
 #endif
Signature · 1 line
2.34.1
#1 lore raw

[AMD Official Use Only - AMD Internal Distribution Only]

You need to split this in a dma_buf patch and amd userq patch separately.

Regards
Sunil Khatri

-----Original Message-----
From: Yadav, Arvind <[email protected]>
Sent: Wednesday, April 23, 2025 4:18 PM
To: Koenig, Christian <[email protected]>; Deucher, Alexander <[email protected]>; Khatri, Sunil <[email protected]>; Paneer Selvam, Arunpravin <[email protected]>
Cc: [email protected]; Yadav, Arvind <[email protected]>; Koenig, Christian <[email protected]>; Yadav, Arvind <[email protected]>
Subject: [PATCH v2] 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.

v2: Export this code from dma-fence-unwrap.c(by Christian).

Cc Alex Deucher <[email protected]>
Cc Christian König <[email protected]>
Cc Sunil Khatri <[email protected]>
Cc Arunpravin Paneer Selvam <[email protected]>
---
 drivers/dma-buf/dma-fence-unwrap.c            | 29 +++++++++++++++++++
 .../gpu/drm/amd/amdgpu/amdgpu_userq_fence.c   |  2 ++
 include/linux/dma-fence-unwrap.h              |  3 ++
 3 files changed, 34 insertions(+)
drivers/dma-buf/dma-fence-unwrap.c +29 -0
index 2a059ac0ed27..18874eedf091 100644
@@ -179,3 +179,32 @@ struct dma_fence *__dma_fence_unwrap_merge(unsigned int num_fences,
        return tmp;
 }
 EXPORT_SYMBOL_GPL(__dma_fence_unwrap_merge);
+
+/**
+ * dma_fence_shrink_array - return the shrink fences from fence
+containers
+ * @fences: array of fence
+ * @num_fences: number of fence in fence array @fences
+ *
+ * only keep most recent fence for each context. It will remove
+duplicate
+ * and old fence of same context.
+ */
+unsigned short dma_fence_shrink_array(struct dma_fence **fences,
+                                     unsigned short num_fences)
+{
+       unsigned short count = 0;
+
+       if (num_fences <= 1)
+               return num_fences;
+
+       sort(fences, num_fences, sizeof(*fences), fence_cmp, NULL);
+
+       for (int i = 1; i < num_fences; i++) {
+               if (fences[i]->context == fences[count]->context)
+                       dma_fence_put(fences[i]);
+               else
+                       fences[++count] = fences[i];
+       }
+
+       return ++count;

+}
+EXPORT_SYMBOL_GPL(dma_fence_shrink_array);

drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c +2 -0
index 3288c2ff692e..cc74bb0ed06f 100644
@@ -851,6 +851,8 @@ int amdgpu_userq_wait_ioctl(struct drm_device *dev, void *data,
                        fences[num_fences++] = fence;
                }
 
+               num_fences = dma_fence_shrink_array(fences, num_fences);
+
                waitq = idr_find(&userq_mgr->userq_idr, wait_info->waitq_id);
                if (!waitq)
                        goto free_fences;
include/linux/dma-fence-unwrap.h +3 -0
index 66b1e56fbb81..94801feeee4b 100644
@@ -72,4 +72,7 @@ struct dma_fence *__dma_fence_unwrap_merge(unsigned int num_fences,
                __dma_fence_unwrap_merge(ARRAY_SIZE(__f), __f, __c);    \
        })
 
+
+unsigned short dma_fence_shrink_array(struct dma_fence **fences,
+                                     unsigned short num_fences);
 #endif

--
2.34.1

maintainer
#2 lore raw
Quoted 3 lines · Sunil

[AMD Official Use Only - AMD Internal Distribution Only]

You need to split this in a dma_buf patch and amd userq patch separately.

Yeah agree, and the dma_buf patch should re-use and re-structure the existing code and not just add a new function.

Regards,
Christian.

Quoted 95 lines

Regards
Sunil Khatri

-----Original Message-----
From: Yadav, Arvind <[email protected]>
Sent: Wednesday, April 23, 2025 4:18 PM
To: Koenig, Christian <[email protected]>; Deucher, Alexander <[email protected]>; Khatri, Sunil <[email protected]>; Paneer Selvam, Arunpravin <[email protected]>
Cc: [email protected]; Yadav, Arvind <[email protected]>; Koenig, Christian <[email protected]>; Yadav, Arvind <[email protected]>
Subject: [PATCH v2] 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.

v2: Export this code from dma-fence-unwrap.c(by Christian).

Cc Alex Deucher <[email protected]>
Cc Christian König <[email protected]>
Cc Sunil Khatri <[email protected]>
Cc Arunpravin Paneer Selvam <[email protected]>
---
  drivers/dma-buf/dma-fence-unwrap.c            | 29 +++++++++++++++++++
  .../gpu/drm/amd/amdgpu/amdgpu_userq_fence.c   |  2 ++
  include/linux/dma-fence-unwrap.h              |  3 ++
  3 files changed, 34 insertions(+)
drivers/dma-buf/dma-fence-unwrap.c +31 -0
index 2a059ac0ed27..18874eedf091 100644
@@ -179,3 +179,32 @@ struct dma_fence *__dma_fence_unwrap_merge(unsigned int num_fences,
         return tmp;
  }
  EXPORT_SYMBOL_GPL(__dma_fence_unwrap_merge);
+
+/**
+ * dma_fence_shrink_array - return the shrink fences from fence
+containers
+ * @fences: array of fence
+ * @num_fences: number of fence in fence array @fences
+ *
+ * only keep most recent fence for each context. It will remove
+duplicate
+ * and old fence of same context.
+ */
+unsigned short dma_fence_shrink_array(struct dma_fence **fences,
+                                     unsigned short num_fences)
+{
+       unsigned short count = 0;
+
+       if (num_fences <= 1)
+               return num_fences;
+
+       sort(fences, num_fences, sizeof(*fences), fence_cmp, NULL);
+
+       for (int i = 1; i < num_fences; i++) {
+               if (fences[i]->context == fences[count]->context)
+                       dma_fence_put(fences[i]);
+               else
+                       fences[++count] = fences[i];
+       }
+
+       return ++count;
+}
+EXPORT_SYMBOL_GPL(dma_fence_shrink_array);
drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c +2 -0
index 3288c2ff692e..cc74bb0ed06f 100644
@@ -851,6 +851,8 @@ int amdgpu_userq_wait_ioctl(struct drm_device *dev, void *data,
                         fences[num_fences++] = fence;
                 }
 
+               num_fences = dma_fence_shrink_array(fences, num_fences);
+
                 waitq = idr_find(&userq_mgr->userq_idr, wait_info->waitq_id);
                 if (!waitq)
                         goto free_fences;
include/linux/dma-fence-unwrap.h +3 -1
index 66b1e56fbb81..94801feeee4b 100644
@@ -72,4 +72,7 @@ struct dma_fence *__dma_fence_unwrap_merge(unsigned int num_fences,
                 __dma_fence_unwrap_merge(ARRAY_SIZE(__f), __f, __c);    \
         })
 
+
+unsigned short dma_fence_shrink_array(struct dma_fence **fences,
+                                     unsigned short num_fences);
  #endif
--

2.34.1

#3 lore raw
Quoted 8 lines · Christian König
Quoted 3 lines · Sunil

[AMD Official Use Only - AMD Internal Distribution Only]

You need to split this in a dma_buf patch and amd userq patch separately.

Yeah agree, and the dma_buf patch should re-use and re-structure the existing code and not just add a new function.

sure, I will not add new function and try to re-use existing.

Thankyou,
~arvind

Quoted 101 lines

Regards,
Christian.

Quoted 98 lines

Regards
Sunil Khatri

-----Original Message-----
From: Yadav, Arvind <[email protected]>
Sent: Wednesday, April 23, 2025 4:18 PM
To: Koenig, Christian <[email protected]>; Deucher, Alexander <[email protected]>; Khatri, Sunil <[email protected]>; Paneer Selvam, Arunpravin <[email protected]>
Cc: [email protected]; Yadav, Arvind <[email protected]>; Koenig, Christian <[email protected]>; Yadav, Arvind <[email protected]>
Subject: [PATCH v2] 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.

v2: Export this code from dma-fence-unwrap.c(by Christian).

Cc Alex Deucher <[email protected]>
Cc Christian König <[email protected]>
Cc Sunil Khatri <[email protected]>
Cc Arunpravin Paneer Selvam <[email protected]>

---
  drivers/dma-buf/dma-fence-unwrap.c            | 29 +++++++++++++++++++ .../gpu/drm/amd/amdgpu/amdgpu_userq_fence.c   |  2 ++ include/linux/dma-fence-unwrap.h              |  3 ++

  3 files changed, 34 insertions(+)
drivers/dma-buf/dma-fence-unwrap.c +0 -0
index 2a059ac0ed27..18874eedf091 100644
@@ -179,3 +179,32 @@ struct dma_fence 
 *__dma_fence_unwrap_merge(unsigned int num_fences,
          return tmp;
   }

  EXPORT_SYMBOL_GPL(__dma_fence_unwrap_merge);

drivers/dma-buf/dma-fence-unwrap.c +31 -0
+
+/**
+ * dma_fence_shrink_array - return the shrink fences from fence
+containers
+ * @fences: array of fence
+ * @num_fences: number of fence in fence array @fences
+ *
+ * only keep most recent fence for each context. It will remove
+duplicate
+ * and old fence of same context.
+ */
+unsigned short dma_fence_shrink_array(struct dma_fence **fences,
+                                     unsigned short num_fences)
+{
+       unsigned short count = 0;
+
+       if (num_fences <= 1)
+               return num_fences;
+
+       sort(fences, num_fences, sizeof(*fences), fence_cmp, NULL);
+
+       for (int i = 1; i < num_fences; i++) {
+               if (fences[i]->context == fences[count]->context)
+                       dma_fence_put(fences[i]);
+               else
+                       fences[++count] = fences[i];
+       }
+
+       return ++count;
+}
+EXPORT_SYMBOL_GPL(dma_fence_shrink_array);
drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c +2 -0
index 3288c2ff692e..cc74bb0ed06f 100644
@@ -851,6 +851,8 @@ int amdgpu_userq_wait_ioctl(struct drm_device 
 *dev, void *data,
                          fences[num_fences++] = fence;
                  }
 
+               num_fences = dma_fence_shrink_array(fences, num_fences);
+
                  waitq = idr_find(&userq_mgr->userq_idr, wait_info->waitq_id);
                  if (!waitq)

                         goto free_fences;

include/linux/dma-fence-unwrap.h +3 -0
index 66b1e56fbb81..94801feeee4b 100644
@@ -72,4 +72,7 @@ struct dma_fence *__dma_fence_unwrap_merge(unsigned 
 int num_fences,
                  __dma_fence_unwrap_merge(ARRAY_SIZE(__f), __f, __c);    \
          })
 
+
+unsigned short dma_fence_shrink_array(struct dma_fence **fences,
+                                     unsigned short num_fences);

  #endif

Signature · 1 line
2.34.1