i915_gem_userptr.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823
  1. /*
  2. * Copyright © 2012-2014 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. * IN THE SOFTWARE.
  22. *
  23. */
  24. #include <drm/drmP.h>
  25. #include <drm/i915_drm.h>
  26. #include "i915_drv.h"
  27. #include "i915_trace.h"
  28. #include "intel_drv.h"
  29. #include <linux/mmu_context.h>
  30. #include <linux/mmu_notifier.h>
  31. #include <linux/mempolicy.h>
  32. #include <linux/swap.h>
  33. struct i915_mm_struct {
  34. struct mm_struct *mm;
  35. struct drm_i915_private *i915;
  36. struct i915_mmu_notifier *mn;
  37. struct hlist_node node;
  38. struct kref kref;
  39. struct work_struct work;
  40. };
  41. #if defined(CONFIG_MMU_NOTIFIER)
  42. #include <linux/interval_tree.h>
  43. struct i915_mmu_notifier {
  44. spinlock_t lock;
  45. struct hlist_node node;
  46. struct mmu_notifier mn;
  47. struct rb_root objects;
  48. struct workqueue_struct *wq;
  49. };
  50. struct i915_mmu_object {
  51. struct i915_mmu_notifier *mn;
  52. struct drm_i915_gem_object *obj;
  53. struct interval_tree_node it;
  54. struct list_head link;
  55. struct work_struct work;
  56. bool attached;
  57. };
  58. static void cancel_userptr(struct work_struct *work)
  59. {
  60. struct i915_mmu_object *mo = container_of(work, typeof(*mo), work);
  61. struct drm_i915_gem_object *obj = mo->obj;
  62. struct drm_device *dev = obj->base.dev;
  63. i915_gem_object_wait(obj, I915_WAIT_ALL, MAX_SCHEDULE_TIMEOUT, NULL);
  64. mutex_lock(&dev->struct_mutex);
  65. /* Cancel any active worker and force us to re-evaluate gup */
  66. obj->userptr.work = NULL;
  67. /* We are inside a kthread context and can't be interrupted */
  68. if (i915_gem_object_unbind(obj) == 0)
  69. __i915_gem_object_put_pages(obj, I915_MM_NORMAL);
  70. WARN_ONCE(obj->mm.pages,
  71. "Failed to release pages: bind_count=%d, pages_pin_count=%d, pin_display=%d\n",
  72. obj->bind_count,
  73. atomic_read(&obj->mm.pages_pin_count),
  74. obj->pin_display);
  75. i915_gem_object_put(obj);
  76. mutex_unlock(&dev->struct_mutex);
  77. }
  78. static void add_object(struct i915_mmu_object *mo)
  79. {
  80. if (mo->attached)
  81. return;
  82. interval_tree_insert(&mo->it, &mo->mn->objects);
  83. mo->attached = true;
  84. }
  85. static void del_object(struct i915_mmu_object *mo)
  86. {
  87. if (!mo->attached)
  88. return;
  89. interval_tree_remove(&mo->it, &mo->mn->objects);
  90. mo->attached = false;
  91. }
  92. static void i915_gem_userptr_mn_invalidate_range_start(struct mmu_notifier *_mn,
  93. struct mm_struct *mm,
  94. unsigned long start,
  95. unsigned long end)
  96. {
  97. struct i915_mmu_notifier *mn =
  98. container_of(_mn, struct i915_mmu_notifier, mn);
  99. struct i915_mmu_object *mo;
  100. struct interval_tree_node *it;
  101. LIST_HEAD(cancelled);
  102. if (RB_EMPTY_ROOT(&mn->objects))
  103. return;
  104. /* interval ranges are inclusive, but invalidate range is exclusive */
  105. end--;
  106. spin_lock(&mn->lock);
  107. it = interval_tree_iter_first(&mn->objects, start, end);
  108. while (it) {
  109. /* The mmu_object is released late when destroying the
  110. * GEM object so it is entirely possible to gain a
  111. * reference on an object in the process of being freed
  112. * since our serialisation is via the spinlock and not
  113. * the struct_mutex - and consequently use it after it
  114. * is freed and then double free it. To prevent that
  115. * use-after-free we only acquire a reference on the
  116. * object if it is not in the process of being destroyed.
  117. */
  118. mo = container_of(it, struct i915_mmu_object, it);
  119. if (kref_get_unless_zero(&mo->obj->base.refcount))
  120. queue_work(mn->wq, &mo->work);
  121. list_add(&mo->link, &cancelled);
  122. it = interval_tree_iter_next(it, start, end);
  123. }
  124. list_for_each_entry(mo, &cancelled, link)
  125. del_object(mo);
  126. spin_unlock(&mn->lock);
  127. flush_workqueue(mn->wq);
  128. }
  129. static const struct mmu_notifier_ops i915_gem_userptr_notifier = {
  130. .invalidate_range_start = i915_gem_userptr_mn_invalidate_range_start,
  131. };
  132. static struct i915_mmu_notifier *
  133. i915_mmu_notifier_create(struct mm_struct *mm)
  134. {
  135. struct i915_mmu_notifier *mn;
  136. int ret;
  137. mn = kmalloc(sizeof(*mn), GFP_KERNEL);
  138. if (mn == NULL)
  139. return ERR_PTR(-ENOMEM);
  140. spin_lock_init(&mn->lock);
  141. mn->mn.ops = &i915_gem_userptr_notifier;
  142. mn->objects = RB_ROOT;
  143. mn->wq = alloc_workqueue("i915-userptr-release", WQ_UNBOUND, 0);
  144. if (mn->wq == NULL) {
  145. kfree(mn);
  146. return ERR_PTR(-ENOMEM);
  147. }
  148. /* Protected by mmap_sem (write-lock) */
  149. ret = __mmu_notifier_register(&mn->mn, mm);
  150. if (ret) {
  151. destroy_workqueue(mn->wq);
  152. kfree(mn);
  153. return ERR_PTR(ret);
  154. }
  155. return mn;
  156. }
  157. static void
  158. i915_gem_userptr_release__mmu_notifier(struct drm_i915_gem_object *obj)
  159. {
  160. struct i915_mmu_object *mo;
  161. mo = obj->userptr.mmu_object;
  162. if (mo == NULL)
  163. return;
  164. spin_lock(&mo->mn->lock);
  165. del_object(mo);
  166. spin_unlock(&mo->mn->lock);
  167. kfree(mo);
  168. obj->userptr.mmu_object = NULL;
  169. }
  170. static struct i915_mmu_notifier *
  171. i915_mmu_notifier_find(struct i915_mm_struct *mm)
  172. {
  173. struct i915_mmu_notifier *mn = mm->mn;
  174. mn = mm->mn;
  175. if (mn)
  176. return mn;
  177. down_write(&mm->mm->mmap_sem);
  178. mutex_lock(&mm->i915->mm_lock);
  179. if ((mn = mm->mn) == NULL) {
  180. mn = i915_mmu_notifier_create(mm->mm);
  181. if (!IS_ERR(mn))
  182. mm->mn = mn;
  183. }
  184. mutex_unlock(&mm->i915->mm_lock);
  185. up_write(&mm->mm->mmap_sem);
  186. return mn;
  187. }
  188. static int
  189. i915_gem_userptr_init__mmu_notifier(struct drm_i915_gem_object *obj,
  190. unsigned flags)
  191. {
  192. struct i915_mmu_notifier *mn;
  193. struct i915_mmu_object *mo;
  194. if (flags & I915_USERPTR_UNSYNCHRONIZED)
  195. return capable(CAP_SYS_ADMIN) ? 0 : -EPERM;
  196. if (WARN_ON(obj->userptr.mm == NULL))
  197. return -EINVAL;
  198. mn = i915_mmu_notifier_find(obj->userptr.mm);
  199. if (IS_ERR(mn))
  200. return PTR_ERR(mn);
  201. mo = kzalloc(sizeof(*mo), GFP_KERNEL);
  202. if (mo == NULL)
  203. return -ENOMEM;
  204. mo->mn = mn;
  205. mo->obj = obj;
  206. mo->it.start = obj->userptr.ptr;
  207. mo->it.last = obj->userptr.ptr + obj->base.size - 1;
  208. INIT_WORK(&mo->work, cancel_userptr);
  209. obj->userptr.mmu_object = mo;
  210. return 0;
  211. }
  212. static void
  213. i915_mmu_notifier_free(struct i915_mmu_notifier *mn,
  214. struct mm_struct *mm)
  215. {
  216. if (mn == NULL)
  217. return;
  218. mmu_notifier_unregister(&mn->mn, mm);
  219. destroy_workqueue(mn->wq);
  220. kfree(mn);
  221. }
  222. #else
  223. static void
  224. i915_gem_userptr_release__mmu_notifier(struct drm_i915_gem_object *obj)
  225. {
  226. }
  227. static int
  228. i915_gem_userptr_init__mmu_notifier(struct drm_i915_gem_object *obj,
  229. unsigned flags)
  230. {
  231. if ((flags & I915_USERPTR_UNSYNCHRONIZED) == 0)
  232. return -ENODEV;
  233. if (!capable(CAP_SYS_ADMIN))
  234. return -EPERM;
  235. return 0;
  236. }
  237. static void
  238. i915_mmu_notifier_free(struct i915_mmu_notifier *mn,
  239. struct mm_struct *mm)
  240. {
  241. }
  242. #endif
  243. static struct i915_mm_struct *
  244. __i915_mm_struct_find(struct drm_i915_private *dev_priv, struct mm_struct *real)
  245. {
  246. struct i915_mm_struct *mm;
  247. /* Protected by dev_priv->mm_lock */
  248. hash_for_each_possible(dev_priv->mm_structs, mm, node, (unsigned long)real)
  249. if (mm->mm == real)
  250. return mm;
  251. return NULL;
  252. }
  253. static int
  254. i915_gem_userptr_init__mm_struct(struct drm_i915_gem_object *obj)
  255. {
  256. struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
  257. struct i915_mm_struct *mm;
  258. int ret = 0;
  259. /* During release of the GEM object we hold the struct_mutex. This
  260. * precludes us from calling mmput() at that time as that may be
  261. * the last reference and so call exit_mmap(). exit_mmap() will
  262. * attempt to reap the vma, and if we were holding a GTT mmap
  263. * would then call drm_gem_vm_close() and attempt to reacquire
  264. * the struct mutex. So in order to avoid that recursion, we have
  265. * to defer releasing the mm reference until after we drop the
  266. * struct_mutex, i.e. we need to schedule a worker to do the clean
  267. * up.
  268. */
  269. mutex_lock(&dev_priv->mm_lock);
  270. mm = __i915_mm_struct_find(dev_priv, current->mm);
  271. if (mm == NULL) {
  272. mm = kmalloc(sizeof(*mm), GFP_KERNEL);
  273. if (mm == NULL) {
  274. ret = -ENOMEM;
  275. goto out;
  276. }
  277. kref_init(&mm->kref);
  278. mm->i915 = to_i915(obj->base.dev);
  279. mm->mm = current->mm;
  280. mmgrab(current->mm);
  281. mm->mn = NULL;
  282. /* Protected by dev_priv->mm_lock */
  283. hash_add(dev_priv->mm_structs,
  284. &mm->node, (unsigned long)mm->mm);
  285. } else
  286. kref_get(&mm->kref);
  287. obj->userptr.mm = mm;
  288. out:
  289. mutex_unlock(&dev_priv->mm_lock);
  290. return ret;
  291. }
  292. static void
  293. __i915_mm_struct_free__worker(struct work_struct *work)
  294. {
  295. struct i915_mm_struct *mm = container_of(work, typeof(*mm), work);
  296. i915_mmu_notifier_free(mm->mn, mm->mm);
  297. mmdrop(mm->mm);
  298. kfree(mm);
  299. }
  300. static void
  301. __i915_mm_struct_free(struct kref *kref)
  302. {
  303. struct i915_mm_struct *mm = container_of(kref, typeof(*mm), kref);
  304. /* Protected by dev_priv->mm_lock */
  305. hash_del(&mm->node);
  306. mutex_unlock(&mm->i915->mm_lock);
  307. INIT_WORK(&mm->work, __i915_mm_struct_free__worker);
  308. schedule_work(&mm->work);
  309. }
  310. static void
  311. i915_gem_userptr_release__mm_struct(struct drm_i915_gem_object *obj)
  312. {
  313. if (obj->userptr.mm == NULL)
  314. return;
  315. kref_put_mutex(&obj->userptr.mm->kref,
  316. __i915_mm_struct_free,
  317. &to_i915(obj->base.dev)->mm_lock);
  318. obj->userptr.mm = NULL;
  319. }
  320. struct get_pages_work {
  321. struct work_struct work;
  322. struct drm_i915_gem_object *obj;
  323. struct task_struct *task;
  324. };
  325. #if IS_ENABLED(CONFIG_SWIOTLB)
  326. #define swiotlb_active() swiotlb_nr_tbl()
  327. #else
  328. #define swiotlb_active() 0
  329. #endif
  330. static int
  331. st_set_pages(struct sg_table **st, struct page **pvec, int num_pages)
  332. {
  333. struct scatterlist *sg;
  334. int ret, n;
  335. *st = kmalloc(sizeof(**st), GFP_KERNEL);
  336. if (*st == NULL)
  337. return -ENOMEM;
  338. if (swiotlb_active()) {
  339. ret = sg_alloc_table(*st, num_pages, GFP_KERNEL);
  340. if (ret)
  341. goto err;
  342. for_each_sg((*st)->sgl, sg, num_pages, n)
  343. sg_set_page(sg, pvec[n], PAGE_SIZE, 0);
  344. } else {
  345. ret = sg_alloc_table_from_pages(*st, pvec, num_pages,
  346. 0, num_pages << PAGE_SHIFT,
  347. GFP_KERNEL);
  348. if (ret)
  349. goto err;
  350. }
  351. return 0;
  352. err:
  353. kfree(*st);
  354. *st = NULL;
  355. return ret;
  356. }
  357. static struct sg_table *
  358. __i915_gem_userptr_set_pages(struct drm_i915_gem_object *obj,
  359. struct page **pvec, int num_pages)
  360. {
  361. struct sg_table *pages;
  362. int ret;
  363. ret = st_set_pages(&pages, pvec, num_pages);
  364. if (ret)
  365. return ERR_PTR(ret);
  366. ret = i915_gem_gtt_prepare_pages(obj, pages);
  367. if (ret) {
  368. sg_free_table(pages);
  369. kfree(pages);
  370. return ERR_PTR(ret);
  371. }
  372. return pages;
  373. }
  374. static int
  375. __i915_gem_userptr_set_active(struct drm_i915_gem_object *obj,
  376. bool value)
  377. {
  378. int ret = 0;
  379. /* During mm_invalidate_range we need to cancel any userptr that
  380. * overlaps the range being invalidated. Doing so requires the
  381. * struct_mutex, and that risks recursion. In order to cause
  382. * recursion, the user must alias the userptr address space with
  383. * a GTT mmapping (possible with a MAP_FIXED) - then when we have
  384. * to invalidate that mmaping, mm_invalidate_range is called with
  385. * the userptr address *and* the struct_mutex held. To prevent that
  386. * we set a flag under the i915_mmu_notifier spinlock to indicate
  387. * whether this object is valid.
  388. */
  389. #if defined(CONFIG_MMU_NOTIFIER)
  390. if (obj->userptr.mmu_object == NULL)
  391. return 0;
  392. spin_lock(&obj->userptr.mmu_object->mn->lock);
  393. /* In order to serialise get_pages with an outstanding
  394. * cancel_userptr, we must drop the struct_mutex and try again.
  395. */
  396. if (!value)
  397. del_object(obj->userptr.mmu_object);
  398. else if (!work_pending(&obj->userptr.mmu_object->work))
  399. add_object(obj->userptr.mmu_object);
  400. else
  401. ret = -EAGAIN;
  402. spin_unlock(&obj->userptr.mmu_object->mn->lock);
  403. #endif
  404. return ret;
  405. }
  406. static void
  407. __i915_gem_userptr_get_pages_worker(struct work_struct *_work)
  408. {
  409. struct get_pages_work *work = container_of(_work, typeof(*work), work);
  410. struct drm_i915_gem_object *obj = work->obj;
  411. const int npages = obj->base.size >> PAGE_SHIFT;
  412. struct page **pvec;
  413. int pinned, ret;
  414. ret = -ENOMEM;
  415. pinned = 0;
  416. pvec = drm_malloc_gfp(npages, sizeof(struct page *), GFP_TEMPORARY);
  417. if (pvec != NULL) {
  418. struct mm_struct *mm = obj->userptr.mm->mm;
  419. unsigned int flags = 0;
  420. if (!obj->userptr.read_only)
  421. flags |= FOLL_WRITE;
  422. ret = -EFAULT;
  423. if (mmget_not_zero(mm)) {
  424. down_read(&mm->mmap_sem);
  425. while (pinned < npages) {
  426. ret = get_user_pages_remote
  427. (work->task, mm,
  428. obj->userptr.ptr + pinned * PAGE_SIZE,
  429. npages - pinned,
  430. flags,
  431. pvec + pinned, NULL, NULL);
  432. if (ret < 0)
  433. break;
  434. pinned += ret;
  435. }
  436. up_read(&mm->mmap_sem);
  437. mmput(mm);
  438. }
  439. }
  440. mutex_lock(&obj->mm.lock);
  441. if (obj->userptr.work == &work->work) {
  442. struct sg_table *pages = ERR_PTR(ret);
  443. if (pinned == npages) {
  444. pages = __i915_gem_userptr_set_pages(obj, pvec, npages);
  445. if (!IS_ERR(pages)) {
  446. __i915_gem_object_set_pages(obj, pages);
  447. pinned = 0;
  448. pages = NULL;
  449. }
  450. }
  451. obj->userptr.work = ERR_CAST(pages);
  452. }
  453. mutex_unlock(&obj->mm.lock);
  454. release_pages(pvec, pinned, 0);
  455. drm_free_large(pvec);
  456. i915_gem_object_put(obj);
  457. put_task_struct(work->task);
  458. kfree(work);
  459. }
  460. static struct sg_table *
  461. __i915_gem_userptr_get_pages_schedule(struct drm_i915_gem_object *obj,
  462. bool *active)
  463. {
  464. struct get_pages_work *work;
  465. /* Spawn a worker so that we can acquire the
  466. * user pages without holding our mutex. Access
  467. * to the user pages requires mmap_sem, and we have
  468. * a strict lock ordering of mmap_sem, struct_mutex -
  469. * we already hold struct_mutex here and so cannot
  470. * call gup without encountering a lock inversion.
  471. *
  472. * Userspace will keep on repeating the operation
  473. * (thanks to EAGAIN) until either we hit the fast
  474. * path or the worker completes. If the worker is
  475. * cancelled or superseded, the task is still run
  476. * but the results ignored. (This leads to
  477. * complications that we may have a stray object
  478. * refcount that we need to be wary of when
  479. * checking for existing objects during creation.)
  480. * If the worker encounters an error, it reports
  481. * that error back to this function through
  482. * obj->userptr.work = ERR_PTR.
  483. */
  484. work = kmalloc(sizeof(*work), GFP_KERNEL);
  485. if (work == NULL)
  486. return ERR_PTR(-ENOMEM);
  487. obj->userptr.work = &work->work;
  488. work->obj = i915_gem_object_get(obj);
  489. work->task = current;
  490. get_task_struct(work->task);
  491. INIT_WORK(&work->work, __i915_gem_userptr_get_pages_worker);
  492. schedule_work(&work->work);
  493. *active = true;
  494. return ERR_PTR(-EAGAIN);
  495. }
  496. static struct sg_table *
  497. i915_gem_userptr_get_pages(struct drm_i915_gem_object *obj)
  498. {
  499. const int num_pages = obj->base.size >> PAGE_SHIFT;
  500. struct page **pvec;
  501. struct sg_table *pages;
  502. int pinned, ret;
  503. bool active;
  504. /* If userspace should engineer that these pages are replaced in
  505. * the vma between us binding this page into the GTT and completion
  506. * of rendering... Their loss. If they change the mapping of their
  507. * pages they need to create a new bo to point to the new vma.
  508. *
  509. * However, that still leaves open the possibility of the vma
  510. * being copied upon fork. Which falls under the same userspace
  511. * synchronisation issue as a regular bo, except that this time
  512. * the process may not be expecting that a particular piece of
  513. * memory is tied to the GPU.
  514. *
  515. * Fortunately, we can hook into the mmu_notifier in order to
  516. * discard the page references prior to anything nasty happening
  517. * to the vma (discard or cloning) which should prevent the more
  518. * egregious cases from causing harm.
  519. */
  520. if (obj->userptr.work) {
  521. /* active flag should still be held for the pending work */
  522. if (IS_ERR(obj->userptr.work))
  523. return ERR_CAST(obj->userptr.work);
  524. else
  525. return ERR_PTR(-EAGAIN);
  526. }
  527. /* Let the mmu-notifier know that we have begun and need cancellation */
  528. ret = __i915_gem_userptr_set_active(obj, true);
  529. if (ret)
  530. return ERR_PTR(ret);
  531. pvec = NULL;
  532. pinned = 0;
  533. if (obj->userptr.mm->mm == current->mm) {
  534. pvec = drm_malloc_gfp(num_pages, sizeof(struct page *),
  535. GFP_TEMPORARY);
  536. if (pvec == NULL) {
  537. __i915_gem_userptr_set_active(obj, false);
  538. return ERR_PTR(-ENOMEM);
  539. }
  540. pinned = __get_user_pages_fast(obj->userptr.ptr, num_pages,
  541. !obj->userptr.read_only, pvec);
  542. }
  543. active = false;
  544. if (pinned < 0)
  545. pages = ERR_PTR(pinned), pinned = 0;
  546. else if (pinned < num_pages)
  547. pages = __i915_gem_userptr_get_pages_schedule(obj, &active);
  548. else
  549. pages = __i915_gem_userptr_set_pages(obj, pvec, num_pages);
  550. if (IS_ERR(pages)) {
  551. __i915_gem_userptr_set_active(obj, active);
  552. release_pages(pvec, pinned, 0);
  553. }
  554. drm_free_large(pvec);
  555. return pages;
  556. }
  557. static void
  558. i915_gem_userptr_put_pages(struct drm_i915_gem_object *obj,
  559. struct sg_table *pages)
  560. {
  561. struct sgt_iter sgt_iter;
  562. struct page *page;
  563. BUG_ON(obj->userptr.work != NULL);
  564. __i915_gem_userptr_set_active(obj, false);
  565. if (obj->mm.madv != I915_MADV_WILLNEED)
  566. obj->mm.dirty = false;
  567. i915_gem_gtt_finish_pages(obj, pages);
  568. for_each_sgt_page(page, sgt_iter, pages) {
  569. if (obj->mm.dirty)
  570. set_page_dirty(page);
  571. mark_page_accessed(page);
  572. put_page(page);
  573. }
  574. obj->mm.dirty = false;
  575. sg_free_table(pages);
  576. kfree(pages);
  577. }
  578. static void
  579. i915_gem_userptr_release(struct drm_i915_gem_object *obj)
  580. {
  581. i915_gem_userptr_release__mmu_notifier(obj);
  582. i915_gem_userptr_release__mm_struct(obj);
  583. }
  584. static int
  585. i915_gem_userptr_dmabuf_export(struct drm_i915_gem_object *obj)
  586. {
  587. if (obj->userptr.mmu_object)
  588. return 0;
  589. return i915_gem_userptr_init__mmu_notifier(obj, 0);
  590. }
  591. static const struct drm_i915_gem_object_ops i915_gem_userptr_ops = {
  592. .flags = I915_GEM_OBJECT_HAS_STRUCT_PAGE |
  593. I915_GEM_OBJECT_IS_SHRINKABLE,
  594. .get_pages = i915_gem_userptr_get_pages,
  595. .put_pages = i915_gem_userptr_put_pages,
  596. .dmabuf_export = i915_gem_userptr_dmabuf_export,
  597. .release = i915_gem_userptr_release,
  598. };
  599. /**
  600. * Creates a new mm object that wraps some normal memory from the process
  601. * context - user memory.
  602. *
  603. * We impose several restrictions upon the memory being mapped
  604. * into the GPU.
  605. * 1. It must be page aligned (both start/end addresses, i.e ptr and size).
  606. * 2. It must be normal system memory, not a pointer into another map of IO
  607. * space (e.g. it must not be a GTT mmapping of another object).
  608. * 3. We only allow a bo as large as we could in theory map into the GTT,
  609. * that is we limit the size to the total size of the GTT.
  610. * 4. The bo is marked as being snoopable. The backing pages are left
  611. * accessible directly by the CPU, but reads and writes by the GPU may
  612. * incur the cost of a snoop (unless you have an LLC architecture).
  613. *
  614. * Synchronisation between multiple users and the GPU is left to userspace
  615. * through the normal set-domain-ioctl. The kernel will enforce that the
  616. * GPU relinquishes the VMA before it is returned back to the system
  617. * i.e. upon free(), munmap() or process termination. However, the userspace
  618. * malloc() library may not immediately relinquish the VMA after free() and
  619. * instead reuse it whilst the GPU is still reading and writing to the VMA.
  620. * Caveat emptor.
  621. *
  622. * Also note, that the object created here is not currently a "first class"
  623. * object, in that several ioctls are banned. These are the CPU access
  624. * ioctls: mmap(), pwrite and pread. In practice, you are expected to use
  625. * direct access via your pointer rather than use those ioctls. Another
  626. * restriction is that we do not allow userptr surfaces to be pinned to the
  627. * hardware and so we reject any attempt to create a framebuffer out of a
  628. * userptr.
  629. *
  630. * If you think this is a good interface to use to pass GPU memory between
  631. * drivers, please use dma-buf instead. In fact, wherever possible use
  632. * dma-buf instead.
  633. */
  634. int
  635. i915_gem_userptr_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
  636. {
  637. struct drm_i915_private *dev_priv = to_i915(dev);
  638. struct drm_i915_gem_userptr *args = data;
  639. struct drm_i915_gem_object *obj;
  640. int ret;
  641. u32 handle;
  642. if (!HAS_LLC(dev_priv) && !HAS_SNOOP(dev_priv)) {
  643. /* We cannot support coherent userptr objects on hw without
  644. * LLC and broken snooping.
  645. */
  646. return -ENODEV;
  647. }
  648. if (args->flags & ~(I915_USERPTR_READ_ONLY |
  649. I915_USERPTR_UNSYNCHRONIZED))
  650. return -EINVAL;
  651. if (offset_in_page(args->user_ptr | args->user_size))
  652. return -EINVAL;
  653. if (!access_ok(args->flags & I915_USERPTR_READ_ONLY ? VERIFY_READ : VERIFY_WRITE,
  654. (char __user *)(unsigned long)args->user_ptr, args->user_size))
  655. return -EFAULT;
  656. if (args->flags & I915_USERPTR_READ_ONLY) {
  657. /* On almost all of the current hw, we cannot tell the GPU that a
  658. * page is readonly, so this is just a placeholder in the uAPI.
  659. */
  660. return -ENODEV;
  661. }
  662. obj = i915_gem_object_alloc(dev_priv);
  663. if (obj == NULL)
  664. return -ENOMEM;
  665. drm_gem_private_object_init(dev, &obj->base, args->user_size);
  666. i915_gem_object_init(obj, &i915_gem_userptr_ops);
  667. obj->cache_level = I915_CACHE_LLC;
  668. obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  669. obj->base.read_domains = I915_GEM_DOMAIN_CPU;
  670. obj->userptr.ptr = args->user_ptr;
  671. obj->userptr.read_only = !!(args->flags & I915_USERPTR_READ_ONLY);
  672. /* And keep a pointer to the current->mm for resolving the user pages
  673. * at binding. This means that we need to hook into the mmu_notifier
  674. * in order to detect if the mmu is destroyed.
  675. */
  676. ret = i915_gem_userptr_init__mm_struct(obj);
  677. if (ret == 0)
  678. ret = i915_gem_userptr_init__mmu_notifier(obj, args->flags);
  679. if (ret == 0)
  680. ret = drm_gem_handle_create(file, &obj->base, &handle);
  681. /* drop reference from allocate - handle holds it now */
  682. i915_gem_object_put(obj);
  683. if (ret)
  684. return ret;
  685. args->handle = handle;
  686. return 0;
  687. }
  688. void i915_gem_init_userptr(struct drm_i915_private *dev_priv)
  689. {
  690. mutex_init(&dev_priv->mm_lock);
  691. hash_init(dev_priv->mm_structs);
  692. }