Commit dc0e3aa5 authored by Sanjay Yadav's avatar Sanjay Yadav Committed by Arunpravin Paneer Selvam
Browse files

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: default avatarMatthew Auld <matthew.auld@intel.com>
Signed-off-by: default avatarSanjay Yadav <sanjay.kumar.yadav@intel.com>
Reviewed-by: default avatarMatthew Auld <matthew.auld@intel.com>
Signed-off-by: default avatarArunpravin Paneer Selvam <Arunpravin.PaneerSelvam@amd.com>
Link: https://patch.msgid.link/20260108113227.2101872-6-sanjay.kumar.yadav@intel.com
parent 5488a295
Loading
Loading
Loading
Loading
+35 −0
Original line number Diff line number Diff line
@@ -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),
	{}
};