i915_gem_userptr.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899
  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. down_read(&mm->mmap_sem);
  486. while (pinned < npages) {
  487. ret = get_user_pages_remote(work->task, mm,
  488. obj->userptr.ptr + pinned * PAGE_SIZE,
  489. npages - pinned,
  490. !obj->userptr.read_only, 0,
  491. pvec + pinned, NULL);
  492. if (ret < 0)
  493. break;
  494. pinned += ret;
  495. }
  496. up_read(&mm->mmap_sem);
  497. }
  498. mutex_lock(&dev->struct_mutex);
  499. if (obj->userptr.work == &work->work) {
  500. if (pinned == npages) {
  501. ret = __i915_gem_userptr_set_pages(obj, pvec, npages);
  502. if (ret == 0) {
  503. list_add_tail(&obj->global_list,
  504. &to_i915(dev)->mm.unbound_list);
  505. obj->get_page.sg = obj->pages->sgl;
  506. obj->get_page.last = 0;
  507. pinned = 0;
  508. }
  509. }
  510. obj->userptr.work = ERR_PTR(ret);
  511. if (ret)
  512. __i915_gem_userptr_set_active(obj, false);
  513. }
  514. obj->userptr.workers--;
  515. drm_gem_object_unreference(&obj->base);
  516. mutex_unlock(&dev->struct_mutex);
  517. release_pages(pvec, pinned, 0);
  518. drm_free_large(pvec);
  519. put_task_struct(work->task);
  520. kfree(work);
  521. }
  522. static int
  523. __i915_gem_userptr_get_pages_schedule(struct drm_i915_gem_object *obj,
  524. bool *active)
  525. {
  526. struct get_pages_work *work;
  527. /* Spawn a worker so that we can acquire the
  528. * user pages without holding our mutex. Access
  529. * to the user pages requires mmap_sem, and we have
  530. * a strict lock ordering of mmap_sem, struct_mutex -
  531. * we already hold struct_mutex here and so cannot
  532. * call gup without encountering a lock inversion.
  533. *
  534. * Userspace will keep on repeating the operation
  535. * (thanks to EAGAIN) until either we hit the fast
  536. * path or the worker completes. If the worker is
  537. * cancelled or superseded, the task is still run
  538. * but the results ignored. (This leads to
  539. * complications that we may have a stray object
  540. * refcount that we need to be wary of when
  541. * checking for existing objects during creation.)
  542. * If the worker encounters an error, it reports
  543. * that error back to this function through
  544. * obj->userptr.work = ERR_PTR.
  545. */
  546. if (obj->userptr.workers >= I915_GEM_USERPTR_MAX_WORKERS)
  547. return -EAGAIN;
  548. work = kmalloc(sizeof(*work), GFP_KERNEL);
  549. if (work == NULL)
  550. return -ENOMEM;
  551. obj->userptr.work = &work->work;
  552. obj->userptr.workers++;
  553. work->obj = obj;
  554. drm_gem_object_reference(&obj->base);
  555. work->task = current;
  556. get_task_struct(work->task);
  557. INIT_WORK(&work->work, __i915_gem_userptr_get_pages_worker);
  558. schedule_work(&work->work);
  559. *active = true;
  560. return -EAGAIN;
  561. }
  562. static int
  563. i915_gem_userptr_get_pages(struct drm_i915_gem_object *obj)
  564. {
  565. const int num_pages = obj->base.size >> PAGE_SHIFT;
  566. struct page **pvec;
  567. int pinned, ret;
  568. bool active;
  569. /* If userspace should engineer that these pages are replaced in
  570. * the vma between us binding this page into the GTT and completion
  571. * of rendering... Their loss. If they change the mapping of their
  572. * pages they need to create a new bo to point to the new vma.
  573. *
  574. * However, that still leaves open the possibility of the vma
  575. * being copied upon fork. Which falls under the same userspace
  576. * synchronisation issue as a regular bo, except that this time
  577. * the process may not be expecting that a particular piece of
  578. * memory is tied to the GPU.
  579. *
  580. * Fortunately, we can hook into the mmu_notifier in order to
  581. * discard the page references prior to anything nasty happening
  582. * to the vma (discard or cloning) which should prevent the more
  583. * egregious cases from causing harm.
  584. */
  585. if (IS_ERR(obj->userptr.work)) {
  586. /* active flag will have been dropped already by the worker */
  587. ret = PTR_ERR(obj->userptr.work);
  588. obj->userptr.work = NULL;
  589. return ret;
  590. }
  591. if (obj->userptr.work)
  592. /* active flag should still be held for the pending work */
  593. return -EAGAIN;
  594. /* Let the mmu-notifier know that we have begun and need cancellation */
  595. ret = __i915_gem_userptr_set_active(obj, true);
  596. if (ret)
  597. return ret;
  598. pvec = NULL;
  599. pinned = 0;
  600. if (obj->userptr.mm->mm == current->mm) {
  601. pvec = kmalloc(num_pages*sizeof(struct page *),
  602. GFP_TEMPORARY | __GFP_NOWARN | __GFP_NORETRY);
  603. if (pvec == NULL) {
  604. pvec = drm_malloc_ab(num_pages, sizeof(struct page *));
  605. if (pvec == NULL) {
  606. __i915_gem_userptr_set_active(obj, false);
  607. return -ENOMEM;
  608. }
  609. }
  610. pinned = __get_user_pages_fast(obj->userptr.ptr, num_pages,
  611. !obj->userptr.read_only, pvec);
  612. }
  613. active = false;
  614. if (pinned < 0)
  615. ret = pinned, pinned = 0;
  616. else if (pinned < num_pages)
  617. ret = __i915_gem_userptr_get_pages_schedule(obj, &active);
  618. else
  619. ret = __i915_gem_userptr_set_pages(obj, pvec, num_pages);
  620. if (ret) {
  621. __i915_gem_userptr_set_active(obj, active);
  622. release_pages(pvec, pinned, 0);
  623. }
  624. drm_free_large(pvec);
  625. return ret;
  626. }
  627. static void
  628. i915_gem_userptr_put_pages(struct drm_i915_gem_object *obj)
  629. {
  630. struct sg_page_iter sg_iter;
  631. BUG_ON(obj->userptr.work != NULL);
  632. __i915_gem_userptr_set_active(obj, false);
  633. if (obj->madv != I915_MADV_WILLNEED)
  634. obj->dirty = 0;
  635. i915_gem_gtt_finish_object(obj);
  636. for_each_sg_page(obj->pages->sgl, &sg_iter, obj->pages->nents, 0) {
  637. struct page *page = sg_page_iter_page(&sg_iter);
  638. if (obj->dirty)
  639. set_page_dirty(page);
  640. mark_page_accessed(page);
  641. page_cache_release(page);
  642. }
  643. obj->dirty = 0;
  644. sg_free_table(obj->pages);
  645. kfree(obj->pages);
  646. }
  647. static void
  648. i915_gem_userptr_release(struct drm_i915_gem_object *obj)
  649. {
  650. i915_gem_userptr_release__mmu_notifier(obj);
  651. i915_gem_userptr_release__mm_struct(obj);
  652. }
  653. static int
  654. i915_gem_userptr_dmabuf_export(struct drm_i915_gem_object *obj)
  655. {
  656. if (obj->userptr.mmu_object)
  657. return 0;
  658. return i915_gem_userptr_init__mmu_notifier(obj, 0);
  659. }
  660. static const struct drm_i915_gem_object_ops i915_gem_userptr_ops = {
  661. .flags = I915_GEM_OBJECT_HAS_STRUCT_PAGE,
  662. .get_pages = i915_gem_userptr_get_pages,
  663. .put_pages = i915_gem_userptr_put_pages,
  664. .dmabuf_export = i915_gem_userptr_dmabuf_export,
  665. .release = i915_gem_userptr_release,
  666. };
  667. /**
  668. * Creates a new mm object that wraps some normal memory from the process
  669. * context - user memory.
  670. *
  671. * We impose several restrictions upon the memory being mapped
  672. * into the GPU.
  673. * 1. It must be page aligned (both start/end addresses, i.e ptr and size).
  674. * 2. It must be normal system memory, not a pointer into another map of IO
  675. * space (e.g. it must not be a GTT mmapping of another object).
  676. * 3. We only allow a bo as large as we could in theory map into the GTT,
  677. * that is we limit the size to the total size of the GTT.
  678. * 4. The bo is marked as being snoopable. The backing pages are left
  679. * accessible directly by the CPU, but reads and writes by the GPU may
  680. * incur the cost of a snoop (unless you have an LLC architecture).
  681. *
  682. * Synchronisation between multiple users and the GPU is left to userspace
  683. * through the normal set-domain-ioctl. The kernel will enforce that the
  684. * GPU relinquishes the VMA before it is returned back to the system
  685. * i.e. upon free(), munmap() or process termination. However, the userspace
  686. * malloc() library may not immediately relinquish the VMA after free() and
  687. * instead reuse it whilst the GPU is still reading and writing to the VMA.
  688. * Caveat emptor.
  689. *
  690. * Also note, that the object created here is not currently a "first class"
  691. * object, in that several ioctls are banned. These are the CPU access
  692. * ioctls: mmap(), pwrite and pread. In practice, you are expected to use
  693. * direct access via your pointer rather than use those ioctls. Another
  694. * restriction is that we do not allow userptr surfaces to be pinned to the
  695. * hardware and so we reject any attempt to create a framebuffer out of a
  696. * userptr.
  697. *
  698. * If you think this is a good interface to use to pass GPU memory between
  699. * drivers, please use dma-buf instead. In fact, wherever possible use
  700. * dma-buf instead.
  701. */
  702. int
  703. i915_gem_userptr_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
  704. {
  705. struct drm_i915_gem_userptr *args = data;
  706. struct drm_i915_gem_object *obj;
  707. int ret;
  708. u32 handle;
  709. if (args->flags & ~(I915_USERPTR_READ_ONLY |
  710. I915_USERPTR_UNSYNCHRONIZED))
  711. return -EINVAL;
  712. if (offset_in_page(args->user_ptr | args->user_size))
  713. return -EINVAL;
  714. if (!access_ok(args->flags & I915_USERPTR_READ_ONLY ? VERIFY_READ : VERIFY_WRITE,
  715. (char __user *)(unsigned long)args->user_ptr, args->user_size))
  716. return -EFAULT;
  717. if (args->flags & I915_USERPTR_READ_ONLY) {
  718. /* On almost all of the current hw, we cannot tell the GPU that a
  719. * page is readonly, so this is just a placeholder in the uAPI.
  720. */
  721. return -ENODEV;
  722. }
  723. obj = i915_gem_object_alloc(dev);
  724. if (obj == NULL)
  725. return -ENOMEM;
  726. drm_gem_private_object_init(dev, &obj->base, args->user_size);
  727. i915_gem_object_init(obj, &i915_gem_userptr_ops);
  728. obj->cache_level = I915_CACHE_LLC;
  729. obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  730. obj->base.read_domains = I915_GEM_DOMAIN_CPU;
  731. obj->userptr.ptr = args->user_ptr;
  732. obj->userptr.read_only = !!(args->flags & I915_USERPTR_READ_ONLY);
  733. /* And keep a pointer to the current->mm for resolving the user pages
  734. * at binding. This means that we need to hook into the mmu_notifier
  735. * in order to detect if the mmu is destroyed.
  736. */
  737. ret = i915_gem_userptr_init__mm_struct(obj);
  738. if (ret == 0)
  739. ret = i915_gem_userptr_init__mmu_notifier(obj, args->flags);
  740. if (ret == 0)
  741. ret = drm_gem_handle_create(file, &obj->base, &handle);
  742. /* drop reference from allocate - handle holds it now */
  743. drm_gem_object_unreference_unlocked(&obj->base);
  744. if (ret)
  745. return ret;
  746. args->handle = handle;
  747. return 0;
  748. }
  749. int
  750. i915_gem_init_userptr(struct drm_device *dev)
  751. {
  752. struct drm_i915_private *dev_priv = to_i915(dev);
  753. mutex_init(&dev_priv->mm_lock);
  754. hash_init(dev_priv->mm_structs);
  755. return 0;
  756. }