i915_gem_userptr.c 21 KB

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