i915_gem_userptr.c 22 KB

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