1 /* 2 * SPDX-License-Identifier: MIT 3 * 4 * Copyright © 2012-2014 Intel Corporation 5 */ 6 7 #include <linux/mmu_context.h> 8 #include <linux/mmu_notifier.h> 9 #include <linux/mempolicy.h> 10 #include <linux/swap.h> 11 #include <linux/sched/mm.h> 12 13 #include <drm/i915_drm.h> 14 15 #include "i915_drv.h" 16 #include "i915_gem_ioctls.h" 17 #include "i915_gem_object.h" 18 #include "i915_scatterlist.h" 19 20 struct i915_mm_struct { 21 struct mm_struct *mm; 22 struct drm_i915_private *i915; 23 struct i915_mmu_notifier *mn; 24 struct hlist_node node; 25 struct kref kref; 26 struct work_struct work; 27 }; 28 29 #if defined(CONFIG_MMU_NOTIFIER) 30 #include <linux/interval_tree.h> 31 32 struct i915_mmu_notifier { 33 spinlock_t lock; 34 struct hlist_node node; 35 struct mmu_notifier mn; 36 struct rb_root_cached objects; 37 struct i915_mm_struct *mm; 38 }; 39 40 struct i915_mmu_object { 41 struct i915_mmu_notifier *mn; 42 struct drm_i915_gem_object *obj; 43 struct interval_tree_node it; 44 }; 45 46 static void add_object(struct i915_mmu_object *mo) 47 { 48 GEM_BUG_ON(!RB_EMPTY_NODE(&mo->it.rb)); 49 interval_tree_insert(&mo->it, &mo->mn->objects); 50 } 51 52 static void del_object(struct i915_mmu_object *mo) 53 { 54 if (RB_EMPTY_NODE(&mo->it.rb)) 55 return; 56 57 interval_tree_remove(&mo->it, &mo->mn->objects); 58 RB_CLEAR_NODE(&mo->it.rb); 59 } 60 61 static void 62 __i915_gem_userptr_set_active(struct drm_i915_gem_object *obj, bool value) 63 { 64 struct i915_mmu_object *mo = obj->userptr.mmu_object; 65 66 /* 67 * During mm_invalidate_range we need to cancel any userptr that 68 * overlaps the range being invalidated. Doing so requires the 69 * struct_mutex, and that risks recursion. In order to cause 70 * recursion, the user must alias the userptr address space with 71 * a GTT mmapping (possible with a MAP_FIXED) - then when we have 72 * to invalidate that mmaping, mm_invalidate_range is called with 73 * the userptr address *and* the struct_mutex held. To prevent that 74 * we set a flag under the i915_mmu_notifier spinlock to indicate 75 * whether this object is valid. 76 */ 77 if (!mo) 78 return; 79 80 spin_lock(&mo->mn->lock); 81 if (value) 82 add_object(mo); 83 else 84 del_object(mo); 85 spin_unlock(&mo->mn->lock); 86 } 87 88 static int 89 userptr_mn_invalidate_range_start(struct mmu_notifier *_mn, 90 const struct mmu_notifier_range *range) 91 { 92 struct i915_mmu_notifier *mn = 93 container_of(_mn, struct i915_mmu_notifier, mn); 94 struct interval_tree_node *it; 95 unsigned long end; 96 int ret = 0; 97 98 if (RB_EMPTY_ROOT(&mn->objects.rb_root)) 99 return 0; 100 101 /* interval ranges are inclusive, but invalidate range is exclusive */ 102 end = range->end - 1; 103 104 spin_lock(&mn->lock); 105 it = interval_tree_iter_first(&mn->objects, range->start, end); 106 while (it) { 107 struct drm_i915_gem_object *obj; 108 109 if (!mmu_notifier_range_blockable(range)) { 110 ret = -EAGAIN; 111 break; 112 } 113 114 /* 115 * The mmu_object is released late when destroying the 116 * GEM object so it is entirely possible to gain a 117 * reference on an object in the process of being freed 118 * since our serialisation is via the spinlock and not 119 * the struct_mutex - and consequently use it after it 120 * is freed and then double free it. To prevent that 121 * use-after-free we only acquire a reference on the 122 * object if it is not in the process of being destroyed. 123 */ 124 obj = container_of(it, struct i915_mmu_object, it)->obj; 125 if (!kref_get_unless_zero(&obj->base.refcount)) { 126 it = interval_tree_iter_next(it, range->start, end); 127 continue; 128 } 129 spin_unlock(&mn->lock); 130 131 ret = i915_gem_object_unbind(obj, 132 I915_GEM_OBJECT_UNBIND_ACTIVE | 133 I915_GEM_OBJECT_UNBIND_BARRIER); 134 if (ret == 0) 135 ret = __i915_gem_object_put_pages(obj); 136 i915_gem_object_put(obj); 137 if (ret) 138 return ret; 139 140 spin_lock(&mn->lock); 141 142 /* 143 * As we do not (yet) protect the mmu from concurrent insertion 144 * over this range, there is no guarantee that this search will 145 * terminate given a pathologic workload. 146 */ 147 it = interval_tree_iter_first(&mn->objects, range->start, end); 148 } 149 spin_unlock(&mn->lock); 150 151 return ret; 152 153 } 154 155 static const struct mmu_notifier_ops i915_gem_userptr_notifier = { 156 .invalidate_range_start = userptr_mn_invalidate_range_start, 157 }; 158 159 static struct i915_mmu_notifier * 160 i915_mmu_notifier_create(struct i915_mm_struct *mm) 161 { 162 struct i915_mmu_notifier *mn; 163 164 mn = kmalloc(sizeof(*mn), GFP_KERNEL); 165 if (mn == NULL) 166 return ERR_PTR(-ENOMEM); 167 168 spin_lock_init(&mn->lock); 169 mn->mn.ops = &i915_gem_userptr_notifier; 170 mn->objects = RB_ROOT_CACHED; 171 mn->mm = mm; 172 173 return mn; 174 } 175 176 static void 177 i915_gem_userptr_release__mmu_notifier(struct drm_i915_gem_object *obj) 178 { 179 struct i915_mmu_object *mo; 180 181 mo = fetch_and_zero(&obj->userptr.mmu_object); 182 if (!mo) 183 return; 184 185 spin_lock(&mo->mn->lock); 186 del_object(mo); 187 spin_unlock(&mo->mn->lock); 188 kfree(mo); 189 } 190 191 static struct i915_mmu_notifier * 192 i915_mmu_notifier_find(struct i915_mm_struct *mm) 193 { 194 struct i915_mmu_notifier *mn; 195 int err = 0; 196 197 mn = mm->mn; 198 if (mn) 199 return mn; 200 201 mn = i915_mmu_notifier_create(mm); 202 if (IS_ERR(mn)) 203 err = PTR_ERR(mn); 204 205 down_write(&mm->mm->mmap_sem); 206 mutex_lock(&mm->i915->mm_lock); 207 if (mm->mn == NULL && !err) { 208 /* Protected by mmap_sem (write-lock) */ 209 err = __mmu_notifier_register(&mn->mn, mm->mm); 210 if (!err) { 211 /* Protected by mm_lock */ 212 mm->mn = fetch_and_zero(&mn); 213 } 214 } else if (mm->mn) { 215 /* 216 * Someone else raced and successfully installed the mmu 217 * notifier, we can cancel our own errors. 218 */ 219 err = 0; 220 } 221 mutex_unlock(&mm->i915->mm_lock); 222 up_write(&mm->mm->mmap_sem); 223 224 if (mn && !IS_ERR(mn)) 225 kfree(mn); 226 227 return err ? ERR_PTR(err) : mm->mn; 228 } 229 230 static int 231 i915_gem_userptr_init__mmu_notifier(struct drm_i915_gem_object *obj, 232 unsigned flags) 233 { 234 struct i915_mmu_notifier *mn; 235 struct i915_mmu_object *mo; 236 237 if (flags & I915_USERPTR_UNSYNCHRONIZED) 238 return capable(CAP_SYS_ADMIN) ? 0 : -EPERM; 239 240 if (WARN_ON(obj->userptr.mm == NULL)) 241 return -EINVAL; 242 243 mn = i915_mmu_notifier_find(obj->userptr.mm); 244 if (IS_ERR(mn)) 245 return PTR_ERR(mn); 246 247 mo = kzalloc(sizeof(*mo), GFP_KERNEL); 248 if (!mo) 249 return -ENOMEM; 250 251 mo->mn = mn; 252 mo->obj = obj; 253 mo->it.start = obj->userptr.ptr; 254 mo->it.last = obj->userptr.ptr + obj->base.size - 1; 255 RB_CLEAR_NODE(&mo->it.rb); 256 257 obj->userptr.mmu_object = mo; 258 return 0; 259 } 260 261 static void 262 i915_mmu_notifier_free(struct i915_mmu_notifier *mn, 263 struct mm_struct *mm) 264 { 265 if (mn == NULL) 266 return; 267 268 mmu_notifier_unregister(&mn->mn, mm); 269 kfree(mn); 270 } 271 272 #else 273 274 static void 275 __i915_gem_userptr_set_active(struct drm_i915_gem_object *obj, bool value) 276 { 277 } 278 279 static void 280 i915_gem_userptr_release__mmu_notifier(struct drm_i915_gem_object *obj) 281 { 282 } 283 284 static int 285 i915_gem_userptr_init__mmu_notifier(struct drm_i915_gem_object *obj, 286 unsigned flags) 287 { 288 if ((flags & I915_USERPTR_UNSYNCHRONIZED) == 0) 289 return -ENODEV; 290 291 if (!capable(CAP_SYS_ADMIN)) 292 return -EPERM; 293 294 return 0; 295 } 296 297 static void 298 i915_mmu_notifier_free(struct i915_mmu_notifier *mn, 299 struct mm_struct *mm) 300 { 301 } 302 303 #endif 304 305 static struct i915_mm_struct * 306 __i915_mm_struct_find(struct drm_i915_private *dev_priv, struct mm_struct *real) 307 { 308 struct i915_mm_struct *mm; 309 310 /* Protected by dev_priv->mm_lock */ 311 hash_for_each_possible(dev_priv->mm_structs, mm, node, (unsigned long)real) 312 if (mm->mm == real) 313 return mm; 314 315 return NULL; 316 } 317 318 static int 319 i915_gem_userptr_init__mm_struct(struct drm_i915_gem_object *obj) 320 { 321 struct drm_i915_private *dev_priv = to_i915(obj->base.dev); 322 struct i915_mm_struct *mm; 323 int ret = 0; 324 325 /* During release of the GEM object we hold the struct_mutex. This 326 * precludes us from calling mmput() at that time as that may be 327 * the last reference and so call exit_mmap(). exit_mmap() will 328 * attempt to reap the vma, and if we were holding a GTT mmap 329 * would then call drm_gem_vm_close() and attempt to reacquire 330 * the struct mutex. So in order to avoid that recursion, we have 331 * to defer releasing the mm reference until after we drop the 332 * struct_mutex, i.e. we need to schedule a worker to do the clean 333 * up. 334 */ 335 mutex_lock(&dev_priv->mm_lock); 336 mm = __i915_mm_struct_find(dev_priv, current->mm); 337 if (mm == NULL) { 338 mm = kmalloc(sizeof(*mm), GFP_KERNEL); 339 if (mm == NULL) { 340 ret = -ENOMEM; 341 goto out; 342 } 343 344 kref_init(&mm->kref); 345 mm->i915 = to_i915(obj->base.dev); 346 347 mm->mm = current->mm; 348 mmgrab(current->mm); 349 350 mm->mn = NULL; 351 352 /* Protected by dev_priv->mm_lock */ 353 hash_add(dev_priv->mm_structs, 354 &mm->node, (unsigned long)mm->mm); 355 } else 356 kref_get(&mm->kref); 357 358 obj->userptr.mm = mm; 359 out: 360 mutex_unlock(&dev_priv->mm_lock); 361 return ret; 362 } 363 364 static void 365 __i915_mm_struct_free__worker(struct work_struct *work) 366 { 367 struct i915_mm_struct *mm = container_of(work, typeof(*mm), work); 368 i915_mmu_notifier_free(mm->mn, mm->mm); 369 mmdrop(mm->mm); 370 kfree(mm); 371 } 372 373 static void 374 __i915_mm_struct_free(struct kref *kref) 375 { 376 struct i915_mm_struct *mm = container_of(kref, typeof(*mm), kref); 377 378 /* Protected by dev_priv->mm_lock */ 379 hash_del(&mm->node); 380 mutex_unlock(&mm->i915->mm_lock); 381 382 INIT_WORK(&mm->work, __i915_mm_struct_free__worker); 383 queue_work(mm->i915->mm.userptr_wq, &mm->work); 384 } 385 386 static void 387 i915_gem_userptr_release__mm_struct(struct drm_i915_gem_object *obj) 388 { 389 if (obj->userptr.mm == NULL) 390 return; 391 392 kref_put_mutex(&obj->userptr.mm->kref, 393 __i915_mm_struct_free, 394 &to_i915(obj->base.dev)->mm_lock); 395 obj->userptr.mm = NULL; 396 } 397 398 struct get_pages_work { 399 struct work_struct work; 400 struct drm_i915_gem_object *obj; 401 struct task_struct *task; 402 }; 403 404 static struct sg_table * 405 __i915_gem_userptr_alloc_pages(struct drm_i915_gem_object *obj, 406 struct page **pvec, int num_pages) 407 { 408 unsigned int max_segment = i915_sg_segment_size(); 409 struct sg_table *st; 410 unsigned int sg_page_sizes; 411 int ret; 412 413 st = kmalloc(sizeof(*st), GFP_KERNEL); 414 if (!st) 415 return ERR_PTR(-ENOMEM); 416 417 alloc_table: 418 ret = __sg_alloc_table_from_pages(st, pvec, num_pages, 419 0, num_pages << PAGE_SHIFT, 420 max_segment, 421 GFP_KERNEL); 422 if (ret) { 423 kfree(st); 424 return ERR_PTR(ret); 425 } 426 427 ret = i915_gem_gtt_prepare_pages(obj, st); 428 if (ret) { 429 sg_free_table(st); 430 431 if (max_segment > PAGE_SIZE) { 432 max_segment = PAGE_SIZE; 433 goto alloc_table; 434 } 435 436 kfree(st); 437 return ERR_PTR(ret); 438 } 439 440 sg_page_sizes = i915_sg_page_sizes(st->sgl); 441 442 __i915_gem_object_set_pages(obj, st, sg_page_sizes); 443 444 return st; 445 } 446 447 static void 448 __i915_gem_userptr_get_pages_worker(struct work_struct *_work) 449 { 450 struct get_pages_work *work = container_of(_work, typeof(*work), work); 451 struct drm_i915_gem_object *obj = work->obj; 452 const int npages = obj->base.size >> PAGE_SHIFT; 453 struct page **pvec; 454 int pinned, ret; 455 456 ret = -ENOMEM; 457 pinned = 0; 458 459 pvec = kvmalloc_array(npages, sizeof(struct page *), GFP_KERNEL); 460 if (pvec != NULL) { 461 struct mm_struct *mm = obj->userptr.mm->mm; 462 unsigned int flags = 0; 463 int locked = 0; 464 465 if (!i915_gem_object_is_readonly(obj)) 466 flags |= FOLL_WRITE; 467 468 ret = -EFAULT; 469 if (mmget_not_zero(mm)) { 470 while (pinned < npages) { 471 if (!locked) { 472 down_read(&mm->mmap_sem); 473 locked = 1; 474 } 475 ret = get_user_pages_remote 476 (work->task, mm, 477 obj->userptr.ptr + pinned * PAGE_SIZE, 478 npages - pinned, 479 flags, 480 pvec + pinned, NULL, &locked); 481 if (ret < 0) 482 break; 483 484 pinned += ret; 485 } 486 if (locked) 487 up_read(&mm->mmap_sem); 488 mmput(mm); 489 } 490 } 491 492 mutex_lock_nested(&obj->mm.lock, I915_MM_GET_PAGES); 493 if (obj->userptr.work == &work->work) { 494 struct sg_table *pages = ERR_PTR(ret); 495 496 if (pinned == npages) { 497 pages = __i915_gem_userptr_alloc_pages(obj, pvec, 498 npages); 499 if (!IS_ERR(pages)) { 500 pinned = 0; 501 pages = NULL; 502 } 503 } 504 505 obj->userptr.work = ERR_CAST(pages); 506 if (IS_ERR(pages)) 507 __i915_gem_userptr_set_active(obj, false); 508 } 509 mutex_unlock(&obj->mm.lock); 510 511 release_pages(pvec, pinned); 512 kvfree(pvec); 513 514 i915_gem_object_put(obj); 515 put_task_struct(work->task); 516 kfree(work); 517 } 518 519 static struct sg_table * 520 __i915_gem_userptr_get_pages_schedule(struct drm_i915_gem_object *obj) 521 { 522 struct get_pages_work *work; 523 524 /* Spawn a worker so that we can acquire the 525 * user pages without holding our mutex. Access 526 * to the user pages requires mmap_sem, and we have 527 * a strict lock ordering of mmap_sem, struct_mutex - 528 * we already hold struct_mutex here and so cannot 529 * call gup without encountering a lock inversion. 530 * 531 * Userspace will keep on repeating the operation 532 * (thanks to EAGAIN) until either we hit the fast 533 * path or the worker completes. If the worker is 534 * cancelled or superseded, the task is still run 535 * but the results ignored. (This leads to 536 * complications that we may have a stray object 537 * refcount that we need to be wary of when 538 * checking for existing objects during creation.) 539 * If the worker encounters an error, it reports 540 * that error back to this function through 541 * obj->userptr.work = ERR_PTR. 542 */ 543 work = kmalloc(sizeof(*work), GFP_KERNEL); 544 if (work == NULL) 545 return ERR_PTR(-ENOMEM); 546 547 obj->userptr.work = &work->work; 548 549 work->obj = i915_gem_object_get(obj); 550 551 work->task = current; 552 get_task_struct(work->task); 553 554 INIT_WORK(&work->work, __i915_gem_userptr_get_pages_worker); 555 queue_work(to_i915(obj->base.dev)->mm.userptr_wq, &work->work); 556 557 return ERR_PTR(-EAGAIN); 558 } 559 560 static int i915_gem_userptr_get_pages(struct drm_i915_gem_object *obj) 561 { 562 const int num_pages = obj->base.size >> PAGE_SHIFT; 563 struct mm_struct *mm = obj->userptr.mm->mm; 564 struct page **pvec; 565 struct sg_table *pages; 566 bool active; 567 int pinned; 568 569 /* If userspace should engineer that these pages are replaced in 570 * the vma between us binding this page into the GTT and completion 571 * of rendering... Their loss. If they change the mapping of their 572 * pages they need to create a new bo to point to the new vma. 573 * 574 * However, that still leaves open the possibility of the vma 575 * being copied upon fork. Which falls under the same userspace 576 * synchronisation issue as a regular bo, except that this time 577 * the process may not be expecting that a particular piece of 578 * memory is tied to the GPU. 579 * 580 * Fortunately, we can hook into the mmu_notifier in order to 581 * discard the page references prior to anything nasty happening 582 * to the vma (discard or cloning) which should prevent the more 583 * egregious cases from causing harm. 584 */ 585 586 if (obj->userptr.work) { 587 /* active flag should still be held for the pending work */ 588 if (IS_ERR(obj->userptr.work)) 589 return PTR_ERR(obj->userptr.work); 590 else 591 return -EAGAIN; 592 } 593 594 pvec = NULL; 595 pinned = 0; 596 597 if (mm == current->mm) { 598 pvec = kvmalloc_array(num_pages, sizeof(struct page *), 599 GFP_KERNEL | 600 __GFP_NORETRY | 601 __GFP_NOWARN); 602 if (pvec) /* defer to worker if malloc fails */ 603 pinned = __get_user_pages_fast(obj->userptr.ptr, 604 num_pages, 605 !i915_gem_object_is_readonly(obj), 606 pvec); 607 } 608 609 active = false; 610 if (pinned < 0) { 611 pages = ERR_PTR(pinned); 612 pinned = 0; 613 } else if (pinned < num_pages) { 614 pages = __i915_gem_userptr_get_pages_schedule(obj); 615 active = pages == ERR_PTR(-EAGAIN); 616 } else { 617 pages = __i915_gem_userptr_alloc_pages(obj, pvec, num_pages); 618 active = !IS_ERR(pages); 619 } 620 if (active) 621 __i915_gem_userptr_set_active(obj, true); 622 623 if (IS_ERR(pages)) 624 release_pages(pvec, pinned); 625 kvfree(pvec); 626 627 return PTR_ERR_OR_ZERO(pages); 628 } 629 630 static void 631 i915_gem_userptr_put_pages(struct drm_i915_gem_object *obj, 632 struct sg_table *pages) 633 { 634 struct sgt_iter sgt_iter; 635 struct page *page; 636 637 /* Cancel any inflight work and force them to restart their gup */ 638 obj->userptr.work = NULL; 639 __i915_gem_userptr_set_active(obj, false); 640 if (!pages) 641 return; 642 643 __i915_gem_object_release_shmem(obj, pages, true); 644 i915_gem_gtt_finish_pages(obj, pages); 645 646 /* 647 * We always mark objects as dirty when they are used by the GPU, 648 * just in case. However, if we set the vma as being read-only we know 649 * that the object will never have been written to. 650 */ 651 if (i915_gem_object_is_readonly(obj)) 652 obj->mm.dirty = false; 653 654 for_each_sgt_page(page, sgt_iter, pages) { 655 if (obj->mm.dirty && trylock_page(page)) { 656 /* 657 * As this may not be anonymous memory (e.g. shmem) 658 * but exist on a real mapping, we have to lock 659 * the page in order to dirty it -- holding 660 * the page reference is not sufficient to 661 * prevent the inode from being truncated. 662 * Play safe and take the lock. 663 * 664 * However...! 665 * 666 * The mmu-notifier can be invalidated for a 667 * migrate_page, that is alreadying holding the lock 668 * on the page. Such a try_to_unmap() will result 669 * in us calling put_pages() and so recursively try 670 * to lock the page. We avoid that deadlock with 671 * a trylock_page() and in exchange we risk missing 672 * some page dirtying. 673 */ 674 set_page_dirty(page); 675 unlock_page(page); 676 } 677 678 mark_page_accessed(page); 679 put_page(page); 680 } 681 obj->mm.dirty = false; 682 683 sg_free_table(pages); 684 kfree(pages); 685 } 686 687 static void 688 i915_gem_userptr_release(struct drm_i915_gem_object *obj) 689 { 690 i915_gem_userptr_release__mmu_notifier(obj); 691 i915_gem_userptr_release__mm_struct(obj); 692 } 693 694 static int 695 i915_gem_userptr_dmabuf_export(struct drm_i915_gem_object *obj) 696 { 697 if (obj->userptr.mmu_object) 698 return 0; 699 700 return i915_gem_userptr_init__mmu_notifier(obj, 0); 701 } 702 703 static const struct drm_i915_gem_object_ops i915_gem_userptr_ops = { 704 .flags = I915_GEM_OBJECT_HAS_STRUCT_PAGE | 705 I915_GEM_OBJECT_IS_SHRINKABLE | 706 I915_GEM_OBJECT_NO_GGTT | 707 I915_GEM_OBJECT_ASYNC_CANCEL, 708 .get_pages = i915_gem_userptr_get_pages, 709 .put_pages = i915_gem_userptr_put_pages, 710 .dmabuf_export = i915_gem_userptr_dmabuf_export, 711 .release = i915_gem_userptr_release, 712 }; 713 714 /* 715 * Creates a new mm object that wraps some normal memory from the process 716 * context - user memory. 717 * 718 * We impose several restrictions upon the memory being mapped 719 * into the GPU. 720 * 1. It must be page aligned (both start/end addresses, i.e ptr and size). 721 * 2. It must be normal system memory, not a pointer into another map of IO 722 * space (e.g. it must not be a GTT mmapping of another object). 723 * 3. We only allow a bo as large as we could in theory map into the GTT, 724 * that is we limit the size to the total size of the GTT. 725 * 4. The bo is marked as being snoopable. The backing pages are left 726 * accessible directly by the CPU, but reads and writes by the GPU may 727 * incur the cost of a snoop (unless you have an LLC architecture). 728 * 729 * Synchronisation between multiple users and the GPU is left to userspace 730 * through the normal set-domain-ioctl. The kernel will enforce that the 731 * GPU relinquishes the VMA before it is returned back to the system 732 * i.e. upon free(), munmap() or process termination. However, the userspace 733 * malloc() library may not immediately relinquish the VMA after free() and 734 * instead reuse it whilst the GPU is still reading and writing to the VMA. 735 * Caveat emptor. 736 * 737 * Also note, that the object created here is not currently a "first class" 738 * object, in that several ioctls are banned. These are the CPU access 739 * ioctls: mmap(), pwrite and pread. In practice, you are expected to use 740 * direct access via your pointer rather than use those ioctls. Another 741 * restriction is that we do not allow userptr surfaces to be pinned to the 742 * hardware and so we reject any attempt to create a framebuffer out of a 743 * userptr. 744 * 745 * If you think this is a good interface to use to pass GPU memory between 746 * drivers, please use dma-buf instead. In fact, wherever possible use 747 * dma-buf instead. 748 */ 749 int 750 i915_gem_userptr_ioctl(struct drm_device *dev, 751 void *data, 752 struct drm_file *file) 753 { 754 static struct lock_class_key lock_class; 755 struct drm_i915_private *dev_priv = to_i915(dev); 756 struct drm_i915_gem_userptr *args = data; 757 struct drm_i915_gem_object *obj; 758 int ret; 759 u32 handle; 760 761 if (!HAS_LLC(dev_priv) && !HAS_SNOOP(dev_priv)) { 762 /* We cannot support coherent userptr objects on hw without 763 * LLC and broken snooping. 764 */ 765 return -ENODEV; 766 } 767 768 if (args->flags & ~(I915_USERPTR_READ_ONLY | 769 I915_USERPTR_UNSYNCHRONIZED)) 770 return -EINVAL; 771 772 if (!args->user_size) 773 return -EINVAL; 774 775 if (offset_in_page(args->user_ptr | args->user_size)) 776 return -EINVAL; 777 778 if (!access_ok((char __user *)(unsigned long)args->user_ptr, args->user_size)) 779 return -EFAULT; 780 781 if (args->flags & I915_USERPTR_READ_ONLY) { 782 /* 783 * On almost all of the older hw, we cannot tell the GPU that 784 * a page is readonly. 785 */ 786 if (!dev_priv->gt.vm->has_read_only) 787 return -ENODEV; 788 } 789 790 obj = i915_gem_object_alloc(); 791 if (obj == NULL) 792 return -ENOMEM; 793 794 drm_gem_private_object_init(dev, &obj->base, args->user_size); 795 i915_gem_object_init(obj, &i915_gem_userptr_ops, &lock_class); 796 obj->read_domains = I915_GEM_DOMAIN_CPU; 797 obj->write_domain = I915_GEM_DOMAIN_CPU; 798 i915_gem_object_set_cache_coherency(obj, I915_CACHE_LLC); 799 800 obj->userptr.ptr = args->user_ptr; 801 if (args->flags & I915_USERPTR_READ_ONLY) 802 i915_gem_object_set_readonly(obj); 803 804 /* And keep a pointer to the current->mm for resolving the user pages 805 * at binding. This means that we need to hook into the mmu_notifier 806 * in order to detect if the mmu is destroyed. 807 */ 808 ret = i915_gem_userptr_init__mm_struct(obj); 809 if (ret == 0) 810 ret = i915_gem_userptr_init__mmu_notifier(obj, args->flags); 811 if (ret == 0) 812 ret = drm_gem_handle_create(file, &obj->base, &handle); 813 814 /* drop reference from allocate - handle holds it now */ 815 i915_gem_object_put(obj); 816 if (ret) 817 return ret; 818 819 args->handle = handle; 820 return 0; 821 } 822 823 int i915_gem_init_userptr(struct drm_i915_private *dev_priv) 824 { 825 mutex_init(&dev_priv->mm_lock); 826 hash_init(dev_priv->mm_structs); 827 828 dev_priv->mm.userptr_wq = 829 alloc_workqueue("i915-userptr-acquire", 830 WQ_HIGHPRI | WQ_UNBOUND, 831 0); 832 if (!dev_priv->mm.userptr_wq) 833 return -ENOMEM; 834 835 return 0; 836 } 837 838 void i915_gem_cleanup_userptr(struct drm_i915_private *dev_priv) 839 { 840 destroy_workqueue(dev_priv->mm.userptr_wq); 841 } 842