blob: e6b5938ce6e244064cf2c7a824b4403d8b073e32 [file] [log] [blame]
Chris Wilson5cc9ed42014-05-16 14:22:37 +01001/*
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
Maarten Lankhorstb588c922015-05-13 09:56:00 +020025#include <drm/drmP.h>
26#include <drm/i915_drm.h>
Chris Wilson5cc9ed42014-05-16 14:22:37 +010027#include "i915_drv.h"
28#include "i915_trace.h"
29#include "intel_drv.h"
30#include <linux/mmu_context.h>
31#include <linux/mmu_notifier.h>
32#include <linux/mempolicy.h>
33#include <linux/swap.h>
34
Chris Wilsonad46cb52014-08-07 14:20:40 +010035struct i915_mm_struct {
36 struct mm_struct *mm;
Chris Wilsonf470b192016-04-05 15:00:01 +010037 struct drm_i915_private *i915;
Chris Wilsonad46cb52014-08-07 14:20:40 +010038 struct i915_mmu_notifier *mn;
39 struct hlist_node node;
40 struct kref kref;
41 struct work_struct work;
42};
43
Chris Wilson5cc9ed42014-05-16 14:22:37 +010044#if defined(CONFIG_MMU_NOTIFIER)
45#include <linux/interval_tree.h>
46
47struct i915_mmu_notifier {
48 spinlock_t lock;
49 struct hlist_node node;
50 struct mmu_notifier mn;
51 struct rb_root objects;
Chris Wilson393afc22016-04-05 14:59:59 +010052 struct workqueue_struct *wq;
Chris Wilson5cc9ed42014-05-16 14:22:37 +010053};
54
55struct i915_mmu_object {
Chris Wilsonad46cb52014-08-07 14:20:40 +010056 struct i915_mmu_notifier *mn;
Chris Wilson768e1592016-01-21 17:32:43 +000057 struct drm_i915_gem_object *obj;
Chris Wilson5cc9ed42014-05-16 14:22:37 +010058 struct interval_tree_node it;
Chris Wilsonec8b0dd2014-07-21 13:21:23 +010059 struct list_head link;
Chris Wilson380996a2015-10-01 12:34:47 +010060 struct work_struct work;
Chris Wilson768e1592016-01-21 17:32:43 +000061 bool attached;
Chris Wilson5cc9ed42014-05-16 14:22:37 +010062};
63
Chris Wilson393afc22016-04-05 14:59:59 +010064static void wait_rendering(struct drm_i915_gem_object *obj)
65{
66 struct drm_device *dev = obj->base.dev;
67 struct drm_i915_gem_request *requests[I915_NUM_ENGINES];
Chris Wilson393afc22016-04-05 14:59:59 +010068 int i, n;
69
70 if (!obj->active)
71 return;
72
73 n = 0;
74 for (i = 0; i < I915_NUM_ENGINES; i++) {
75 struct drm_i915_gem_request *req;
76
77 req = obj->last_read_req[i];
78 if (req == NULL)
79 continue;
80
81 requests[n++] = i915_gem_request_reference(req);
82 }
83
Chris Wilson393afc22016-04-05 14:59:59 +010084 mutex_unlock(&dev->struct_mutex);
85
86 for (i = 0; i < n; i++)
Chris Wilson299259a2016-04-13 17:35:06 +010087 __i915_wait_request(requests[i], false, NULL, NULL);
Chris Wilson393afc22016-04-05 14:59:59 +010088
89 mutex_lock(&dev->struct_mutex);
90
91 for (i = 0; i < n; i++)
92 i915_gem_request_unreference(requests[i]);
93}
94
Chris Wilson768e1592016-01-21 17:32:43 +000095static void cancel_userptr(struct work_struct *work)
Chris Wilsonec8b0dd2014-07-21 13:21:23 +010096{
Chris Wilson380996a2015-10-01 12:34:47 +010097 struct i915_mmu_object *mo = container_of(work, typeof(*mo), work);
98 struct drm_i915_gem_object *obj = mo->obj;
Chris Wilsonec8b0dd2014-07-21 13:21:23 +010099 struct drm_device *dev = obj->base.dev;
Chris Wilsonec8b0dd2014-07-21 13:21:23 +0100100
101 mutex_lock(&dev->struct_mutex);
102 /* Cancel any active worker and force us to re-evaluate gup */
103 obj->userptr.work = NULL;
104
105 if (obj->pages != NULL) {
106 struct drm_i915_private *dev_priv = to_i915(dev);
107 struct i915_vma *vma, *tmp;
108 bool was_interruptible;
109
Chris Wilson393afc22016-04-05 14:59:59 +0100110 wait_rendering(obj);
111
Chris Wilsonec8b0dd2014-07-21 13:21:23 +0100112 was_interruptible = dev_priv->mm.interruptible;
113 dev_priv->mm.interruptible = false;
114
Chris Wilson1c7f4bc2016-02-26 11:03:19 +0000115 list_for_each_entry_safe(vma, tmp, &obj->vma_list, obj_link) {
Chris Wilsonec8b0dd2014-07-21 13:21:23 +0100116 int ret = i915_vma_unbind(vma);
117 WARN_ON(ret && ret != -EIO);
118 }
119 WARN_ON(i915_gem_object_put_pages(obj));
120
121 dev_priv->mm.interruptible = was_interruptible;
122 }
123
Chris Wilsonec8b0dd2014-07-21 13:21:23 +0100124 drm_gem_object_unreference(&obj->base);
125 mutex_unlock(&dev->struct_mutex);
Chris Wilsonec8b0dd2014-07-21 13:21:23 +0100126}
127
Chris Wilson768e1592016-01-21 17:32:43 +0000128static void add_object(struct i915_mmu_object *mo)
Chris Wilsonec8b0dd2014-07-21 13:21:23 +0100129{
Chris Wilson768e1592016-01-21 17:32:43 +0000130 if (mo->attached)
131 return;
Chris Wilsonec8b0dd2014-07-21 13:21:23 +0100132
Chris Wilson768e1592016-01-21 17:32:43 +0000133 interval_tree_insert(&mo->it, &mo->mn->objects);
134 mo->attached = true;
135}
Chris Wilsonec8b0dd2014-07-21 13:21:23 +0100136
Chris Wilson768e1592016-01-21 17:32:43 +0000137static void del_object(struct i915_mmu_object *mo)
138{
139 if (!mo->attached)
140 return;
141
142 interval_tree_remove(&mo->it, &mo->mn->objects);
143 mo->attached = false;
Chris Wilsonec8b0dd2014-07-21 13:21:23 +0100144}
145
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100146static void i915_gem_userptr_mn_invalidate_range_start(struct mmu_notifier *_mn,
147 struct mm_struct *mm,
148 unsigned long start,
149 unsigned long end)
150{
Chris Wilson380996a2015-10-01 12:34:47 +0100151 struct i915_mmu_notifier *mn =
152 container_of(_mn, struct i915_mmu_notifier, mn);
153 struct i915_mmu_object *mo;
Chris Wilson768e1592016-01-21 17:32:43 +0000154 struct interval_tree_node *it;
155 LIST_HEAD(cancelled);
156
157 if (RB_EMPTY_ROOT(&mn->objects))
158 return;
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100159
Chris Wilson380996a2015-10-01 12:34:47 +0100160 /* interval ranges are inclusive, but invalidate range is exclusive */
161 end--;
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100162
Chris Wilson380996a2015-10-01 12:34:47 +0100163 spin_lock(&mn->lock);
Chris Wilson768e1592016-01-21 17:32:43 +0000164 it = interval_tree_iter_first(&mn->objects, start, end);
165 while (it) {
166 /* The mmu_object is released late when destroying the
167 * GEM object so it is entirely possible to gain a
168 * reference on an object in the process of being freed
169 * since our serialisation is via the spinlock and not
170 * the struct_mutex - and consequently use it after it
171 * is freed and then double free it. To prevent that
172 * use-after-free we only acquire a reference on the
173 * object if it is not in the process of being destroyed.
174 */
175 mo = container_of(it, struct i915_mmu_object, it);
176 if (kref_get_unless_zero(&mo->obj->base.refcount))
Chris Wilson393afc22016-04-05 14:59:59 +0100177 queue_work(mn->wq, &mo->work);
Michał Winiarski460822b2015-02-03 15:48:17 +0100178
Chris Wilson768e1592016-01-21 17:32:43 +0000179 list_add(&mo->link, &cancelled);
180 it = interval_tree_iter_next(it, start, end);
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100181 }
Chris Wilson768e1592016-01-21 17:32:43 +0000182 list_for_each_entry(mo, &cancelled, link)
183 del_object(mo);
Chris Wilson380996a2015-10-01 12:34:47 +0100184 spin_unlock(&mn->lock);
Chris Wilson393afc22016-04-05 14:59:59 +0100185
186 flush_workqueue(mn->wq);
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100187}
188
189static const struct mmu_notifier_ops i915_gem_userptr_notifier = {
190 .invalidate_range_start = i915_gem_userptr_mn_invalidate_range_start,
191};
192
193static struct i915_mmu_notifier *
Chris Wilsonad46cb52014-08-07 14:20:40 +0100194i915_mmu_notifier_create(struct mm_struct *mm)
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100195{
Chris Wilsonad46cb52014-08-07 14:20:40 +0100196 struct i915_mmu_notifier *mn;
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100197 int ret;
198
Chris Wilsonad46cb52014-08-07 14:20:40 +0100199 mn = kmalloc(sizeof(*mn), GFP_KERNEL);
200 if (mn == NULL)
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100201 return ERR_PTR(-ENOMEM);
202
Chris Wilsonad46cb52014-08-07 14:20:40 +0100203 spin_lock_init(&mn->lock);
204 mn->mn.ops = &i915_gem_userptr_notifier;
205 mn->objects = RB_ROOT;
Chris Wilson393afc22016-04-05 14:59:59 +0100206 mn->wq = alloc_workqueue("i915-userptr-release", WQ_UNBOUND, 0);
207 if (mn->wq == NULL) {
208 kfree(mn);
209 return ERR_PTR(-ENOMEM);
210 }
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100211
Chris Wilsonad46cb52014-08-07 14:20:40 +0100212 /* Protected by mmap_sem (write-lock) */
213 ret = __mmu_notifier_register(&mn->mn, mm);
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100214 if (ret) {
Chris Wilson393afc22016-04-05 14:59:59 +0100215 destroy_workqueue(mn->wq);
Chris Wilsonad46cb52014-08-07 14:20:40 +0100216 kfree(mn);
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100217 return ERR_PTR(ret);
218 }
219
Chris Wilsonad46cb52014-08-07 14:20:40 +0100220 return mn;
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100221}
222
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100223static void
224i915_gem_userptr_release__mmu_notifier(struct drm_i915_gem_object *obj)
225{
Chris Wilsonad46cb52014-08-07 14:20:40 +0100226 struct i915_mmu_object *mo;
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100227
Chris Wilsonad46cb52014-08-07 14:20:40 +0100228 mo = obj->userptr.mmu_object;
229 if (mo == NULL)
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100230 return;
231
Chris Wilson768e1592016-01-21 17:32:43 +0000232 spin_lock(&mo->mn->lock);
233 del_object(mo);
234 spin_unlock(&mo->mn->lock);
Chris Wilsonad46cb52014-08-07 14:20:40 +0100235 kfree(mo);
236
237 obj->userptr.mmu_object = NULL;
238}
239
240static struct i915_mmu_notifier *
241i915_mmu_notifier_find(struct i915_mm_struct *mm)
242{
Chris Wilsone9681362014-09-26 10:31:02 +0100243 struct i915_mmu_notifier *mn = mm->mn;
244
245 mn = mm->mn;
246 if (mn)
247 return mn;
248
249 down_write(&mm->mm->mmap_sem);
Chris Wilsonf470b192016-04-05 15:00:01 +0100250 mutex_lock(&mm->i915->mm_lock);
Chris Wilsone9681362014-09-26 10:31:02 +0100251 if ((mn = mm->mn) == NULL) {
252 mn = i915_mmu_notifier_create(mm->mm);
253 if (!IS_ERR(mn))
254 mm->mn = mn;
Chris Wilsonad46cb52014-08-07 14:20:40 +0100255 }
Chris Wilsonf470b192016-04-05 15:00:01 +0100256 mutex_unlock(&mm->i915->mm_lock);
Chris Wilsone9681362014-09-26 10:31:02 +0100257 up_write(&mm->mm->mmap_sem);
258
259 return mn;
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100260}
261
262static int
263i915_gem_userptr_init__mmu_notifier(struct drm_i915_gem_object *obj,
264 unsigned flags)
265{
Chris Wilsonad46cb52014-08-07 14:20:40 +0100266 struct i915_mmu_notifier *mn;
267 struct i915_mmu_object *mo;
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100268
269 if (flags & I915_USERPTR_UNSYNCHRONIZED)
270 return capable(CAP_SYS_ADMIN) ? 0 : -EPERM;
271
Chris Wilsonad46cb52014-08-07 14:20:40 +0100272 if (WARN_ON(obj->userptr.mm == NULL))
273 return -EINVAL;
274
275 mn = i915_mmu_notifier_find(obj->userptr.mm);
276 if (IS_ERR(mn))
277 return PTR_ERR(mn);
278
279 mo = kzalloc(sizeof(*mo), GFP_KERNEL);
280 if (mo == NULL)
281 return -ENOMEM;
282
283 mo->mn = mn;
Chris Wilsonad46cb52014-08-07 14:20:40 +0100284 mo->obj = obj;
Chris Wilson768e1592016-01-21 17:32:43 +0000285 mo->it.start = obj->userptr.ptr;
286 mo->it.last = obj->userptr.ptr + obj->base.size - 1;
287 INIT_WORK(&mo->work, cancel_userptr);
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100288
Chris Wilsonad46cb52014-08-07 14:20:40 +0100289 obj->userptr.mmu_object = mo;
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100290 return 0;
Chris Wilsonad46cb52014-08-07 14:20:40 +0100291}
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100292
Chris Wilsonad46cb52014-08-07 14:20:40 +0100293static void
294i915_mmu_notifier_free(struct i915_mmu_notifier *mn,
295 struct mm_struct *mm)
296{
297 if (mn == NULL)
298 return;
299
300 mmu_notifier_unregister(&mn->mn, mm);
Chris Wilson393afc22016-04-05 14:59:59 +0100301 destroy_workqueue(mn->wq);
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100302 kfree(mn);
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100303}
304
305#else
306
307static void
308i915_gem_userptr_release__mmu_notifier(struct drm_i915_gem_object *obj)
309{
310}
311
312static int
313i915_gem_userptr_init__mmu_notifier(struct drm_i915_gem_object *obj,
314 unsigned flags)
315{
316 if ((flags & I915_USERPTR_UNSYNCHRONIZED) == 0)
317 return -ENODEV;
318
319 if (!capable(CAP_SYS_ADMIN))
320 return -EPERM;
321
322 return 0;
323}
Chris Wilsonad46cb52014-08-07 14:20:40 +0100324
325static void
326i915_mmu_notifier_free(struct i915_mmu_notifier *mn,
327 struct mm_struct *mm)
328{
329}
330
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100331#endif
332
Chris Wilsonad46cb52014-08-07 14:20:40 +0100333static 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
338 /* Protected by dev_priv->mm_lock */
339 hash_for_each_possible(dev_priv->mm_structs, mm, node, (unsigned long)real)
340 if (mm->mm == real)
341 return mm;
342
343 return NULL;
344}
345
346static int
347i915_gem_userptr_init__mm_struct(struct drm_i915_gem_object *obj)
348{
349 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
350 struct i915_mm_struct *mm;
351 int ret = 0;
352
353 /* During release of the GEM object we hold the struct_mutex. This
354 * precludes us from calling mmput() at that time as that may be
355 * the last reference and so call exit_mmap(). exit_mmap() will
356 * attempt to reap the vma, and if we were holding a GTT mmap
357 * would then call drm_gem_vm_close() and attempt to reacquire
358 * the struct mutex. So in order to avoid that recursion, we have
359 * to defer releasing the mm reference until after we drop the
360 * struct_mutex, i.e. we need to schedule a worker to do the clean
361 * up.
362 */
363 mutex_lock(&dev_priv->mm_lock);
364 mm = __i915_mm_struct_find(dev_priv, current->mm);
365 if (mm == NULL) {
366 mm = kmalloc(sizeof(*mm), GFP_KERNEL);
367 if (mm == NULL) {
368 ret = -ENOMEM;
369 goto out;
370 }
371
372 kref_init(&mm->kref);
Chris Wilsonf470b192016-04-05 15:00:01 +0100373 mm->i915 = to_i915(obj->base.dev);
Chris Wilsonad46cb52014-08-07 14:20:40 +0100374
375 mm->mm = current->mm;
376 atomic_inc(&current->mm->mm_count);
377
378 mm->mn = NULL;
379
380 /* Protected by dev_priv->mm_lock */
381 hash_add(dev_priv->mm_structs,
382 &mm->node, (unsigned long)mm->mm);
383 } else
384 kref_get(&mm->kref);
385
386 obj->userptr.mm = mm;
387out:
388 mutex_unlock(&dev_priv->mm_lock);
389 return ret;
390}
391
392static void
393__i915_mm_struct_free__worker(struct work_struct *work)
394{
395 struct i915_mm_struct *mm = container_of(work, typeof(*mm), work);
396 i915_mmu_notifier_free(mm->mn, mm->mm);
397 mmdrop(mm->mm);
398 kfree(mm);
399}
400
401static void
402__i915_mm_struct_free(struct kref *kref)
403{
404 struct i915_mm_struct *mm = container_of(kref, typeof(*mm), kref);
405
406 /* Protected by dev_priv->mm_lock */
407 hash_del(&mm->node);
Chris Wilsonf470b192016-04-05 15:00:01 +0100408 mutex_unlock(&mm->i915->mm_lock);
Chris Wilsonad46cb52014-08-07 14:20:40 +0100409
410 INIT_WORK(&mm->work, __i915_mm_struct_free__worker);
411 schedule_work(&mm->work);
412}
413
414static void
415i915_gem_userptr_release__mm_struct(struct drm_i915_gem_object *obj)
416{
417 if (obj->userptr.mm == NULL)
418 return;
419
420 kref_put_mutex(&obj->userptr.mm->kref,
421 __i915_mm_struct_free,
422 &to_i915(obj->base.dev)->mm_lock);
423 obj->userptr.mm = NULL;
424}
425
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100426struct get_pages_work {
427 struct work_struct work;
428 struct drm_i915_gem_object *obj;
429 struct task_struct *task;
430};
431
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100432#if IS_ENABLED(CONFIG_SWIOTLB)
433#define swiotlb_active() swiotlb_nr_tbl()
434#else
435#define swiotlb_active() 0
436#endif
437
438static int
439st_set_pages(struct sg_table **st, struct page **pvec, int num_pages)
440{
441 struct scatterlist *sg;
442 int ret, n;
443
444 *st = kmalloc(sizeof(**st), GFP_KERNEL);
445 if (*st == NULL)
446 return -ENOMEM;
447
448 if (swiotlb_active()) {
449 ret = sg_alloc_table(*st, num_pages, GFP_KERNEL);
450 if (ret)
451 goto err;
452
453 for_each_sg((*st)->sgl, sg, num_pages, n)
454 sg_set_page(sg, pvec[n], PAGE_SIZE, 0);
455 } else {
456 ret = sg_alloc_table_from_pages(*st, pvec, num_pages,
457 0, num_pages << PAGE_SHIFT,
458 GFP_KERNEL);
459 if (ret)
460 goto err;
461 }
462
463 return 0;
464
465err:
466 kfree(*st);
467 *st = NULL;
468 return ret;
469}
470
Imre Deake2273302015-07-09 12:59:05 +0300471static int
472__i915_gem_userptr_set_pages(struct drm_i915_gem_object *obj,
473 struct page **pvec, int num_pages)
474{
475 int ret;
476
477 ret = st_set_pages(&obj->pages, pvec, num_pages);
478 if (ret)
479 return ret;
480
481 ret = i915_gem_gtt_prepare_object(obj);
482 if (ret) {
483 sg_free_table(obj->pages);
484 kfree(obj->pages);
485 obj->pages = NULL;
486 }
487
488 return ret;
489}
490
Chris Wilson380996a2015-10-01 12:34:47 +0100491static int
Chris Wilsone4b946b2015-10-01 12:34:46 +0100492__i915_gem_userptr_set_active(struct drm_i915_gem_object *obj,
493 bool value)
494{
Chris Wilson380996a2015-10-01 12:34:47 +0100495 int ret = 0;
496
Chris Wilsone4b946b2015-10-01 12:34:46 +0100497 /* During mm_invalidate_range we need to cancel any userptr that
498 * overlaps the range being invalidated. Doing so requires the
499 * struct_mutex, and that risks recursion. In order to cause
500 * recursion, the user must alias the userptr address space with
501 * a GTT mmapping (possible with a MAP_FIXED) - then when we have
502 * to invalidate that mmaping, mm_invalidate_range is called with
503 * the userptr address *and* the struct_mutex held. To prevent that
504 * we set a flag under the i915_mmu_notifier spinlock to indicate
505 * whether this object is valid.
506 */
507#if defined(CONFIG_MMU_NOTIFIER)
508 if (obj->userptr.mmu_object == NULL)
Chris Wilson380996a2015-10-01 12:34:47 +0100509 return 0;
Chris Wilsone4b946b2015-10-01 12:34:46 +0100510
511 spin_lock(&obj->userptr.mmu_object->mn->lock);
Chris Wilson380996a2015-10-01 12:34:47 +0100512 /* In order to serialise get_pages with an outstanding
513 * cancel_userptr, we must drop the struct_mutex and try again.
514 */
Chris Wilson768e1592016-01-21 17:32:43 +0000515 if (!value)
516 del_object(obj->userptr.mmu_object);
517 else if (!work_pending(&obj->userptr.mmu_object->work))
518 add_object(obj->userptr.mmu_object);
Chris Wilson380996a2015-10-01 12:34:47 +0100519 else
520 ret = -EAGAIN;
Chris Wilsone4b946b2015-10-01 12:34:46 +0100521 spin_unlock(&obj->userptr.mmu_object->mn->lock);
522#endif
Chris Wilson380996a2015-10-01 12:34:47 +0100523
524 return ret;
Chris Wilsone4b946b2015-10-01 12:34:46 +0100525}
526
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100527static void
528__i915_gem_userptr_get_pages_worker(struct work_struct *_work)
529{
530 struct get_pages_work *work = container_of(_work, typeof(*work), work);
531 struct drm_i915_gem_object *obj = work->obj;
532 struct drm_device *dev = obj->base.dev;
Chris Wilson68d6c842015-10-01 12:34:45 +0100533 const int npages = obj->base.size >> PAGE_SHIFT;
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100534 struct page **pvec;
535 int pinned, ret;
536
537 ret = -ENOMEM;
538 pinned = 0;
539
Chris Wilsonf2a85e12016-04-08 12:11:13 +0100540 pvec = drm_malloc_gfp(npages, sizeof(struct page *), GFP_TEMPORARY);
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100541 if (pvec != NULL) {
Chris Wilsonad46cb52014-08-07 14:20:40 +0100542 struct mm_struct *mm = obj->userptr.mm->mm;
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100543
Chris Wilson40313f02016-04-05 15:00:00 +0100544 ret = -EFAULT;
545 if (atomic_inc_not_zero(&mm->mm_users)) {
546 down_read(&mm->mmap_sem);
547 while (pinned < npages) {
548 ret = get_user_pages_remote
549 (work->task, mm,
550 obj->userptr.ptr + pinned * PAGE_SIZE,
551 npages - pinned,
552 !obj->userptr.read_only, 0,
553 pvec + pinned, NULL);
554 if (ret < 0)
555 break;
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100556
Chris Wilson40313f02016-04-05 15:00:00 +0100557 pinned += ret;
558 }
559 up_read(&mm->mmap_sem);
560 mmput(mm);
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100561 }
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100562 }
563
564 mutex_lock(&dev->struct_mutex);
Chris Wilson68d6c842015-10-01 12:34:45 +0100565 if (obj->userptr.work == &work->work) {
566 if (pinned == npages) {
567 ret = __i915_gem_userptr_set_pages(obj, pvec, npages);
568 if (ret == 0) {
569 list_add_tail(&obj->global_list,
570 &to_i915(dev)->mm.unbound_list);
571 obj->get_page.sg = obj->pages->sgl;
572 obj->get_page.last = 0;
573 pinned = 0;
574 }
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100575 }
Chris Wilson68d6c842015-10-01 12:34:45 +0100576 obj->userptr.work = ERR_PTR(ret);
Chris Wilsone4b946b2015-10-01 12:34:46 +0100577 if (ret)
578 __i915_gem_userptr_set_active(obj, false);
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100579 }
580
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100581 obj->userptr.workers--;
582 drm_gem_object_unreference(&obj->base);
583 mutex_unlock(&dev->struct_mutex);
584
585 release_pages(pvec, pinned, 0);
586 drm_free_large(pvec);
587
588 put_task_struct(work->task);
589 kfree(work);
590}
591
592static int
Chris Wilsone4b946b2015-10-01 12:34:46 +0100593__i915_gem_userptr_get_pages_schedule(struct drm_i915_gem_object *obj,
594 bool *active)
595{
596 struct get_pages_work *work;
597
598 /* Spawn a worker so that we can acquire the
599 * user pages without holding our mutex. Access
600 * to the user pages requires mmap_sem, and we have
601 * a strict lock ordering of mmap_sem, struct_mutex -
602 * we already hold struct_mutex here and so cannot
603 * call gup without encountering a lock inversion.
604 *
605 * Userspace will keep on repeating the operation
606 * (thanks to EAGAIN) until either we hit the fast
607 * path or the worker completes. If the worker is
608 * cancelled or superseded, the task is still run
609 * but the results ignored. (This leads to
610 * complications that we may have a stray object
611 * refcount that we need to be wary of when
612 * checking for existing objects during creation.)
613 * If the worker encounters an error, it reports
614 * that error back to this function through
615 * obj->userptr.work = ERR_PTR.
616 */
617 if (obj->userptr.workers >= I915_GEM_USERPTR_MAX_WORKERS)
618 return -EAGAIN;
619
620 work = kmalloc(sizeof(*work), GFP_KERNEL);
621 if (work == NULL)
622 return -ENOMEM;
623
624 obj->userptr.work = &work->work;
625 obj->userptr.workers++;
626
627 work->obj = obj;
628 drm_gem_object_reference(&obj->base);
629
630 work->task = current;
631 get_task_struct(work->task);
632
633 INIT_WORK(&work->work, __i915_gem_userptr_get_pages_worker);
634 schedule_work(&work->work);
635
636 *active = true;
637 return -EAGAIN;
638}
639
640static int
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100641i915_gem_userptr_get_pages(struct drm_i915_gem_object *obj)
642{
643 const int num_pages = obj->base.size >> PAGE_SHIFT;
644 struct page **pvec;
645 int pinned, ret;
Chris Wilsone4b946b2015-10-01 12:34:46 +0100646 bool active;
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100647
648 /* If userspace should engineer that these pages are replaced in
649 * the vma between us binding this page into the GTT and completion
650 * of rendering... Their loss. If they change the mapping of their
651 * pages they need to create a new bo to point to the new vma.
652 *
653 * However, that still leaves open the possibility of the vma
654 * being copied upon fork. Which falls under the same userspace
655 * synchronisation issue as a regular bo, except that this time
656 * the process may not be expecting that a particular piece of
657 * memory is tied to the GPU.
658 *
659 * Fortunately, we can hook into the mmu_notifier in order to
660 * discard the page references prior to anything nasty happening
661 * to the vma (discard or cloning) which should prevent the more
662 * egregious cases from causing harm.
663 */
Chris Wilsone4b946b2015-10-01 12:34:46 +0100664 if (IS_ERR(obj->userptr.work)) {
665 /* active flag will have been dropped already by the worker */
666 ret = PTR_ERR(obj->userptr.work);
667 obj->userptr.work = NULL;
668 return ret;
669 }
670 if (obj->userptr.work)
671 /* active flag should still be held for the pending work */
672 return -EAGAIN;
673
674 /* Let the mmu-notifier know that we have begun and need cancellation */
Chris Wilson380996a2015-10-01 12:34:47 +0100675 ret = __i915_gem_userptr_set_active(obj, true);
676 if (ret)
677 return ret;
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100678
679 pvec = NULL;
680 pinned = 0;
Chris Wilsonad46cb52014-08-07 14:20:40 +0100681 if (obj->userptr.mm->mm == current->mm) {
Chris Wilsonf2a85e12016-04-08 12:11:13 +0100682 pvec = drm_malloc_gfp(num_pages, sizeof(struct page *),
683 GFP_TEMPORARY);
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100684 if (pvec == NULL) {
Chris Wilsonf2a85e12016-04-08 12:11:13 +0100685 __i915_gem_userptr_set_active(obj, false);
686 return -ENOMEM;
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100687 }
688
689 pinned = __get_user_pages_fast(obj->userptr.ptr, num_pages,
690 !obj->userptr.read_only, pvec);
691 }
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100692
Chris Wilsone4b946b2015-10-01 12:34:46 +0100693 active = false;
694 if (pinned < 0)
695 ret = pinned, pinned = 0;
696 else if (pinned < num_pages)
697 ret = __i915_gem_userptr_get_pages_schedule(obj, &active);
698 else
Imre Deake2273302015-07-09 12:59:05 +0300699 ret = __i915_gem_userptr_set_pages(obj, pvec, num_pages);
Chris Wilsone4b946b2015-10-01 12:34:46 +0100700 if (ret) {
701 __i915_gem_userptr_set_active(obj, active);
702 release_pages(pvec, pinned, 0);
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100703 }
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100704 drm_free_large(pvec);
705 return ret;
706}
707
708static void
709i915_gem_userptr_put_pages(struct drm_i915_gem_object *obj)
710{
Tvrtko Ursulinc479f432014-09-26 15:05:22 +0100711 struct sg_page_iter sg_iter;
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100712
713 BUG_ON(obj->userptr.work != NULL);
Chris Wilsone4b946b2015-10-01 12:34:46 +0100714 __i915_gem_userptr_set_active(obj, false);
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100715
716 if (obj->madv != I915_MADV_WILLNEED)
717 obj->dirty = 0;
718
Imre Deake2273302015-07-09 12:59:05 +0300719 i915_gem_gtt_finish_object(obj);
720
Tvrtko Ursulinc479f432014-09-26 15:05:22 +0100721 for_each_sg_page(obj->pages->sgl, &sg_iter, obj->pages->nents, 0) {
722 struct page *page = sg_page_iter_page(&sg_iter);
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100723
724 if (obj->dirty)
725 set_page_dirty(page);
726
727 mark_page_accessed(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300728 put_page(page);
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100729 }
730 obj->dirty = 0;
731
732 sg_free_table(obj->pages);
733 kfree(obj->pages);
734}
735
736static void
737i915_gem_userptr_release(struct drm_i915_gem_object *obj)
738{
739 i915_gem_userptr_release__mmu_notifier(obj);
Chris Wilsonad46cb52014-08-07 14:20:40 +0100740 i915_gem_userptr_release__mm_struct(obj);
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100741}
742
743static int
744i915_gem_userptr_dmabuf_export(struct drm_i915_gem_object *obj)
745{
Chris Wilsonad46cb52014-08-07 14:20:40 +0100746 if (obj->userptr.mmu_object)
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100747 return 0;
748
749 return i915_gem_userptr_init__mmu_notifier(obj, 0);
750}
751
752static const struct drm_i915_gem_object_ops i915_gem_userptr_ops = {
Chris Wilsonde472662016-01-22 18:32:31 +0000753 .flags = I915_GEM_OBJECT_HAS_STRUCT_PAGE,
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100754 .get_pages = i915_gem_userptr_get_pages,
755 .put_pages = i915_gem_userptr_put_pages,
Chris Wilsonde472662016-01-22 18:32:31 +0000756 .dmabuf_export = i915_gem_userptr_dmabuf_export,
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100757 .release = i915_gem_userptr_release,
758};
759
760/**
761 * Creates a new mm object that wraps some normal memory from the process
762 * context - user memory.
763 *
764 * We impose several restrictions upon the memory being mapped
765 * into the GPU.
766 * 1. It must be page aligned (both start/end addresses, i.e ptr and size).
Chris Wilsonec8b0dd2014-07-21 13:21:23 +0100767 * 2. It must be normal system memory, not a pointer into another map of IO
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100768 * space (e.g. it must not be a GTT mmapping of another object).
Chris Wilsonec8b0dd2014-07-21 13:21:23 +0100769 * 3. We only allow a bo as large as we could in theory map into the GTT,
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100770 * that is we limit the size to the total size of the GTT.
Chris Wilsonec8b0dd2014-07-21 13:21:23 +0100771 * 4. The bo is marked as being snoopable. The backing pages are left
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100772 * accessible directly by the CPU, but reads and writes by the GPU may
773 * incur the cost of a snoop (unless you have an LLC architecture).
774 *
775 * Synchronisation between multiple users and the GPU is left to userspace
776 * through the normal set-domain-ioctl. The kernel will enforce that the
777 * GPU relinquishes the VMA before it is returned back to the system
778 * i.e. upon free(), munmap() or process termination. However, the userspace
779 * malloc() library may not immediately relinquish the VMA after free() and
780 * instead reuse it whilst the GPU is still reading and writing to the VMA.
781 * Caveat emptor.
782 *
783 * Also note, that the object created here is not currently a "first class"
784 * object, in that several ioctls are banned. These are the CPU access
785 * ioctls: mmap(), pwrite and pread. In practice, you are expected to use
Chris Wilsoncc917ab2015-10-13 14:22:26 +0100786 * direct access via your pointer rather than use those ioctls. Another
787 * restriction is that we do not allow userptr surfaces to be pinned to the
788 * hardware and so we reject any attempt to create a framebuffer out of a
789 * userptr.
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100790 *
791 * If you think this is a good interface to use to pass GPU memory between
792 * drivers, please use dma-buf instead. In fact, wherever possible use
793 * dma-buf instead.
794 */
795int
796i915_gem_userptr_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
797{
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100798 struct drm_i915_gem_userptr *args = data;
799 struct drm_i915_gem_object *obj;
800 int ret;
801 u32 handle;
802
Tvrtko Ursulinca377802016-03-02 12:10:31 +0000803 if (!HAS_LLC(dev) && !HAS_SNOOP(dev)) {
804 /* We cannot support coherent userptr objects on hw without
805 * LLC and broken snooping.
806 */
807 return -ENODEV;
808 }
809
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100810 if (args->flags & ~(I915_USERPTR_READ_ONLY |
811 I915_USERPTR_UNSYNCHRONIZED))
812 return -EINVAL;
813
814 if (offset_in_page(args->user_ptr | args->user_size))
815 return -EINVAL;
816
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100817 if (!access_ok(args->flags & I915_USERPTR_READ_ONLY ? VERIFY_READ : VERIFY_WRITE,
818 (char __user *)(unsigned long)args->user_ptr, args->user_size))
819 return -EFAULT;
820
821 if (args->flags & I915_USERPTR_READ_ONLY) {
822 /* On almost all of the current hw, we cannot tell the GPU that a
823 * page is readonly, so this is just a placeholder in the uAPI.
824 */
825 return -ENODEV;
826 }
827
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100828 obj = i915_gem_object_alloc(dev);
829 if (obj == NULL)
830 return -ENOMEM;
831
832 drm_gem_private_object_init(dev, &obj->base, args->user_size);
833 i915_gem_object_init(obj, &i915_gem_userptr_ops);
834 obj->cache_level = I915_CACHE_LLC;
835 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
836 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
837
838 obj->userptr.ptr = args->user_ptr;
839 obj->userptr.read_only = !!(args->flags & I915_USERPTR_READ_ONLY);
840
841 /* And keep a pointer to the current->mm for resolving the user pages
842 * at binding. This means that we need to hook into the mmu_notifier
843 * in order to detect if the mmu is destroyed.
844 */
Chris Wilsonad46cb52014-08-07 14:20:40 +0100845 ret = i915_gem_userptr_init__mm_struct(obj);
846 if (ret == 0)
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100847 ret = i915_gem_userptr_init__mmu_notifier(obj, args->flags);
848 if (ret == 0)
849 ret = drm_gem_handle_create(file, &obj->base, &handle);
850
851 /* drop reference from allocate - handle holds it now */
852 drm_gem_object_unreference_unlocked(&obj->base);
853 if (ret)
854 return ret;
855
856 args->handle = handle;
857 return 0;
858}
859
860int
861i915_gem_init_userptr(struct drm_device *dev)
862{
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100863 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilsonad46cb52014-08-07 14:20:40 +0100864 mutex_init(&dev_priv->mm_lock);
865 hash_init(dev_priv->mm_structs);
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100866 return 0;
867}