Skip to content

Commit dc0e3aa

Browse files
sanjayumangarunpravin24
authored andcommitted
drm/tests/drm_buddy: Add tests for allocations exceeding max_order
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 <christian.koenig@amd.com> Cc: Arunpravin Paneer Selvam <Arunpravin.PaneerSelvam@amd.com> Suggested-by: Matthew Auld <matthew.auld@intel.com> Signed-off-by: Sanjay Yadav <sanjay.kumar.yadav@intel.com> Reviewed-by: Matthew Auld <matthew.auld@intel.com> Signed-off-by: Arunpravin Paneer Selvam <Arunpravin.PaneerSelvam@amd.com> Link: https://patch.msgid.link/20260108113227.2101872-6-sanjay.kumar.yadav@intel.com
1 parent 5488a29 commit dc0e3aa

1 file changed

Lines changed: 35 additions & 0 deletions

File tree

drivers/gpu/drm/tests/drm_buddy_test.c

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -857,6 +857,40 @@ static void drm_test_buddy_alloc_limit(struct kunit *test)
857857
drm_buddy_fini(&mm);
858858
}
859859

860+
static void drm_test_buddy_alloc_exceeds_max_order(struct kunit *test)
861+
{
862+
u64 mm_size = SZ_8G + SZ_2G, size = SZ_8G + SZ_1G, min_block_size = SZ_8G;
863+
struct drm_buddy mm;
864+
LIST_HEAD(blocks);
865+
int err;
866+
867+
KUNIT_ASSERT_FALSE_MSG(test, drm_buddy_init(&mm, mm_size, SZ_4K),
868+
"buddy_init failed\n");
869+
870+
/* CONTIGUOUS allocation should succeed via try_harder fallback */
871+
KUNIT_ASSERT_FALSE_MSG(test, drm_buddy_alloc_blocks(&mm, 0, mm_size, size,
872+
SZ_4K, &blocks,
873+
DRM_BUDDY_CONTIGUOUS_ALLOCATION),
874+
"buddy_alloc hit an error size=%llu\n", size);
875+
drm_buddy_free_list(&mm, &blocks, 0);
876+
877+
/* Non-CONTIGUOUS with large min_block_size should return -EINVAL */
878+
err = drm_buddy_alloc_blocks(&mm, 0, mm_size, size, min_block_size, &blocks, 0);
879+
KUNIT_EXPECT_EQ(test, err, -EINVAL);
880+
881+
/* Non-CONTIGUOUS + RANGE with large min_block_size should return -EINVAL */
882+
err = drm_buddy_alloc_blocks(&mm, 0, mm_size, size, min_block_size, &blocks,
883+
DRM_BUDDY_RANGE_ALLOCATION);
884+
KUNIT_EXPECT_EQ(test, err, -EINVAL);
885+
886+
/* CONTIGUOUS + RANGE should return -EINVAL (no try_harder for RANGE) */
887+
err = drm_buddy_alloc_blocks(&mm, 0, mm_size, size, SZ_4K, &blocks,
888+
DRM_BUDDY_CONTIGUOUS_ALLOCATION | DRM_BUDDY_RANGE_ALLOCATION);
889+
KUNIT_EXPECT_EQ(test, err, -EINVAL);
890+
891+
drm_buddy_fini(&mm);
892+
}
893+
860894
static int drm_buddy_suite_init(struct kunit_suite *suite)
861895
{
862896
while (!random_seed)
@@ -877,6 +911,7 @@ static struct kunit_case drm_buddy_tests[] = {
877911
KUNIT_CASE(drm_test_buddy_alloc_clear),
878912
KUNIT_CASE(drm_test_buddy_alloc_range_bias),
879913
KUNIT_CASE(drm_test_buddy_fragmentation_performance),
914+
KUNIT_CASE(drm_test_buddy_alloc_exceeds_max_order),
880915
{}
881916
};
882917

0 commit comments

Comments
 (0)