i915_gem_userptr.c 22 KB

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