1 // SPDX-License-Identifier: MIT
3 * Copyright © 2019 Intel Corporation
7 #include <kunit/test.h>
9 #include <linux/prime_numbers.h>
10 #include <linux/sched/signal.h>
11 #include <linux/sizes.h>
13 #include <drm/drm_buddy.h>
15 #include "../lib/drm_random.h"
17 static unsigned int random_seed;
19 static inline u64 get_size(int order, u64 chunk_size)
21 return (1 << order) * chunk_size;
24 static void drm_test_buddy_alloc_range_bias(struct kunit *test)
26 u32 mm_size, ps, bias_size, bias_start, bias_end, bias_rem;
27 DRM_RND_STATE(prng, random_seed);
28 unsigned int i, count, *order;
33 ps = roundup_pow_of_two(prandom_u32_state(&prng) % bias_size);
35 mm_size = (SZ_8M-1) & ~(ps-1); /* Multiple roots */
37 kunit_info(test, "mm_size=%u, ps=%u\n", mm_size, ps);
39 KUNIT_ASSERT_FALSE_MSG(test, drm_buddy_init(&mm, mm_size, ps),
40 "buddy_init failed\n");
42 count = mm_size / bias_size;
43 order = drm_random_order(count, &prng);
44 KUNIT_EXPECT_TRUE(test, order);
47 * Idea is to split the address space into uniform bias ranges, and then
48 * in some random order allocate within each bias, using various
49 * patterns within. This should detect if allocations leak out from a
50 * given bias, for example.
53 for (i = 0; i < count; i++) {
57 bias_start = order[i] * bias_size;
58 bias_end = bias_start + bias_size;
61 /* internal round_up too big */
62 KUNIT_ASSERT_TRUE_MSG(test,
63 drm_buddy_alloc_blocks(&mm, bias_start,
64 bias_end, bias_size + ps, bias_size,
66 DRM_BUDDY_RANGE_ALLOCATION),
67 "buddy_alloc failed with bias(%x-%x), size=%u, ps=%u\n",
68 bias_start, bias_end, bias_size, bias_size);
71 KUNIT_ASSERT_TRUE_MSG(test,
72 drm_buddy_alloc_blocks(&mm, bias_start,
73 bias_end, bias_size + ps, ps,
75 DRM_BUDDY_RANGE_ALLOCATION),
76 "buddy_alloc didn't fail with bias(%x-%x), size=%u, ps=%u\n",
77 bias_start, bias_end, bias_size + ps, ps);
79 /* bias range too small for size */
80 KUNIT_ASSERT_TRUE_MSG(test,
81 drm_buddy_alloc_blocks(&mm, bias_start + ps,
82 bias_end, bias_size, ps,
84 DRM_BUDDY_RANGE_ALLOCATION),
85 "buddy_alloc didn't fail with bias(%x-%x), size=%u, ps=%u\n",
86 bias_start + ps, bias_end, bias_size, ps);
89 KUNIT_ASSERT_TRUE_MSG(test,
90 drm_buddy_alloc_blocks(&mm, bias_start + ps,
92 bias_size >> 1, bias_size >> 1,
94 DRM_BUDDY_RANGE_ALLOCATION),
95 "buddy_alloc h didn't fail with bias(%x-%x), size=%u, ps=%u\n",
96 bias_start + ps, bias_end - ps, bias_size >> 1, bias_size >> 1);
99 KUNIT_ASSERT_FALSE_MSG(test,
100 drm_buddy_alloc_blocks(&mm, bias_start,
101 bias_end, bias_size, bias_size,
103 DRM_BUDDY_RANGE_ALLOCATION),
104 "buddy_alloc i failed with bias(%x-%x), size=%u, ps=%u\n",
105 bias_start, bias_end, bias_size, bias_size);
106 drm_buddy_free_list(&mm, &tmp);
108 /* single page with internal round_up */
109 KUNIT_ASSERT_FALSE_MSG(test,
110 drm_buddy_alloc_blocks(&mm, bias_start,
111 bias_end, ps, bias_size,
113 DRM_BUDDY_RANGE_ALLOCATION),
114 "buddy_alloc failed with bias(%x-%x), size=%u, ps=%u\n",
115 bias_start, bias_end, ps, bias_size);
116 drm_buddy_free_list(&mm, &tmp);
118 /* random size within */
119 size = max(round_up(prandom_u32_state(&prng) % bias_rem, ps), ps);
121 KUNIT_ASSERT_FALSE_MSG(test,
122 drm_buddy_alloc_blocks(&mm, bias_start,
125 DRM_BUDDY_RANGE_ALLOCATION),
126 "buddy_alloc failed with bias(%x-%x), size=%u, ps=%u\n",
127 bias_start, bias_end, size, ps);
130 /* too big for current avail */
131 KUNIT_ASSERT_TRUE_MSG(test,
132 drm_buddy_alloc_blocks(&mm, bias_start,
133 bias_end, bias_rem + ps, ps,
135 DRM_BUDDY_RANGE_ALLOCATION),
136 "buddy_alloc didn't fail with bias(%x-%x), size=%u, ps=%u\n",
137 bias_start, bias_end, bias_rem + ps, ps);
140 /* random fill of the remainder */
141 size = max(round_up(prandom_u32_state(&prng) % bias_rem, ps), ps);
142 size = max(size, ps);
144 KUNIT_ASSERT_FALSE_MSG(test,
145 drm_buddy_alloc_blocks(&mm, bias_start,
148 DRM_BUDDY_RANGE_ALLOCATION),
149 "buddy_alloc failed with bias(%x-%x), size=%u, ps=%u\n",
150 bias_start, bias_end, size, ps);
152 * Intentionally allow some space to be left
153 * unallocated, and ideally not always on the bias
156 drm_buddy_free_list(&mm, &tmp);
158 list_splice_tail(&tmp, &allocated);
163 drm_buddy_free_list(&mm, &allocated);
167 * Something more free-form. Idea is to pick a random starting bias
168 * range within the address space and then start filling it up. Also
169 * randomly grow the bias range in both directions as we go along. This
170 * should give us bias start/end which is not always uniform like above,
171 * and in some cases will require the allocator to jump over already
172 * allocated nodes in the middle of the address space.
175 KUNIT_ASSERT_FALSE_MSG(test, drm_buddy_init(&mm, mm_size, ps),
176 "buddy_init failed\n");
178 bias_start = round_up(prandom_u32_state(&prng) % (mm_size - ps), ps);
179 bias_end = round_up(bias_start + prandom_u32_state(&prng) % (mm_size - bias_start), ps);
180 bias_end = max(bias_end, bias_start + ps);
181 bias_rem = bias_end - bias_start;
184 u32 size = max(round_up(prandom_u32_state(&prng) % bias_rem, ps), ps);
186 KUNIT_ASSERT_FALSE_MSG(test,
187 drm_buddy_alloc_blocks(&mm, bias_start,
190 DRM_BUDDY_RANGE_ALLOCATION),
191 "buddy_alloc failed with bias(%x-%x), size=%u, ps=%u\n",
192 bias_start, bias_end, size, ps);
196 * Try to randomly grow the bias range in both directions, or
197 * only one, or perhaps don't grow at all.
200 u32 old_bias_start = bias_start;
201 u32 old_bias_end = bias_end;
204 bias_start -= round_up(prandom_u32_state(&prng) % bias_start, ps);
205 if (bias_end != mm_size)
206 bias_end += round_up(prandom_u32_state(&prng) % (mm_size - bias_end), ps);
208 bias_rem += old_bias_start - bias_start;
209 bias_rem += bias_end - old_bias_end;
210 } while (!bias_rem && (bias_start || bias_end != mm_size));
213 KUNIT_ASSERT_EQ(test, bias_start, 0);
214 KUNIT_ASSERT_EQ(test, bias_end, mm_size);
215 KUNIT_ASSERT_TRUE_MSG(test,
216 drm_buddy_alloc_blocks(&mm, bias_start, bias_end,
219 DRM_BUDDY_RANGE_ALLOCATION),
220 "buddy_alloc passed with bias(%x-%x), size=%u\n",
221 bias_start, bias_end, ps);
223 drm_buddy_free_list(&mm, &allocated);
227 static void drm_test_buddy_alloc_contiguous(struct kunit *test)
229 const unsigned long ps = SZ_4K, mm_size = 16 * 3 * SZ_4K;
230 unsigned long i, n_pages, total;
231 struct drm_buddy_block *block;
236 LIST_HEAD(allocated);
238 KUNIT_EXPECT_FALSE(test, drm_buddy_init(&mm, mm_size, ps));
241 * Idea is to fragment the address space by alternating block
242 * allocations between three different lists; one for left, middle and
243 * right. We can then free a list to simulate fragmentation. In
244 * particular we want to exercise the DRM_BUDDY_CONTIGUOUS_ALLOCATION,
245 * including the try_harder path.
249 n_pages = mm_size / ps;
251 struct list_head *list;
260 KUNIT_ASSERT_FALSE_MSG(test,
261 drm_buddy_alloc_blocks(&mm, 0, mm_size,
263 "buddy_alloc hit an error size=%lu\n",
265 } while (++i < n_pages);
267 KUNIT_ASSERT_TRUE_MSG(test, drm_buddy_alloc_blocks(&mm, 0, mm_size,
268 3 * ps, ps, &allocated,
269 DRM_BUDDY_CONTIGUOUS_ALLOCATION),
270 "buddy_alloc didn't error size=%lu\n", 3 * ps);
272 drm_buddy_free_list(&mm, &middle);
273 KUNIT_ASSERT_TRUE_MSG(test, drm_buddy_alloc_blocks(&mm, 0, mm_size,
274 3 * ps, ps, &allocated,
275 DRM_BUDDY_CONTIGUOUS_ALLOCATION),
276 "buddy_alloc didn't error size=%lu\n", 3 * ps);
277 KUNIT_ASSERT_TRUE_MSG(test, drm_buddy_alloc_blocks(&mm, 0, mm_size,
278 2 * ps, ps, &allocated,
279 DRM_BUDDY_CONTIGUOUS_ALLOCATION),
280 "buddy_alloc didn't error size=%lu\n", 2 * ps);
282 drm_buddy_free_list(&mm, &right);
283 KUNIT_ASSERT_TRUE_MSG(test, drm_buddy_alloc_blocks(&mm, 0, mm_size,
284 3 * ps, ps, &allocated,
285 DRM_BUDDY_CONTIGUOUS_ALLOCATION),
286 "buddy_alloc didn't error size=%lu\n", 3 * ps);
288 * At this point we should have enough contiguous space for 2 blocks,
289 * however they are never buddies (since we freed middle and right) so
290 * will require the try_harder logic to find them.
292 KUNIT_ASSERT_FALSE_MSG(test, drm_buddy_alloc_blocks(&mm, 0, mm_size,
293 2 * ps, ps, &allocated,
294 DRM_BUDDY_CONTIGUOUS_ALLOCATION),
295 "buddy_alloc hit an error size=%lu\n", 2 * ps);
297 drm_buddy_free_list(&mm, &left);
298 KUNIT_ASSERT_FALSE_MSG(test, drm_buddy_alloc_blocks(&mm, 0, mm_size,
299 3 * ps, ps, &allocated,
300 DRM_BUDDY_CONTIGUOUS_ALLOCATION),
301 "buddy_alloc hit an error size=%lu\n", 3 * ps);
304 list_for_each_entry(block, &allocated, link)
305 total += drm_buddy_block_size(&mm, block);
307 KUNIT_ASSERT_EQ(test, total, ps * 2 + ps * 3);
309 drm_buddy_free_list(&mm, &allocated);
313 static void drm_test_buddy_alloc_pathological(struct kunit *test)
315 u64 mm_size, size, start = 0;
316 struct drm_buddy_block *block;
317 const int max_order = 3;
318 unsigned long flags = 0;
326 * Create a pot-sized mm, then allocate one of each possible
327 * order within. This should leave the mm with exactly one
328 * page left. Free the largest block, then whittle down again.
329 * Eventually we will have a fully 50% fragmented mm.
332 mm_size = PAGE_SIZE << max_order;
333 KUNIT_ASSERT_FALSE_MSG(test, drm_buddy_init(&mm, mm_size, PAGE_SIZE),
334 "buddy_init failed\n");
336 KUNIT_EXPECT_EQ(test, mm.max_order, max_order);
338 for (top = max_order; top; top--) {
339 /* Make room by freeing the largest allocated block */
340 block = list_first_entry_or_null(&blocks, typeof(*block), link);
342 list_del(&block->link);
343 drm_buddy_free_block(&mm, block);
346 for (order = top; order--;) {
347 size = get_size(order, PAGE_SIZE);
348 KUNIT_ASSERT_FALSE_MSG(test, drm_buddy_alloc_blocks(&mm, start,
351 "buddy_alloc hit -ENOMEM with order=%d, top=%d\n",
354 block = list_first_entry_or_null(&tmp, struct drm_buddy_block, link);
355 KUNIT_ASSERT_TRUE_MSG(test, block, "alloc_blocks has no blocks\n");
357 list_move_tail(&block->link, &blocks);
360 /* There should be one final page for this sub-allocation */
361 size = get_size(0, PAGE_SIZE);
362 KUNIT_ASSERT_FALSE_MSG(test, drm_buddy_alloc_blocks(&mm, start, mm_size,
363 size, size, &tmp, flags),
364 "buddy_alloc hit -ENOMEM for hole\n");
366 block = list_first_entry_or_null(&tmp, struct drm_buddy_block, link);
367 KUNIT_ASSERT_TRUE_MSG(test, block, "alloc_blocks has no blocks\n");
369 list_move_tail(&block->link, &holes);
371 size = get_size(top, PAGE_SIZE);
372 KUNIT_ASSERT_TRUE_MSG(test, drm_buddy_alloc_blocks(&mm, start, mm_size,
373 size, size, &tmp, flags),
374 "buddy_alloc unexpectedly succeeded at top-order %d/%d, it should be full!",
378 drm_buddy_free_list(&mm, &holes);
380 /* Nothing larger than blocks of chunk_size now available */
381 for (order = 1; order <= max_order; order++) {
382 size = get_size(order, PAGE_SIZE);
383 KUNIT_ASSERT_TRUE_MSG(test, drm_buddy_alloc_blocks(&mm, start, mm_size,
384 size, size, &tmp, flags),
385 "buddy_alloc unexpectedly succeeded at order %d, it should be full!",
389 list_splice_tail(&holes, &blocks);
390 drm_buddy_free_list(&mm, &blocks);
394 static void drm_test_buddy_alloc_pessimistic(struct kunit *test)
396 u64 mm_size, size, start = 0;
397 struct drm_buddy_block *block, *bn;
398 const unsigned int max_order = 16;
399 unsigned long flags = 0;
406 * Create a pot-sized mm, then allocate one of each possible
407 * order within. This should leave the mm with exactly one
411 mm_size = PAGE_SIZE << max_order;
412 KUNIT_ASSERT_FALSE_MSG(test, drm_buddy_init(&mm, mm_size, PAGE_SIZE),
413 "buddy_init failed\n");
415 KUNIT_EXPECT_EQ(test, mm.max_order, max_order);
417 for (order = 0; order < max_order; order++) {
418 size = get_size(order, PAGE_SIZE);
419 KUNIT_ASSERT_FALSE_MSG(test, drm_buddy_alloc_blocks(&mm, start, mm_size,
420 size, size, &tmp, flags),
421 "buddy_alloc hit -ENOMEM with order=%d\n",
424 block = list_first_entry_or_null(&tmp, struct drm_buddy_block, link);
425 KUNIT_ASSERT_TRUE_MSG(test, block, "alloc_blocks has no blocks\n");
427 list_move_tail(&block->link, &blocks);
430 /* And now the last remaining block available */
431 size = get_size(0, PAGE_SIZE);
432 KUNIT_ASSERT_FALSE_MSG(test, drm_buddy_alloc_blocks(&mm, start, mm_size,
433 size, size, &tmp, flags),
434 "buddy_alloc hit -ENOMEM on final alloc\n");
436 block = list_first_entry_or_null(&tmp, struct drm_buddy_block, link);
437 KUNIT_ASSERT_TRUE_MSG(test, block, "alloc_blocks has no blocks\n");
439 list_move_tail(&block->link, &blocks);
441 /* Should be completely full! */
442 for (order = max_order; order--;) {
443 size = get_size(order, PAGE_SIZE);
444 KUNIT_ASSERT_TRUE_MSG(test, drm_buddy_alloc_blocks(&mm, start, mm_size,
445 size, size, &tmp, flags),
446 "buddy_alloc unexpectedly succeeded, it should be full!");
449 block = list_last_entry(&blocks, typeof(*block), link);
450 list_del(&block->link);
451 drm_buddy_free_block(&mm, block);
453 /* As we free in increasing size, we make available larger blocks */
455 list_for_each_entry_safe(block, bn, &blocks, link) {
456 list_del(&block->link);
457 drm_buddy_free_block(&mm, block);
459 size = get_size(order, PAGE_SIZE);
460 KUNIT_ASSERT_FALSE_MSG(test, drm_buddy_alloc_blocks(&mm, start, mm_size,
461 size, size, &tmp, flags),
462 "buddy_alloc hit -ENOMEM with order=%d\n",
465 block = list_first_entry_or_null(&tmp, struct drm_buddy_block, link);
466 KUNIT_ASSERT_TRUE_MSG(test, block, "alloc_blocks has no blocks\n");
468 list_del(&block->link);
469 drm_buddy_free_block(&mm, block);
473 /* To confirm, now the whole mm should be available */
474 size = get_size(max_order, PAGE_SIZE);
475 KUNIT_ASSERT_FALSE_MSG(test, drm_buddy_alloc_blocks(&mm, start, mm_size,
476 size, size, &tmp, flags),
477 "buddy_alloc (realloc) hit -ENOMEM with order=%d\n",
480 block = list_first_entry_or_null(&tmp, struct drm_buddy_block, link);
481 KUNIT_ASSERT_TRUE_MSG(test, block, "alloc_blocks has no blocks\n");
483 list_del(&block->link);
484 drm_buddy_free_block(&mm, block);
485 drm_buddy_free_list(&mm, &blocks);
489 static void drm_test_buddy_alloc_optimistic(struct kunit *test)
491 u64 mm_size, size, start = 0;
492 struct drm_buddy_block *block;
493 unsigned long flags = 0;
494 const int max_order = 16;
501 * Create a mm with one block of each order available, and
502 * try to allocate them all.
505 mm_size = PAGE_SIZE * ((1 << (max_order + 1)) - 1);
507 KUNIT_ASSERT_FALSE_MSG(test, drm_buddy_init(&mm, mm_size, PAGE_SIZE),
508 "buddy_init failed\n");
510 KUNIT_EXPECT_EQ(test, mm.max_order, max_order);
512 for (order = 0; order <= max_order; order++) {
513 size = get_size(order, PAGE_SIZE);
514 KUNIT_ASSERT_FALSE_MSG(test, drm_buddy_alloc_blocks(&mm, start, mm_size,
515 size, size, &tmp, flags),
516 "buddy_alloc hit -ENOMEM with order=%d\n",
519 block = list_first_entry_or_null(&tmp, struct drm_buddy_block, link);
520 KUNIT_ASSERT_TRUE_MSG(test, block, "alloc_blocks has no blocks\n");
522 list_move_tail(&block->link, &blocks);
525 /* Should be completely full! */
526 size = get_size(0, PAGE_SIZE);
527 KUNIT_ASSERT_TRUE_MSG(test, drm_buddy_alloc_blocks(&mm, start, mm_size,
528 size, size, &tmp, flags),
529 "buddy_alloc unexpectedly succeeded, it should be full!");
531 drm_buddy_free_list(&mm, &blocks);
535 static void drm_test_buddy_alloc_limit(struct kunit *test)
537 u64 size = U64_MAX, start = 0;
538 struct drm_buddy_block *block;
539 unsigned long flags = 0;
540 LIST_HEAD(allocated);
543 KUNIT_EXPECT_FALSE(test, drm_buddy_init(&mm, size, PAGE_SIZE));
545 KUNIT_EXPECT_EQ_MSG(test, mm.max_order, DRM_BUDDY_MAX_ORDER,
546 "mm.max_order(%d) != %d\n", mm.max_order,
547 DRM_BUDDY_MAX_ORDER);
549 size = mm.chunk_size << mm.max_order;
550 KUNIT_EXPECT_FALSE(test, drm_buddy_alloc_blocks(&mm, start, size, size,
551 PAGE_SIZE, &allocated, flags));
553 block = list_first_entry_or_null(&allocated, struct drm_buddy_block, link);
554 KUNIT_EXPECT_TRUE(test, block);
556 KUNIT_EXPECT_EQ_MSG(test, drm_buddy_block_order(block), mm.max_order,
557 "block order(%d) != %d\n",
558 drm_buddy_block_order(block), mm.max_order);
560 KUNIT_EXPECT_EQ_MSG(test, drm_buddy_block_size(&mm, block),
561 BIT_ULL(mm.max_order) * PAGE_SIZE,
562 "block size(%llu) != %llu\n",
563 drm_buddy_block_size(&mm, block),
564 BIT_ULL(mm.max_order) * PAGE_SIZE);
566 drm_buddy_free_list(&mm, &allocated);
570 static int drm_buddy_suite_init(struct kunit_suite *suite)
573 random_seed = get_random_u32();
575 kunit_info(suite, "Testing DRM buddy manager, with random_seed=0x%x\n",
581 static struct kunit_case drm_buddy_tests[] = {
582 KUNIT_CASE(drm_test_buddy_alloc_limit),
583 KUNIT_CASE(drm_test_buddy_alloc_optimistic),
584 KUNIT_CASE(drm_test_buddy_alloc_pessimistic),
585 KUNIT_CASE(drm_test_buddy_alloc_pathological),
586 KUNIT_CASE(drm_test_buddy_alloc_contiguous),
587 KUNIT_CASE(drm_test_buddy_alloc_range_bias),
591 static struct kunit_suite drm_buddy_test_suite = {
593 .suite_init = drm_buddy_suite_init,
594 .test_cases = drm_buddy_tests,
597 kunit_test_suite(drm_buddy_test_suite);
599 MODULE_AUTHOR("Intel Corporation");
600 MODULE_LICENSE("GPL");