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