Chris Wilson | 5cc9ed4 | 2014-05-16 14:22:37 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2012-2014 Intel Corporation |
| 3 | * |
| 4 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 5 | * copy of this software and associated documentation files (the "Software"), |
| 6 | * to deal in the Software without restriction, including without limitation |
| 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 8 | * and/or sell copies of the Software, and to permit persons to whom the |
| 9 | * Software is furnished to do so, subject to the following conditions: |
| 10 | * |
| 11 | * The above copyright notice and this permission notice (including the next |
| 12 | * paragraph) shall be included in all copies or substantial portions of the |
| 13 | * Software. |
| 14 | * |
| 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 18 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 20 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 21 | * IN THE SOFTWARE. |
| 22 | * |
| 23 | */ |
| 24 | |
Maarten Lankhorst | b588c92 | 2015-05-13 09:56:00 +0200 | [diff] [blame] | 25 | #include <drm/drmP.h> |
| 26 | #include <drm/i915_drm.h> |
Chris Wilson | 5cc9ed4 | 2014-05-16 14:22:37 +0100 | [diff] [blame] | 27 | #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 Wilson | ad46cb5 | 2014-08-07 14:20:40 +0100 | [diff] [blame] | 35 | struct i915_mm_struct { |
| 36 | struct mm_struct *mm; |
Chris Wilson | f470b19 | 2016-04-05 15:00:01 +0100 | [diff] [blame] | 37 | struct drm_i915_private *i915; |
Chris Wilson | ad46cb5 | 2014-08-07 14:20:40 +0100 | [diff] [blame] | 38 | struct i915_mmu_notifier *mn; |
| 39 | struct hlist_node node; |
| 40 | struct kref kref; |
| 41 | struct work_struct work; |
| 42 | }; |
| 43 | |
Chris Wilson | 5cc9ed4 | 2014-05-16 14:22:37 +0100 | [diff] [blame] | 44 | #if defined(CONFIG_MMU_NOTIFIER) |
| 45 | #include <linux/interval_tree.h> |
| 46 | |
| 47 | struct i915_mmu_notifier { |
| 48 | spinlock_t lock; |
| 49 | struct hlist_node node; |
| 50 | struct mmu_notifier mn; |
| 51 | struct rb_root objects; |
Chris Wilson | 393afc2 | 2016-04-05 14:59:59 +0100 | [diff] [blame] | 52 | struct workqueue_struct *wq; |
Chris Wilson | 5cc9ed4 | 2014-05-16 14:22:37 +0100 | [diff] [blame] | 53 | }; |
| 54 | |
| 55 | struct i915_mmu_object { |
Chris Wilson | ad46cb5 | 2014-08-07 14:20:40 +0100 | [diff] [blame] | 56 | struct i915_mmu_notifier *mn; |
Chris Wilson | 768e159 | 2016-01-21 17:32:43 +0000 | [diff] [blame] | 57 | struct drm_i915_gem_object *obj; |
Chris Wilson | 5cc9ed4 | 2014-05-16 14:22:37 +0100 | [diff] [blame] | 58 | struct interval_tree_node it; |
Chris Wilson | ec8b0dd | 2014-07-21 13:21:23 +0100 | [diff] [blame] | 59 | struct list_head link; |
Chris Wilson | 380996a | 2015-10-01 12:34:47 +0100 | [diff] [blame] | 60 | struct work_struct work; |
Chris Wilson | 768e159 | 2016-01-21 17:32:43 +0000 | [diff] [blame] | 61 | bool attached; |
Chris Wilson | 5cc9ed4 | 2014-05-16 14:22:37 +0100 | [diff] [blame] | 62 | }; |
| 63 | |
Chris Wilson | 393afc2 | 2016-04-05 14:59:59 +0100 | [diff] [blame] | 64 | static 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 Wilson | 393afc2 | 2016-04-05 14:59:59 +0100 | [diff] [blame] | 68 | int i, n; |
| 69 | |
| 70 | if (!obj->active) |
| 71 | return; |
| 72 | |
| 73 | n = 0; |
| 74 | for (i = 0; i < I915_NUM_ENGINES; i++) { |
| 75 | struct drm_i915_gem_request *req; |
| 76 | |
| 77 | req = obj->last_read_req[i]; |
| 78 | if (req == NULL) |
| 79 | continue; |
| 80 | |
| 81 | requests[n++] = i915_gem_request_reference(req); |
| 82 | } |
| 83 | |
Chris Wilson | 393afc2 | 2016-04-05 14:59:59 +0100 | [diff] [blame] | 84 | mutex_unlock(&dev->struct_mutex); |
| 85 | |
| 86 | for (i = 0; i < n; i++) |
Chris Wilson | 299259a | 2016-04-13 17:35:06 +0100 | [diff] [blame] | 87 | __i915_wait_request(requests[i], false, NULL, NULL); |
Chris Wilson | 393afc2 | 2016-04-05 14:59:59 +0100 | [diff] [blame] | 88 | |
| 89 | mutex_lock(&dev->struct_mutex); |
| 90 | |
| 91 | for (i = 0; i < n; i++) |
| 92 | i915_gem_request_unreference(requests[i]); |
| 93 | } |
| 94 | |
Chris Wilson | 768e159 | 2016-01-21 17:32:43 +0000 | [diff] [blame] | 95 | static void cancel_userptr(struct work_struct *work) |
Chris Wilson | ec8b0dd | 2014-07-21 13:21:23 +0100 | [diff] [blame] | 96 | { |
Chris Wilson | 380996a | 2015-10-01 12:34:47 +0100 | [diff] [blame] | 97 | struct i915_mmu_object *mo = container_of(work, typeof(*mo), work); |
| 98 | struct drm_i915_gem_object *obj = mo->obj; |
Chris Wilson | ec8b0dd | 2014-07-21 13:21:23 +0100 | [diff] [blame] | 99 | struct drm_device *dev = obj->base.dev; |
Chris Wilson | ec8b0dd | 2014-07-21 13:21:23 +0100 | [diff] [blame] | 100 | |
| 101 | mutex_lock(&dev->struct_mutex); |
| 102 | /* Cancel any active worker and force us to re-evaluate gup */ |
| 103 | obj->userptr.work = NULL; |
| 104 | |
| 105 | if (obj->pages != NULL) { |
| 106 | struct drm_i915_private *dev_priv = to_i915(dev); |
| 107 | struct i915_vma *vma, *tmp; |
| 108 | bool was_interruptible; |
| 109 | |
Chris Wilson | 393afc2 | 2016-04-05 14:59:59 +0100 | [diff] [blame] | 110 | wait_rendering(obj); |
| 111 | |
Chris Wilson | ec8b0dd | 2014-07-21 13:21:23 +0100 | [diff] [blame] | 112 | was_interruptible = dev_priv->mm.interruptible; |
| 113 | dev_priv->mm.interruptible = false; |
| 114 | |
Chris Wilson | 1c7f4bc | 2016-02-26 11:03:19 +0000 | [diff] [blame] | 115 | list_for_each_entry_safe(vma, tmp, &obj->vma_list, obj_link) { |
Chris Wilson | ec8b0dd | 2014-07-21 13:21:23 +0100 | [diff] [blame] | 116 | int ret = i915_vma_unbind(vma); |
| 117 | WARN_ON(ret && ret != -EIO); |
| 118 | } |
| 119 | WARN_ON(i915_gem_object_put_pages(obj)); |
| 120 | |
| 121 | dev_priv->mm.interruptible = was_interruptible; |
| 122 | } |
| 123 | |
Chris Wilson | ec8b0dd | 2014-07-21 13:21:23 +0100 | [diff] [blame] | 124 | drm_gem_object_unreference(&obj->base); |
| 125 | mutex_unlock(&dev->struct_mutex); |
Chris Wilson | ec8b0dd | 2014-07-21 13:21:23 +0100 | [diff] [blame] | 126 | } |
| 127 | |
Chris Wilson | 768e159 | 2016-01-21 17:32:43 +0000 | [diff] [blame] | 128 | static void add_object(struct i915_mmu_object *mo) |
Chris Wilson | ec8b0dd | 2014-07-21 13:21:23 +0100 | [diff] [blame] | 129 | { |
Chris Wilson | 768e159 | 2016-01-21 17:32:43 +0000 | [diff] [blame] | 130 | if (mo->attached) |
| 131 | return; |
Chris Wilson | ec8b0dd | 2014-07-21 13:21:23 +0100 | [diff] [blame] | 132 | |
Chris Wilson | 768e159 | 2016-01-21 17:32:43 +0000 | [diff] [blame] | 133 | interval_tree_insert(&mo->it, &mo->mn->objects); |
| 134 | mo->attached = true; |
| 135 | } |
Chris Wilson | ec8b0dd | 2014-07-21 13:21:23 +0100 | [diff] [blame] | 136 | |
Chris Wilson | 768e159 | 2016-01-21 17:32:43 +0000 | [diff] [blame] | 137 | static void del_object(struct i915_mmu_object *mo) |
| 138 | { |
| 139 | if (!mo->attached) |
| 140 | return; |
| 141 | |
| 142 | interval_tree_remove(&mo->it, &mo->mn->objects); |
| 143 | mo->attached = false; |
Chris Wilson | ec8b0dd | 2014-07-21 13:21:23 +0100 | [diff] [blame] | 144 | } |
| 145 | |
Chris Wilson | 5cc9ed4 | 2014-05-16 14:22:37 +0100 | [diff] [blame] | 146 | static void i915_gem_userptr_mn_invalidate_range_start(struct mmu_notifier *_mn, |
| 147 | struct mm_struct *mm, |
| 148 | unsigned long start, |
| 149 | unsigned long end) |
| 150 | { |
Chris Wilson | 380996a | 2015-10-01 12:34:47 +0100 | [diff] [blame] | 151 | struct i915_mmu_notifier *mn = |
| 152 | container_of(_mn, struct i915_mmu_notifier, mn); |
| 153 | struct i915_mmu_object *mo; |
Chris Wilson | 768e159 | 2016-01-21 17:32:43 +0000 | [diff] [blame] | 154 | struct interval_tree_node *it; |
| 155 | LIST_HEAD(cancelled); |
| 156 | |
| 157 | if (RB_EMPTY_ROOT(&mn->objects)) |
| 158 | return; |
Chris Wilson | 5cc9ed4 | 2014-05-16 14:22:37 +0100 | [diff] [blame] | 159 | |
Chris Wilson | 380996a | 2015-10-01 12:34:47 +0100 | [diff] [blame] | 160 | /* interval ranges are inclusive, but invalidate range is exclusive */ |
| 161 | end--; |
Chris Wilson | 5cc9ed4 | 2014-05-16 14:22:37 +0100 | [diff] [blame] | 162 | |
Chris Wilson | 380996a | 2015-10-01 12:34:47 +0100 | [diff] [blame] | 163 | spin_lock(&mn->lock); |
Chris Wilson | 768e159 | 2016-01-21 17:32:43 +0000 | [diff] [blame] | 164 | it = interval_tree_iter_first(&mn->objects, start, end); |
| 165 | while (it) { |
| 166 | /* The mmu_object is released late when destroying the |
| 167 | * GEM object so it is entirely possible to gain a |
| 168 | * reference on an object in the process of being freed |
| 169 | * since our serialisation is via the spinlock and not |
| 170 | * the struct_mutex - and consequently use it after it |
| 171 | * is freed and then double free it. To prevent that |
| 172 | * use-after-free we only acquire a reference on the |
| 173 | * object if it is not in the process of being destroyed. |
| 174 | */ |
| 175 | mo = container_of(it, struct i915_mmu_object, it); |
| 176 | if (kref_get_unless_zero(&mo->obj->base.refcount)) |
Chris Wilson | 393afc2 | 2016-04-05 14:59:59 +0100 | [diff] [blame] | 177 | queue_work(mn->wq, &mo->work); |
Michał Winiarski | 460822b | 2015-02-03 15:48:17 +0100 | [diff] [blame] | 178 | |
Chris Wilson | 768e159 | 2016-01-21 17:32:43 +0000 | [diff] [blame] | 179 | list_add(&mo->link, &cancelled); |
| 180 | it = interval_tree_iter_next(it, start, end); |
Chris Wilson | 5cc9ed4 | 2014-05-16 14:22:37 +0100 | [diff] [blame] | 181 | } |
Chris Wilson | 768e159 | 2016-01-21 17:32:43 +0000 | [diff] [blame] | 182 | list_for_each_entry(mo, &cancelled, link) |
| 183 | del_object(mo); |
Chris Wilson | 380996a | 2015-10-01 12:34:47 +0100 | [diff] [blame] | 184 | spin_unlock(&mn->lock); |
Chris Wilson | 393afc2 | 2016-04-05 14:59:59 +0100 | [diff] [blame] | 185 | |
| 186 | flush_workqueue(mn->wq); |
Chris Wilson | 5cc9ed4 | 2014-05-16 14:22:37 +0100 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | static const struct mmu_notifier_ops i915_gem_userptr_notifier = { |
| 190 | .invalidate_range_start = i915_gem_userptr_mn_invalidate_range_start, |
| 191 | }; |
| 192 | |
| 193 | static struct i915_mmu_notifier * |
Chris Wilson | ad46cb5 | 2014-08-07 14:20:40 +0100 | [diff] [blame] | 194 | i915_mmu_notifier_create(struct mm_struct *mm) |
Chris Wilson | 5cc9ed4 | 2014-05-16 14:22:37 +0100 | [diff] [blame] | 195 | { |
Chris Wilson | ad46cb5 | 2014-08-07 14:20:40 +0100 | [diff] [blame] | 196 | struct i915_mmu_notifier *mn; |
Chris Wilson | 5cc9ed4 | 2014-05-16 14:22:37 +0100 | [diff] [blame] | 197 | int ret; |
| 198 | |
Chris Wilson | ad46cb5 | 2014-08-07 14:20:40 +0100 | [diff] [blame] | 199 | mn = kmalloc(sizeof(*mn), GFP_KERNEL); |
| 200 | if (mn == NULL) |
Chris Wilson | 5cc9ed4 | 2014-05-16 14:22:37 +0100 | [diff] [blame] | 201 | return ERR_PTR(-ENOMEM); |
| 202 | |
Chris Wilson | ad46cb5 | 2014-08-07 14:20:40 +0100 | [diff] [blame] | 203 | spin_lock_init(&mn->lock); |
| 204 | mn->mn.ops = &i915_gem_userptr_notifier; |
| 205 | mn->objects = RB_ROOT; |
Chris Wilson | 393afc2 | 2016-04-05 14:59:59 +0100 | [diff] [blame] | 206 | mn->wq = alloc_workqueue("i915-userptr-release", WQ_UNBOUND, 0); |
| 207 | if (mn->wq == NULL) { |
| 208 | kfree(mn); |
| 209 | return ERR_PTR(-ENOMEM); |
| 210 | } |
Chris Wilson | 5cc9ed4 | 2014-05-16 14:22:37 +0100 | [diff] [blame] | 211 | |
Chris Wilson | ad46cb5 | 2014-08-07 14:20:40 +0100 | [diff] [blame] | 212 | /* Protected by mmap_sem (write-lock) */ |
| 213 | ret = __mmu_notifier_register(&mn->mn, mm); |
Chris Wilson | 5cc9ed4 | 2014-05-16 14:22:37 +0100 | [diff] [blame] | 214 | if (ret) { |
Chris Wilson | 393afc2 | 2016-04-05 14:59:59 +0100 | [diff] [blame] | 215 | destroy_workqueue(mn->wq); |
Chris Wilson | ad46cb5 | 2014-08-07 14:20:40 +0100 | [diff] [blame] | 216 | kfree(mn); |
Chris Wilson | 5cc9ed4 | 2014-05-16 14:22:37 +0100 | [diff] [blame] | 217 | return ERR_PTR(ret); |
| 218 | } |
| 219 | |
Chris Wilson | ad46cb5 | 2014-08-07 14:20:40 +0100 | [diff] [blame] | 220 | return mn; |
Chris Wilson | 5cc9ed4 | 2014-05-16 14:22:37 +0100 | [diff] [blame] | 221 | } |
| 222 | |
Chris Wilson | 5cc9ed4 | 2014-05-16 14:22:37 +0100 | [diff] [blame] | 223 | static void |
| 224 | i915_gem_userptr_release__mmu_notifier(struct drm_i915_gem_object *obj) |
| 225 | { |
Chris Wilson | ad46cb5 | 2014-08-07 14:20:40 +0100 | [diff] [blame] | 226 | struct i915_mmu_object *mo; |
Chris Wilson | 5cc9ed4 | 2014-05-16 14:22:37 +0100 | [diff] [blame] | 227 | |
Chris Wilson | ad46cb5 | 2014-08-07 14:20:40 +0100 | [diff] [blame] | 228 | mo = obj->userptr.mmu_object; |
| 229 | if (mo == NULL) |
Chris Wilson | 5cc9ed4 | 2014-05-16 14:22:37 +0100 | [diff] [blame] | 230 | return; |
| 231 | |
Chris Wilson | 768e159 | 2016-01-21 17:32:43 +0000 | [diff] [blame] | 232 | spin_lock(&mo->mn->lock); |
| 233 | del_object(mo); |
| 234 | spin_unlock(&mo->mn->lock); |
Chris Wilson | ad46cb5 | 2014-08-07 14:20:40 +0100 | [diff] [blame] | 235 | kfree(mo); |
| 236 | |
| 237 | obj->userptr.mmu_object = NULL; |
| 238 | } |
| 239 | |
| 240 | static struct i915_mmu_notifier * |
| 241 | i915_mmu_notifier_find(struct i915_mm_struct *mm) |
| 242 | { |
Chris Wilson | e968136 | 2014-09-26 10:31:02 +0100 | [diff] [blame] | 243 | struct i915_mmu_notifier *mn = mm->mn; |
| 244 | |
| 245 | mn = mm->mn; |
| 246 | if (mn) |
| 247 | return mn; |
| 248 | |
| 249 | down_write(&mm->mm->mmap_sem); |
Chris Wilson | f470b19 | 2016-04-05 15:00:01 +0100 | [diff] [blame] | 250 | mutex_lock(&mm->i915->mm_lock); |
Chris Wilson | e968136 | 2014-09-26 10:31:02 +0100 | [diff] [blame] | 251 | if ((mn = mm->mn) == NULL) { |
| 252 | mn = i915_mmu_notifier_create(mm->mm); |
| 253 | if (!IS_ERR(mn)) |
| 254 | mm->mn = mn; |
Chris Wilson | ad46cb5 | 2014-08-07 14:20:40 +0100 | [diff] [blame] | 255 | } |
Chris Wilson | f470b19 | 2016-04-05 15:00:01 +0100 | [diff] [blame] | 256 | mutex_unlock(&mm->i915->mm_lock); |
Chris Wilson | e968136 | 2014-09-26 10:31:02 +0100 | [diff] [blame] | 257 | up_write(&mm->mm->mmap_sem); |
| 258 | |
| 259 | return mn; |
Chris Wilson | 5cc9ed4 | 2014-05-16 14:22:37 +0100 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | static int |
| 263 | i915_gem_userptr_init__mmu_notifier(struct drm_i915_gem_object *obj, |
| 264 | unsigned flags) |
| 265 | { |
Chris Wilson | ad46cb5 | 2014-08-07 14:20:40 +0100 | [diff] [blame] | 266 | struct i915_mmu_notifier *mn; |
| 267 | struct i915_mmu_object *mo; |
Chris Wilson | 5cc9ed4 | 2014-05-16 14:22:37 +0100 | [diff] [blame] | 268 | |
| 269 | if (flags & I915_USERPTR_UNSYNCHRONIZED) |
| 270 | return capable(CAP_SYS_ADMIN) ? 0 : -EPERM; |
| 271 | |
Chris Wilson | ad46cb5 | 2014-08-07 14:20:40 +0100 | [diff] [blame] | 272 | if (WARN_ON(obj->userptr.mm == NULL)) |
| 273 | return -EINVAL; |
| 274 | |
| 275 | mn = i915_mmu_notifier_find(obj->userptr.mm); |
| 276 | if (IS_ERR(mn)) |
| 277 | return PTR_ERR(mn); |
| 278 | |
| 279 | mo = kzalloc(sizeof(*mo), GFP_KERNEL); |
| 280 | if (mo == NULL) |
| 281 | return -ENOMEM; |
| 282 | |
| 283 | mo->mn = mn; |
Chris Wilson | ad46cb5 | 2014-08-07 14:20:40 +0100 | [diff] [blame] | 284 | mo->obj = obj; |
Chris Wilson | 768e159 | 2016-01-21 17:32:43 +0000 | [diff] [blame] | 285 | mo->it.start = obj->userptr.ptr; |
| 286 | mo->it.last = obj->userptr.ptr + obj->base.size - 1; |
| 287 | INIT_WORK(&mo->work, cancel_userptr); |
Chris Wilson | 5cc9ed4 | 2014-05-16 14:22:37 +0100 | [diff] [blame] | 288 | |
Chris Wilson | ad46cb5 | 2014-08-07 14:20:40 +0100 | [diff] [blame] | 289 | obj->userptr.mmu_object = mo; |
Chris Wilson | 5cc9ed4 | 2014-05-16 14:22:37 +0100 | [diff] [blame] | 290 | return 0; |
Chris Wilson | ad46cb5 | 2014-08-07 14:20:40 +0100 | [diff] [blame] | 291 | } |
Chris Wilson | 5cc9ed4 | 2014-05-16 14:22:37 +0100 | [diff] [blame] | 292 | |
Chris Wilson | ad46cb5 | 2014-08-07 14:20:40 +0100 | [diff] [blame] | 293 | static void |
| 294 | i915_mmu_notifier_free(struct i915_mmu_notifier *mn, |
| 295 | struct mm_struct *mm) |
| 296 | { |
| 297 | if (mn == NULL) |
| 298 | return; |
| 299 | |
| 300 | mmu_notifier_unregister(&mn->mn, mm); |
Chris Wilson | 393afc2 | 2016-04-05 14:59:59 +0100 | [diff] [blame] | 301 | destroy_workqueue(mn->wq); |
Chris Wilson | 5cc9ed4 | 2014-05-16 14:22:37 +0100 | [diff] [blame] | 302 | kfree(mn); |
Chris Wilson | 5cc9ed4 | 2014-05-16 14:22:37 +0100 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | #else |
| 306 | |
| 307 | static void |
| 308 | i915_gem_userptr_release__mmu_notifier(struct drm_i915_gem_object *obj) |
| 309 | { |
| 310 | } |
| 311 | |
| 312 | static int |
| 313 | i915_gem_userptr_init__mmu_notifier(struct drm_i915_gem_object *obj, |
| 314 | unsigned flags) |
| 315 | { |
| 316 | if ((flags & I915_USERPTR_UNSYNCHRONIZED) == 0) |
| 317 | return -ENODEV; |
| 318 | |
| 319 | if (!capable(CAP_SYS_ADMIN)) |
| 320 | return -EPERM; |
| 321 | |
| 322 | return 0; |
| 323 | } |
Chris Wilson | ad46cb5 | 2014-08-07 14:20:40 +0100 | [diff] [blame] | 324 | |
| 325 | static void |
| 326 | i915_mmu_notifier_free(struct i915_mmu_notifier *mn, |
| 327 | struct mm_struct *mm) |
| 328 | { |
| 329 | } |
| 330 | |
Chris Wilson | 5cc9ed4 | 2014-05-16 14:22:37 +0100 | [diff] [blame] | 331 | #endif |
| 332 | |
Chris Wilson | ad46cb5 | 2014-08-07 14:20:40 +0100 | [diff] [blame] | 333 | static struct i915_mm_struct * |
| 334 | __i915_mm_struct_find(struct drm_i915_private *dev_priv, struct mm_struct *real) |
| 335 | { |
| 336 | struct i915_mm_struct *mm; |
| 337 | |
| 338 | /* Protected by dev_priv->mm_lock */ |
| 339 | hash_for_each_possible(dev_priv->mm_structs, mm, node, (unsigned long)real) |
| 340 | if (mm->mm == real) |
| 341 | return mm; |
| 342 | |
| 343 | return NULL; |
| 344 | } |
| 345 | |
| 346 | static int |
| 347 | i915_gem_userptr_init__mm_struct(struct drm_i915_gem_object *obj) |
| 348 | { |
| 349 | struct drm_i915_private *dev_priv = to_i915(obj->base.dev); |
| 350 | struct i915_mm_struct *mm; |
| 351 | int ret = 0; |
| 352 | |
| 353 | /* During release of the GEM object we hold the struct_mutex. This |
| 354 | * precludes us from calling mmput() at that time as that may be |
| 355 | * the last reference and so call exit_mmap(). exit_mmap() will |
| 356 | * attempt to reap the vma, and if we were holding a GTT mmap |
| 357 | * would then call drm_gem_vm_close() and attempt to reacquire |
| 358 | * the struct mutex. So in order to avoid that recursion, we have |
| 359 | * to defer releasing the mm reference until after we drop the |
| 360 | * struct_mutex, i.e. we need to schedule a worker to do the clean |
| 361 | * up. |
| 362 | */ |
| 363 | mutex_lock(&dev_priv->mm_lock); |
| 364 | mm = __i915_mm_struct_find(dev_priv, current->mm); |
| 365 | if (mm == NULL) { |
| 366 | mm = kmalloc(sizeof(*mm), GFP_KERNEL); |
| 367 | if (mm == NULL) { |
| 368 | ret = -ENOMEM; |
| 369 | goto out; |
| 370 | } |
| 371 | |
| 372 | kref_init(&mm->kref); |
Chris Wilson | f470b19 | 2016-04-05 15:00:01 +0100 | [diff] [blame] | 373 | mm->i915 = to_i915(obj->base.dev); |
Chris Wilson | ad46cb5 | 2014-08-07 14:20:40 +0100 | [diff] [blame] | 374 | |
| 375 | mm->mm = current->mm; |
| 376 | atomic_inc(¤t->mm->mm_count); |
| 377 | |
| 378 | mm->mn = NULL; |
| 379 | |
| 380 | /* Protected by dev_priv->mm_lock */ |
| 381 | hash_add(dev_priv->mm_structs, |
| 382 | &mm->node, (unsigned long)mm->mm); |
| 383 | } else |
| 384 | kref_get(&mm->kref); |
| 385 | |
| 386 | obj->userptr.mm = mm; |
| 387 | out: |
| 388 | mutex_unlock(&dev_priv->mm_lock); |
| 389 | return ret; |
| 390 | } |
| 391 | |
| 392 | static void |
| 393 | __i915_mm_struct_free__worker(struct work_struct *work) |
| 394 | { |
| 395 | struct i915_mm_struct *mm = container_of(work, typeof(*mm), work); |
| 396 | i915_mmu_notifier_free(mm->mn, mm->mm); |
| 397 | mmdrop(mm->mm); |
| 398 | kfree(mm); |
| 399 | } |
| 400 | |
| 401 | static void |
| 402 | __i915_mm_struct_free(struct kref *kref) |
| 403 | { |
| 404 | struct i915_mm_struct *mm = container_of(kref, typeof(*mm), kref); |
| 405 | |
| 406 | /* Protected by dev_priv->mm_lock */ |
| 407 | hash_del(&mm->node); |
Chris Wilson | f470b19 | 2016-04-05 15:00:01 +0100 | [diff] [blame] | 408 | mutex_unlock(&mm->i915->mm_lock); |
Chris Wilson | ad46cb5 | 2014-08-07 14:20:40 +0100 | [diff] [blame] | 409 | |
| 410 | INIT_WORK(&mm->work, __i915_mm_struct_free__worker); |
| 411 | schedule_work(&mm->work); |
| 412 | } |
| 413 | |
| 414 | static void |
| 415 | i915_gem_userptr_release__mm_struct(struct drm_i915_gem_object *obj) |
| 416 | { |
| 417 | if (obj->userptr.mm == NULL) |
| 418 | return; |
| 419 | |
| 420 | kref_put_mutex(&obj->userptr.mm->kref, |
| 421 | __i915_mm_struct_free, |
| 422 | &to_i915(obj->base.dev)->mm_lock); |
| 423 | obj->userptr.mm = NULL; |
| 424 | } |
| 425 | |
Chris Wilson | 5cc9ed4 | 2014-05-16 14:22:37 +0100 | [diff] [blame] | 426 | struct get_pages_work { |
| 427 | struct work_struct work; |
| 428 | struct drm_i915_gem_object *obj; |
| 429 | struct task_struct *task; |
| 430 | }; |
| 431 | |
Chris Wilson | 5cc9ed4 | 2014-05-16 14:22:37 +0100 | [diff] [blame] | 432 | #if IS_ENABLED(CONFIG_SWIOTLB) |
| 433 | #define swiotlb_active() swiotlb_nr_tbl() |
| 434 | #else |
| 435 | #define swiotlb_active() 0 |
| 436 | #endif |
| 437 | |
| 438 | static int |
| 439 | st_set_pages(struct sg_table **st, struct page **pvec, int num_pages) |
| 440 | { |
| 441 | struct scatterlist *sg; |
| 442 | int ret, n; |
| 443 | |
| 444 | *st = kmalloc(sizeof(**st), GFP_KERNEL); |
| 445 | if (*st == NULL) |
| 446 | return -ENOMEM; |
| 447 | |
| 448 | if (swiotlb_active()) { |
| 449 | ret = sg_alloc_table(*st, num_pages, GFP_KERNEL); |
| 450 | if (ret) |
| 451 | goto err; |
| 452 | |
| 453 | for_each_sg((*st)->sgl, sg, num_pages, n) |
| 454 | sg_set_page(sg, pvec[n], PAGE_SIZE, 0); |
| 455 | } else { |
| 456 | ret = sg_alloc_table_from_pages(*st, pvec, num_pages, |
| 457 | 0, num_pages << PAGE_SHIFT, |
| 458 | GFP_KERNEL); |
| 459 | if (ret) |
| 460 | goto err; |
| 461 | } |
| 462 | |
| 463 | return 0; |
| 464 | |
| 465 | err: |
| 466 | kfree(*st); |
| 467 | *st = NULL; |
| 468 | return ret; |
| 469 | } |
| 470 | |
Imre Deak | e227330 | 2015-07-09 12:59:05 +0300 | [diff] [blame] | 471 | static int |
| 472 | __i915_gem_userptr_set_pages(struct drm_i915_gem_object *obj, |
| 473 | struct page **pvec, int num_pages) |
| 474 | { |
| 475 | int ret; |
| 476 | |
| 477 | ret = st_set_pages(&obj->pages, pvec, num_pages); |
| 478 | if (ret) |
| 479 | return ret; |
| 480 | |
| 481 | ret = i915_gem_gtt_prepare_object(obj); |
| 482 | if (ret) { |
| 483 | sg_free_table(obj->pages); |
| 484 | kfree(obj->pages); |
| 485 | obj->pages = NULL; |
| 486 | } |
| 487 | |
| 488 | return ret; |
| 489 | } |
| 490 | |
Chris Wilson | 380996a | 2015-10-01 12:34:47 +0100 | [diff] [blame] | 491 | static int |
Chris Wilson | e4b946b | 2015-10-01 12:34:46 +0100 | [diff] [blame] | 492 | __i915_gem_userptr_set_active(struct drm_i915_gem_object *obj, |
| 493 | bool value) |
| 494 | { |
Chris Wilson | 380996a | 2015-10-01 12:34:47 +0100 | [diff] [blame] | 495 | int ret = 0; |
| 496 | |
Chris Wilson | e4b946b | 2015-10-01 12:34:46 +0100 | [diff] [blame] | 497 | /* During mm_invalidate_range we need to cancel any userptr that |
| 498 | * overlaps the range being invalidated. Doing so requires the |
| 499 | * struct_mutex, and that risks recursion. In order to cause |
| 500 | * recursion, the user must alias the userptr address space with |
| 501 | * a GTT mmapping (possible with a MAP_FIXED) - then when we have |
| 502 | * to invalidate that mmaping, mm_invalidate_range is called with |
| 503 | * the userptr address *and* the struct_mutex held. To prevent that |
| 504 | * we set a flag under the i915_mmu_notifier spinlock to indicate |
| 505 | * whether this object is valid. |
| 506 | */ |
| 507 | #if defined(CONFIG_MMU_NOTIFIER) |
| 508 | if (obj->userptr.mmu_object == NULL) |
Chris Wilson | 380996a | 2015-10-01 12:34:47 +0100 | [diff] [blame] | 509 | return 0; |
Chris Wilson | e4b946b | 2015-10-01 12:34:46 +0100 | [diff] [blame] | 510 | |
| 511 | spin_lock(&obj->userptr.mmu_object->mn->lock); |
Chris Wilson | 380996a | 2015-10-01 12:34:47 +0100 | [diff] [blame] | 512 | /* In order to serialise get_pages with an outstanding |
| 513 | * cancel_userptr, we must drop the struct_mutex and try again. |
| 514 | */ |
Chris Wilson | 768e159 | 2016-01-21 17:32:43 +0000 | [diff] [blame] | 515 | if (!value) |
| 516 | del_object(obj->userptr.mmu_object); |
| 517 | else if (!work_pending(&obj->userptr.mmu_object->work)) |
| 518 | add_object(obj->userptr.mmu_object); |
Chris Wilson | 380996a | 2015-10-01 12:34:47 +0100 | [diff] [blame] | 519 | else |
| 520 | ret = -EAGAIN; |
Chris Wilson | e4b946b | 2015-10-01 12:34:46 +0100 | [diff] [blame] | 521 | spin_unlock(&obj->userptr.mmu_object->mn->lock); |
| 522 | #endif |
Chris Wilson | 380996a | 2015-10-01 12:34:47 +0100 | [diff] [blame] | 523 | |
| 524 | return ret; |
Chris Wilson | e4b946b | 2015-10-01 12:34:46 +0100 | [diff] [blame] | 525 | } |
| 526 | |
Chris Wilson | 5cc9ed4 | 2014-05-16 14:22:37 +0100 | [diff] [blame] | 527 | static void |
| 528 | __i915_gem_userptr_get_pages_worker(struct work_struct *_work) |
| 529 | { |
| 530 | struct get_pages_work *work = container_of(_work, typeof(*work), work); |
| 531 | struct drm_i915_gem_object *obj = work->obj; |
| 532 | struct drm_device *dev = obj->base.dev; |
Chris Wilson | 68d6c84 | 2015-10-01 12:34:45 +0100 | [diff] [blame] | 533 | const int npages = obj->base.size >> PAGE_SHIFT; |
Chris Wilson | 5cc9ed4 | 2014-05-16 14:22:37 +0100 | [diff] [blame] | 534 | struct page **pvec; |
| 535 | int pinned, ret; |
| 536 | |
| 537 | ret = -ENOMEM; |
| 538 | pinned = 0; |
| 539 | |
Chris Wilson | f2a85e1 | 2016-04-08 12:11:13 +0100 | [diff] [blame] | 540 | pvec = drm_malloc_gfp(npages, sizeof(struct page *), GFP_TEMPORARY); |
Chris Wilson | 5cc9ed4 | 2014-05-16 14:22:37 +0100 | [diff] [blame] | 541 | if (pvec != NULL) { |
Chris Wilson | ad46cb5 | 2014-08-07 14:20:40 +0100 | [diff] [blame] | 542 | struct mm_struct *mm = obj->userptr.mm->mm; |
Chris Wilson | 5cc9ed4 | 2014-05-16 14:22:37 +0100 | [diff] [blame] | 543 | |
Chris Wilson | 40313f0 | 2016-04-05 15:00:00 +0100 | [diff] [blame] | 544 | ret = -EFAULT; |
| 545 | if (atomic_inc_not_zero(&mm->mm_users)) { |
| 546 | down_read(&mm->mmap_sem); |
| 547 | while (pinned < npages) { |
| 548 | ret = get_user_pages_remote |
| 549 | (work->task, mm, |
| 550 | obj->userptr.ptr + pinned * PAGE_SIZE, |
| 551 | npages - pinned, |
| 552 | !obj->userptr.read_only, 0, |
| 553 | pvec + pinned, NULL); |
| 554 | if (ret < 0) |
| 555 | break; |
Chris Wilson | 5cc9ed4 | 2014-05-16 14:22:37 +0100 | [diff] [blame] | 556 | |
Chris Wilson | 40313f0 | 2016-04-05 15:00:00 +0100 | [diff] [blame] | 557 | pinned += ret; |
| 558 | } |
| 559 | up_read(&mm->mmap_sem); |
| 560 | mmput(mm); |
Chris Wilson | 5cc9ed4 | 2014-05-16 14:22:37 +0100 | [diff] [blame] | 561 | } |
Chris Wilson | 5cc9ed4 | 2014-05-16 14:22:37 +0100 | [diff] [blame] | 562 | } |
| 563 | |
| 564 | mutex_lock(&dev->struct_mutex); |
Chris Wilson | 68d6c84 | 2015-10-01 12:34:45 +0100 | [diff] [blame] | 565 | if (obj->userptr.work == &work->work) { |
| 566 | if (pinned == npages) { |
| 567 | ret = __i915_gem_userptr_set_pages(obj, pvec, npages); |
| 568 | if (ret == 0) { |
| 569 | list_add_tail(&obj->global_list, |
| 570 | &to_i915(dev)->mm.unbound_list); |
| 571 | obj->get_page.sg = obj->pages->sgl; |
| 572 | obj->get_page.last = 0; |
| 573 | pinned = 0; |
| 574 | } |
Chris Wilson | 5cc9ed4 | 2014-05-16 14:22:37 +0100 | [diff] [blame] | 575 | } |
Chris Wilson | 68d6c84 | 2015-10-01 12:34:45 +0100 | [diff] [blame] | 576 | obj->userptr.work = ERR_PTR(ret); |
Chris Wilson | e4b946b | 2015-10-01 12:34:46 +0100 | [diff] [blame] | 577 | if (ret) |
| 578 | __i915_gem_userptr_set_active(obj, false); |
Chris Wilson | 5cc9ed4 | 2014-05-16 14:22:37 +0100 | [diff] [blame] | 579 | } |
| 580 | |
Chris Wilson | 5cc9ed4 | 2014-05-16 14:22:37 +0100 | [diff] [blame] | 581 | obj->userptr.workers--; |
| 582 | drm_gem_object_unreference(&obj->base); |
| 583 | mutex_unlock(&dev->struct_mutex); |
| 584 | |
| 585 | release_pages(pvec, pinned, 0); |
| 586 | drm_free_large(pvec); |
| 587 | |
| 588 | put_task_struct(work->task); |
| 589 | kfree(work); |
| 590 | } |
| 591 | |
| 592 | static int |
Chris Wilson | e4b946b | 2015-10-01 12:34:46 +0100 | [diff] [blame] | 593 | __i915_gem_userptr_get_pages_schedule(struct drm_i915_gem_object *obj, |
| 594 | bool *active) |
| 595 | { |
| 596 | struct get_pages_work *work; |
| 597 | |
| 598 | /* Spawn a worker so that we can acquire the |
| 599 | * user pages without holding our mutex. Access |
| 600 | * to the user pages requires mmap_sem, and we have |
| 601 | * a strict lock ordering of mmap_sem, struct_mutex - |
| 602 | * we already hold struct_mutex here and so cannot |
| 603 | * call gup without encountering a lock inversion. |
| 604 | * |
| 605 | * Userspace will keep on repeating the operation |
| 606 | * (thanks to EAGAIN) until either we hit the fast |
| 607 | * path or the worker completes. If the worker is |
| 608 | * cancelled or superseded, the task is still run |
| 609 | * but the results ignored. (This leads to |
| 610 | * complications that we may have a stray object |
| 611 | * refcount that we need to be wary of when |
| 612 | * checking for existing objects during creation.) |
| 613 | * If the worker encounters an error, it reports |
| 614 | * that error back to this function through |
| 615 | * obj->userptr.work = ERR_PTR. |
| 616 | */ |
| 617 | if (obj->userptr.workers >= I915_GEM_USERPTR_MAX_WORKERS) |
| 618 | return -EAGAIN; |
| 619 | |
| 620 | work = kmalloc(sizeof(*work), GFP_KERNEL); |
| 621 | if (work == NULL) |
| 622 | return -ENOMEM; |
| 623 | |
| 624 | obj->userptr.work = &work->work; |
| 625 | obj->userptr.workers++; |
| 626 | |
| 627 | work->obj = obj; |
| 628 | drm_gem_object_reference(&obj->base); |
| 629 | |
| 630 | work->task = current; |
| 631 | get_task_struct(work->task); |
| 632 | |
| 633 | INIT_WORK(&work->work, __i915_gem_userptr_get_pages_worker); |
| 634 | schedule_work(&work->work); |
| 635 | |
| 636 | *active = true; |
| 637 | return -EAGAIN; |
| 638 | } |
| 639 | |
| 640 | static int |
Chris Wilson | 5cc9ed4 | 2014-05-16 14:22:37 +0100 | [diff] [blame] | 641 | i915_gem_userptr_get_pages(struct drm_i915_gem_object *obj) |
| 642 | { |
| 643 | const int num_pages = obj->base.size >> PAGE_SHIFT; |
| 644 | struct page **pvec; |
| 645 | int pinned, ret; |
Chris Wilson | e4b946b | 2015-10-01 12:34:46 +0100 | [diff] [blame] | 646 | bool active; |
Chris Wilson | 5cc9ed4 | 2014-05-16 14:22:37 +0100 | [diff] [blame] | 647 | |
| 648 | /* If userspace should engineer that these pages are replaced in |
| 649 | * the vma between us binding this page into the GTT and completion |
| 650 | * of rendering... Their loss. If they change the mapping of their |
| 651 | * pages they need to create a new bo to point to the new vma. |
| 652 | * |
| 653 | * However, that still leaves open the possibility of the vma |
| 654 | * being copied upon fork. Which falls under the same userspace |
| 655 | * synchronisation issue as a regular bo, except that this time |
| 656 | * the process may not be expecting that a particular piece of |
| 657 | * memory is tied to the GPU. |
| 658 | * |
| 659 | * Fortunately, we can hook into the mmu_notifier in order to |
| 660 | * discard the page references prior to anything nasty happening |
| 661 | * to the vma (discard or cloning) which should prevent the more |
| 662 | * egregious cases from causing harm. |
| 663 | */ |
Chris Wilson | e4b946b | 2015-10-01 12:34:46 +0100 | [diff] [blame] | 664 | if (IS_ERR(obj->userptr.work)) { |
| 665 | /* active flag will have been dropped already by the worker */ |
| 666 | ret = PTR_ERR(obj->userptr.work); |
| 667 | obj->userptr.work = NULL; |
| 668 | return ret; |
| 669 | } |
| 670 | if (obj->userptr.work) |
| 671 | /* active flag should still be held for the pending work */ |
| 672 | return -EAGAIN; |
| 673 | |
| 674 | /* Let the mmu-notifier know that we have begun and need cancellation */ |
Chris Wilson | 380996a | 2015-10-01 12:34:47 +0100 | [diff] [blame] | 675 | ret = __i915_gem_userptr_set_active(obj, true); |
| 676 | if (ret) |
| 677 | return ret; |
Chris Wilson | 5cc9ed4 | 2014-05-16 14:22:37 +0100 | [diff] [blame] | 678 | |
| 679 | pvec = NULL; |
| 680 | pinned = 0; |
Chris Wilson | ad46cb5 | 2014-08-07 14:20:40 +0100 | [diff] [blame] | 681 | if (obj->userptr.mm->mm == current->mm) { |
Chris Wilson | f2a85e1 | 2016-04-08 12:11:13 +0100 | [diff] [blame] | 682 | pvec = drm_malloc_gfp(num_pages, sizeof(struct page *), |
| 683 | GFP_TEMPORARY); |
Chris Wilson | 5cc9ed4 | 2014-05-16 14:22:37 +0100 | [diff] [blame] | 684 | if (pvec == NULL) { |
Chris Wilson | f2a85e1 | 2016-04-08 12:11:13 +0100 | [diff] [blame] | 685 | __i915_gem_userptr_set_active(obj, false); |
| 686 | return -ENOMEM; |
Chris Wilson | 5cc9ed4 | 2014-05-16 14:22:37 +0100 | [diff] [blame] | 687 | } |
| 688 | |
| 689 | pinned = __get_user_pages_fast(obj->userptr.ptr, num_pages, |
| 690 | !obj->userptr.read_only, pvec); |
| 691 | } |
Chris Wilson | 5cc9ed4 | 2014-05-16 14:22:37 +0100 | [diff] [blame] | 692 | |
Chris Wilson | e4b946b | 2015-10-01 12:34:46 +0100 | [diff] [blame] | 693 | active = false; |
| 694 | if (pinned < 0) |
| 695 | ret = pinned, pinned = 0; |
| 696 | else if (pinned < num_pages) |
| 697 | ret = __i915_gem_userptr_get_pages_schedule(obj, &active); |
| 698 | else |
Imre Deak | e227330 | 2015-07-09 12:59:05 +0300 | [diff] [blame] | 699 | ret = __i915_gem_userptr_set_pages(obj, pvec, num_pages); |
Chris Wilson | e4b946b | 2015-10-01 12:34:46 +0100 | [diff] [blame] | 700 | if (ret) { |
| 701 | __i915_gem_userptr_set_active(obj, active); |
| 702 | release_pages(pvec, pinned, 0); |
Chris Wilson | 5cc9ed4 | 2014-05-16 14:22:37 +0100 | [diff] [blame] | 703 | } |
Chris Wilson | 5cc9ed4 | 2014-05-16 14:22:37 +0100 | [diff] [blame] | 704 | drm_free_large(pvec); |
| 705 | return ret; |
| 706 | } |
| 707 | |
| 708 | static void |
| 709 | i915_gem_userptr_put_pages(struct drm_i915_gem_object *obj) |
| 710 | { |
Tvrtko Ursulin | c479f43 | 2014-09-26 15:05:22 +0100 | [diff] [blame] | 711 | struct sg_page_iter sg_iter; |
Chris Wilson | 5cc9ed4 | 2014-05-16 14:22:37 +0100 | [diff] [blame] | 712 | |
| 713 | BUG_ON(obj->userptr.work != NULL); |
Chris Wilson | e4b946b | 2015-10-01 12:34:46 +0100 | [diff] [blame] | 714 | __i915_gem_userptr_set_active(obj, false); |
Chris Wilson | 5cc9ed4 | 2014-05-16 14:22:37 +0100 | [diff] [blame] | 715 | |
| 716 | if (obj->madv != I915_MADV_WILLNEED) |
| 717 | obj->dirty = 0; |
| 718 | |
Imre Deak | e227330 | 2015-07-09 12:59:05 +0300 | [diff] [blame] | 719 | i915_gem_gtt_finish_object(obj); |
| 720 | |
Tvrtko Ursulin | c479f43 | 2014-09-26 15:05:22 +0100 | [diff] [blame] | 721 | for_each_sg_page(obj->pages->sgl, &sg_iter, obj->pages->nents, 0) { |
| 722 | struct page *page = sg_page_iter_page(&sg_iter); |
Chris Wilson | 5cc9ed4 | 2014-05-16 14:22:37 +0100 | [diff] [blame] | 723 | |
| 724 | if (obj->dirty) |
| 725 | set_page_dirty(page); |
| 726 | |
| 727 | mark_page_accessed(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 728 | put_page(page); |
Chris Wilson | 5cc9ed4 | 2014-05-16 14:22:37 +0100 | [diff] [blame] | 729 | } |
| 730 | obj->dirty = 0; |
| 731 | |
| 732 | sg_free_table(obj->pages); |
| 733 | kfree(obj->pages); |
| 734 | } |
| 735 | |
| 736 | static void |
| 737 | i915_gem_userptr_release(struct drm_i915_gem_object *obj) |
| 738 | { |
| 739 | i915_gem_userptr_release__mmu_notifier(obj); |
Chris Wilson | ad46cb5 | 2014-08-07 14:20:40 +0100 | [diff] [blame] | 740 | i915_gem_userptr_release__mm_struct(obj); |
Chris Wilson | 5cc9ed4 | 2014-05-16 14:22:37 +0100 | [diff] [blame] | 741 | } |
| 742 | |
| 743 | static int |
| 744 | i915_gem_userptr_dmabuf_export(struct drm_i915_gem_object *obj) |
| 745 | { |
Chris Wilson | ad46cb5 | 2014-08-07 14:20:40 +0100 | [diff] [blame] | 746 | if (obj->userptr.mmu_object) |
Chris Wilson | 5cc9ed4 | 2014-05-16 14:22:37 +0100 | [diff] [blame] | 747 | return 0; |
| 748 | |
| 749 | return i915_gem_userptr_init__mmu_notifier(obj, 0); |
| 750 | } |
| 751 | |
| 752 | static const struct drm_i915_gem_object_ops i915_gem_userptr_ops = { |
Chris Wilson | de47266 | 2016-01-22 18:32:31 +0000 | [diff] [blame] | 753 | .flags = I915_GEM_OBJECT_HAS_STRUCT_PAGE, |
Chris Wilson | 5cc9ed4 | 2014-05-16 14:22:37 +0100 | [diff] [blame] | 754 | .get_pages = i915_gem_userptr_get_pages, |
| 755 | .put_pages = i915_gem_userptr_put_pages, |
Chris Wilson | de47266 | 2016-01-22 18:32:31 +0000 | [diff] [blame] | 756 | .dmabuf_export = i915_gem_userptr_dmabuf_export, |
Chris Wilson | 5cc9ed4 | 2014-05-16 14:22:37 +0100 | [diff] [blame] | 757 | .release = i915_gem_userptr_release, |
| 758 | }; |
| 759 | |
| 760 | /** |
| 761 | * Creates a new mm object that wraps some normal memory from the process |
| 762 | * context - user memory. |
| 763 | * |
| 764 | * We impose several restrictions upon the memory being mapped |
| 765 | * into the GPU. |
| 766 | * 1. It must be page aligned (both start/end addresses, i.e ptr and size). |
Chris Wilson | ec8b0dd | 2014-07-21 13:21:23 +0100 | [diff] [blame] | 767 | * 2. It must be normal system memory, not a pointer into another map of IO |
Chris Wilson | 5cc9ed4 | 2014-05-16 14:22:37 +0100 | [diff] [blame] | 768 | * space (e.g. it must not be a GTT mmapping of another object). |
Chris Wilson | ec8b0dd | 2014-07-21 13:21:23 +0100 | [diff] [blame] | 769 | * 3. We only allow a bo as large as we could in theory map into the GTT, |
Chris Wilson | 5cc9ed4 | 2014-05-16 14:22:37 +0100 | [diff] [blame] | 770 | * that is we limit the size to the total size of the GTT. |
Chris Wilson | ec8b0dd | 2014-07-21 13:21:23 +0100 | [diff] [blame] | 771 | * 4. The bo is marked as being snoopable. The backing pages are left |
Chris Wilson | 5cc9ed4 | 2014-05-16 14:22:37 +0100 | [diff] [blame] | 772 | * accessible directly by the CPU, but reads and writes by the GPU may |
| 773 | * incur the cost of a snoop (unless you have an LLC architecture). |
| 774 | * |
| 775 | * Synchronisation between multiple users and the GPU is left to userspace |
| 776 | * through the normal set-domain-ioctl. The kernel will enforce that the |
| 777 | * GPU relinquishes the VMA before it is returned back to the system |
| 778 | * i.e. upon free(), munmap() or process termination. However, the userspace |
| 779 | * malloc() library may not immediately relinquish the VMA after free() and |
| 780 | * instead reuse it whilst the GPU is still reading and writing to the VMA. |
| 781 | * Caveat emptor. |
| 782 | * |
| 783 | * Also note, that the object created here is not currently a "first class" |
| 784 | * object, in that several ioctls are banned. These are the CPU access |
| 785 | * ioctls: mmap(), pwrite and pread. In practice, you are expected to use |
Chris Wilson | cc917ab | 2015-10-13 14:22:26 +0100 | [diff] [blame] | 786 | * direct access via your pointer rather than use those ioctls. Another |
| 787 | * restriction is that we do not allow userptr surfaces to be pinned to the |
| 788 | * hardware and so we reject any attempt to create a framebuffer out of a |
| 789 | * userptr. |
Chris Wilson | 5cc9ed4 | 2014-05-16 14:22:37 +0100 | [diff] [blame] | 790 | * |
| 791 | * If you think this is a good interface to use to pass GPU memory between |
| 792 | * drivers, please use dma-buf instead. In fact, wherever possible use |
| 793 | * dma-buf instead. |
| 794 | */ |
| 795 | int |
| 796 | i915_gem_userptr_ioctl(struct drm_device *dev, void *data, struct drm_file *file) |
| 797 | { |
Chris Wilson | 5cc9ed4 | 2014-05-16 14:22:37 +0100 | [diff] [blame] | 798 | struct drm_i915_gem_userptr *args = data; |
| 799 | struct drm_i915_gem_object *obj; |
| 800 | int ret; |
| 801 | u32 handle; |
| 802 | |
Tvrtko Ursulin | ca37780 | 2016-03-02 12:10:31 +0000 | [diff] [blame] | 803 | if (!HAS_LLC(dev) && !HAS_SNOOP(dev)) { |
| 804 | /* We cannot support coherent userptr objects on hw without |
| 805 | * LLC and broken snooping. |
| 806 | */ |
| 807 | return -ENODEV; |
| 808 | } |
| 809 | |
Chris Wilson | 5cc9ed4 | 2014-05-16 14:22:37 +0100 | [diff] [blame] | 810 | if (args->flags & ~(I915_USERPTR_READ_ONLY | |
| 811 | I915_USERPTR_UNSYNCHRONIZED)) |
| 812 | return -EINVAL; |
| 813 | |
| 814 | if (offset_in_page(args->user_ptr | args->user_size)) |
| 815 | return -EINVAL; |
| 816 | |
Chris Wilson | 5cc9ed4 | 2014-05-16 14:22:37 +0100 | [diff] [blame] | 817 | if (!access_ok(args->flags & I915_USERPTR_READ_ONLY ? VERIFY_READ : VERIFY_WRITE, |
| 818 | (char __user *)(unsigned long)args->user_ptr, args->user_size)) |
| 819 | return -EFAULT; |
| 820 | |
| 821 | if (args->flags & I915_USERPTR_READ_ONLY) { |
| 822 | /* On almost all of the current hw, we cannot tell the GPU that a |
| 823 | * page is readonly, so this is just a placeholder in the uAPI. |
| 824 | */ |
| 825 | return -ENODEV; |
| 826 | } |
| 827 | |
Chris Wilson | 5cc9ed4 | 2014-05-16 14:22:37 +0100 | [diff] [blame] | 828 | obj = i915_gem_object_alloc(dev); |
| 829 | if (obj == NULL) |
| 830 | return -ENOMEM; |
| 831 | |
| 832 | drm_gem_private_object_init(dev, &obj->base, args->user_size); |
| 833 | i915_gem_object_init(obj, &i915_gem_userptr_ops); |
| 834 | obj->cache_level = I915_CACHE_LLC; |
| 835 | obj->base.write_domain = I915_GEM_DOMAIN_CPU; |
| 836 | obj->base.read_domains = I915_GEM_DOMAIN_CPU; |
| 837 | |
| 838 | obj->userptr.ptr = args->user_ptr; |
| 839 | obj->userptr.read_only = !!(args->flags & I915_USERPTR_READ_ONLY); |
| 840 | |
| 841 | /* And keep a pointer to the current->mm for resolving the user pages |
| 842 | * at binding. This means that we need to hook into the mmu_notifier |
| 843 | * in order to detect if the mmu is destroyed. |
| 844 | */ |
Chris Wilson | ad46cb5 | 2014-08-07 14:20:40 +0100 | [diff] [blame] | 845 | ret = i915_gem_userptr_init__mm_struct(obj); |
| 846 | if (ret == 0) |
Chris Wilson | 5cc9ed4 | 2014-05-16 14:22:37 +0100 | [diff] [blame] | 847 | ret = i915_gem_userptr_init__mmu_notifier(obj, args->flags); |
| 848 | if (ret == 0) |
| 849 | ret = drm_gem_handle_create(file, &obj->base, &handle); |
| 850 | |
| 851 | /* drop reference from allocate - handle holds it now */ |
| 852 | drm_gem_object_unreference_unlocked(&obj->base); |
| 853 | if (ret) |
| 854 | return ret; |
| 855 | |
| 856 | args->handle = handle; |
| 857 | return 0; |
| 858 | } |
| 859 | |
| 860 | int |
| 861 | i915_gem_init_userptr(struct drm_device *dev) |
| 862 | { |
Chris Wilson | 5cc9ed4 | 2014-05-16 14:22:37 +0100 | [diff] [blame] | 863 | struct drm_i915_private *dev_priv = to_i915(dev); |
Chris Wilson | ad46cb5 | 2014-08-07 14:20:40 +0100 | [diff] [blame] | 864 | mutex_init(&dev_priv->mm_lock); |
| 865 | hash_init(dev_priv->mm_structs); |
Chris Wilson | 5cc9ed4 | 2014-05-16 14:22:37 +0100 | [diff] [blame] | 866 | return 0; |
| 867 | } |