i915_gem_userptr.c 22 KB

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