Skip to content

Commit a64056b

Browse files
matt-auldChristianKoenigAMD
authored andcommitted
drm/tests/drm_buddy: add alloc_contiguous test
Sanity check DRM_BUDDY_CONTIGUOUS_ALLOCATION. v2: Fix checkpatch warnings. Signed-off-by: Matthew Auld <matthew.auld@intel.com> Reviewed-by: Arunpravin Paneer Selvam <Arunpravin.PaneerSelvam@amd.com> Signed-off-by: Arunpravin Paneer Selvam <Arunpravin.PaneerSelvam@amd.com> Link: https://patchwork.freedesktop.org/patch/msgid/20240214131853.5934-2-Arunpravin.PaneerSelvam@amd.com Signed-off-by: Christian König <christian.koenig@amd.com>
1 parent 8746c6c commit a64056b

File tree

1 file changed

+89
-0
lines changed

1 file changed

+89
-0
lines changed

drivers/gpu/drm/tests/drm_buddy_test.c

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include <linux/prime_numbers.h>
1010
#include <linux/sched/signal.h>
11+
#include <linux/sizes.h>
1112

1213
#include <drm/drm_buddy.h>
1314

@@ -18,6 +19,93 @@ static inline u64 get_size(int order, u64 chunk_size)
1819
return (1 << order) * chunk_size;
1920
}
2021

22+
static void drm_test_buddy_alloc_contiguous(struct kunit *test)
23+
{
24+
u64 mm_size, ps = SZ_4K, i, n_pages, total;
25+
struct drm_buddy_block *block;
26+
struct drm_buddy mm;
27+
LIST_HEAD(left);
28+
LIST_HEAD(middle);
29+
LIST_HEAD(right);
30+
LIST_HEAD(allocated);
31+
32+
mm_size = 16 * 3 * SZ_4K;
33+
34+
KUNIT_EXPECT_FALSE(test, drm_buddy_init(&mm, mm_size, ps));
35+
36+
/*
37+
* Idea is to fragment the address space by alternating block
38+
* allocations between three different lists; one for left, middle and
39+
* right. We can then free a list to simulate fragmentation. In
40+
* particular we want to exercise the DRM_BUDDY_CONTIGUOUS_ALLOCATION,
41+
* including the try_harder path.
42+
*/
43+
44+
i = 0;
45+
n_pages = mm_size / ps;
46+
do {
47+
struct list_head *list;
48+
int slot = i % 3;
49+
50+
if (slot == 0)
51+
list = &left;
52+
else if (slot == 1)
53+
list = &middle;
54+
else
55+
list = &right;
56+
KUNIT_ASSERT_FALSE_MSG(test,
57+
drm_buddy_alloc_blocks(&mm, 0, mm_size,
58+
ps, ps, list, 0),
59+
"buddy_alloc hit an error size=%d\n",
60+
ps);
61+
} while (++i < n_pages);
62+
63+
KUNIT_ASSERT_TRUE_MSG(test, drm_buddy_alloc_blocks(&mm, 0, mm_size,
64+
3 * ps, ps, &allocated,
65+
DRM_BUDDY_CONTIGUOUS_ALLOCATION),
66+
"buddy_alloc didn't error size=%d\n", 3 * ps);
67+
68+
drm_buddy_free_list(&mm, &middle);
69+
KUNIT_ASSERT_TRUE_MSG(test, drm_buddy_alloc_blocks(&mm, 0, mm_size,
70+
3 * ps, ps, &allocated,
71+
DRM_BUDDY_CONTIGUOUS_ALLOCATION),
72+
"buddy_alloc didn't error size=%llu\n", 3 * ps);
73+
KUNIT_ASSERT_TRUE_MSG(test, drm_buddy_alloc_blocks(&mm, 0, mm_size,
74+
2 * ps, ps, &allocated,
75+
DRM_BUDDY_CONTIGUOUS_ALLOCATION),
76+
"buddy_alloc didn't error size=%llu\n", 2 * ps);
77+
78+
drm_buddy_free_list(&mm, &right);
79+
KUNIT_ASSERT_TRUE_MSG(test, drm_buddy_alloc_blocks(&mm, 0, mm_size,
80+
3 * ps, ps, &allocated,
81+
DRM_BUDDY_CONTIGUOUS_ALLOCATION),
82+
"buddy_alloc didn't error size=%llu\n", 3 * ps);
83+
/*
84+
* At this point we should have enough contiguous space for 2 blocks,
85+
* however they are never buddies (since we freed middle and right) so
86+
* will require the try_harder logic to find them.
87+
*/
88+
KUNIT_ASSERT_FALSE_MSG(test, drm_buddy_alloc_blocks(&mm, 0, mm_size,
89+
2 * ps, ps, &allocated,
90+
DRM_BUDDY_CONTIGUOUS_ALLOCATION),
91+
"buddy_alloc hit an error size=%d\n", 2 * ps);
92+
93+
drm_buddy_free_list(&mm, &left);
94+
KUNIT_ASSERT_FALSE_MSG(test, drm_buddy_alloc_blocks(&mm, 0, mm_size,
95+
3 * ps, ps, &allocated,
96+
DRM_BUDDY_CONTIGUOUS_ALLOCATION),
97+
"buddy_alloc hit an error size=%d\n", 3 * ps);
98+
99+
total = 0;
100+
list_for_each_entry(block, &allocated, link)
101+
total += drm_buddy_block_size(&mm, block);
102+
103+
KUNIT_ASSERT_EQ(test, total, ps * 2 + ps * 3);
104+
105+
drm_buddy_free_list(&mm, &allocated);
106+
drm_buddy_fini(&mm);
107+
}
108+
21109
static void drm_test_buddy_alloc_pathological(struct kunit *test)
22110
{
23111
u64 mm_size, size, start = 0;
@@ -280,6 +368,7 @@ static struct kunit_case drm_buddy_tests[] = {
280368
KUNIT_CASE(drm_test_buddy_alloc_optimistic),
281369
KUNIT_CASE(drm_test_buddy_alloc_pessimistic),
282370
KUNIT_CASE(drm_test_buddy_alloc_pathological),
371+
KUNIT_CASE(drm_test_buddy_alloc_contiguous),
283372
{}
284373
};
285374

0 commit comments

Comments
 (0)