Skip to content

Commit 972a254

Browse files
committed
Merge tag 'drm-next-2024-05-16' of https://gitlab.freedesktop.org/drm/kernel
Pull drm fix from Dave Airlie: - fix breakage in buddy allocator * tag 'drm-next-2024-05-16' of https://gitlab.freedesktop.org/drm/kernel: drm/tests: Add a unit test for range bias allocation drm/buddy: Fix the range bias clear memory allocation issue
2 parents 3c999d1 + 431c590 commit 972a254

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

drivers/gpu/drm/drm_buddy.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,7 @@ int drm_buddy_init(struct drm_buddy *mm, u64 size, u64 chunk_size)
249249

250250
mm->size = size;
251251
mm->avail = size;
252+
mm->clear_avail = 0;
252253
mm->chunk_size = chunk_size;
253254
mm->max_order = ilog2(size) - ilog2(chunk_size);
254255

@@ -574,7 +575,7 @@ __drm_buddy_alloc_range_bias(struct drm_buddy *mm,
574575

575576
block = __alloc_range_bias(mm, start, end, order,
576577
flags, fallback);
577-
if (IS_ERR(block) && mm->clear_avail)
578+
if (IS_ERR(block))
578579
return __alloc_range_bias(mm, start, end, order,
579580
flags, !fallback);
580581

drivers/gpu/drm/tests/drm_buddy_test.c

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,11 @@ static inline u64 get_size(int order, u64 chunk_size)
2323

2424
static void drm_test_buddy_alloc_range_bias(struct kunit *test)
2525
{
26-
u32 mm_size, ps, bias_size, bias_start, bias_end, bias_rem;
26+
u32 mm_size, size, ps, bias_size, bias_start, bias_end, bias_rem;
2727
DRM_RND_STATE(prng, random_seed);
2828
unsigned int i, count, *order;
29+
struct drm_buddy_block *block;
30+
unsigned long flags;
2931
struct drm_buddy mm;
3032
LIST_HEAD(allocated);
3133

@@ -222,6 +224,38 @@ static void drm_test_buddy_alloc_range_bias(struct kunit *test)
222224

223225
drm_buddy_free_list(&mm, &allocated, 0);
224226
drm_buddy_fini(&mm);
227+
228+
/*
229+
* Allocate cleared blocks in the bias range when the DRM buddy's clear avail is
230+
* zero. This will validate the bias range allocation in scenarios like system boot
231+
* when no cleared blocks are available and exercise the fallback path too. The resulting
232+
* blocks should always be dirty.
233+
*/
234+
235+
KUNIT_ASSERT_FALSE_MSG(test, drm_buddy_init(&mm, mm_size, ps),
236+
"buddy_init failed\n");
237+
238+
bias_start = round_up(prandom_u32_state(&prng) % (mm_size - ps), ps);
239+
bias_end = round_up(bias_start + prandom_u32_state(&prng) % (mm_size - bias_start), ps);
240+
bias_end = max(bias_end, bias_start + ps);
241+
bias_rem = bias_end - bias_start;
242+
243+
flags = DRM_BUDDY_CLEAR_ALLOCATION | DRM_BUDDY_RANGE_ALLOCATION;
244+
size = max(round_up(prandom_u32_state(&prng) % bias_rem, ps), ps);
245+
246+
KUNIT_ASSERT_FALSE_MSG(test,
247+
drm_buddy_alloc_blocks(&mm, bias_start,
248+
bias_end, size, ps,
249+
&allocated,
250+
flags),
251+
"buddy_alloc failed with bias(%x-%x), size=%u, ps=%u\n",
252+
bias_start, bias_end, size, ps);
253+
254+
list_for_each_entry(block, &allocated, link)
255+
KUNIT_EXPECT_EQ(test, drm_buddy_block_is_clear(block), false);
256+
257+
drm_buddy_free_list(&mm, &allocated, 0);
258+
drm_buddy_fini(&mm);
225259
}
226260

227261
static void drm_test_buddy_alloc_clear(struct kunit *test)

0 commit comments

Comments
 (0)