i915_gem_userptr.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818
  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_device *dev;
  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. };
  49. struct i915_mmu_object {
  50. struct i915_mmu_notifier *mn;
  51. struct drm_i915_gem_object *obj;
  52. struct interval_tree_node it;
  53. struct list_head link;
  54. struct work_struct work;
  55. bool attached;
  56. };
  57. static void cancel_userptr(struct work_struct *work)
  58. {
  59. struct i915_mmu_object *mo = container_of(work, typeof(*mo), work);
  60. struct drm_i915_gem_object *obj = mo->obj;
  61. struct drm_device *dev = obj->base.dev;
  62. mutex_lock(&dev->struct_mutex);
  63. /* Cancel any active worker and force us to re-evaluate gup */
  64. obj->userptr.work = NULL;
  65. if (obj->pages != NULL) {
  66. struct drm_i915_private *dev_priv = to_i915(dev);
  67. struct i915_vma *vma, *tmp;
  68. bool was_interruptible;
  69. was_interruptible = dev_priv->mm.interruptible;
  70. dev_priv->mm.interruptible = false;
  71. list_for_each_entry_safe(vma, tmp, &obj->vma_list, obj_link) {
  72. int ret = i915_vma_unbind(vma);
  73. WARN_ON(ret && ret != -EIO);
  74. }
  75. WARN_ON(i915_gem_object_put_pages(obj));
  76. dev_priv->mm.interruptible = was_interruptible;
  77. }
  78. drm_gem_object_unreference(&obj->base);
  79. mutex_unlock(&dev->struct_mutex);
  80. }
  81. static void add_object(struct i915_mmu_object *mo)
  82. {
  83. if (mo->attached)
  84. return;
  85. interval_tree_insert(&mo->it, &mo->mn->objects);
  86. mo->attached = true;
  87. }
  88. static void del_object(struct i915_mmu_object *mo)
  89. {
  90. if (!mo->attached)
  91. return;
  92. interval_tree_remove(&mo->it, &mo->mn->objects);
  93. mo->attached = false;
  94. }
  95. static void i915_gem_userptr_mn_invalidate_range_start(struct mmu_notifier *_mn,
  96. struct mm_struct *mm,
  97. unsigned long start,
  98. unsigned long end)
  99. {
  100. struct i915_mmu_notifier *mn =
  101. container_of(_mn, struct i915_mmu_notifier, mn);
  102. struct i915_mmu_object *mo;
  103. struct interval_tree_node *it;
  104. LIST_HEAD(cancelled);
  105. if (RB_EMPTY_ROOT(&mn->objects))
  106. return;
  107. /* interval ranges are inclusive, but invalidate range is exclusive */
  108. end--;
  109. spin_lock(&mn->lock);
  110. it = interval_tree_iter_first(&mn->objects, start, end);
  111. while (it) {
  112. /* The mmu_object is released late when destroying the
  113. * GEM object so it is entirely possible to gain a
  114. * reference on an object in the process of being freed
  115. * since our serialisation is via the spinlock and not
  116. * the struct_mutex - and consequently use it after it
  117. * is freed and then double free it. To prevent that
  118. * use-after-free we only acquire a reference on the
  119. * object if it is not in the process of being destroyed.
  120. */
  121. mo = container_of(it, struct i915_mmu_object, it);
  122. if (kref_get_unless_zero(&mo->obj->base.refcount))
  123. schedule_work(&mo->work);
  124. list_add(&mo->link, &cancelled);
  125. it = interval_tree_iter_next(it, start, end);
  126. }
  127. list_for_each_entry(mo, &cancelled, link)
  128. del_object(mo);
  129. spin_unlock(&mn->lock);
  130. }
  131. static const struct mmu_notifier_ops i915_gem_userptr_notifier = {
  132. .invalidate_range_start = i915_gem_userptr_mn_invalidate_range_start,
  133. };
  134. static struct i915_mmu_notifier *
  135. i915_mmu_notifier_create(struct mm_struct *mm)
  136. {
  137. struct i915_mmu_notifier *mn;
  138. int ret;
  139. mn = kmalloc(sizeof(*mn), GFP_KERNEL);
  140. if (mn == NULL)
  141. return ERR_PTR(-ENOMEM);
  142. spin_lock_init(&mn->lock);
  143. mn->mn.ops = &i915_gem_userptr_notifier;
  144. mn->objects = RB_ROOT;
  145. /* Protected by mmap_sem (write-lock) */
  146. ret = __mmu_notifier_register(&mn->mn, mm);
  147. if (ret) {
  148. kfree(mn);
  149. return ERR_PTR(ret);
  150. }
  151. return mn;
  152. }
  153. static void
  154. i915_gem_userptr_release__mmu_notifier(struct drm_i915_gem_object *obj)
  155. {
  156. struct i915_mmu_object *mo;
  157. mo = obj->userptr.mmu_object;
  158. if (mo == NULL)
  159. return;
  160. spin_lock(&mo->mn->lock);
  161. del_object(mo);
  162. spin_unlock(&mo->mn->lock);
  163. kfree(mo);
  164. obj->userptr.mmu_object = NULL;
  165. }
  166. static struct i915_mmu_notifier *
  167. i915_mmu_notifier_find(struct i915_mm_struct *mm)
  168. {
  169. struct i915_mmu_notifier *mn = mm->mn;
  170. mn = mm->mn;
  171. if (mn)
  172. return mn;
  173. down_write(&mm->mm->mmap_sem);
  174. mutex_lock(&to_i915(mm->dev)->mm_lock);
  175. if ((mn = mm->mn) == NULL) {
  176. mn = i915_mmu_notifier_create(mm->mm);
  177. if (!IS_ERR(mn))
  178. mm->mn = mn;
  179. }
  180. mutex_unlock(&to_i915(mm->dev)->mm_lock);
  181. up_write(&mm->mm->mmap_sem);
  182. return mn;
  183. }
  184. static int
  185. i915_gem_userptr_init__mmu_notifier(struct drm_i915_gem_object *obj,
  186. unsigned flags)
  187. {
  188. struct i915_mmu_notifier *mn;
  189. struct i915_mmu_object *mo;
  190. if (flags & I915_USERPTR_UNSYNCHRONIZED)
  191. return capable(CAP_SYS_ADMIN) ? 0 : -EPERM;
  192. if (WARN_ON(obj->userptr.mm == NULL))
  193. return -EINVAL;
  194. mn = i915_mmu_notifier_find(obj->userptr.mm);
  195. if (IS_ERR(mn))
  196. return PTR_ERR(mn);
  197. mo = kzalloc(sizeof(*mo), GFP_KERNEL);
  198. if (mo == NULL)
  199. return -ENOMEM;
  200. mo->mn = mn;
  201. mo->obj = obj;
  202. mo->it.start = obj->userptr.ptr;
  203. mo->it.last = obj->userptr.ptr + obj->base.size - 1;
  204. INIT_WORK(&mo->work, cancel_userptr);
  205. obj->userptr.mmu_object = mo;
  206. return 0;
  207. }
  208. static void
  209. i915_mmu_notifier_free(struct i915_mmu_notifier *mn,
  210. struct mm_struct *mm)
  211. {
  212. if (mn == NULL)
  213. return;
  214. mmu_notifier_unregister(&mn->mn, mm);
  215. kfree(mn);
  216. }
  217. #else
  218. static void
  219. i915_gem_userptr_release__mmu_notifier(struct drm_i915_gem_object *obj)
  220. {
  221. }
  222. static int
  223. i915_gem_userptr_init__mmu_notifier(struct drm_i915_gem_object *obj,
  224. unsigned flags)
  225. {
  226. if ((flags & I915_USERPTR_UNSYNCHRONIZED) == 0)
  227. return -ENODEV;
  228. if (!capable(CAP_SYS_ADMIN))
  229. return -EPERM;
  230. return 0;
  231. }
  232. static void
  233. i915_mmu_notifier_free(struct i915_mmu_notifier *mn,
  234. struct mm_struct *mm)
  235. {
  236. }
  237. #endif
  238. static struct i915_mm_struct *
  239. __i915_mm_struct_find(struct drm_i915_private *dev_priv, struct mm_struct *real)
  240. {
  241. struct i915_mm_struct *mm;
  242. /* Protected by dev_priv->mm_lock */
  243. hash_for_each_possible(dev_priv->mm_structs, mm, node, (unsigned long)real)
  244. if (mm->mm == real)
  245. return mm;
  246. return NULL;
  247. }
  248. static int
  249. i915_gem_userptr_init__mm_struct(struct drm_i915_gem_object *obj)
  250. {
  251. struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
  252. struct i915_mm_struct *mm;
  253. int ret = 0;
  254. /* During release of the GEM object we hold the struct_mutex. This
  255. * precludes us from calling mmput() at that time as that may be
  256. * the last reference and so call exit_mmap(). exit_mmap() will
  257. * attempt to reap the vma, and if we were holding a GTT mmap
  258. * would then call drm_gem_vm_close() and attempt to reacquire
  259. * the struct mutex. So in order to avoid that recursion, we have
  260. * to defer releasing the mm reference until after we drop the
  261. * struct_mutex, i.e. we need to schedule a worker to do the clean
  262. * up.
  263. */
  264. mutex_lock(&dev_priv->mm_lock);
  265. mm = __i915_mm_struct_find(dev_priv, current->mm);
  266. if (mm == NULL) {
  267. mm = kmalloc(sizeof(*mm), GFP_KERNEL);
  268. if (mm == NULL) {
  269. ret = -ENOMEM;
  270. goto out;
  271. }
  272. kref_init(&mm->kref);
  273. mm->dev = obj->base.dev;
  274. mm->mm = current->mm;
  275. atomic_inc(&current->mm->mm_count);
  276. mm->mn = NULL;
  277. /* Protected by dev_priv->mm_lock */
  278. hash_add(dev_priv->mm_structs,
  279. &mm->node, (unsigned long)mm->mm);
  280. } else
  281. kref_get(&mm->kref);
  282. obj->userptr.mm = mm;
  283. out:
  284. mutex_unlock(&dev_priv->mm_lock);
  285. return ret;
  286. }
  287. static void
  288. __i915_mm_struct_free__worker(struct work_struct *work)
  289. {
  290. struct i915_mm_struct *mm = container_of(work, typeof(*mm), work);
  291. i915_mmu_notifier_free(mm->mn, mm->mm);
  292. mmdrop(mm->mm);
  293. kfree(mm);
  294. }
  295. static void
  296. __i915_mm_struct_free(struct kref *kref)
  297. {
  298. struct i915_mm_struct *mm = container_of(kref, typeof(*mm), kref);
  299. /* Protected by dev_priv->mm_lock */
  300. hash_del(&mm->node);
  301. mutex_unlock(&to_i915(mm->dev)->mm_lock);
  302. INIT_WORK(&mm->work, __i915_mm_struct_free__worker);
  303. schedule_work(&mm->work);
  304. }
  305. static void
  306. i915_gem_userptr_release__mm_struct(struct drm_i915_gem_object *obj)
  307. {
  308. if (obj->userptr.mm == NULL)
  309. return;
  310. kref_put_mutex(&obj->userptr.mm->kref,
  311. __i915_mm_struct_free,
  312. &to_i915(obj->base.dev)->mm_lock);
  313. obj->userptr.mm = NULL;
  314. }
  315. struct get_pages_work {
  316. struct work_struct work;
  317. struct drm_i915_gem_object *obj;
  318. struct task_struct *task;
  319. };
  320. #if IS_ENABLED(CONFIG_SWIOTLB)
  321. #define swiotlb_active() swiotlb_nr_tbl()
  322. #else
  323. #define swiotlb_active() 0
  324. #endif
  325. static int
  326. st_set_pages(struct sg_table **st, struct page **pvec, int num_pages)
  327. {
  328. struct scatterlist *sg;
  329. int ret, n;
  330. *st = kmalloc(sizeof(**st), GFP_KERNEL);
  331. if (*st == NULL)
  332. return -ENOMEM;
  333. if (swiotlb_active()) {
  334. ret = sg_alloc_table(*st, num_pages, GFP_KERNEL);
  335. if (ret)
  336. goto err;
  337. for_each_sg((*st)->sgl, sg, num_pages, n)
  338. sg_set_page(sg, pvec[n], PAGE_SIZE, 0);
  339. } else {
  340. ret = sg_alloc_table_from_pages(*st, pvec, num_pages,
  341. 0, num_pages << PAGE_SHIFT,
  342. GFP_KERNEL);
  343. if (ret)
  344. goto err;
  345. }
  346. return 0;
  347. err:
  348. kfree(*st);
  349. *st = NULL;
  350. return ret;
  351. }
  352. static int
  353. __i915_gem_userptr_set_pages(struct drm_i915_gem_object *obj,
  354. struct page **pvec, int num_pages)
  355. {
  356. int ret;
  357. ret = st_set_pages(&obj->pages, pvec, num_pages);
  358. if (ret)
  359. return ret;
  360. ret = i915_gem_gtt_prepare_object(obj);
  361. if (ret) {
  362. sg_free_table(obj->pages);
  363. kfree(obj->pages);
  364. obj->pages = NULL;
  365. }
  366. return ret;
  367. }
  368. static int
  369. __i915_gem_userptr_set_active(struct drm_i915_gem_object *obj,
  370. bool value)
  371. {
  372. int ret = 0;
  373. /* During mm_invalidate_range we need to cancel any userptr that
  374. * overlaps the range being invalidated. Doing so requires the
  375. * struct_mutex, and that risks recursion. In order to cause
  376. * recursion, the user must alias the userptr address space with
  377. * a GTT mmapping (possible with a MAP_FIXED) - then when we have
  378. * to invalidate that mmaping, mm_invalidate_range is called with
  379. * the userptr address *and* the struct_mutex held. To prevent that
  380. * we set a flag under the i915_mmu_notifier spinlock to indicate
  381. * whether this object is valid.
  382. */
  383. #if defined(CONFIG_MMU_NOTIFIER)
  384. if (obj->userptr.mmu_object == NULL)
  385. return 0;
  386. spin_lock(&obj->userptr.mmu_object->mn->lock);
  387. /* In order to serialise get_pages with an outstanding
  388. * cancel_userptr, we must drop the struct_mutex and try again.
  389. */
  390. if (!value)
  391. del_object(obj->userptr.mmu_object);
  392. else if (!work_pending(&obj->userptr.mmu_object->work))
  393. add_object(obj->userptr.mmu_object);
  394. else
  395. ret = -EAGAIN;
  396. spin_unlock(&obj->userptr.mmu_object->mn->lock);
  397. #endif
  398. return ret;
  399. }
  400. static void
  401. __i915_gem_userptr_get_pages_worker(struct work_struct *_work)
  402. {
  403. struct get_pages_work *work = container_of(_work, typeof(*work), work);
  404. struct drm_i915_gem_object *obj = work->obj;
  405. struct drm_device *dev = obj->base.dev;
  406. const int npages = obj->base.size >> PAGE_SHIFT;
  407. struct page **pvec;
  408. int pinned, ret;
  409. ret = -ENOMEM;
  410. pinned = 0;
  411. pvec = kmalloc(npages*sizeof(struct page *),
  412. GFP_TEMPORARY | __GFP_NOWARN | __GFP_NORETRY);
  413. if (pvec == NULL)
  414. pvec = drm_malloc_ab(npages, sizeof(struct page *));
  415. if (pvec != NULL) {
  416. struct mm_struct *mm = obj->userptr.mm->mm;
  417. down_read(&mm->mmap_sem);
  418. while (pinned < npages) {
  419. ret = get_user_pages_remote(work->task, mm,
  420. obj->userptr.ptr + pinned * PAGE_SIZE,
  421. npages - pinned,
  422. !obj->userptr.read_only, 0,
  423. pvec + pinned, NULL);
  424. if (ret < 0)
  425. break;
  426. pinned += ret;
  427. }
  428. up_read(&mm->mmap_sem);
  429. }
  430. mutex_lock(&dev->struct_mutex);
  431. if (obj->userptr.work == &work->work) {
  432. if (pinned == npages) {
  433. ret = __i915_gem_userptr_set_pages(obj, pvec, npages);
  434. if (ret == 0) {
  435. list_add_tail(&obj->global_list,
  436. &to_i915(dev)->mm.unbound_list);
  437. obj->get_page.sg = obj->pages->sgl;
  438. obj->get_page.last = 0;
  439. pinned = 0;
  440. }
  441. }
  442. obj->userptr.work = ERR_PTR(ret);
  443. if (ret)
  444. __i915_gem_userptr_set_active(obj, false);
  445. }
  446. obj->userptr.workers--;
  447. drm_gem_object_unreference(&obj->base);
  448. mutex_unlock(&dev->struct_mutex);
  449. release_pages(pvec, pinned, 0);
  450. drm_free_large(pvec);
  451. put_task_struct(work->task);
  452. kfree(work);
  453. }
  454. static int
  455. __i915_gem_userptr_get_pages_schedule(struct drm_i915_gem_object *obj,
  456. bool *active)
  457. {
  458. struct get_pages_work *work;
  459. /* Spawn a worker so that we can acquire the
  460. * user pages without holding our mutex. Access
  461. * to the user pages requires mmap_sem, and we have
  462. * a strict lock ordering of mmap_sem, struct_mutex -
  463. * we already hold struct_mutex here and so cannot
  464. * call gup without encountering a lock inversion.
  465. *
  466. * Userspace will keep on repeating the operation
  467. * (thanks to EAGAIN) until either we hit the fast
  468. * path or the worker completes. If the worker is
  469. * cancelled or superseded, the task is still run
  470. * but the results ignored. (This leads to
  471. * complications that we may have a stray object
  472. * refcount that we need to be wary of when
  473. * checking for existing objects during creation.)
  474. * If the worker encounters an error, it reports
  475. * that error back to this function through
  476. * obj->userptr.work = ERR_PTR.
  477. */
  478. if (obj->userptr.workers >= I915_GEM_USERPTR_MAX_WORKERS)
  479. return -EAGAIN;
  480. work = kmalloc(sizeof(*work), GFP_KERNEL);
  481. if (work == NULL)
  482. return -ENOMEM;
  483. obj->userptr.work = &work->work;
  484. obj->userptr.workers++;
  485. work->obj = obj;
  486. drm_gem_object_reference(&obj->base);
  487. work->task = current;
  488. get_task_struct(work->task);
  489. INIT_WORK(&work->work, __i915_gem_userptr_get_pages_worker);
  490. schedule_work(&work->work);
  491. *active = true;
  492. return -EAGAIN;
  493. }
  494. static int
  495. i915_gem_userptr_get_pages(struct drm_i915_gem_object *obj)
  496. {
  497. const int num_pages = obj->base.size >> PAGE_SHIFT;
  498. struct page **pvec;
  499. int pinned, ret;
  500. bool active;
  501. /* If userspace should engineer that these pages are replaced in
  502. * the vma between us binding this page into the GTT and completion
  503. * of rendering... Their loss. If they change the mapping of their
  504. * pages they need to create a new bo to point to the new vma.
  505. *
  506. * However, that still leaves open the possibility of the vma
  507. * being copied upon fork. Which falls under the same userspace
  508. * synchronisation issue as a regular bo, except that this time
  509. * the process may not be expecting that a particular piece of
  510. * memory is tied to the GPU.
  511. *
  512. * Fortunately, we can hook into the mmu_notifier in order to
  513. * discard the page references prior to anything nasty happening
  514. * to the vma (discard or cloning) which should prevent the more
  515. * egregious cases from causing harm.
  516. */
  517. if (IS_ERR(obj->userptr.work)) {
  518. /* active flag will have been dropped already by the worker */
  519. ret = PTR_ERR(obj->userptr.work);
  520. obj->userptr.work = NULL;
  521. return ret;
  522. }
  523. if (obj->userptr.work)
  524. /* active flag should still be held for the pending work */
  525. return -EAGAIN;
  526. /* Let the mmu-notifier know that we have begun and need cancellation */
  527. ret = __i915_gem_userptr_set_active(obj, true);
  528. if (ret)
  529. return ret;
  530. pvec = NULL;
  531. pinned = 0;
  532. if (obj->userptr.mm->mm == current->mm) {
  533. pvec = kmalloc(num_pages*sizeof(struct page *),
  534. GFP_TEMPORARY | __GFP_NOWARN | __GFP_NORETRY);
  535. if (pvec == NULL) {
  536. pvec = drm_malloc_ab(num_pages, sizeof(struct page *));
  537. if (pvec == NULL) {
  538. __i915_gem_userptr_set_active(obj, false);
  539. return -ENOMEM;
  540. }
  541. }
  542. pinned = __get_user_pages_fast(obj->userptr.ptr, num_pages,
  543. !obj->userptr.read_only, pvec);
  544. }
  545. active = false;
  546. if (pinned < 0)
  547. ret = pinned, pinned = 0;
  548. else if (pinned < num_pages)
  549. ret = __i915_gem_userptr_get_pages_schedule(obj, &active);
  550. else
  551. ret = __i915_gem_userptr_set_pages(obj, pvec, num_pages);
  552. if (ret) {
  553. __i915_gem_userptr_set_active(obj, active);
  554. release_pages(pvec, pinned, 0);
  555. }
  556. drm_free_large(pvec);
  557. return ret;
  558. }
  559. static void
  560. i915_gem_userptr_put_pages(struct drm_i915_gem_object *obj)
  561. {
  562. struct sg_page_iter sg_iter;
  563. BUG_ON(obj->userptr.work != NULL);
  564. __i915_gem_userptr_set_active(obj, false);
  565. if (obj->madv != I915_MADV_WILLNEED)
  566. obj->dirty = 0;
  567. i915_gem_gtt_finish_object(obj);
  568. for_each_sg_page(obj->pages->sgl, &sg_iter, obj->pages->nents, 0) {
  569. struct page *page = sg_page_iter_page(&sg_iter);
  570. if (obj->dirty)
  571. set_page_dirty(page);
  572. mark_page_accessed(page);
  573. put_page(page);
  574. }
  575. obj->dirty = 0;
  576. sg_free_table(obj->pages);
  577. kfree(obj->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. .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_gem_userptr *args = data;
  638. struct drm_i915_gem_object *obj;
  639. int ret;
  640. u32 handle;
  641. if (args->flags & ~(I915_USERPTR_READ_ONLY |
  642. I915_USERPTR_UNSYNCHRONIZED))
  643. return -EINVAL;
  644. if (offset_in_page(args->user_ptr | args->user_size))
  645. return -EINVAL;
  646. if (!access_ok(args->flags & I915_USERPTR_READ_ONLY ? VERIFY_READ : VERIFY_WRITE,
  647. (char __user *)(unsigned long)args->user_ptr, args->user_size))
  648. return -EFAULT;
  649. if (args->flags & I915_USERPTR_READ_ONLY) {
  650. /* On almost all of the current hw, we cannot tell the GPU that a
  651. * page is readonly, so this is just a placeholder in the uAPI.
  652. */
  653. return -ENODEV;
  654. }
  655. obj = i915_gem_object_alloc(dev);
  656. if (obj == NULL)
  657. return -ENOMEM;
  658. drm_gem_private_object_init(dev, &obj->base, args->user_size);
  659. i915_gem_object_init(obj, &i915_gem_userptr_ops);
  660. obj->cache_level = I915_CACHE_LLC;
  661. obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  662. obj->base.read_domains = I915_GEM_DOMAIN_CPU;
  663. obj->userptr.ptr = args->user_ptr;
  664. obj->userptr.read_only = !!(args->flags & I915_USERPTR_READ_ONLY);
  665. /* And keep a pointer to the current->mm for resolving the user pages
  666. * at binding. This means that we need to hook into the mmu_notifier
  667. * in order to detect if the mmu is destroyed.
  668. */
  669. ret = i915_gem_userptr_init__mm_struct(obj);
  670. if (ret == 0)
  671. ret = i915_gem_userptr_init__mmu_notifier(obj, args->flags);
  672. if (ret == 0)
  673. ret = drm_gem_handle_create(file, &obj->base, &handle);
  674. /* drop reference from allocate - handle holds it now */
  675. drm_gem_object_unreference_unlocked(&obj->base);
  676. if (ret)
  677. return ret;
  678. args->handle = handle;
  679. return 0;
  680. }
  681. int
  682. i915_gem_init_userptr(struct drm_device *dev)
  683. {
  684. struct drm_i915_private *dev_priv = to_i915(dev);
  685. mutex_init(&dev_priv->mm_lock);
  686. hash_init(dev_priv->mm_structs);
  687. return 0;
  688. }