1 /* 2 * SPDX-License-Identifier: MIT 3 * 4 * Copyright © 2014-2016 Intel Corporation 5 */ 6 7 #include <linux/pagevec.h> 8 #include <linux/swap.h> 9 10 #include "gem/i915_gem_region.h" 11 #include "i915_drv.h" 12 #include "i915_gemfs.h" 13 #include "i915_gem_object.h" 14 #include "i915_scatterlist.h" 15 #include "i915_trace.h" 16 17 /* 18 * Move pages to appropriate lru and release the pagevec, decrementing the 19 * ref count of those pages. 20 */ 21 static void check_release_pagevec(struct pagevec *pvec) 22 { 23 check_move_unevictable_pages(pvec); 24 __pagevec_release(pvec); 25 cond_resched(); 26 } 27 28 void shmem_sg_free_table(struct sg_table *st, struct address_space *mapping, 29 bool dirty, bool backup) 30 { 31 struct sgt_iter sgt_iter; 32 struct pagevec pvec; 33 struct page *page; 34 35 mapping_clear_unevictable(mapping); 36 37 pagevec_init(&pvec); 38 for_each_sgt_page(page, sgt_iter, st) { 39 if (dirty) 40 set_page_dirty(page); 41 42 if (backup) 43 mark_page_accessed(page); 44 45 if (!pagevec_add(&pvec, page)) 46 check_release_pagevec(&pvec); 47 } 48 if (pagevec_count(&pvec)) 49 check_release_pagevec(&pvec); 50 51 sg_free_table(st); 52 } 53 54 int shmem_sg_alloc_table(struct drm_i915_private *i915, struct sg_table *st, 55 size_t size, struct intel_memory_region *mr, 56 struct address_space *mapping, 57 unsigned int max_segment) 58 { 59 const unsigned long page_count = size / PAGE_SIZE; 60 unsigned long i; 61 struct scatterlist *sg; 62 struct page *page; 63 unsigned long last_pfn = 0; /* suppress gcc warning */ 64 gfp_t noreclaim; 65 int ret; 66 67 /* 68 * If there's no chance of allocating enough pages for the whole 69 * object, bail early. 70 */ 71 if (size > resource_size(&mr->region)) 72 return -ENOMEM; 73 74 if (sg_alloc_table(st, page_count, GFP_KERNEL)) 75 return -ENOMEM; 76 77 /* 78 * Get the list of pages out of our struct file. They'll be pinned 79 * at this point until we release them. 80 * 81 * Fail silently without starting the shrinker 82 */ 83 mapping_set_unevictable(mapping); 84 noreclaim = mapping_gfp_constraint(mapping, ~__GFP_RECLAIM); 85 noreclaim |= __GFP_NORETRY | __GFP_NOWARN; 86 87 sg = st->sgl; 88 st->nents = 0; 89 for (i = 0; i < page_count; i++) { 90 const unsigned int shrink[] = { 91 I915_SHRINK_BOUND | I915_SHRINK_UNBOUND, 92 0, 93 }, *s = shrink; 94 gfp_t gfp = noreclaim; 95 96 do { 97 cond_resched(); 98 page = shmem_read_mapping_page_gfp(mapping, i, gfp); 99 if (!IS_ERR(page)) 100 break; 101 102 if (!*s) { 103 ret = PTR_ERR(page); 104 goto err_sg; 105 } 106 107 i915_gem_shrink(NULL, i915, 2 * page_count, NULL, *s++); 108 109 /* 110 * We've tried hard to allocate the memory by reaping 111 * our own buffer, now let the real VM do its job and 112 * go down in flames if truly OOM. 113 * 114 * However, since graphics tend to be disposable, 115 * defer the oom here by reporting the ENOMEM back 116 * to userspace. 117 */ 118 if (!*s) { 119 /* reclaim and warn, but no oom */ 120 gfp = mapping_gfp_mask(mapping); 121 122 /* 123 * Our bo are always dirty and so we require 124 * kswapd to reclaim our pages (direct reclaim 125 * does not effectively begin pageout of our 126 * buffers on its own). However, direct reclaim 127 * only waits for kswapd when under allocation 128 * congestion. So as a result __GFP_RECLAIM is 129 * unreliable and fails to actually reclaim our 130 * dirty pages -- unless you try over and over 131 * again with !__GFP_NORETRY. However, we still 132 * want to fail this allocation rather than 133 * trigger the out-of-memory killer and for 134 * this we want __GFP_RETRY_MAYFAIL. 135 */ 136 gfp |= __GFP_RETRY_MAYFAIL; 137 } 138 } while (1); 139 140 if (!i || 141 sg->length >= max_segment || 142 page_to_pfn(page) != last_pfn + 1) { 143 if (i) 144 sg = sg_next(sg); 145 146 st->nents++; 147 sg_set_page(sg, page, PAGE_SIZE, 0); 148 } else { 149 sg->length += PAGE_SIZE; 150 } 151 last_pfn = page_to_pfn(page); 152 153 /* Check that the i965g/gm workaround works. */ 154 GEM_BUG_ON(gfp & __GFP_DMA32 && last_pfn >= 0x00100000UL); 155 } 156 if (sg) /* loop terminated early; short sg table */ 157 sg_mark_end(sg); 158 159 /* Trim unused sg entries to avoid wasting memory. */ 160 i915_sg_trim(st); 161 162 return 0; 163 err_sg: 164 sg_mark_end(sg); 165 if (sg != st->sgl) { 166 shmem_sg_free_table(st, mapping, false, false); 167 } else { 168 mapping_clear_unevictable(mapping); 169 sg_free_table(st); 170 } 171 172 /* 173 * shmemfs first checks if there is enough memory to allocate the page 174 * and reports ENOSPC should there be insufficient, along with the usual 175 * ENOMEM for a genuine allocation failure. 176 * 177 * We use ENOSPC in our driver to mean that we have run out of aperture 178 * space and so want to translate the error from shmemfs back to our 179 * usual understanding of ENOMEM. 180 */ 181 if (ret == -ENOSPC) 182 ret = -ENOMEM; 183 184 return ret; 185 } 186 187 static int shmem_get_pages(struct drm_i915_gem_object *obj) 188 { 189 struct drm_i915_private *i915 = to_i915(obj->base.dev); 190 struct intel_memory_region *mem = obj->mm.region; 191 struct address_space *mapping = obj->base.filp->f_mapping; 192 const unsigned long page_count = obj->base.size / PAGE_SIZE; 193 unsigned int max_segment = i915_sg_segment_size(); 194 struct sg_table *st; 195 struct sgt_iter sgt_iter; 196 struct page *page; 197 int ret; 198 199 /* 200 * Assert that the object is not currently in any GPU domain. As it 201 * wasn't in the GTT, there shouldn't be any way it could have been in 202 * a GPU cache 203 */ 204 GEM_BUG_ON(obj->read_domains & I915_GEM_GPU_DOMAINS); 205 GEM_BUG_ON(obj->write_domain & I915_GEM_GPU_DOMAINS); 206 207 rebuild_st: 208 st = kmalloc(sizeof(*st), GFP_KERNEL); 209 if (!st) 210 return -ENOMEM; 211 212 ret = shmem_sg_alloc_table(i915, st, obj->base.size, mem, mapping, 213 max_segment); 214 if (ret) 215 goto err_st; 216 217 ret = i915_gem_gtt_prepare_pages(obj, st); 218 if (ret) { 219 /* 220 * DMA remapping failed? One possible cause is that 221 * it could not reserve enough large entries, asking 222 * for PAGE_SIZE chunks instead may be helpful. 223 */ 224 if (max_segment > PAGE_SIZE) { 225 for_each_sgt_page(page, sgt_iter, st) 226 put_page(page); 227 sg_free_table(st); 228 kfree(st); 229 230 max_segment = PAGE_SIZE; 231 goto rebuild_st; 232 } else { 233 dev_warn(i915->drm.dev, 234 "Failed to DMA remap %lu pages\n", 235 page_count); 236 goto err_pages; 237 } 238 } 239 240 if (i915_gem_object_needs_bit17_swizzle(obj)) 241 i915_gem_object_do_bit_17_swizzle(obj, st); 242 243 if (i915_gem_object_can_bypass_llc(obj)) 244 obj->cache_dirty = true; 245 246 __i915_gem_object_set_pages(obj, st, i915_sg_dma_sizes(st->sgl)); 247 248 return 0; 249 250 err_pages: 251 shmem_sg_free_table(st, mapping, false, false); 252 /* 253 * shmemfs first checks if there is enough memory to allocate the page 254 * and reports ENOSPC should there be insufficient, along with the usual 255 * ENOMEM for a genuine allocation failure. 256 * 257 * We use ENOSPC in our driver to mean that we have run out of aperture 258 * space and so want to translate the error from shmemfs back to our 259 * usual understanding of ENOMEM. 260 */ 261 err_st: 262 if (ret == -ENOSPC) 263 ret = -ENOMEM; 264 265 kfree(st); 266 267 return ret; 268 } 269 270 static int 271 shmem_truncate(struct drm_i915_gem_object *obj) 272 { 273 /* 274 * Our goal here is to return as much of the memory as 275 * is possible back to the system as we are called from OOM. 276 * To do this we must instruct the shmfs to drop all of its 277 * backing pages, *now*. 278 */ 279 shmem_truncate_range(file_inode(obj->base.filp), 0, (loff_t)-1); 280 obj->mm.madv = __I915_MADV_PURGED; 281 obj->mm.pages = ERR_PTR(-EFAULT); 282 283 return 0; 284 } 285 286 void __shmem_writeback(size_t size, struct address_space *mapping) 287 { 288 struct writeback_control wbc = { 289 .sync_mode = WB_SYNC_NONE, 290 .nr_to_write = SWAP_CLUSTER_MAX, 291 .range_start = 0, 292 .range_end = LLONG_MAX, 293 .for_reclaim = 1, 294 }; 295 unsigned long i; 296 297 /* 298 * Leave mmapings intact (GTT will have been revoked on unbinding, 299 * leaving only CPU mmapings around) and add those pages to the LRU 300 * instead of invoking writeback so they are aged and paged out 301 * as normal. 302 */ 303 304 /* Begin writeback on each dirty page */ 305 for (i = 0; i < size >> PAGE_SHIFT; i++) { 306 struct page *page; 307 308 page = find_lock_page(mapping, i); 309 if (!page) 310 continue; 311 312 if (!page_mapped(page) && clear_page_dirty_for_io(page)) { 313 int ret; 314 315 SetPageReclaim(page); 316 ret = mapping->a_ops->writepage(page, &wbc); 317 if (!PageWriteback(page)) 318 ClearPageReclaim(page); 319 if (!ret) 320 goto put; 321 } 322 unlock_page(page); 323 put: 324 put_page(page); 325 } 326 } 327 328 static void 329 shmem_writeback(struct drm_i915_gem_object *obj) 330 { 331 __shmem_writeback(obj->base.size, obj->base.filp->f_mapping); 332 } 333 334 static int shmem_shrinker_release_pages(struct drm_i915_gem_object *obj, 335 bool no_gpu_wait, 336 bool writeback) 337 { 338 switch (obj->mm.madv) { 339 case I915_MADV_DONTNEED: 340 return i915_gem_object_truncate(obj); 341 case __I915_MADV_PURGED: 342 return 0; 343 } 344 345 if (writeback) 346 shmem_writeback(obj); 347 348 return 0; 349 } 350 351 void 352 __i915_gem_object_release_shmem(struct drm_i915_gem_object *obj, 353 struct sg_table *pages, 354 bool needs_clflush) 355 { 356 struct drm_i915_private *i915 = to_i915(obj->base.dev); 357 358 GEM_BUG_ON(obj->mm.madv == __I915_MADV_PURGED); 359 360 if (obj->mm.madv == I915_MADV_DONTNEED) 361 obj->mm.dirty = false; 362 363 if (needs_clflush && 364 (obj->read_domains & I915_GEM_DOMAIN_CPU) == 0 && 365 !(obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_READ)) 366 drm_clflush_sg(pages); 367 368 __start_cpu_write(obj); 369 /* 370 * On non-LLC platforms, force the flush-on-acquire if this is ever 371 * swapped-in. Our async flush path is not trust worthy enough yet(and 372 * happens in the wrong order), and with some tricks it's conceivable 373 * for userspace to change the cache-level to I915_CACHE_NONE after the 374 * pages are swapped-in, and since execbuf binds the object before doing 375 * the async flush, we have a race window. 376 */ 377 if (!HAS_LLC(i915)) 378 obj->cache_dirty = true; 379 } 380 381 void i915_gem_object_put_pages_shmem(struct drm_i915_gem_object *obj, struct sg_table *pages) 382 { 383 __i915_gem_object_release_shmem(obj, pages, true); 384 385 i915_gem_gtt_finish_pages(obj, pages); 386 387 if (i915_gem_object_needs_bit17_swizzle(obj)) 388 i915_gem_object_save_bit_17_swizzle(obj, pages); 389 390 shmem_sg_free_table(pages, file_inode(obj->base.filp)->i_mapping, 391 obj->mm.dirty, obj->mm.madv == I915_MADV_WILLNEED); 392 kfree(pages); 393 obj->mm.dirty = false; 394 } 395 396 static void 397 shmem_put_pages(struct drm_i915_gem_object *obj, struct sg_table *pages) 398 { 399 if (likely(i915_gem_object_has_struct_page(obj))) 400 i915_gem_object_put_pages_shmem(obj, pages); 401 else 402 i915_gem_object_put_pages_phys(obj, pages); 403 } 404 405 static int 406 shmem_pwrite(struct drm_i915_gem_object *obj, 407 const struct drm_i915_gem_pwrite *arg) 408 { 409 struct address_space *mapping = obj->base.filp->f_mapping; 410 char __user *user_data = u64_to_user_ptr(arg->data_ptr); 411 u64 remain, offset; 412 unsigned int pg; 413 414 /* Caller already validated user args */ 415 GEM_BUG_ON(!access_ok(user_data, arg->size)); 416 417 if (!i915_gem_object_has_struct_page(obj)) 418 return i915_gem_object_pwrite_phys(obj, arg); 419 420 /* 421 * Before we instantiate/pin the backing store for our use, we 422 * can prepopulate the shmemfs filp efficiently using a write into 423 * the pagecache. We avoid the penalty of instantiating all the 424 * pages, important if the user is just writing to a few and never 425 * uses the object on the GPU, and using a direct write into shmemfs 426 * allows it to avoid the cost of retrieving a page (either swapin 427 * or clearing-before-use) before it is overwritten. 428 */ 429 if (i915_gem_object_has_pages(obj)) 430 return -ENODEV; 431 432 if (obj->mm.madv != I915_MADV_WILLNEED) 433 return -EFAULT; 434 435 /* 436 * Before the pages are instantiated the object is treated as being 437 * in the CPU domain. The pages will be clflushed as required before 438 * use, and we can freely write into the pages directly. If userspace 439 * races pwrite with any other operation; corruption will ensue - 440 * that is userspace's prerogative! 441 */ 442 443 remain = arg->size; 444 offset = arg->offset; 445 pg = offset_in_page(offset); 446 447 do { 448 unsigned int len, unwritten; 449 struct page *page; 450 void *data, *vaddr; 451 int err; 452 char c; 453 454 len = PAGE_SIZE - pg; 455 if (len > remain) 456 len = remain; 457 458 /* Prefault the user page to reduce potential recursion */ 459 err = __get_user(c, user_data); 460 if (err) 461 return err; 462 463 err = __get_user(c, user_data + len - 1); 464 if (err) 465 return err; 466 467 err = pagecache_write_begin(obj->base.filp, mapping, 468 offset, len, 0, 469 &page, &data); 470 if (err < 0) 471 return err; 472 473 vaddr = kmap_atomic(page); 474 unwritten = __copy_from_user_inatomic(vaddr + pg, 475 user_data, 476 len); 477 kunmap_atomic(vaddr); 478 479 err = pagecache_write_end(obj->base.filp, mapping, 480 offset, len, len - unwritten, 481 page, data); 482 if (err < 0) 483 return err; 484 485 /* We don't handle -EFAULT, leave it to the caller to check */ 486 if (unwritten) 487 return -ENODEV; 488 489 remain -= len; 490 user_data += len; 491 offset += len; 492 pg = 0; 493 } while (remain); 494 495 return 0; 496 } 497 498 static int 499 shmem_pread(struct drm_i915_gem_object *obj, 500 const struct drm_i915_gem_pread *arg) 501 { 502 if (!i915_gem_object_has_struct_page(obj)) 503 return i915_gem_object_pread_phys(obj, arg); 504 505 return -ENODEV; 506 } 507 508 static void shmem_release(struct drm_i915_gem_object *obj) 509 { 510 if (i915_gem_object_has_struct_page(obj)) 511 i915_gem_object_release_memory_region(obj); 512 513 fput(obj->base.filp); 514 } 515 516 const struct drm_i915_gem_object_ops i915_gem_shmem_ops = { 517 .name = "i915_gem_object_shmem", 518 .flags = I915_GEM_OBJECT_IS_SHRINKABLE, 519 520 .get_pages = shmem_get_pages, 521 .put_pages = shmem_put_pages, 522 .truncate = shmem_truncate, 523 .shrinker_release_pages = shmem_shrinker_release_pages, 524 525 .pwrite = shmem_pwrite, 526 .pread = shmem_pread, 527 528 .release = shmem_release, 529 }; 530 531 static int __create_shmem(struct drm_i915_private *i915, 532 struct drm_gem_object *obj, 533 resource_size_t size) 534 { 535 unsigned long flags = VM_NORESERVE; 536 struct file *filp; 537 538 drm_gem_private_object_init(&i915->drm, obj, size); 539 540 if (i915->mm.gemfs) 541 filp = shmem_file_setup_with_mnt(i915->mm.gemfs, "i915", size, 542 flags); 543 else 544 filp = shmem_file_setup("i915", size, flags); 545 if (IS_ERR(filp)) 546 return PTR_ERR(filp); 547 548 obj->filp = filp; 549 return 0; 550 } 551 552 static int shmem_object_init(struct intel_memory_region *mem, 553 struct drm_i915_gem_object *obj, 554 resource_size_t size, 555 resource_size_t page_size, 556 unsigned int flags) 557 { 558 static struct lock_class_key lock_class; 559 struct drm_i915_private *i915 = mem->i915; 560 struct address_space *mapping; 561 unsigned int cache_level; 562 gfp_t mask; 563 int ret; 564 565 ret = __create_shmem(i915, &obj->base, size); 566 if (ret) 567 return ret; 568 569 mask = GFP_HIGHUSER | __GFP_RECLAIMABLE; 570 if (IS_I965GM(i915) || IS_I965G(i915)) { 571 /* 965gm cannot relocate objects above 4GiB. */ 572 mask &= ~__GFP_HIGHMEM; 573 mask |= __GFP_DMA32; 574 } 575 576 mapping = obj->base.filp->f_mapping; 577 mapping_set_gfp_mask(mapping, mask); 578 GEM_BUG_ON(!(mapping_gfp_mask(mapping) & __GFP_RECLAIM)); 579 580 i915_gem_object_init(obj, &i915_gem_shmem_ops, &lock_class, 0); 581 obj->mem_flags |= I915_BO_FLAG_STRUCT_PAGE; 582 obj->write_domain = I915_GEM_DOMAIN_CPU; 583 obj->read_domains = I915_GEM_DOMAIN_CPU; 584 585 if (HAS_LLC(i915)) 586 /* On some devices, we can have the GPU use the LLC (the CPU 587 * cache) for about a 10% performance improvement 588 * compared to uncached. Graphics requests other than 589 * display scanout are coherent with the CPU in 590 * accessing this cache. This means in this mode we 591 * don't need to clflush on the CPU side, and on the 592 * GPU side we only need to flush internal caches to 593 * get data visible to the CPU. 594 * 595 * However, we maintain the display planes as UC, and so 596 * need to rebind when first used as such. 597 */ 598 cache_level = I915_CACHE_LLC; 599 else 600 cache_level = I915_CACHE_NONE; 601 602 i915_gem_object_set_cache_coherency(obj, cache_level); 603 604 i915_gem_object_init_memory_region(obj, mem); 605 606 return 0; 607 } 608 609 struct drm_i915_gem_object * 610 i915_gem_object_create_shmem(struct drm_i915_private *i915, 611 resource_size_t size) 612 { 613 return i915_gem_object_create_region(i915->mm.regions[INTEL_REGION_SMEM], 614 size, 0, 0); 615 } 616 617 /* Allocate a new GEM object and fill it with the supplied data */ 618 struct drm_i915_gem_object * 619 i915_gem_object_create_shmem_from_data(struct drm_i915_private *dev_priv, 620 const void *data, resource_size_t size) 621 { 622 struct drm_i915_gem_object *obj; 623 struct file *file; 624 resource_size_t offset; 625 int err; 626 627 GEM_WARN_ON(IS_DGFX(dev_priv)); 628 obj = i915_gem_object_create_shmem(dev_priv, round_up(size, PAGE_SIZE)); 629 if (IS_ERR(obj)) 630 return obj; 631 632 GEM_BUG_ON(obj->write_domain != I915_GEM_DOMAIN_CPU); 633 634 file = obj->base.filp; 635 offset = 0; 636 do { 637 unsigned int len = min_t(typeof(size), size, PAGE_SIZE); 638 struct page *page; 639 void *pgdata, *vaddr; 640 641 err = pagecache_write_begin(file, file->f_mapping, 642 offset, len, 0, 643 &page, &pgdata); 644 if (err < 0) 645 goto fail; 646 647 vaddr = kmap(page); 648 memcpy(vaddr, data, len); 649 kunmap(page); 650 651 err = pagecache_write_end(file, file->f_mapping, 652 offset, len, len, 653 page, pgdata); 654 if (err < 0) 655 goto fail; 656 657 size -= len; 658 data += len; 659 offset += len; 660 } while (size); 661 662 return obj; 663 664 fail: 665 i915_gem_object_put(obj); 666 return ERR_PTR(err); 667 } 668 669 static int init_shmem(struct intel_memory_region *mem) 670 { 671 int err; 672 673 err = i915_gemfs_init(mem->i915); 674 if (err) { 675 DRM_NOTE("Unable to create a private tmpfs mount, hugepage support will be disabled(%d).\n", 676 err); 677 } 678 679 intel_memory_region_set_name(mem, "system"); 680 681 return 0; /* Don't error, we can simply fallback to the kernel mnt */ 682 } 683 684 static int release_shmem(struct intel_memory_region *mem) 685 { 686 i915_gemfs_fini(mem->i915); 687 return 0; 688 } 689 690 static const struct intel_memory_region_ops shmem_region_ops = { 691 .init = init_shmem, 692 .release = release_shmem, 693 .init_object = shmem_object_init, 694 }; 695 696 struct intel_memory_region *i915_gem_shmem_setup(struct drm_i915_private *i915, 697 u16 type, u16 instance) 698 { 699 return intel_memory_region_create(i915, 0, 700 totalram_pages() << PAGE_SHIFT, 701 PAGE_SIZE, 0, 702 type, instance, 703 &shmem_region_ops); 704 } 705 706 bool i915_gem_object_is_shmem(const struct drm_i915_gem_object *obj) 707 { 708 return obj->ops == &i915_gem_shmem_ops; 709 } 710