1 /******************************************************************************
4 * Device for accessing (in user-space) pages that have been granted by other
7 * Copyright (c) 2006-2007, D G Murray.
8 * (c) 2009 Gerd Hoffmann <kraxel@redhat.com>
9 * (c) 2018 Oleksandr Andrushchenko, EPAM Systems Inc.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #define pr_fmt(fmt) "xen:" KBUILD_MODNAME ": " fmt
25 #include <linux/module.h>
26 #include <linux/kernel.h>
27 #include <linux/init.h>
28 #include <linux/miscdevice.h>
30 #include <linux/uaccess.h>
31 #include <linux/sched.h>
32 #include <linux/sched/mm.h>
33 #include <linux/spinlock.h>
34 #include <linux/slab.h>
35 #include <linux/highmem.h>
36 #include <linux/refcount.h>
37 #ifdef CONFIG_XEN_GRANT_DMA_ALLOC
38 #include <linux/of_device.h>
42 #include <xen/grant_table.h>
43 #include <xen/balloon.h>
44 #include <xen/gntdev.h>
45 #include <xen/events.h>
47 #include <asm/xen/hypervisor.h>
48 #include <asm/xen/hypercall.h>
50 #include "gntdev-common.h"
52 MODULE_LICENSE("GPL");
53 MODULE_AUTHOR("Derek G. Murray <Derek.Murray@cl.cam.ac.uk>, "
54 "Gerd Hoffmann <kraxel@redhat.com>");
55 MODULE_DESCRIPTION("User-space granted page access driver");
57 static int limit = 1024*1024;
58 module_param(limit, int, 0644);
59 MODULE_PARM_DESC(limit, "Maximum number of grants that may be mapped by "
62 static atomic_t pages_mapped = ATOMIC_INIT(0);
64 static int use_ptemod;
65 #define populate_freeable_maps use_ptemod
67 static int unmap_grant_pages(struct gntdev_grant_map *map,
68 int offset, int pages);
70 static struct miscdevice gntdev_miscdev;
72 /* ------------------------------------------------------------------ */
74 bool gntdev_account_mapped_pages(int count)
76 return atomic_add_return(count, &pages_mapped) > limit;
79 static void gntdev_print_maps(struct gntdev_priv *priv,
80 char *text, int text_index)
83 struct gntdev_grant_map *map;
85 pr_debug("%s: maps list (priv %p)\n", __func__, priv);
86 list_for_each_entry(map, &priv->maps, next)
87 pr_debug(" index %2d, count %2d %s\n",
88 map->index, map->count,
89 map->index == text_index && text ? text : "");
93 static void gntdev_free_map(struct gntdev_grant_map *map)
98 #ifdef CONFIG_XEN_GRANT_DMA_ALLOC
100 struct gnttab_dma_alloc_args args;
102 args.dev = map->dma_dev;
103 args.coherent = !!(map->dma_flags & GNTDEV_DMA_FLAG_COHERENT);
104 args.nr_pages = map->count;
105 args.pages = map->pages;
106 args.frames = map->frames;
107 args.vaddr = map->dma_vaddr;
108 args.dev_bus_addr = map->dma_bus_addr;
110 gnttab_dma_free_pages(&args);
114 gnttab_free_pages(map->count, map->pages);
116 #ifdef CONFIG_XEN_GRANT_DMA_ALLOC
122 kfree(map->unmap_ops);
123 kfree(map->kmap_ops);
124 kfree(map->kunmap_ops);
128 struct gntdev_grant_map *gntdev_alloc_map(struct gntdev_priv *priv, int count,
131 struct gntdev_grant_map *add;
134 add = kzalloc(sizeof(*add), GFP_KERNEL);
138 add->grants = kcalloc(count, sizeof(add->grants[0]), GFP_KERNEL);
139 add->map_ops = kcalloc(count, sizeof(add->map_ops[0]), GFP_KERNEL);
140 add->unmap_ops = kcalloc(count, sizeof(add->unmap_ops[0]), GFP_KERNEL);
141 add->kmap_ops = kcalloc(count, sizeof(add->kmap_ops[0]), GFP_KERNEL);
142 add->kunmap_ops = kcalloc(count, sizeof(add->kunmap_ops[0]), GFP_KERNEL);
143 add->pages = kcalloc(count, sizeof(add->pages[0]), GFP_KERNEL);
144 if (NULL == add->grants ||
145 NULL == add->map_ops ||
146 NULL == add->unmap_ops ||
147 NULL == add->kmap_ops ||
148 NULL == add->kunmap_ops ||
152 #ifdef CONFIG_XEN_GRANT_DMA_ALLOC
153 add->dma_flags = dma_flags;
156 * Check if this mapping is requested to be backed
159 if (dma_flags & (GNTDEV_DMA_FLAG_WC | GNTDEV_DMA_FLAG_COHERENT)) {
160 struct gnttab_dma_alloc_args args;
162 add->frames = kcalloc(count, sizeof(add->frames[0]),
167 /* Remember the device, so we can free DMA memory. */
168 add->dma_dev = priv->dma_dev;
170 args.dev = priv->dma_dev;
171 args.coherent = !!(dma_flags & GNTDEV_DMA_FLAG_COHERENT);
172 args.nr_pages = count;
173 args.pages = add->pages;
174 args.frames = add->frames;
176 if (gnttab_dma_alloc_pages(&args))
179 add->dma_vaddr = args.vaddr;
180 add->dma_bus_addr = args.dev_bus_addr;
183 if (gnttab_alloc_pages(count, add->pages))
186 for (i = 0; i < count; i++) {
187 add->map_ops[i].handle = -1;
188 add->unmap_ops[i].handle = -1;
189 add->kmap_ops[i].handle = -1;
190 add->kunmap_ops[i].handle = -1;
195 refcount_set(&add->users, 1);
200 gntdev_free_map(add);
204 void gntdev_add_map(struct gntdev_priv *priv, struct gntdev_grant_map *add)
206 struct gntdev_grant_map *map;
208 list_for_each_entry(map, &priv->maps, next) {
209 if (add->index + add->count < map->index) {
210 list_add_tail(&add->next, &map->next);
213 add->index = map->index + map->count;
215 list_add_tail(&add->next, &priv->maps);
218 gntdev_print_maps(priv, "[new]", add->index);
221 static struct gntdev_grant_map *gntdev_find_map_index(struct gntdev_priv *priv,
222 int index, int count)
224 struct gntdev_grant_map *map;
226 list_for_each_entry(map, &priv->maps, next) {
227 if (map->index != index)
229 if (count && map->count != count)
236 void gntdev_put_map(struct gntdev_priv *priv, struct gntdev_grant_map *map)
241 if (!refcount_dec_and_test(&map->users))
244 atomic_sub(map->count, &pages_mapped);
246 if (map->notify.flags & UNMAP_NOTIFY_SEND_EVENT) {
247 notify_remote_via_evtchn(map->notify.event);
248 evtchn_put(map->notify.event);
251 if (populate_freeable_maps && priv) {
252 mutex_lock(&priv->lock);
253 list_del(&map->next);
254 mutex_unlock(&priv->lock);
257 if (map->pages && !use_ptemod)
258 unmap_grant_pages(map, 0, map->count);
259 gntdev_free_map(map);
262 /* ------------------------------------------------------------------ */
264 static int find_grant_ptes(pte_t *pte, pgtable_t token,
265 unsigned long addr, void *data)
267 struct gntdev_grant_map *map = data;
268 unsigned int pgnr = (addr - map->vma->vm_start) >> PAGE_SHIFT;
269 int flags = map->flags | GNTMAP_application_map | GNTMAP_contains_pte;
272 BUG_ON(pgnr >= map->count);
273 pte_maddr = arbitrary_virt_to_machine(pte).maddr;
276 * Set the PTE as special to force get_user_pages_fast() fall
277 * back to the slow path. If this is not supported as part of
278 * the grant map, it will be done afterwards.
280 if (xen_feature(XENFEAT_gnttab_map_avail_bits))
281 flags |= (1 << _GNTMAP_guest_avail0);
283 gnttab_set_map_op(&map->map_ops[pgnr], pte_maddr, flags,
284 map->grants[pgnr].ref,
285 map->grants[pgnr].domid);
286 gnttab_set_unmap_op(&map->unmap_ops[pgnr], pte_maddr, flags,
292 static int set_grant_ptes_as_special(pte_t *pte, pgtable_t token,
293 unsigned long addr, void *data)
295 set_pte_at(current->mm, addr, pte, pte_mkspecial(*pte));
300 int gntdev_map_grant_pages(struct gntdev_grant_map *map)
305 /* Note: it could already be mapped */
306 if (map->map_ops[0].handle != -1)
308 for (i = 0; i < map->count; i++) {
309 unsigned long addr = (unsigned long)
310 pfn_to_kaddr(page_to_pfn(map->pages[i]));
311 gnttab_set_map_op(&map->map_ops[i], addr, map->flags,
313 map->grants[i].domid);
314 gnttab_set_unmap_op(&map->unmap_ops[i], addr,
315 map->flags, -1 /* handle */);
319 * Setup the map_ops corresponding to the pte entries pointing
320 * to the kernel linear addresses of the struct pages.
321 * These ptes are completely different from the user ptes dealt
322 * with find_grant_ptes.
324 for (i = 0; i < map->count; i++) {
325 unsigned long address = (unsigned long)
326 pfn_to_kaddr(page_to_pfn(map->pages[i]));
327 BUG_ON(PageHighMem(map->pages[i]));
329 gnttab_set_map_op(&map->kmap_ops[i], address,
330 map->flags | GNTMAP_host_map,
332 map->grants[i].domid);
333 gnttab_set_unmap_op(&map->kunmap_ops[i], address,
334 map->flags | GNTMAP_host_map, -1);
338 pr_debug("map %d+%d\n", map->index, map->count);
339 err = gnttab_map_refs(map->map_ops, use_ptemod ? map->kmap_ops : NULL,
340 map->pages, map->count);
344 for (i = 0; i < map->count; i++) {
345 if (map->map_ops[i].status) {
350 map->unmap_ops[i].handle = map->map_ops[i].handle;
352 map->kunmap_ops[i].handle = map->kmap_ops[i].handle;
353 #ifdef CONFIG_XEN_GRANT_DMA_ALLOC
354 else if (map->dma_vaddr) {
357 bfn = pfn_to_bfn(page_to_pfn(map->pages[i]));
358 map->unmap_ops[i].dev_bus_addr = __pfn_to_phys(bfn);
365 static int __unmap_grant_pages(struct gntdev_grant_map *map, int offset,
369 struct gntab_unmap_queue_data unmap_data;
371 if (map->notify.flags & UNMAP_NOTIFY_CLEAR_BYTE) {
372 int pgno = (map->notify.addr >> PAGE_SHIFT);
373 if (pgno >= offset && pgno < offset + pages) {
374 /* No need for kmap, pages are in lowmem */
375 uint8_t *tmp = pfn_to_kaddr(page_to_pfn(map->pages[pgno]));
376 tmp[map->notify.addr & (PAGE_SIZE-1)] = 0;
377 map->notify.flags &= ~UNMAP_NOTIFY_CLEAR_BYTE;
381 unmap_data.unmap_ops = map->unmap_ops + offset;
382 unmap_data.kunmap_ops = use_ptemod ? map->kunmap_ops + offset : NULL;
383 unmap_data.pages = map->pages + offset;
384 unmap_data.count = pages;
386 err = gnttab_unmap_refs_sync(&unmap_data);
390 for (i = 0; i < pages; i++) {
391 if (map->unmap_ops[offset+i].status)
393 pr_debug("unmap handle=%d st=%d\n",
394 map->unmap_ops[offset+i].handle,
395 map->unmap_ops[offset+i].status);
396 map->unmap_ops[offset+i].handle = -1;
401 static int unmap_grant_pages(struct gntdev_grant_map *map, int offset,
406 pr_debug("unmap %d+%d [%d+%d]\n", map->index, map->count, offset, pages);
408 /* It is possible the requested range will have a "hole" where we
409 * already unmapped some of the grants. Only unmap valid ranges.
411 while (pages && !err) {
412 while (pages && map->unmap_ops[offset].handle == -1) {
417 while (range < pages) {
418 if (map->unmap_ops[offset+range].handle == -1)
422 err = __unmap_grant_pages(map, offset, range);
430 /* ------------------------------------------------------------------ */
432 static void gntdev_vma_open(struct vm_area_struct *vma)
434 struct gntdev_grant_map *map = vma->vm_private_data;
436 pr_debug("gntdev_vma_open %p\n", vma);
437 refcount_inc(&map->users);
440 static void gntdev_vma_close(struct vm_area_struct *vma)
442 struct gntdev_grant_map *map = vma->vm_private_data;
443 struct file *file = vma->vm_file;
444 struct gntdev_priv *priv = file->private_data;
446 pr_debug("gntdev_vma_close %p\n", vma);
448 /* It is possible that an mmu notifier could be running
449 * concurrently, so take priv->lock to ensure that the vma won't
450 * vanishing during the unmap_grant_pages call, since we will
451 * spin here until that completes. Such a concurrent call will
452 * not do any unmapping, since that has been done prior to
453 * closing the vma, but it may still iterate the unmap_ops list.
455 mutex_lock(&priv->lock);
457 mutex_unlock(&priv->lock);
459 vma->vm_private_data = NULL;
460 gntdev_put_map(priv, map);
463 static struct page *gntdev_vma_find_special_page(struct vm_area_struct *vma,
466 struct gntdev_grant_map *map = vma->vm_private_data;
468 return map->pages[(addr - map->pages_vm_start) >> PAGE_SHIFT];
471 static const struct vm_operations_struct gntdev_vmops = {
472 .open = gntdev_vma_open,
473 .close = gntdev_vma_close,
474 .find_special_page = gntdev_vma_find_special_page,
477 /* ------------------------------------------------------------------ */
479 static void unmap_if_in_range(struct gntdev_grant_map *map,
480 unsigned long start, unsigned long end)
482 unsigned long mstart, mend;
487 if (map->vma->vm_start >= end)
489 if (map->vma->vm_end <= start)
491 mstart = max(start, map->vma->vm_start);
492 mend = min(end, map->vma->vm_end);
493 pr_debug("map %d+%d (%lx %lx), range %lx %lx, mrange %lx %lx\n",
494 map->index, map->count,
495 map->vma->vm_start, map->vma->vm_end,
496 start, end, mstart, mend);
497 err = unmap_grant_pages(map,
498 (mstart - map->vma->vm_start) >> PAGE_SHIFT,
499 (mend - mstart) >> PAGE_SHIFT);
503 static void mn_invl_range_start(struct mmu_notifier *mn,
504 struct mm_struct *mm,
505 unsigned long start, unsigned long end)
507 struct gntdev_priv *priv = container_of(mn, struct gntdev_priv, mn);
508 struct gntdev_grant_map *map;
510 mutex_lock(&priv->lock);
511 list_for_each_entry(map, &priv->maps, next) {
512 unmap_if_in_range(map, start, end);
514 list_for_each_entry(map, &priv->freeable_maps, next) {
515 unmap_if_in_range(map, start, end);
517 mutex_unlock(&priv->lock);
520 static void mn_release(struct mmu_notifier *mn,
521 struct mm_struct *mm)
523 struct gntdev_priv *priv = container_of(mn, struct gntdev_priv, mn);
524 struct gntdev_grant_map *map;
527 mutex_lock(&priv->lock);
528 list_for_each_entry(map, &priv->maps, next) {
531 pr_debug("map %d+%d (%lx %lx)\n",
532 map->index, map->count,
533 map->vma->vm_start, map->vma->vm_end);
534 err = unmap_grant_pages(map, /* offset */ 0, map->count);
537 list_for_each_entry(map, &priv->freeable_maps, next) {
540 pr_debug("map %d+%d (%lx %lx)\n",
541 map->index, map->count,
542 map->vma->vm_start, map->vma->vm_end);
543 err = unmap_grant_pages(map, /* offset */ 0, map->count);
546 mutex_unlock(&priv->lock);
549 static const struct mmu_notifier_ops gntdev_mmu_ops = {
550 .release = mn_release,
551 .invalidate_range_start = mn_invl_range_start,
554 /* ------------------------------------------------------------------ */
556 static int gntdev_open(struct inode *inode, struct file *flip)
558 struct gntdev_priv *priv;
561 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
565 INIT_LIST_HEAD(&priv->maps);
566 INIT_LIST_HEAD(&priv->freeable_maps);
567 mutex_init(&priv->lock);
570 priv->mm = get_task_mm(current);
575 priv->mn.ops = &gntdev_mmu_ops;
576 ret = mmu_notifier_register(&priv->mn, priv->mm);
585 flip->private_data = priv;
586 #ifdef CONFIG_XEN_GRANT_DMA_ALLOC
587 priv->dma_dev = gntdev_miscdev.this_device;
590 * The device is not spawn from a device tree, so arch_setup_dma_ops
591 * is not called, thus leaving the device with dummy DMA ops.
592 * Fix this by calling of_dma_configure() with a NULL node to set
595 of_dma_configure(priv->dma_dev, NULL, true);
597 pr_debug("priv %p\n", priv);
602 static int gntdev_release(struct inode *inode, struct file *flip)
604 struct gntdev_priv *priv = flip->private_data;
605 struct gntdev_grant_map *map;
607 pr_debug("priv %p\n", priv);
609 mutex_lock(&priv->lock);
610 while (!list_empty(&priv->maps)) {
611 map = list_entry(priv->maps.next,
612 struct gntdev_grant_map, next);
613 list_del(&map->next);
614 gntdev_put_map(NULL /* already removed */, map);
616 WARN_ON(!list_empty(&priv->freeable_maps));
617 mutex_unlock(&priv->lock);
620 mmu_notifier_unregister(&priv->mn, priv->mm);
625 static long gntdev_ioctl_map_grant_ref(struct gntdev_priv *priv,
626 struct ioctl_gntdev_map_grant_ref __user *u)
628 struct ioctl_gntdev_map_grant_ref op;
629 struct gntdev_grant_map *map;
632 if (copy_from_user(&op, u, sizeof(op)) != 0)
634 pr_debug("priv %p, add %d\n", priv, op.count);
635 if (unlikely(op.count <= 0))
639 map = gntdev_alloc_map(priv, op.count, 0 /* This is not a dma-buf. */);
643 if (unlikely(gntdev_account_mapped_pages(op.count))) {
644 pr_debug("can't map: over limit\n");
645 gntdev_put_map(NULL, map);
649 if (copy_from_user(map->grants, &u->refs,
650 sizeof(map->grants[0]) * op.count) != 0) {
651 gntdev_put_map(NULL, map);
655 mutex_lock(&priv->lock);
656 gntdev_add_map(priv, map);
657 op.index = map->index << PAGE_SHIFT;
658 mutex_unlock(&priv->lock);
660 if (copy_to_user(u, &op, sizeof(op)) != 0)
666 static long gntdev_ioctl_unmap_grant_ref(struct gntdev_priv *priv,
667 struct ioctl_gntdev_unmap_grant_ref __user *u)
669 struct ioctl_gntdev_unmap_grant_ref op;
670 struct gntdev_grant_map *map;
673 if (copy_from_user(&op, u, sizeof(op)) != 0)
675 pr_debug("priv %p, del %d+%d\n", priv, (int)op.index, (int)op.count);
677 mutex_lock(&priv->lock);
678 map = gntdev_find_map_index(priv, op.index >> PAGE_SHIFT, op.count);
680 list_del(&map->next);
681 if (populate_freeable_maps)
682 list_add_tail(&map->next, &priv->freeable_maps);
685 mutex_unlock(&priv->lock);
687 gntdev_put_map(priv, map);
691 static long gntdev_ioctl_get_offset_for_vaddr(struct gntdev_priv *priv,
692 struct ioctl_gntdev_get_offset_for_vaddr __user *u)
694 struct ioctl_gntdev_get_offset_for_vaddr op;
695 struct vm_area_struct *vma;
696 struct gntdev_grant_map *map;
699 if (copy_from_user(&op, u, sizeof(op)) != 0)
701 pr_debug("priv %p, offset for vaddr %lx\n", priv, (unsigned long)op.vaddr);
703 down_read(¤t->mm->mmap_sem);
704 vma = find_vma(current->mm, op.vaddr);
705 if (!vma || vma->vm_ops != &gntdev_vmops)
708 map = vma->vm_private_data;
712 op.offset = map->index << PAGE_SHIFT;
713 op.count = map->count;
717 up_read(¤t->mm->mmap_sem);
719 if (rv == 0 && copy_to_user(u, &op, sizeof(op)) != 0)
724 static long gntdev_ioctl_notify(struct gntdev_priv *priv, void __user *u)
726 struct ioctl_gntdev_unmap_notify op;
727 struct gntdev_grant_map *map;
730 unsigned int out_event;
732 if (copy_from_user(&op, u, sizeof(op)))
735 if (op.action & ~(UNMAP_NOTIFY_CLEAR_BYTE|UNMAP_NOTIFY_SEND_EVENT))
738 /* We need to grab a reference to the event channel we are going to use
739 * to send the notify before releasing the reference we may already have
740 * (if someone has called this ioctl twice). This is required so that
741 * it is possible to change the clear_byte part of the notification
742 * without disturbing the event channel part, which may now be the last
743 * reference to that event channel.
745 if (op.action & UNMAP_NOTIFY_SEND_EVENT) {
746 if (evtchn_get(op.event_channel_port))
750 out_flags = op.action;
751 out_event = op.event_channel_port;
753 mutex_lock(&priv->lock);
755 list_for_each_entry(map, &priv->maps, next) {
756 uint64_t begin = map->index << PAGE_SHIFT;
757 uint64_t end = (map->index + map->count) << PAGE_SHIFT;
758 if (op.index >= begin && op.index < end)
765 if ((op.action & UNMAP_NOTIFY_CLEAR_BYTE) &&
766 (map->flags & GNTMAP_readonly)) {
771 out_flags = map->notify.flags;
772 out_event = map->notify.event;
774 map->notify.flags = op.action;
775 map->notify.addr = op.index - (map->index << PAGE_SHIFT);
776 map->notify.event = op.event_channel_port;
781 mutex_unlock(&priv->lock);
783 /* Drop the reference to the event channel we did not save in the map */
784 if (out_flags & UNMAP_NOTIFY_SEND_EVENT)
785 evtchn_put(out_event);
790 #define GNTDEV_COPY_BATCH 16
792 struct gntdev_copy_batch {
793 struct gnttab_copy ops[GNTDEV_COPY_BATCH];
794 struct page *pages[GNTDEV_COPY_BATCH];
795 s16 __user *status[GNTDEV_COPY_BATCH];
797 unsigned int nr_pages;
800 static int gntdev_get_page(struct gntdev_copy_batch *batch, void __user *virt,
801 bool writeable, unsigned long *gfn)
803 unsigned long addr = (unsigned long)virt;
805 unsigned long xen_pfn;
808 ret = get_user_pages_fast(addr, 1, writeable, &page);
812 batch->pages[batch->nr_pages++] = page;
814 xen_pfn = page_to_xen_pfn(page) + XEN_PFN_DOWN(addr & ~PAGE_MASK);
815 *gfn = pfn_to_gfn(xen_pfn);
820 static void gntdev_put_pages(struct gntdev_copy_batch *batch)
824 for (i = 0; i < batch->nr_pages; i++)
825 put_page(batch->pages[i]);
829 static int gntdev_copy(struct gntdev_copy_batch *batch)
833 gnttab_batch_copy(batch->ops, batch->nr_ops);
834 gntdev_put_pages(batch);
837 * For each completed op, update the status if the op failed
838 * and all previous ops for the segment were successful.
840 for (i = 0; i < batch->nr_ops; i++) {
841 s16 status = batch->ops[i].status;
844 if (status == GNTST_okay)
847 if (__get_user(old_status, batch->status[i]))
850 if (old_status != GNTST_okay)
853 if (__put_user(status, batch->status[i]))
861 static int gntdev_grant_copy_seg(struct gntdev_copy_batch *batch,
862 struct gntdev_grant_copy_segment *seg,
868 * Disallow local -> local copies since there is only space in
869 * batch->pages for one page per-op and this would be a very
870 * expensive memcpy().
872 if (!(seg->flags & (GNTCOPY_source_gref | GNTCOPY_dest_gref)))
875 /* Can't cross page if source/dest is a grant ref. */
876 if (seg->flags & GNTCOPY_source_gref) {
877 if (seg->source.foreign.offset + seg->len > XEN_PAGE_SIZE)
880 if (seg->flags & GNTCOPY_dest_gref) {
881 if (seg->dest.foreign.offset + seg->len > XEN_PAGE_SIZE)
885 if (put_user(GNTST_okay, status))
888 while (copied < seg->len) {
889 struct gnttab_copy *op;
895 if (batch->nr_ops >= GNTDEV_COPY_BATCH) {
896 ret = gntdev_copy(batch);
901 len = seg->len - copied;
903 op = &batch->ops[batch->nr_ops];
906 if (seg->flags & GNTCOPY_source_gref) {
907 op->source.u.ref = seg->source.foreign.ref;
908 op->source.domid = seg->source.foreign.domid;
909 op->source.offset = seg->source.foreign.offset + copied;
910 op->flags |= GNTCOPY_source_gref;
912 virt = seg->source.virt + copied;
913 off = (unsigned long)virt & ~XEN_PAGE_MASK;
914 len = min(len, (size_t)XEN_PAGE_SIZE - off);
916 ret = gntdev_get_page(batch, virt, false, &gfn);
920 op->source.u.gmfn = gfn;
921 op->source.domid = DOMID_SELF;
922 op->source.offset = off;
925 if (seg->flags & GNTCOPY_dest_gref) {
926 op->dest.u.ref = seg->dest.foreign.ref;
927 op->dest.domid = seg->dest.foreign.domid;
928 op->dest.offset = seg->dest.foreign.offset + copied;
929 op->flags |= GNTCOPY_dest_gref;
931 virt = seg->dest.virt + copied;
932 off = (unsigned long)virt & ~XEN_PAGE_MASK;
933 len = min(len, (size_t)XEN_PAGE_SIZE - off);
935 ret = gntdev_get_page(batch, virt, true, &gfn);
939 op->dest.u.gmfn = gfn;
940 op->dest.domid = DOMID_SELF;
941 op->dest.offset = off;
947 batch->status[batch->nr_ops] = status;
954 static long gntdev_ioctl_grant_copy(struct gntdev_priv *priv, void __user *u)
956 struct ioctl_gntdev_grant_copy copy;
957 struct gntdev_copy_batch batch;
961 if (copy_from_user(©, u, sizeof(copy)))
967 for (i = 0; i < copy.count; i++) {
968 struct gntdev_grant_copy_segment seg;
970 if (copy_from_user(&seg, ©.segments[i], sizeof(seg))) {
975 ret = gntdev_grant_copy_seg(&batch, &seg, ©.segments[i].status);
982 ret = gntdev_copy(&batch);
986 gntdev_put_pages(&batch);
990 static long gntdev_ioctl(struct file *flip,
991 unsigned int cmd, unsigned long arg)
993 struct gntdev_priv *priv = flip->private_data;
994 void __user *ptr = (void __user *)arg;
997 case IOCTL_GNTDEV_MAP_GRANT_REF:
998 return gntdev_ioctl_map_grant_ref(priv, ptr);
1000 case IOCTL_GNTDEV_UNMAP_GRANT_REF:
1001 return gntdev_ioctl_unmap_grant_ref(priv, ptr);
1003 case IOCTL_GNTDEV_GET_OFFSET_FOR_VADDR:
1004 return gntdev_ioctl_get_offset_for_vaddr(priv, ptr);
1006 case IOCTL_GNTDEV_SET_UNMAP_NOTIFY:
1007 return gntdev_ioctl_notify(priv, ptr);
1009 case IOCTL_GNTDEV_GRANT_COPY:
1010 return gntdev_ioctl_grant_copy(priv, ptr);
1013 pr_debug("priv %p, unknown cmd %x\n", priv, cmd);
1014 return -ENOIOCTLCMD;
1020 static int gntdev_mmap(struct file *flip, struct vm_area_struct *vma)
1022 struct gntdev_priv *priv = flip->private_data;
1023 int index = vma->vm_pgoff;
1024 int count = vma_pages(vma);
1025 struct gntdev_grant_map *map;
1026 int i, err = -EINVAL;
1028 if ((vma->vm_flags & VM_WRITE) && !(vma->vm_flags & VM_SHARED))
1031 pr_debug("map %d+%d at %lx (pgoff %lx)\n",
1032 index, count, vma->vm_start, vma->vm_pgoff);
1034 mutex_lock(&priv->lock);
1035 map = gntdev_find_map_index(priv, index, count);
1038 if (use_ptemod && map->vma)
1040 if (use_ptemod && priv->mm != vma->vm_mm) {
1041 pr_warn("Huh? Other mm?\n");
1045 refcount_inc(&map->users);
1047 vma->vm_ops = &gntdev_vmops;
1049 vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP | VM_MIXEDMAP;
1052 vma->vm_flags |= VM_DONTCOPY;
1054 vma->vm_private_data = map;
1060 if ((vma->vm_flags & VM_WRITE) &&
1061 (map->flags & GNTMAP_readonly))
1062 goto out_unlock_put;
1064 map->flags = GNTMAP_host_map;
1065 if (!(vma->vm_flags & VM_WRITE))
1066 map->flags |= GNTMAP_readonly;
1069 mutex_unlock(&priv->lock);
1072 map->pages_vm_start = vma->vm_start;
1073 err = apply_to_page_range(vma->vm_mm, vma->vm_start,
1074 vma->vm_end - vma->vm_start,
1075 find_grant_ptes, map);
1077 pr_warn("find_grant_ptes() failure.\n");
1082 err = gntdev_map_grant_pages(map);
1087 for (i = 0; i < count; i++) {
1088 err = vm_insert_page(vma, vma->vm_start + i*PAGE_SIZE,
1096 * If the PTEs were not made special by the grant map
1097 * hypercall, do so here.
1099 * This is racy since the mapping is already visible
1100 * to userspace but userspace should be well-behaved
1101 * enough to not touch it until the mmap() call
1104 if (!xen_feature(XENFEAT_gnttab_map_avail_bits)) {
1105 apply_to_page_range(vma->vm_mm, vma->vm_start,
1106 vma->vm_end - vma->vm_start,
1107 set_grant_ptes_as_special, NULL);
1115 mutex_unlock(&priv->lock);
1119 mutex_unlock(&priv->lock);
1123 unmap_grant_pages(map, 0, map->count);
1125 gntdev_put_map(priv, map);
1129 static const struct file_operations gntdev_fops = {
1130 .owner = THIS_MODULE,
1131 .open = gntdev_open,
1132 .release = gntdev_release,
1133 .mmap = gntdev_mmap,
1134 .unlocked_ioctl = gntdev_ioctl
1137 static struct miscdevice gntdev_miscdev = {
1138 .minor = MISC_DYNAMIC_MINOR,
1139 .name = "xen/gntdev",
1140 .fops = &gntdev_fops,
1143 /* ------------------------------------------------------------------ */
1145 static int __init gntdev_init(void)
1152 use_ptemod = !xen_feature(XENFEAT_auto_translated_physmap);
1154 err = misc_register(&gntdev_miscdev);
1156 pr_err("Could not register gntdev device\n");
1162 static void __exit gntdev_exit(void)
1164 misc_deregister(&gntdev_miscdev);
1167 module_init(gntdev_init);
1168 module_exit(gntdev_exit);
1170 /* ------------------------------------------------------------------ */