1 /* 2 * Copyright © 2016 Intel Corporation 3 * 4 * Permission is hereby granted, free of charge, to any person obtaining a 5 * copy of this software and associated documentation files (the "Software"), 6 * to deal in the Software without restriction, including without limitation 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 * and/or sell copies of the Software, and to permit persons to whom the 9 * Software is furnished to do so, subject to the following conditions: 10 * 11 * The above copyright notice and this permission notice (including the next 12 * paragraph) shall be included in all copies or substantial portions of the 13 * Software. 14 * 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21 * IN THE SOFTWARE. 22 * 23 */ 24 25 #include "../i915_selftest.h" 26 27 #include "lib_sw_fence.h" 28 #include "mock_context.h" 29 #include "mock_drm.h" 30 #include "mock_gem_device.h" 31 32 static int populate_ggtt(struct drm_i915_private *i915) 33 { 34 struct drm_i915_gem_object *obj; 35 u64 size; 36 37 for (size = 0; 38 size + I915_GTT_PAGE_SIZE <= i915->ggtt.vm.total; 39 size += I915_GTT_PAGE_SIZE) { 40 struct i915_vma *vma; 41 42 obj = i915_gem_object_create_internal(i915, I915_GTT_PAGE_SIZE); 43 if (IS_ERR(obj)) 44 return PTR_ERR(obj); 45 46 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, 0); 47 if (IS_ERR(vma)) 48 return PTR_ERR(vma); 49 } 50 51 if (!list_empty(&i915->mm.unbound_list)) { 52 size = 0; 53 list_for_each_entry(obj, &i915->mm.unbound_list, mm.link) 54 size++; 55 56 pr_err("Found %lld objects unbound!\n", size); 57 return -EINVAL; 58 } 59 60 if (list_empty(&i915->ggtt.vm.inactive_list)) { 61 pr_err("No objects on the GGTT inactive list!\n"); 62 return -EINVAL; 63 } 64 65 return 0; 66 } 67 68 static void unpin_ggtt(struct drm_i915_private *i915) 69 { 70 struct i915_vma *vma; 71 72 list_for_each_entry(vma, &i915->ggtt.vm.inactive_list, vm_link) 73 i915_vma_unpin(vma); 74 } 75 76 static void cleanup_objects(struct drm_i915_private *i915) 77 { 78 struct drm_i915_gem_object *obj, *on; 79 80 list_for_each_entry_safe(obj, on, &i915->mm.unbound_list, mm.link) 81 i915_gem_object_put(obj); 82 83 list_for_each_entry_safe(obj, on, &i915->mm.bound_list, mm.link) 84 i915_gem_object_put(obj); 85 86 mutex_unlock(&i915->drm.struct_mutex); 87 88 i915_gem_drain_freed_objects(i915); 89 90 mutex_lock(&i915->drm.struct_mutex); 91 } 92 93 static int igt_evict_something(void *arg) 94 { 95 struct drm_i915_private *i915 = arg; 96 struct i915_ggtt *ggtt = &i915->ggtt; 97 int err; 98 99 /* Fill the GGTT with pinned objects and try to evict one. */ 100 101 err = populate_ggtt(i915); 102 if (err) 103 goto cleanup; 104 105 /* Everything is pinned, nothing should happen */ 106 err = i915_gem_evict_something(&ggtt->vm, 107 I915_GTT_PAGE_SIZE, 0, 0, 108 0, U64_MAX, 109 0); 110 if (err != -ENOSPC) { 111 pr_err("i915_gem_evict_something failed on a full GGTT with err=%d\n", 112 err); 113 goto cleanup; 114 } 115 116 unpin_ggtt(i915); 117 118 /* Everything is unpinned, we should be able to evict something */ 119 err = i915_gem_evict_something(&ggtt->vm, 120 I915_GTT_PAGE_SIZE, 0, 0, 121 0, U64_MAX, 122 0); 123 if (err) { 124 pr_err("i915_gem_evict_something failed on a full GGTT with err=%d\n", 125 err); 126 goto cleanup; 127 } 128 129 cleanup: 130 cleanup_objects(i915); 131 return err; 132 } 133 134 static int igt_overcommit(void *arg) 135 { 136 struct drm_i915_private *i915 = arg; 137 struct drm_i915_gem_object *obj; 138 struct i915_vma *vma; 139 int err; 140 141 /* Fill the GGTT with pinned objects and then try to pin one more. 142 * We expect it to fail. 143 */ 144 145 err = populate_ggtt(i915); 146 if (err) 147 goto cleanup; 148 149 obj = i915_gem_object_create_internal(i915, I915_GTT_PAGE_SIZE); 150 if (IS_ERR(obj)) { 151 err = PTR_ERR(obj); 152 goto cleanup; 153 } 154 155 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, 0); 156 if (!IS_ERR(vma) || PTR_ERR(vma) != -ENOSPC) { 157 pr_err("Failed to evict+insert, i915_gem_object_ggtt_pin returned err=%d\n", (int)PTR_ERR(vma)); 158 err = -EINVAL; 159 goto cleanup; 160 } 161 162 cleanup: 163 cleanup_objects(i915); 164 return err; 165 } 166 167 static int igt_evict_for_vma(void *arg) 168 { 169 struct drm_i915_private *i915 = arg; 170 struct i915_ggtt *ggtt = &i915->ggtt; 171 struct drm_mm_node target = { 172 .start = 0, 173 .size = 4096, 174 }; 175 int err; 176 177 /* Fill the GGTT with pinned objects and try to evict a range. */ 178 179 err = populate_ggtt(i915); 180 if (err) 181 goto cleanup; 182 183 /* Everything is pinned, nothing should happen */ 184 err = i915_gem_evict_for_node(&ggtt->vm, &target, 0); 185 if (err != -ENOSPC) { 186 pr_err("i915_gem_evict_for_node on a full GGTT returned err=%d\n", 187 err); 188 goto cleanup; 189 } 190 191 unpin_ggtt(i915); 192 193 /* Everything is unpinned, we should be able to evict the node */ 194 err = i915_gem_evict_for_node(&ggtt->vm, &target, 0); 195 if (err) { 196 pr_err("i915_gem_evict_for_node returned err=%d\n", 197 err); 198 goto cleanup; 199 } 200 201 cleanup: 202 cleanup_objects(i915); 203 return err; 204 } 205 206 static void mock_color_adjust(const struct drm_mm_node *node, 207 unsigned long color, 208 u64 *start, 209 u64 *end) 210 { 211 } 212 213 static int igt_evict_for_cache_color(void *arg) 214 { 215 struct drm_i915_private *i915 = arg; 216 struct i915_ggtt *ggtt = &i915->ggtt; 217 const unsigned long flags = PIN_OFFSET_FIXED; 218 struct drm_mm_node target = { 219 .start = I915_GTT_PAGE_SIZE * 2, 220 .size = I915_GTT_PAGE_SIZE, 221 .color = I915_CACHE_LLC, 222 }; 223 struct drm_i915_gem_object *obj; 224 struct i915_vma *vma; 225 int err; 226 227 /* Currently the use of color_adjust is limited to cache domains within 228 * the ggtt, and so the presence of mm.color_adjust is assumed to be 229 * i915_gtt_color_adjust throughout our driver, so using a mock color 230 * adjust will work just fine for our purposes. 231 */ 232 ggtt->vm.mm.color_adjust = mock_color_adjust; 233 234 obj = i915_gem_object_create_internal(i915, I915_GTT_PAGE_SIZE); 235 if (IS_ERR(obj)) { 236 err = PTR_ERR(obj); 237 goto cleanup; 238 } 239 i915_gem_object_set_cache_level(obj, I915_CACHE_LLC); 240 241 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, 242 I915_GTT_PAGE_SIZE | flags); 243 if (IS_ERR(vma)) { 244 pr_err("[0]i915_gem_object_ggtt_pin failed\n"); 245 err = PTR_ERR(vma); 246 goto cleanup; 247 } 248 249 obj = i915_gem_object_create_internal(i915, I915_GTT_PAGE_SIZE); 250 if (IS_ERR(obj)) { 251 err = PTR_ERR(obj); 252 goto cleanup; 253 } 254 i915_gem_object_set_cache_level(obj, I915_CACHE_LLC); 255 256 /* Neighbouring; same colour - should fit */ 257 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, 258 (I915_GTT_PAGE_SIZE * 2) | flags); 259 if (IS_ERR(vma)) { 260 pr_err("[1]i915_gem_object_ggtt_pin failed\n"); 261 err = PTR_ERR(vma); 262 goto cleanup; 263 } 264 265 i915_vma_unpin(vma); 266 267 /* Remove just the second vma */ 268 err = i915_gem_evict_for_node(&ggtt->vm, &target, 0); 269 if (err) { 270 pr_err("[0]i915_gem_evict_for_node returned err=%d\n", err); 271 goto cleanup; 272 } 273 274 /* Attempt to remove the first *pinned* vma, by removing the (empty) 275 * neighbour -- this should fail. 276 */ 277 target.color = I915_CACHE_L3_LLC; 278 279 err = i915_gem_evict_for_node(&ggtt->vm, &target, 0); 280 if (!err) { 281 pr_err("[1]i915_gem_evict_for_node returned err=%d\n", err); 282 err = -EINVAL; 283 goto cleanup; 284 } 285 286 err = 0; 287 288 cleanup: 289 unpin_ggtt(i915); 290 cleanup_objects(i915); 291 ggtt->vm.mm.color_adjust = NULL; 292 return err; 293 } 294 295 static int igt_evict_vm(void *arg) 296 { 297 struct drm_i915_private *i915 = arg; 298 struct i915_ggtt *ggtt = &i915->ggtt; 299 int err; 300 301 /* Fill the GGTT with pinned objects and try to evict everything. */ 302 303 err = populate_ggtt(i915); 304 if (err) 305 goto cleanup; 306 307 /* Everything is pinned, nothing should happen */ 308 err = i915_gem_evict_vm(&ggtt->vm); 309 if (err) { 310 pr_err("i915_gem_evict_vm on a full GGTT returned err=%d]\n", 311 err); 312 goto cleanup; 313 } 314 315 unpin_ggtt(i915); 316 317 err = i915_gem_evict_vm(&ggtt->vm); 318 if (err) { 319 pr_err("i915_gem_evict_vm on a full GGTT returned err=%d]\n", 320 err); 321 goto cleanup; 322 } 323 324 cleanup: 325 cleanup_objects(i915); 326 return err; 327 } 328 329 static int igt_evict_contexts(void *arg) 330 { 331 const u64 PRETEND_GGTT_SIZE = 16ull << 20; 332 struct drm_i915_private *i915 = arg; 333 struct intel_engine_cs *engine; 334 enum intel_engine_id id; 335 struct reserved { 336 struct drm_mm_node node; 337 struct reserved *next; 338 } *reserved = NULL; 339 struct drm_mm_node hole; 340 unsigned long count; 341 int err; 342 343 /* 344 * The purpose of this test is to verify that we will trigger an 345 * eviction in the GGTT when constructing a request that requires 346 * additional space in the GGTT for pinning the context. This space 347 * is not directly tied to the request so reclaiming it requires 348 * extra work. 349 * 350 * As such this test is only meaningful for full-ppgtt environments 351 * where the GTT space of the request is separate from the GGTT 352 * allocation required to build the request. 353 */ 354 if (!USES_FULL_PPGTT(i915)) 355 return 0; 356 357 mutex_lock(&i915->drm.struct_mutex); 358 intel_runtime_pm_get(i915); 359 360 /* Reserve a block so that we know we have enough to fit a few rq */ 361 memset(&hole, 0, sizeof(hole)); 362 err = i915_gem_gtt_insert(&i915->ggtt.vm, &hole, 363 PRETEND_GGTT_SIZE, 0, I915_COLOR_UNEVICTABLE, 364 0, i915->ggtt.vm.total, 365 PIN_NOEVICT); 366 if (err) 367 goto out_locked; 368 369 /* Make the GGTT appear small by filling it with unevictable nodes */ 370 count = 0; 371 do { 372 struct reserved *r; 373 374 r = kcalloc(1, sizeof(*r), GFP_KERNEL); 375 if (!r) { 376 err = -ENOMEM; 377 goto out_locked; 378 } 379 380 if (i915_gem_gtt_insert(&i915->ggtt.vm, &r->node, 381 1ul << 20, 0, I915_COLOR_UNEVICTABLE, 382 0, i915->ggtt.vm.total, 383 PIN_NOEVICT)) { 384 kfree(r); 385 break; 386 } 387 388 r->next = reserved; 389 reserved = r; 390 391 count++; 392 } while (1); 393 drm_mm_remove_node(&hole); 394 mutex_unlock(&i915->drm.struct_mutex); 395 pr_info("Filled GGTT with %lu 1MiB nodes\n", count); 396 397 /* Overfill the GGTT with context objects and so try to evict one. */ 398 for_each_engine(engine, i915, id) { 399 struct i915_sw_fence fence; 400 struct drm_file *file; 401 402 file = mock_file(i915); 403 if (IS_ERR(file)) 404 return PTR_ERR(file); 405 406 count = 0; 407 mutex_lock(&i915->drm.struct_mutex); 408 onstack_fence_init(&fence); 409 do { 410 struct i915_request *rq; 411 struct i915_gem_context *ctx; 412 413 ctx = live_context(i915, file); 414 if (!ctx) 415 break; 416 417 /* We will need some GGTT space for the rq's context */ 418 igt_evict_ctl.fail_if_busy = true; 419 rq = i915_request_alloc(engine, ctx); 420 igt_evict_ctl.fail_if_busy = false; 421 422 if (IS_ERR(rq)) { 423 /* When full, fail_if_busy will trigger EBUSY */ 424 if (PTR_ERR(rq) != -EBUSY) { 425 pr_err("Unexpected error from request alloc (ctx hw id %u, on %s): %d\n", 426 ctx->hw_id, engine->name, 427 (int)PTR_ERR(rq)); 428 err = PTR_ERR(rq); 429 } 430 break; 431 } 432 433 /* Keep every request/ctx pinned until we are full */ 434 err = i915_sw_fence_await_sw_fence_gfp(&rq->submit, 435 &fence, 436 GFP_KERNEL); 437 if (err < 0) 438 break; 439 440 i915_request_add(rq); 441 count++; 442 err = 0; 443 } while(1); 444 mutex_unlock(&i915->drm.struct_mutex); 445 446 onstack_fence_fini(&fence); 447 pr_info("Submitted %lu contexts/requests on %s\n", 448 count, engine->name); 449 450 mock_file_free(i915, file); 451 if (err) 452 break; 453 } 454 455 mutex_lock(&i915->drm.struct_mutex); 456 out_locked: 457 while (reserved) { 458 struct reserved *next = reserved->next; 459 460 drm_mm_remove_node(&reserved->node); 461 kfree(reserved); 462 463 reserved = next; 464 } 465 if (drm_mm_node_allocated(&hole)) 466 drm_mm_remove_node(&hole); 467 intel_runtime_pm_put(i915); 468 mutex_unlock(&i915->drm.struct_mutex); 469 470 return err; 471 } 472 473 int i915_gem_evict_mock_selftests(void) 474 { 475 static const struct i915_subtest tests[] = { 476 SUBTEST(igt_evict_something), 477 SUBTEST(igt_evict_for_vma), 478 SUBTEST(igt_evict_for_cache_color), 479 SUBTEST(igt_evict_vm), 480 SUBTEST(igt_overcommit), 481 }; 482 struct drm_i915_private *i915; 483 int err; 484 485 i915 = mock_gem_device(); 486 if (!i915) 487 return -ENOMEM; 488 489 mutex_lock(&i915->drm.struct_mutex); 490 err = i915_subtests(tests, i915); 491 mutex_unlock(&i915->drm.struct_mutex); 492 493 drm_dev_unref(&i915->drm); 494 return err; 495 } 496 497 int i915_gem_evict_live_selftests(struct drm_i915_private *i915) 498 { 499 static const struct i915_subtest tests[] = { 500 SUBTEST(igt_evict_contexts), 501 }; 502 503 return i915_subtests(tests, i915); 504 } 505