i915_gem_userptr.c 22 KB

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