i915_gem_userptr.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838
  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. struct i915_mmu_notifier *mn = mm->mn;
  248. mn = mm->mn;
  249. if (mn)
  250. return mn;
  251. down_write(&mm->mm->mmap_sem);
  252. mutex_lock(&to_i915(mm->dev)->mm_lock);
  253. if ((mn = mm->mn) == NULL) {
  254. mn = i915_mmu_notifier_create(mm->mm);
  255. if (!IS_ERR(mn))
  256. mm->mn = mn;
  257. }
  258. mutex_unlock(&to_i915(mm->dev)->mm_lock);
  259. up_write(&mm->mm->mmap_sem);
  260. return mn;
  261. }
  262. static int
  263. i915_gem_userptr_init__mmu_notifier(struct drm_i915_gem_object *obj,
  264. unsigned flags)
  265. {
  266. struct i915_mmu_notifier *mn;
  267. struct i915_mmu_object *mo;
  268. int ret;
  269. if (flags & I915_USERPTR_UNSYNCHRONIZED)
  270. return capable(CAP_SYS_ADMIN) ? 0 : -EPERM;
  271. if (WARN_ON(obj->userptr.mm == NULL))
  272. return -EINVAL;
  273. mn = i915_mmu_notifier_find(obj->userptr.mm);
  274. if (IS_ERR(mn))
  275. return PTR_ERR(mn);
  276. mo = kzalloc(sizeof(*mo), GFP_KERNEL);
  277. if (mo == NULL)
  278. return -ENOMEM;
  279. mo->mn = mn;
  280. mo->it.start = obj->userptr.ptr;
  281. mo->it.last = mo->it.start + obj->base.size - 1;
  282. mo->obj = obj;
  283. ret = i915_mmu_notifier_add(obj->base.dev, mn, mo);
  284. if (ret) {
  285. kfree(mo);
  286. return ret;
  287. }
  288. obj->userptr.mmu_object = mo;
  289. return 0;
  290. }
  291. static void
  292. i915_mmu_notifier_free(struct i915_mmu_notifier *mn,
  293. struct mm_struct *mm)
  294. {
  295. if (mn == NULL)
  296. return;
  297. mmu_notifier_unregister(&mn->mn, mm);
  298. kfree(mn);
  299. }
  300. #else
  301. static void
  302. i915_gem_userptr_release__mmu_notifier(struct drm_i915_gem_object *obj)
  303. {
  304. }
  305. static int
  306. i915_gem_userptr_init__mmu_notifier(struct drm_i915_gem_object *obj,
  307. unsigned flags)
  308. {
  309. if ((flags & I915_USERPTR_UNSYNCHRONIZED) == 0)
  310. return -ENODEV;
  311. if (!capable(CAP_SYS_ADMIN))
  312. return -EPERM;
  313. return 0;
  314. }
  315. static void
  316. i915_mmu_notifier_free(struct i915_mmu_notifier *mn,
  317. struct mm_struct *mm)
  318. {
  319. }
  320. #endif
  321. static struct i915_mm_struct *
  322. __i915_mm_struct_find(struct drm_i915_private *dev_priv, struct mm_struct *real)
  323. {
  324. struct i915_mm_struct *mm;
  325. /* Protected by dev_priv->mm_lock */
  326. hash_for_each_possible(dev_priv->mm_structs, mm, node, (unsigned long)real)
  327. if (mm->mm == real)
  328. return mm;
  329. return NULL;
  330. }
  331. static int
  332. i915_gem_userptr_init__mm_struct(struct drm_i915_gem_object *obj)
  333. {
  334. struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
  335. struct i915_mm_struct *mm;
  336. int ret = 0;
  337. /* During release of the GEM object we hold the struct_mutex. This
  338. * precludes us from calling mmput() at that time as that may be
  339. * the last reference and so call exit_mmap(). exit_mmap() will
  340. * attempt to reap the vma, and if we were holding a GTT mmap
  341. * would then call drm_gem_vm_close() and attempt to reacquire
  342. * the struct mutex. So in order to avoid that recursion, we have
  343. * to defer releasing the mm reference until after we drop the
  344. * struct_mutex, i.e. we need to schedule a worker to do the clean
  345. * up.
  346. */
  347. mutex_lock(&dev_priv->mm_lock);
  348. mm = __i915_mm_struct_find(dev_priv, current->mm);
  349. if (mm == NULL) {
  350. mm = kmalloc(sizeof(*mm), GFP_KERNEL);
  351. if (mm == NULL) {
  352. ret = -ENOMEM;
  353. goto out;
  354. }
  355. kref_init(&mm->kref);
  356. mm->dev = obj->base.dev;
  357. mm->mm = current->mm;
  358. atomic_inc(&current->mm->mm_count);
  359. mm->mn = NULL;
  360. /* Protected by dev_priv->mm_lock */
  361. hash_add(dev_priv->mm_structs,
  362. &mm->node, (unsigned long)mm->mm);
  363. } else
  364. kref_get(&mm->kref);
  365. obj->userptr.mm = mm;
  366. out:
  367. mutex_unlock(&dev_priv->mm_lock);
  368. return ret;
  369. }
  370. static void
  371. __i915_mm_struct_free__worker(struct work_struct *work)
  372. {
  373. struct i915_mm_struct *mm = container_of(work, typeof(*mm), work);
  374. i915_mmu_notifier_free(mm->mn, mm->mm);
  375. mmdrop(mm->mm);
  376. kfree(mm);
  377. }
  378. static void
  379. __i915_mm_struct_free(struct kref *kref)
  380. {
  381. struct i915_mm_struct *mm = container_of(kref, typeof(*mm), kref);
  382. /* Protected by dev_priv->mm_lock */
  383. hash_del(&mm->node);
  384. mutex_unlock(&to_i915(mm->dev)->mm_lock);
  385. INIT_WORK(&mm->work, __i915_mm_struct_free__worker);
  386. schedule_work(&mm->work);
  387. }
  388. static void
  389. i915_gem_userptr_release__mm_struct(struct drm_i915_gem_object *obj)
  390. {
  391. if (obj->userptr.mm == NULL)
  392. return;
  393. kref_put_mutex(&obj->userptr.mm->kref,
  394. __i915_mm_struct_free,
  395. &to_i915(obj->base.dev)->mm_lock);
  396. obj->userptr.mm = NULL;
  397. }
  398. struct get_pages_work {
  399. struct work_struct work;
  400. struct drm_i915_gem_object *obj;
  401. struct task_struct *task;
  402. };
  403. #if IS_ENABLED(CONFIG_SWIOTLB)
  404. #define swiotlb_active() swiotlb_nr_tbl()
  405. #else
  406. #define swiotlb_active() 0
  407. #endif
  408. static int
  409. st_set_pages(struct sg_table **st, struct page **pvec, int num_pages)
  410. {
  411. struct scatterlist *sg;
  412. int ret, n;
  413. *st = kmalloc(sizeof(**st), GFP_KERNEL);
  414. if (*st == NULL)
  415. return -ENOMEM;
  416. if (swiotlb_active()) {
  417. ret = sg_alloc_table(*st, num_pages, GFP_KERNEL);
  418. if (ret)
  419. goto err;
  420. for_each_sg((*st)->sgl, sg, num_pages, n)
  421. sg_set_page(sg, pvec[n], PAGE_SIZE, 0);
  422. } else {
  423. ret = sg_alloc_table_from_pages(*st, pvec, num_pages,
  424. 0, num_pages << PAGE_SHIFT,
  425. GFP_KERNEL);
  426. if (ret)
  427. goto err;
  428. }
  429. return 0;
  430. err:
  431. kfree(*st);
  432. *st = NULL;
  433. return ret;
  434. }
  435. static void
  436. __i915_gem_userptr_get_pages_worker(struct work_struct *_work)
  437. {
  438. struct get_pages_work *work = container_of(_work, typeof(*work), work);
  439. struct drm_i915_gem_object *obj = work->obj;
  440. struct drm_device *dev = obj->base.dev;
  441. const int num_pages = obj->base.size >> PAGE_SHIFT;
  442. struct page **pvec;
  443. int pinned, ret;
  444. ret = -ENOMEM;
  445. pinned = 0;
  446. pvec = kmalloc(num_pages*sizeof(struct page *),
  447. GFP_TEMPORARY | __GFP_NOWARN | __GFP_NORETRY);
  448. if (pvec == NULL)
  449. pvec = drm_malloc_ab(num_pages, sizeof(struct page *));
  450. if (pvec != NULL) {
  451. struct mm_struct *mm = obj->userptr.mm->mm;
  452. down_read(&mm->mmap_sem);
  453. while (pinned < num_pages) {
  454. ret = get_user_pages(work->task, mm,
  455. obj->userptr.ptr + pinned * PAGE_SIZE,
  456. num_pages - pinned,
  457. !obj->userptr.read_only, 0,
  458. pvec + pinned, NULL);
  459. if (ret < 0)
  460. break;
  461. pinned += ret;
  462. }
  463. up_read(&mm->mmap_sem);
  464. }
  465. mutex_lock(&dev->struct_mutex);
  466. if (obj->userptr.work != &work->work) {
  467. ret = 0;
  468. } else if (pinned == num_pages) {
  469. ret = st_set_pages(&obj->pages, pvec, num_pages);
  470. if (ret == 0) {
  471. list_add_tail(&obj->global_list, &to_i915(dev)->mm.unbound_list);
  472. pinned = 0;
  473. }
  474. }
  475. obj->userptr.work = ERR_PTR(ret);
  476. obj->userptr.workers--;
  477. drm_gem_object_unreference(&obj->base);
  478. mutex_unlock(&dev->struct_mutex);
  479. release_pages(pvec, pinned, 0);
  480. drm_free_large(pvec);
  481. put_task_struct(work->task);
  482. kfree(work);
  483. }
  484. static int
  485. i915_gem_userptr_get_pages(struct drm_i915_gem_object *obj)
  486. {
  487. const int num_pages = obj->base.size >> PAGE_SHIFT;
  488. struct page **pvec;
  489. int pinned, ret;
  490. /* If userspace should engineer that these pages are replaced in
  491. * the vma between us binding this page into the GTT and completion
  492. * of rendering... Their loss. If they change the mapping of their
  493. * pages they need to create a new bo to point to the new vma.
  494. *
  495. * However, that still leaves open the possibility of the vma
  496. * being copied upon fork. Which falls under the same userspace
  497. * synchronisation issue as a regular bo, except that this time
  498. * the process may not be expecting that a particular piece of
  499. * memory is tied to the GPU.
  500. *
  501. * Fortunately, we can hook into the mmu_notifier in order to
  502. * discard the page references prior to anything nasty happening
  503. * to the vma (discard or cloning) which should prevent the more
  504. * egregious cases from causing harm.
  505. */
  506. pvec = NULL;
  507. pinned = 0;
  508. if (obj->userptr.mm->mm == current->mm) {
  509. pvec = kmalloc(num_pages*sizeof(struct page *),
  510. GFP_TEMPORARY | __GFP_NOWARN | __GFP_NORETRY);
  511. if (pvec == NULL) {
  512. pvec = drm_malloc_ab(num_pages, sizeof(struct page *));
  513. if (pvec == NULL)
  514. return -ENOMEM;
  515. }
  516. pinned = __get_user_pages_fast(obj->userptr.ptr, num_pages,
  517. !obj->userptr.read_only, pvec);
  518. }
  519. if (pinned < num_pages) {
  520. if (pinned < 0) {
  521. ret = pinned;
  522. pinned = 0;
  523. } else {
  524. /* Spawn a worker so that we can acquire the
  525. * user pages without holding our mutex. Access
  526. * to the user pages requires mmap_sem, and we have
  527. * a strict lock ordering of mmap_sem, struct_mutex -
  528. * we already hold struct_mutex here and so cannot
  529. * call gup without encountering a lock inversion.
  530. *
  531. * Userspace will keep on repeating the operation
  532. * (thanks to EAGAIN) until either we hit the fast
  533. * path or the worker completes. If the worker is
  534. * cancelled or superseded, the task is still run
  535. * but the results ignored. (This leads to
  536. * complications that we may have a stray object
  537. * refcount that we need to be wary of when
  538. * checking for existing objects during creation.)
  539. * If the worker encounters an error, it reports
  540. * that error back to this function through
  541. * obj->userptr.work = ERR_PTR.
  542. */
  543. ret = -EAGAIN;
  544. if (obj->userptr.work == NULL &&
  545. obj->userptr.workers < I915_GEM_USERPTR_MAX_WORKERS) {
  546. struct get_pages_work *work;
  547. work = kmalloc(sizeof(*work), GFP_KERNEL);
  548. if (work != NULL) {
  549. obj->userptr.work = &work->work;
  550. obj->userptr.workers++;
  551. work->obj = obj;
  552. drm_gem_object_reference(&obj->base);
  553. work->task = current;
  554. get_task_struct(work->task);
  555. INIT_WORK(&work->work, __i915_gem_userptr_get_pages_worker);
  556. schedule_work(&work->work);
  557. } else
  558. ret = -ENOMEM;
  559. } else {
  560. if (IS_ERR(obj->userptr.work)) {
  561. ret = PTR_ERR(obj->userptr.work);
  562. obj->userptr.work = NULL;
  563. }
  564. }
  565. }
  566. } else {
  567. ret = st_set_pages(&obj->pages, pvec, num_pages);
  568. if (ret == 0) {
  569. obj->userptr.work = NULL;
  570. pinned = 0;
  571. }
  572. }
  573. release_pages(pvec, pinned, 0);
  574. drm_free_large(pvec);
  575. return ret;
  576. }
  577. static void
  578. i915_gem_userptr_put_pages(struct drm_i915_gem_object *obj)
  579. {
  580. struct sg_page_iter sg_iter;
  581. BUG_ON(obj->userptr.work != NULL);
  582. if (obj->madv != I915_MADV_WILLNEED)
  583. obj->dirty = 0;
  584. for_each_sg_page(obj->pages->sgl, &sg_iter, obj->pages->nents, 0) {
  585. struct page *page = sg_page_iter_page(&sg_iter);
  586. if (obj->dirty)
  587. set_page_dirty(page);
  588. mark_page_accessed(page);
  589. page_cache_release(page);
  590. }
  591. obj->dirty = 0;
  592. sg_free_table(obj->pages);
  593. kfree(obj->pages);
  594. }
  595. static void
  596. i915_gem_userptr_release(struct drm_i915_gem_object *obj)
  597. {
  598. i915_gem_userptr_release__mmu_notifier(obj);
  599. i915_gem_userptr_release__mm_struct(obj);
  600. }
  601. static int
  602. i915_gem_userptr_dmabuf_export(struct drm_i915_gem_object *obj)
  603. {
  604. if (obj->userptr.mmu_object)
  605. return 0;
  606. return i915_gem_userptr_init__mmu_notifier(obj, 0);
  607. }
  608. static const struct drm_i915_gem_object_ops i915_gem_userptr_ops = {
  609. .dmabuf_export = i915_gem_userptr_dmabuf_export,
  610. .get_pages = i915_gem_userptr_get_pages,
  611. .put_pages = i915_gem_userptr_put_pages,
  612. .release = i915_gem_userptr_release,
  613. };
  614. /**
  615. * Creates a new mm object that wraps some normal memory from the process
  616. * context - user memory.
  617. *
  618. * We impose several restrictions upon the memory being mapped
  619. * into the GPU.
  620. * 1. It must be page aligned (both start/end addresses, i.e ptr and size).
  621. * 2. It must be normal system memory, not a pointer into another map of IO
  622. * space (e.g. it must not be a GTT mmapping of another object).
  623. * 3. We only allow a bo as large as we could in theory map into the GTT,
  624. * that is we limit the size to the total size of the GTT.
  625. * 4. The bo is marked as being snoopable. The backing pages are left
  626. * accessible directly by the CPU, but reads and writes by the GPU may
  627. * incur the cost of a snoop (unless you have an LLC architecture).
  628. *
  629. * Synchronisation between multiple users and the GPU is left to userspace
  630. * through the normal set-domain-ioctl. The kernel will enforce that the
  631. * GPU relinquishes the VMA before it is returned back to the system
  632. * i.e. upon free(), munmap() or process termination. However, the userspace
  633. * malloc() library may not immediately relinquish the VMA after free() and
  634. * instead reuse it whilst the GPU is still reading and writing to the VMA.
  635. * Caveat emptor.
  636. *
  637. * Also note, that the object created here is not currently a "first class"
  638. * object, in that several ioctls are banned. These are the CPU access
  639. * ioctls: mmap(), pwrite and pread. In practice, you are expected to use
  640. * direct access via your pointer rather than use those ioctls.
  641. *
  642. * If you think this is a good interface to use to pass GPU memory between
  643. * drivers, please use dma-buf instead. In fact, wherever possible use
  644. * dma-buf instead.
  645. */
  646. int
  647. i915_gem_userptr_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
  648. {
  649. struct drm_i915_private *dev_priv = dev->dev_private;
  650. struct drm_i915_gem_userptr *args = data;
  651. struct drm_i915_gem_object *obj;
  652. int ret;
  653. u32 handle;
  654. if (args->flags & ~(I915_USERPTR_READ_ONLY |
  655. I915_USERPTR_UNSYNCHRONIZED))
  656. return -EINVAL;
  657. if (offset_in_page(args->user_ptr | args->user_size))
  658. return -EINVAL;
  659. if (args->user_size > dev_priv->gtt.base.total)
  660. return -E2BIG;
  661. if (!access_ok(args->flags & I915_USERPTR_READ_ONLY ? VERIFY_READ : VERIFY_WRITE,
  662. (char __user *)(unsigned long)args->user_ptr, args->user_size))
  663. return -EFAULT;
  664. if (args->flags & I915_USERPTR_READ_ONLY) {
  665. /* On almost all of the current hw, we cannot tell the GPU that a
  666. * page is readonly, so this is just a placeholder in the uAPI.
  667. */
  668. return -ENODEV;
  669. }
  670. obj = i915_gem_object_alloc(dev);
  671. if (obj == NULL)
  672. return -ENOMEM;
  673. drm_gem_private_object_init(dev, &obj->base, args->user_size);
  674. i915_gem_object_init(obj, &i915_gem_userptr_ops);
  675. obj->cache_level = I915_CACHE_LLC;
  676. obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  677. obj->base.read_domains = I915_GEM_DOMAIN_CPU;
  678. obj->userptr.ptr = args->user_ptr;
  679. obj->userptr.read_only = !!(args->flags & I915_USERPTR_READ_ONLY);
  680. /* And keep a pointer to the current->mm for resolving the user pages
  681. * at binding. This means that we need to hook into the mmu_notifier
  682. * in order to detect if the mmu is destroyed.
  683. */
  684. ret = i915_gem_userptr_init__mm_struct(obj);
  685. if (ret == 0)
  686. ret = i915_gem_userptr_init__mmu_notifier(obj, args->flags);
  687. if (ret == 0)
  688. ret = drm_gem_handle_create(file, &obj->base, &handle);
  689. /* drop reference from allocate - handle holds it now */
  690. drm_gem_object_unreference_unlocked(&obj->base);
  691. if (ret)
  692. return ret;
  693. args->handle = handle;
  694. return 0;
  695. }
  696. int
  697. i915_gem_init_userptr(struct drm_device *dev)
  698. {
  699. struct drm_i915_private *dev_priv = to_i915(dev);
  700. mutex_init(&dev_priv->mm_lock);
  701. hash_init(dev_priv->mm_structs);
  702. return 0;
  703. }