blob: 59e45b3a69379a0e892fbd85d7a17ba3f85913eb [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;
37 struct drm_device *dev;
38 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 Wilsonec8b0dd2014-07-21 13:21:23 +010052 struct list_head linear;
Chris Wilsonec8b0dd2014-07-21 13:21:23 +010053 bool has_linear;
Chris Wilson5cc9ed42014-05-16 14:22:37 +010054};
55
56struct i915_mmu_object {
Chris Wilsonad46cb52014-08-07 14:20:40 +010057 struct i915_mmu_notifier *mn;
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 Wilson5cc9ed42014-05-16 14:22:37 +010060 struct drm_i915_gem_object *obj;
Chris Wilson380996a2015-10-01 12:34:47 +010061 struct work_struct work;
Chris Wilsone4b946b2015-10-01 12:34:46 +010062 bool active;
Chris Wilsonec8b0dd2014-07-21 13:21:23 +010063 bool is_linear;
Chris Wilson5cc9ed42014-05-16 14:22:37 +010064};
65
Chris Wilson380996a2015-10-01 12:34:47 +010066static void __cancel_userptr__worker(struct work_struct *work)
Chris Wilsonec8b0dd2014-07-21 13:21:23 +010067{
Chris Wilson380996a2015-10-01 12:34:47 +010068 struct i915_mmu_object *mo = container_of(work, typeof(*mo), work);
69 struct drm_i915_gem_object *obj = mo->obj;
Chris Wilsonec8b0dd2014-07-21 13:21:23 +010070 struct drm_device *dev = obj->base.dev;
Chris Wilsonec8b0dd2014-07-21 13:21:23 +010071
72 mutex_lock(&dev->struct_mutex);
73 /* Cancel any active worker and force us to re-evaluate gup */
74 obj->userptr.work = NULL;
75
76 if (obj->pages != NULL) {
77 struct drm_i915_private *dev_priv = to_i915(dev);
78 struct i915_vma *vma, *tmp;
79 bool was_interruptible;
80
81 was_interruptible = dev_priv->mm.interruptible;
82 dev_priv->mm.interruptible = false;
83
84 list_for_each_entry_safe(vma, tmp, &obj->vma_list, vma_link) {
85 int ret = i915_vma_unbind(vma);
86 WARN_ON(ret && ret != -EIO);
87 }
88 WARN_ON(i915_gem_object_put_pages(obj));
89
90 dev_priv->mm.interruptible = was_interruptible;
91 }
92
Chris Wilsonec8b0dd2014-07-21 13:21:23 +010093 drm_gem_object_unreference(&obj->base);
94 mutex_unlock(&dev->struct_mutex);
Chris Wilsonec8b0dd2014-07-21 13:21:23 +010095}
96
Chris Wilson380996a2015-10-01 12:34:47 +010097static unsigned long cancel_userptr(struct i915_mmu_object *mo)
Chris Wilsonec8b0dd2014-07-21 13:21:23 +010098{
Chris Wilson380996a2015-10-01 12:34:47 +010099 unsigned long end = mo->obj->userptr.ptr + mo->obj->base.size;
Chris Wilsonec8b0dd2014-07-21 13:21:23 +0100100
Chris Wilson380996a2015-10-01 12:34:47 +0100101 /* The mmu_object is released late when destroying the
102 * GEM object so it is entirely possible to gain a
103 * reference on an object in the process of being freed
104 * since our serialisation is via the spinlock and not
105 * the struct_mutex - and consequently use it after it
106 * is freed and then double free it.
107 */
108 if (mo->active && kref_get_unless_zero(&mo->obj->base.refcount)) {
109 schedule_work(&mo->work);
110 /* only schedule one work packet to avoid the refleak */
111 mo->active = false;
Chris Wilsonec8b0dd2014-07-21 13:21:23 +0100112 }
113
Chris Wilson380996a2015-10-01 12:34:47 +0100114 return end;
Chris Wilsonec8b0dd2014-07-21 13:21:23 +0100115}
116
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100117static void i915_gem_userptr_mn_invalidate_range_start(struct mmu_notifier *_mn,
118 struct mm_struct *mm,
119 unsigned long start,
120 unsigned long end)
121{
Chris Wilson380996a2015-10-01 12:34:47 +0100122 struct i915_mmu_notifier *mn =
123 container_of(_mn, struct i915_mmu_notifier, mn);
124 struct i915_mmu_object *mo;
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100125
Chris Wilson380996a2015-10-01 12:34:47 +0100126 /* interval ranges are inclusive, but invalidate range is exclusive */
127 end--;
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100128
Chris Wilson380996a2015-10-01 12:34:47 +0100129 spin_lock(&mn->lock);
130 if (mn->has_linear) {
131 list_for_each_entry(mo, &mn->linear, link) {
132 if (mo->it.last < start || mo->it.start > end)
Michał Winiarski460822b2015-02-03 15:48:17 +0100133 continue;
Michał Winiarski460822b2015-02-03 15:48:17 +0100134
Chris Wilson380996a2015-10-01 12:34:47 +0100135 cancel_userptr(mo);
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100136 }
Chris Wilson380996a2015-10-01 12:34:47 +0100137 } else {
138 struct interval_tree_node *it;
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100139
Chris Wilson380996a2015-10-01 12:34:47 +0100140 it = interval_tree_iter_first(&mn->objects, start, end);
141 while (it) {
142 mo = container_of(it, struct i915_mmu_object, it);
143 start = cancel_userptr(mo);
144 it = interval_tree_iter_next(it, start, end);
145 }
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100146 }
Chris Wilson380996a2015-10-01 12:34:47 +0100147 spin_unlock(&mn->lock);
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100148}
149
150static const struct mmu_notifier_ops i915_gem_userptr_notifier = {
151 .invalidate_range_start = i915_gem_userptr_mn_invalidate_range_start,
152};
153
154static struct i915_mmu_notifier *
Chris Wilsonad46cb52014-08-07 14:20:40 +0100155i915_mmu_notifier_create(struct mm_struct *mm)
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100156{
Chris Wilsonad46cb52014-08-07 14:20:40 +0100157 struct i915_mmu_notifier *mn;
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100158 int ret;
159
Chris Wilsonad46cb52014-08-07 14:20:40 +0100160 mn = kmalloc(sizeof(*mn), GFP_KERNEL);
161 if (mn == NULL)
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100162 return ERR_PTR(-ENOMEM);
163
Chris Wilsonad46cb52014-08-07 14:20:40 +0100164 spin_lock_init(&mn->lock);
165 mn->mn.ops = &i915_gem_userptr_notifier;
166 mn->objects = RB_ROOT;
Chris Wilsonad46cb52014-08-07 14:20:40 +0100167 INIT_LIST_HEAD(&mn->linear);
168 mn->has_linear = false;
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100169
Chris Wilsonad46cb52014-08-07 14:20:40 +0100170 /* Protected by mmap_sem (write-lock) */
171 ret = __mmu_notifier_register(&mn->mn, mm);
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100172 if (ret) {
Chris Wilsonad46cb52014-08-07 14:20:40 +0100173 kfree(mn);
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100174 return ERR_PTR(ret);
175 }
176
Chris Wilsonad46cb52014-08-07 14:20:40 +0100177 return mn;
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100178}
179
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100180static int
Chris Wilsonad46cb52014-08-07 14:20:40 +0100181i915_mmu_notifier_add(struct drm_device *dev,
182 struct i915_mmu_notifier *mn,
183 struct i915_mmu_object *mo)
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100184{
185 struct interval_tree_node *it;
Chris Wilson281400f2015-05-15 11:42:21 +0100186 int ret = 0;
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100187
Chris Wilson281400f2015-05-15 11:42:21 +0100188 /* By this point we have already done a lot of expensive setup that
189 * we do not want to repeat just because the caller (e.g. X) has a
190 * signal pending (and partly because of that expensive setup, X
191 * using an interrupt timer is likely to get stuck in an EINTR loop).
192 */
193 mutex_lock(&dev->struct_mutex);
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100194
195 /* Make sure we drop the final active reference (and thereby
196 * remove the objects from the interval tree) before we do
197 * the check for overlapping objects.
198 */
Chris Wilsonad46cb52014-08-07 14:20:40 +0100199 i915_gem_retire_requests(dev);
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100200
Chris Wilsonad46cb52014-08-07 14:20:40 +0100201 spin_lock(&mn->lock);
202 it = interval_tree_iter_first(&mn->objects,
203 mo->it.start, mo->it.last);
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100204 if (it) {
205 struct drm_i915_gem_object *obj;
206
207 /* We only need to check the first object in the range as it
208 * either has cancelled gup work queued and we need to
209 * return back to the user to give time for the gup-workers
210 * to flush their object references upon which the object will
211 * be removed from the interval-tree, or the the range is
212 * still in use by another client and the overlap is invalid.
Chris Wilsonec8b0dd2014-07-21 13:21:23 +0100213 *
214 * If we do have an overlap, we cannot use the interval tree
215 * for fast range invalidation.
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100216 */
217
218 obj = container_of(it, struct i915_mmu_object, it)->obj;
Chris Wilsonec8b0dd2014-07-21 13:21:23 +0100219 if (!obj->userptr.workers)
Chris Wilsonad46cb52014-08-07 14:20:40 +0100220 mn->has_linear = mo->is_linear = true;
Chris Wilsonec8b0dd2014-07-21 13:21:23 +0100221 else
222 ret = -EAGAIN;
223 } else
Chris Wilsonad46cb52014-08-07 14:20:40 +0100224 interval_tree_insert(&mo->it, &mn->objects);
Chris Wilsonec8b0dd2014-07-21 13:21:23 +0100225
Chris Wilson380996a2015-10-01 12:34:47 +0100226 if (ret == 0)
Chris Wilsonad46cb52014-08-07 14:20:40 +0100227 list_add(&mo->link, &mn->linear);
Chris Wilson380996a2015-10-01 12:34:47 +0100228
Chris Wilsonad46cb52014-08-07 14:20:40 +0100229 spin_unlock(&mn->lock);
230 mutex_unlock(&dev->struct_mutex);
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100231
232 return ret;
233}
234
Chris Wilsonad46cb52014-08-07 14:20:40 +0100235static bool i915_mmu_notifier_has_linear(struct i915_mmu_notifier *mn)
236{
237 struct i915_mmu_object *mo;
238
239 list_for_each_entry(mo, &mn->linear, link)
240 if (mo->is_linear)
241 return true;
242
243 return false;
244}
245
246static void
247i915_mmu_notifier_del(struct i915_mmu_notifier *mn,
248 struct i915_mmu_object *mo)
249{
250 spin_lock(&mn->lock);
251 list_del(&mo->link);
252 if (mo->is_linear)
253 mn->has_linear = i915_mmu_notifier_has_linear(mn);
254 else
255 interval_tree_remove(&mo->it, &mn->objects);
Chris Wilsonad46cb52014-08-07 14:20:40 +0100256 spin_unlock(&mn->lock);
257}
258
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100259static void
260i915_gem_userptr_release__mmu_notifier(struct drm_i915_gem_object *obj)
261{
Chris Wilsonad46cb52014-08-07 14:20:40 +0100262 struct i915_mmu_object *mo;
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100263
Chris Wilsonad46cb52014-08-07 14:20:40 +0100264 mo = obj->userptr.mmu_object;
265 if (mo == NULL)
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100266 return;
267
Chris Wilsonad46cb52014-08-07 14:20:40 +0100268 i915_mmu_notifier_del(mo->mn, mo);
269 kfree(mo);
270
271 obj->userptr.mmu_object = NULL;
272}
273
274static struct i915_mmu_notifier *
275i915_mmu_notifier_find(struct i915_mm_struct *mm)
276{
Chris Wilsone9681362014-09-26 10:31:02 +0100277 struct i915_mmu_notifier *mn = mm->mn;
278
279 mn = mm->mn;
280 if (mn)
281 return mn;
282
283 down_write(&mm->mm->mmap_sem);
284 mutex_lock(&to_i915(mm->dev)->mm_lock);
285 if ((mn = mm->mn) == NULL) {
286 mn = i915_mmu_notifier_create(mm->mm);
287 if (!IS_ERR(mn))
288 mm->mn = mn;
Chris Wilsonad46cb52014-08-07 14:20:40 +0100289 }
Chris Wilsone9681362014-09-26 10:31:02 +0100290 mutex_unlock(&to_i915(mm->dev)->mm_lock);
291 up_write(&mm->mm->mmap_sem);
292
293 return mn;
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100294}
295
296static int
297i915_gem_userptr_init__mmu_notifier(struct drm_i915_gem_object *obj,
298 unsigned flags)
299{
Chris Wilsonad46cb52014-08-07 14:20:40 +0100300 struct i915_mmu_notifier *mn;
301 struct i915_mmu_object *mo;
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100302 int ret;
303
304 if (flags & I915_USERPTR_UNSYNCHRONIZED)
305 return capable(CAP_SYS_ADMIN) ? 0 : -EPERM;
306
Chris Wilsonad46cb52014-08-07 14:20:40 +0100307 if (WARN_ON(obj->userptr.mm == NULL))
308 return -EINVAL;
309
310 mn = i915_mmu_notifier_find(obj->userptr.mm);
311 if (IS_ERR(mn))
312 return PTR_ERR(mn);
313
314 mo = kzalloc(sizeof(*mo), GFP_KERNEL);
315 if (mo == NULL)
316 return -ENOMEM;
317
318 mo->mn = mn;
319 mo->it.start = obj->userptr.ptr;
320 mo->it.last = mo->it.start + obj->base.size - 1;
321 mo->obj = obj;
Chris Wilson380996a2015-10-01 12:34:47 +0100322 INIT_WORK(&mo->work, __cancel_userptr__worker);
Chris Wilsonad46cb52014-08-07 14:20:40 +0100323
324 ret = i915_mmu_notifier_add(obj->base.dev, mn, mo);
325 if (ret) {
326 kfree(mo);
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100327 return ret;
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100328 }
329
Chris Wilsonad46cb52014-08-07 14:20:40 +0100330 obj->userptr.mmu_object = mo;
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100331 return 0;
Chris Wilsonad46cb52014-08-07 14:20:40 +0100332}
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100333
Chris Wilsonad46cb52014-08-07 14:20:40 +0100334static void
335i915_mmu_notifier_free(struct i915_mmu_notifier *mn,
336 struct mm_struct *mm)
337{
338 if (mn == NULL)
339 return;
340
341 mmu_notifier_unregister(&mn->mn, mm);
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100342 kfree(mn);
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100343}
344
345#else
346
347static void
348i915_gem_userptr_release__mmu_notifier(struct drm_i915_gem_object *obj)
349{
350}
351
352static int
353i915_gem_userptr_init__mmu_notifier(struct drm_i915_gem_object *obj,
354 unsigned flags)
355{
356 if ((flags & I915_USERPTR_UNSYNCHRONIZED) == 0)
357 return -ENODEV;
358
359 if (!capable(CAP_SYS_ADMIN))
360 return -EPERM;
361
362 return 0;
363}
Chris Wilsonad46cb52014-08-07 14:20:40 +0100364
365static void
366i915_mmu_notifier_free(struct i915_mmu_notifier *mn,
367 struct mm_struct *mm)
368{
369}
370
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100371#endif
372
Chris Wilsonad46cb52014-08-07 14:20:40 +0100373static struct i915_mm_struct *
374__i915_mm_struct_find(struct drm_i915_private *dev_priv, struct mm_struct *real)
375{
376 struct i915_mm_struct *mm;
377
378 /* Protected by dev_priv->mm_lock */
379 hash_for_each_possible(dev_priv->mm_structs, mm, node, (unsigned long)real)
380 if (mm->mm == real)
381 return mm;
382
383 return NULL;
384}
385
386static int
387i915_gem_userptr_init__mm_struct(struct drm_i915_gem_object *obj)
388{
389 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
390 struct i915_mm_struct *mm;
391 int ret = 0;
392
393 /* During release of the GEM object we hold the struct_mutex. This
394 * precludes us from calling mmput() at that time as that may be
395 * the last reference and so call exit_mmap(). exit_mmap() will
396 * attempt to reap the vma, and if we were holding a GTT mmap
397 * would then call drm_gem_vm_close() and attempt to reacquire
398 * the struct mutex. So in order to avoid that recursion, we have
399 * to defer releasing the mm reference until after we drop the
400 * struct_mutex, i.e. we need to schedule a worker to do the clean
401 * up.
402 */
403 mutex_lock(&dev_priv->mm_lock);
404 mm = __i915_mm_struct_find(dev_priv, current->mm);
405 if (mm == NULL) {
406 mm = kmalloc(sizeof(*mm), GFP_KERNEL);
407 if (mm == NULL) {
408 ret = -ENOMEM;
409 goto out;
410 }
411
412 kref_init(&mm->kref);
413 mm->dev = obj->base.dev;
414
415 mm->mm = current->mm;
416 atomic_inc(&current->mm->mm_count);
417
418 mm->mn = NULL;
419
420 /* Protected by dev_priv->mm_lock */
421 hash_add(dev_priv->mm_structs,
422 &mm->node, (unsigned long)mm->mm);
423 } else
424 kref_get(&mm->kref);
425
426 obj->userptr.mm = mm;
427out:
428 mutex_unlock(&dev_priv->mm_lock);
429 return ret;
430}
431
432static void
433__i915_mm_struct_free__worker(struct work_struct *work)
434{
435 struct i915_mm_struct *mm = container_of(work, typeof(*mm), work);
436 i915_mmu_notifier_free(mm->mn, mm->mm);
437 mmdrop(mm->mm);
438 kfree(mm);
439}
440
441static void
442__i915_mm_struct_free(struct kref *kref)
443{
444 struct i915_mm_struct *mm = container_of(kref, typeof(*mm), kref);
445
446 /* Protected by dev_priv->mm_lock */
447 hash_del(&mm->node);
448 mutex_unlock(&to_i915(mm->dev)->mm_lock);
449
450 INIT_WORK(&mm->work, __i915_mm_struct_free__worker);
451 schedule_work(&mm->work);
452}
453
454static void
455i915_gem_userptr_release__mm_struct(struct drm_i915_gem_object *obj)
456{
457 if (obj->userptr.mm == NULL)
458 return;
459
460 kref_put_mutex(&obj->userptr.mm->kref,
461 __i915_mm_struct_free,
462 &to_i915(obj->base.dev)->mm_lock);
463 obj->userptr.mm = NULL;
464}
465
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100466struct get_pages_work {
467 struct work_struct work;
468 struct drm_i915_gem_object *obj;
469 struct task_struct *task;
470};
471
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100472#if IS_ENABLED(CONFIG_SWIOTLB)
473#define swiotlb_active() swiotlb_nr_tbl()
474#else
475#define swiotlb_active() 0
476#endif
477
478static int
479st_set_pages(struct sg_table **st, struct page **pvec, int num_pages)
480{
481 struct scatterlist *sg;
482 int ret, n;
483
484 *st = kmalloc(sizeof(**st), GFP_KERNEL);
485 if (*st == NULL)
486 return -ENOMEM;
487
488 if (swiotlb_active()) {
489 ret = sg_alloc_table(*st, num_pages, GFP_KERNEL);
490 if (ret)
491 goto err;
492
493 for_each_sg((*st)->sgl, sg, num_pages, n)
494 sg_set_page(sg, pvec[n], PAGE_SIZE, 0);
495 } else {
496 ret = sg_alloc_table_from_pages(*st, pvec, num_pages,
497 0, num_pages << PAGE_SHIFT,
498 GFP_KERNEL);
499 if (ret)
500 goto err;
501 }
502
503 return 0;
504
505err:
506 kfree(*st);
507 *st = NULL;
508 return ret;
509}
510
Imre Deake2273302015-07-09 12:59:05 +0300511static int
512__i915_gem_userptr_set_pages(struct drm_i915_gem_object *obj,
513 struct page **pvec, int num_pages)
514{
515 int ret;
516
517 ret = st_set_pages(&obj->pages, pvec, num_pages);
518 if (ret)
519 return ret;
520
521 ret = i915_gem_gtt_prepare_object(obj);
522 if (ret) {
523 sg_free_table(obj->pages);
524 kfree(obj->pages);
525 obj->pages = NULL;
526 }
527
528 return ret;
529}
530
Chris Wilson380996a2015-10-01 12:34:47 +0100531static int
Chris Wilsone4b946b2015-10-01 12:34:46 +0100532__i915_gem_userptr_set_active(struct drm_i915_gem_object *obj,
533 bool value)
534{
Chris Wilson380996a2015-10-01 12:34:47 +0100535 int ret = 0;
536
Chris Wilsone4b946b2015-10-01 12:34:46 +0100537 /* During mm_invalidate_range we need to cancel any userptr that
538 * overlaps the range being invalidated. Doing so requires the
539 * struct_mutex, and that risks recursion. In order to cause
540 * recursion, the user must alias the userptr address space with
541 * a GTT mmapping (possible with a MAP_FIXED) - then when we have
542 * to invalidate that mmaping, mm_invalidate_range is called with
543 * the userptr address *and* the struct_mutex held. To prevent that
544 * we set a flag under the i915_mmu_notifier spinlock to indicate
545 * whether this object is valid.
546 */
547#if defined(CONFIG_MMU_NOTIFIER)
548 if (obj->userptr.mmu_object == NULL)
Chris Wilson380996a2015-10-01 12:34:47 +0100549 return 0;
Chris Wilsone4b946b2015-10-01 12:34:46 +0100550
551 spin_lock(&obj->userptr.mmu_object->mn->lock);
Chris Wilson380996a2015-10-01 12:34:47 +0100552 /* In order to serialise get_pages with an outstanding
553 * cancel_userptr, we must drop the struct_mutex and try again.
554 */
555 if (!value || !work_pending(&obj->userptr.mmu_object->work))
556 obj->userptr.mmu_object->active = value;
557 else
558 ret = -EAGAIN;
Chris Wilsone4b946b2015-10-01 12:34:46 +0100559 spin_unlock(&obj->userptr.mmu_object->mn->lock);
560#endif
Chris Wilson380996a2015-10-01 12:34:47 +0100561
562 return ret;
Chris Wilsone4b946b2015-10-01 12:34:46 +0100563}
564
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100565static void
566__i915_gem_userptr_get_pages_worker(struct work_struct *_work)
567{
568 struct get_pages_work *work = container_of(_work, typeof(*work), work);
569 struct drm_i915_gem_object *obj = work->obj;
570 struct drm_device *dev = obj->base.dev;
Chris Wilson68d6c842015-10-01 12:34:45 +0100571 const int npages = obj->base.size >> PAGE_SHIFT;
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100572 struct page **pvec;
573 int pinned, ret;
574
575 ret = -ENOMEM;
576 pinned = 0;
577
Chris Wilson68d6c842015-10-01 12:34:45 +0100578 pvec = kmalloc(npages*sizeof(struct page *),
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100579 GFP_TEMPORARY | __GFP_NOWARN | __GFP_NORETRY);
580 if (pvec == NULL)
Chris Wilson68d6c842015-10-01 12:34:45 +0100581 pvec = drm_malloc_ab(npages, sizeof(struct page *));
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100582 if (pvec != NULL) {
Chris Wilsonad46cb52014-08-07 14:20:40 +0100583 struct mm_struct *mm = obj->userptr.mm->mm;
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100584
585 down_read(&mm->mmap_sem);
Chris Wilson68d6c842015-10-01 12:34:45 +0100586 while (pinned < npages) {
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100587 ret = get_user_pages(work->task, mm,
588 obj->userptr.ptr + pinned * PAGE_SIZE,
Chris Wilson68d6c842015-10-01 12:34:45 +0100589 npages - pinned,
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100590 !obj->userptr.read_only, 0,
591 pvec + pinned, NULL);
592 if (ret < 0)
593 break;
594
595 pinned += ret;
596 }
597 up_read(&mm->mmap_sem);
598 }
599
600 mutex_lock(&dev->struct_mutex);
Chris Wilson68d6c842015-10-01 12:34:45 +0100601 if (obj->userptr.work == &work->work) {
602 if (pinned == npages) {
603 ret = __i915_gem_userptr_set_pages(obj, pvec, npages);
604 if (ret == 0) {
605 list_add_tail(&obj->global_list,
606 &to_i915(dev)->mm.unbound_list);
607 obj->get_page.sg = obj->pages->sgl;
608 obj->get_page.last = 0;
609 pinned = 0;
610 }
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100611 }
Chris Wilson68d6c842015-10-01 12:34:45 +0100612 obj->userptr.work = ERR_PTR(ret);
Chris Wilsone4b946b2015-10-01 12:34:46 +0100613 if (ret)
614 __i915_gem_userptr_set_active(obj, false);
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100615 }
616
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100617 obj->userptr.workers--;
618 drm_gem_object_unreference(&obj->base);
619 mutex_unlock(&dev->struct_mutex);
620
621 release_pages(pvec, pinned, 0);
622 drm_free_large(pvec);
623
624 put_task_struct(work->task);
625 kfree(work);
626}
627
628static int
Chris Wilsone4b946b2015-10-01 12:34:46 +0100629__i915_gem_userptr_get_pages_schedule(struct drm_i915_gem_object *obj,
630 bool *active)
631{
632 struct get_pages_work *work;
633
634 /* Spawn a worker so that we can acquire the
635 * user pages without holding our mutex. Access
636 * to the user pages requires mmap_sem, and we have
637 * a strict lock ordering of mmap_sem, struct_mutex -
638 * we already hold struct_mutex here and so cannot
639 * call gup without encountering a lock inversion.
640 *
641 * Userspace will keep on repeating the operation
642 * (thanks to EAGAIN) until either we hit the fast
643 * path or the worker completes. If the worker is
644 * cancelled or superseded, the task is still run
645 * but the results ignored. (This leads to
646 * complications that we may have a stray object
647 * refcount that we need to be wary of when
648 * checking for existing objects during creation.)
649 * If the worker encounters an error, it reports
650 * that error back to this function through
651 * obj->userptr.work = ERR_PTR.
652 */
653 if (obj->userptr.workers >= I915_GEM_USERPTR_MAX_WORKERS)
654 return -EAGAIN;
655
656 work = kmalloc(sizeof(*work), GFP_KERNEL);
657 if (work == NULL)
658 return -ENOMEM;
659
660 obj->userptr.work = &work->work;
661 obj->userptr.workers++;
662
663 work->obj = obj;
664 drm_gem_object_reference(&obj->base);
665
666 work->task = current;
667 get_task_struct(work->task);
668
669 INIT_WORK(&work->work, __i915_gem_userptr_get_pages_worker);
670 schedule_work(&work->work);
671
672 *active = true;
673 return -EAGAIN;
674}
675
676static int
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100677i915_gem_userptr_get_pages(struct drm_i915_gem_object *obj)
678{
679 const int num_pages = obj->base.size >> PAGE_SHIFT;
680 struct page **pvec;
681 int pinned, ret;
Chris Wilsone4b946b2015-10-01 12:34:46 +0100682 bool active;
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100683
684 /* If userspace should engineer that these pages are replaced in
685 * the vma between us binding this page into the GTT and completion
686 * of rendering... Their loss. If they change the mapping of their
687 * pages they need to create a new bo to point to the new vma.
688 *
689 * However, that still leaves open the possibility of the vma
690 * being copied upon fork. Which falls under the same userspace
691 * synchronisation issue as a regular bo, except that this time
692 * the process may not be expecting that a particular piece of
693 * memory is tied to the GPU.
694 *
695 * Fortunately, we can hook into the mmu_notifier in order to
696 * discard the page references prior to anything nasty happening
697 * to the vma (discard or cloning) which should prevent the more
698 * egregious cases from causing harm.
699 */
Chris Wilsone4b946b2015-10-01 12:34:46 +0100700 if (IS_ERR(obj->userptr.work)) {
701 /* active flag will have been dropped already by the worker */
702 ret = PTR_ERR(obj->userptr.work);
703 obj->userptr.work = NULL;
704 return ret;
705 }
706 if (obj->userptr.work)
707 /* active flag should still be held for the pending work */
708 return -EAGAIN;
709
710 /* Let the mmu-notifier know that we have begun and need cancellation */
Chris Wilson380996a2015-10-01 12:34:47 +0100711 ret = __i915_gem_userptr_set_active(obj, true);
712 if (ret)
713 return ret;
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100714
715 pvec = NULL;
716 pinned = 0;
Chris Wilsonad46cb52014-08-07 14:20:40 +0100717 if (obj->userptr.mm->mm == current->mm) {
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100718 pvec = kmalloc(num_pages*sizeof(struct page *),
719 GFP_TEMPORARY | __GFP_NOWARN | __GFP_NORETRY);
720 if (pvec == NULL) {
721 pvec = drm_malloc_ab(num_pages, sizeof(struct page *));
Chris Wilsone4b946b2015-10-01 12:34:46 +0100722 if (pvec == NULL) {
723 __i915_gem_userptr_set_active(obj, false);
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100724 return -ENOMEM;
Chris Wilsone4b946b2015-10-01 12:34:46 +0100725 }
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100726 }
727
728 pinned = __get_user_pages_fast(obj->userptr.ptr, num_pages,
729 !obj->userptr.read_only, pvec);
730 }
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100731
Chris Wilsone4b946b2015-10-01 12:34:46 +0100732 active = false;
733 if (pinned < 0)
734 ret = pinned, pinned = 0;
735 else if (pinned < num_pages)
736 ret = __i915_gem_userptr_get_pages_schedule(obj, &active);
737 else
Imre Deake2273302015-07-09 12:59:05 +0300738 ret = __i915_gem_userptr_set_pages(obj, pvec, num_pages);
Chris Wilsone4b946b2015-10-01 12:34:46 +0100739 if (ret) {
740 __i915_gem_userptr_set_active(obj, active);
741 release_pages(pvec, pinned, 0);
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100742 }
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100743 drm_free_large(pvec);
744 return ret;
745}
746
747static void
748i915_gem_userptr_put_pages(struct drm_i915_gem_object *obj)
749{
Tvrtko Ursulinc479f432014-09-26 15:05:22 +0100750 struct sg_page_iter sg_iter;
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100751
752 BUG_ON(obj->userptr.work != NULL);
Chris Wilsone4b946b2015-10-01 12:34:46 +0100753 __i915_gem_userptr_set_active(obj, false);
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100754
755 if (obj->madv != I915_MADV_WILLNEED)
756 obj->dirty = 0;
757
Imre Deake2273302015-07-09 12:59:05 +0300758 i915_gem_gtt_finish_object(obj);
759
Tvrtko Ursulinc479f432014-09-26 15:05:22 +0100760 for_each_sg_page(obj->pages->sgl, &sg_iter, obj->pages->nents, 0) {
761 struct page *page = sg_page_iter_page(&sg_iter);
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100762
763 if (obj->dirty)
764 set_page_dirty(page);
765
766 mark_page_accessed(page);
767 page_cache_release(page);
768 }
769 obj->dirty = 0;
770
771 sg_free_table(obj->pages);
772 kfree(obj->pages);
773}
774
775static void
776i915_gem_userptr_release(struct drm_i915_gem_object *obj)
777{
778 i915_gem_userptr_release__mmu_notifier(obj);
Chris Wilsonad46cb52014-08-07 14:20:40 +0100779 i915_gem_userptr_release__mm_struct(obj);
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100780}
781
782static int
783i915_gem_userptr_dmabuf_export(struct drm_i915_gem_object *obj)
784{
Chris Wilsonad46cb52014-08-07 14:20:40 +0100785 if (obj->userptr.mmu_object)
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100786 return 0;
787
788 return i915_gem_userptr_init__mmu_notifier(obj, 0);
789}
790
791static const struct drm_i915_gem_object_ops i915_gem_userptr_ops = {
Chris Wilson93232ae2016-01-22 18:32:31 +0000792 .flags = I915_GEM_OBJECT_HAS_STRUCT_PAGE,
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100793 .get_pages = i915_gem_userptr_get_pages,
794 .put_pages = i915_gem_userptr_put_pages,
Chris Wilson93232ae2016-01-22 18:32:31 +0000795 .dmabuf_export = i915_gem_userptr_dmabuf_export,
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100796 .release = i915_gem_userptr_release,
797};
798
799/**
800 * Creates a new mm object that wraps some normal memory from the process
801 * context - user memory.
802 *
803 * We impose several restrictions upon the memory being mapped
804 * into the GPU.
805 * 1. It must be page aligned (both start/end addresses, i.e ptr and size).
Chris Wilsonec8b0dd2014-07-21 13:21:23 +0100806 * 2. It must be normal system memory, not a pointer into another map of IO
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100807 * space (e.g. it must not be a GTT mmapping of another object).
Chris Wilsonec8b0dd2014-07-21 13:21:23 +0100808 * 3. We only allow a bo as large as we could in theory map into the GTT,
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100809 * that is we limit the size to the total size of the GTT.
Chris Wilsonec8b0dd2014-07-21 13:21:23 +0100810 * 4. The bo is marked as being snoopable. The backing pages are left
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100811 * accessible directly by the CPU, but reads and writes by the GPU may
812 * incur the cost of a snoop (unless you have an LLC architecture).
813 *
814 * Synchronisation between multiple users and the GPU is left to userspace
815 * through the normal set-domain-ioctl. The kernel will enforce that the
816 * GPU relinquishes the VMA before it is returned back to the system
817 * i.e. upon free(), munmap() or process termination. However, the userspace
818 * malloc() library may not immediately relinquish the VMA after free() and
819 * instead reuse it whilst the GPU is still reading and writing to the VMA.
820 * Caveat emptor.
821 *
822 * Also note, that the object created here is not currently a "first class"
823 * object, in that several ioctls are banned. These are the CPU access
824 * ioctls: mmap(), pwrite and pread. In practice, you are expected to use
Chris Wilsoncc917ab2015-10-13 14:22:26 +0100825 * direct access via your pointer rather than use those ioctls. Another
826 * restriction is that we do not allow userptr surfaces to be pinned to the
827 * hardware and so we reject any attempt to create a framebuffer out of a
828 * userptr.
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100829 *
830 * If you think this is a good interface to use to pass GPU memory between
831 * drivers, please use dma-buf instead. In fact, wherever possible use
832 * dma-buf instead.
833 */
834int
835i915_gem_userptr_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
836{
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100837 struct drm_i915_gem_userptr *args = data;
838 struct drm_i915_gem_object *obj;
839 int ret;
840 u32 handle;
841
842 if (args->flags & ~(I915_USERPTR_READ_ONLY |
843 I915_USERPTR_UNSYNCHRONIZED))
844 return -EINVAL;
845
846 if (offset_in_page(args->user_ptr | args->user_size))
847 return -EINVAL;
848
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100849 if (!access_ok(args->flags & I915_USERPTR_READ_ONLY ? VERIFY_READ : VERIFY_WRITE,
850 (char __user *)(unsigned long)args->user_ptr, args->user_size))
851 return -EFAULT;
852
853 if (args->flags & I915_USERPTR_READ_ONLY) {
854 /* On almost all of the current hw, we cannot tell the GPU that a
855 * page is readonly, so this is just a placeholder in the uAPI.
856 */
857 return -ENODEV;
858 }
859
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100860 obj = i915_gem_object_alloc(dev);
861 if (obj == NULL)
862 return -ENOMEM;
863
864 drm_gem_private_object_init(dev, &obj->base, args->user_size);
865 i915_gem_object_init(obj, &i915_gem_userptr_ops);
866 obj->cache_level = I915_CACHE_LLC;
867 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
868 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
869
870 obj->userptr.ptr = args->user_ptr;
871 obj->userptr.read_only = !!(args->flags & I915_USERPTR_READ_ONLY);
872
873 /* And keep a pointer to the current->mm for resolving the user pages
874 * at binding. This means that we need to hook into the mmu_notifier
875 * in order to detect if the mmu is destroyed.
876 */
Chris Wilsonad46cb52014-08-07 14:20:40 +0100877 ret = i915_gem_userptr_init__mm_struct(obj);
878 if (ret == 0)
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100879 ret = i915_gem_userptr_init__mmu_notifier(obj, args->flags);
880 if (ret == 0)
881 ret = drm_gem_handle_create(file, &obj->base, &handle);
882
883 /* drop reference from allocate - handle holds it now */
884 drm_gem_object_unreference_unlocked(&obj->base);
885 if (ret)
886 return ret;
887
888 args->handle = handle;
889 return 0;
890}
891
892int
893i915_gem_init_userptr(struct drm_device *dev)
894{
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100895 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilsonad46cb52014-08-07 14:20:40 +0100896 mutex_init(&dev_priv->mm_lock);
897 hash_init(dev_priv->mm_structs);
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100898 return 0;
899}