Add KUnit test to verify DRM suballocator allocation and insertion over
multiple iterations with randomized sizes. Tests both successful
allocations and allocation-free cycles to exercise fragmentation handling.

Signed-off-by: Satyanarayana K V P <[email protected]>
Cc: Matthew Brost <[email protected]>
Cc: Thomas Hellström <[email protected]>
Cc: Maarten Lankhorst <[email protected]>
Cc: Michal Wajdeczko <[email protected]>
Cc: Christian König <[email protected]>
Cc: [email protected]
---
 drivers/gpu/drm/Kconfig.debug             |  1 +
 drivers/gpu/drm/tests/.kunitconfig        |  1 +
 drivers/gpu/drm/tests/Makefile            |  1 +
 drivers/gpu/drm/tests/drm_suballoc_test.c | 79 +++++++++++++++++++++++
 4 files changed, 82 insertions(+)
 create mode 100644 drivers/gpu/drm/tests/drm_suballoc_test.c

diff --git a/drivers/gpu/drm/Kconfig.debug b/drivers/gpu/drm/Kconfig.debug
index 3b7886865335..ea18d9399d5c 100644
--- a/drivers/gpu/drm/Kconfig.debug
+++ b/drivers/gpu/drm/Kconfig.debug
@@ -69,6 +69,7 @@ config DRM_KUNIT_TEST
        select DRM_EXPORT_FOR_TESTS if m
        select DRM_GEM_SHMEM_HELPER
        select DRM_KUNIT_TEST_HELPERS
+       select DRM_SUBALLOC_HELPER
        select DRM_SYSFB_HELPER
        select PRIME_NUMBERS
        default KUNIT_ALL_TESTS
diff --git a/drivers/gpu/drm/tests/.kunitconfig 
b/drivers/gpu/drm/tests/.kunitconfig
index 5be8e71f45d5..67758bb211d2 100644
--- a/drivers/gpu/drm/tests/.kunitconfig
+++ b/drivers/gpu/drm/tests/.kunitconfig
@@ -3,3 +3,4 @@ CONFIG_DRM=y
 CONFIG_DRM_VKMS=y
 CONFIG_DRM_FBDEV_EMULATION=y
 CONFIG_DRM_KUNIT_TEST=y
+CONFIG_DRM_SUBALLOC_HELPER=y
diff --git a/drivers/gpu/drm/tests/Makefile b/drivers/gpu/drm/tests/Makefile
index d2e2e3d8349a..7f8aa03c5a6d 100644
--- a/drivers/gpu/drm/tests/Makefile
+++ b/drivers/gpu/drm/tests/Makefile
@@ -23,6 +23,7 @@ obj-$(CONFIG_DRM_KUNIT_TEST) += \
        drm_plane_helper_test.o \
        drm_probe_helper_test.o \
        drm_rect_test.o \
+       drm_suballoc_test.o \
        drm_sysfb_modeset_test.o \
        drm_fixp_test.o
 
diff --git a/drivers/gpu/drm/tests/drm_suballoc_test.c 
b/drivers/gpu/drm/tests/drm_suballoc_test.c
new file mode 100644
index 000000000000..f03327e1c8af
--- /dev/null
+++ b/drivers/gpu/drm/tests/drm_suballoc_test.c
@@ -0,0 +1,79 @@
+// SPDX-License-Identifier: GPL-2.0 AND MIT
+/*
+ * Copyright © 2026 Intel Corporation
+ */
+
+#include <drm/drm_suballoc.h>
+
+#include <kunit/test.h>
+#include <linux/errno.h>
+#include <linux/module.h>
+#include <linux/sizes.h>
+
+#define DRM_SUBALLOC_TEST_ITERATIONS        128
+#define DRM_SUBALLOC_TEST_MANAGER_ALIGN     SZ_16
+#define DRM_SUBALLOC_TEST_MANAGER_SIZE      SZ_16K
+#define DRM_SUBALLOC_TEST_MAX_ALLOCS        16
+#define DRM_SUBALLOC_TEST_MAX_ALLOC_SIZE    SZ_512
+
+static bool fence_disable = true;
+module_param_named(fence_disable, fence_disable, bool, 0644);
+MODULE_PARM_DESC(fence_disable, "Disable suballoc fence tracking in test");
+
+static void drm_test_suballoc_alloc_insert(struct kunit *test)
+{
+       struct drm_suballoc_manager manager;
+       struct drm_suballoc *sa_arr[DRM_SUBALLOC_TEST_MAX_ALLOCS], *sa;
+       struct drm_suballoc
+               *sa_alloc_arr[DRM_SUBALLOC_TEST_ITERATIONS / 
DRM_SUBALLOC_TEST_MAX_ALLOCS] = {0};
+       int i, size, sa_index, sa_alloc_arr_index;
+
+       drm_suballoc_manager_init(&manager,
+                                 DRM_SUBALLOC_TEST_MANAGER_SIZE,
+                                 DRM_SUBALLOC_TEST_MANAGER_ALIGN);
+       drm_suballoc_manager_fence_disable(&manager, fence_disable);
+
+       kunit_info(test, "Starting suballoc test with %d iterations with fence 
%s\n",
+                  DRM_SUBALLOC_TEST_ITERATIONS, fence_disable ? "disabled" : 
"enabled");
+
+       for (i = 0, sa_index = 0, sa_alloc_arr_index = 0;
+            i < DRM_SUBALLOC_TEST_ITERATIONS; i++) {
+               size = get_random_u32_below(DRM_SUBALLOC_TEST_MAX_ALLOC_SIZE) + 
1;
+               size = ALIGN(size, 16);
+
+               sa = drm_suballoc_new(&manager, size, GFP_KERNEL, true, 0);
+               KUNIT_ASSERT_NOT_ERR_OR_NULL(test, sa);
+               KUNIT_ASSERT_EQ(test, drm_suballoc_size(sa), size);
+               sa_arr[sa_index++] = sa;
+
+               if (sa_index == DRM_SUBALLOC_TEST_MAX_ALLOCS) {
+                       for (int free_iter = 0; free_iter < 
DRM_SUBALLOC_TEST_MAX_ALLOCS - 1;
+                            free_iter++) {
+                               drm_suballoc_free(sa_arr[free_iter], NULL);
+                       }
+                       sa_alloc_arr[sa_alloc_arr_index++] =
+                               sa_arr[DRM_SUBALLOC_TEST_MAX_ALLOCS - 1];
+                       sa_index = 0;
+               }
+       }
+
+       for (i = 0; i < sa_alloc_arr_index; i++)
+               drm_suballoc_free(sa_alloc_arr[i], NULL);
+
+       drm_suballoc_manager_fini(&manager);
+}
+
+static struct kunit_case drm_suballoc_tests[] = {
+       KUNIT_CASE(drm_test_suballoc_alloc_insert),
+       {}
+};
+
+static struct kunit_suite drm_suballoc_test = {
+       .name = "drm_suballoc",
+       .test_cases = drm_suballoc_tests,
+};
+
+kunit_test_suite(drm_suballoc_test);
+
+MODULE_DESCRIPTION("KUnit DRM suballoc test suite");
+MODULE_LICENSE("GPL");
-- 
2.43.0

Reply via email to