i915_gem_userptr.c 24 KB

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