[PATCH] drm/tests/gpu_buddy: fix interleaving in buffer clearance test

Arunpravin Paneer Selvam <[email protected]> Wed, 22 Jul 2026 11:47:18 +0530
Newsgroups org.freedesktop.lists.intel-gfx,org.freedesktop.lists.amd-gfx,org.freedesktop.lists.dri-devel,org.freedesktop.lists.intel-xe
Message-ID <[email protected]>
The resume clearance test skipped every other allocation, expecting an
interleaved clear/dirty layout. But the buddy allocator hands out blocks
contiguously, so this just allocated half the pages in one chunk and never
exercised gpu_buddy_reset_clear()'s force-merge of opposite-state buddies.
Allocate all pages into two lists instead and free one cleared, one dirty,
to build a truly interleaved pattern.

Fixes: e3335ccbf4da ("drm/tests/gpu_buddy: add a new test case for buffer clearance during resume")
Closes: https://sashiko.dev/#/patchset/[email protected]?part=1
Cc: Matthew Auld <[email protected]>
Cc: Christian König <[email protected]>
Signed-off-by: Arunpravin Paneer Selvam <[email protected]>
---
 drivers/gpu/tests/gpu_buddy_test.c | 47 ++++++++++++++++++------------
 1 file changed, 29 insertions(+), 18 deletions(-)

diff --git a/drivers/gpu/tests/gpu_buddy_test.c b/drivers/gpu/tests/gpu_buddy_test.c
index 89698563c61b..b793aa441424 100644
--- a/drivers/gpu/tests/gpu_buddy_test.c
+++ b/drivers/gpu/tests/gpu_buddy_test.c
@@ -1004,10 +1004,13 @@ static void gpu_test_buddy_alloc_clear(struct kunit *test)
 	gpu_buddy_fini(&mm);
 
 	/*
-	 * Using a non-power-of-two mm size, allocate alternating blocks of 4KiB in an
-	 * even sequence and free them as cleared. All blocks should be marked as
-	 * dirty and the split blocks should be merged back to their original
-	 * size when the blocks clear reset function is called.
+	 * Using a non-power-of-two mm size, allocate all 4KiB blocks and split
+	 * them across two alternating lists, then free one list as cleared and
+	 * the other as dirty. This interleaves cleared and dirty blocks so that
+	 * neighbouring buddies cannot be merged, fragmenting the address space.
+	 * After gpu_buddy_reset_clear(false) every block should be marked dirty
+	 * and the split blocks should be merged back to their original size, so
+	 * clear_avail must drop to 0.
 	 */
 	KUNIT_EXPECT_FALSE(test, gpu_buddy_init(&mm, mm_size, ps));
 	KUNIT_EXPECT_EQ(test, mm.max_order, max_order);
@@ -1015,31 +1018,39 @@ static void gpu_test_buddy_alloc_clear(struct kunit *test)
 	i = 0;
 	n_pages = mm_size / ps;
 	do {
-		if (i % 2 == 0)
-			KUNIT_ASSERT_FALSE_MSG(test, gpu_buddy_alloc_blocks(&mm, 0, mm_size,
-									    ps, ps, &allocated, 0),
-					"buddy_alloc hit an error size=%lu\n", ps);
+		struct list_head *list = (i % 2) ? &clean : &dirty;
+
+		KUNIT_ASSERT_FALSE_MSG(test, gpu_buddy_alloc_blocks(&mm, 0, mm_size,
+								    ps, ps, list, 0),
+				"buddy_alloc hit an error size=%lu\n", ps);
 	} while (++i < n_pages);
 
-	gpu_buddy_free_list(&mm, &allocated, GPU_BUDDY_CLEARED);
+	gpu_buddy_free_list(&mm, &clean, GPU_BUDDY_CLEARED);
+	gpu_buddy_free_list(&mm, &dirty, 0);
 	gpu_buddy_reset_clear(&mm, false);
 	KUNIT_EXPECT_EQ(test, mm.clear_avail, 0);
+	gpu_buddy_fini(&mm);
 
 	/*
-	 * Using a non-power-of-two mm size, allocate alternating blocks of 4KiB in an
-	 * odd sequence and free them as cleared. All blocks should be marked as
-	 * cleared and the split blocks should be merged back to their original
-	 * size when the blocks clear reset function is called.
+	 * Repeat the same fragmented setup, but this time call
+	 * gpu_buddy_reset_clear(true). Every block should be marked cleared and
+	 * the split blocks should be merged back to their original size, so the
+	 * whole address space (clear_avail) must equal mm_size.
 	 */
+	KUNIT_EXPECT_FALSE(test, gpu_buddy_init(&mm, mm_size, ps));
+	KUNIT_EXPECT_EQ(test, mm.max_order, max_order);
+
 	i = 0;
 	do {
-		if (i % 2 != 0)
-			KUNIT_ASSERT_FALSE_MSG(test, gpu_buddy_alloc_blocks(&mm, 0, mm_size,
-									    ps, ps, &allocated, 0),
-					"buddy_alloc hit an error size=%lu\n", ps);
+		struct list_head *list = (i % 2) ? &clean : &dirty;
+
+		KUNIT_ASSERT_FALSE_MSG(test, gpu_buddy_alloc_blocks(&mm, 0, mm_size,
+								    ps, ps, list, 0),
+				"buddy_alloc hit an error size=%lu\n", ps);
 	} while (++i < n_pages);
 
-	gpu_buddy_free_list(&mm, &allocated, GPU_BUDDY_CLEARED);
+	gpu_buddy_free_list(&mm, &clean, GPU_BUDDY_CLEARED);
+	gpu_buddy_free_list(&mm, &dirty, 0);
 	gpu_buddy_reset_clear(&mm, true);
 	KUNIT_EXPECT_EQ(test, mm.clear_avail, mm_size);
 	gpu_buddy_fini(&mm);
-- 
2.34.1