hc
2024-12-19 9370bb92b2d16684ee45cf24e879c93c509162da
kernel/drivers/xen/gntdev.c
....@@ -22,6 +22,7 @@
2222
2323 #define pr_fmt(fmt) "xen:" KBUILD_MODNAME ": " fmt
2424
25
+#include <linux/dma-mapping.h>
2526 #include <linux/module.h>
2627 #include <linux/kernel.h>
2728 #include <linux/init.h>
....@@ -34,9 +35,7 @@
3435 #include <linux/slab.h>
3536 #include <linux/highmem.h>
3637 #include <linux/refcount.h>
37
-#ifdef CONFIG_XEN_GRANT_DMA_ALLOC
38
-#include <linux/of_device.h>
39
-#endif
38
+#include <linux/workqueue.h>
4039
4140 #include <xen/xen.h>
4241 #include <xen/grant_table.h>
....@@ -57,26 +56,24 @@
5756 "Gerd Hoffmann <kraxel@redhat.com>");
5857 MODULE_DESCRIPTION("User-space granted page access driver");
5958
60
-static int limit = 1024*1024;
61
-module_param(limit, int, 0644);
62
-MODULE_PARM_DESC(limit, "Maximum number of grants that may be mapped by "
63
- "the gntdev device");
59
+static unsigned int limit = 64*1024;
60
+module_param(limit, uint, 0644);
61
+MODULE_PARM_DESC(limit,
62
+ "Maximum number of grants that may be mapped by one mapping request");
6463
65
-static atomic_t pages_mapped = ATOMIC_INIT(0);
66
-
64
+/* True in PV mode, false otherwise */
6765 static int use_ptemod;
68
-#define populate_freeable_maps use_ptemod
6966
70
-static int unmap_grant_pages(struct gntdev_grant_map *map,
71
- int offset, int pages);
67
+static void unmap_grant_pages(struct gntdev_grant_map *map,
68
+ int offset, int pages);
7269
7370 static struct miscdevice gntdev_miscdev;
7471
7572 /* ------------------------------------------------------------------ */
7673
77
-bool gntdev_account_mapped_pages(int count)
74
+bool gntdev_test_page_count(unsigned int count)
7875 {
79
- return atomic_add_return(count, &pages_mapped) > limit;
76
+ return !count || count > limit;
8077 }
8178
8279 static void gntdev_print_maps(struct gntdev_priv *priv,
....@@ -117,14 +114,15 @@
117114 gnttab_free_pages(map->count, map->pages);
118115
119116 #ifdef CONFIG_XEN_GRANT_DMA_ALLOC
120
- kfree(map->frames);
117
+ kvfree(map->frames);
121118 #endif
122
- kfree(map->pages);
123
- kfree(map->grants);
124
- kfree(map->map_ops);
125
- kfree(map->unmap_ops);
126
- kfree(map->kmap_ops);
127
- kfree(map->kunmap_ops);
119
+ kvfree(map->pages);
120
+ kvfree(map->grants);
121
+ kvfree(map->map_ops);
122
+ kvfree(map->unmap_ops);
123
+ kvfree(map->kmap_ops);
124
+ kvfree(map->kunmap_ops);
125
+ kvfree(map->being_removed);
128126 kfree(map);
129127 }
130128
....@@ -138,18 +136,22 @@
138136 if (NULL == add)
139137 return NULL;
140138
141
- add->grants = kcalloc(count, sizeof(add->grants[0]), GFP_KERNEL);
142
- add->map_ops = kcalloc(count, sizeof(add->map_ops[0]), GFP_KERNEL);
143
- add->unmap_ops = kcalloc(count, sizeof(add->unmap_ops[0]), GFP_KERNEL);
144
- add->kmap_ops = kcalloc(count, sizeof(add->kmap_ops[0]), GFP_KERNEL);
145
- add->kunmap_ops = kcalloc(count, sizeof(add->kunmap_ops[0]), GFP_KERNEL);
146
- add->pages = kcalloc(count, sizeof(add->pages[0]), GFP_KERNEL);
139
+ add->grants = kvcalloc(count, sizeof(add->grants[0]), GFP_KERNEL);
140
+ add->map_ops = kvcalloc(count, sizeof(add->map_ops[0]), GFP_KERNEL);
141
+ add->unmap_ops = kvcalloc(count, sizeof(add->unmap_ops[0]), GFP_KERNEL);
142
+ add->kmap_ops = kvcalloc(count, sizeof(add->kmap_ops[0]), GFP_KERNEL);
143
+ add->kunmap_ops = kvcalloc(count,
144
+ sizeof(add->kunmap_ops[0]), GFP_KERNEL);
145
+ add->pages = kvcalloc(count, sizeof(add->pages[0]), GFP_KERNEL);
146
+ add->being_removed =
147
+ kvcalloc(count, sizeof(add->being_removed[0]), GFP_KERNEL);
147148 if (NULL == add->grants ||
148149 NULL == add->map_ops ||
149150 NULL == add->unmap_ops ||
150151 NULL == add->kmap_ops ||
151152 NULL == add->kunmap_ops ||
152
- NULL == add->pages)
153
+ NULL == add->pages ||
154
+ NULL == add->being_removed)
153155 goto err;
154156
155157 #ifdef CONFIG_XEN_GRANT_DMA_ALLOC
....@@ -162,8 +164,8 @@
162164 if (dma_flags & (GNTDEV_DMA_FLAG_WC | GNTDEV_DMA_FLAG_COHERENT)) {
163165 struct gnttab_dma_alloc_args args;
164166
165
- add->frames = kcalloc(count, sizeof(add->frames[0]),
166
- GFP_KERNEL);
167
+ add->frames = kvcalloc(count, sizeof(add->frames[0]),
168
+ GFP_KERNEL);
167169 if (!add->frames)
168170 goto err;
169171
....@@ -244,44 +246,58 @@
244246 if (!refcount_dec_and_test(&map->users))
245247 return;
246248
247
- atomic_sub(map->count, &pages_mapped);
249
+ if (map->pages && !use_ptemod) {
250
+ /*
251
+ * Increment the reference count. This ensures that the
252
+ * subsequent call to unmap_grant_pages() will not wind up
253
+ * re-entering itself. It *can* wind up calling
254
+ * gntdev_put_map() recursively, but such calls will be with a
255
+ * reference count greater than 1, so they will return before
256
+ * this code is reached. The recursion depth is thus limited to
257
+ * 1. Do NOT use refcount_inc() here, as it will detect that
258
+ * the reference count is zero and WARN().
259
+ */
260
+ refcount_set(&map->users, 1);
261
+
262
+ /*
263
+ * Unmap the grants. This may or may not be asynchronous, so it
264
+ * is possible that the reference count is 1 on return, but it
265
+ * could also be greater than 1.
266
+ */
267
+ unmap_grant_pages(map, 0, map->count);
268
+
269
+ /* Check if the memory now needs to be freed */
270
+ if (!refcount_dec_and_test(&map->users))
271
+ return;
272
+
273
+ /*
274
+ * All pages have been returned to the hypervisor, so free the
275
+ * map.
276
+ */
277
+ }
278
+
279
+ if (use_ptemod && map->notifier_init)
280
+ mmu_interval_notifier_remove(&map->notifier);
248281
249282 if (map->notify.flags & UNMAP_NOTIFY_SEND_EVENT) {
250283 notify_remote_via_evtchn(map->notify.event);
251284 evtchn_put(map->notify.event);
252285 }
253
-
254
- if (populate_freeable_maps && priv) {
255
- mutex_lock(&priv->lock);
256
- list_del(&map->next);
257
- mutex_unlock(&priv->lock);
258
- }
259
-
260
- if (map->pages && !use_ptemod)
261
- unmap_grant_pages(map, 0, map->count);
262286 gntdev_free_map(map);
263287 }
264288
265289 /* ------------------------------------------------------------------ */
266290
267
-static int find_grant_ptes(pte_t *pte, pgtable_t token,
268
- unsigned long addr, void *data)
291
+static int find_grant_ptes(pte_t *pte, unsigned long addr, void *data)
269292 {
270293 struct gntdev_grant_map *map = data;
271
- unsigned int pgnr = (addr - map->vma->vm_start) >> PAGE_SHIFT;
272
- int flags = map->flags | GNTMAP_application_map | GNTMAP_contains_pte;
294
+ unsigned int pgnr = (addr - map->pages_vm_start) >> PAGE_SHIFT;
295
+ int flags = map->flags | GNTMAP_application_map | GNTMAP_contains_pte |
296
+ (1 << _GNTMAP_guest_avail0);
273297 u64 pte_maddr;
274298
275299 BUG_ON(pgnr >= map->count);
276300 pte_maddr = arbitrary_virt_to_machine(pte).maddr;
277
-
278
- /*
279
- * Set the PTE as special to force get_user_pages_fast() fall
280
- * back to the slow path. If this is not supported as part of
281
- * the grant map, it will be done afterwards.
282
- */
283
- if (xen_feature(XENFEAT_gnttab_map_avail_bits))
284
- flags |= (1 << _GNTMAP_guest_avail0);
285301
286302 gnttab_set_map_op(&map->map_ops[pgnr], pte_maddr, flags,
287303 map->grants[pgnr].ref,
....@@ -291,17 +307,9 @@
291307 return 0;
292308 }
293309
294
-#ifdef CONFIG_X86
295
-static int set_grant_ptes_as_special(pte_t *pte, pgtable_t token,
296
- unsigned long addr, void *data)
297
-{
298
- set_pte_at(current->mm, addr, pte, pte_mkspecial(*pte));
299
- return 0;
300
-}
301
-#endif
302
-
303310 int gntdev_map_grant_pages(struct gntdev_grant_map *map)
304311 {
312
+ size_t alloced = 0;
305313 int i, err = 0;
306314
307315 if (!use_ptemod) {
....@@ -350,87 +358,130 @@
350358 map->pages, map->count);
351359
352360 for (i = 0; i < map->count; i++) {
353
- if (map->map_ops[i].status == GNTST_okay)
361
+ if (map->map_ops[i].status == GNTST_okay) {
354362 map->unmap_ops[i].handle = map->map_ops[i].handle;
355
- else if (!err)
363
+ alloced++;
364
+ } else if (!err)
356365 err = -EINVAL;
357366
358367 if (map->flags & GNTMAP_device_map)
359368 map->unmap_ops[i].dev_bus_addr = map->map_ops[i].dev_bus_addr;
360369
361370 if (use_ptemod) {
362
- if (map->kmap_ops[i].status == GNTST_okay)
371
+ if (map->kmap_ops[i].status == GNTST_okay) {
372
+ alloced++;
363373 map->kunmap_ops[i].handle = map->kmap_ops[i].handle;
364
- else if (!err)
374
+ } else if (!err)
365375 err = -EINVAL;
366376 }
367377 }
378
+ atomic_add(alloced, &map->live_grants);
368379 return err;
369380 }
370381
371
-static int __unmap_grant_pages(struct gntdev_grant_map *map, int offset,
382
+static void __unmap_grant_pages_done(int result,
383
+ struct gntab_unmap_queue_data *data)
384
+{
385
+ unsigned int i;
386
+ struct gntdev_grant_map *map = data->data;
387
+ unsigned int offset = data->unmap_ops - map->unmap_ops;
388
+ int successful_unmaps = 0;
389
+ int live_grants;
390
+
391
+ for (i = 0; i < data->count; i++) {
392
+ if (map->unmap_ops[offset + i].status == GNTST_okay &&
393
+ map->unmap_ops[offset + i].handle != -1)
394
+ successful_unmaps++;
395
+
396
+ WARN_ON(map->unmap_ops[offset+i].status &&
397
+ map->unmap_ops[offset+i].handle != -1);
398
+ pr_debug("unmap handle=%d st=%d\n",
399
+ map->unmap_ops[offset+i].handle,
400
+ map->unmap_ops[offset+i].status);
401
+ map->unmap_ops[offset+i].handle = -1;
402
+ if (use_ptemod) {
403
+ if (map->kunmap_ops[offset + i].status == GNTST_okay &&
404
+ map->kunmap_ops[offset + i].handle != -1)
405
+ successful_unmaps++;
406
+
407
+ WARN_ON(map->kunmap_ops[offset+i].status &&
408
+ map->kunmap_ops[offset+i].handle != -1);
409
+ pr_debug("kunmap handle=%u st=%d\n",
410
+ map->kunmap_ops[offset+i].handle,
411
+ map->kunmap_ops[offset+i].status);
412
+ map->kunmap_ops[offset+i].handle = -1;
413
+ }
414
+ }
415
+
416
+ /*
417
+ * Decrease the live-grant counter. This must happen after the loop to
418
+ * prevent premature reuse of the grants by gnttab_mmap().
419
+ */
420
+ live_grants = atomic_sub_return(successful_unmaps, &map->live_grants);
421
+ if (WARN_ON(live_grants < 0))
422
+ pr_err("%s: live_grants became negative (%d) after unmapping %d pages!\n",
423
+ __func__, live_grants, successful_unmaps);
424
+
425
+ /* Release reference taken by __unmap_grant_pages */
426
+ gntdev_put_map(NULL, map);
427
+}
428
+
429
+static void __unmap_grant_pages(struct gntdev_grant_map *map, int offset,
372430 int pages)
373431 {
374
- int i, err = 0;
375
- struct gntab_unmap_queue_data unmap_data;
376
-
377432 if (map->notify.flags & UNMAP_NOTIFY_CLEAR_BYTE) {
378433 int pgno = (map->notify.addr >> PAGE_SHIFT);
434
+
379435 if (pgno >= offset && pgno < offset + pages) {
380436 /* No need for kmap, pages are in lowmem */
381437 uint8_t *tmp = pfn_to_kaddr(page_to_pfn(map->pages[pgno]));
438
+
382439 tmp[map->notify.addr & (PAGE_SIZE-1)] = 0;
383440 map->notify.flags &= ~UNMAP_NOTIFY_CLEAR_BYTE;
384441 }
385442 }
386443
387
- unmap_data.unmap_ops = map->unmap_ops + offset;
388
- unmap_data.kunmap_ops = use_ptemod ? map->kunmap_ops + offset : NULL;
389
- unmap_data.pages = map->pages + offset;
390
- unmap_data.count = pages;
444
+ map->unmap_data.unmap_ops = map->unmap_ops + offset;
445
+ map->unmap_data.kunmap_ops = use_ptemod ? map->kunmap_ops + offset : NULL;
446
+ map->unmap_data.pages = map->pages + offset;
447
+ map->unmap_data.count = pages;
448
+ map->unmap_data.done = __unmap_grant_pages_done;
449
+ map->unmap_data.data = map;
450
+ refcount_inc(&map->users); /* to keep map alive during async call below */
391451
392
- err = gnttab_unmap_refs_sync(&unmap_data);
393
- if (err)
394
- return err;
395
-
396
- for (i = 0; i < pages; i++) {
397
- if (map->unmap_ops[offset+i].status)
398
- err = -EINVAL;
399
- pr_debug("unmap handle=%d st=%d\n",
400
- map->unmap_ops[offset+i].handle,
401
- map->unmap_ops[offset+i].status);
402
- map->unmap_ops[offset+i].handle = -1;
403
- }
404
- return err;
452
+ gnttab_unmap_refs_async(&map->unmap_data);
405453 }
406454
407
-static int unmap_grant_pages(struct gntdev_grant_map *map, int offset,
408
- int pages)
455
+static void unmap_grant_pages(struct gntdev_grant_map *map, int offset,
456
+ int pages)
409457 {
410
- int range, err = 0;
458
+ int range;
459
+
460
+ if (atomic_read(&map->live_grants) == 0)
461
+ return; /* Nothing to do */
411462
412463 pr_debug("unmap %d+%d [%d+%d]\n", map->index, map->count, offset, pages);
413464
414465 /* It is possible the requested range will have a "hole" where we
415466 * already unmapped some of the grants. Only unmap valid ranges.
416467 */
417
- while (pages && !err) {
418
- while (pages && map->unmap_ops[offset].handle == -1) {
468
+ while (pages) {
469
+ while (pages && map->being_removed[offset]) {
419470 offset++;
420471 pages--;
421472 }
422473 range = 0;
423474 while (range < pages) {
424
- if (map->unmap_ops[offset+range].handle == -1)
475
+ if (map->being_removed[offset + range])
425476 break;
477
+ map->being_removed[offset + range] = true;
426478 range++;
427479 }
428
- err = __unmap_grant_pages(map, offset, range);
480
+ if (range)
481
+ __unmap_grant_pages(map, offset, range);
429482 offset += range;
430483 pages -= range;
431484 }
432
-
433
- return err;
434485 }
435486
436487 /* ------------------------------------------------------------------ */
....@@ -450,18 +501,7 @@
450501 struct gntdev_priv *priv = file->private_data;
451502
452503 pr_debug("gntdev_vma_close %p\n", vma);
453
- if (use_ptemod) {
454
- /* It is possible that an mmu notifier could be running
455
- * concurrently, so take priv->lock to ensure that the vma won't
456
- * vanishing during the unmap_grant_pages call, since we will
457
- * spin here until that completes. Such a concurrent call will
458
- * not do any unmapping, since that has been done prior to
459
- * closing the vma, but it may still iterate the unmap_ops list.
460
- */
461
- mutex_lock(&priv->lock);
462
- map->vma = NULL;
463
- mutex_unlock(&priv->lock);
464
- }
504
+
465505 vma->vm_private_data = NULL;
466506 gntdev_put_map(priv, map);
467507 }
....@@ -482,109 +522,43 @@
482522
483523 /* ------------------------------------------------------------------ */
484524
485
-static bool in_range(struct gntdev_grant_map *map,
486
- unsigned long start, unsigned long end)
525
+static bool gntdev_invalidate(struct mmu_interval_notifier *mn,
526
+ const struct mmu_notifier_range *range,
527
+ unsigned long cur_seq)
487528 {
488
- if (!map->vma)
529
+ struct gntdev_grant_map *map =
530
+ container_of(mn, struct gntdev_grant_map, notifier);
531
+ unsigned long mstart, mend;
532
+ unsigned long map_start, map_end;
533
+
534
+ if (!mmu_notifier_range_blockable(range))
489535 return false;
490
- if (map->vma->vm_start >= end)
491
- return false;
492
- if (map->vma->vm_end <= start)
493
- return false;
536
+
537
+ map_start = map->pages_vm_start;
538
+ map_end = map->pages_vm_start + (map->count << PAGE_SHIFT);
539
+
540
+ /*
541
+ * If the VMA is split or otherwise changed the notifier is not
542
+ * updated, but we don't want to process VA's outside the modified
543
+ * VMA. FIXME: It would be much more understandable to just prevent
544
+ * modifying the VMA in the first place.
545
+ */
546
+ if (map_start >= range->end || map_end <= range->start)
547
+ return true;
548
+
549
+ mstart = max(range->start, map_start);
550
+ mend = min(range->end, map_end);
551
+ pr_debug("map %d+%d (%lx %lx), range %lx %lx, mrange %lx %lx\n",
552
+ map->index, map->count, map_start, map_end,
553
+ range->start, range->end, mstart, mend);
554
+ unmap_grant_pages(map, (mstart - map_start) >> PAGE_SHIFT,
555
+ (mend - mstart) >> PAGE_SHIFT);
494556
495557 return true;
496558 }
497559
498
-static int unmap_if_in_range(struct gntdev_grant_map *map,
499
- unsigned long start, unsigned long end,
500
- bool blockable)
501
-{
502
- unsigned long mstart, mend;
503
- int err;
504
-
505
- if (!in_range(map, start, end))
506
- return 0;
507
-
508
- if (!blockable)
509
- return -EAGAIN;
510
-
511
- mstart = max(start, map->vma->vm_start);
512
- mend = min(end, map->vma->vm_end);
513
- pr_debug("map %d+%d (%lx %lx), range %lx %lx, mrange %lx %lx\n",
514
- map->index, map->count,
515
- map->vma->vm_start, map->vma->vm_end,
516
- start, end, mstart, mend);
517
- err = unmap_grant_pages(map,
518
- (mstart - map->vma->vm_start) >> PAGE_SHIFT,
519
- (mend - mstart) >> PAGE_SHIFT);
520
- WARN_ON(err);
521
-
522
- return 0;
523
-}
524
-
525
-static int mn_invl_range_start(struct mmu_notifier *mn,
526
- struct mm_struct *mm,
527
- unsigned long start, unsigned long end,
528
- bool blockable)
529
-{
530
- struct gntdev_priv *priv = container_of(mn, struct gntdev_priv, mn);
531
- struct gntdev_grant_map *map;
532
- int ret = 0;
533
-
534
- if (blockable)
535
- mutex_lock(&priv->lock);
536
- else if (!mutex_trylock(&priv->lock))
537
- return -EAGAIN;
538
-
539
- list_for_each_entry(map, &priv->maps, next) {
540
- ret = unmap_if_in_range(map, start, end, blockable);
541
- if (ret)
542
- goto out_unlock;
543
- }
544
- list_for_each_entry(map, &priv->freeable_maps, next) {
545
- ret = unmap_if_in_range(map, start, end, blockable);
546
- if (ret)
547
- goto out_unlock;
548
- }
549
-
550
-out_unlock:
551
- mutex_unlock(&priv->lock);
552
-
553
- return ret;
554
-}
555
-
556
-static void mn_release(struct mmu_notifier *mn,
557
- struct mm_struct *mm)
558
-{
559
- struct gntdev_priv *priv = container_of(mn, struct gntdev_priv, mn);
560
- struct gntdev_grant_map *map;
561
- int err;
562
-
563
- mutex_lock(&priv->lock);
564
- list_for_each_entry(map, &priv->maps, next) {
565
- if (!map->vma)
566
- continue;
567
- pr_debug("map %d+%d (%lx %lx)\n",
568
- map->index, map->count,
569
- map->vma->vm_start, map->vma->vm_end);
570
- err = unmap_grant_pages(map, /* offset */ 0, map->count);
571
- WARN_ON(err);
572
- }
573
- list_for_each_entry(map, &priv->freeable_maps, next) {
574
- if (!map->vma)
575
- continue;
576
- pr_debug("map %d+%d (%lx %lx)\n",
577
- map->index, map->count,
578
- map->vma->vm_start, map->vma->vm_end);
579
- err = unmap_grant_pages(map, /* offset */ 0, map->count);
580
- WARN_ON(err);
581
- }
582
- mutex_unlock(&priv->lock);
583
-}
584
-
585
-static const struct mmu_notifier_ops gntdev_mmu_ops = {
586
- .release = mn_release,
587
- .invalidate_range_start = mn_invl_range_start,
560
+static const struct mmu_interval_notifier_ops gntdev_mmu_ops = {
561
+ .invalidate = gntdev_invalidate,
588562 };
589563
590564 /* ------------------------------------------------------------------ */
....@@ -592,52 +566,28 @@
592566 static int gntdev_open(struct inode *inode, struct file *flip)
593567 {
594568 struct gntdev_priv *priv;
595
- int ret = 0;
596569
597570 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
598571 if (!priv)
599572 return -ENOMEM;
600573
601574 INIT_LIST_HEAD(&priv->maps);
602
- INIT_LIST_HEAD(&priv->freeable_maps);
603575 mutex_init(&priv->lock);
604576
605577 #ifdef CONFIG_XEN_GNTDEV_DMABUF
606578 priv->dmabuf_priv = gntdev_dmabuf_init(flip);
607579 if (IS_ERR(priv->dmabuf_priv)) {
608
- ret = PTR_ERR(priv->dmabuf_priv);
580
+ int ret = PTR_ERR(priv->dmabuf_priv);
581
+
609582 kfree(priv);
610583 return ret;
611584 }
612585 #endif
613586
614
- if (use_ptemod) {
615
- priv->mm = get_task_mm(current);
616
- if (!priv->mm) {
617
- kfree(priv);
618
- return -ENOMEM;
619
- }
620
- priv->mn.ops = &gntdev_mmu_ops;
621
- ret = mmu_notifier_register(&priv->mn, priv->mm);
622
- mmput(priv->mm);
623
- }
624
-
625
- if (ret) {
626
- kfree(priv);
627
- return ret;
628
- }
629
-
630587 flip->private_data = priv;
631588 #ifdef CONFIG_XEN_GRANT_DMA_ALLOC
632589 priv->dma_dev = gntdev_miscdev.this_device;
633
-
634
- /*
635
- * The device is not spawn from a device tree, so arch_setup_dma_ops
636
- * is not called, thus leaving the device with dummy DMA ops.
637
- * Fix this by calling of_dma_configure() with a NULL node to set
638
- * default DMA ops.
639
- */
640
- of_dma_configure(priv->dma_dev, NULL, true);
590
+ dma_coerce_mask_and_coherent(priv->dma_dev, DMA_BIT_MASK(64));
641591 #endif
642592 pr_debug("priv %p\n", priv);
643593
....@@ -658,15 +608,11 @@
658608 list_del(&map->next);
659609 gntdev_put_map(NULL /* already removed */, map);
660610 }
661
- WARN_ON(!list_empty(&priv->freeable_maps));
662611 mutex_unlock(&priv->lock);
663612
664613 #ifdef CONFIG_XEN_GNTDEV_DMABUF
665614 gntdev_dmabuf_fini(priv->dmabuf_priv);
666615 #endif
667
-
668
- if (use_ptemod)
669
- mmu_notifier_unregister(&priv->mn, priv->mm);
670616
671617 kfree(priv);
672618 return 0;
....@@ -682,19 +628,13 @@
682628 if (copy_from_user(&op, u, sizeof(op)) != 0)
683629 return -EFAULT;
684630 pr_debug("priv %p, add %d\n", priv, op.count);
685
- if (unlikely(op.count <= 0))
631
+ if (unlikely(gntdev_test_page_count(op.count)))
686632 return -EINVAL;
687633
688634 err = -ENOMEM;
689635 map = gntdev_alloc_map(priv, op.count, 0 /* This is not a dma-buf. */);
690636 if (!map)
691637 return err;
692
-
693
- if (unlikely(gntdev_account_mapped_pages(op.count))) {
694
- pr_debug("can't map: over limit\n");
695
- gntdev_put_map(NULL, map);
696
- return err;
697
- }
698638
699639 if (copy_from_user(map->grants, &u->refs,
700640 sizeof(map->grants[0]) * op.count) != 0) {
....@@ -728,8 +668,6 @@
728668 map = gntdev_find_map_index(priv, op.index >> PAGE_SHIFT, op.count);
729669 if (map) {
730670 list_del(&map->next);
731
- if (populate_freeable_maps)
732
- list_add_tail(&map->next, &priv->freeable_maps);
733671 err = 0;
734672 }
735673 mutex_unlock(&priv->lock);
....@@ -750,7 +688,7 @@
750688 return -EFAULT;
751689 pr_debug("priv %p, offset for vaddr %lx\n", priv, (unsigned long)op.vaddr);
752690
753
- down_read(&current->mm->mmap_sem);
691
+ mmap_read_lock(current->mm);
754692 vma = find_vma(current->mm, op.vaddr);
755693 if (!vma || vma->vm_ops != &gntdev_vmops)
756694 goto out_unlock;
....@@ -764,7 +702,7 @@
764702 rv = 0;
765703
766704 out_unlock:
767
- up_read(&current->mm->mmap_sem);
705
+ mmap_read_unlock(current->mm);
768706
769707 if (rv == 0 && copy_to_user(u, &op, sizeof(op)) != 0)
770708 return -EFAULT;
....@@ -777,7 +715,7 @@
777715 struct gntdev_grant_map *map;
778716 int rc;
779717 int out_flags;
780
- unsigned int out_event;
718
+ evtchn_port_t out_event;
781719
782720 if (copy_from_user(&op, u, sizeof(op)))
783721 return -EFAULT;
....@@ -856,7 +794,7 @@
856794 unsigned long xen_pfn;
857795 int ret;
858796
859
- ret = get_user_pages_fast(addr, 1, batch->writeable, &page);
797
+ ret = pin_user_pages_fast(addr, 1, batch->writeable ? FOLL_WRITE : 0, &page);
860798 if (ret < 0)
861799 return ret;
862800
....@@ -870,13 +808,7 @@
870808
871809 static void gntdev_put_pages(struct gntdev_copy_batch *batch)
872810 {
873
- unsigned int i;
874
-
875
- for (i = 0; i < batch->nr_pages; i++) {
876
- if (batch->writeable && !PageDirty(batch->pages[i]))
877
- set_page_dirty_lock(batch->pages[i]);
878
- put_page(batch->pages[i]);
879
- }
811
+ unpin_user_pages_dirty_lock(batch->pages, batch->nr_pages, batch->writeable);
880812 batch->nr_pages = 0;
881813 batch->writeable = false;
882814 }
....@@ -1094,24 +1026,20 @@
10941026 int index = vma->vm_pgoff;
10951027 int count = vma_pages(vma);
10961028 struct gntdev_grant_map *map;
1097
- int i, err = -EINVAL;
1029
+ int err = -EINVAL;
10981030
10991031 if ((vma->vm_flags & VM_WRITE) && !(vma->vm_flags & VM_SHARED))
11001032 return -EINVAL;
11011033
11021034 pr_debug("map %d+%d at %lx (pgoff %lx)\n",
1103
- index, count, vma->vm_start, vma->vm_pgoff);
1035
+ index, count, vma->vm_start, vma->vm_pgoff);
11041036
11051037 mutex_lock(&priv->lock);
11061038 map = gntdev_find_map_index(priv, index, count);
11071039 if (!map)
11081040 goto unlock_out;
1109
- if (use_ptemod && map->vma)
1041
+ if (!atomic_add_unless(&map->in_use, 1, 1))
11101042 goto unlock_out;
1111
- if (use_ptemod && priv->mm != vma->vm_mm) {
1112
- pr_warn("Huh? Other mm?\n");
1113
- goto unlock_out;
1114
- }
11151043
11161044 refcount_inc(&map->users);
11171045
....@@ -1123,10 +1051,6 @@
11231051 vma->vm_flags |= VM_DONTCOPY;
11241052
11251053 vma->vm_private_data = map;
1126
-
1127
- if (use_ptemod)
1128
- map->vma = vma;
1129
-
11301054 if (map->flags) {
11311055 if ((vma->vm_flags & VM_WRITE) &&
11321056 (map->flags & GNTMAP_readonly))
....@@ -1137,10 +1061,32 @@
11371061 map->flags |= GNTMAP_readonly;
11381062 }
11391063
1064
+ map->pages_vm_start = vma->vm_start;
1065
+
1066
+ if (use_ptemod) {
1067
+ err = mmu_interval_notifier_insert_locked(
1068
+ &map->notifier, vma->vm_mm, vma->vm_start,
1069
+ vma->vm_end - vma->vm_start, &gntdev_mmu_ops);
1070
+ if (err)
1071
+ goto out_unlock_put;
1072
+
1073
+ map->notifier_init = true;
1074
+ }
11401075 mutex_unlock(&priv->lock);
11411076
11421077 if (use_ptemod) {
1143
- map->pages_vm_start = vma->vm_start;
1078
+ /*
1079
+ * gntdev takes the address of the PTE in find_grant_ptes() and
1080
+ * passes it to the hypervisor in gntdev_map_grant_pages(). The
1081
+ * purpose of the notifier is to prevent the hypervisor pointer
1082
+ * to the PTE from going stale.
1083
+ *
1084
+ * Since this vma's mappings can't be touched without the
1085
+ * mmap_lock, and we are holding it now, there is no need for
1086
+ * the notifier_range locking pattern.
1087
+ */
1088
+ mmu_interval_read_begin(&map->notifier);
1089
+
11441090 err = apply_to_page_range(vma->vm_mm, vma->vm_start,
11451091 vma->vm_end - vma->vm_start,
11461092 find_grant_ptes, map);
....@@ -1155,29 +1101,9 @@
11551101 goto out_put_map;
11561102
11571103 if (!use_ptemod) {
1158
- for (i = 0; i < count; i++) {
1159
- err = vm_insert_page(vma, vma->vm_start + i*PAGE_SIZE,
1160
- map->pages[i]);
1161
- if (err)
1162
- goto out_put_map;
1163
- }
1164
- } else {
1165
-#ifdef CONFIG_X86
1166
- /*
1167
- * If the PTEs were not made special by the grant map
1168
- * hypercall, do so here.
1169
- *
1170
- * This is racy since the mapping is already visible
1171
- * to userspace but userspace should be well-behaved
1172
- * enough to not touch it until the mmap() call
1173
- * returns.
1174
- */
1175
- if (!xen_feature(XENFEAT_gnttab_map_avail_bits)) {
1176
- apply_to_page_range(vma->vm_mm, vma->vm_start,
1177
- vma->vm_end - vma->vm_start,
1178
- set_grant_ptes_as_special, NULL);
1179
- }
1180
-#endif
1104
+ err = vm_map_pages_zero(vma, map->pages, map->count);
1105
+ if (err)
1106
+ goto out_put_map;
11811107 }
11821108
11831109 return 0;
....@@ -1189,10 +1115,8 @@
11891115 out_unlock_put:
11901116 mutex_unlock(&priv->lock);
11911117 out_put_map:
1192
- if (use_ptemod) {
1193
- map->vma = NULL;
1118
+ if (use_ptemod)
11941119 unmap_grant_pages(map, 0, map->count);
1195
- }
11961120 gntdev_put_map(priv, map);
11971121 return err;
11981122 }