i915_gem_userptr.c 22 KB

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