1 // SPDX-License-Identifier: GPL-2.0-only
3 * Test cases for the drm_mm range manager
6 #define pr_fmt(fmt) "drm_mm: " fmt
8 #include <linux/module.h>
9 #include <linux/prime_numbers.h>
10 #include <linux/slab.h>
11 #include <linux/random.h>
12 #include <linux/vmalloc.h>
14 #include <drm/drm_mm.h>
16 #include "../lib/drm_random.h"
18 #define TESTS "drm_mm_selftests.h"
19 #include "drm_selftest.h"
21 static unsigned int random_seed
;
22 static unsigned int max_iterations
= 8192;
23 static unsigned int max_prime
= 128;
32 static const struct insert_mode
{
34 enum drm_mm_insert_mode mode
;
36 [BEST
] = { "best", DRM_MM_INSERT_BEST
},
37 [BOTTOMUP
] = { "bottom-up", DRM_MM_INSERT_LOW
},
38 [TOPDOWN
] = { "top-down", DRM_MM_INSERT_HIGH
},
39 [EVICT
] = { "evict", DRM_MM_INSERT_EVICT
},
42 { "bottom-up", DRM_MM_INSERT_LOW
},
43 { "top-down", DRM_MM_INSERT_HIGH
},
47 static int igt_sanitycheck(void *ignored
)
49 pr_info("%s - ok!\n", __func__
);
53 static bool assert_no_holes(const struct drm_mm
*mm
)
55 struct drm_mm_node
*hole
;
56 u64 hole_start
, hole_end
;
60 drm_mm_for_each_hole(hole
, mm
, hole_start
, hole_end
)
63 pr_err("Expected to find no holes (after reserve), found %lu instead\n", count
);
67 drm_mm_for_each_node(hole
, mm
) {
68 if (drm_mm_hole_follows(hole
)) {
69 pr_err("Hole follows node, expected none!\n");
77 static bool assert_one_hole(const struct drm_mm
*mm
, u64 start
, u64 end
)
79 struct drm_mm_node
*hole
;
80 u64 hole_start
, hole_end
;
88 drm_mm_for_each_hole(hole
, mm
, hole_start
, hole_end
) {
89 if (start
!= hole_start
|| end
!= hole_end
) {
91 pr_err("empty mm has incorrect hole, found (%llx, %llx), expect (%llx, %llx)\n",
99 pr_err("Expected to find one hole, found %lu instead\n", count
);
106 static bool assert_continuous(const struct drm_mm
*mm
, u64 size
)
108 struct drm_mm_node
*node
, *check
, *found
;
112 if (!assert_no_holes(mm
))
117 drm_mm_for_each_node(node
, mm
) {
118 if (node
->start
!= addr
) {
119 pr_err("node[%ld] list out of order, expected %llx found %llx\n",
120 n
, addr
, node
->start
);
124 if (node
->size
!= size
) {
125 pr_err("node[%ld].size incorrect, expected %llx, found %llx\n",
126 n
, size
, node
->size
);
130 if (drm_mm_hole_follows(node
)) {
131 pr_err("node[%ld] is followed by a hole!\n", n
);
136 drm_mm_for_each_node_in_range(check
, mm
, addr
, addr
+ size
) {
138 pr_err("lookup return wrong node, expected start %llx, found %llx\n",
139 node
->start
, check
->start
);
145 pr_err("lookup failed for node %llx + %llx\n",
157 static u64
misalignment(struct drm_mm_node
*node
, u64 alignment
)
164 div64_u64_rem(node
->start
, alignment
, &rem
);
168 static bool assert_node(struct drm_mm_node
*node
, struct drm_mm
*mm
,
169 u64 size
, u64 alignment
, unsigned long color
)
173 if (!drm_mm_node_allocated(node
) || node
->mm
!= mm
) {
174 pr_err("node not allocated\n");
178 if (node
->size
!= size
) {
179 pr_err("node has wrong size, found %llu, expected %llu\n",
184 if (misalignment(node
, alignment
)) {
185 pr_err("node is misaligned, start %llx rem %llu, expected alignment %llu\n",
186 node
->start
, misalignment(node
, alignment
), alignment
);
190 if (node
->color
!= color
) {
191 pr_err("node has wrong color, found %lu, expected %lu\n",
199 #define show_mm(mm) do { \
200 struct drm_printer __p = drm_debug_printer(__func__); \
201 drm_mm_print((mm), &__p); } while (0)
203 static int igt_init(void *ignored
)
205 const unsigned int size
= 4096;
207 struct drm_mm_node tmp
;
210 /* Start with some simple checks on initialising the struct drm_mm */
211 memset(&mm
, 0, sizeof(mm
));
212 if (drm_mm_initialized(&mm
)) {
213 pr_err("zeroed mm claims to be initialized\n");
217 memset(&mm
, 0xff, sizeof(mm
));
218 drm_mm_init(&mm
, 0, size
);
219 if (!drm_mm_initialized(&mm
)) {
220 pr_err("mm claims not to be initialized\n");
224 if (!drm_mm_clean(&mm
)) {
225 pr_err("mm not empty on creation\n");
229 /* After creation, it should all be one massive hole */
230 if (!assert_one_hole(&mm
, 0, size
)) {
235 memset(&tmp
, 0, sizeof(tmp
));
238 ret
= drm_mm_reserve_node(&mm
, &tmp
);
240 pr_err("failed to reserve whole drm_mm\n");
244 /* After filling the range entirely, there should be no holes */
245 if (!assert_no_holes(&mm
)) {
250 /* And then after emptying it again, the massive hole should be back */
251 drm_mm_remove_node(&tmp
);
252 if (!assert_one_hole(&mm
, 0, size
)) {
260 drm_mm_takedown(&mm
);
264 static int igt_debug(void *ignored
)
267 struct drm_mm_node nodes
[2];
270 /* Create a small drm_mm with a couple of nodes and a few holes, and
271 * check that the debug iterator doesn't explode over a trivial drm_mm.
274 drm_mm_init(&mm
, 0, 4096);
276 memset(nodes
, 0, sizeof(nodes
));
277 nodes
[0].start
= 512;
278 nodes
[0].size
= 1024;
279 ret
= drm_mm_reserve_node(&mm
, &nodes
[0]);
281 pr_err("failed to reserve node[0] {start=%lld, size=%lld)\n",
282 nodes
[0].start
, nodes
[0].size
);
286 nodes
[1].size
= 1024;
287 nodes
[1].start
= 4096 - 512 - nodes
[1].size
;
288 ret
= drm_mm_reserve_node(&mm
, &nodes
[1]);
290 pr_err("failed to reserve node[1] {start=%lld, size=%lld)\n",
291 nodes
[1].start
, nodes
[1].size
);
299 static struct drm_mm_node
*set_node(struct drm_mm_node
*node
,
307 static bool expect_reserve_fail(struct drm_mm
*mm
, struct drm_mm_node
*node
)
311 err
= drm_mm_reserve_node(mm
, node
);
312 if (likely(err
== -ENOSPC
))
316 pr_err("impossible reserve succeeded, node %llu + %llu\n",
317 node
->start
, node
->size
);
318 drm_mm_remove_node(node
);
320 pr_err("impossible reserve failed with wrong error %d [expected %d], node %llu + %llu\n",
321 err
, -ENOSPC
, node
->start
, node
->size
);
326 static bool check_reserve_boundaries(struct drm_mm
*mm
,
330 const struct boundary
{
334 #define B(st, sz) { (st), (sz), "{ " #st ", " #sz "}" }
345 B(count
*size
, -size
),
346 B(count
*size
, count
*size
),
347 B(count
*size
, -count
*size
),
348 B(count
*size
, -(count
+1)*size
),
349 B((count
+1)*size
, size
),
350 B((count
+1)*size
, -size
),
351 B((count
+1)*size
, -2*size
),
354 struct drm_mm_node tmp
= {};
357 for (n
= 0; n
< ARRAY_SIZE(boundaries
); n
++) {
358 if (!expect_reserve_fail(mm
,
361 boundaries
[n
].size
))) {
362 pr_err("boundary[%d:%s] failed, count=%u, size=%lld\n",
363 n
, boundaries
[n
].name
, count
, size
);
371 static int __igt_reserve(unsigned int count
, u64 size
)
373 DRM_RND_STATE(prng
, random_seed
);
375 struct drm_mm_node tmp
, *nodes
, *node
, *next
;
376 unsigned int *order
, n
, m
, o
= 0;
379 /* For exercising drm_mm_reserve_node(), we want to check that
380 * reservations outside of the drm_mm range are rejected, and to
381 * overlapping and otherwise already occupied ranges. Afterwards,
382 * the tree and nodes should be intact.
385 DRM_MM_BUG_ON(!count
);
386 DRM_MM_BUG_ON(!size
);
389 order
= drm_random_order(count
, &prng
);
393 nodes
= vzalloc(array_size(count
, sizeof(*nodes
)));
398 drm_mm_init(&mm
, 0, count
* size
);
400 if (!check_reserve_boundaries(&mm
, count
, size
))
403 for (n
= 0; n
< count
; n
++) {
404 nodes
[n
].start
= order
[n
] * size
;
405 nodes
[n
].size
= size
;
407 err
= drm_mm_reserve_node(&mm
, &nodes
[n
]);
409 pr_err("reserve failed, step %d, start %llu\n",
415 if (!drm_mm_node_allocated(&nodes
[n
])) {
416 pr_err("reserved node not allocated! step %d, start %llu\n",
421 if (!expect_reserve_fail(&mm
, &nodes
[n
]))
425 /* After random insertion the nodes should be in order */
426 if (!assert_continuous(&mm
, size
))
429 /* Repeated use should then fail */
430 drm_random_reorder(order
, count
, &prng
);
431 for (n
= 0; n
< count
; n
++) {
432 if (!expect_reserve_fail(&mm
,
433 set_node(&tmp
, order
[n
] * size
, 1)))
436 /* Remove and reinsert should work */
437 drm_mm_remove_node(&nodes
[order
[n
]]);
438 err
= drm_mm_reserve_node(&mm
, &nodes
[order
[n
]]);
440 pr_err("reserve failed, step %d, start %llu\n",
447 if (!assert_continuous(&mm
, size
))
450 /* Overlapping use should then fail */
451 for (n
= 0; n
< count
; n
++) {
452 if (!expect_reserve_fail(&mm
, set_node(&tmp
, 0, size
*count
)))
455 for (n
= 0; n
< count
; n
++) {
456 if (!expect_reserve_fail(&mm
,
459 size
* (count
- n
))))
463 /* Remove several, reinsert, check full */
464 for_each_prime_number(n
, min(max_prime
, count
)) {
465 for (m
= 0; m
< n
; m
++) {
466 node
= &nodes
[order
[(o
+ m
) % count
]];
467 drm_mm_remove_node(node
);
470 for (m
= 0; m
< n
; m
++) {
471 node
= &nodes
[order
[(o
+ m
) % count
]];
472 err
= drm_mm_reserve_node(&mm
, node
);
474 pr_err("reserve failed, step %d/%d, start %llu\n",
483 if (!assert_continuous(&mm
, size
))
489 drm_mm_for_each_node_safe(node
, next
, &mm
)
490 drm_mm_remove_node(node
);
491 drm_mm_takedown(&mm
);
499 static int igt_reserve(void *ignored
)
501 const unsigned int count
= min_t(unsigned int, BIT(10), max_iterations
);
504 for_each_prime_number_from(n
, 1, 54) {
505 u64 size
= BIT_ULL(n
);
507 ret
= __igt_reserve(count
, size
- 1);
511 ret
= __igt_reserve(count
, size
);
515 ret
= __igt_reserve(count
, size
+ 1);
525 static bool expect_insert(struct drm_mm
*mm
, struct drm_mm_node
*node
,
526 u64 size
, u64 alignment
, unsigned long color
,
527 const struct insert_mode
*mode
)
531 err
= drm_mm_insert_node_generic(mm
, node
,
532 size
, alignment
, color
,
535 pr_err("insert (size=%llu, alignment=%llu, color=%lu, mode=%s) failed with err=%d\n",
536 size
, alignment
, color
, mode
->name
, err
);
540 if (!assert_node(node
, mm
, size
, alignment
, color
)) {
541 drm_mm_remove_node(node
);
548 static bool expect_insert_fail(struct drm_mm
*mm
, u64 size
)
550 struct drm_mm_node tmp
= {};
553 err
= drm_mm_insert_node(mm
, &tmp
, size
);
554 if (likely(err
== -ENOSPC
))
558 pr_err("impossible insert succeeded, node %llu + %llu\n",
559 tmp
.start
, tmp
.size
);
560 drm_mm_remove_node(&tmp
);
562 pr_err("impossible insert failed with wrong error %d [expected %d], size %llu\n",
568 static int __igt_insert(unsigned int count
, u64 size
, bool replace
)
570 DRM_RND_STATE(prng
, random_seed
);
571 const struct insert_mode
*mode
;
573 struct drm_mm_node
*nodes
, *node
, *next
;
574 unsigned int *order
, n
, m
, o
= 0;
577 /* Fill a range with lots of nodes, check it doesn't fail too early */
579 DRM_MM_BUG_ON(!count
);
580 DRM_MM_BUG_ON(!size
);
583 nodes
= vmalloc(array_size(count
, sizeof(*nodes
)));
587 order
= drm_random_order(count
, &prng
);
592 drm_mm_init(&mm
, 0, count
* size
);
594 for (mode
= insert_modes
; mode
->name
; mode
++) {
595 for (n
= 0; n
< count
; n
++) {
596 struct drm_mm_node tmp
;
598 node
= replace
? &tmp
: &nodes
[n
];
599 memset(node
, 0, sizeof(*node
));
600 if (!expect_insert(&mm
, node
, size
, 0, n
, mode
)) {
601 pr_err("%s insert failed, size %llu step %d\n",
602 mode
->name
, size
, n
);
607 drm_mm_replace_node(&tmp
, &nodes
[n
]);
608 if (drm_mm_node_allocated(&tmp
)) {
609 pr_err("replaced old-node still allocated! step %d\n",
614 if (!assert_node(&nodes
[n
], &mm
, size
, 0, n
)) {
615 pr_err("replaced node did not inherit parameters, size %llu step %d\n",
620 if (tmp
.start
!= nodes
[n
].start
) {
621 pr_err("replaced node mismatch location expected [%llx + %llx], found [%llx + %llx]\n",
623 nodes
[n
].start
, nodes
[n
].size
);
629 /* After random insertion the nodes should be in order */
630 if (!assert_continuous(&mm
, size
))
633 /* Repeated use should then fail */
634 if (!expect_insert_fail(&mm
, size
))
637 /* Remove one and reinsert, as the only hole it should refill itself */
638 for (n
= 0; n
< count
; n
++) {
639 u64 addr
= nodes
[n
].start
;
641 drm_mm_remove_node(&nodes
[n
]);
642 if (!expect_insert(&mm
, &nodes
[n
], size
, 0, n
, mode
)) {
643 pr_err("%s reinsert failed, size %llu step %d\n",
644 mode
->name
, size
, n
);
648 if (nodes
[n
].start
!= addr
) {
649 pr_err("%s reinsert node moved, step %d, expected %llx, found %llx\n",
650 mode
->name
, n
, addr
, nodes
[n
].start
);
654 if (!assert_continuous(&mm
, size
))
658 /* Remove several, reinsert, check full */
659 for_each_prime_number(n
, min(max_prime
, count
)) {
660 for (m
= 0; m
< n
; m
++) {
661 node
= &nodes
[order
[(o
+ m
) % count
]];
662 drm_mm_remove_node(node
);
665 for (m
= 0; m
< n
; m
++) {
666 node
= &nodes
[order
[(o
+ m
) % count
]];
667 if (!expect_insert(&mm
, node
, size
, 0, n
, mode
)) {
668 pr_err("%s multiple reinsert failed, size %llu step %d\n",
669 mode
->name
, size
, n
);
676 if (!assert_continuous(&mm
, size
))
679 if (!expect_insert_fail(&mm
, size
))
683 drm_mm_for_each_node_safe(node
, next
, &mm
)
684 drm_mm_remove_node(node
);
685 DRM_MM_BUG_ON(!drm_mm_clean(&mm
));
692 drm_mm_for_each_node_safe(node
, next
, &mm
)
693 drm_mm_remove_node(node
);
694 drm_mm_takedown(&mm
);
702 static int igt_insert(void *ignored
)
704 const unsigned int count
= min_t(unsigned int, BIT(10), max_iterations
);
708 for_each_prime_number_from(n
, 1, 54) {
709 u64 size
= BIT_ULL(n
);
711 ret
= __igt_insert(count
, size
- 1, false);
715 ret
= __igt_insert(count
, size
, false);
719 ret
= __igt_insert(count
, size
+ 1, false);
729 static int igt_replace(void *ignored
)
731 const unsigned int count
= min_t(unsigned int, BIT(10), max_iterations
);
735 /* Reuse igt_insert to exercise replacement by inserting a dummy node,
736 * then replacing it with the intended node. We want to check that
737 * the tree is intact and all the information we need is carried
738 * across to the target node.
741 for_each_prime_number_from(n
, 1, 54) {
742 u64 size
= BIT_ULL(n
);
744 ret
= __igt_insert(count
, size
- 1, true);
748 ret
= __igt_insert(count
, size
, true);
752 ret
= __igt_insert(count
, size
+ 1, true);
762 static bool expect_insert_in_range(struct drm_mm
*mm
, struct drm_mm_node
*node
,
763 u64 size
, u64 alignment
, unsigned long color
,
764 u64 range_start
, u64 range_end
,
765 const struct insert_mode
*mode
)
769 err
= drm_mm_insert_node_in_range(mm
, node
,
770 size
, alignment
, color
,
771 range_start
, range_end
,
774 pr_err("insert (size=%llu, alignment=%llu, color=%lu, mode=%s) nto range [%llx, %llx] failed with err=%d\n",
775 size
, alignment
, color
, mode
->name
,
776 range_start
, range_end
, err
);
780 if (!assert_node(node
, mm
, size
, alignment
, color
)) {
781 drm_mm_remove_node(node
);
788 static bool expect_insert_in_range_fail(struct drm_mm
*mm
,
793 struct drm_mm_node tmp
= {};
796 err
= drm_mm_insert_node_in_range(mm
, &tmp
,
798 range_start
, range_end
,
800 if (likely(err
== -ENOSPC
))
804 pr_err("impossible insert succeeded, node %llx + %llu, range [%llx, %llx]\n",
805 tmp
.start
, tmp
.size
, range_start
, range_end
);
806 drm_mm_remove_node(&tmp
);
808 pr_err("impossible insert failed with wrong error %d [expected %d], size %llu, range [%llx, %llx]\n",
809 err
, -ENOSPC
, size
, range_start
, range_end
);
815 static bool assert_contiguous_in_range(struct drm_mm
*mm
,
820 struct drm_mm_node
*node
;
823 if (!expect_insert_in_range_fail(mm
, size
, start
, end
))
826 n
= div64_u64(start
+ size
- 1, size
);
827 drm_mm_for_each_node(node
, mm
) {
828 if (node
->start
< start
|| node
->start
+ node
->size
> end
) {
829 pr_err("node %d out of range, address [%llx + %llu], range [%llx, %llx]\n",
830 n
, node
->start
, node
->start
+ node
->size
, start
, end
);
834 if (node
->start
!= n
* size
) {
835 pr_err("node %d out of order, expected start %llx, found %llx\n",
836 n
, n
* size
, node
->start
);
840 if (node
->size
!= size
) {
841 pr_err("node %d has wrong size, expected size %llx, found %llx\n",
842 n
, size
, node
->size
);
846 if (drm_mm_hole_follows(node
) &&
847 drm_mm_hole_node_end(node
) < end
) {
848 pr_err("node %d is followed by a hole!\n", n
);
856 node
= __drm_mm_interval_first(mm
, 0, start
- 1);
857 if (drm_mm_node_allocated(node
)) {
858 pr_err("node before start: node=%llx+%llu, start=%llx\n",
859 node
->start
, node
->size
, start
);
865 node
= __drm_mm_interval_first(mm
, end
, U64_MAX
);
866 if (drm_mm_node_allocated(node
)) {
867 pr_err("node after end: node=%llx+%llu, end=%llx\n",
868 node
->start
, node
->size
, end
);
876 static int __igt_insert_range(unsigned int count
, u64 size
, u64 start
, u64 end
)
878 const struct insert_mode
*mode
;
880 struct drm_mm_node
*nodes
, *node
, *next
;
881 unsigned int n
, start_n
, end_n
;
884 DRM_MM_BUG_ON(!count
);
885 DRM_MM_BUG_ON(!size
);
886 DRM_MM_BUG_ON(end
<= start
);
888 /* Very similar to __igt_insert(), but now instead of populating the
889 * full range of the drm_mm, we try to fill a small portion of it.
893 nodes
= vzalloc(array_size(count
, sizeof(*nodes
)));
898 drm_mm_init(&mm
, 0, count
* size
);
900 start_n
= div64_u64(start
+ size
- 1, size
);
901 end_n
= div64_u64(end
- size
, size
);
903 for (mode
= insert_modes
; mode
->name
; mode
++) {
904 for (n
= start_n
; n
<= end_n
; n
++) {
905 if (!expect_insert_in_range(&mm
, &nodes
[n
],
908 pr_err("%s insert failed, size %llu, step %d [%d, %d], range [%llx, %llx]\n",
916 if (!assert_contiguous_in_range(&mm
, size
, start
, end
)) {
917 pr_err("%s: range [%llx, %llx] not full after initialisation, size=%llu\n",
918 mode
->name
, start
, end
, size
);
922 /* Remove one and reinsert, it should refill itself */
923 for (n
= start_n
; n
<= end_n
; n
++) {
924 u64 addr
= nodes
[n
].start
;
926 drm_mm_remove_node(&nodes
[n
]);
927 if (!expect_insert_in_range(&mm
, &nodes
[n
],
930 pr_err("%s reinsert failed, step %d\n", mode
->name
, n
);
934 if (nodes
[n
].start
!= addr
) {
935 pr_err("%s reinsert node moved, step %d, expected %llx, found %llx\n",
936 mode
->name
, n
, addr
, nodes
[n
].start
);
941 if (!assert_contiguous_in_range(&mm
, size
, start
, end
)) {
942 pr_err("%s: range [%llx, %llx] not full after reinsertion, size=%llu\n",
943 mode
->name
, start
, end
, size
);
947 drm_mm_for_each_node_safe(node
, next
, &mm
)
948 drm_mm_remove_node(node
);
949 DRM_MM_BUG_ON(!drm_mm_clean(&mm
));
956 drm_mm_for_each_node_safe(node
, next
, &mm
)
957 drm_mm_remove_node(node
);
958 drm_mm_takedown(&mm
);
964 static int insert_outside_range(void)
967 const unsigned int start
= 1024;
968 const unsigned int end
= 2048;
969 const unsigned int size
= end
- start
;
971 drm_mm_init(&mm
, start
, size
);
973 if (!expect_insert_in_range_fail(&mm
, 1, 0, start
))
976 if (!expect_insert_in_range_fail(&mm
, size
,
977 start
- size
/2, start
+ (size
+1)/2))
980 if (!expect_insert_in_range_fail(&mm
, size
,
981 end
- (size
+1)/2, end
+ size
/2))
984 if (!expect_insert_in_range_fail(&mm
, 1, end
, end
+ size
))
987 drm_mm_takedown(&mm
);
991 static int igt_insert_range(void *ignored
)
993 const unsigned int count
= min_t(unsigned int, BIT(13), max_iterations
);
997 /* Check that requests outside the bounds of drm_mm are rejected. */
998 ret
= insert_outside_range();
1002 for_each_prime_number_from(n
, 1, 50) {
1003 const u64 size
= BIT_ULL(n
);
1004 const u64 max
= count
* size
;
1006 ret
= __igt_insert_range(count
, size
, 0, max
);
1010 ret
= __igt_insert_range(count
, size
, 1, max
);
1014 ret
= __igt_insert_range(count
, size
, 0, max
- 1);
1018 ret
= __igt_insert_range(count
, size
, 0, max
/2);
1022 ret
= __igt_insert_range(count
, size
, max
/2, max
);
1026 ret
= __igt_insert_range(count
, size
, max
/4+1, 3*max
/4-1);
1036 static int igt_align(void *ignored
)
1038 const struct insert_mode
*mode
;
1039 const unsigned int max_count
= min(8192u, max_prime
);
1041 struct drm_mm_node
*nodes
, *node
, *next
;
1045 /* For each of the possible insertion modes, we pick a few
1046 * arbitrary alignments and check that the inserted node
1047 * meets our requirements.
1050 nodes
= vzalloc(array_size(max_count
, sizeof(*nodes
)));
1054 drm_mm_init(&mm
, 1, U64_MAX
- 2);
1056 for (mode
= insert_modes
; mode
->name
; mode
++) {
1059 for_each_prime_number_from(prime
, 1, max_count
) {
1060 u64 size
= next_prime_number(prime
);
1062 if (!expect_insert(&mm
, &nodes
[i
],
1065 pr_err("%s insert failed with alignment=%d",
1073 drm_mm_for_each_node_safe(node
, next
, &mm
)
1074 drm_mm_remove_node(node
);
1075 DRM_MM_BUG_ON(!drm_mm_clean(&mm
));
1082 drm_mm_for_each_node_safe(node
, next
, &mm
)
1083 drm_mm_remove_node(node
);
1084 drm_mm_takedown(&mm
);
1090 static int igt_align_pot(int max
)
1093 struct drm_mm_node
*node
, *next
;
1097 /* Check that we can align to the full u64 address space */
1099 drm_mm_init(&mm
, 1, U64_MAX
- 2);
1101 for (bit
= max
- 1; bit
; bit
--) {
1104 node
= kzalloc(sizeof(*node
), GFP_KERNEL
);
1110 align
= BIT_ULL(bit
);
1111 size
= BIT_ULL(bit
-1) + 1;
1112 if (!expect_insert(&mm
, node
,
1114 &insert_modes
[0])) {
1115 pr_err("insert failed with alignment=%llx [%d]",
1125 drm_mm_for_each_node_safe(node
, next
, &mm
) {
1126 drm_mm_remove_node(node
);
1129 drm_mm_takedown(&mm
);
1133 static int igt_align32(void *ignored
)
1135 return igt_align_pot(32);
1138 static int igt_align64(void *ignored
)
1140 return igt_align_pot(64);
1143 static void show_scan(const struct drm_mm_scan
*scan
)
1145 pr_info("scan: hit [%llx, %llx], size=%lld, align=%lld, color=%ld\n",
1146 scan
->hit_start
, scan
->hit_end
,
1147 scan
->size
, scan
->alignment
, scan
->color
);
1150 static void show_holes(const struct drm_mm
*mm
, int count
)
1152 u64 hole_start
, hole_end
;
1153 struct drm_mm_node
*hole
;
1155 drm_mm_for_each_hole(hole
, mm
, hole_start
, hole_end
) {
1156 struct drm_mm_node
*next
= list_next_entry(hole
, node_list
);
1157 const char *node1
= NULL
, *node2
= NULL
;
1159 if (drm_mm_node_allocated(hole
))
1160 node1
= kasprintf(GFP_KERNEL
,
1161 "[%llx + %lld, color=%ld], ",
1162 hole
->start
, hole
->size
, hole
->color
);
1164 if (drm_mm_node_allocated(next
))
1165 node2
= kasprintf(GFP_KERNEL
,
1166 ", [%llx + %lld, color=%ld]",
1167 next
->start
, next
->size
, next
->color
);
1169 pr_info("%sHole [%llx - %llx, size %lld]%s\n",
1171 hole_start
, hole_end
, hole_end
- hole_start
,
1183 struct drm_mm_node node
;
1184 struct list_head link
;
1187 static bool evict_nodes(struct drm_mm_scan
*scan
,
1188 struct evict_node
*nodes
,
1189 unsigned int *order
,
1192 struct list_head
*evict_list
)
1194 struct evict_node
*e
, *en
;
1197 for (i
= 0; i
< count
; i
++) {
1198 e
= &nodes
[order
? order
[i
] : i
];
1199 list_add(&e
->link
, evict_list
);
1200 if (drm_mm_scan_add_block(scan
, &e
->node
))
1203 list_for_each_entry_safe(e
, en
, evict_list
, link
) {
1204 if (!drm_mm_scan_remove_block(scan
, &e
->node
))
1207 if (list_empty(evict_list
)) {
1208 pr_err("Failed to find eviction: size=%lld [avail=%d], align=%lld (color=%lu)\n",
1209 scan
->size
, count
, scan
->alignment
, scan
->color
);
1213 list_for_each_entry(e
, evict_list
, link
)
1214 drm_mm_remove_node(&e
->node
);
1217 struct drm_mm_node
*node
;
1219 while ((node
= drm_mm_scan_color_evict(scan
))) {
1220 e
= container_of(node
, typeof(*e
), node
);
1221 drm_mm_remove_node(&e
->node
);
1222 list_add(&e
->link
, evict_list
);
1225 if (drm_mm_scan_color_evict(scan
)) {
1226 pr_err("drm_mm_scan_color_evict unexpectedly reported overlapping nodes!\n");
1234 static bool evict_nothing(struct drm_mm
*mm
,
1235 unsigned int total_size
,
1236 struct evict_node
*nodes
)
1238 struct drm_mm_scan scan
;
1239 LIST_HEAD(evict_list
);
1240 struct evict_node
*e
;
1241 struct drm_mm_node
*node
;
1244 drm_mm_scan_init(&scan
, mm
, 1, 0, 0, 0);
1245 for (n
= 0; n
< total_size
; n
++) {
1247 list_add(&e
->link
, &evict_list
);
1248 drm_mm_scan_add_block(&scan
, &e
->node
);
1250 list_for_each_entry(e
, &evict_list
, link
)
1251 drm_mm_scan_remove_block(&scan
, &e
->node
);
1253 for (n
= 0; n
< total_size
; n
++) {
1256 if (!drm_mm_node_allocated(&e
->node
)) {
1257 pr_err("node[%d] no longer allocated!\n", n
);
1261 e
->link
.next
= NULL
;
1264 drm_mm_for_each_node(node
, mm
) {
1265 e
= container_of(node
, typeof(*e
), node
);
1266 e
->link
.next
= &e
->link
;
1269 for (n
= 0; n
< total_size
; n
++) {
1272 if (!e
->link
.next
) {
1273 pr_err("node[%d] no longer connected!\n", n
);
1278 return assert_continuous(mm
, nodes
[0].node
.size
);
1281 static bool evict_everything(struct drm_mm
*mm
,
1282 unsigned int total_size
,
1283 struct evict_node
*nodes
)
1285 struct drm_mm_scan scan
;
1286 LIST_HEAD(evict_list
);
1287 struct evict_node
*e
;
1291 drm_mm_scan_init(&scan
, mm
, total_size
, 0, 0, 0);
1292 for (n
= 0; n
< total_size
; n
++) {
1294 list_add(&e
->link
, &evict_list
);
1295 if (drm_mm_scan_add_block(&scan
, &e
->node
))
1300 list_for_each_entry(e
, &evict_list
, link
) {
1301 if (!drm_mm_scan_remove_block(&scan
, &e
->node
)) {
1303 pr_err("Node %lld not marked for eviction!\n",
1312 list_for_each_entry(e
, &evict_list
, link
)
1313 drm_mm_remove_node(&e
->node
);
1315 if (!assert_one_hole(mm
, 0, total_size
))
1318 list_for_each_entry(e
, &evict_list
, link
) {
1319 err
= drm_mm_reserve_node(mm
, &e
->node
);
1321 pr_err("Failed to reinsert node after eviction: start=%llx\n",
1327 return assert_continuous(mm
, nodes
[0].node
.size
);
1330 static int evict_something(struct drm_mm
*mm
,
1331 u64 range_start
, u64 range_end
,
1332 struct evict_node
*nodes
,
1333 unsigned int *order
,
1336 unsigned int alignment
,
1337 const struct insert_mode
*mode
)
1339 struct drm_mm_scan scan
;
1340 LIST_HEAD(evict_list
);
1341 struct evict_node
*e
;
1342 struct drm_mm_node tmp
;
1345 drm_mm_scan_init_with_range(&scan
, mm
,
1347 range_start
, range_end
,
1349 if (!evict_nodes(&scan
,
1350 nodes
, order
, count
, false,
1354 memset(&tmp
, 0, sizeof(tmp
));
1355 err
= drm_mm_insert_node_generic(mm
, &tmp
, size
, alignment
, 0,
1356 DRM_MM_INSERT_EVICT
);
1358 pr_err("Failed to insert into eviction hole: size=%d, align=%d\n",
1365 if (tmp
.start
< range_start
|| tmp
.start
+ tmp
.size
> range_end
) {
1366 pr_err("Inserted [address=%llu + %llu] did not fit into the request range [%llu, %llu]\n",
1367 tmp
.start
, tmp
.size
, range_start
, range_end
);
1371 if (!assert_node(&tmp
, mm
, size
, alignment
, 0) ||
1372 drm_mm_hole_follows(&tmp
)) {
1373 pr_err("Inserted did not fill the eviction hole: size=%lld [%d], align=%d [rem=%lld], start=%llx, hole-follows?=%d\n",
1375 alignment
, misalignment(&tmp
, alignment
),
1376 tmp
.start
, drm_mm_hole_follows(&tmp
));
1380 drm_mm_remove_node(&tmp
);
1384 list_for_each_entry(e
, &evict_list
, link
) {
1385 err
= drm_mm_reserve_node(mm
, &e
->node
);
1387 pr_err("Failed to reinsert node after eviction: start=%llx\n",
1393 if (!assert_continuous(mm
, nodes
[0].node
.size
)) {
1394 pr_err("range is no longer continuous\n");
1401 static int igt_evict(void *ignored
)
1403 DRM_RND_STATE(prng
, random_seed
);
1404 const unsigned int size
= 8192;
1405 const struct insert_mode
*mode
;
1407 struct evict_node
*nodes
;
1408 struct drm_mm_node
*node
, *next
;
1409 unsigned int *order
, n
;
1412 /* Here we populate a full drm_mm and then try and insert a new node
1413 * by evicting other nodes in a random order. The drm_mm_scan should
1414 * pick the first matching hole it finds from the random list. We
1415 * repeat that for different allocation strategies, alignments and
1416 * sizes to try and stress the hole finder.
1420 nodes
= vzalloc(array_size(size
, sizeof(*nodes
)));
1424 order
= drm_random_order(size
, &prng
);
1429 drm_mm_init(&mm
, 0, size
);
1430 for (n
= 0; n
< size
; n
++) {
1431 err
= drm_mm_insert_node(&mm
, &nodes
[n
].node
, 1);
1433 pr_err("insert failed, step %d\n", n
);
1439 /* First check that using the scanner doesn't break the mm */
1440 if (!evict_nothing(&mm
, size
, nodes
)) {
1441 pr_err("evict_nothing() failed\n");
1444 if (!evict_everything(&mm
, size
, nodes
)) {
1445 pr_err("evict_everything() failed\n");
1449 for (mode
= evict_modes
; mode
->name
; mode
++) {
1450 for (n
= 1; n
<= size
; n
<<= 1) {
1451 drm_random_reorder(order
, size
, &prng
);
1452 err
= evict_something(&mm
, 0, U64_MAX
,
1457 pr_err("%s evict_something(size=%u) failed\n",
1464 for (n
= 1; n
< size
; n
<<= 1) {
1465 drm_random_reorder(order
, size
, &prng
);
1466 err
= evict_something(&mm
, 0, U64_MAX
,
1471 pr_err("%s evict_something(size=%u, alignment=%u) failed\n",
1472 mode
->name
, size
/2, n
);
1478 for_each_prime_number_from(n
, 1, min(size
, max_prime
)) {
1479 unsigned int nsize
= (size
- n
+ 1) / 2;
1481 DRM_MM_BUG_ON(!nsize
);
1483 drm_random_reorder(order
, size
, &prng
);
1484 err
= evict_something(&mm
, 0, U64_MAX
,
1489 pr_err("%s evict_something(size=%u, alignment=%u) failed\n",
1490 mode
->name
, nsize
, n
);
1501 drm_mm_for_each_node_safe(node
, next
, &mm
)
1502 drm_mm_remove_node(node
);
1503 drm_mm_takedown(&mm
);
1511 static int igt_evict_range(void *ignored
)
1513 DRM_RND_STATE(prng
, random_seed
);
1514 const unsigned int size
= 8192;
1515 const unsigned int range_size
= size
/ 2;
1516 const unsigned int range_start
= size
/ 4;
1517 const unsigned int range_end
= range_start
+ range_size
;
1518 const struct insert_mode
*mode
;
1520 struct evict_node
*nodes
;
1521 struct drm_mm_node
*node
, *next
;
1522 unsigned int *order
, n
;
1525 /* Like igt_evict() but now we are limiting the search to a
1526 * small portion of the full drm_mm.
1530 nodes
= vzalloc(array_size(size
, sizeof(*nodes
)));
1534 order
= drm_random_order(size
, &prng
);
1539 drm_mm_init(&mm
, 0, size
);
1540 for (n
= 0; n
< size
; n
++) {
1541 err
= drm_mm_insert_node(&mm
, &nodes
[n
].node
, 1);
1543 pr_err("insert failed, step %d\n", n
);
1549 for (mode
= evict_modes
; mode
->name
; mode
++) {
1550 for (n
= 1; n
<= range_size
; n
<<= 1) {
1551 drm_random_reorder(order
, size
, &prng
);
1552 err
= evict_something(&mm
, range_start
, range_end
,
1557 pr_err("%s evict_something(size=%u) failed with range [%u, %u]\n",
1558 mode
->name
, n
, range_start
, range_end
);
1563 for (n
= 1; n
<= range_size
; n
<<= 1) {
1564 drm_random_reorder(order
, size
, &prng
);
1565 err
= evict_something(&mm
, range_start
, range_end
,
1570 pr_err("%s evict_something(size=%u, alignment=%u) failed with range [%u, %u]\n",
1571 mode
->name
, range_size
/2, n
, range_start
, range_end
);
1576 for_each_prime_number_from(n
, 1, min(range_size
, max_prime
)) {
1577 unsigned int nsize
= (range_size
- n
+ 1) / 2;
1579 DRM_MM_BUG_ON(!nsize
);
1581 drm_random_reorder(order
, size
, &prng
);
1582 err
= evict_something(&mm
, range_start
, range_end
,
1587 pr_err("%s evict_something(size=%u, alignment=%u) failed with range [%u, %u]\n",
1588 mode
->name
, nsize
, n
, range_start
, range_end
);
1598 drm_mm_for_each_node_safe(node
, next
, &mm
)
1599 drm_mm_remove_node(node
);
1600 drm_mm_takedown(&mm
);
1608 static unsigned int node_index(const struct drm_mm_node
*node
)
1610 return div64_u64(node
->start
, node
->size
);
1613 static int igt_topdown(void *ignored
)
1615 const struct insert_mode
*topdown
= &insert_modes
[TOPDOWN
];
1616 DRM_RND_STATE(prng
, random_seed
);
1617 const unsigned int count
= 8192;
1619 unsigned long *bitmap
;
1621 struct drm_mm_node
*nodes
, *node
, *next
;
1622 unsigned int *order
, n
, m
, o
= 0;
1625 /* When allocating top-down, we expect to be returned a node
1626 * from a suitable hole at the top of the drm_mm. We check that
1627 * the returned node does match the highest available slot.
1631 nodes
= vzalloc(array_size(count
, sizeof(*nodes
)));
1635 bitmap
= bitmap_zalloc(count
, GFP_KERNEL
);
1639 order
= drm_random_order(count
, &prng
);
1644 for (size
= 1; size
<= 64; size
<<= 1) {
1645 drm_mm_init(&mm
, 0, size
*count
);
1646 for (n
= 0; n
< count
; n
++) {
1647 if (!expect_insert(&mm
, &nodes
[n
],
1650 pr_err("insert failed, size %u step %d\n", size
, n
);
1654 if (drm_mm_hole_follows(&nodes
[n
])) {
1655 pr_err("hole after topdown insert %d, start=%llx\n, size=%u",
1656 n
, nodes
[n
].start
, size
);
1660 if (!assert_one_hole(&mm
, 0, size
*(count
- n
- 1)))
1664 if (!assert_continuous(&mm
, size
))
1667 drm_random_reorder(order
, count
, &prng
);
1668 for_each_prime_number_from(n
, 1, min(count
, max_prime
)) {
1669 for (m
= 0; m
< n
; m
++) {
1670 node
= &nodes
[order
[(o
+ m
) % count
]];
1671 drm_mm_remove_node(node
);
1672 __set_bit(node_index(node
), bitmap
);
1675 for (m
= 0; m
< n
; m
++) {
1678 node
= &nodes
[order
[(o
+ m
) % count
]];
1679 if (!expect_insert(&mm
, node
,
1682 pr_err("insert failed, step %d/%d\n", m
, n
);
1686 if (drm_mm_hole_follows(node
)) {
1687 pr_err("hole after topdown insert %d/%d, start=%llx\n",
1692 last
= find_last_bit(bitmap
, count
);
1693 if (node_index(node
) != last
) {
1694 pr_err("node %d/%d, size %d, not inserted into upmost hole, expected %d, found %d\n",
1695 m
, n
, size
, last
, node_index(node
));
1699 __clear_bit(last
, bitmap
);
1702 DRM_MM_BUG_ON(find_first_bit(bitmap
, count
) != count
);
1707 drm_mm_for_each_node_safe(node
, next
, &mm
)
1708 drm_mm_remove_node(node
);
1709 DRM_MM_BUG_ON(!drm_mm_clean(&mm
));
1715 drm_mm_for_each_node_safe(node
, next
, &mm
)
1716 drm_mm_remove_node(node
);
1717 drm_mm_takedown(&mm
);
1720 bitmap_free(bitmap
);
1727 static int igt_bottomup(void *ignored
)
1729 const struct insert_mode
*bottomup
= &insert_modes
[BOTTOMUP
];
1730 DRM_RND_STATE(prng
, random_seed
);
1731 const unsigned int count
= 8192;
1733 unsigned long *bitmap
;
1735 struct drm_mm_node
*nodes
, *node
, *next
;
1736 unsigned int *order
, n
, m
, o
= 0;
1739 /* Like igt_topdown, but instead of searching for the last hole,
1740 * we search for the first.
1744 nodes
= vzalloc(array_size(count
, sizeof(*nodes
)));
1748 bitmap
= bitmap_zalloc(count
, GFP_KERNEL
);
1752 order
= drm_random_order(count
, &prng
);
1757 for (size
= 1; size
<= 64; size
<<= 1) {
1758 drm_mm_init(&mm
, 0, size
*count
);
1759 for (n
= 0; n
< count
; n
++) {
1760 if (!expect_insert(&mm
, &nodes
[n
],
1763 pr_err("bottomup insert failed, size %u step %d\n", size
, n
);
1767 if (!assert_one_hole(&mm
, size
*(n
+ 1), size
*count
))
1771 if (!assert_continuous(&mm
, size
))
1774 drm_random_reorder(order
, count
, &prng
);
1775 for_each_prime_number_from(n
, 1, min(count
, max_prime
)) {
1776 for (m
= 0; m
< n
; m
++) {
1777 node
= &nodes
[order
[(o
+ m
) % count
]];
1778 drm_mm_remove_node(node
);
1779 __set_bit(node_index(node
), bitmap
);
1782 for (m
= 0; m
< n
; m
++) {
1785 node
= &nodes
[order
[(o
+ m
) % count
]];
1786 if (!expect_insert(&mm
, node
,
1789 pr_err("insert failed, step %d/%d\n", m
, n
);
1793 first
= find_first_bit(bitmap
, count
);
1794 if (node_index(node
) != first
) {
1795 pr_err("node %d/%d not inserted into bottom hole, expected %d, found %d\n",
1796 m
, n
, first
, node_index(node
));
1799 __clear_bit(first
, bitmap
);
1802 DRM_MM_BUG_ON(find_first_bit(bitmap
, count
) != count
);
1807 drm_mm_for_each_node_safe(node
, next
, &mm
)
1808 drm_mm_remove_node(node
);
1809 DRM_MM_BUG_ON(!drm_mm_clean(&mm
));
1815 drm_mm_for_each_node_safe(node
, next
, &mm
)
1816 drm_mm_remove_node(node
);
1817 drm_mm_takedown(&mm
);
1820 bitmap_free(bitmap
);
1827 static int __igt_once(unsigned int mode
)
1830 struct drm_mm_node rsvd_lo
, rsvd_hi
, node
;
1833 drm_mm_init(&mm
, 0, 7);
1835 memset(&rsvd_lo
, 0, sizeof(rsvd_lo
));
1838 err
= drm_mm_reserve_node(&mm
, &rsvd_lo
);
1840 pr_err("Could not reserve low node\n");
1844 memset(&rsvd_hi
, 0, sizeof(rsvd_hi
));
1847 err
= drm_mm_reserve_node(&mm
, &rsvd_hi
);
1849 pr_err("Could not reserve low node\n");
1853 if (!drm_mm_hole_follows(&rsvd_lo
) || !drm_mm_hole_follows(&rsvd_hi
)) {
1854 pr_err("Expected a hole after lo and high nodes!\n");
1859 memset(&node
, 0, sizeof(node
));
1860 err
= drm_mm_insert_node_generic(&mm
, &node
,
1862 mode
| DRM_MM_INSERT_ONCE
);
1864 pr_err("Unexpectedly inserted the node into the wrong hole: node.start=%llx\n",
1870 err
= drm_mm_insert_node_generic(&mm
, &node
, 2, 0, 0, mode
);
1872 pr_err("Could not insert the node into the available hole!\n");
1878 drm_mm_remove_node(&node
);
1880 drm_mm_remove_node(&rsvd_hi
);
1882 drm_mm_remove_node(&rsvd_lo
);
1884 drm_mm_takedown(&mm
);
1888 static int igt_lowest(void *ignored
)
1890 return __igt_once(DRM_MM_INSERT_LOW
);
1893 static int igt_highest(void *ignored
)
1895 return __igt_once(DRM_MM_INSERT_HIGH
);
1898 static void separate_adjacent_colors(const struct drm_mm_node
*node
,
1899 unsigned long color
,
1903 if (drm_mm_node_allocated(node
) && node
->color
!= color
)
1906 node
= list_next_entry(node
, node_list
);
1907 if (drm_mm_node_allocated(node
) && node
->color
!= color
)
1911 static bool colors_abutt(const struct drm_mm_node
*node
)
1913 if (!drm_mm_hole_follows(node
) &&
1914 drm_mm_node_allocated(list_next_entry(node
, node_list
))) {
1915 pr_err("colors abutt; %ld [%llx + %llx] is next to %ld [%llx + %llx]!\n",
1916 node
->color
, node
->start
, node
->size
,
1917 list_next_entry(node
, node_list
)->color
,
1918 list_next_entry(node
, node_list
)->start
,
1919 list_next_entry(node
, node_list
)->size
);
1926 static int igt_color(void *ignored
)
1928 const unsigned int count
= min(4096u, max_iterations
);
1929 const struct insert_mode
*mode
;
1931 struct drm_mm_node
*node
, *nn
;
1933 int ret
= -EINVAL
, err
;
1935 /* Color adjustment complicates everything. First we just check
1936 * that when we insert a node we apply any color_adjustment callback.
1937 * The callback we use should ensure that there is a gap between
1938 * any two nodes, and so after each insertion we check that those
1939 * holes are inserted and that they are preserved.
1942 drm_mm_init(&mm
, 0, U64_MAX
);
1944 for (n
= 1; n
<= count
; n
++) {
1945 node
= kzalloc(sizeof(*node
), GFP_KERNEL
);
1951 if (!expect_insert(&mm
, node
,
1953 &insert_modes
[0])) {
1954 pr_err("insert failed, step %d\n", n
);
1960 drm_mm_for_each_node_safe(node
, nn
, &mm
) {
1961 if (node
->color
!= node
->size
) {
1962 pr_err("invalid color stored: expected %lld, found %ld\n",
1963 node
->size
, node
->color
);
1968 drm_mm_remove_node(node
);
1972 /* Now, let's start experimenting with applying a color callback */
1973 mm
.color_adjust
= separate_adjacent_colors
;
1974 for (mode
= insert_modes
; mode
->name
; mode
++) {
1977 node
= kzalloc(sizeof(*node
), GFP_KERNEL
);
1983 node
->size
= 1 + 2*count
;
1984 node
->color
= node
->size
;
1986 err
= drm_mm_reserve_node(&mm
, node
);
1988 pr_err("initial reserve failed!\n");
1993 last
= node
->start
+ node
->size
;
1995 for (n
= 1; n
<= count
; n
++) {
1998 node
= kzalloc(sizeof(*node
), GFP_KERNEL
);
2005 node
->size
= n
+ count
;
2006 node
->color
= node
->size
;
2008 err
= drm_mm_reserve_node(&mm
, node
);
2009 if (err
!= -ENOSPC
) {
2010 pr_err("reserve %d did not report color overlap! err=%d\n",
2015 node
->start
+= n
+ 1;
2016 rem
= misalignment(node
, n
+ count
);
2017 node
->start
+= n
+ count
- rem
;
2019 err
= drm_mm_reserve_node(&mm
, node
);
2021 pr_err("reserve %d failed, err=%d\n", n
, err
);
2026 last
= node
->start
+ node
->size
;
2029 for (n
= 1; n
<= count
; n
++) {
2030 node
= kzalloc(sizeof(*node
), GFP_KERNEL
);
2036 if (!expect_insert(&mm
, node
,
2039 pr_err("%s insert failed, step %d\n",
2046 drm_mm_for_each_node_safe(node
, nn
, &mm
) {
2049 if (node
->color
!= node
->size
) {
2050 pr_err("%s invalid color stored: expected %lld, found %ld\n",
2051 mode
->name
, node
->size
, node
->color
);
2056 if (colors_abutt(node
))
2059 div64_u64_rem(node
->start
, node
->size
, &rem
);
2061 pr_err("%s colored node misaligned, start=%llx expected alignment=%lld [rem=%lld]\n",
2062 mode
->name
, node
->start
, node
->size
, rem
);
2066 drm_mm_remove_node(node
);
2075 drm_mm_for_each_node_safe(node
, nn
, &mm
) {
2076 drm_mm_remove_node(node
);
2079 drm_mm_takedown(&mm
);
2083 static int evict_color(struct drm_mm
*mm
,
2084 u64 range_start
, u64 range_end
,
2085 struct evict_node
*nodes
,
2086 unsigned int *order
,
2089 unsigned int alignment
,
2090 unsigned long color
,
2091 const struct insert_mode
*mode
)
2093 struct drm_mm_scan scan
;
2094 LIST_HEAD(evict_list
);
2095 struct evict_node
*e
;
2096 struct drm_mm_node tmp
;
2099 drm_mm_scan_init_with_range(&scan
, mm
,
2100 size
, alignment
, color
,
2101 range_start
, range_end
,
2103 if (!evict_nodes(&scan
,
2104 nodes
, order
, count
, true,
2108 memset(&tmp
, 0, sizeof(tmp
));
2109 err
= drm_mm_insert_node_generic(mm
, &tmp
, size
, alignment
, color
,
2110 DRM_MM_INSERT_EVICT
);
2112 pr_err("Failed to insert into eviction hole: size=%d, align=%d, color=%lu, err=%d\n",
2113 size
, alignment
, color
, err
);
2119 if (tmp
.start
< range_start
|| tmp
.start
+ tmp
.size
> range_end
) {
2120 pr_err("Inserted [address=%llu + %llu] did not fit into the request range [%llu, %llu]\n",
2121 tmp
.start
, tmp
.size
, range_start
, range_end
);
2125 if (colors_abutt(&tmp
))
2128 if (!assert_node(&tmp
, mm
, size
, alignment
, color
)) {
2129 pr_err("Inserted did not fit the eviction hole: size=%lld [%d], align=%d [rem=%lld], start=%llx\n",
2131 alignment
, misalignment(&tmp
, alignment
), tmp
.start
);
2135 drm_mm_remove_node(&tmp
);
2139 list_for_each_entry(e
, &evict_list
, link
) {
2140 err
= drm_mm_reserve_node(mm
, &e
->node
);
2142 pr_err("Failed to reinsert node after eviction: start=%llx\n",
2152 static int igt_color_evict(void *ignored
)
2154 DRM_RND_STATE(prng
, random_seed
);
2155 const unsigned int total_size
= min(8192u, max_iterations
);
2156 const struct insert_mode
*mode
;
2157 unsigned long color
= 0;
2159 struct evict_node
*nodes
;
2160 struct drm_mm_node
*node
, *next
;
2161 unsigned int *order
, n
;
2164 /* Check that the drm_mm_scan also honours color adjustment when
2165 * choosing its victims to create a hole. Our color_adjust does not
2166 * allow two nodes to be placed together without an intervening hole
2167 * enlarging the set of victims that must be evicted.
2171 nodes
= vzalloc(array_size(total_size
, sizeof(*nodes
)));
2175 order
= drm_random_order(total_size
, &prng
);
2180 drm_mm_init(&mm
, 0, 2*total_size
- 1);
2181 mm
.color_adjust
= separate_adjacent_colors
;
2182 for (n
= 0; n
< total_size
; n
++) {
2183 if (!expect_insert(&mm
, &nodes
[n
].node
,
2185 &insert_modes
[0])) {
2186 pr_err("insert failed, step %d\n", n
);
2191 for (mode
= evict_modes
; mode
->name
; mode
++) {
2192 for (n
= 1; n
<= total_size
; n
<<= 1) {
2193 drm_random_reorder(order
, total_size
, &prng
);
2194 err
= evict_color(&mm
, 0, U64_MAX
,
2195 nodes
, order
, total_size
,
2199 pr_err("%s evict_color(size=%u) failed\n",
2205 for (n
= 1; n
< total_size
; n
<<= 1) {
2206 drm_random_reorder(order
, total_size
, &prng
);
2207 err
= evict_color(&mm
, 0, U64_MAX
,
2208 nodes
, order
, total_size
,
2209 total_size
/2, n
, color
++,
2212 pr_err("%s evict_color(size=%u, alignment=%u) failed\n",
2213 mode
->name
, total_size
/2, n
);
2218 for_each_prime_number_from(n
, 1, min(total_size
, max_prime
)) {
2219 unsigned int nsize
= (total_size
- n
+ 1) / 2;
2221 DRM_MM_BUG_ON(!nsize
);
2223 drm_random_reorder(order
, total_size
, &prng
);
2224 err
= evict_color(&mm
, 0, U64_MAX
,
2225 nodes
, order
, total_size
,
2229 pr_err("%s evict_color(size=%u, alignment=%u) failed\n",
2230 mode
->name
, nsize
, n
);
2242 drm_mm_for_each_node_safe(node
, next
, &mm
)
2243 drm_mm_remove_node(node
);
2244 drm_mm_takedown(&mm
);
2252 static int igt_color_evict_range(void *ignored
)
2254 DRM_RND_STATE(prng
, random_seed
);
2255 const unsigned int total_size
= 8192;
2256 const unsigned int range_size
= total_size
/ 2;
2257 const unsigned int range_start
= total_size
/ 4;
2258 const unsigned int range_end
= range_start
+ range_size
;
2259 const struct insert_mode
*mode
;
2260 unsigned long color
= 0;
2262 struct evict_node
*nodes
;
2263 struct drm_mm_node
*node
, *next
;
2264 unsigned int *order
, n
;
2267 /* Like igt_color_evict(), but limited to small portion of the full
2272 nodes
= vzalloc(array_size(total_size
, sizeof(*nodes
)));
2276 order
= drm_random_order(total_size
, &prng
);
2281 drm_mm_init(&mm
, 0, 2*total_size
- 1);
2282 mm
.color_adjust
= separate_adjacent_colors
;
2283 for (n
= 0; n
< total_size
; n
++) {
2284 if (!expect_insert(&mm
, &nodes
[n
].node
,
2286 &insert_modes
[0])) {
2287 pr_err("insert failed, step %d\n", n
);
2292 for (mode
= evict_modes
; mode
->name
; mode
++) {
2293 for (n
= 1; n
<= range_size
; n
<<= 1) {
2294 drm_random_reorder(order
, range_size
, &prng
);
2295 err
= evict_color(&mm
, range_start
, range_end
,
2296 nodes
, order
, total_size
,
2300 pr_err("%s evict_color(size=%u) failed for range [%x, %x]\n",
2301 mode
->name
, n
, range_start
, range_end
);
2306 for (n
= 1; n
< range_size
; n
<<= 1) {
2307 drm_random_reorder(order
, total_size
, &prng
);
2308 err
= evict_color(&mm
, range_start
, range_end
,
2309 nodes
, order
, total_size
,
2310 range_size
/2, n
, color
++,
2313 pr_err("%s evict_color(size=%u, alignment=%u) failed for range [%x, %x]\n",
2314 mode
->name
, total_size
/2, n
, range_start
, range_end
);
2319 for_each_prime_number_from(n
, 1, min(range_size
, max_prime
)) {
2320 unsigned int nsize
= (range_size
- n
+ 1) / 2;
2322 DRM_MM_BUG_ON(!nsize
);
2324 drm_random_reorder(order
, total_size
, &prng
);
2325 err
= evict_color(&mm
, range_start
, range_end
,
2326 nodes
, order
, total_size
,
2330 pr_err("%s evict_color(size=%u, alignment=%u) failed for range [%x, %x]\n",
2331 mode
->name
, nsize
, n
, range_start
, range_end
);
2343 drm_mm_for_each_node_safe(node
, next
, &mm
)
2344 drm_mm_remove_node(node
);
2345 drm_mm_takedown(&mm
);
2353 #include "drm_selftest.c"
2355 static int __init
test_drm_mm_init(void)
2359 while (!random_seed
)
2360 random_seed
= get_random_int();
2362 pr_info("Testing DRM range manger (struct drm_mm), with random_seed=0x%x max_iterations=%u max_prime=%u\n",
2363 random_seed
, max_iterations
, max_prime
);
2364 err
= run_selftests(selftests
, ARRAY_SIZE(selftests
), NULL
);
2366 return err
> 0 ? 0 : err
;
2369 static void __exit
test_drm_mm_exit(void)
2373 module_init(test_drm_mm_init
);
2374 module_exit(test_drm_mm_exit
);
2376 module_param(random_seed
, uint
, 0400);
2377 module_param(max_iterations
, uint
, 0400);
2378 module_param(max_prime
, uint
, 0400);
2380 MODULE_AUTHOR("Intel Corporation");
2381 MODULE_LICENSE("GPL");