i915_gem_userptr.c 22 KB

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