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 | |
| 25 | #include "drmP.h" |
| 26 | #include "i915_drm.h" |
| 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 | |
| 35 | #if defined(CONFIG_MMU_NOTIFIER) |
| 36 | #include <linux/interval_tree.h> |
| 37 | |
| 38 | struct i915_mmu_notifier { |
| 39 | spinlock_t lock; |
| 40 | struct hlist_node node; |
| 41 | struct mmu_notifier mn; |
| 42 | struct rb_root objects; |
Chris Wilson | ec8b0dd | 2014-07-21 13:21:23 +0100 | [diff] [blame] | 43 | struct list_head linear; |
Chris Wilson | 5cc9ed4 | 2014-05-16 14:22:37 +0100 | [diff] [blame] | 44 | struct drm_device *dev; |
| 45 | struct mm_struct *mm; |
| 46 | struct work_struct work; |
| 47 | unsigned long count; |
| 48 | unsigned long serial; |
Chris Wilson | ec8b0dd | 2014-07-21 13:21:23 +0100 | [diff] [blame] | 49 | bool has_linear; |
Chris Wilson | 5cc9ed4 | 2014-05-16 14:22:37 +0100 | [diff] [blame] | 50 | }; |
| 51 | |
| 52 | struct i915_mmu_object { |
| 53 | struct i915_mmu_notifier *mmu; |
| 54 | struct interval_tree_node it; |
Chris Wilson | ec8b0dd | 2014-07-21 13:21:23 +0100 | [diff] [blame] | 55 | struct list_head link; |
Chris Wilson | 5cc9ed4 | 2014-05-16 14:22:37 +0100 | [diff] [blame] | 56 | struct drm_i915_gem_object *obj; |
Chris Wilson | ec8b0dd | 2014-07-21 13:21:23 +0100 | [diff] [blame] | 57 | bool is_linear; |
Chris Wilson | 5cc9ed4 | 2014-05-16 14:22:37 +0100 | [diff] [blame] | 58 | }; |
| 59 | |
Chris Wilson | ec8b0dd | 2014-07-21 13:21:23 +0100 | [diff] [blame] | 60 | static unsigned long cancel_userptr(struct drm_i915_gem_object *obj) |
| 61 | { |
| 62 | struct drm_device *dev = obj->base.dev; |
| 63 | unsigned long end; |
| 64 | |
| 65 | mutex_lock(&dev->struct_mutex); |
| 66 | /* Cancel any active worker and force us to re-evaluate gup */ |
| 67 | obj->userptr.work = NULL; |
| 68 | |
| 69 | if (obj->pages != NULL) { |
| 70 | struct drm_i915_private *dev_priv = to_i915(dev); |
| 71 | struct i915_vma *vma, *tmp; |
| 72 | bool was_interruptible; |
| 73 | |
| 74 | was_interruptible = dev_priv->mm.interruptible; |
| 75 | dev_priv->mm.interruptible = false; |
| 76 | |
| 77 | list_for_each_entry_safe(vma, tmp, &obj->vma_list, vma_link) { |
| 78 | int ret = i915_vma_unbind(vma); |
| 79 | WARN_ON(ret && ret != -EIO); |
| 80 | } |
| 81 | WARN_ON(i915_gem_object_put_pages(obj)); |
| 82 | |
| 83 | dev_priv->mm.interruptible = was_interruptible; |
| 84 | } |
| 85 | |
| 86 | end = obj->userptr.ptr + obj->base.size; |
| 87 | |
| 88 | drm_gem_object_unreference(&obj->base); |
| 89 | mutex_unlock(&dev->struct_mutex); |
| 90 | |
| 91 | return end; |
| 92 | } |
| 93 | |
Chris Wilson | 4877776 | 2014-07-24 13:28:44 +0100 | [diff] [blame] | 94 | static void *invalidate_range__linear(struct i915_mmu_notifier *mn, |
| 95 | struct mm_struct *mm, |
| 96 | unsigned long start, |
| 97 | unsigned long end) |
Chris Wilson | ec8b0dd | 2014-07-21 13:21:23 +0100 | [diff] [blame] | 98 | { |
| 99 | struct i915_mmu_object *mmu; |
| 100 | unsigned long serial; |
| 101 | |
| 102 | restart: |
| 103 | serial = mn->serial; |
| 104 | list_for_each_entry(mmu, &mn->linear, link) { |
| 105 | struct drm_i915_gem_object *obj; |
| 106 | |
| 107 | if (mmu->it.last < start || mmu->it.start > end) |
| 108 | continue; |
| 109 | |
| 110 | obj = mmu->obj; |
| 111 | drm_gem_object_reference(&obj->base); |
| 112 | spin_unlock(&mn->lock); |
| 113 | |
| 114 | cancel_userptr(obj); |
| 115 | |
| 116 | spin_lock(&mn->lock); |
| 117 | if (serial != mn->serial) |
| 118 | goto restart; |
| 119 | } |
| 120 | |
Chris Wilson | 4877776 | 2014-07-24 13:28:44 +0100 | [diff] [blame] | 121 | return NULL; |
Chris Wilson | ec8b0dd | 2014-07-21 13:21:23 +0100 | [diff] [blame] | 122 | } |
| 123 | |
Chris Wilson | 5cc9ed4 | 2014-05-16 14:22:37 +0100 | [diff] [blame] | 124 | static void i915_gem_userptr_mn_invalidate_range_start(struct mmu_notifier *_mn, |
| 125 | struct mm_struct *mm, |
| 126 | unsigned long start, |
| 127 | unsigned long end) |
| 128 | { |
| 129 | struct i915_mmu_notifier *mn = container_of(_mn, struct i915_mmu_notifier, mn); |
| 130 | struct interval_tree_node *it = NULL; |
Chris Wilson | ec8b0dd | 2014-07-21 13:21:23 +0100 | [diff] [blame] | 131 | unsigned long next = start; |
Chris Wilson | 5cc9ed4 | 2014-05-16 14:22:37 +0100 | [diff] [blame] | 132 | unsigned long serial = 0; |
| 133 | |
| 134 | end--; /* interval ranges are inclusive, but invalidate range is exclusive */ |
Chris Wilson | ec8b0dd | 2014-07-21 13:21:23 +0100 | [diff] [blame] | 135 | while (next < end) { |
Chris Wilson | 4877776 | 2014-07-24 13:28:44 +0100 | [diff] [blame] | 136 | struct drm_i915_gem_object *obj = NULL; |
Chris Wilson | 5cc9ed4 | 2014-05-16 14:22:37 +0100 | [diff] [blame] | 137 | |
Chris Wilson | 5cc9ed4 | 2014-05-16 14:22:37 +0100 | [diff] [blame] | 138 | spin_lock(&mn->lock); |
Chris Wilson | ec8b0dd | 2014-07-21 13:21:23 +0100 | [diff] [blame] | 139 | if (mn->has_linear) |
Chris Wilson | 4877776 | 2014-07-24 13:28:44 +0100 | [diff] [blame] | 140 | it = invalidate_range__linear(mn, mm, start, end); |
| 141 | else if (serial == mn->serial) |
Chris Wilson | ec8b0dd | 2014-07-21 13:21:23 +0100 | [diff] [blame] | 142 | it = interval_tree_iter_next(it, next, end); |
Chris Wilson | 5cc9ed4 | 2014-05-16 14:22:37 +0100 | [diff] [blame] | 143 | else |
| 144 | it = interval_tree_iter_first(&mn->objects, start, end); |
| 145 | if (it != NULL) { |
| 146 | obj = container_of(it, struct i915_mmu_object, it)->obj; |
| 147 | drm_gem_object_reference(&obj->base); |
| 148 | serial = mn->serial; |
| 149 | } |
| 150 | spin_unlock(&mn->lock); |
| 151 | if (obj == NULL) |
| 152 | return; |
| 153 | |
Chris Wilson | ec8b0dd | 2014-07-21 13:21:23 +0100 | [diff] [blame] | 154 | next = cancel_userptr(obj); |
Chris Wilson | 5cc9ed4 | 2014-05-16 14:22:37 +0100 | [diff] [blame] | 155 | } |
| 156 | } |
| 157 | |
| 158 | static const struct mmu_notifier_ops i915_gem_userptr_notifier = { |
| 159 | .invalidate_range_start = i915_gem_userptr_mn_invalidate_range_start, |
| 160 | }; |
| 161 | |
| 162 | static struct i915_mmu_notifier * |
| 163 | __i915_mmu_notifier_lookup(struct drm_device *dev, struct mm_struct *mm) |
| 164 | { |
| 165 | struct drm_i915_private *dev_priv = to_i915(dev); |
| 166 | struct i915_mmu_notifier *mmu; |
| 167 | |
| 168 | /* Protected by dev->struct_mutex */ |
| 169 | hash_for_each_possible(dev_priv->mmu_notifiers, mmu, node, (unsigned long)mm) |
| 170 | if (mmu->mm == mm) |
| 171 | return mmu; |
| 172 | |
| 173 | return NULL; |
| 174 | } |
| 175 | |
| 176 | static struct i915_mmu_notifier * |
| 177 | i915_mmu_notifier_get(struct drm_device *dev, struct mm_struct *mm) |
| 178 | { |
| 179 | struct drm_i915_private *dev_priv = to_i915(dev); |
| 180 | struct i915_mmu_notifier *mmu; |
| 181 | int ret; |
| 182 | |
| 183 | lockdep_assert_held(&dev->struct_mutex); |
| 184 | |
| 185 | mmu = __i915_mmu_notifier_lookup(dev, mm); |
| 186 | if (mmu) |
| 187 | return mmu; |
| 188 | |
| 189 | mmu = kmalloc(sizeof(*mmu), GFP_KERNEL); |
| 190 | if (mmu == NULL) |
| 191 | return ERR_PTR(-ENOMEM); |
| 192 | |
| 193 | spin_lock_init(&mmu->lock); |
| 194 | mmu->dev = dev; |
| 195 | mmu->mn.ops = &i915_gem_userptr_notifier; |
| 196 | mmu->mm = mm; |
| 197 | mmu->objects = RB_ROOT; |
| 198 | mmu->count = 0; |
Chris Wilson | 6c308fe | 2014-07-11 11:28:01 +0100 | [diff] [blame] | 199 | mmu->serial = 1; |
Chris Wilson | ec8b0dd | 2014-07-21 13:21:23 +0100 | [diff] [blame] | 200 | INIT_LIST_HEAD(&mmu->linear); |
| 201 | mmu->has_linear = false; |
Chris Wilson | 5cc9ed4 | 2014-05-16 14:22:37 +0100 | [diff] [blame] | 202 | |
| 203 | /* Protected by mmap_sem (write-lock) */ |
| 204 | ret = __mmu_notifier_register(&mmu->mn, mm); |
| 205 | if (ret) { |
| 206 | kfree(mmu); |
| 207 | return ERR_PTR(ret); |
| 208 | } |
| 209 | |
| 210 | /* Protected by dev->struct_mutex */ |
| 211 | hash_add(dev_priv->mmu_notifiers, &mmu->node, (unsigned long)mm); |
| 212 | return mmu; |
| 213 | } |
| 214 | |
| 215 | static void |
| 216 | __i915_mmu_notifier_destroy_worker(struct work_struct *work) |
| 217 | { |
| 218 | struct i915_mmu_notifier *mmu = container_of(work, typeof(*mmu), work); |
| 219 | mmu_notifier_unregister(&mmu->mn, mmu->mm); |
| 220 | kfree(mmu); |
| 221 | } |
| 222 | |
| 223 | static void |
| 224 | __i915_mmu_notifier_destroy(struct i915_mmu_notifier *mmu) |
| 225 | { |
| 226 | lockdep_assert_held(&mmu->dev->struct_mutex); |
| 227 | |
| 228 | /* Protected by dev->struct_mutex */ |
| 229 | hash_del(&mmu->node); |
| 230 | |
| 231 | /* Our lock ordering is: mmap_sem, mmu_notifier_scru, struct_mutex. |
| 232 | * We enter the function holding struct_mutex, therefore we need |
| 233 | * to drop our mutex prior to calling mmu_notifier_unregister in |
| 234 | * order to prevent lock inversion (and system-wide deadlock) |
| 235 | * between the mmap_sem and struct-mutex. Hence we defer the |
| 236 | * unregistration to a workqueue where we hold no locks. |
| 237 | */ |
| 238 | INIT_WORK(&mmu->work, __i915_mmu_notifier_destroy_worker); |
| 239 | schedule_work(&mmu->work); |
| 240 | } |
| 241 | |
| 242 | static void __i915_mmu_notifier_update_serial(struct i915_mmu_notifier *mmu) |
| 243 | { |
| 244 | if (++mmu->serial == 0) |
| 245 | mmu->serial = 1; |
| 246 | } |
| 247 | |
Chris Wilson | ec8b0dd | 2014-07-21 13:21:23 +0100 | [diff] [blame] | 248 | static bool i915_mmu_notifier_has_linear(struct i915_mmu_notifier *mmu) |
| 249 | { |
| 250 | struct i915_mmu_object *mn; |
| 251 | |
| 252 | list_for_each_entry(mn, &mmu->linear, link) |
| 253 | if (mn->is_linear) |
| 254 | return true; |
| 255 | |
| 256 | return false; |
| 257 | } |
| 258 | |
Chris Wilson | 5cc9ed4 | 2014-05-16 14:22:37 +0100 | [diff] [blame] | 259 | static void |
| 260 | i915_mmu_notifier_del(struct i915_mmu_notifier *mmu, |
| 261 | struct i915_mmu_object *mn) |
| 262 | { |
| 263 | lockdep_assert_held(&mmu->dev->struct_mutex); |
| 264 | |
| 265 | spin_lock(&mmu->lock); |
Chris Wilson | ec8b0dd | 2014-07-21 13:21:23 +0100 | [diff] [blame] | 266 | list_del(&mn->link); |
| 267 | if (mn->is_linear) |
| 268 | mmu->has_linear = i915_mmu_notifier_has_linear(mmu); |
| 269 | else |
| 270 | interval_tree_remove(&mn->it, &mmu->objects); |
Chris Wilson | 5cc9ed4 | 2014-05-16 14:22:37 +0100 | [diff] [blame] | 271 | __i915_mmu_notifier_update_serial(mmu); |
| 272 | spin_unlock(&mmu->lock); |
| 273 | |
| 274 | /* Protected against _add() by dev->struct_mutex */ |
| 275 | if (--mmu->count == 0) |
| 276 | __i915_mmu_notifier_destroy(mmu); |
| 277 | } |
| 278 | |
| 279 | static int |
| 280 | i915_mmu_notifier_add(struct i915_mmu_notifier *mmu, |
| 281 | struct i915_mmu_object *mn) |
| 282 | { |
| 283 | struct interval_tree_node *it; |
| 284 | int ret; |
| 285 | |
| 286 | ret = i915_mutex_lock_interruptible(mmu->dev); |
| 287 | if (ret) |
| 288 | return ret; |
| 289 | |
| 290 | /* Make sure we drop the final active reference (and thereby |
| 291 | * remove the objects from the interval tree) before we do |
| 292 | * the check for overlapping objects. |
| 293 | */ |
| 294 | i915_gem_retire_requests(mmu->dev); |
| 295 | |
Chris Wilson | 5cc9ed4 | 2014-05-16 14:22:37 +0100 | [diff] [blame] | 296 | spin_lock(&mmu->lock); |
| 297 | it = interval_tree_iter_first(&mmu->objects, |
| 298 | mn->it.start, mn->it.last); |
| 299 | if (it) { |
| 300 | struct drm_i915_gem_object *obj; |
| 301 | |
| 302 | /* We only need to check the first object in the range as it |
| 303 | * either has cancelled gup work queued and we need to |
| 304 | * return back to the user to give time for the gup-workers |
| 305 | * to flush their object references upon which the object will |
| 306 | * be removed from the interval-tree, or the the range is |
| 307 | * still in use by another client and the overlap is invalid. |
Chris Wilson | ec8b0dd | 2014-07-21 13:21:23 +0100 | [diff] [blame] | 308 | * |
| 309 | * If we do have an overlap, we cannot use the interval tree |
| 310 | * for fast range invalidation. |
Chris Wilson | 5cc9ed4 | 2014-05-16 14:22:37 +0100 | [diff] [blame] | 311 | */ |
| 312 | |
| 313 | obj = container_of(it, struct i915_mmu_object, it)->obj; |
Chris Wilson | ec8b0dd | 2014-07-21 13:21:23 +0100 | [diff] [blame] | 314 | if (!obj->userptr.workers) |
| 315 | mmu->has_linear = mn->is_linear = true; |
| 316 | else |
| 317 | ret = -EAGAIN; |
| 318 | } else |
Chris Wilson | 5cc9ed4 | 2014-05-16 14:22:37 +0100 | [diff] [blame] | 319 | interval_tree_insert(&mn->it, &mmu->objects); |
Chris Wilson | ec8b0dd | 2014-07-21 13:21:23 +0100 | [diff] [blame] | 320 | |
| 321 | if (ret == 0) { |
| 322 | list_add(&mn->link, &mmu->linear); |
Chris Wilson | 5cc9ed4 | 2014-05-16 14:22:37 +0100 | [diff] [blame] | 323 | __i915_mmu_notifier_update_serial(mmu); |
Chris Wilson | 5cc9ed4 | 2014-05-16 14:22:37 +0100 | [diff] [blame] | 324 | } |
| 325 | spin_unlock(&mmu->lock); |
| 326 | mutex_unlock(&mmu->dev->struct_mutex); |
| 327 | |
| 328 | return ret; |
| 329 | } |
| 330 | |
| 331 | static void |
| 332 | i915_gem_userptr_release__mmu_notifier(struct drm_i915_gem_object *obj) |
| 333 | { |
| 334 | struct i915_mmu_object *mn; |
| 335 | |
| 336 | mn = obj->userptr.mn; |
| 337 | if (mn == NULL) |
| 338 | return; |
| 339 | |
| 340 | i915_mmu_notifier_del(mn->mmu, mn); |
| 341 | obj->userptr.mn = NULL; |
| 342 | } |
| 343 | |
| 344 | static int |
| 345 | i915_gem_userptr_init__mmu_notifier(struct drm_i915_gem_object *obj, |
| 346 | unsigned flags) |
| 347 | { |
| 348 | struct i915_mmu_notifier *mmu; |
| 349 | struct i915_mmu_object *mn; |
| 350 | int ret; |
| 351 | |
| 352 | if (flags & I915_USERPTR_UNSYNCHRONIZED) |
| 353 | return capable(CAP_SYS_ADMIN) ? 0 : -EPERM; |
| 354 | |
| 355 | down_write(&obj->userptr.mm->mmap_sem); |
| 356 | ret = i915_mutex_lock_interruptible(obj->base.dev); |
| 357 | if (ret == 0) { |
| 358 | mmu = i915_mmu_notifier_get(obj->base.dev, obj->userptr.mm); |
| 359 | if (!IS_ERR(mmu)) |
| 360 | mmu->count++; /* preemptive add to act as a refcount */ |
| 361 | else |
| 362 | ret = PTR_ERR(mmu); |
| 363 | mutex_unlock(&obj->base.dev->struct_mutex); |
| 364 | } |
| 365 | up_write(&obj->userptr.mm->mmap_sem); |
| 366 | if (ret) |
| 367 | return ret; |
| 368 | |
| 369 | mn = kzalloc(sizeof(*mn), GFP_KERNEL); |
| 370 | if (mn == NULL) { |
| 371 | ret = -ENOMEM; |
| 372 | goto destroy_mmu; |
| 373 | } |
| 374 | |
| 375 | mn->mmu = mmu; |
| 376 | mn->it.start = obj->userptr.ptr; |
| 377 | mn->it.last = mn->it.start + obj->base.size - 1; |
| 378 | mn->obj = obj; |
| 379 | |
| 380 | ret = i915_mmu_notifier_add(mmu, mn); |
| 381 | if (ret) |
| 382 | goto free_mn; |
| 383 | |
| 384 | obj->userptr.mn = mn; |
| 385 | return 0; |
| 386 | |
| 387 | free_mn: |
| 388 | kfree(mn); |
| 389 | destroy_mmu: |
| 390 | mutex_lock(&obj->base.dev->struct_mutex); |
| 391 | if (--mmu->count == 0) |
| 392 | __i915_mmu_notifier_destroy(mmu); |
| 393 | mutex_unlock(&obj->base.dev->struct_mutex); |
| 394 | return ret; |
| 395 | } |
| 396 | |
| 397 | #else |
| 398 | |
| 399 | static void |
| 400 | i915_gem_userptr_release__mmu_notifier(struct drm_i915_gem_object *obj) |
| 401 | { |
| 402 | } |
| 403 | |
| 404 | static int |
| 405 | i915_gem_userptr_init__mmu_notifier(struct drm_i915_gem_object *obj, |
| 406 | unsigned flags) |
| 407 | { |
| 408 | if ((flags & I915_USERPTR_UNSYNCHRONIZED) == 0) |
| 409 | return -ENODEV; |
| 410 | |
| 411 | if (!capable(CAP_SYS_ADMIN)) |
| 412 | return -EPERM; |
| 413 | |
| 414 | return 0; |
| 415 | } |
| 416 | #endif |
| 417 | |
| 418 | struct get_pages_work { |
| 419 | struct work_struct work; |
| 420 | struct drm_i915_gem_object *obj; |
| 421 | struct task_struct *task; |
| 422 | }; |
| 423 | |
| 424 | |
| 425 | #if IS_ENABLED(CONFIG_SWIOTLB) |
| 426 | #define swiotlb_active() swiotlb_nr_tbl() |
| 427 | #else |
| 428 | #define swiotlb_active() 0 |
| 429 | #endif |
| 430 | |
| 431 | static int |
| 432 | st_set_pages(struct sg_table **st, struct page **pvec, int num_pages) |
| 433 | { |
| 434 | struct scatterlist *sg; |
| 435 | int ret, n; |
| 436 | |
| 437 | *st = kmalloc(sizeof(**st), GFP_KERNEL); |
| 438 | if (*st == NULL) |
| 439 | return -ENOMEM; |
| 440 | |
| 441 | if (swiotlb_active()) { |
| 442 | ret = sg_alloc_table(*st, num_pages, GFP_KERNEL); |
| 443 | if (ret) |
| 444 | goto err; |
| 445 | |
| 446 | for_each_sg((*st)->sgl, sg, num_pages, n) |
| 447 | sg_set_page(sg, pvec[n], PAGE_SIZE, 0); |
| 448 | } else { |
| 449 | ret = sg_alloc_table_from_pages(*st, pvec, num_pages, |
| 450 | 0, num_pages << PAGE_SHIFT, |
| 451 | GFP_KERNEL); |
| 452 | if (ret) |
| 453 | goto err; |
| 454 | } |
| 455 | |
| 456 | return 0; |
| 457 | |
| 458 | err: |
| 459 | kfree(*st); |
| 460 | *st = NULL; |
| 461 | return ret; |
| 462 | } |
| 463 | |
| 464 | static void |
| 465 | __i915_gem_userptr_get_pages_worker(struct work_struct *_work) |
| 466 | { |
| 467 | struct get_pages_work *work = container_of(_work, typeof(*work), work); |
| 468 | struct drm_i915_gem_object *obj = work->obj; |
| 469 | struct drm_device *dev = obj->base.dev; |
| 470 | const int num_pages = obj->base.size >> PAGE_SHIFT; |
| 471 | struct page **pvec; |
| 472 | int pinned, ret; |
| 473 | |
| 474 | ret = -ENOMEM; |
| 475 | pinned = 0; |
| 476 | |
| 477 | pvec = kmalloc(num_pages*sizeof(struct page *), |
| 478 | GFP_TEMPORARY | __GFP_NOWARN | __GFP_NORETRY); |
| 479 | if (pvec == NULL) |
| 480 | pvec = drm_malloc_ab(num_pages, sizeof(struct page *)); |
| 481 | if (pvec != NULL) { |
| 482 | struct mm_struct *mm = obj->userptr.mm; |
| 483 | |
| 484 | down_read(&mm->mmap_sem); |
| 485 | while (pinned < num_pages) { |
| 486 | ret = get_user_pages(work->task, mm, |
| 487 | obj->userptr.ptr + pinned * PAGE_SIZE, |
| 488 | num_pages - pinned, |
| 489 | !obj->userptr.read_only, 0, |
| 490 | pvec + pinned, NULL); |
| 491 | if (ret < 0) |
| 492 | break; |
| 493 | |
| 494 | pinned += ret; |
| 495 | } |
| 496 | up_read(&mm->mmap_sem); |
| 497 | } |
| 498 | |
| 499 | mutex_lock(&dev->struct_mutex); |
| 500 | if (obj->userptr.work != &work->work) { |
| 501 | ret = 0; |
| 502 | } else if (pinned == num_pages) { |
| 503 | ret = st_set_pages(&obj->pages, pvec, num_pages); |
| 504 | if (ret == 0) { |
| 505 | list_add_tail(&obj->global_list, &to_i915(dev)->mm.unbound_list); |
| 506 | pinned = 0; |
| 507 | } |
| 508 | } |
| 509 | |
| 510 | obj->userptr.work = ERR_PTR(ret); |
| 511 | obj->userptr.workers--; |
| 512 | drm_gem_object_unreference(&obj->base); |
| 513 | mutex_unlock(&dev->struct_mutex); |
| 514 | |
| 515 | release_pages(pvec, pinned, 0); |
| 516 | drm_free_large(pvec); |
| 517 | |
| 518 | put_task_struct(work->task); |
| 519 | kfree(work); |
| 520 | } |
| 521 | |
| 522 | static int |
| 523 | i915_gem_userptr_get_pages(struct drm_i915_gem_object *obj) |
| 524 | { |
| 525 | const int num_pages = obj->base.size >> PAGE_SHIFT; |
| 526 | struct page **pvec; |
| 527 | int pinned, ret; |
| 528 | |
| 529 | /* If userspace should engineer that these pages are replaced in |
| 530 | * the vma between us binding this page into the GTT and completion |
| 531 | * of rendering... Their loss. If they change the mapping of their |
| 532 | * pages they need to create a new bo to point to the new vma. |
| 533 | * |
| 534 | * However, that still leaves open the possibility of the vma |
| 535 | * being copied upon fork. Which falls under the same userspace |
| 536 | * synchronisation issue as a regular bo, except that this time |
| 537 | * the process may not be expecting that a particular piece of |
| 538 | * memory is tied to the GPU. |
| 539 | * |
| 540 | * Fortunately, we can hook into the mmu_notifier in order to |
| 541 | * discard the page references prior to anything nasty happening |
| 542 | * to the vma (discard or cloning) which should prevent the more |
| 543 | * egregious cases from causing harm. |
| 544 | */ |
| 545 | |
| 546 | pvec = NULL; |
| 547 | pinned = 0; |
| 548 | if (obj->userptr.mm == current->mm) { |
| 549 | pvec = kmalloc(num_pages*sizeof(struct page *), |
| 550 | GFP_TEMPORARY | __GFP_NOWARN | __GFP_NORETRY); |
| 551 | if (pvec == NULL) { |
| 552 | pvec = drm_malloc_ab(num_pages, sizeof(struct page *)); |
| 553 | if (pvec == NULL) |
| 554 | return -ENOMEM; |
| 555 | } |
| 556 | |
| 557 | pinned = __get_user_pages_fast(obj->userptr.ptr, num_pages, |
| 558 | !obj->userptr.read_only, pvec); |
| 559 | } |
| 560 | if (pinned < num_pages) { |
| 561 | if (pinned < 0) { |
| 562 | ret = pinned; |
| 563 | pinned = 0; |
| 564 | } else { |
| 565 | /* Spawn a worker so that we can acquire the |
| 566 | * user pages without holding our mutex. Access |
| 567 | * to the user pages requires mmap_sem, and we have |
| 568 | * a strict lock ordering of mmap_sem, struct_mutex - |
| 569 | * we already hold struct_mutex here and so cannot |
| 570 | * call gup without encountering a lock inversion. |
| 571 | * |
| 572 | * Userspace will keep on repeating the operation |
| 573 | * (thanks to EAGAIN) until either we hit the fast |
| 574 | * path or the worker completes. If the worker is |
| 575 | * cancelled or superseded, the task is still run |
| 576 | * but the results ignored. (This leads to |
| 577 | * complications that we may have a stray object |
| 578 | * refcount that we need to be wary of when |
| 579 | * checking for existing objects during creation.) |
| 580 | * If the worker encounters an error, it reports |
| 581 | * that error back to this function through |
| 582 | * obj->userptr.work = ERR_PTR. |
| 583 | */ |
| 584 | ret = -EAGAIN; |
| 585 | if (obj->userptr.work == NULL && |
| 586 | obj->userptr.workers < I915_GEM_USERPTR_MAX_WORKERS) { |
| 587 | struct get_pages_work *work; |
| 588 | |
| 589 | work = kmalloc(sizeof(*work), GFP_KERNEL); |
| 590 | if (work != NULL) { |
| 591 | obj->userptr.work = &work->work; |
| 592 | obj->userptr.workers++; |
| 593 | |
| 594 | work->obj = obj; |
| 595 | drm_gem_object_reference(&obj->base); |
| 596 | |
| 597 | work->task = current; |
| 598 | get_task_struct(work->task); |
| 599 | |
| 600 | INIT_WORK(&work->work, __i915_gem_userptr_get_pages_worker); |
| 601 | schedule_work(&work->work); |
| 602 | } else |
| 603 | ret = -ENOMEM; |
| 604 | } else { |
| 605 | if (IS_ERR(obj->userptr.work)) { |
| 606 | ret = PTR_ERR(obj->userptr.work); |
| 607 | obj->userptr.work = NULL; |
| 608 | } |
| 609 | } |
| 610 | } |
| 611 | } else { |
| 612 | ret = st_set_pages(&obj->pages, pvec, num_pages); |
| 613 | if (ret == 0) { |
| 614 | obj->userptr.work = NULL; |
| 615 | pinned = 0; |
| 616 | } |
| 617 | } |
| 618 | |
| 619 | release_pages(pvec, pinned, 0); |
| 620 | drm_free_large(pvec); |
| 621 | return ret; |
| 622 | } |
| 623 | |
| 624 | static void |
| 625 | i915_gem_userptr_put_pages(struct drm_i915_gem_object *obj) |
| 626 | { |
| 627 | struct scatterlist *sg; |
| 628 | int i; |
| 629 | |
| 630 | BUG_ON(obj->userptr.work != NULL); |
| 631 | |
| 632 | if (obj->madv != I915_MADV_WILLNEED) |
| 633 | obj->dirty = 0; |
| 634 | |
| 635 | for_each_sg(obj->pages->sgl, sg, obj->pages->nents, i) { |
| 636 | struct page *page = sg_page(sg); |
| 637 | |
| 638 | if (obj->dirty) |
| 639 | set_page_dirty(page); |
| 640 | |
| 641 | mark_page_accessed(page); |
| 642 | page_cache_release(page); |
| 643 | } |
| 644 | obj->dirty = 0; |
| 645 | |
| 646 | sg_free_table(obj->pages); |
| 647 | kfree(obj->pages); |
| 648 | } |
| 649 | |
| 650 | static void |
| 651 | i915_gem_userptr_release(struct drm_i915_gem_object *obj) |
| 652 | { |
| 653 | i915_gem_userptr_release__mmu_notifier(obj); |
| 654 | |
| 655 | if (obj->userptr.mm) { |
| 656 | mmput(obj->userptr.mm); |
| 657 | obj->userptr.mm = NULL; |
| 658 | } |
| 659 | } |
| 660 | |
| 661 | static int |
| 662 | i915_gem_userptr_dmabuf_export(struct drm_i915_gem_object *obj) |
| 663 | { |
| 664 | if (obj->userptr.mn) |
| 665 | return 0; |
| 666 | |
| 667 | return i915_gem_userptr_init__mmu_notifier(obj, 0); |
| 668 | } |
| 669 | |
| 670 | static const struct drm_i915_gem_object_ops i915_gem_userptr_ops = { |
| 671 | .dmabuf_export = i915_gem_userptr_dmabuf_export, |
| 672 | .get_pages = i915_gem_userptr_get_pages, |
| 673 | .put_pages = i915_gem_userptr_put_pages, |
| 674 | .release = i915_gem_userptr_release, |
| 675 | }; |
| 676 | |
| 677 | /** |
| 678 | * Creates a new mm object that wraps some normal memory from the process |
| 679 | * context - user memory. |
| 680 | * |
| 681 | * We impose several restrictions upon the memory being mapped |
| 682 | * into the GPU. |
| 683 | * 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] | 684 | * 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] | 685 | * 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] | 686 | * 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] | 687 | * 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] | 688 | * 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] | 689 | * accessible directly by the CPU, but reads and writes by the GPU may |
| 690 | * incur the cost of a snoop (unless you have an LLC architecture). |
| 691 | * |
| 692 | * Synchronisation between multiple users and the GPU is left to userspace |
| 693 | * through the normal set-domain-ioctl. The kernel will enforce that the |
| 694 | * GPU relinquishes the VMA before it is returned back to the system |
| 695 | * i.e. upon free(), munmap() or process termination. However, the userspace |
| 696 | * malloc() library may not immediately relinquish the VMA after free() and |
| 697 | * instead reuse it whilst the GPU is still reading and writing to the VMA. |
| 698 | * Caveat emptor. |
| 699 | * |
| 700 | * Also note, that the object created here is not currently a "first class" |
| 701 | * object, in that several ioctls are banned. These are the CPU access |
| 702 | * ioctls: mmap(), pwrite and pread. In practice, you are expected to use |
| 703 | * direct access via your pointer rather than use those ioctls. |
| 704 | * |
| 705 | * If you think this is a good interface to use to pass GPU memory between |
| 706 | * drivers, please use dma-buf instead. In fact, wherever possible use |
| 707 | * dma-buf instead. |
| 708 | */ |
| 709 | int |
| 710 | i915_gem_userptr_ioctl(struct drm_device *dev, void *data, struct drm_file *file) |
| 711 | { |
| 712 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 713 | struct drm_i915_gem_userptr *args = data; |
| 714 | struct drm_i915_gem_object *obj; |
| 715 | int ret; |
| 716 | u32 handle; |
| 717 | |
| 718 | if (args->flags & ~(I915_USERPTR_READ_ONLY | |
| 719 | I915_USERPTR_UNSYNCHRONIZED)) |
| 720 | return -EINVAL; |
| 721 | |
| 722 | if (offset_in_page(args->user_ptr | args->user_size)) |
| 723 | return -EINVAL; |
| 724 | |
| 725 | if (args->user_size > dev_priv->gtt.base.total) |
| 726 | return -E2BIG; |
| 727 | |
| 728 | if (!access_ok(args->flags & I915_USERPTR_READ_ONLY ? VERIFY_READ : VERIFY_WRITE, |
| 729 | (char __user *)(unsigned long)args->user_ptr, args->user_size)) |
| 730 | return -EFAULT; |
| 731 | |
| 732 | if (args->flags & I915_USERPTR_READ_ONLY) { |
| 733 | /* On almost all of the current hw, we cannot tell the GPU that a |
| 734 | * page is readonly, so this is just a placeholder in the uAPI. |
| 735 | */ |
| 736 | return -ENODEV; |
| 737 | } |
| 738 | |
| 739 | /* Allocate the new object */ |
| 740 | obj = i915_gem_object_alloc(dev); |
| 741 | if (obj == NULL) |
| 742 | return -ENOMEM; |
| 743 | |
| 744 | drm_gem_private_object_init(dev, &obj->base, args->user_size); |
| 745 | i915_gem_object_init(obj, &i915_gem_userptr_ops); |
| 746 | obj->cache_level = I915_CACHE_LLC; |
| 747 | obj->base.write_domain = I915_GEM_DOMAIN_CPU; |
| 748 | obj->base.read_domains = I915_GEM_DOMAIN_CPU; |
| 749 | |
| 750 | obj->userptr.ptr = args->user_ptr; |
| 751 | obj->userptr.read_only = !!(args->flags & I915_USERPTR_READ_ONLY); |
| 752 | |
| 753 | /* And keep a pointer to the current->mm for resolving the user pages |
| 754 | * at binding. This means that we need to hook into the mmu_notifier |
| 755 | * in order to detect if the mmu is destroyed. |
| 756 | */ |
| 757 | ret = -ENOMEM; |
| 758 | if ((obj->userptr.mm = get_task_mm(current))) |
| 759 | ret = i915_gem_userptr_init__mmu_notifier(obj, args->flags); |
| 760 | if (ret == 0) |
| 761 | ret = drm_gem_handle_create(file, &obj->base, &handle); |
| 762 | |
| 763 | /* drop reference from allocate - handle holds it now */ |
| 764 | drm_gem_object_unreference_unlocked(&obj->base); |
| 765 | if (ret) |
| 766 | return ret; |
| 767 | |
| 768 | args->handle = handle; |
| 769 | return 0; |
| 770 | } |
| 771 | |
| 772 | int |
| 773 | i915_gem_init_userptr(struct drm_device *dev) |
| 774 | { |
| 775 | #if defined(CONFIG_MMU_NOTIFIER) |
| 776 | struct drm_i915_private *dev_priv = to_i915(dev); |
| 777 | hash_init(dev_priv->mmu_notifiers); |
| 778 | #endif |
| 779 | return 0; |
| 780 | } |