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