blob: 65802d93fdc13cfb3351dc4d577f0dbd0e4c9a4f [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 Wilson768e1592016-01-21 17:32:43 +000064static void cancel_userptr(struct work_struct *work)
Chris Wilsonec8b0dd2014-07-21 13:21:23 +010065{
Chris Wilson380996a2015-10-01 12:34:47 +010066 struct i915_mmu_object *mo = container_of(work, typeof(*mo), work);
67 struct drm_i915_gem_object *obj = mo->obj;
Chris Wilsonec8b0dd2014-07-21 13:21:23 +010068 struct drm_device *dev = obj->base.dev;
Chris Wilsonec8b0dd2014-07-21 13:21:23 +010069
Chris Wilsone95433c2016-10-28 13:58:27 +010070 i915_gem_object_wait(obj, I915_WAIT_ALL, MAX_SCHEDULE_TIMEOUT, NULL);
Chris Wilson8a3b3d52016-08-05 10:14:08 +010071
Chris Wilsonec8b0dd2014-07-21 13:21:23 +010072 mutex_lock(&dev->struct_mutex);
73 /* Cancel any active worker and force us to re-evaluate gup */
74 obj->userptr.work = NULL;
75
Chris Wilson03ac84f2016-10-28 13:58:36 +010076 /* We are inside a kthread context and can't be interrupted */
77 if (i915_gem_object_unbind(obj) == 0)
Chris Wilson548625e2016-11-01 12:11:34 +000078 __i915_gem_object_put_pages(obj, I915_MM_NORMAL);
Chris Wilson03ac84f2016-10-28 13:58:36 +010079 WARN_ONCE(obj->mm.pages,
80 "Failed to release pages: bind_count=%d, pages_pin_count=%d, pin_display=%d\n",
81 obj->bind_count,
Chris Wilson1233e2d2016-10-28 13:58:37 +010082 atomic_read(&obj->mm.pages_pin_count),
Chris Wilson03ac84f2016-10-28 13:58:36 +010083 obj->pin_display);
Chris Wilsonec8b0dd2014-07-21 13:21:23 +010084
Chris Wilsonf8c417c2016-07-20 13:31:53 +010085 i915_gem_object_put(obj);
Chris Wilsonec8b0dd2014-07-21 13:21:23 +010086 mutex_unlock(&dev->struct_mutex);
Chris Wilsonec8b0dd2014-07-21 13:21:23 +010087}
88
Chris Wilson768e1592016-01-21 17:32:43 +000089static void add_object(struct i915_mmu_object *mo)
Chris Wilsonec8b0dd2014-07-21 13:21:23 +010090{
Chris Wilson768e1592016-01-21 17:32:43 +000091 if (mo->attached)
92 return;
Chris Wilsonec8b0dd2014-07-21 13:21:23 +010093
Chris Wilson768e1592016-01-21 17:32:43 +000094 interval_tree_insert(&mo->it, &mo->mn->objects);
95 mo->attached = true;
96}
Chris Wilsonec8b0dd2014-07-21 13:21:23 +010097
Chris Wilson768e1592016-01-21 17:32:43 +000098static void del_object(struct i915_mmu_object *mo)
99{
100 if (!mo->attached)
101 return;
102
103 interval_tree_remove(&mo->it, &mo->mn->objects);
104 mo->attached = false;
Chris Wilsonec8b0dd2014-07-21 13:21:23 +0100105}
106
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100107static void i915_gem_userptr_mn_invalidate_range_start(struct mmu_notifier *_mn,
108 struct mm_struct *mm,
109 unsigned long start,
110 unsigned long end)
111{
Chris Wilson380996a2015-10-01 12:34:47 +0100112 struct i915_mmu_notifier *mn =
113 container_of(_mn, struct i915_mmu_notifier, mn);
114 struct i915_mmu_object *mo;
Chris Wilson768e1592016-01-21 17:32:43 +0000115 struct interval_tree_node *it;
116 LIST_HEAD(cancelled);
117
118 if (RB_EMPTY_ROOT(&mn->objects))
119 return;
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100120
Chris Wilson380996a2015-10-01 12:34:47 +0100121 /* interval ranges are inclusive, but invalidate range is exclusive */
122 end--;
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100123
Chris Wilson380996a2015-10-01 12:34:47 +0100124 spin_lock(&mn->lock);
Chris Wilson768e1592016-01-21 17:32:43 +0000125 it = interval_tree_iter_first(&mn->objects, start, end);
126 while (it) {
127 /* The mmu_object is released late when destroying the
128 * GEM object so it is entirely possible to gain a
129 * reference on an object in the process of being freed
130 * since our serialisation is via the spinlock and not
131 * the struct_mutex - and consequently use it after it
132 * is freed and then double free it. To prevent that
133 * use-after-free we only acquire a reference on the
134 * object if it is not in the process of being destroyed.
135 */
136 mo = container_of(it, struct i915_mmu_object, it);
137 if (kref_get_unless_zero(&mo->obj->base.refcount))
Chris Wilson393afc22016-04-05 14:59:59 +0100138 queue_work(mn->wq, &mo->work);
Michał Winiarski460822b2015-02-03 15:48:17 +0100139
Chris Wilson768e1592016-01-21 17:32:43 +0000140 list_add(&mo->link, &cancelled);
141 it = interval_tree_iter_next(it, start, end);
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100142 }
Chris Wilson768e1592016-01-21 17:32:43 +0000143 list_for_each_entry(mo, &cancelled, link)
144 del_object(mo);
Chris Wilson380996a2015-10-01 12:34:47 +0100145 spin_unlock(&mn->lock);
Chris Wilson393afc22016-04-05 14:59:59 +0100146
147 flush_workqueue(mn->wq);
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 Wilson393afc22016-04-05 14:59:59 +0100167 mn->wq = alloc_workqueue("i915-userptr-release", WQ_UNBOUND, 0);
168 if (mn->wq == NULL) {
169 kfree(mn);
170 return ERR_PTR(-ENOMEM);
171 }
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100172
Chris Wilsonad46cb52014-08-07 14:20:40 +0100173 /* Protected by mmap_sem (write-lock) */
174 ret = __mmu_notifier_register(&mn->mn, mm);
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100175 if (ret) {
Chris Wilson393afc22016-04-05 14:59:59 +0100176 destroy_workqueue(mn->wq);
Chris Wilsonad46cb52014-08-07 14:20:40 +0100177 kfree(mn);
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100178 return ERR_PTR(ret);
179 }
180
Chris Wilsonad46cb52014-08-07 14:20:40 +0100181 return mn;
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100182}
183
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100184static void
185i915_gem_userptr_release__mmu_notifier(struct drm_i915_gem_object *obj)
186{
Chris Wilsonad46cb52014-08-07 14:20:40 +0100187 struct i915_mmu_object *mo;
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100188
Chris Wilsonad46cb52014-08-07 14:20:40 +0100189 mo = obj->userptr.mmu_object;
190 if (mo == NULL)
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100191 return;
192
Chris Wilson768e1592016-01-21 17:32:43 +0000193 spin_lock(&mo->mn->lock);
194 del_object(mo);
195 spin_unlock(&mo->mn->lock);
Chris Wilsonad46cb52014-08-07 14:20:40 +0100196 kfree(mo);
197
198 obj->userptr.mmu_object = NULL;
199}
200
201static struct i915_mmu_notifier *
202i915_mmu_notifier_find(struct i915_mm_struct *mm)
203{
Chris Wilsone9681362014-09-26 10:31:02 +0100204 struct i915_mmu_notifier *mn = mm->mn;
205
206 mn = mm->mn;
207 if (mn)
208 return mn;
209
210 down_write(&mm->mm->mmap_sem);
Chris Wilsonf470b192016-04-05 15:00:01 +0100211 mutex_lock(&mm->i915->mm_lock);
Chris Wilsone9681362014-09-26 10:31:02 +0100212 if ((mn = mm->mn) == NULL) {
213 mn = i915_mmu_notifier_create(mm->mm);
214 if (!IS_ERR(mn))
215 mm->mn = mn;
Chris Wilsonad46cb52014-08-07 14:20:40 +0100216 }
Chris Wilsonf470b192016-04-05 15:00:01 +0100217 mutex_unlock(&mm->i915->mm_lock);
Chris Wilsone9681362014-09-26 10:31:02 +0100218 up_write(&mm->mm->mmap_sem);
219
220 return mn;
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100221}
222
223static int
224i915_gem_userptr_init__mmu_notifier(struct drm_i915_gem_object *obj,
225 unsigned flags)
226{
Chris Wilsonad46cb52014-08-07 14:20:40 +0100227 struct i915_mmu_notifier *mn;
228 struct i915_mmu_object *mo;
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100229
230 if (flags & I915_USERPTR_UNSYNCHRONIZED)
231 return capable(CAP_SYS_ADMIN) ? 0 : -EPERM;
232
Chris Wilsonad46cb52014-08-07 14:20:40 +0100233 if (WARN_ON(obj->userptr.mm == NULL))
234 return -EINVAL;
235
236 mn = i915_mmu_notifier_find(obj->userptr.mm);
237 if (IS_ERR(mn))
238 return PTR_ERR(mn);
239
240 mo = kzalloc(sizeof(*mo), GFP_KERNEL);
241 if (mo == NULL)
242 return -ENOMEM;
243
244 mo->mn = mn;
Chris Wilsonad46cb52014-08-07 14:20:40 +0100245 mo->obj = obj;
Chris Wilson768e1592016-01-21 17:32:43 +0000246 mo->it.start = obj->userptr.ptr;
247 mo->it.last = obj->userptr.ptr + obj->base.size - 1;
248 INIT_WORK(&mo->work, cancel_userptr);
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100249
Chris Wilsonad46cb52014-08-07 14:20:40 +0100250 obj->userptr.mmu_object = mo;
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100251 return 0;
Chris Wilsonad46cb52014-08-07 14:20:40 +0100252}
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100253
Chris Wilsonad46cb52014-08-07 14:20:40 +0100254static void
255i915_mmu_notifier_free(struct i915_mmu_notifier *mn,
256 struct mm_struct *mm)
257{
258 if (mn == NULL)
259 return;
260
261 mmu_notifier_unregister(&mn->mn, mm);
Chris Wilson393afc22016-04-05 14:59:59 +0100262 destroy_workqueue(mn->wq);
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100263 kfree(mn);
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100264}
265
266#else
267
268static void
269i915_gem_userptr_release__mmu_notifier(struct drm_i915_gem_object *obj)
270{
271}
272
273static int
274i915_gem_userptr_init__mmu_notifier(struct drm_i915_gem_object *obj,
275 unsigned flags)
276{
277 if ((flags & I915_USERPTR_UNSYNCHRONIZED) == 0)
278 return -ENODEV;
279
280 if (!capable(CAP_SYS_ADMIN))
281 return -EPERM;
282
283 return 0;
284}
Chris Wilsonad46cb52014-08-07 14:20:40 +0100285
286static void
287i915_mmu_notifier_free(struct i915_mmu_notifier *mn,
288 struct mm_struct *mm)
289{
290}
291
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100292#endif
293
Chris Wilsonad46cb52014-08-07 14:20:40 +0100294static struct i915_mm_struct *
295__i915_mm_struct_find(struct drm_i915_private *dev_priv, struct mm_struct *real)
296{
297 struct i915_mm_struct *mm;
298
299 /* Protected by dev_priv->mm_lock */
300 hash_for_each_possible(dev_priv->mm_structs, mm, node, (unsigned long)real)
301 if (mm->mm == real)
302 return mm;
303
304 return NULL;
305}
306
307static int
308i915_gem_userptr_init__mm_struct(struct drm_i915_gem_object *obj)
309{
310 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
311 struct i915_mm_struct *mm;
312 int ret = 0;
313
314 /* During release of the GEM object we hold the struct_mutex. This
315 * precludes us from calling mmput() at that time as that may be
316 * the last reference and so call exit_mmap(). exit_mmap() will
317 * attempt to reap the vma, and if we were holding a GTT mmap
318 * would then call drm_gem_vm_close() and attempt to reacquire
319 * the struct mutex. So in order to avoid that recursion, we have
320 * to defer releasing the mm reference until after we drop the
321 * struct_mutex, i.e. we need to schedule a worker to do the clean
322 * up.
323 */
324 mutex_lock(&dev_priv->mm_lock);
325 mm = __i915_mm_struct_find(dev_priv, current->mm);
326 if (mm == NULL) {
327 mm = kmalloc(sizeof(*mm), GFP_KERNEL);
328 if (mm == NULL) {
329 ret = -ENOMEM;
330 goto out;
331 }
332
333 kref_init(&mm->kref);
Chris Wilsonf470b192016-04-05 15:00:01 +0100334 mm->i915 = to_i915(obj->base.dev);
Chris Wilsonad46cb52014-08-07 14:20:40 +0100335
336 mm->mm = current->mm;
Vegard Nossumf1f10072017-02-27 14:30:07 -0800337 mmgrab(current->mm);
Chris Wilsonad46cb52014-08-07 14:20:40 +0100338
339 mm->mn = NULL;
340
341 /* Protected by dev_priv->mm_lock */
342 hash_add(dev_priv->mm_structs,
343 &mm->node, (unsigned long)mm->mm);
344 } else
345 kref_get(&mm->kref);
346
347 obj->userptr.mm = mm;
348out:
349 mutex_unlock(&dev_priv->mm_lock);
350 return ret;
351}
352
353static void
354__i915_mm_struct_free__worker(struct work_struct *work)
355{
356 struct i915_mm_struct *mm = container_of(work, typeof(*mm), work);
357 i915_mmu_notifier_free(mm->mn, mm->mm);
358 mmdrop(mm->mm);
359 kfree(mm);
360}
361
362static void
363__i915_mm_struct_free(struct kref *kref)
364{
365 struct i915_mm_struct *mm = container_of(kref, typeof(*mm), kref);
366
367 /* Protected by dev_priv->mm_lock */
368 hash_del(&mm->node);
Chris Wilsonf470b192016-04-05 15:00:01 +0100369 mutex_unlock(&mm->i915->mm_lock);
Chris Wilsonad46cb52014-08-07 14:20:40 +0100370
371 INIT_WORK(&mm->work, __i915_mm_struct_free__worker);
372 schedule_work(&mm->work);
373}
374
375static void
376i915_gem_userptr_release__mm_struct(struct drm_i915_gem_object *obj)
377{
378 if (obj->userptr.mm == NULL)
379 return;
380
381 kref_put_mutex(&obj->userptr.mm->kref,
382 __i915_mm_struct_free,
383 &to_i915(obj->base.dev)->mm_lock);
384 obj->userptr.mm = NULL;
385}
386
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100387struct get_pages_work {
388 struct work_struct work;
389 struct drm_i915_gem_object *obj;
390 struct task_struct *task;
391};
392
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100393#if IS_ENABLED(CONFIG_SWIOTLB)
394#define swiotlb_active() swiotlb_nr_tbl()
395#else
396#define swiotlb_active() 0
397#endif
398
399static int
400st_set_pages(struct sg_table **st, struct page **pvec, int num_pages)
401{
402 struct scatterlist *sg;
403 int ret, n;
404
405 *st = kmalloc(sizeof(**st), GFP_KERNEL);
406 if (*st == NULL)
407 return -ENOMEM;
408
409 if (swiotlb_active()) {
410 ret = sg_alloc_table(*st, num_pages, GFP_KERNEL);
411 if (ret)
412 goto err;
413
414 for_each_sg((*st)->sgl, sg, num_pages, n)
415 sg_set_page(sg, pvec[n], PAGE_SIZE, 0);
416 } else {
417 ret = sg_alloc_table_from_pages(*st, pvec, num_pages,
418 0, num_pages << PAGE_SHIFT,
419 GFP_KERNEL);
420 if (ret)
421 goto err;
422 }
423
424 return 0;
425
426err:
427 kfree(*st);
428 *st = NULL;
429 return ret;
430}
431
Chris Wilson03ac84f2016-10-28 13:58:36 +0100432static struct sg_table *
Imre Deake2273302015-07-09 12:59:05 +0300433__i915_gem_userptr_set_pages(struct drm_i915_gem_object *obj,
434 struct page **pvec, int num_pages)
435{
Chris Wilson03ac84f2016-10-28 13:58:36 +0100436 struct sg_table *pages;
Imre Deake2273302015-07-09 12:59:05 +0300437 int ret;
438
Chris Wilson03ac84f2016-10-28 13:58:36 +0100439 ret = st_set_pages(&pages, pvec, num_pages);
Imre Deake2273302015-07-09 12:59:05 +0300440 if (ret)
Chris Wilson03ac84f2016-10-28 13:58:36 +0100441 return ERR_PTR(ret);
Imre Deake2273302015-07-09 12:59:05 +0300442
Chris Wilson03ac84f2016-10-28 13:58:36 +0100443 ret = i915_gem_gtt_prepare_pages(obj, pages);
Imre Deake2273302015-07-09 12:59:05 +0300444 if (ret) {
Chris Wilson03ac84f2016-10-28 13:58:36 +0100445 sg_free_table(pages);
446 kfree(pages);
447 return ERR_PTR(ret);
Imre Deake2273302015-07-09 12:59:05 +0300448 }
449
Chris Wilson03ac84f2016-10-28 13:58:36 +0100450 return pages;
Imre Deake2273302015-07-09 12:59:05 +0300451}
452
Chris Wilson380996a2015-10-01 12:34:47 +0100453static int
Chris Wilsone4b946b2015-10-01 12:34:46 +0100454__i915_gem_userptr_set_active(struct drm_i915_gem_object *obj,
455 bool value)
456{
Chris Wilson380996a2015-10-01 12:34:47 +0100457 int ret = 0;
458
Chris Wilsone4b946b2015-10-01 12:34:46 +0100459 /* During mm_invalidate_range we need to cancel any userptr that
460 * overlaps the range being invalidated. Doing so requires the
461 * struct_mutex, and that risks recursion. In order to cause
462 * recursion, the user must alias the userptr address space with
463 * a GTT mmapping (possible with a MAP_FIXED) - then when we have
464 * to invalidate that mmaping, mm_invalidate_range is called with
465 * the userptr address *and* the struct_mutex held. To prevent that
466 * we set a flag under the i915_mmu_notifier spinlock to indicate
467 * whether this object is valid.
468 */
469#if defined(CONFIG_MMU_NOTIFIER)
470 if (obj->userptr.mmu_object == NULL)
Chris Wilson380996a2015-10-01 12:34:47 +0100471 return 0;
Chris Wilsone4b946b2015-10-01 12:34:46 +0100472
473 spin_lock(&obj->userptr.mmu_object->mn->lock);
Chris Wilson380996a2015-10-01 12:34:47 +0100474 /* In order to serialise get_pages with an outstanding
475 * cancel_userptr, we must drop the struct_mutex and try again.
476 */
Chris Wilson768e1592016-01-21 17:32:43 +0000477 if (!value)
478 del_object(obj->userptr.mmu_object);
479 else if (!work_pending(&obj->userptr.mmu_object->work))
480 add_object(obj->userptr.mmu_object);
Chris Wilson380996a2015-10-01 12:34:47 +0100481 else
482 ret = -EAGAIN;
Chris Wilsone4b946b2015-10-01 12:34:46 +0100483 spin_unlock(&obj->userptr.mmu_object->mn->lock);
484#endif
Chris Wilson380996a2015-10-01 12:34:47 +0100485
486 return ret;
Chris Wilsone4b946b2015-10-01 12:34:46 +0100487}
488
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100489static void
490__i915_gem_userptr_get_pages_worker(struct work_struct *_work)
491{
492 struct get_pages_work *work = container_of(_work, typeof(*work), work);
493 struct drm_i915_gem_object *obj = work->obj;
Chris Wilson68d6c842015-10-01 12:34:45 +0100494 const int npages = obj->base.size >> PAGE_SHIFT;
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100495 struct page **pvec;
496 int pinned, ret;
497
498 ret = -ENOMEM;
499 pinned = 0;
500
Chris Wilsonf2a85e12016-04-08 12:11:13 +0100501 pvec = drm_malloc_gfp(npages, sizeof(struct page *), GFP_TEMPORARY);
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100502 if (pvec != NULL) {
Chris Wilsonad46cb52014-08-07 14:20:40 +0100503 struct mm_struct *mm = obj->userptr.mm->mm;
Lorenzo Stoakes9beae1e2016-10-13 01:20:17 +0100504 unsigned int flags = 0;
505
506 if (!obj->userptr.read_only)
507 flags |= FOLL_WRITE;
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100508
Chris Wilson40313f02016-04-05 15:00:00 +0100509 ret = -EFAULT;
510 if (atomic_inc_not_zero(&mm->mm_users)) {
511 down_read(&mm->mmap_sem);
512 while (pinned < npages) {
513 ret = get_user_pages_remote
514 (work->task, mm,
515 obj->userptr.ptr + pinned * PAGE_SIZE,
516 npages - pinned,
Lorenzo Stoakes9beae1e2016-10-13 01:20:17 +0100517 flags,
Lorenzo Stoakes5b56d492016-12-14 15:06:52 -0800518 pvec + pinned, NULL, NULL);
Chris Wilson40313f02016-04-05 15:00:00 +0100519 if (ret < 0)
520 break;
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100521
Chris Wilson40313f02016-04-05 15:00:00 +0100522 pinned += ret;
523 }
524 up_read(&mm->mmap_sem);
525 mmput(mm);
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100526 }
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100527 }
528
Chris Wilson1233e2d2016-10-28 13:58:37 +0100529 mutex_lock(&obj->mm.lock);
Chris Wilson68d6c842015-10-01 12:34:45 +0100530 if (obj->userptr.work == &work->work) {
Chris Wilson03ac84f2016-10-28 13:58:36 +0100531 struct sg_table *pages = ERR_PTR(ret);
532
Chris Wilson68d6c842015-10-01 12:34:45 +0100533 if (pinned == npages) {
Chris Wilson03ac84f2016-10-28 13:58:36 +0100534 pages = __i915_gem_userptr_set_pages(obj, pvec, npages);
535 if (!IS_ERR(pages)) {
536 __i915_gem_object_set_pages(obj, pages);
Chris Wilson68d6c842015-10-01 12:34:45 +0100537 pinned = 0;
Chris Wilson03ac84f2016-10-28 13:58:36 +0100538 pages = NULL;
Chris Wilson68d6c842015-10-01 12:34:45 +0100539 }
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100540 }
Chris Wilson03ac84f2016-10-28 13:58:36 +0100541
542 obj->userptr.work = ERR_CAST(pages);
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100543 }
Chris Wilson1233e2d2016-10-28 13:58:37 +0100544 mutex_unlock(&obj->mm.lock);
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100545
546 release_pages(pvec, pinned, 0);
547 drm_free_large(pvec);
548
Chris Wilsonf0cd5182016-10-28 13:58:43 +0100549 i915_gem_object_put(obj);
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100550 put_task_struct(work->task);
551 kfree(work);
552}
553
Chris Wilson03ac84f2016-10-28 13:58:36 +0100554static struct sg_table *
Chris Wilsone4b946b2015-10-01 12:34:46 +0100555__i915_gem_userptr_get_pages_schedule(struct drm_i915_gem_object *obj,
556 bool *active)
557{
558 struct get_pages_work *work;
559
560 /* Spawn a worker so that we can acquire the
561 * user pages without holding our mutex. Access
562 * to the user pages requires mmap_sem, and we have
563 * a strict lock ordering of mmap_sem, struct_mutex -
564 * we already hold struct_mutex here and so cannot
565 * call gup without encountering a lock inversion.
566 *
567 * Userspace will keep on repeating the operation
568 * (thanks to EAGAIN) until either we hit the fast
569 * path or the worker completes. If the worker is
570 * cancelled or superseded, the task is still run
571 * but the results ignored. (This leads to
572 * complications that we may have a stray object
573 * refcount that we need to be wary of when
574 * checking for existing objects during creation.)
575 * If the worker encounters an error, it reports
576 * that error back to this function through
577 * obj->userptr.work = ERR_PTR.
578 */
Chris Wilsone4b946b2015-10-01 12:34:46 +0100579 work = kmalloc(sizeof(*work), GFP_KERNEL);
580 if (work == NULL)
Chris Wilson03ac84f2016-10-28 13:58:36 +0100581 return ERR_PTR(-ENOMEM);
Chris Wilsone4b946b2015-10-01 12:34:46 +0100582
583 obj->userptr.work = &work->work;
Chris Wilsone4b946b2015-10-01 12:34:46 +0100584
Chris Wilson25dc5562016-07-20 13:31:52 +0100585 work->obj = i915_gem_object_get(obj);
Chris Wilsone4b946b2015-10-01 12:34:46 +0100586
587 work->task = current;
588 get_task_struct(work->task);
589
590 INIT_WORK(&work->work, __i915_gem_userptr_get_pages_worker);
591 schedule_work(&work->work);
592
593 *active = true;
Chris Wilson03ac84f2016-10-28 13:58:36 +0100594 return ERR_PTR(-EAGAIN);
Chris Wilsone4b946b2015-10-01 12:34:46 +0100595}
596
Chris Wilson03ac84f2016-10-28 13:58:36 +0100597static struct sg_table *
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100598i915_gem_userptr_get_pages(struct drm_i915_gem_object *obj)
599{
600 const int num_pages = obj->base.size >> PAGE_SHIFT;
601 struct page **pvec;
Chris Wilson03ac84f2016-10-28 13:58:36 +0100602 struct sg_table *pages;
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100603 int pinned, ret;
Chris Wilsone4b946b2015-10-01 12:34:46 +0100604 bool active;
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100605
606 /* If userspace should engineer that these pages are replaced in
607 * the vma between us binding this page into the GTT and completion
608 * of rendering... Their loss. If they change the mapping of their
609 * pages they need to create a new bo to point to the new vma.
610 *
611 * However, that still leaves open the possibility of the vma
612 * being copied upon fork. Which falls under the same userspace
613 * synchronisation issue as a regular bo, except that this time
614 * the process may not be expecting that a particular piece of
615 * memory is tied to the GPU.
616 *
617 * Fortunately, we can hook into the mmu_notifier in order to
618 * discard the page references prior to anything nasty happening
619 * to the vma (discard or cloning) which should prevent the more
620 * egregious cases from causing harm.
621 */
Chris Wilson364c8172016-08-18 17:16:58 +0100622
623 if (obj->userptr.work) {
Chris Wilsone4b946b2015-10-01 12:34:46 +0100624 /* active flag should still be held for the pending work */
Chris Wilson364c8172016-08-18 17:16:58 +0100625 if (IS_ERR(obj->userptr.work))
Chris Wilson03ac84f2016-10-28 13:58:36 +0100626 return ERR_CAST(obj->userptr.work);
Chris Wilson364c8172016-08-18 17:16:58 +0100627 else
Chris Wilson03ac84f2016-10-28 13:58:36 +0100628 return ERR_PTR(-EAGAIN);
Chris Wilson364c8172016-08-18 17:16:58 +0100629 }
Chris Wilsone4b946b2015-10-01 12:34:46 +0100630
631 /* Let the mmu-notifier know that we have begun and need cancellation */
Chris Wilson380996a2015-10-01 12:34:47 +0100632 ret = __i915_gem_userptr_set_active(obj, true);
633 if (ret)
Chris Wilson03ac84f2016-10-28 13:58:36 +0100634 return ERR_PTR(ret);
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100635
636 pvec = NULL;
637 pinned = 0;
Chris Wilsonad46cb52014-08-07 14:20:40 +0100638 if (obj->userptr.mm->mm == current->mm) {
Chris Wilsonf2a85e12016-04-08 12:11:13 +0100639 pvec = drm_malloc_gfp(num_pages, sizeof(struct page *),
640 GFP_TEMPORARY);
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100641 if (pvec == NULL) {
Chris Wilsonf2a85e12016-04-08 12:11:13 +0100642 __i915_gem_userptr_set_active(obj, false);
Chris Wilson03ac84f2016-10-28 13:58:36 +0100643 return ERR_PTR(-ENOMEM);
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100644 }
645
646 pinned = __get_user_pages_fast(obj->userptr.ptr, num_pages,
647 !obj->userptr.read_only, pvec);
648 }
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100649
Chris Wilsone4b946b2015-10-01 12:34:46 +0100650 active = false;
651 if (pinned < 0)
Chris Wilson03ac84f2016-10-28 13:58:36 +0100652 pages = ERR_PTR(pinned), pinned = 0;
Chris Wilsone4b946b2015-10-01 12:34:46 +0100653 else if (pinned < num_pages)
Chris Wilson03ac84f2016-10-28 13:58:36 +0100654 pages = __i915_gem_userptr_get_pages_schedule(obj, &active);
Chris Wilsone4b946b2015-10-01 12:34:46 +0100655 else
Chris Wilson03ac84f2016-10-28 13:58:36 +0100656 pages = __i915_gem_userptr_set_pages(obj, pvec, num_pages);
657 if (IS_ERR(pages)) {
Chris Wilsone4b946b2015-10-01 12:34:46 +0100658 __i915_gem_userptr_set_active(obj, active);
659 release_pages(pvec, pinned, 0);
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100660 }
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100661 drm_free_large(pvec);
Chris Wilson03ac84f2016-10-28 13:58:36 +0100662 return pages;
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100663}
664
665static void
Chris Wilson03ac84f2016-10-28 13:58:36 +0100666i915_gem_userptr_put_pages(struct drm_i915_gem_object *obj,
667 struct sg_table *pages)
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100668{
Dave Gordon85d12252016-05-20 11:54:06 +0100669 struct sgt_iter sgt_iter;
670 struct page *page;
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100671
672 BUG_ON(obj->userptr.work != NULL);
Chris Wilsone4b946b2015-10-01 12:34:46 +0100673 __i915_gem_userptr_set_active(obj, false);
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100674
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100675 if (obj->mm.madv != I915_MADV_WILLNEED)
676 obj->mm.dirty = false;
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100677
Chris Wilson03ac84f2016-10-28 13:58:36 +0100678 i915_gem_gtt_finish_pages(obj, pages);
Imre Deake2273302015-07-09 12:59:05 +0300679
Chris Wilson03ac84f2016-10-28 13:58:36 +0100680 for_each_sgt_page(page, sgt_iter, pages) {
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100681 if (obj->mm.dirty)
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100682 set_page_dirty(page);
683
684 mark_page_accessed(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300685 put_page(page);
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100686 }
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100687 obj->mm.dirty = false;
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100688
Chris Wilson03ac84f2016-10-28 13:58:36 +0100689 sg_free_table(pages);
690 kfree(pages);
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100691}
692
693static void
694i915_gem_userptr_release(struct drm_i915_gem_object *obj)
695{
696 i915_gem_userptr_release__mmu_notifier(obj);
Chris Wilsonad46cb52014-08-07 14:20:40 +0100697 i915_gem_userptr_release__mm_struct(obj);
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100698}
699
700static int
701i915_gem_userptr_dmabuf_export(struct drm_i915_gem_object *obj)
702{
Chris Wilsonad46cb52014-08-07 14:20:40 +0100703 if (obj->userptr.mmu_object)
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100704 return 0;
705
706 return i915_gem_userptr_init__mmu_notifier(obj, 0);
707}
708
709static const struct drm_i915_gem_object_ops i915_gem_userptr_ops = {
Tvrtko Ursulin3599a912016-11-01 14:44:10 +0000710 .flags = I915_GEM_OBJECT_HAS_STRUCT_PAGE |
711 I915_GEM_OBJECT_IS_SHRINKABLE,
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100712 .get_pages = i915_gem_userptr_get_pages,
713 .put_pages = i915_gem_userptr_put_pages,
Chris Wilsonde472662016-01-22 18:32:31 +0000714 .dmabuf_export = i915_gem_userptr_dmabuf_export,
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100715 .release = i915_gem_userptr_release,
716};
717
718/**
719 * Creates a new mm object that wraps some normal memory from the process
720 * context - user memory.
721 *
722 * We impose several restrictions upon the memory being mapped
723 * into the GPU.
724 * 1. It must be page aligned (both start/end addresses, i.e ptr and size).
Chris Wilsonec8b0dd2014-07-21 13:21:23 +0100725 * 2. It must be normal system memory, not a pointer into another map of IO
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100726 * space (e.g. it must not be a GTT mmapping of another object).
Chris Wilsonec8b0dd2014-07-21 13:21:23 +0100727 * 3. We only allow a bo as large as we could in theory map into the GTT,
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100728 * that is we limit the size to the total size of the GTT.
Chris Wilsonec8b0dd2014-07-21 13:21:23 +0100729 * 4. The bo is marked as being snoopable. The backing pages are left
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100730 * accessible directly by the CPU, but reads and writes by the GPU may
731 * incur the cost of a snoop (unless you have an LLC architecture).
732 *
733 * Synchronisation between multiple users and the GPU is left to userspace
734 * through the normal set-domain-ioctl. The kernel will enforce that the
735 * GPU relinquishes the VMA before it is returned back to the system
736 * i.e. upon free(), munmap() or process termination. However, the userspace
737 * malloc() library may not immediately relinquish the VMA after free() and
738 * instead reuse it whilst the GPU is still reading and writing to the VMA.
739 * Caveat emptor.
740 *
741 * Also note, that the object created here is not currently a "first class"
742 * object, in that several ioctls are banned. These are the CPU access
743 * ioctls: mmap(), pwrite and pread. In practice, you are expected to use
Chris Wilsoncc917ab2015-10-13 14:22:26 +0100744 * direct access via your pointer rather than use those ioctls. Another
745 * restriction is that we do not allow userptr surfaces to be pinned to the
746 * hardware and so we reject any attempt to create a framebuffer out of a
747 * userptr.
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100748 *
749 * If you think this is a good interface to use to pass GPU memory between
750 * drivers, please use dma-buf instead. In fact, wherever possible use
751 * dma-buf instead.
752 */
753int
754i915_gem_userptr_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
755{
Tvrtko Ursulin0031fb92016-11-04 14:42:44 +0000756 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100757 struct drm_i915_gem_userptr *args = data;
758 struct drm_i915_gem_object *obj;
759 int ret;
760 u32 handle;
761
Tvrtko Ursulin0031fb92016-11-04 14:42:44 +0000762 if (!HAS_LLC(dev_priv) && !HAS_SNOOP(dev_priv)) {
Tvrtko Ursulinca377802016-03-02 12:10:31 +0000763 /* We cannot support coherent userptr objects on hw without
764 * LLC and broken snooping.
765 */
766 return -ENODEV;
767 }
768
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100769 if (args->flags & ~(I915_USERPTR_READ_ONLY |
770 I915_USERPTR_UNSYNCHRONIZED))
771 return -EINVAL;
772
773 if (offset_in_page(args->user_ptr | args->user_size))
774 return -EINVAL;
775
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100776 if (!access_ok(args->flags & I915_USERPTR_READ_ONLY ? VERIFY_READ : VERIFY_WRITE,
777 (char __user *)(unsigned long)args->user_ptr, args->user_size))
778 return -EFAULT;
779
780 if (args->flags & I915_USERPTR_READ_ONLY) {
781 /* On almost all of the current hw, we cannot tell the GPU that a
782 * page is readonly, so this is just a placeholder in the uAPI.
783 */
784 return -ENODEV;
785 }
786
Tvrtko Ursulin187685c2016-12-01 14:16:36 +0000787 obj = i915_gem_object_alloc(dev_priv);
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100788 if (obj == NULL)
789 return -ENOMEM;
790
791 drm_gem_private_object_init(dev, &obj->base, args->user_size);
792 i915_gem_object_init(obj, &i915_gem_userptr_ops);
793 obj->cache_level = I915_CACHE_LLC;
794 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
795 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
796
797 obj->userptr.ptr = args->user_ptr;
798 obj->userptr.read_only = !!(args->flags & I915_USERPTR_READ_ONLY);
799
800 /* And keep a pointer to the current->mm for resolving the user pages
801 * at binding. This means that we need to hook into the mmu_notifier
802 * in order to detect if the mmu is destroyed.
803 */
Chris Wilsonad46cb52014-08-07 14:20:40 +0100804 ret = i915_gem_userptr_init__mm_struct(obj);
805 if (ret == 0)
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100806 ret = i915_gem_userptr_init__mmu_notifier(obj, args->flags);
807 if (ret == 0)
808 ret = drm_gem_handle_create(file, &obj->base, &handle);
809
810 /* drop reference from allocate - handle holds it now */
Chris Wilsonf0cd5182016-10-28 13:58:43 +0100811 i915_gem_object_put(obj);
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100812 if (ret)
813 return ret;
814
815 args->handle = handle;
816 return 0;
817}
818
Chris Wilson72778cb2016-05-19 16:17:16 +0100819void i915_gem_init_userptr(struct drm_i915_private *dev_priv)
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100820{
Chris Wilsonad46cb52014-08-07 14:20:40 +0100821 mutex_init(&dev_priv->mm_lock);
822 hash_init(dev_priv->mm_structs);
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100823}