blob: 32f50a70ea42782316b3a3565bbffea21474e4c9 [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
Chris Wilson381f3712016-08-04 07:52:29 +010077 req = obj->last_read[i].request;
Chris Wilson393afc22016-04-05 14:59:59 +010078 if (req == NULL)
79 continue;
80
Chris Wilsone8a261e2016-07-20 13:31:49 +010081 requests[n++] = i915_gem_request_get(req);
Chris Wilson393afc22016-04-05 14:59:59 +010082 }
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++)
Chris Wilsone8a261e2016-07-20 13:31:49 +010092 i915_gem_request_put(requests[i]);
Chris Wilson393afc22016-04-05 14:59:59 +010093}
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);
Chris Wilsonec8b0dd2014-07-21 13:21:23 +0100107 bool was_interruptible;
108
Chris Wilson393afc22016-04-05 14:59:59 +0100109 wait_rendering(obj);
110
Chris Wilsonec8b0dd2014-07-21 13:21:23 +0100111 was_interruptible = dev_priv->mm.interruptible;
112 dev_priv->mm.interruptible = false;
113
Chris Wilsonaa653a62016-08-04 07:52:27 +0100114 WARN_ON(i915_gem_object_unbind(obj));
Chris Wilsonec8b0dd2014-07-21 13:21:23 +0100115 WARN_ON(i915_gem_object_put_pages(obj));
116
117 dev_priv->mm.interruptible = was_interruptible;
118 }
119
Chris Wilsonf8c417c2016-07-20 13:31:53 +0100120 i915_gem_object_put(obj);
Chris Wilsonec8b0dd2014-07-21 13:21:23 +0100121 mutex_unlock(&dev->struct_mutex);
Chris Wilsonec8b0dd2014-07-21 13:21:23 +0100122}
123
Chris Wilson768e1592016-01-21 17:32:43 +0000124static void add_object(struct i915_mmu_object *mo)
Chris Wilsonec8b0dd2014-07-21 13:21:23 +0100125{
Chris Wilson768e1592016-01-21 17:32:43 +0000126 if (mo->attached)
127 return;
Chris Wilsonec8b0dd2014-07-21 13:21:23 +0100128
Chris Wilson768e1592016-01-21 17:32:43 +0000129 interval_tree_insert(&mo->it, &mo->mn->objects);
130 mo->attached = true;
131}
Chris Wilsonec8b0dd2014-07-21 13:21:23 +0100132
Chris Wilson768e1592016-01-21 17:32:43 +0000133static void del_object(struct i915_mmu_object *mo)
134{
135 if (!mo->attached)
136 return;
137
138 interval_tree_remove(&mo->it, &mo->mn->objects);
139 mo->attached = false;
Chris Wilsonec8b0dd2014-07-21 13:21:23 +0100140}
141
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100142static void i915_gem_userptr_mn_invalidate_range_start(struct mmu_notifier *_mn,
143 struct mm_struct *mm,
144 unsigned long start,
145 unsigned long end)
146{
Chris Wilson380996a2015-10-01 12:34:47 +0100147 struct i915_mmu_notifier *mn =
148 container_of(_mn, struct i915_mmu_notifier, mn);
149 struct i915_mmu_object *mo;
Chris Wilson768e1592016-01-21 17:32:43 +0000150 struct interval_tree_node *it;
151 LIST_HEAD(cancelled);
152
153 if (RB_EMPTY_ROOT(&mn->objects))
154 return;
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100155
Chris Wilson380996a2015-10-01 12:34:47 +0100156 /* interval ranges are inclusive, but invalidate range is exclusive */
157 end--;
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100158
Chris Wilson380996a2015-10-01 12:34:47 +0100159 spin_lock(&mn->lock);
Chris Wilson768e1592016-01-21 17:32:43 +0000160 it = interval_tree_iter_first(&mn->objects, start, end);
161 while (it) {
162 /* The mmu_object is released late when destroying the
163 * GEM object so it is entirely possible to gain a
164 * reference on an object in the process of being freed
165 * since our serialisation is via the spinlock and not
166 * the struct_mutex - and consequently use it after it
167 * is freed and then double free it. To prevent that
168 * use-after-free we only acquire a reference on the
169 * object if it is not in the process of being destroyed.
170 */
171 mo = container_of(it, struct i915_mmu_object, it);
172 if (kref_get_unless_zero(&mo->obj->base.refcount))
Chris Wilson393afc22016-04-05 14:59:59 +0100173 queue_work(mn->wq, &mo->work);
Michał Winiarski460822b2015-02-03 15:48:17 +0100174
Chris Wilson768e1592016-01-21 17:32:43 +0000175 list_add(&mo->link, &cancelled);
176 it = interval_tree_iter_next(it, start, end);
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100177 }
Chris Wilson768e1592016-01-21 17:32:43 +0000178 list_for_each_entry(mo, &cancelled, link)
179 del_object(mo);
Chris Wilson380996a2015-10-01 12:34:47 +0100180 spin_unlock(&mn->lock);
Chris Wilson393afc22016-04-05 14:59:59 +0100181
182 flush_workqueue(mn->wq);
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100183}
184
185static const struct mmu_notifier_ops i915_gem_userptr_notifier = {
186 .invalidate_range_start = i915_gem_userptr_mn_invalidate_range_start,
187};
188
189static struct i915_mmu_notifier *
Chris Wilsonad46cb52014-08-07 14:20:40 +0100190i915_mmu_notifier_create(struct mm_struct *mm)
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100191{
Chris Wilsonad46cb52014-08-07 14:20:40 +0100192 struct i915_mmu_notifier *mn;
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100193 int ret;
194
Chris Wilsonad46cb52014-08-07 14:20:40 +0100195 mn = kmalloc(sizeof(*mn), GFP_KERNEL);
196 if (mn == NULL)
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100197 return ERR_PTR(-ENOMEM);
198
Chris Wilsonad46cb52014-08-07 14:20:40 +0100199 spin_lock_init(&mn->lock);
200 mn->mn.ops = &i915_gem_userptr_notifier;
201 mn->objects = RB_ROOT;
Chris Wilson393afc22016-04-05 14:59:59 +0100202 mn->wq = alloc_workqueue("i915-userptr-release", WQ_UNBOUND, 0);
203 if (mn->wq == NULL) {
204 kfree(mn);
205 return ERR_PTR(-ENOMEM);
206 }
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100207
Chris Wilsonad46cb52014-08-07 14:20:40 +0100208 /* Protected by mmap_sem (write-lock) */
209 ret = __mmu_notifier_register(&mn->mn, mm);
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100210 if (ret) {
Chris Wilson393afc22016-04-05 14:59:59 +0100211 destroy_workqueue(mn->wq);
Chris Wilsonad46cb52014-08-07 14:20:40 +0100212 kfree(mn);
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100213 return ERR_PTR(ret);
214 }
215
Chris Wilsonad46cb52014-08-07 14:20:40 +0100216 return mn;
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100217}
218
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100219static void
220i915_gem_userptr_release__mmu_notifier(struct drm_i915_gem_object *obj)
221{
Chris Wilsonad46cb52014-08-07 14:20:40 +0100222 struct i915_mmu_object *mo;
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100223
Chris Wilsonad46cb52014-08-07 14:20:40 +0100224 mo = obj->userptr.mmu_object;
225 if (mo == NULL)
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100226 return;
227
Chris Wilson768e1592016-01-21 17:32:43 +0000228 spin_lock(&mo->mn->lock);
229 del_object(mo);
230 spin_unlock(&mo->mn->lock);
Chris Wilsonad46cb52014-08-07 14:20:40 +0100231 kfree(mo);
232
233 obj->userptr.mmu_object = NULL;
234}
235
236static struct i915_mmu_notifier *
237i915_mmu_notifier_find(struct i915_mm_struct *mm)
238{
Chris Wilsone9681362014-09-26 10:31:02 +0100239 struct i915_mmu_notifier *mn = mm->mn;
240
241 mn = mm->mn;
242 if (mn)
243 return mn;
244
245 down_write(&mm->mm->mmap_sem);
Chris Wilsonf470b192016-04-05 15:00:01 +0100246 mutex_lock(&mm->i915->mm_lock);
Chris Wilsone9681362014-09-26 10:31:02 +0100247 if ((mn = mm->mn) == NULL) {
248 mn = i915_mmu_notifier_create(mm->mm);
249 if (!IS_ERR(mn))
250 mm->mn = mn;
Chris Wilsonad46cb52014-08-07 14:20:40 +0100251 }
Chris Wilsonf470b192016-04-05 15:00:01 +0100252 mutex_unlock(&mm->i915->mm_lock);
Chris Wilsone9681362014-09-26 10:31:02 +0100253 up_write(&mm->mm->mmap_sem);
254
255 return mn;
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100256}
257
258static int
259i915_gem_userptr_init__mmu_notifier(struct drm_i915_gem_object *obj,
260 unsigned flags)
261{
Chris Wilsonad46cb52014-08-07 14:20:40 +0100262 struct i915_mmu_notifier *mn;
263 struct i915_mmu_object *mo;
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100264
265 if (flags & I915_USERPTR_UNSYNCHRONIZED)
266 return capable(CAP_SYS_ADMIN) ? 0 : -EPERM;
267
Chris Wilsonad46cb52014-08-07 14:20:40 +0100268 if (WARN_ON(obj->userptr.mm == NULL))
269 return -EINVAL;
270
271 mn = i915_mmu_notifier_find(obj->userptr.mm);
272 if (IS_ERR(mn))
273 return PTR_ERR(mn);
274
275 mo = kzalloc(sizeof(*mo), GFP_KERNEL);
276 if (mo == NULL)
277 return -ENOMEM;
278
279 mo->mn = mn;
Chris Wilsonad46cb52014-08-07 14:20:40 +0100280 mo->obj = obj;
Chris Wilson768e1592016-01-21 17:32:43 +0000281 mo->it.start = obj->userptr.ptr;
282 mo->it.last = obj->userptr.ptr + obj->base.size - 1;
283 INIT_WORK(&mo->work, cancel_userptr);
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100284
Chris Wilsonad46cb52014-08-07 14:20:40 +0100285 obj->userptr.mmu_object = mo;
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100286 return 0;
Chris Wilsonad46cb52014-08-07 14:20:40 +0100287}
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100288
Chris Wilsonad46cb52014-08-07 14:20:40 +0100289static void
290i915_mmu_notifier_free(struct i915_mmu_notifier *mn,
291 struct mm_struct *mm)
292{
293 if (mn == NULL)
294 return;
295
296 mmu_notifier_unregister(&mn->mn, mm);
Chris Wilson393afc22016-04-05 14:59:59 +0100297 destroy_workqueue(mn->wq);
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100298 kfree(mn);
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100299}
300
301#else
302
303static void
304i915_gem_userptr_release__mmu_notifier(struct drm_i915_gem_object *obj)
305{
306}
307
308static int
309i915_gem_userptr_init__mmu_notifier(struct drm_i915_gem_object *obj,
310 unsigned flags)
311{
312 if ((flags & I915_USERPTR_UNSYNCHRONIZED) == 0)
313 return -ENODEV;
314
315 if (!capable(CAP_SYS_ADMIN))
316 return -EPERM;
317
318 return 0;
319}
Chris Wilsonad46cb52014-08-07 14:20:40 +0100320
321static void
322i915_mmu_notifier_free(struct i915_mmu_notifier *mn,
323 struct mm_struct *mm)
324{
325}
326
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100327#endif
328
Chris Wilsonad46cb52014-08-07 14:20:40 +0100329static struct i915_mm_struct *
330__i915_mm_struct_find(struct drm_i915_private *dev_priv, struct mm_struct *real)
331{
332 struct i915_mm_struct *mm;
333
334 /* Protected by dev_priv->mm_lock */
335 hash_for_each_possible(dev_priv->mm_structs, mm, node, (unsigned long)real)
336 if (mm->mm == real)
337 return mm;
338
339 return NULL;
340}
341
342static int
343i915_gem_userptr_init__mm_struct(struct drm_i915_gem_object *obj)
344{
345 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
346 struct i915_mm_struct *mm;
347 int ret = 0;
348
349 /* During release of the GEM object we hold the struct_mutex. This
350 * precludes us from calling mmput() at that time as that may be
351 * the last reference and so call exit_mmap(). exit_mmap() will
352 * attempt to reap the vma, and if we were holding a GTT mmap
353 * would then call drm_gem_vm_close() and attempt to reacquire
354 * the struct mutex. So in order to avoid that recursion, we have
355 * to defer releasing the mm reference until after we drop the
356 * struct_mutex, i.e. we need to schedule a worker to do the clean
357 * up.
358 */
359 mutex_lock(&dev_priv->mm_lock);
360 mm = __i915_mm_struct_find(dev_priv, current->mm);
361 if (mm == NULL) {
362 mm = kmalloc(sizeof(*mm), GFP_KERNEL);
363 if (mm == NULL) {
364 ret = -ENOMEM;
365 goto out;
366 }
367
368 kref_init(&mm->kref);
Chris Wilsonf470b192016-04-05 15:00:01 +0100369 mm->i915 = to_i915(obj->base.dev);
Chris Wilsonad46cb52014-08-07 14:20:40 +0100370
371 mm->mm = current->mm;
372 atomic_inc(&current->mm->mm_count);
373
374 mm->mn = NULL;
375
376 /* Protected by dev_priv->mm_lock */
377 hash_add(dev_priv->mm_structs,
378 &mm->node, (unsigned long)mm->mm);
379 } else
380 kref_get(&mm->kref);
381
382 obj->userptr.mm = mm;
383out:
384 mutex_unlock(&dev_priv->mm_lock);
385 return ret;
386}
387
388static void
389__i915_mm_struct_free__worker(struct work_struct *work)
390{
391 struct i915_mm_struct *mm = container_of(work, typeof(*mm), work);
392 i915_mmu_notifier_free(mm->mn, mm->mm);
393 mmdrop(mm->mm);
394 kfree(mm);
395}
396
397static void
398__i915_mm_struct_free(struct kref *kref)
399{
400 struct i915_mm_struct *mm = container_of(kref, typeof(*mm), kref);
401
402 /* Protected by dev_priv->mm_lock */
403 hash_del(&mm->node);
Chris Wilsonf470b192016-04-05 15:00:01 +0100404 mutex_unlock(&mm->i915->mm_lock);
Chris Wilsonad46cb52014-08-07 14:20:40 +0100405
406 INIT_WORK(&mm->work, __i915_mm_struct_free__worker);
407 schedule_work(&mm->work);
408}
409
410static void
411i915_gem_userptr_release__mm_struct(struct drm_i915_gem_object *obj)
412{
413 if (obj->userptr.mm == NULL)
414 return;
415
416 kref_put_mutex(&obj->userptr.mm->kref,
417 __i915_mm_struct_free,
418 &to_i915(obj->base.dev)->mm_lock);
419 obj->userptr.mm = NULL;
420}
421
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100422struct get_pages_work {
423 struct work_struct work;
424 struct drm_i915_gem_object *obj;
425 struct task_struct *task;
426};
427
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100428#if IS_ENABLED(CONFIG_SWIOTLB)
429#define swiotlb_active() swiotlb_nr_tbl()
430#else
431#define swiotlb_active() 0
432#endif
433
434static int
435st_set_pages(struct sg_table **st, struct page **pvec, int num_pages)
436{
437 struct scatterlist *sg;
438 int ret, n;
439
440 *st = kmalloc(sizeof(**st), GFP_KERNEL);
441 if (*st == NULL)
442 return -ENOMEM;
443
444 if (swiotlb_active()) {
445 ret = sg_alloc_table(*st, num_pages, GFP_KERNEL);
446 if (ret)
447 goto err;
448
449 for_each_sg((*st)->sgl, sg, num_pages, n)
450 sg_set_page(sg, pvec[n], PAGE_SIZE, 0);
451 } else {
452 ret = sg_alloc_table_from_pages(*st, pvec, num_pages,
453 0, num_pages << PAGE_SHIFT,
454 GFP_KERNEL);
455 if (ret)
456 goto err;
457 }
458
459 return 0;
460
461err:
462 kfree(*st);
463 *st = NULL;
464 return ret;
465}
466
Imre Deake2273302015-07-09 12:59:05 +0300467static int
468__i915_gem_userptr_set_pages(struct drm_i915_gem_object *obj,
469 struct page **pvec, int num_pages)
470{
471 int ret;
472
473 ret = st_set_pages(&obj->pages, pvec, num_pages);
474 if (ret)
475 return ret;
476
477 ret = i915_gem_gtt_prepare_object(obj);
478 if (ret) {
479 sg_free_table(obj->pages);
480 kfree(obj->pages);
481 obj->pages = NULL;
482 }
483
484 return ret;
485}
486
Chris Wilson380996a2015-10-01 12:34:47 +0100487static int
Chris Wilsone4b946b2015-10-01 12:34:46 +0100488__i915_gem_userptr_set_active(struct drm_i915_gem_object *obj,
489 bool value)
490{
Chris Wilson380996a2015-10-01 12:34:47 +0100491 int ret = 0;
492
Chris Wilsone4b946b2015-10-01 12:34:46 +0100493 /* During mm_invalidate_range we need to cancel any userptr that
494 * overlaps the range being invalidated. Doing so requires the
495 * struct_mutex, and that risks recursion. In order to cause
496 * recursion, the user must alias the userptr address space with
497 * a GTT mmapping (possible with a MAP_FIXED) - then when we have
498 * to invalidate that mmaping, mm_invalidate_range is called with
499 * the userptr address *and* the struct_mutex held. To prevent that
500 * we set a flag under the i915_mmu_notifier spinlock to indicate
501 * whether this object is valid.
502 */
503#if defined(CONFIG_MMU_NOTIFIER)
504 if (obj->userptr.mmu_object == NULL)
Chris Wilson380996a2015-10-01 12:34:47 +0100505 return 0;
Chris Wilsone4b946b2015-10-01 12:34:46 +0100506
507 spin_lock(&obj->userptr.mmu_object->mn->lock);
Chris Wilson380996a2015-10-01 12:34:47 +0100508 /* In order to serialise get_pages with an outstanding
509 * cancel_userptr, we must drop the struct_mutex and try again.
510 */
Chris Wilson768e1592016-01-21 17:32:43 +0000511 if (!value)
512 del_object(obj->userptr.mmu_object);
513 else if (!work_pending(&obj->userptr.mmu_object->work))
514 add_object(obj->userptr.mmu_object);
Chris Wilson380996a2015-10-01 12:34:47 +0100515 else
516 ret = -EAGAIN;
Chris Wilsone4b946b2015-10-01 12:34:46 +0100517 spin_unlock(&obj->userptr.mmu_object->mn->lock);
518#endif
Chris Wilson380996a2015-10-01 12:34:47 +0100519
520 return ret;
Chris Wilsone4b946b2015-10-01 12:34:46 +0100521}
522
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100523static void
524__i915_gem_userptr_get_pages_worker(struct work_struct *_work)
525{
526 struct get_pages_work *work = container_of(_work, typeof(*work), work);
527 struct drm_i915_gem_object *obj = work->obj;
528 struct drm_device *dev = obj->base.dev;
Chris Wilson68d6c842015-10-01 12:34:45 +0100529 const int npages = obj->base.size >> PAGE_SHIFT;
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100530 struct page **pvec;
531 int pinned, ret;
532
533 ret = -ENOMEM;
534 pinned = 0;
535
Chris Wilsonf2a85e12016-04-08 12:11:13 +0100536 pvec = drm_malloc_gfp(npages, sizeof(struct page *), GFP_TEMPORARY);
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100537 if (pvec != NULL) {
Chris Wilsonad46cb52014-08-07 14:20:40 +0100538 struct mm_struct *mm = obj->userptr.mm->mm;
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100539
Chris Wilson40313f02016-04-05 15:00:00 +0100540 ret = -EFAULT;
541 if (atomic_inc_not_zero(&mm->mm_users)) {
542 down_read(&mm->mmap_sem);
543 while (pinned < npages) {
544 ret = get_user_pages_remote
545 (work->task, mm,
546 obj->userptr.ptr + pinned * PAGE_SIZE,
547 npages - pinned,
548 !obj->userptr.read_only, 0,
549 pvec + pinned, NULL);
550 if (ret < 0)
551 break;
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100552
Chris Wilson40313f02016-04-05 15:00:00 +0100553 pinned += ret;
554 }
555 up_read(&mm->mmap_sem);
556 mmput(mm);
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100557 }
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100558 }
559
560 mutex_lock(&dev->struct_mutex);
Chris Wilson68d6c842015-10-01 12:34:45 +0100561 if (obj->userptr.work == &work->work) {
562 if (pinned == npages) {
563 ret = __i915_gem_userptr_set_pages(obj, pvec, npages);
564 if (ret == 0) {
565 list_add_tail(&obj->global_list,
566 &to_i915(dev)->mm.unbound_list);
567 obj->get_page.sg = obj->pages->sgl;
568 obj->get_page.last = 0;
569 pinned = 0;
570 }
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100571 }
Chris Wilson68d6c842015-10-01 12:34:45 +0100572 obj->userptr.work = ERR_PTR(ret);
Chris Wilsone4b946b2015-10-01 12:34:46 +0100573 if (ret)
574 __i915_gem_userptr_set_active(obj, false);
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100575 }
576
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100577 obj->userptr.workers--;
Chris Wilsonf8c417c2016-07-20 13:31:53 +0100578 i915_gem_object_put(obj);
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100579 mutex_unlock(&dev->struct_mutex);
580
581 release_pages(pvec, pinned, 0);
582 drm_free_large(pvec);
583
584 put_task_struct(work->task);
585 kfree(work);
586}
587
588static int
Chris Wilsone4b946b2015-10-01 12:34:46 +0100589__i915_gem_userptr_get_pages_schedule(struct drm_i915_gem_object *obj,
590 bool *active)
591{
592 struct get_pages_work *work;
593
594 /* Spawn a worker so that we can acquire the
595 * user pages without holding our mutex. Access
596 * to the user pages requires mmap_sem, and we have
597 * a strict lock ordering of mmap_sem, struct_mutex -
598 * we already hold struct_mutex here and so cannot
599 * call gup without encountering a lock inversion.
600 *
601 * Userspace will keep on repeating the operation
602 * (thanks to EAGAIN) until either we hit the fast
603 * path or the worker completes. If the worker is
604 * cancelled or superseded, the task is still run
605 * but the results ignored. (This leads to
606 * complications that we may have a stray object
607 * refcount that we need to be wary of when
608 * checking for existing objects during creation.)
609 * If the worker encounters an error, it reports
610 * that error back to this function through
611 * obj->userptr.work = ERR_PTR.
612 */
613 if (obj->userptr.workers >= I915_GEM_USERPTR_MAX_WORKERS)
614 return -EAGAIN;
615
616 work = kmalloc(sizeof(*work), GFP_KERNEL);
617 if (work == NULL)
618 return -ENOMEM;
619
620 obj->userptr.work = &work->work;
621 obj->userptr.workers++;
622
Chris Wilson25dc5562016-07-20 13:31:52 +0100623 work->obj = i915_gem_object_get(obj);
Chris Wilsone4b946b2015-10-01 12:34:46 +0100624
625 work->task = current;
626 get_task_struct(work->task);
627
628 INIT_WORK(&work->work, __i915_gem_userptr_get_pages_worker);
629 schedule_work(&work->work);
630
631 *active = true;
632 return -EAGAIN;
633}
634
635static int
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100636i915_gem_userptr_get_pages(struct drm_i915_gem_object *obj)
637{
638 const int num_pages = obj->base.size >> PAGE_SHIFT;
639 struct page **pvec;
640 int pinned, ret;
Chris Wilsone4b946b2015-10-01 12:34:46 +0100641 bool active;
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100642
643 /* If userspace should engineer that these pages are replaced in
644 * the vma between us binding this page into the GTT and completion
645 * of rendering... Their loss. If they change the mapping of their
646 * pages they need to create a new bo to point to the new vma.
647 *
648 * However, that still leaves open the possibility of the vma
649 * being copied upon fork. Which falls under the same userspace
650 * synchronisation issue as a regular bo, except that this time
651 * the process may not be expecting that a particular piece of
652 * memory is tied to the GPU.
653 *
654 * Fortunately, we can hook into the mmu_notifier in order to
655 * discard the page references prior to anything nasty happening
656 * to the vma (discard or cloning) which should prevent the more
657 * egregious cases from causing harm.
658 */
Chris Wilsone4b946b2015-10-01 12:34:46 +0100659 if (IS_ERR(obj->userptr.work)) {
660 /* active flag will have been dropped already by the worker */
661 ret = PTR_ERR(obj->userptr.work);
662 obj->userptr.work = NULL;
663 return ret;
664 }
665 if (obj->userptr.work)
666 /* active flag should still be held for the pending work */
667 return -EAGAIN;
668
669 /* Let the mmu-notifier know that we have begun and need cancellation */
Chris Wilson380996a2015-10-01 12:34:47 +0100670 ret = __i915_gem_userptr_set_active(obj, true);
671 if (ret)
672 return ret;
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100673
674 pvec = NULL;
675 pinned = 0;
Chris Wilsonad46cb52014-08-07 14:20:40 +0100676 if (obj->userptr.mm->mm == current->mm) {
Chris Wilsonf2a85e12016-04-08 12:11:13 +0100677 pvec = drm_malloc_gfp(num_pages, sizeof(struct page *),
678 GFP_TEMPORARY);
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100679 if (pvec == NULL) {
Chris Wilsonf2a85e12016-04-08 12:11:13 +0100680 __i915_gem_userptr_set_active(obj, false);
681 return -ENOMEM;
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100682 }
683
684 pinned = __get_user_pages_fast(obj->userptr.ptr, num_pages,
685 !obj->userptr.read_only, pvec);
686 }
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100687
Chris Wilsone4b946b2015-10-01 12:34:46 +0100688 active = false;
689 if (pinned < 0)
690 ret = pinned, pinned = 0;
691 else if (pinned < num_pages)
692 ret = __i915_gem_userptr_get_pages_schedule(obj, &active);
693 else
Imre Deake2273302015-07-09 12:59:05 +0300694 ret = __i915_gem_userptr_set_pages(obj, pvec, num_pages);
Chris Wilsone4b946b2015-10-01 12:34:46 +0100695 if (ret) {
696 __i915_gem_userptr_set_active(obj, active);
697 release_pages(pvec, pinned, 0);
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100698 }
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100699 drm_free_large(pvec);
700 return ret;
701}
702
703static void
704i915_gem_userptr_put_pages(struct drm_i915_gem_object *obj)
705{
Dave Gordon85d12252016-05-20 11:54:06 +0100706 struct sgt_iter sgt_iter;
707 struct page *page;
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100708
709 BUG_ON(obj->userptr.work != NULL);
Chris Wilsone4b946b2015-10-01 12:34:46 +0100710 __i915_gem_userptr_set_active(obj, false);
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100711
712 if (obj->madv != I915_MADV_WILLNEED)
713 obj->dirty = 0;
714
Imre Deake2273302015-07-09 12:59:05 +0300715 i915_gem_gtt_finish_object(obj);
716
Dave Gordon85d12252016-05-20 11:54:06 +0100717 for_each_sgt_page(page, sgt_iter, obj->pages) {
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100718 if (obj->dirty)
719 set_page_dirty(page);
720
721 mark_page_accessed(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300722 put_page(page);
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100723 }
724 obj->dirty = 0;
725
726 sg_free_table(obj->pages);
727 kfree(obj->pages);
728}
729
730static void
731i915_gem_userptr_release(struct drm_i915_gem_object *obj)
732{
733 i915_gem_userptr_release__mmu_notifier(obj);
Chris Wilsonad46cb52014-08-07 14:20:40 +0100734 i915_gem_userptr_release__mm_struct(obj);
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100735}
736
737static int
738i915_gem_userptr_dmabuf_export(struct drm_i915_gem_object *obj)
739{
Chris Wilsonad46cb52014-08-07 14:20:40 +0100740 if (obj->userptr.mmu_object)
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100741 return 0;
742
743 return i915_gem_userptr_init__mmu_notifier(obj, 0);
744}
745
746static const struct drm_i915_gem_object_ops i915_gem_userptr_ops = {
Chris Wilsonde472662016-01-22 18:32:31 +0000747 .flags = I915_GEM_OBJECT_HAS_STRUCT_PAGE,
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100748 .get_pages = i915_gem_userptr_get_pages,
749 .put_pages = i915_gem_userptr_put_pages,
Chris Wilsonde472662016-01-22 18:32:31 +0000750 .dmabuf_export = i915_gem_userptr_dmabuf_export,
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100751 .release = i915_gem_userptr_release,
752};
753
754/**
755 * Creates a new mm object that wraps some normal memory from the process
756 * context - user memory.
757 *
758 * We impose several restrictions upon the memory being mapped
759 * into the GPU.
760 * 1. It must be page aligned (both start/end addresses, i.e ptr and size).
Chris Wilsonec8b0dd2014-07-21 13:21:23 +0100761 * 2. It must be normal system memory, not a pointer into another map of IO
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100762 * space (e.g. it must not be a GTT mmapping of another object).
Chris Wilsonec8b0dd2014-07-21 13:21:23 +0100763 * 3. We only allow a bo as large as we could in theory map into the GTT,
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100764 * that is we limit the size to the total size of the GTT.
Chris Wilsonec8b0dd2014-07-21 13:21:23 +0100765 * 4. The bo is marked as being snoopable. The backing pages are left
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100766 * accessible directly by the CPU, but reads and writes by the GPU may
767 * incur the cost of a snoop (unless you have an LLC architecture).
768 *
769 * Synchronisation between multiple users and the GPU is left to userspace
770 * through the normal set-domain-ioctl. The kernel will enforce that the
771 * GPU relinquishes the VMA before it is returned back to the system
772 * i.e. upon free(), munmap() or process termination. However, the userspace
773 * malloc() library may not immediately relinquish the VMA after free() and
774 * instead reuse it whilst the GPU is still reading and writing to the VMA.
775 * Caveat emptor.
776 *
777 * Also note, that the object created here is not currently a "first class"
778 * object, in that several ioctls are banned. These are the CPU access
779 * ioctls: mmap(), pwrite and pread. In practice, you are expected to use
Chris Wilsoncc917ab2015-10-13 14:22:26 +0100780 * direct access via your pointer rather than use those ioctls. Another
781 * restriction is that we do not allow userptr surfaces to be pinned to the
782 * hardware and so we reject any attempt to create a framebuffer out of a
783 * userptr.
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100784 *
785 * If you think this is a good interface to use to pass GPU memory between
786 * drivers, please use dma-buf instead. In fact, wherever possible use
787 * dma-buf instead.
788 */
789int
790i915_gem_userptr_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
791{
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100792 struct drm_i915_gem_userptr *args = data;
793 struct drm_i915_gem_object *obj;
794 int ret;
795 u32 handle;
796
Tvrtko Ursulinca377802016-03-02 12:10:31 +0000797 if (!HAS_LLC(dev) && !HAS_SNOOP(dev)) {
798 /* We cannot support coherent userptr objects on hw without
799 * LLC and broken snooping.
800 */
801 return -ENODEV;
802 }
803
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100804 if (args->flags & ~(I915_USERPTR_READ_ONLY |
805 I915_USERPTR_UNSYNCHRONIZED))
806 return -EINVAL;
807
808 if (offset_in_page(args->user_ptr | args->user_size))
809 return -EINVAL;
810
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100811 if (!access_ok(args->flags & I915_USERPTR_READ_ONLY ? VERIFY_READ : VERIFY_WRITE,
812 (char __user *)(unsigned long)args->user_ptr, args->user_size))
813 return -EFAULT;
814
815 if (args->flags & I915_USERPTR_READ_ONLY) {
816 /* On almost all of the current hw, we cannot tell the GPU that a
817 * page is readonly, so this is just a placeholder in the uAPI.
818 */
819 return -ENODEV;
820 }
821
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100822 obj = i915_gem_object_alloc(dev);
823 if (obj == NULL)
824 return -ENOMEM;
825
826 drm_gem_private_object_init(dev, &obj->base, args->user_size);
827 i915_gem_object_init(obj, &i915_gem_userptr_ops);
828 obj->cache_level = I915_CACHE_LLC;
829 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
830 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
831
832 obj->userptr.ptr = args->user_ptr;
833 obj->userptr.read_only = !!(args->flags & I915_USERPTR_READ_ONLY);
834
835 /* And keep a pointer to the current->mm for resolving the user pages
836 * at binding. This means that we need to hook into the mmu_notifier
837 * in order to detect if the mmu is destroyed.
838 */
Chris Wilsonad46cb52014-08-07 14:20:40 +0100839 ret = i915_gem_userptr_init__mm_struct(obj);
840 if (ret == 0)
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100841 ret = i915_gem_userptr_init__mmu_notifier(obj, args->flags);
842 if (ret == 0)
843 ret = drm_gem_handle_create(file, &obj->base, &handle);
844
845 /* drop reference from allocate - handle holds it now */
Chris Wilson34911fd2016-07-20 13:31:54 +0100846 i915_gem_object_put_unlocked(obj);
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100847 if (ret)
848 return ret;
849
850 args->handle = handle;
851 return 0;
852}
853
Chris Wilson72778cb2016-05-19 16:17:16 +0100854void i915_gem_init_userptr(struct drm_i915_private *dev_priv)
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100855{
Chris Wilsonad46cb52014-08-07 14:20:40 +0100856 mutex_init(&dev_priv->mm_lock);
857 hash_init(dev_priv->mm_structs);
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100858}