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/dma-mapping.h>
26 #include <linux/module.h>
27 #include <linux/kernel.h>
28 #include <linux/init.h>
29 #include <linux/miscdevice.h>
31 #include <linux/uaccess.h>
32 #include <linux/sched.h>
33 #include <linux/sched/mm.h>
34 #include <linux/spinlock.h>
35 #include <linux/slab.h>
36 #include <linux/highmem.h>
37 #include <linux/refcount.h>
40 #include <xen/grant_table.h>
41 #include <xen/balloon.h>
42 #include <xen/gntdev.h>
43 #include <xen/events.h>
45 #include <asm/xen/hypervisor.h>
46 #include <asm/xen/hypercall.h>
48 #include "gntdev-common.h"
49 #ifdef CONFIG_XEN_GNTDEV_DMABUF
50 #include "gntdev-dmabuf.h"
53 MODULE_LICENSE("GPL");
54 MODULE_AUTHOR("Derek G. Murray <Derek.Murray@cl.cam.ac.uk>, "
55 "Gerd Hoffmann <kraxel@redhat.com>");
56 MODULE_DESCRIPTION("User-space granted page access driver");
58 static int limit = 1024*1024;
59 module_param(limit, int, 0644);
60 MODULE_PARM_DESC(limit, "Maximum number of grants that may be mapped by "
63 static atomic_t pages_mapped = ATOMIC_INIT(0);
65 static int use_ptemod;
66 #define populate_freeable_maps use_ptemod
68 static int unmap_grant_pages(struct gntdev_grant_map *map,
69 int offset, int pages);
71 static struct miscdevice gntdev_miscdev;
73 /* ------------------------------------------------------------------ */
75 bool gntdev_account_mapped_pages(int count)
77 return atomic_add_return(count, &pages_mapped) > limit;
80 static void gntdev_print_maps(struct gntdev_priv *priv,
81 char *text, int text_index)
84 struct gntdev_grant_map *map;
86 pr_debug("%s: maps list (priv %p)\n", __func__, priv);
87 list_for_each_entry(map, &priv->maps, next)
88 pr_debug(" index %2d, count %2d %s\n",
89 map->index, map->count,
90 map->index == text_index && text ? text : "");
94 static void gntdev_free_map(struct gntdev_grant_map *map)
99 #ifdef CONFIG_XEN_GRANT_DMA_ALLOC
100 if (map->dma_vaddr) {
101 struct gnttab_dma_alloc_args args;
103 args.dev = map->dma_dev;
104 args.coherent = !!(map->dma_flags & GNTDEV_DMA_FLAG_COHERENT);
105 args.nr_pages = map->count;
106 args.pages = map->pages;
107 args.frames = map->frames;
108 args.vaddr = map->dma_vaddr;
109 args.dev_bus_addr = map->dma_bus_addr;
111 gnttab_dma_free_pages(&args);
115 gnttab_free_pages(map->count, map->pages);
117 #ifdef CONFIG_XEN_GRANT_DMA_ALLOC
123 kfree(map->unmap_ops);
124 kfree(map->kmap_ops);
125 kfree(map->kunmap_ops);
129 struct gntdev_grant_map *gntdev_alloc_map(struct gntdev_priv *priv, int count,
132 struct gntdev_grant_map *add;
135 add = kzalloc(sizeof(*add), GFP_KERNEL);
139 add->grants = kcalloc(count, sizeof(add->grants[0]), GFP_KERNEL);
140 add->map_ops = kcalloc(count, sizeof(add->map_ops[0]), GFP_KERNEL);
141 add->unmap_ops = kcalloc(count, sizeof(add->unmap_ops[0]), GFP_KERNEL);
142 add->kmap_ops = kcalloc(count, sizeof(add->kmap_ops[0]), GFP_KERNEL);
143 add->kunmap_ops = kcalloc(count, sizeof(add->kunmap_ops[0]), GFP_KERNEL);
144 add->pages = kcalloc(count, sizeof(add->pages[0]), GFP_KERNEL);
145 if (NULL == add->grants ||
146 NULL == add->map_ops ||
147 NULL == add->unmap_ops ||
148 NULL == add->kmap_ops ||
149 NULL == add->kunmap_ops ||
153 #ifdef CONFIG_XEN_GRANT_DMA_ALLOC
154 add->dma_flags = dma_flags;
157 * Check if this mapping is requested to be backed
160 if (dma_flags & (GNTDEV_DMA_FLAG_WC | GNTDEV_DMA_FLAG_COHERENT)) {
161 struct gnttab_dma_alloc_args args;
163 add->frames = kcalloc(count, sizeof(add->frames[0]),
168 /* Remember the device, so we can free DMA memory. */
169 add->dma_dev = priv->dma_dev;
171 args.dev = priv->dma_dev;
172 args.coherent = !!(dma_flags & GNTDEV_DMA_FLAG_COHERENT);
173 args.nr_pages = count;
174 args.pages = add->pages;
175 args.frames = add->frames;
177 if (gnttab_dma_alloc_pages(&args))
180 add->dma_vaddr = args.vaddr;
181 add->dma_bus_addr = args.dev_bus_addr;
184 if (gnttab_alloc_pages(count, add->pages))
187 for (i = 0; i < count; i++) {
188 add->map_ops[i].handle = -1;
189 add->unmap_ops[i].handle = -1;
190 add->kmap_ops[i].handle = -1;
191 add->kunmap_ops[i].handle = -1;
196 refcount_set(&add->users, 1);
201 gntdev_free_map(add);
205 void gntdev_add_map(struct gntdev_priv *priv, struct gntdev_grant_map *add)
207 struct gntdev_grant_map *map;
209 list_for_each_entry(map, &priv->maps, next) {
210 if (add->index + add->count < map->index) {
211 list_add_tail(&add->next, &map->next);
214 add->index = map->index + map->count;
216 list_add_tail(&add->next, &priv->maps);
219 gntdev_print_maps(priv, "[new]", add->index);
222 static struct gntdev_grant_map *gntdev_find_map_index(struct gntdev_priv *priv,
223 int index, int count)
225 struct gntdev_grant_map *map;
227 list_for_each_entry(map, &priv->maps, next) {
228 if (map->index != index)
230 if (count && map->count != count)
237 void gntdev_put_map(struct gntdev_priv *priv, struct gntdev_grant_map *map)
242 if (!refcount_dec_and_test(&map->users))
245 atomic_sub(map->count, &pages_mapped);
247 if (map->notify.flags & UNMAP_NOTIFY_SEND_EVENT) {
248 notify_remote_via_evtchn(map->notify.event);
249 evtchn_put(map->notify.event);
252 if (populate_freeable_maps && priv) {
253 mutex_lock(&priv->lock);
254 list_del(&map->next);
255 mutex_unlock(&priv->lock);
258 if (map->pages && !use_ptemod)
259 unmap_grant_pages(map, 0, map->count);
260 gntdev_free_map(map);
263 /* ------------------------------------------------------------------ */
265 static int find_grant_ptes(pte_t *pte, 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, unsigned long addr, void *data)
294 set_pte_at(current->mm, addr, pte, pte_mkspecial(*pte));
299 int gntdev_map_grant_pages(struct gntdev_grant_map *map)
304 /* Note: it could already be mapped */
305 if (map->map_ops[0].handle != -1)
307 for (i = 0; i < map->count; i++) {
308 unsigned long addr = (unsigned long)
309 pfn_to_kaddr(page_to_pfn(map->pages[i]));
310 gnttab_set_map_op(&map->map_ops[i], addr, map->flags,
312 map->grants[i].domid);
313 gnttab_set_unmap_op(&map->unmap_ops[i], addr,
314 map->flags, -1 /* handle */);
318 * Setup the map_ops corresponding to the pte entries pointing
319 * to the kernel linear addresses of the struct pages.
320 * These ptes are completely different from the user ptes dealt
321 * with find_grant_ptes.
322 * Note that GNTMAP_device_map isn't needed here: The
323 * dev_bus_addr output field gets consumed only from ->map_ops,
324 * and by not requesting it when mapping we also avoid needing
325 * to mirror dev_bus_addr into ->unmap_ops (and holding an extra
326 * reference to the page in the hypervisor).
328 unsigned int flags = (map->flags & ~GNTMAP_device_map) |
331 for (i = 0; i < map->count; i++) {
332 unsigned long address = (unsigned long)
333 pfn_to_kaddr(page_to_pfn(map->pages[i]));
334 BUG_ON(PageHighMem(map->pages[i]));
336 gnttab_set_map_op(&map->kmap_ops[i], address, flags,
338 map->grants[i].domid);
339 gnttab_set_unmap_op(&map->kunmap_ops[i], address,
344 pr_debug("map %d+%d\n", map->index, map->count);
345 err = gnttab_map_refs(map->map_ops, use_ptemod ? map->kmap_ops : NULL,
346 map->pages, map->count);
348 for (i = 0; i < map->count; i++) {
349 if (map->map_ops[i].status == GNTST_okay)
350 map->unmap_ops[i].handle = map->map_ops[i].handle;
354 if (map->flags & GNTMAP_device_map)
355 map->unmap_ops[i].dev_bus_addr = map->map_ops[i].dev_bus_addr;
358 if (map->kmap_ops[i].status == GNTST_okay)
359 map->kunmap_ops[i].handle = map->kmap_ops[i].handle;
367 static int __unmap_grant_pages(struct gntdev_grant_map *map, int offset,
371 struct gntab_unmap_queue_data unmap_data;
373 if (map->notify.flags & UNMAP_NOTIFY_CLEAR_BYTE) {
374 int pgno = (map->notify.addr >> PAGE_SHIFT);
375 if (pgno >= offset && pgno < offset + pages) {
376 /* No need for kmap, pages are in lowmem */
377 uint8_t *tmp = pfn_to_kaddr(page_to_pfn(map->pages[pgno]));
378 tmp[map->notify.addr & (PAGE_SIZE-1)] = 0;
379 map->notify.flags &= ~UNMAP_NOTIFY_CLEAR_BYTE;
383 unmap_data.unmap_ops = map->unmap_ops + offset;
384 unmap_data.kunmap_ops = use_ptemod ? map->kunmap_ops + offset : NULL;
385 unmap_data.pages = map->pages + offset;
386 unmap_data.count = pages;
388 err = gnttab_unmap_refs_sync(&unmap_data);
392 for (i = 0; i < pages; i++) {
393 if (map->unmap_ops[offset+i].status)
395 pr_debug("unmap handle=%d st=%d\n",
396 map->unmap_ops[offset+i].handle,
397 map->unmap_ops[offset+i].status);
398 map->unmap_ops[offset+i].handle = -1;
403 static int unmap_grant_pages(struct gntdev_grant_map *map, int offset,
408 pr_debug("unmap %d+%d [%d+%d]\n", map->index, map->count, offset, pages);
410 /* It is possible the requested range will have a "hole" where we
411 * already unmapped some of the grants. Only unmap valid ranges.
413 while (pages && !err) {
414 while (pages && map->unmap_ops[offset].handle == -1) {
419 while (range < pages) {
420 if (map->unmap_ops[offset+range].handle == -1)
424 err = __unmap_grant_pages(map, offset, range);
432 /* ------------------------------------------------------------------ */
434 static void gntdev_vma_open(struct vm_area_struct *vma)
436 struct gntdev_grant_map *map = vma->vm_private_data;
438 pr_debug("gntdev_vma_open %p\n", vma);
439 refcount_inc(&map->users);
442 static void gntdev_vma_close(struct vm_area_struct *vma)
444 struct gntdev_grant_map *map = vma->vm_private_data;
445 struct file *file = vma->vm_file;
446 struct gntdev_priv *priv = file->private_data;
448 pr_debug("gntdev_vma_close %p\n", vma);
450 /* It is possible that an mmu notifier could be running
451 * concurrently, so take priv->lock to ensure that the vma won't
452 * vanishing during the unmap_grant_pages call, since we will
453 * spin here until that completes. Such a concurrent call will
454 * not do any unmapping, since that has been done prior to
455 * closing the vma, but it may still iterate the unmap_ops list.
457 mutex_lock(&priv->lock);
459 mutex_unlock(&priv->lock);
461 vma->vm_private_data = NULL;
462 gntdev_put_map(priv, map);
465 static struct page *gntdev_vma_find_special_page(struct vm_area_struct *vma,
468 struct gntdev_grant_map *map = vma->vm_private_data;
470 return map->pages[(addr - map->pages_vm_start) >> PAGE_SHIFT];
473 static const struct vm_operations_struct gntdev_vmops = {
474 .open = gntdev_vma_open,
475 .close = gntdev_vma_close,
476 .find_special_page = gntdev_vma_find_special_page,
479 /* ------------------------------------------------------------------ */
481 static bool in_range(struct gntdev_grant_map *map,
482 unsigned long start, unsigned long end)
486 if (map->vma->vm_start >= end)
488 if (map->vma->vm_end <= start)
494 static int unmap_if_in_range(struct gntdev_grant_map *map,
495 unsigned long start, unsigned long end,
498 unsigned long mstart, mend;
501 if (!in_range(map, start, end))
507 mstart = max(start, map->vma->vm_start);
508 mend = min(end, map->vma->vm_end);
509 pr_debug("map %d+%d (%lx %lx), range %lx %lx, mrange %lx %lx\n",
510 map->index, map->count,
511 map->vma->vm_start, map->vma->vm_end,
512 start, end, mstart, mend);
513 err = unmap_grant_pages(map,
514 (mstart - map->vma->vm_start) >> PAGE_SHIFT,
515 (mend - mstart) >> PAGE_SHIFT);
521 static int mn_invl_range_start(struct mmu_notifier *mn,
522 const struct mmu_notifier_range *range)
524 struct gntdev_priv *priv = container_of(mn, struct gntdev_priv, mn);
525 struct gntdev_grant_map *map;
528 if (mmu_notifier_range_blockable(range))
529 mutex_lock(&priv->lock);
530 else if (!mutex_trylock(&priv->lock))
533 list_for_each_entry(map, &priv->maps, next) {
534 ret = unmap_if_in_range(map, range->start, range->end,
535 mmu_notifier_range_blockable(range));
539 list_for_each_entry(map, &priv->freeable_maps, next) {
540 ret = unmap_if_in_range(map, range->start, range->end,
541 mmu_notifier_range_blockable(range));
547 mutex_unlock(&priv->lock);
552 static void mn_release(struct mmu_notifier *mn,
553 struct mm_struct *mm)
555 struct gntdev_priv *priv = container_of(mn, struct gntdev_priv, mn);
556 struct gntdev_grant_map *map;
559 mutex_lock(&priv->lock);
560 list_for_each_entry(map, &priv->maps, next) {
563 pr_debug("map %d+%d (%lx %lx)\n",
564 map->index, map->count,
565 map->vma->vm_start, map->vma->vm_end);
566 err = unmap_grant_pages(map, /* offset */ 0, map->count);
569 list_for_each_entry(map, &priv->freeable_maps, next) {
572 pr_debug("map %d+%d (%lx %lx)\n",
573 map->index, map->count,
574 map->vma->vm_start, map->vma->vm_end);
575 err = unmap_grant_pages(map, /* offset */ 0, map->count);
578 mutex_unlock(&priv->lock);
581 static const struct mmu_notifier_ops gntdev_mmu_ops = {
582 .release = mn_release,
583 .invalidate_range_start = mn_invl_range_start,
586 /* ------------------------------------------------------------------ */
588 static int gntdev_open(struct inode *inode, struct file *flip)
590 struct gntdev_priv *priv;
593 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
597 INIT_LIST_HEAD(&priv->maps);
598 INIT_LIST_HEAD(&priv->freeable_maps);
599 mutex_init(&priv->lock);
601 #ifdef CONFIG_XEN_GNTDEV_DMABUF
602 priv->dmabuf_priv = gntdev_dmabuf_init(flip);
603 if (IS_ERR(priv->dmabuf_priv)) {
604 ret = PTR_ERR(priv->dmabuf_priv);
611 priv->mm = get_task_mm(current);
616 priv->mn.ops = &gntdev_mmu_ops;
617 ret = mmu_notifier_register(&priv->mn, priv->mm);
626 flip->private_data = priv;
627 #ifdef CONFIG_XEN_GRANT_DMA_ALLOC
628 priv->dma_dev = gntdev_miscdev.this_device;
629 dma_coerce_mask_and_coherent(priv->dma_dev, DMA_BIT_MASK(64));
631 pr_debug("priv %p\n", priv);
636 static int gntdev_release(struct inode *inode, struct file *flip)
638 struct gntdev_priv *priv = flip->private_data;
639 struct gntdev_grant_map *map;
641 pr_debug("priv %p\n", priv);
643 mutex_lock(&priv->lock);
644 while (!list_empty(&priv->maps)) {
645 map = list_entry(priv->maps.next,
646 struct gntdev_grant_map, next);
647 list_del(&map->next);
648 gntdev_put_map(NULL /* already removed */, map);
650 WARN_ON(!list_empty(&priv->freeable_maps));
651 mutex_unlock(&priv->lock);
653 #ifdef CONFIG_XEN_GNTDEV_DMABUF
654 gntdev_dmabuf_fini(priv->dmabuf_priv);
658 mmu_notifier_unregister(&priv->mn, priv->mm);
664 static long gntdev_ioctl_map_grant_ref(struct gntdev_priv *priv,
665 struct ioctl_gntdev_map_grant_ref __user *u)
667 struct ioctl_gntdev_map_grant_ref op;
668 struct gntdev_grant_map *map;
671 if (copy_from_user(&op, u, sizeof(op)) != 0)
673 pr_debug("priv %p, add %d\n", priv, op.count);
674 if (unlikely(op.count <= 0))
678 map = gntdev_alloc_map(priv, op.count, 0 /* This is not a dma-buf. */);
682 if (unlikely(gntdev_account_mapped_pages(op.count))) {
683 pr_debug("can't map: over limit\n");
684 gntdev_put_map(NULL, map);
688 if (copy_from_user(map->grants, &u->refs,
689 sizeof(map->grants[0]) * op.count) != 0) {
690 gntdev_put_map(NULL, map);
694 mutex_lock(&priv->lock);
695 gntdev_add_map(priv, map);
696 op.index = map->index << PAGE_SHIFT;
697 mutex_unlock(&priv->lock);
699 if (copy_to_user(u, &op, sizeof(op)) != 0)
705 static long gntdev_ioctl_unmap_grant_ref(struct gntdev_priv *priv,
706 struct ioctl_gntdev_unmap_grant_ref __user *u)
708 struct ioctl_gntdev_unmap_grant_ref op;
709 struct gntdev_grant_map *map;
712 if (copy_from_user(&op, u, sizeof(op)) != 0)
714 pr_debug("priv %p, del %d+%d\n", priv, (int)op.index, (int)op.count);
716 mutex_lock(&priv->lock);
717 map = gntdev_find_map_index(priv, op.index >> PAGE_SHIFT, op.count);
719 list_del(&map->next);
720 if (populate_freeable_maps)
721 list_add_tail(&map->next, &priv->freeable_maps);
724 mutex_unlock(&priv->lock);
726 gntdev_put_map(priv, map);
730 static long gntdev_ioctl_get_offset_for_vaddr(struct gntdev_priv *priv,
731 struct ioctl_gntdev_get_offset_for_vaddr __user *u)
733 struct ioctl_gntdev_get_offset_for_vaddr op;
734 struct vm_area_struct *vma;
735 struct gntdev_grant_map *map;
738 if (copy_from_user(&op, u, sizeof(op)) != 0)
740 pr_debug("priv %p, offset for vaddr %lx\n", priv, (unsigned long)op.vaddr);
742 down_read(¤t->mm->mmap_sem);
743 vma = find_vma(current->mm, op.vaddr);
744 if (!vma || vma->vm_ops != &gntdev_vmops)
747 map = vma->vm_private_data;
751 op.offset = map->index << PAGE_SHIFT;
752 op.count = map->count;
756 up_read(¤t->mm->mmap_sem);
758 if (rv == 0 && copy_to_user(u, &op, sizeof(op)) != 0)
763 static long gntdev_ioctl_notify(struct gntdev_priv *priv, void __user *u)
765 struct ioctl_gntdev_unmap_notify op;
766 struct gntdev_grant_map *map;
769 unsigned int out_event;
771 if (copy_from_user(&op, u, sizeof(op)))
774 if (op.action & ~(UNMAP_NOTIFY_CLEAR_BYTE|UNMAP_NOTIFY_SEND_EVENT))
777 /* We need to grab a reference to the event channel we are going to use
778 * to send the notify before releasing the reference we may already have
779 * (if someone has called this ioctl twice). This is required so that
780 * it is possible to change the clear_byte part of the notification
781 * without disturbing the event channel part, which may now be the last
782 * reference to that event channel.
784 if (op.action & UNMAP_NOTIFY_SEND_EVENT) {
785 if (evtchn_get(op.event_channel_port))
789 out_flags = op.action;
790 out_event = op.event_channel_port;
792 mutex_lock(&priv->lock);
794 list_for_each_entry(map, &priv->maps, next) {
795 uint64_t begin = map->index << PAGE_SHIFT;
796 uint64_t end = (map->index + map->count) << PAGE_SHIFT;
797 if (op.index >= begin && op.index < end)
804 if ((op.action & UNMAP_NOTIFY_CLEAR_BYTE) &&
805 (map->flags & GNTMAP_readonly)) {
810 out_flags = map->notify.flags;
811 out_event = map->notify.event;
813 map->notify.flags = op.action;
814 map->notify.addr = op.index - (map->index << PAGE_SHIFT);
815 map->notify.event = op.event_channel_port;
820 mutex_unlock(&priv->lock);
822 /* Drop the reference to the event channel we did not save in the map */
823 if (out_flags & UNMAP_NOTIFY_SEND_EVENT)
824 evtchn_put(out_event);
829 #define GNTDEV_COPY_BATCH 16
831 struct gntdev_copy_batch {
832 struct gnttab_copy ops[GNTDEV_COPY_BATCH];
833 struct page *pages[GNTDEV_COPY_BATCH];
834 s16 __user *status[GNTDEV_COPY_BATCH];
836 unsigned int nr_pages;
840 static int gntdev_get_page(struct gntdev_copy_batch *batch, void __user *virt,
843 unsigned long addr = (unsigned long)virt;
845 unsigned long xen_pfn;
848 ret = get_user_pages_fast(addr, 1, batch->writeable ? FOLL_WRITE : 0, &page);
852 batch->pages[batch->nr_pages++] = page;
854 xen_pfn = page_to_xen_pfn(page) + XEN_PFN_DOWN(addr & ~PAGE_MASK);
855 *gfn = pfn_to_gfn(xen_pfn);
860 static void gntdev_put_pages(struct gntdev_copy_batch *batch)
864 for (i = 0; i < batch->nr_pages; i++) {
865 if (batch->writeable && !PageDirty(batch->pages[i]))
866 set_page_dirty_lock(batch->pages[i]);
867 put_page(batch->pages[i]);
870 batch->writeable = false;
873 static int gntdev_copy(struct gntdev_copy_batch *batch)
877 gnttab_batch_copy(batch->ops, batch->nr_ops);
878 gntdev_put_pages(batch);
881 * For each completed op, update the status if the op failed
882 * and all previous ops for the segment were successful.
884 for (i = 0; i < batch->nr_ops; i++) {
885 s16 status = batch->ops[i].status;
888 if (status == GNTST_okay)
891 if (__get_user(old_status, batch->status[i]))
894 if (old_status != GNTST_okay)
897 if (__put_user(status, batch->status[i]))
905 static int gntdev_grant_copy_seg(struct gntdev_copy_batch *batch,
906 struct gntdev_grant_copy_segment *seg,
912 * Disallow local -> local copies since there is only space in
913 * batch->pages for one page per-op and this would be a very
914 * expensive memcpy().
916 if (!(seg->flags & (GNTCOPY_source_gref | GNTCOPY_dest_gref)))
919 /* Can't cross page if source/dest is a grant ref. */
920 if (seg->flags & GNTCOPY_source_gref) {
921 if (seg->source.foreign.offset + seg->len > XEN_PAGE_SIZE)
924 if (seg->flags & GNTCOPY_dest_gref) {
925 if (seg->dest.foreign.offset + seg->len > XEN_PAGE_SIZE)
929 if (put_user(GNTST_okay, status))
932 while (copied < seg->len) {
933 struct gnttab_copy *op;
939 if (batch->nr_ops >= GNTDEV_COPY_BATCH) {
940 ret = gntdev_copy(batch);
945 len = seg->len - copied;
947 op = &batch->ops[batch->nr_ops];
950 if (seg->flags & GNTCOPY_source_gref) {
951 op->source.u.ref = seg->source.foreign.ref;
952 op->source.domid = seg->source.foreign.domid;
953 op->source.offset = seg->source.foreign.offset + copied;
954 op->flags |= GNTCOPY_source_gref;
956 virt = seg->source.virt + copied;
957 off = (unsigned long)virt & ~XEN_PAGE_MASK;
958 len = min(len, (size_t)XEN_PAGE_SIZE - off);
959 batch->writeable = false;
961 ret = gntdev_get_page(batch, virt, &gfn);
965 op->source.u.gmfn = gfn;
966 op->source.domid = DOMID_SELF;
967 op->source.offset = off;
970 if (seg->flags & GNTCOPY_dest_gref) {
971 op->dest.u.ref = seg->dest.foreign.ref;
972 op->dest.domid = seg->dest.foreign.domid;
973 op->dest.offset = seg->dest.foreign.offset + copied;
974 op->flags |= GNTCOPY_dest_gref;
976 virt = seg->dest.virt + copied;
977 off = (unsigned long)virt & ~XEN_PAGE_MASK;
978 len = min(len, (size_t)XEN_PAGE_SIZE - off);
979 batch->writeable = true;
981 ret = gntdev_get_page(batch, virt, &gfn);
985 op->dest.u.gmfn = gfn;
986 op->dest.domid = DOMID_SELF;
987 op->dest.offset = off;
993 batch->status[batch->nr_ops] = status;
1000 static long gntdev_ioctl_grant_copy(struct gntdev_priv *priv, void __user *u)
1002 struct ioctl_gntdev_grant_copy copy;
1003 struct gntdev_copy_batch batch;
1007 if (copy_from_user(©, u, sizeof(copy)))
1013 for (i = 0; i < copy.count; i++) {
1014 struct gntdev_grant_copy_segment seg;
1016 if (copy_from_user(&seg, ©.segments[i], sizeof(seg))) {
1021 ret = gntdev_grant_copy_seg(&batch, &seg, ©.segments[i].status);
1028 ret = gntdev_copy(&batch);
1032 gntdev_put_pages(&batch);
1036 static long gntdev_ioctl(struct file *flip,
1037 unsigned int cmd, unsigned long arg)
1039 struct gntdev_priv *priv = flip->private_data;
1040 void __user *ptr = (void __user *)arg;
1043 case IOCTL_GNTDEV_MAP_GRANT_REF:
1044 return gntdev_ioctl_map_grant_ref(priv, ptr);
1046 case IOCTL_GNTDEV_UNMAP_GRANT_REF:
1047 return gntdev_ioctl_unmap_grant_ref(priv, ptr);
1049 case IOCTL_GNTDEV_GET_OFFSET_FOR_VADDR:
1050 return gntdev_ioctl_get_offset_for_vaddr(priv, ptr);
1052 case IOCTL_GNTDEV_SET_UNMAP_NOTIFY:
1053 return gntdev_ioctl_notify(priv, ptr);
1055 case IOCTL_GNTDEV_GRANT_COPY:
1056 return gntdev_ioctl_grant_copy(priv, ptr);
1058 #ifdef CONFIG_XEN_GNTDEV_DMABUF
1059 case IOCTL_GNTDEV_DMABUF_EXP_FROM_REFS:
1060 return gntdev_ioctl_dmabuf_exp_from_refs(priv, use_ptemod, ptr);
1062 case IOCTL_GNTDEV_DMABUF_EXP_WAIT_RELEASED:
1063 return gntdev_ioctl_dmabuf_exp_wait_released(priv, ptr);
1065 case IOCTL_GNTDEV_DMABUF_IMP_TO_REFS:
1066 return gntdev_ioctl_dmabuf_imp_to_refs(priv, ptr);
1068 case IOCTL_GNTDEV_DMABUF_IMP_RELEASE:
1069 return gntdev_ioctl_dmabuf_imp_release(priv, ptr);
1073 pr_debug("priv %p, unknown cmd %x\n", priv, cmd);
1074 return -ENOIOCTLCMD;
1080 static int gntdev_mmap(struct file *flip, struct vm_area_struct *vma)
1082 struct gntdev_priv *priv = flip->private_data;
1083 int index = vma->vm_pgoff;
1084 int count = vma_pages(vma);
1085 struct gntdev_grant_map *map;
1088 if ((vma->vm_flags & VM_WRITE) && !(vma->vm_flags & VM_SHARED))
1091 pr_debug("map %d+%d at %lx (pgoff %lx)\n",
1092 index, count, vma->vm_start, vma->vm_pgoff);
1094 mutex_lock(&priv->lock);
1095 map = gntdev_find_map_index(priv, index, count);
1098 if (use_ptemod && map->vma)
1100 if (use_ptemod && priv->mm != vma->vm_mm) {
1101 pr_warn("Huh? Other mm?\n");
1105 refcount_inc(&map->users);
1107 vma->vm_ops = &gntdev_vmops;
1109 vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP | VM_MIXEDMAP;
1112 vma->vm_flags |= VM_DONTCOPY;
1114 vma->vm_private_data = map;
1120 if ((vma->vm_flags & VM_WRITE) &&
1121 (map->flags & GNTMAP_readonly))
1122 goto out_unlock_put;
1124 map->flags = GNTMAP_host_map;
1125 if (!(vma->vm_flags & VM_WRITE))
1126 map->flags |= GNTMAP_readonly;
1129 mutex_unlock(&priv->lock);
1132 map->pages_vm_start = vma->vm_start;
1133 err = apply_to_page_range(vma->vm_mm, vma->vm_start,
1134 vma->vm_end - vma->vm_start,
1135 find_grant_ptes, map);
1137 pr_warn("find_grant_ptes() failure.\n");
1142 err = gntdev_map_grant_pages(map);
1147 err = vm_map_pages_zero(vma, map->pages, map->count);
1153 * If the PTEs were not made special by the grant map
1154 * hypercall, do so here.
1156 * This is racy since the mapping is already visible
1157 * to userspace but userspace should be well-behaved
1158 * enough to not touch it until the mmap() call
1161 if (!xen_feature(XENFEAT_gnttab_map_avail_bits)) {
1162 apply_to_page_range(vma->vm_mm, vma->vm_start,
1163 vma->vm_end - vma->vm_start,
1164 set_grant_ptes_as_special, NULL);
1172 mutex_unlock(&priv->lock);
1176 mutex_unlock(&priv->lock);
1180 unmap_grant_pages(map, 0, map->count);
1182 gntdev_put_map(priv, map);
1186 static const struct file_operations gntdev_fops = {
1187 .owner = THIS_MODULE,
1188 .open = gntdev_open,
1189 .release = gntdev_release,
1190 .mmap = gntdev_mmap,
1191 .unlocked_ioctl = gntdev_ioctl
1194 static struct miscdevice gntdev_miscdev = {
1195 .minor = MISC_DYNAMIC_MINOR,
1196 .name = "xen/gntdev",
1197 .fops = &gntdev_fops,
1200 /* ------------------------------------------------------------------ */
1202 static int __init gntdev_init(void)
1209 use_ptemod = !xen_feature(XENFEAT_auto_translated_physmap);
1211 err = misc_register(&gntdev_miscdev);
1213 pr_err("Could not register gntdev device\n");
1219 static void __exit gntdev_exit(void)
1221 misc_deregister(&gntdev_miscdev);
1224 module_init(gntdev_init);
1225 module_exit(gntdev_exit);
1227 /* ------------------------------------------------------------------ */