Add kunit tests that exercise edge cases where allocation requests
exceed mm->max_order after rounding. This can happen with
non-power-of-two VRAM sizes when the allocator rounds up requests.

For example, with 10G VRAM (8G + 2G roots), mm->max_order represents
the 8G block. A 9G allocation can round up to 16G in multiple ways:
CONTIGUOUS allocation rounds to next power-of-two, or non-CONTIGUOUS
with 8G min_block_size rounds to next alignment boundary.

The test validates CONTIGUOUS and RANGE flag combinations, ensuring that
only CONTIGUOUS-alone allocations use try_harder fallback, while other
combinations return -EINVAL when rounded size exceeds memory, preventing
BUG_ON assertions.

Cc: Christian König <[email protected]>
Cc: Arunpravin Paneer Selvam <[email protected]>
Suggested-by: Matthew Auld <[email protected]>
Signed-off-by: Sanjay Yadav <[email protected]>
---
 drivers/gpu/drm/tests/drm_buddy_test.c | 35 ++++++++++++++++++++++++++
 1 file changed, 35 insertions(+)

diff --git a/drivers/gpu/drm/tests/drm_buddy_test.c 
b/drivers/gpu/drm/tests/drm_buddy_test.c
index 5f40b5343bd8..e6f8459c6c54 100644
--- a/drivers/gpu/drm/tests/drm_buddy_test.c
+++ b/drivers/gpu/drm/tests/drm_buddy_test.c
@@ -857,6 +857,40 @@ static void drm_test_buddy_alloc_limit(struct kunit *test)
        drm_buddy_fini(&mm);
 }
 
+static void drm_test_buddy_alloc_exceeds_max_order(struct kunit *test)
+{
+       u64 mm_size = SZ_8G + SZ_2G, size = SZ_8G + SZ_1G, min_block_size = 
SZ_8G;
+       struct drm_buddy mm;
+       LIST_HEAD(blocks);
+       int err;
+
+       KUNIT_ASSERT_FALSE_MSG(test, drm_buddy_init(&mm, mm_size, SZ_4K),
+                              "buddy_init failed\n");
+
+       /* CONTIGUOUS allocation should succeed via try_harder fallback */
+       KUNIT_ASSERT_FALSE_MSG(test, drm_buddy_alloc_blocks(&mm, 0, mm_size, 
size,
+                                                           SZ_4K, &blocks,
+                                                           
DRM_BUDDY_CONTIGUOUS_ALLOCATION),
+                              "buddy_alloc hit an error size=%llu\n", size);
+       drm_buddy_free_list(&mm, &blocks, 0);
+
+       /* Non-CONTIGUOUS with large min_block_size should return -EINVAL */
+       err = drm_buddy_alloc_blocks(&mm, 0, mm_size, size, min_block_size, 
&blocks, 0);
+       KUNIT_EXPECT_EQ(test, err, -EINVAL);
+
+       /* Non-CONTIGUOUS + RANGE with large min_block_size should return 
-EINVAL */
+       err = drm_buddy_alloc_blocks(&mm, 0, mm_size, size, min_block_size, 
&blocks,
+                                    DRM_BUDDY_RANGE_ALLOCATION);
+       KUNIT_EXPECT_EQ(test, err, -EINVAL);
+
+       /* CONTIGUOUS + RANGE should return -EINVAL (no try_harder for RANGE) */
+       err = drm_buddy_alloc_blocks(&mm, 0, mm_size, size, SZ_4K, &blocks,
+                                    DRM_BUDDY_CONTIGUOUS_ALLOCATION | 
DRM_BUDDY_RANGE_ALLOCATION);
+       KUNIT_EXPECT_EQ(test, err, -EINVAL);
+
+       drm_buddy_fini(&mm);
+}
+
 static int drm_buddy_suite_init(struct kunit_suite *suite)
 {
        while (!random_seed)
@@ -877,6 +911,7 @@ static struct kunit_case drm_buddy_tests[] = {
        KUNIT_CASE(drm_test_buddy_alloc_clear),
        KUNIT_CASE(drm_test_buddy_alloc_range_bias),
        KUNIT_CASE(drm_test_buddy_fragmentation_performance),
+       KUNIT_CASE(drm_test_buddy_alloc_exceeds_max_order),
        {}
 };
 
-- 
2.52.0

Reply via email to