2 * SPDX-License-Identifier: MIT
4 * Copyright © 2012-2014 Intel Corporation
6 * Based on amdgpu_mn, which bears the following notice:
8 * Copyright 2014 Advanced Micro Devices, Inc.
11 * Permission is hereby granted, free of charge, to any person obtaining a
12 * copy of this software and associated documentation files (the
13 * "Software"), to deal in the Software without restriction, including
14 * without limitation the rights to use, copy, modify, merge, publish,
15 * distribute, sub license, and/or sell copies of the Software, and to
16 * permit persons to whom the Software is furnished to do so, subject to
17 * the following conditions:
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
22 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
23 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
24 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
25 * USE OR OTHER DEALINGS IN THE SOFTWARE.
27 * The above copyright notice and this permission notice (including the
28 * next paragraph) shall be included in all copies or substantial portions
34 * Christian König <christian.koenig@amd.com>
37 #include <linux/mmu_context.h>
38 #include <linux/mempolicy.h>
39 #include <linux/swap.h>
40 #include <linux/sched/mm.h>
43 #include "i915_gem_ioctls.h"
44 #include "i915_gem_object.h"
45 #include "i915_scatterlist.h"
47 #ifdef CONFIG_MMU_NOTIFIER
50 * i915_gem_userptr_invalidate - callback to notify about mm change
52 * @mni: the range (mm) is about to update
53 * @range: details on the invalidation
54 * @cur_seq: Value to pass to mmu_interval_set_seq()
56 * Block for operations on BOs to finish and mark pages as accessed and
59 static bool i915_gem_userptr_invalidate(struct mmu_interval_notifier *mni,
60 const struct mmu_notifier_range *range,
61 unsigned long cur_seq)
63 struct drm_i915_gem_object *obj = container_of(mni, struct drm_i915_gem_object, userptr.notifier);
64 struct drm_i915_private *i915 = to_i915(obj->base.dev);
67 if (!mmu_notifier_range_blockable(range))
70 write_lock(&i915->mm.notifier_lock);
72 mmu_interval_set_seq(mni, cur_seq);
74 write_unlock(&i915->mm.notifier_lock);
77 * We don't wait when the process is exiting. This is valid
78 * because the object will be cleaned up anyway.
80 * This is also temporarily required as a hack, because we
81 * cannot currently force non-consistent batch buffers to preempt
82 * and reschedule by waiting on it, hanging processes on exit.
84 if (current->flags & PF_EXITING)
87 /* we will unbind on next submission, still have userptr pins */
88 r = dma_resv_wait_timeout(obj->base.resv, true, false,
89 MAX_SCHEDULE_TIMEOUT);
91 drm_err(&i915->drm, "(%ld) failed to wait for idle\n", r);
96 static const struct mmu_interval_notifier_ops i915_gem_userptr_notifier_ops = {
97 .invalidate = i915_gem_userptr_invalidate,
101 i915_gem_userptr_init__mmu_notifier(struct drm_i915_gem_object *obj)
103 return mmu_interval_notifier_insert(&obj->userptr.notifier, current->mm,
104 obj->userptr.ptr, obj->base.size,
105 &i915_gem_userptr_notifier_ops);
108 static void i915_gem_object_userptr_drop_ref(struct drm_i915_gem_object *obj)
110 struct page **pvec = NULL;
112 assert_object_held_shared(obj);
114 if (!--obj->userptr.page_ref) {
115 pvec = obj->userptr.pvec;
116 obj->userptr.pvec = NULL;
118 GEM_BUG_ON(obj->userptr.page_ref < 0);
121 const unsigned long num_pages = obj->base.size >> PAGE_SHIFT;
123 unpin_user_pages(pvec, num_pages);
128 static int i915_gem_userptr_get_pages(struct drm_i915_gem_object *obj)
130 const unsigned long num_pages = obj->base.size >> PAGE_SHIFT;
131 unsigned int max_segment = i915_sg_segment_size();
133 unsigned int sg_page_sizes;
134 struct scatterlist *sg;
138 st = kmalloc(sizeof(*st), GFP_KERNEL);
142 if (!obj->userptr.page_ref) {
147 obj->userptr.page_ref++;
148 pvec = obj->userptr.pvec;
151 sg = __sg_alloc_table_from_pages(st, pvec, num_pages, 0,
152 num_pages << PAGE_SHIFT, max_segment,
153 NULL, 0, GFP_KERNEL);
159 ret = i915_gem_gtt_prepare_pages(obj, st);
163 if (max_segment > PAGE_SIZE) {
164 max_segment = PAGE_SIZE;
171 sg_page_sizes = i915_sg_dma_sizes(st->sgl);
173 __i915_gem_object_set_pages(obj, st, sg_page_sizes);
178 i915_gem_object_userptr_drop_ref(obj);
185 i915_gem_userptr_put_pages(struct drm_i915_gem_object *obj,
186 struct sg_table *pages)
188 struct sgt_iter sgt_iter;
194 __i915_gem_object_release_shmem(obj, pages, true);
195 i915_gem_gtt_finish_pages(obj, pages);
198 * We always mark objects as dirty when they are used by the GPU,
199 * just in case. However, if we set the vma as being read-only we know
200 * that the object will never have been written to.
202 if (i915_gem_object_is_readonly(obj))
203 obj->mm.dirty = false;
205 for_each_sgt_page(page, sgt_iter, pages) {
206 if (obj->mm.dirty && trylock_page(page)) {
208 * As this may not be anonymous memory (e.g. shmem)
209 * but exist on a real mapping, we have to lock
210 * the page in order to dirty it -- holding
211 * the page reference is not sufficient to
212 * prevent the inode from being truncated.
213 * Play safe and take the lock.
217 * The mmu-notifier can be invalidated for a
218 * migrate_page, that is alreadying holding the lock
219 * on the page. Such a try_to_unmap() will result
220 * in us calling put_pages() and so recursively try
221 * to lock the page. We avoid that deadlock with
222 * a trylock_page() and in exchange we risk missing
223 * some page dirtying.
225 set_page_dirty(page);
229 mark_page_accessed(page);
231 obj->mm.dirty = false;
233 sg_free_table(pages);
236 i915_gem_object_userptr_drop_ref(obj);
239 static int i915_gem_object_userptr_unbind(struct drm_i915_gem_object *obj)
241 struct sg_table *pages;
244 err = i915_gem_object_unbind(obj, I915_GEM_OBJECT_UNBIND_ACTIVE);
248 if (GEM_WARN_ON(i915_gem_object_has_pinned_pages(obj)))
251 assert_object_held(obj);
253 pages = __i915_gem_object_unset_pages(obj);
254 if (!IS_ERR_OR_NULL(pages))
255 i915_gem_userptr_put_pages(obj, pages);
260 int i915_gem_object_userptr_submit_init(struct drm_i915_gem_object *obj)
262 const unsigned long num_pages = obj->base.size >> PAGE_SHIFT;
264 unsigned int gup_flags = 0;
265 unsigned long notifier_seq;
268 if (obj->userptr.notifier.mm != current->mm)
271 notifier_seq = mmu_interval_read_begin(&obj->userptr.notifier);
273 ret = i915_gem_object_lock_interruptible(obj, NULL);
277 if (notifier_seq == obj->userptr.notifier_seq && obj->userptr.pvec) {
278 i915_gem_object_unlock(obj);
282 ret = i915_gem_object_userptr_unbind(obj);
283 i915_gem_object_unlock(obj);
287 pvec = kvmalloc_array(num_pages, sizeof(struct page *), GFP_KERNEL);
291 if (!i915_gem_object_is_readonly(obj))
292 gup_flags |= FOLL_WRITE;
295 while (pinned < num_pages) {
296 ret = pin_user_pages_fast(obj->userptr.ptr + pinned * PAGE_SIZE,
297 num_pages - pinned, gup_flags,
306 ret = i915_gem_object_lock_interruptible(obj, NULL);
310 if (mmu_interval_read_retry(&obj->userptr.notifier,
311 !obj->userptr.page_ref ? notifier_seq :
312 obj->userptr.notifier_seq)) {
317 if (!obj->userptr.page_ref++) {
318 obj->userptr.pvec = pvec;
319 obj->userptr.notifier_seq = notifier_seq;
321 ret = ____i915_gem_object_get_pages(obj);
324 obj->userptr.page_ref--;
327 i915_gem_object_unlock(obj);
331 unpin_user_pages(pvec, pinned);
338 int i915_gem_object_userptr_submit_done(struct drm_i915_gem_object *obj)
340 if (mmu_interval_read_retry(&obj->userptr.notifier,
341 obj->userptr.notifier_seq)) {
342 /* We collided with the mmu notifier, need to retry */
350 int i915_gem_object_userptr_validate(struct drm_i915_gem_object *obj)
354 err = i915_gem_object_userptr_submit_init(obj);
358 err = i915_gem_object_lock_interruptible(obj, NULL);
361 * Since we only check validity, not use the pages,
362 * it doesn't matter if we collide with the mmu notifier,
363 * and -EAGAIN handling is not required.
365 err = i915_gem_object_pin_pages(obj);
367 i915_gem_object_unpin_pages(obj);
369 i915_gem_object_unlock(obj);
376 i915_gem_userptr_release(struct drm_i915_gem_object *obj)
378 GEM_WARN_ON(obj->userptr.page_ref);
380 mmu_interval_notifier_remove(&obj->userptr.notifier);
381 obj->userptr.notifier.mm = NULL;
385 i915_gem_userptr_dmabuf_export(struct drm_i915_gem_object *obj)
387 drm_dbg(obj->base.dev, "Exporting userptr no longer allowed\n");
393 i915_gem_userptr_pwrite(struct drm_i915_gem_object *obj,
394 const struct drm_i915_gem_pwrite *args)
396 drm_dbg(obj->base.dev, "pwrite to userptr no longer allowed\n");
402 i915_gem_userptr_pread(struct drm_i915_gem_object *obj,
403 const struct drm_i915_gem_pread *args)
405 drm_dbg(obj->base.dev, "pread from userptr no longer allowed\n");
410 static const struct drm_i915_gem_object_ops i915_gem_userptr_ops = {
411 .name = "i915_gem_object_userptr",
412 .flags = I915_GEM_OBJECT_IS_SHRINKABLE |
413 I915_GEM_OBJECT_NO_MMAP |
414 I915_GEM_OBJECT_IS_PROXY,
415 .get_pages = i915_gem_userptr_get_pages,
416 .put_pages = i915_gem_userptr_put_pages,
417 .dmabuf_export = i915_gem_userptr_dmabuf_export,
418 .pwrite = i915_gem_userptr_pwrite,
419 .pread = i915_gem_userptr_pread,
420 .release = i915_gem_userptr_release,
426 probe_range(struct mm_struct *mm, unsigned long addr, unsigned long len)
428 const unsigned long end = addr + len;
429 struct vm_area_struct *vma;
433 for (vma = find_vma(mm, addr); vma; vma = vma->vm_next) {
434 /* Check for holes, note that we also update the addr below */
435 if (vma->vm_start > addr)
438 if (vma->vm_flags & (VM_PFNMAP | VM_MIXEDMAP))
441 if (vma->vm_end >= end) {
448 mmap_read_unlock(mm);
454 * Creates a new mm object that wraps some normal memory from the process
455 * context - user memory.
457 * We impose several restrictions upon the memory being mapped
459 * 1. It must be page aligned (both start/end addresses, i.e ptr and size).
460 * 2. It must be normal system memory, not a pointer into another map of IO
461 * space (e.g. it must not be a GTT mmapping of another object).
462 * 3. We only allow a bo as large as we could in theory map into the GTT,
463 * that is we limit the size to the total size of the GTT.
464 * 4. The bo is marked as being snoopable. The backing pages are left
465 * accessible directly by the CPU, but reads and writes by the GPU may
466 * incur the cost of a snoop (unless you have an LLC architecture).
468 * Synchronisation between multiple users and the GPU is left to userspace
469 * through the normal set-domain-ioctl. The kernel will enforce that the
470 * GPU relinquishes the VMA before it is returned back to the system
471 * i.e. upon free(), munmap() or process termination. However, the userspace
472 * malloc() library may not immediately relinquish the VMA after free() and
473 * instead reuse it whilst the GPU is still reading and writing to the VMA.
476 * Also note, that the object created here is not currently a "first class"
477 * object, in that several ioctls are banned. These are the CPU access
478 * ioctls: mmap(), pwrite and pread. In practice, you are expected to use
479 * direct access via your pointer rather than use those ioctls. Another
480 * restriction is that we do not allow userptr surfaces to be pinned to the
481 * hardware and so we reject any attempt to create a framebuffer out of a
484 * If you think this is a good interface to use to pass GPU memory between
485 * drivers, please use dma-buf instead. In fact, wherever possible use
489 i915_gem_userptr_ioctl(struct drm_device *dev,
491 struct drm_file *file)
493 static struct lock_class_key __maybe_unused lock_class;
494 struct drm_i915_private *dev_priv = to_i915(dev);
495 struct drm_i915_gem_userptr *args = data;
496 struct drm_i915_gem_object __maybe_unused *obj;
497 int __maybe_unused ret;
498 u32 __maybe_unused handle;
500 if (!HAS_LLC(dev_priv) && !HAS_SNOOP(dev_priv)) {
501 /* We cannot support coherent userptr objects on hw without
502 * LLC and broken snooping.
507 if (args->flags & ~(I915_USERPTR_READ_ONLY |
508 I915_USERPTR_UNSYNCHRONIZED |
512 if (i915_gem_object_size_2big(args->user_size))
515 if (!args->user_size)
518 if (offset_in_page(args->user_ptr | args->user_size))
521 if (!access_ok((char __user *)(unsigned long)args->user_ptr, args->user_size))
524 if (args->flags & I915_USERPTR_UNSYNCHRONIZED)
527 if (args->flags & I915_USERPTR_READ_ONLY) {
529 * On almost all of the older hw, we cannot tell the GPU that
530 * a page is readonly.
532 if (!dev_priv->gt.vm->has_read_only)
536 if (args->flags & I915_USERPTR_PROBE) {
538 * Check that the range pointed to represents real struct
539 * pages and not iomappings (at this moment in time!)
541 ret = probe_range(current->mm, args->user_ptr, args->user_size);
546 #ifdef CONFIG_MMU_NOTIFIER
547 obj = i915_gem_object_alloc();
551 drm_gem_private_object_init(dev, &obj->base, args->user_size);
552 i915_gem_object_init(obj, &i915_gem_userptr_ops, &lock_class, 0);
553 obj->mem_flags = I915_BO_FLAG_STRUCT_PAGE;
554 obj->read_domains = I915_GEM_DOMAIN_CPU;
555 obj->write_domain = I915_GEM_DOMAIN_CPU;
556 i915_gem_object_set_cache_coherency(obj, I915_CACHE_LLC);
558 obj->userptr.ptr = args->user_ptr;
559 obj->userptr.notifier_seq = ULONG_MAX;
560 if (args->flags & I915_USERPTR_READ_ONLY)
561 i915_gem_object_set_readonly(obj);
563 /* And keep a pointer to the current->mm for resolving the user pages
564 * at binding. This means that we need to hook into the mmu_notifier
565 * in order to detect if the mmu is destroyed.
567 ret = i915_gem_userptr_init__mmu_notifier(obj);
569 ret = drm_gem_handle_create(file, &obj->base, &handle);
571 /* drop reference from allocate - handle holds it now */
572 i915_gem_object_put(obj);
576 args->handle = handle;
583 int i915_gem_init_userptr(struct drm_i915_private *dev_priv)
585 #ifdef CONFIG_MMU_NOTIFIER
586 rwlock_init(&dev_priv->mm.notifier_lock);
592 void i915_gem_cleanup_userptr(struct drm_i915_private *dev_priv)