i915_gem_userptr.c 22 KB

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