2 * SPDX-License-Identifier: MIT
4 * Copyright © 2014-2016 Intel Corporation
7 #include <linux/pagevec.h>
8 #include <linux/shmem_fs.h>
9 #include <linux/swap.h>
11 #include <drm/drm_cache.h>
13 #include "gem/i915_gem_region.h"
15 #include "i915_gem_object.h"
16 #include "i915_gem_tiling.h"
17 #include "i915_gemfs.h"
18 #include "i915_scatterlist.h"
19 #include "i915_trace.h"
22 * Move pages to appropriate lru and release the pagevec, decrementing the
23 * ref count of those pages.
25 static void check_release_pagevec(struct pagevec *pvec)
27 check_move_unevictable_pages(pvec);
28 __pagevec_release(pvec);
32 void shmem_sg_free_table(struct sg_table *st, struct address_space *mapping,
33 bool dirty, bool backup)
35 struct sgt_iter sgt_iter;
39 mapping_clear_unevictable(mapping);
42 for_each_sgt_page(page, sgt_iter, st) {
47 mark_page_accessed(page);
49 if (!pagevec_add(&pvec, page))
50 check_release_pagevec(&pvec);
52 if (pagevec_count(&pvec))
53 check_release_pagevec(&pvec);
58 int shmem_sg_alloc_table(struct drm_i915_private *i915, struct sg_table *st,
59 size_t size, struct intel_memory_region *mr,
60 struct address_space *mapping,
61 unsigned int max_segment)
63 const unsigned long page_count = size / PAGE_SIZE;
65 struct scatterlist *sg;
67 unsigned long last_pfn = 0; /* suppress gcc warning */
72 * If there's no chance of allocating enough pages for the whole
75 if (size > resource_size(&mr->region))
78 if (sg_alloc_table(st, page_count, GFP_KERNEL))
82 * Get the list of pages out of our struct file. They'll be pinned
83 * at this point until we release them.
85 * Fail silently without starting the shrinker
87 mapping_set_unevictable(mapping);
88 noreclaim = mapping_gfp_constraint(mapping, ~__GFP_RECLAIM);
89 noreclaim |= __GFP_NORETRY | __GFP_NOWARN;
93 for (i = 0; i < page_count; i++) {
94 const unsigned int shrink[] = {
95 I915_SHRINK_BOUND | I915_SHRINK_UNBOUND,
98 gfp_t gfp = noreclaim;
102 page = shmem_read_mapping_page_gfp(mapping, i, gfp);
111 i915_gem_shrink(NULL, i915, 2 * page_count, NULL, *s++);
114 * We've tried hard to allocate the memory by reaping
115 * our own buffer, now let the real VM do its job and
116 * go down in flames if truly OOM.
118 * However, since graphics tend to be disposable,
119 * defer the oom here by reporting the ENOMEM back
123 /* reclaim and warn, but no oom */
124 gfp = mapping_gfp_mask(mapping);
127 * Our bo are always dirty and so we require
128 * kswapd to reclaim our pages (direct reclaim
129 * does not effectively begin pageout of our
130 * buffers on its own). However, direct reclaim
131 * only waits for kswapd when under allocation
132 * congestion. So as a result __GFP_RECLAIM is
133 * unreliable and fails to actually reclaim our
134 * dirty pages -- unless you try over and over
135 * again with !__GFP_NORETRY. However, we still
136 * want to fail this allocation rather than
137 * trigger the out-of-memory killer and for
138 * this we want __GFP_RETRY_MAYFAIL.
140 gfp |= __GFP_RETRY_MAYFAIL;
145 sg->length >= max_segment ||
146 page_to_pfn(page) != last_pfn + 1) {
151 sg_set_page(sg, page, PAGE_SIZE, 0);
153 sg->length += PAGE_SIZE;
155 last_pfn = page_to_pfn(page);
157 /* Check that the i965g/gm workaround works. */
158 GEM_BUG_ON(gfp & __GFP_DMA32 && last_pfn >= 0x00100000UL);
160 if (sg) /* loop terminated early; short sg table */
163 /* Trim unused sg entries to avoid wasting memory. */
170 shmem_sg_free_table(st, mapping, false, false);
172 mapping_clear_unevictable(mapping);
177 * shmemfs first checks if there is enough memory to allocate the page
178 * and reports ENOSPC should there be insufficient, along with the usual
179 * ENOMEM for a genuine allocation failure.
181 * We use ENOSPC in our driver to mean that we have run out of aperture
182 * space and so want to translate the error from shmemfs back to our
183 * usual understanding of ENOMEM.
191 static int shmem_get_pages(struct drm_i915_gem_object *obj)
193 struct drm_i915_private *i915 = to_i915(obj->base.dev);
194 struct intel_memory_region *mem = obj->mm.region;
195 struct address_space *mapping = obj->base.filp->f_mapping;
196 const unsigned long page_count = obj->base.size / PAGE_SIZE;
197 unsigned int max_segment = i915_sg_segment_size();
199 struct sgt_iter sgt_iter;
204 * Assert that the object is not currently in any GPU domain. As it
205 * wasn't in the GTT, there shouldn't be any way it could have been in
208 GEM_BUG_ON(obj->read_domains & I915_GEM_GPU_DOMAINS);
209 GEM_BUG_ON(obj->write_domain & I915_GEM_GPU_DOMAINS);
212 st = kmalloc(sizeof(*st), GFP_KERNEL);
216 ret = shmem_sg_alloc_table(i915, st, obj->base.size, mem, mapping,
221 ret = i915_gem_gtt_prepare_pages(obj, st);
224 * DMA remapping failed? One possible cause is that
225 * it could not reserve enough large entries, asking
226 * for PAGE_SIZE chunks instead may be helpful.
228 if (max_segment > PAGE_SIZE) {
229 for_each_sgt_page(page, sgt_iter, st)
234 max_segment = PAGE_SIZE;
237 dev_warn(i915->drm.dev,
238 "Failed to DMA remap %lu pages\n",
244 if (i915_gem_object_needs_bit17_swizzle(obj))
245 i915_gem_object_do_bit_17_swizzle(obj, st);
247 if (i915_gem_object_can_bypass_llc(obj))
248 obj->cache_dirty = true;
250 __i915_gem_object_set_pages(obj, st, i915_sg_dma_sizes(st->sgl));
255 shmem_sg_free_table(st, mapping, false, false);
257 * shmemfs first checks if there is enough memory to allocate the page
258 * and reports ENOSPC should there be insufficient, along with the usual
259 * ENOMEM for a genuine allocation failure.
261 * We use ENOSPC in our driver to mean that we have run out of aperture
262 * space and so want to translate the error from shmemfs back to our
263 * usual understanding of ENOMEM.
275 shmem_truncate(struct drm_i915_gem_object *obj)
278 * Our goal here is to return as much of the memory as
279 * is possible back to the system as we are called from OOM.
280 * To do this we must instruct the shmfs to drop all of its
281 * backing pages, *now*.
283 shmem_truncate_range(file_inode(obj->base.filp), 0, (loff_t)-1);
284 obj->mm.madv = __I915_MADV_PURGED;
285 obj->mm.pages = ERR_PTR(-EFAULT);
290 void __shmem_writeback(size_t size, struct address_space *mapping)
292 struct writeback_control wbc = {
293 .sync_mode = WB_SYNC_NONE,
294 .nr_to_write = SWAP_CLUSTER_MAX,
296 .range_end = LLONG_MAX,
302 * Leave mmapings intact (GTT will have been revoked on unbinding,
303 * leaving only CPU mmapings around) and add those pages to the LRU
304 * instead of invoking writeback so they are aged and paged out
308 /* Begin writeback on each dirty page */
309 for (i = 0; i < size >> PAGE_SHIFT; i++) {
312 page = find_lock_page(mapping, i);
316 if (!page_mapped(page) && clear_page_dirty_for_io(page)) {
319 SetPageReclaim(page);
320 ret = mapping->a_ops->writepage(page, &wbc);
321 if (!PageWriteback(page))
322 ClearPageReclaim(page);
333 shmem_writeback(struct drm_i915_gem_object *obj)
335 __shmem_writeback(obj->base.size, obj->base.filp->f_mapping);
338 static int shmem_shrink(struct drm_i915_gem_object *obj, unsigned int flags)
340 switch (obj->mm.madv) {
341 case I915_MADV_DONTNEED:
342 return i915_gem_object_truncate(obj);
343 case __I915_MADV_PURGED:
347 if (flags & I915_GEM_OBJECT_SHRINK_WRITEBACK)
348 shmem_writeback(obj);
354 __i915_gem_object_release_shmem(struct drm_i915_gem_object *obj,
355 struct sg_table *pages,
358 struct drm_i915_private *i915 = to_i915(obj->base.dev);
360 GEM_BUG_ON(obj->mm.madv == __I915_MADV_PURGED);
362 if (obj->mm.madv == I915_MADV_DONTNEED)
363 obj->mm.dirty = false;
366 (obj->read_domains & I915_GEM_DOMAIN_CPU) == 0 &&
367 !(obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_READ))
368 drm_clflush_sg(pages);
370 __start_cpu_write(obj);
372 * On non-LLC platforms, force the flush-on-acquire if this is ever
373 * swapped-in. Our async flush path is not trust worthy enough yet(and
374 * happens in the wrong order), and with some tricks it's conceivable
375 * for userspace to change the cache-level to I915_CACHE_NONE after the
376 * pages are swapped-in, and since execbuf binds the object before doing
377 * the async flush, we have a race window.
380 obj->cache_dirty = true;
383 void i915_gem_object_put_pages_shmem(struct drm_i915_gem_object *obj, struct sg_table *pages)
385 __i915_gem_object_release_shmem(obj, pages, true);
387 i915_gem_gtt_finish_pages(obj, pages);
389 if (i915_gem_object_needs_bit17_swizzle(obj))
390 i915_gem_object_save_bit_17_swizzle(obj, pages);
392 shmem_sg_free_table(pages, file_inode(obj->base.filp)->i_mapping,
393 obj->mm.dirty, obj->mm.madv == I915_MADV_WILLNEED);
395 obj->mm.dirty = false;
399 shmem_put_pages(struct drm_i915_gem_object *obj, struct sg_table *pages)
401 if (likely(i915_gem_object_has_struct_page(obj)))
402 i915_gem_object_put_pages_shmem(obj, pages);
404 i915_gem_object_put_pages_phys(obj, pages);
408 shmem_pwrite(struct drm_i915_gem_object *obj,
409 const struct drm_i915_gem_pwrite *arg)
411 struct address_space *mapping = obj->base.filp->f_mapping;
412 const struct address_space_operations *aops = mapping->a_ops;
413 char __user *user_data = u64_to_user_ptr(arg->data_ptr);
417 /* Caller already validated user args */
418 GEM_BUG_ON(!access_ok(user_data, arg->size));
420 if (!i915_gem_object_has_struct_page(obj))
421 return i915_gem_object_pwrite_phys(obj, arg);
424 * Before we instantiate/pin the backing store for our use, we
425 * can prepopulate the shmemfs filp efficiently using a write into
426 * the pagecache. We avoid the penalty of instantiating all the
427 * pages, important if the user is just writing to a few and never
428 * uses the object on the GPU, and using a direct write into shmemfs
429 * allows it to avoid the cost of retrieving a page (either swapin
430 * or clearing-before-use) before it is overwritten.
432 if (i915_gem_object_has_pages(obj))
435 if (obj->mm.madv != I915_MADV_WILLNEED)
439 * Before the pages are instantiated the object is treated as being
440 * in the CPU domain. The pages will be clflushed as required before
441 * use, and we can freely write into the pages directly. If userspace
442 * races pwrite with any other operation; corruption will ensue -
443 * that is userspace's prerogative!
447 offset = arg->offset;
448 pg = offset_in_page(offset);
451 unsigned int len, unwritten;
457 len = PAGE_SIZE - pg;
461 /* Prefault the user page to reduce potential recursion */
462 err = __get_user(c, user_data);
466 err = __get_user(c, user_data + len - 1);
470 err = aops->write_begin(obj->base.filp, mapping, offset, len,
475 vaddr = kmap_atomic(page);
476 unwritten = __copy_from_user_inatomic(vaddr + pg,
479 kunmap_atomic(vaddr);
481 err = aops->write_end(obj->base.filp, mapping, offset, len,
482 len - unwritten, page, data);
486 /* We don't handle -EFAULT, leave it to the caller to check */
500 shmem_pread(struct drm_i915_gem_object *obj,
501 const struct drm_i915_gem_pread *arg)
503 if (!i915_gem_object_has_struct_page(obj))
504 return i915_gem_object_pread_phys(obj, arg);
509 static void shmem_release(struct drm_i915_gem_object *obj)
511 if (i915_gem_object_has_struct_page(obj))
512 i915_gem_object_release_memory_region(obj);
514 fput(obj->base.filp);
517 const struct drm_i915_gem_object_ops i915_gem_shmem_ops = {
518 .name = "i915_gem_object_shmem",
519 .flags = I915_GEM_OBJECT_IS_SHRINKABLE,
521 .get_pages = shmem_get_pages,
522 .put_pages = shmem_put_pages,
523 .truncate = shmem_truncate,
524 .shrink = shmem_shrink,
526 .pwrite = shmem_pwrite,
527 .pread = shmem_pread,
529 .release = shmem_release,
532 static int __create_shmem(struct drm_i915_private *i915,
533 struct drm_gem_object *obj,
534 resource_size_t size)
536 unsigned long flags = VM_NORESERVE;
539 drm_gem_private_object_init(&i915->drm, obj, size);
542 filp = shmem_file_setup_with_mnt(i915->mm.gemfs, "i915", size,
545 filp = shmem_file_setup("i915", size, flags);
547 return PTR_ERR(filp);
553 static int shmem_object_init(struct intel_memory_region *mem,
554 struct drm_i915_gem_object *obj,
555 resource_size_t offset,
556 resource_size_t size,
557 resource_size_t page_size,
560 static struct lock_class_key lock_class;
561 struct drm_i915_private *i915 = mem->i915;
562 struct address_space *mapping;
563 unsigned int cache_level;
567 ret = __create_shmem(i915, &obj->base, size);
571 mask = GFP_HIGHUSER | __GFP_RECLAIMABLE;
572 if (IS_I965GM(i915) || IS_I965G(i915)) {
573 /* 965gm cannot relocate objects above 4GiB. */
574 mask &= ~__GFP_HIGHMEM;
578 mapping = obj->base.filp->f_mapping;
579 mapping_set_gfp_mask(mapping, mask);
580 GEM_BUG_ON(!(mapping_gfp_mask(mapping) & __GFP_RECLAIM));
582 i915_gem_object_init(obj, &i915_gem_shmem_ops, &lock_class, 0);
583 obj->mem_flags |= I915_BO_FLAG_STRUCT_PAGE;
584 obj->write_domain = I915_GEM_DOMAIN_CPU;
585 obj->read_domains = I915_GEM_DOMAIN_CPU;
588 /* On some devices, we can have the GPU use the LLC (the CPU
589 * cache) for about a 10% performance improvement
590 * compared to uncached. Graphics requests other than
591 * display scanout are coherent with the CPU in
592 * accessing this cache. This means in this mode we
593 * don't need to clflush on the CPU side, and on the
594 * GPU side we only need to flush internal caches to
595 * get data visible to the CPU.
597 * However, we maintain the display planes as UC, and so
598 * need to rebind when first used as such.
600 cache_level = I915_CACHE_LLC;
602 cache_level = I915_CACHE_NONE;
604 i915_gem_object_set_cache_coherency(obj, cache_level);
606 i915_gem_object_init_memory_region(obj, mem);
611 struct drm_i915_gem_object *
612 i915_gem_object_create_shmem(struct drm_i915_private *i915,
613 resource_size_t size)
615 return i915_gem_object_create_region(i915->mm.regions[INTEL_REGION_SMEM],
619 /* Allocate a new GEM object and fill it with the supplied data */
620 struct drm_i915_gem_object *
621 i915_gem_object_create_shmem_from_data(struct drm_i915_private *dev_priv,
622 const void *data, resource_size_t size)
624 struct drm_i915_gem_object *obj;
626 const struct address_space_operations *aops;
627 resource_size_t offset;
630 GEM_WARN_ON(IS_DGFX(dev_priv));
631 obj = i915_gem_object_create_shmem(dev_priv, round_up(size, PAGE_SIZE));
635 GEM_BUG_ON(obj->write_domain != I915_GEM_DOMAIN_CPU);
637 file = obj->base.filp;
638 aops = file->f_mapping->a_ops;
641 unsigned int len = min_t(typeof(size), size, PAGE_SIZE);
643 void *pgdata, *vaddr;
645 err = aops->write_begin(file, file->f_mapping, offset, len,
651 memcpy(vaddr, data, len);
654 err = aops->write_end(file, file->f_mapping, offset, len, len,
667 i915_gem_object_put(obj);
671 static int init_shmem(struct intel_memory_region *mem)
675 err = i915_gemfs_init(mem->i915);
677 DRM_NOTE("Unable to create a private tmpfs mount, hugepage support will be disabled(%d).\n",
681 intel_memory_region_set_name(mem, "system");
683 return 0; /* Don't error, we can simply fallback to the kernel mnt */
686 static int release_shmem(struct intel_memory_region *mem)
688 i915_gemfs_fini(mem->i915);
692 static const struct intel_memory_region_ops shmem_region_ops = {
694 .release = release_shmem,
695 .init_object = shmem_object_init,
698 struct intel_memory_region *i915_gem_shmem_setup(struct drm_i915_private *i915,
699 u16 type, u16 instance)
701 return intel_memory_region_create(i915, 0,
702 totalram_pages() << PAGE_SHIFT,
708 bool i915_gem_object_is_shmem(const struct drm_i915_gem_object *obj)
710 return obj->ops == &i915_gem_shmem_ops;