1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Test cases for the drm_mm range manager 4 * 5 * Copyright (c) 2022 Arthur Grillo <arthur.grillo@usp.br> 6 */ 7 8 #include <kunit/test.h> 9 10 #include <linux/prime_numbers.h> 11 #include <linux/slab.h> 12 #include <linux/random.h> 13 #include <linux/vmalloc.h> 14 #include <linux/ktime.h> 15 16 #include <drm/drm_mm.h> 17 #include <drm/drm_print.h> 18 19 enum { 20 BEST, 21 BOTTOMUP, 22 TOPDOWN, 23 EVICT, 24 }; 25 26 static const struct insert_mode { 27 const char *name; 28 enum drm_mm_insert_mode mode; 29 } insert_modes[] = { 30 [BEST] = { "best", DRM_MM_INSERT_BEST }, 31 [BOTTOMUP] = { "bottom-up", DRM_MM_INSERT_LOW }, 32 [TOPDOWN] = { "top-down", DRM_MM_INSERT_HIGH }, 33 [EVICT] = { "evict", DRM_MM_INSERT_EVICT }, 34 {} 35 }; 36 37 static bool assert_no_holes(struct kunit *test, const struct drm_mm *mm) 38 { 39 struct drm_mm_node *hole; 40 u64 hole_start, __always_unused hole_end; 41 unsigned long count; 42 43 count = 0; 44 drm_mm_for_each_hole(hole, mm, hole_start, hole_end) 45 count++; 46 if (count) { 47 KUNIT_FAIL(test, 48 "Expected to find no holes (after reserve), found %lu instead\n", count); 49 return false; 50 } 51 52 drm_mm_for_each_node(hole, mm) { 53 if (drm_mm_hole_follows(hole)) { 54 KUNIT_FAIL(test, "Hole follows node, expected none!\n"); 55 return false; 56 } 57 } 58 59 return true; 60 } 61 62 static bool assert_one_hole(struct kunit *test, const struct drm_mm *mm, u64 start, u64 end) 63 { 64 struct drm_mm_node *hole; 65 u64 hole_start, hole_end; 66 unsigned long count; 67 bool ok = true; 68 69 if (end <= start) 70 return true; 71 72 count = 0; 73 drm_mm_for_each_hole(hole, mm, hole_start, hole_end) { 74 if (start != hole_start || end != hole_end) { 75 if (ok) 76 KUNIT_FAIL(test, 77 "empty mm has incorrect hole, found (%llx, %llx), expect (%llx, %llx)\n", 78 hole_start, hole_end, start, end); 79 ok = false; 80 } 81 count++; 82 } 83 if (count != 1) { 84 KUNIT_FAIL(test, "Expected to find one hole, found %lu instead\n", count); 85 ok = false; 86 } 87 88 return ok; 89 } 90 91 static u64 misalignment(struct drm_mm_node *node, u64 alignment) 92 { 93 u64 rem; 94 95 if (!alignment) 96 return 0; 97 98 div64_u64_rem(node->start, alignment, &rem); 99 return rem; 100 } 101 102 static bool assert_node(struct kunit *test, struct drm_mm_node *node, struct drm_mm *mm, 103 u64 size, u64 alignment, unsigned long color) 104 { 105 bool ok = true; 106 107 if (!drm_mm_node_allocated(node) || node->mm != mm) { 108 KUNIT_FAIL(test, "node not allocated\n"); 109 ok = false; 110 } 111 112 if (node->size != size) { 113 KUNIT_FAIL(test, "node has wrong size, found %llu, expected %llu\n", 114 node->size, size); 115 ok = false; 116 } 117 118 if (misalignment(node, alignment)) { 119 KUNIT_FAIL(test, 120 "node is misaligned, start %llx rem %llu, expected alignment %llu\n", 121 node->start, misalignment(node, alignment), alignment); 122 ok = false; 123 } 124 125 if (node->color != color) { 126 KUNIT_FAIL(test, "node has wrong color, found %lu, expected %lu\n", 127 node->color, color); 128 ok = false; 129 } 130 131 return ok; 132 } 133 134 static void drm_test_mm_init(struct kunit *test) 135 { 136 const unsigned int size = 4096; 137 struct drm_mm mm; 138 struct drm_mm_node tmp; 139 140 /* Start with some simple checks on initialising the struct drm_mm */ 141 memset(&mm, 0, sizeof(mm)); 142 KUNIT_ASSERT_FALSE_MSG(test, drm_mm_initialized(&mm), 143 "zeroed mm claims to be initialized\n"); 144 145 memset(&mm, 0xff, sizeof(mm)); 146 drm_mm_init(&mm, 0, size); 147 if (!drm_mm_initialized(&mm)) { 148 KUNIT_FAIL(test, "mm claims not to be initialized\n"); 149 goto out; 150 } 151 152 if (!drm_mm_clean(&mm)) { 153 KUNIT_FAIL(test, "mm not empty on creation\n"); 154 goto out; 155 } 156 157 /* After creation, it should all be one massive hole */ 158 if (!assert_one_hole(test, &mm, 0, size)) { 159 KUNIT_FAIL(test, "mm not one hole on creation"); 160 goto out; 161 } 162 163 memset(&tmp, 0, sizeof(tmp)); 164 tmp.start = 0; 165 tmp.size = size; 166 if (drm_mm_reserve_node(&mm, &tmp)) { 167 KUNIT_FAIL(test, "failed to reserve whole drm_mm\n"); 168 goto out; 169 } 170 171 /* After filling the range entirely, there should be no holes */ 172 if (!assert_no_holes(test, &mm)) { 173 KUNIT_FAIL(test, "mm has holes when filled"); 174 goto out; 175 } 176 177 /* And then after emptying it again, the massive hole should be back */ 178 drm_mm_remove_node(&tmp); 179 if (!assert_one_hole(test, &mm, 0, size)) { 180 KUNIT_FAIL(test, "mm does not have single hole after emptying"); 181 goto out; 182 } 183 184 out: 185 drm_mm_takedown(&mm); 186 } 187 188 static void drm_test_mm_debug(struct kunit *test) 189 { 190 struct drm_printer p = drm_dbg_printer(NULL, DRM_UT_CORE, test->name); 191 struct drm_mm mm; 192 struct drm_mm_node nodes[2]; 193 194 /* Create a small drm_mm with a couple of nodes and a few holes, and 195 * check that the debug iterator doesn't explode over a trivial drm_mm. 196 */ 197 drm_mm_init(&mm, 0, 4096); 198 199 memset(nodes, 0, sizeof(nodes)); 200 nodes[0].start = 512; 201 nodes[0].size = 1024; 202 KUNIT_ASSERT_FALSE_MSG(test, drm_mm_reserve_node(&mm, &nodes[0]), 203 "failed to reserve node[0] {start=%lld, size=%lld)\n", 204 nodes[0].start, nodes[0].size); 205 206 nodes[1].size = 1024; 207 nodes[1].start = 4096 - 512 - nodes[1].size; 208 KUNIT_ASSERT_FALSE_MSG(test, drm_mm_reserve_node(&mm, &nodes[1]), 209 "failed to reserve node[0] {start=%lld, size=%lld)\n", 210 nodes[0].start, nodes[0].size); 211 212 drm_mm_print(&mm, &p); 213 KUNIT_SUCCEED(test); 214 } 215 216 static bool expect_insert(struct kunit *test, struct drm_mm *mm, 217 struct drm_mm_node *node, u64 size, u64 alignment, unsigned long color, 218 const struct insert_mode *mode) 219 { 220 int err; 221 222 err = drm_mm_insert_node_generic(mm, node, 223 size, alignment, color, 224 mode->mode); 225 if (err) { 226 KUNIT_FAIL(test, 227 "insert (size=%llu, alignment=%llu, color=%lu, mode=%s) failed with err=%d\n", 228 size, alignment, color, mode->name, err); 229 return false; 230 } 231 232 if (!assert_node(test, node, mm, size, alignment, color)) { 233 drm_mm_remove_node(node); 234 return false; 235 } 236 237 return true; 238 } 239 240 static void drm_test_mm_align_pot(struct kunit *test, int max) 241 { 242 struct drm_mm mm; 243 struct drm_mm_node *node, *next; 244 int bit; 245 246 /* Check that we can align to the full u64 address space */ 247 248 drm_mm_init(&mm, 1, U64_MAX - 2); 249 250 for (bit = max - 1; bit; bit--) { 251 u64 align, size; 252 253 node = kzalloc_obj(*node); 254 if (!node) { 255 KUNIT_FAIL(test, "failed to allocate node"); 256 goto out; 257 } 258 259 align = BIT_ULL(bit); 260 size = BIT_ULL(bit - 1) + 1; 261 if (!expect_insert(test, &mm, node, size, align, bit, &insert_modes[0])) { 262 KUNIT_FAIL(test, "insert failed with alignment=%llx [%d]", align, bit); 263 goto out; 264 } 265 266 cond_resched(); 267 } 268 269 out: 270 drm_mm_for_each_node_safe(node, next, &mm) { 271 drm_mm_remove_node(node); 272 kfree(node); 273 } 274 drm_mm_takedown(&mm); 275 } 276 277 static void drm_test_mm_align32(struct kunit *test) 278 { 279 drm_test_mm_align_pot(test, 32); 280 } 281 282 static void drm_test_mm_align64(struct kunit *test) 283 { 284 drm_test_mm_align_pot(test, 64); 285 } 286 287 static void drm_test_mm_once(struct kunit *test, unsigned int mode) 288 { 289 struct drm_mm mm; 290 struct drm_mm_node rsvd_lo, rsvd_hi, node; 291 292 drm_mm_init(&mm, 0, 7); 293 294 memset(&rsvd_lo, 0, sizeof(rsvd_lo)); 295 rsvd_lo.start = 1; 296 rsvd_lo.size = 1; 297 if (drm_mm_reserve_node(&mm, &rsvd_lo)) { 298 KUNIT_FAIL(test, "Could not reserve low node\n"); 299 goto err; 300 } 301 302 memset(&rsvd_hi, 0, sizeof(rsvd_hi)); 303 rsvd_hi.start = 5; 304 rsvd_hi.size = 1; 305 if (drm_mm_reserve_node(&mm, &rsvd_hi)) { 306 KUNIT_FAIL(test, "Could not reserve low node\n"); 307 goto err_lo; 308 } 309 310 if (!drm_mm_hole_follows(&rsvd_lo) || !drm_mm_hole_follows(&rsvd_hi)) { 311 KUNIT_FAIL(test, "Expected a hole after lo and high nodes!\n"); 312 goto err_hi; 313 } 314 315 memset(&node, 0, sizeof(node)); 316 if (drm_mm_insert_node_generic(&mm, &node, 2, 0, 0, mode)) { 317 KUNIT_FAIL(test, "Could not insert the node into the available hole!\n"); 318 goto err_hi; 319 } 320 321 drm_mm_remove_node(&node); 322 err_hi: 323 drm_mm_remove_node(&rsvd_hi); 324 err_lo: 325 drm_mm_remove_node(&rsvd_lo); 326 err: 327 drm_mm_takedown(&mm); 328 } 329 330 static void drm_test_mm_lowest(struct kunit *test) 331 { 332 drm_test_mm_once(test, DRM_MM_INSERT_LOW); 333 } 334 335 static void drm_test_mm_highest(struct kunit *test) 336 { 337 drm_test_mm_once(test, DRM_MM_INSERT_HIGH); 338 } 339 340 static struct kunit_case drm_mm_tests[] = { 341 KUNIT_CASE(drm_test_mm_init), 342 KUNIT_CASE(drm_test_mm_debug), 343 KUNIT_CASE(drm_test_mm_align32), 344 KUNIT_CASE(drm_test_mm_align64), 345 KUNIT_CASE(drm_test_mm_lowest), 346 KUNIT_CASE(drm_test_mm_highest), 347 {} 348 }; 349 350 static struct kunit_suite drm_mm_test_suite = { 351 .name = "drm_mm", 352 .test_cases = drm_mm_tests, 353 }; 354 355 kunit_test_suite(drm_mm_test_suite); 356 357 MODULE_AUTHOR("Intel Corporation"); 358 MODULE_DESCRIPTION("Test cases for the drm_mm range manager"); 359 MODULE_LICENSE("GPL"); 360