Skip to contentSkip to conversation

dma-buf/test: add KUnit case for dma_fence_dedup_array() empty input

PATCH Superseded Baineng Shou 3 participants 6 messages Jun 30 – Oct 14

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

PATCH 1/1 dma-buf/test: add KUnit case for dma_fence_dedup_array() empty input

Add a KUnit case verifying that dma_fence_dedup_array() returns 0, not 1, when called with num_fences == 0. This locks down the contract documented in the kernel-doc and prevents a regression of commit 77a9298741f8 ("dma-fence: Make dma_fence_dedup_array() robust against 0-count input").

---
 drivers/dma-buf/st-dma-fence-unwrap.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
drivers/dma-buf/st-dma-fence-unwrap.c +10 -0
index 4e7ee25372ba..72dea6091d58 100644
@@ -370,6 +370,15 @@ static void test_unwrap_merge_seqno(struct kunit *test)
 	dma_fence_put(f1);
 }
 
+static void test_dedup_empty(struct kunit *test)
+{
+	struct dma_fence *fence[1] = { NULL };
+	int ret;
+
+	ret = dma_fence_dedup_array(fence, 0);
+	KUNIT_EXPECT_EQ(test, ret, 0);
+}
+
 static void test_unwrap_merge_order(struct kunit *test)
 {
 	struct dma_fence *fence, *f1, *f2, *a1, *a2, *c1, *c2;
@@ -595,6 +604,7 @@ static struct kunit_case dma_fence_unwrap_cases[] = {
 	KUNIT_CASE(test_unwrap_merge_order),
 	KUNIT_CASE(test_unwrap_merge_complex),
 	KUNIT_CASE(test_unwrap_merge_complex_seqno),
+	KUNIT_CASE(test_dedup_empty),
 	{}
 };
 
Signature · 1 line
2.34.1
#1 lore raw
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

New issues:
- [Medium] The newly added test_dedup_empty KUnit test will unconditionally fail, breaking CI.

Pre-existing issues:
- [Critical] dma_fence_dedup_array() incorrectly returns 1 for an empty array, causing uninitialized memory dereference in amdgpu_userq_wait_return_fence_info(). --

commit 693b63d34624f1aa69f2c97819d4a6d6655ec6bc
Author: Baineng Shou <[email protected]>

dma-buf/test: add KUnit case for dma_fence_dedup_array() empty input

This commit adds a KUnit test case to verify that dma_fence_dedup_array() returns 0 when provided with an empty array. The test aims to ensure the documented contract is enforced and prevents future regressions.

Quoted 4 lines
drivers/dma-buf/st-dma-fence-unwrap.c +0 -0
index 4e7ee25372ba6..72dea6091d583 100644

[ ... ]

drivers/dma-buf/st-dma-fence-unwrap.c static void test_unwrap_merge_seqno(struct kunit *test) · line 379

Quoted 11 lines
drivers/dma-buf/st-dma-fence-unwrap.c +7 -0
@@ -370,6 +370,15 @@ static void test_unwrap_merge_seqno(struct kunit *test)
 	dma_fence_put(f1);
 }
 
+static void test_dedup_empty(struct kunit *test)
+{
+	struct dma_fence *fence[1] = { NULL };
+	int ret;
+
+	ret = dma_fence_dedup_array(fence, 0);
+	KUNIT_EXPECT_EQ(test, ret, 0);

[Severity: Medium]
Will this test fail unconditionally on the current tree?

Looking at dma_fence_dedup_array() in drivers/dma-buf/dma-fence-unwrap.c, there is no early return for an empty array:

int dma_fence_dedup_array(struct dma_fence **fences, int num_fences)

{
        int i, j;

        sort(fences, num_fences, sizeof(*fences), fence_cmp, NULL);

        j = 0;
        for (i = 1; i < num_fences; i++) {
                ...
        }

        return ++j;
}

The loop condition i < num_fences is skipped when num_fences is 0, but the function ends with return ++j, which returns 1. Since this patch adds a KUnit expectation for 0, won't this cause CI to break until the corresponding fix for dma_fence_dedup_array() is actually applied to the tree?

[Severity: Critical]
This is a pre-existing issue, but does dma_fence_dedup_array() returning 1 for empty arrays cause an uninitialized memory dereference in other subsystems?

For example, in drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c:
amdgpu_userq_wait_return_fence_info() uses kmalloc_array for the fences array and eventually calls dma_fence_dedup_array:

        num_fences = dma_fence_dedup_array(fences, num_fences);

        waitq = amdgpu_userq_get(userq_mgr, wait_info->waitq_id);
        if (!waitq) {
        ...
        for (i = 0, cnt = 0; i < num_fences; i++) {
                struct amdgpu_userq_fence_driver *fence_drv;
                struct amdgpu_userq_fence *userq_fence;
                u32 index;

                userq_fence = to_amdgpu_userq_fence(fences[i]);

If an unprivileged user triggers a path via ioctl where 0 fences are populated but dma_fence_dedup_array() is called with num_fences = 0, returning 1 will cause the subsequent loop to iterate once. Will this dereference an uninitialized garbage pointer from kmalloc_array() when to_amdgpu_userq_fence() evaluates fences[i]?

Quoted 3 lines
drivers/dma-buf/st-dma-fence-unwrap.c +2 -0
+}
+
 static void test_unwrap_merge_order(struct kunit *test)

[ ... ]

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
maintainer Reviewed-by on v1 1/1
#2 lore raw
In reply to Baineng Shou
Quoted 8 lines · Baineng Shou

Add a KUnit case verifying that dma_fence_dedup_array() returns 0, not 1, when called with num_fences == 0. This locks down the contract documented in the kernel-doc and prevents a regression of commit 77a9298741f8 ("dma-fence: Make dma_fence_dedup_array() robust against 0-count input").

Going to push this to drm-misc-next later today. I also pinged our intern to write a cleanup patch whic consistently use size_t for the number of array elements in the code.

Thanks,
Christian.

Quoted 32 lines
---
 drivers/dma-buf/st-dma-fence-unwrap.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
drivers/dma-buf/st-dma-fence-unwrap.c +10 -0
index 4e7ee25372ba..72dea6091d58 100644
@@ -370,6 +370,15 @@ static void test_unwrap_merge_seqno(struct kunit *test)
 	dma_fence_put(f1);
 }
 
+static void test_dedup_empty(struct kunit *test)
+{
+	struct dma_fence *fence[1] = { NULL };
+	int ret;
+
+	ret = dma_fence_dedup_array(fence, 0);
+	KUNIT_EXPECT_EQ(test, ret, 0);
+}
+
 static void test_unwrap_merge_order(struct kunit *test)
 {
 	struct dma_fence *fence, *f1, *f2, *a1, *a2, *c1, *c2;
@@ -595,6 +604,7 @@ static struct kunit_case dma_fence_unwrap_cases[] = {
 	KUNIT_CASE(test_unwrap_merge_order),
 	KUNIT_CASE(test_unwrap_merge_complex),
 	KUNIT_CASE(test_unwrap_merge_complex_seqno),
+	KUNIT_CASE(test_dedup_empty),
 	{}
 };
 
author
#3 lore raw

Thanks Christian! size_t is a nice fit. It removes the negative-input concern Philipp raised in one shot. Looking forward to seeing the cleanup land.

Regards,
Baineng

Quoted 55 lines · Christian König <[email protected]> 于2026年6月30日周二 16:29写道:
Quoted 8 lines · Baineng Shou

Add a KUnit case verifying that dma_fence_dedup_array() returns 0, not 1, when called with num_fences == 0. This locks down the contract documented in the kernel-doc and prevents a regression of commit 77a9298741f8 ("dma-fence: Make dma_fence_dedup_array() robust against 0-count input").

Going to push this to drm-misc-next later today. I also pinged our intern to write a cleanup patch whic consistently use size_t for the number of array elements in the code.

Thanks,
Christian.

Review

Quoted 5 lines
---
 drivers/dma-buf/st-dma-fence-unwrap.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
(quoted diff) +0 -0

b/drivers/dma-buf/st-dma-fence-unwrap.c

drivers/dma-buf/st-dma-fence-unwrap.c static void test_unwrap_merge_seqno(struct kunit

Quoted 4 lines

index 4e7ee25372ba..72dea6091d58 100644

drivers/dma-buf/st-dma-fence-unwrap.c +0 -0
@@ -370,6 +370,15 @@ static void test_unwrap_merge_seqno(struct kunit

*test)

Quoted 23 lines
drivers/dma-buf/st-dma-fence-unwrap.c +10 -0
      dma_fence_put(f1);
 }
 
+static void test_dedup_empty(struct kunit *test)
+{
+     struct dma_fence *fence[1] = { NULL };
+     int ret;
+
+     ret = dma_fence_dedup_array(fence, 0);
+     KUNIT_EXPECT_EQ(test, ret, 0);
+}
+
 static void test_unwrap_merge_order(struct kunit *test)
 {
      struct dma_fence *fence, *f1, *f2, *a1, *a2, *c1, *c2;
@@ -595,6 +604,7 @@ static struct kunit_case dma_fence_unwrap_cases[] = {
      KUNIT_CASE(test_unwrap_merge_order),
      KUNIT_CASE(test_unwrap_merge_complex),
      KUNIT_CASE(test_unwrap_merge_complex_seqno),
+     KUNIT_CASE(test_dedup_empty),
      {}
 };
 
#4 lore raw

PATCH v2 1/1 dma-buf/test: add KUnit case for dma_fence_dedup_array() empty input

In reply to Baineng Shou

Add a KUnit case verifying that dma_fence_dedup_array() returns 0, not 1, when called with num_fences == 0. This locks down the contract documented in the kernel-doc and prevents a regression of commit 77a9298741f8 ("dma-fence: Make dma_fence_dedup_array() robust against 0-count input").

---

Changes in v2:
- Add Reviewed-by from Christian.
- Rebase onto v7.3-rc7.

 drivers/dma-buf/st-dma-fence-unwrap.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
drivers/dma-buf/st-dma-fence-unwrap.c +10 -0
index 4d9d313b460c..c5353b36f0e4 100644
@@ -370,6 +370,15 @@ static void test_unwrap_merge_seqno(struct kunit *test)
 	dma_fence_put(f1);
 }
 
+static void test_dedup_empty(struct kunit *test)
+{
+	struct dma_fence *fence[1] = { NULL };
+	int ret;
+
+	ret = dma_fence_dedup_array(fence, 0);
+	KUNIT_EXPECT_EQ(test, ret, 0);
+}
+
 static void test_unwrap_merge_order(struct kunit *test)
 {
 	struct dma_fence *fence, *f1, *f2, *a1, *a2, *c1, *c2;
@@ -595,6 +604,7 @@ static struct kunit_case dma_fence_unwrap_cases[] = {
 	KUNIT_CASE(test_unwrap_merge_order),
 	KUNIT_CASE(test_unwrap_merge_complex),
 	KUNIT_CASE(test_unwrap_merge_complex_seqno),
+	KUNIT_CASE(test_dedup_empty),
 	{}
 };
 
Signature · 1 line
2.34.1
maintainer
#5 lore raw
Quoted 9 lines · Baineng Shou

Add a KUnit case verifying that dma_fence_dedup_array() returns 0, not 1, when called with num_fences == 0. This locks down the contract documented in the kernel-doc and prevents a regression of commit 77a9298741f8 ("dma-fence: Make dma_fence_dedup_array() robust against 0-count input").

Thanks for the reminder, I had to wait for the -fixes backmerge to push this.

I've just pushed the patch to drm-misc-next.

Regards,
Christian.

Quoted 37 lines

---

Changes in v2:
- Add Reviewed-by from Christian.
- Rebase onto v7.3-rc7.

 drivers/dma-buf/st-dma-fence-unwrap.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
drivers/dma-buf/st-dma-fence-unwrap.c +10 -0
index 4d9d313b460c..c5353b36f0e4 100644
@@ -370,6 +370,15 @@ static void test_unwrap_merge_seqno(struct kunit *test)
 	dma_fence_put(f1);
 }
 
+static void test_dedup_empty(struct kunit *test)
+{
+	struct dma_fence *fence[1] = { NULL };
+	int ret;
+
+	ret = dma_fence_dedup_array(fence, 0);
+	KUNIT_EXPECT_EQ(test, ret, 0);
+}
+
 static void test_unwrap_merge_order(struct kunit *test)
 {
 	struct dma_fence *fence, *f1, *f2, *a1, *a2, *c1, *c2;
@@ -595,6 +604,7 @@ static struct kunit_case dma_fence_unwrap_cases[] = {
 	KUNIT_CASE(test_unwrap_merge_order),
 	KUNIT_CASE(test_unwrap_merge_complex),
 	KUNIT_CASE(test_unwrap_merge_complex_seqno),
+	KUNIT_CASE(test_dedup_empty),
 	{}
 };