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>
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #define pr_fmt(fmt) "xen:" KBUILD_MODNAME ": " fmt
24 #include <linux/module.h>
25 #include <linux/kernel.h>
26 #include <linux/init.h>
27 #include <linux/miscdevice.h>
30 #include <linux/mman.h>
31 #include <linux/mmu_notifier.h>
32 #include <linux/types.h>
33 #include <linux/uaccess.h>
34 #include <linux/sched.h>
35 #include <linux/sched/mm.h>
36 #include <linux/spinlock.h>
37 #include <linux/slab.h>
38 #include <linux/highmem.h>
41 #include <xen/grant_table.h>
42 #include <xen/balloon.h>
43 #include <xen/gntdev.h>
44 #include <xen/events.h>
46 #include <asm/xen/hypervisor.h>
47 #include <asm/xen/hypercall.h>
49 MODULE_LICENSE("GPL");
50 MODULE_AUTHOR("Derek G. Murray <Derek.Murray@cl.cam.ac.uk>, "
51 "Gerd Hoffmann <kraxel@redhat.com>");
52 MODULE_DESCRIPTION("User-space granted page access driver");
54 static int limit = 1024*1024;
55 module_param(limit, int, 0644);
56 MODULE_PARM_DESC(limit, "Maximum number of grants that may be mapped by "
59 static atomic_t pages_mapped = ATOMIC_INIT(0);
61 static int use_ptemod;
62 #define populate_freeable_maps use_ptemod
65 /* maps with visible offsets in the file descriptor */
66 struct list_head maps;
67 /* maps that are not visible; will be freed on munmap.
68 * Only populated if populate_freeable_maps == 1 */
69 struct list_head freeable_maps;
70 /* lock protects maps and freeable_maps */
73 struct mmu_notifier mn;
78 /* Address relative to the start of the grant_map */
84 struct list_head next;
85 struct vm_area_struct *vma;
90 struct unmap_notify notify;
91 struct ioctl_gntdev_grant_ref *grants;
92 struct gnttab_map_grant_ref *map_ops;
93 struct gnttab_unmap_grant_ref *unmap_ops;
94 struct gnttab_map_grant_ref *kmap_ops;
95 struct gnttab_unmap_grant_ref *kunmap_ops;
97 unsigned long pages_vm_start;
100 static int unmap_grant_pages(struct grant_map *map, int offset, int pages);
102 /* ------------------------------------------------------------------ */
104 static void gntdev_print_maps(struct gntdev_priv *priv,
105 char *text, int text_index)
108 struct grant_map *map;
110 pr_debug("%s: maps list (priv %p)\n", __func__, priv);
111 list_for_each_entry(map, &priv->maps, next)
112 pr_debug(" index %2d, count %2d %s\n",
113 map->index, map->count,
114 map->index == text_index && text ? text : "");
118 static void gntdev_free_map(struct grant_map *map)
124 gnttab_free_pages(map->count, map->pages);
128 kfree(map->unmap_ops);
129 kfree(map->kmap_ops);
130 kfree(map->kunmap_ops);
134 static struct grant_map *gntdev_alloc_map(struct gntdev_priv *priv, int count)
136 struct grant_map *add;
139 add = kzalloc(sizeof(struct grant_map), GFP_KERNEL);
143 add->grants = kcalloc(count, sizeof(add->grants[0]), GFP_KERNEL);
144 add->map_ops = kcalloc(count, sizeof(add->map_ops[0]), GFP_KERNEL);
145 add->unmap_ops = kcalloc(count, sizeof(add->unmap_ops[0]), GFP_KERNEL);
146 add->kmap_ops = kcalloc(count, sizeof(add->kmap_ops[0]), GFP_KERNEL);
147 add->kunmap_ops = kcalloc(count, sizeof(add->kunmap_ops[0]), GFP_KERNEL);
148 add->pages = kcalloc(count, sizeof(add->pages[0]), GFP_KERNEL);
149 if (NULL == add->grants ||
150 NULL == add->map_ops ||
151 NULL == add->unmap_ops ||
152 NULL == add->kmap_ops ||
153 NULL == add->kunmap_ops ||
157 if (gnttab_alloc_pages(count, add->pages))
160 for (i = 0; i < count; i++) {
161 add->map_ops[i].handle = -1;
162 add->unmap_ops[i].handle = -1;
163 add->kmap_ops[i].handle = -1;
164 add->kunmap_ops[i].handle = -1;
169 atomic_set(&add->users, 1);
174 gntdev_free_map(add);
178 static void gntdev_add_map(struct gntdev_priv *priv, struct grant_map *add)
180 struct grant_map *map;
182 list_for_each_entry(map, &priv->maps, next) {
183 if (add->index + add->count < map->index) {
184 list_add_tail(&add->next, &map->next);
187 add->index = map->index + map->count;
189 list_add_tail(&add->next, &priv->maps);
192 gntdev_print_maps(priv, "[new]", add->index);
195 static struct grant_map *gntdev_find_map_index(struct gntdev_priv *priv,
196 int index, int count)
198 struct grant_map *map;
200 list_for_each_entry(map, &priv->maps, next) {
201 if (map->index != index)
203 if (count && map->count != count)
210 static void gntdev_put_map(struct gntdev_priv *priv, struct grant_map *map)
215 if (!atomic_dec_and_test(&map->users))
218 atomic_sub(map->count, &pages_mapped);
220 if (map->notify.flags & UNMAP_NOTIFY_SEND_EVENT) {
221 notify_remote_via_evtchn(map->notify.event);
222 evtchn_put(map->notify.event);
225 if (populate_freeable_maps && priv) {
226 mutex_lock(&priv->lock);
227 list_del(&map->next);
228 mutex_unlock(&priv->lock);
231 if (map->pages && !use_ptemod)
232 unmap_grant_pages(map, 0, map->count);
233 gntdev_free_map(map);
236 /* ------------------------------------------------------------------ */
238 static int find_grant_ptes(pte_t *pte, pgtable_t token,
239 unsigned long addr, void *data)
241 struct grant_map *map = data;
242 unsigned int pgnr = (addr - map->vma->vm_start) >> PAGE_SHIFT;
243 int flags = map->flags | GNTMAP_application_map | GNTMAP_contains_pte;
246 BUG_ON(pgnr >= map->count);
247 pte_maddr = arbitrary_virt_to_machine(pte).maddr;
250 * Set the PTE as special to force get_user_pages_fast() fall
251 * back to the slow path. If this is not supported as part of
252 * the grant map, it will be done afterwards.
254 if (xen_feature(XENFEAT_gnttab_map_avail_bits))
255 flags |= (1 << _GNTMAP_guest_avail0);
257 gnttab_set_map_op(&map->map_ops[pgnr], pte_maddr, flags,
258 map->grants[pgnr].ref,
259 map->grants[pgnr].domid);
260 gnttab_set_unmap_op(&map->unmap_ops[pgnr], pte_maddr, flags,
266 static int set_grant_ptes_as_special(pte_t *pte, pgtable_t token,
267 unsigned long addr, void *data)
269 set_pte_at(current->mm, addr, pte, pte_mkspecial(*pte));
274 static int map_grant_pages(struct grant_map *map)
279 /* Note: it could already be mapped */
280 if (map->map_ops[0].handle != -1)
282 for (i = 0; i < map->count; i++) {
283 unsigned long addr = (unsigned long)
284 pfn_to_kaddr(page_to_pfn(map->pages[i]));
285 gnttab_set_map_op(&map->map_ops[i], addr, map->flags,
287 map->grants[i].domid);
288 gnttab_set_unmap_op(&map->unmap_ops[i], addr,
289 map->flags, -1 /* handle */);
293 * Setup the map_ops corresponding to the pte entries pointing
294 * to the kernel linear addresses of the struct pages.
295 * These ptes are completely different from the user ptes dealt
296 * with find_grant_ptes.
298 for (i = 0; i < map->count; i++) {
299 unsigned long address = (unsigned long)
300 pfn_to_kaddr(page_to_pfn(map->pages[i]));
301 BUG_ON(PageHighMem(map->pages[i]));
303 gnttab_set_map_op(&map->kmap_ops[i], address,
304 map->flags | GNTMAP_host_map,
306 map->grants[i].domid);
307 gnttab_set_unmap_op(&map->kunmap_ops[i], address,
308 map->flags | GNTMAP_host_map, -1);
312 pr_debug("map %d+%d\n", map->index, map->count);
313 err = gnttab_map_refs(map->map_ops, use_ptemod ? map->kmap_ops : NULL,
314 map->pages, map->count);
318 for (i = 0; i < map->count; i++) {
319 if (map->map_ops[i].status) {
324 map->unmap_ops[i].handle = map->map_ops[i].handle;
326 map->kunmap_ops[i].handle = map->kmap_ops[i].handle;
331 static int __unmap_grant_pages(struct grant_map *map, int offset, int pages)
334 struct gntab_unmap_queue_data unmap_data;
336 if (map->notify.flags & UNMAP_NOTIFY_CLEAR_BYTE) {
337 int pgno = (map->notify.addr >> PAGE_SHIFT);
338 if (pgno >= offset && pgno < offset + pages) {
339 /* No need for kmap, pages are in lowmem */
340 uint8_t *tmp = pfn_to_kaddr(page_to_pfn(map->pages[pgno]));
341 tmp[map->notify.addr & (PAGE_SIZE-1)] = 0;
342 map->notify.flags &= ~UNMAP_NOTIFY_CLEAR_BYTE;
346 unmap_data.unmap_ops = map->unmap_ops + offset;
347 unmap_data.kunmap_ops = use_ptemod ? map->kunmap_ops + offset : NULL;
348 unmap_data.pages = map->pages + offset;
349 unmap_data.count = pages;
351 err = gnttab_unmap_refs_sync(&unmap_data);
355 for (i = 0; i < pages; i++) {
356 if (map->unmap_ops[offset+i].status)
358 pr_debug("unmap handle=%d st=%d\n",
359 map->unmap_ops[offset+i].handle,
360 map->unmap_ops[offset+i].status);
361 map->unmap_ops[offset+i].handle = -1;
366 static int unmap_grant_pages(struct grant_map *map, int offset, int pages)
370 pr_debug("unmap %d+%d [%d+%d]\n", map->index, map->count, offset, pages);
372 /* It is possible the requested range will have a "hole" where we
373 * already unmapped some of the grants. Only unmap valid ranges.
375 while (pages && !err) {
376 while (pages && map->unmap_ops[offset].handle == -1) {
381 while (range < pages) {
382 if (map->unmap_ops[offset+range].handle == -1) {
388 err = __unmap_grant_pages(map, offset, range);
396 /* ------------------------------------------------------------------ */
398 static void gntdev_vma_open(struct vm_area_struct *vma)
400 struct grant_map *map = vma->vm_private_data;
402 pr_debug("gntdev_vma_open %p\n", vma);
403 atomic_inc(&map->users);
406 static void gntdev_vma_close(struct vm_area_struct *vma)
408 struct grant_map *map = vma->vm_private_data;
409 struct file *file = vma->vm_file;
410 struct gntdev_priv *priv = file->private_data;
412 pr_debug("gntdev_vma_close %p\n", vma);
414 /* It is possible that an mmu notifier could be running
415 * concurrently, so take priv->lock to ensure that the vma won't
416 * vanishing during the unmap_grant_pages call, since we will
417 * spin here until that completes. Such a concurrent call will
418 * not do any unmapping, since that has been done prior to
419 * closing the vma, but it may still iterate the unmap_ops list.
421 mutex_lock(&priv->lock);
423 mutex_unlock(&priv->lock);
425 vma->vm_private_data = NULL;
426 gntdev_put_map(priv, map);
429 static struct page *gntdev_vma_find_special_page(struct vm_area_struct *vma,
432 struct grant_map *map = vma->vm_private_data;
434 return map->pages[(addr - map->pages_vm_start) >> PAGE_SHIFT];
437 static const struct vm_operations_struct gntdev_vmops = {
438 .open = gntdev_vma_open,
439 .close = gntdev_vma_close,
440 .find_special_page = gntdev_vma_find_special_page,
443 /* ------------------------------------------------------------------ */
445 static void unmap_if_in_range(struct grant_map *map,
446 unsigned long start, unsigned long end)
448 unsigned long mstart, mend;
453 if (map->vma->vm_start >= end)
455 if (map->vma->vm_end <= start)
457 mstart = max(start, map->vma->vm_start);
458 mend = min(end, map->vma->vm_end);
459 pr_debug("map %d+%d (%lx %lx), range %lx %lx, mrange %lx %lx\n",
460 map->index, map->count,
461 map->vma->vm_start, map->vma->vm_end,
462 start, end, mstart, mend);
463 err = unmap_grant_pages(map,
464 (mstart - map->vma->vm_start) >> PAGE_SHIFT,
465 (mend - mstart) >> PAGE_SHIFT);
469 static void mn_invl_range_start(struct mmu_notifier *mn,
470 struct mm_struct *mm,
471 unsigned long start, unsigned long end)
473 struct gntdev_priv *priv = container_of(mn, struct gntdev_priv, mn);
474 struct grant_map *map;
476 mutex_lock(&priv->lock);
477 list_for_each_entry(map, &priv->maps, next) {
478 unmap_if_in_range(map, start, end);
480 list_for_each_entry(map, &priv->freeable_maps, next) {
481 unmap_if_in_range(map, start, end);
483 mutex_unlock(&priv->lock);
486 static void mn_invl_page(struct mmu_notifier *mn,
487 struct mm_struct *mm,
488 unsigned long address)
490 mn_invl_range_start(mn, mm, address, address + PAGE_SIZE);
493 static void mn_release(struct mmu_notifier *mn,
494 struct mm_struct *mm)
496 struct gntdev_priv *priv = container_of(mn, struct gntdev_priv, mn);
497 struct grant_map *map;
500 mutex_lock(&priv->lock);
501 list_for_each_entry(map, &priv->maps, next) {
504 pr_debug("map %d+%d (%lx %lx)\n",
505 map->index, map->count,
506 map->vma->vm_start, map->vma->vm_end);
507 err = unmap_grant_pages(map, /* offset */ 0, map->count);
510 list_for_each_entry(map, &priv->freeable_maps, next) {
513 pr_debug("map %d+%d (%lx %lx)\n",
514 map->index, map->count,
515 map->vma->vm_start, map->vma->vm_end);
516 err = unmap_grant_pages(map, /* offset */ 0, map->count);
519 mutex_unlock(&priv->lock);
522 static const struct mmu_notifier_ops gntdev_mmu_ops = {
523 .release = mn_release,
524 .invalidate_page = mn_invl_page,
525 .invalidate_range_start = mn_invl_range_start,
528 /* ------------------------------------------------------------------ */
530 static int gntdev_open(struct inode *inode, struct file *flip)
532 struct gntdev_priv *priv;
535 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
539 INIT_LIST_HEAD(&priv->maps);
540 INIT_LIST_HEAD(&priv->freeable_maps);
541 mutex_init(&priv->lock);
544 priv->mm = get_task_mm(current);
549 priv->mn.ops = &gntdev_mmu_ops;
550 ret = mmu_notifier_register(&priv->mn, priv->mm);
559 flip->private_data = priv;
560 pr_debug("priv %p\n", priv);
565 static int gntdev_release(struct inode *inode, struct file *flip)
567 struct gntdev_priv *priv = flip->private_data;
568 struct grant_map *map;
570 pr_debug("priv %p\n", priv);
572 mutex_lock(&priv->lock);
573 while (!list_empty(&priv->maps)) {
574 map = list_entry(priv->maps.next, struct grant_map, next);
575 list_del(&map->next);
576 gntdev_put_map(NULL /* already removed */, map);
578 WARN_ON(!list_empty(&priv->freeable_maps));
579 mutex_unlock(&priv->lock);
582 mmu_notifier_unregister(&priv->mn, priv->mm);
587 static long gntdev_ioctl_map_grant_ref(struct gntdev_priv *priv,
588 struct ioctl_gntdev_map_grant_ref __user *u)
590 struct ioctl_gntdev_map_grant_ref op;
591 struct grant_map *map;
594 if (copy_from_user(&op, u, sizeof(op)) != 0)
596 pr_debug("priv %p, add %d\n", priv, op.count);
597 if (unlikely(op.count <= 0))
601 map = gntdev_alloc_map(priv, op.count);
605 if (unlikely(atomic_add_return(op.count, &pages_mapped) > limit)) {
606 pr_debug("can't map: over limit\n");
607 gntdev_put_map(NULL, map);
611 if (copy_from_user(map->grants, &u->refs,
612 sizeof(map->grants[0]) * op.count) != 0) {
613 gntdev_put_map(NULL, map);
617 mutex_lock(&priv->lock);
618 gntdev_add_map(priv, map);
619 op.index = map->index << PAGE_SHIFT;
620 mutex_unlock(&priv->lock);
622 if (copy_to_user(u, &op, sizeof(op)) != 0)
628 static long gntdev_ioctl_unmap_grant_ref(struct gntdev_priv *priv,
629 struct ioctl_gntdev_unmap_grant_ref __user *u)
631 struct ioctl_gntdev_unmap_grant_ref op;
632 struct grant_map *map;
635 if (copy_from_user(&op, u, sizeof(op)) != 0)
637 pr_debug("priv %p, del %d+%d\n", priv, (int)op.index, (int)op.count);
639 mutex_lock(&priv->lock);
640 map = gntdev_find_map_index(priv, op.index >> PAGE_SHIFT, op.count);
642 list_del(&map->next);
643 if (populate_freeable_maps)
644 list_add_tail(&map->next, &priv->freeable_maps);
647 mutex_unlock(&priv->lock);
649 gntdev_put_map(priv, map);
653 static long gntdev_ioctl_get_offset_for_vaddr(struct gntdev_priv *priv,
654 struct ioctl_gntdev_get_offset_for_vaddr __user *u)
656 struct ioctl_gntdev_get_offset_for_vaddr op;
657 struct vm_area_struct *vma;
658 struct grant_map *map;
661 if (copy_from_user(&op, u, sizeof(op)) != 0)
663 pr_debug("priv %p, offset for vaddr %lx\n", priv, (unsigned long)op.vaddr);
665 down_read(¤t->mm->mmap_sem);
666 vma = find_vma(current->mm, op.vaddr);
667 if (!vma || vma->vm_ops != &gntdev_vmops)
670 map = vma->vm_private_data;
674 op.offset = map->index << PAGE_SHIFT;
675 op.count = map->count;
679 up_read(¤t->mm->mmap_sem);
681 if (rv == 0 && copy_to_user(u, &op, sizeof(op)) != 0)
686 static long gntdev_ioctl_notify(struct gntdev_priv *priv, void __user *u)
688 struct ioctl_gntdev_unmap_notify op;
689 struct grant_map *map;
692 unsigned int out_event;
694 if (copy_from_user(&op, u, sizeof(op)))
697 if (op.action & ~(UNMAP_NOTIFY_CLEAR_BYTE|UNMAP_NOTIFY_SEND_EVENT))
700 /* We need to grab a reference to the event channel we are going to use
701 * to send the notify before releasing the reference we may already have
702 * (if someone has called this ioctl twice). This is required so that
703 * it is possible to change the clear_byte part of the notification
704 * without disturbing the event channel part, which may now be the last
705 * reference to that event channel.
707 if (op.action & UNMAP_NOTIFY_SEND_EVENT) {
708 if (evtchn_get(op.event_channel_port))
712 out_flags = op.action;
713 out_event = op.event_channel_port;
715 mutex_lock(&priv->lock);
717 list_for_each_entry(map, &priv->maps, next) {
718 uint64_t begin = map->index << PAGE_SHIFT;
719 uint64_t end = (map->index + map->count) << PAGE_SHIFT;
720 if (op.index >= begin && op.index < end)
727 if ((op.action & UNMAP_NOTIFY_CLEAR_BYTE) &&
728 (map->flags & GNTMAP_readonly)) {
733 out_flags = map->notify.flags;
734 out_event = map->notify.event;
736 map->notify.flags = op.action;
737 map->notify.addr = op.index - (map->index << PAGE_SHIFT);
738 map->notify.event = op.event_channel_port;
743 mutex_unlock(&priv->lock);
745 /* Drop the reference to the event channel we did not save in the map */
746 if (out_flags & UNMAP_NOTIFY_SEND_EVENT)
747 evtchn_put(out_event);
752 #define GNTDEV_COPY_BATCH 16
754 struct gntdev_copy_batch {
755 struct gnttab_copy ops[GNTDEV_COPY_BATCH];
756 struct page *pages[GNTDEV_COPY_BATCH];
757 s16 __user *status[GNTDEV_COPY_BATCH];
759 unsigned int nr_pages;
762 static int gntdev_get_page(struct gntdev_copy_batch *batch, void __user *virt,
763 bool writeable, unsigned long *gfn)
765 unsigned long addr = (unsigned long)virt;
767 unsigned long xen_pfn;
770 ret = get_user_pages_fast(addr, 1, writeable, &page);
774 batch->pages[batch->nr_pages++] = page;
776 xen_pfn = page_to_xen_pfn(page) + XEN_PFN_DOWN(addr & ~PAGE_MASK);
777 *gfn = pfn_to_gfn(xen_pfn);
782 static void gntdev_put_pages(struct gntdev_copy_batch *batch)
786 for (i = 0; i < batch->nr_pages; i++)
787 put_page(batch->pages[i]);
791 static int gntdev_copy(struct gntdev_copy_batch *batch)
795 gnttab_batch_copy(batch->ops, batch->nr_ops);
796 gntdev_put_pages(batch);
799 * For each completed op, update the status if the op failed
800 * and all previous ops for the segment were successful.
802 for (i = 0; i < batch->nr_ops; i++) {
803 s16 status = batch->ops[i].status;
806 if (status == GNTST_okay)
809 if (__get_user(old_status, batch->status[i]))
812 if (old_status != GNTST_okay)
815 if (__put_user(status, batch->status[i]))
823 static int gntdev_grant_copy_seg(struct gntdev_copy_batch *batch,
824 struct gntdev_grant_copy_segment *seg,
830 * Disallow local -> local copies since there is only space in
831 * batch->pages for one page per-op and this would be a very
832 * expensive memcpy().
834 if (!(seg->flags & (GNTCOPY_source_gref | GNTCOPY_dest_gref)))
837 /* Can't cross page if source/dest is a grant ref. */
838 if (seg->flags & GNTCOPY_source_gref) {
839 if (seg->source.foreign.offset + seg->len > XEN_PAGE_SIZE)
842 if (seg->flags & GNTCOPY_dest_gref) {
843 if (seg->dest.foreign.offset + seg->len > XEN_PAGE_SIZE)
847 if (put_user(GNTST_okay, status))
850 while (copied < seg->len) {
851 struct gnttab_copy *op;
857 if (batch->nr_ops >= GNTDEV_COPY_BATCH) {
858 ret = gntdev_copy(batch);
863 len = seg->len - copied;
865 op = &batch->ops[batch->nr_ops];
868 if (seg->flags & GNTCOPY_source_gref) {
869 op->source.u.ref = seg->source.foreign.ref;
870 op->source.domid = seg->source.foreign.domid;
871 op->source.offset = seg->source.foreign.offset + copied;
872 op->flags |= GNTCOPY_source_gref;
874 virt = seg->source.virt + copied;
875 off = (unsigned long)virt & ~XEN_PAGE_MASK;
876 len = min(len, (size_t)XEN_PAGE_SIZE - off);
878 ret = gntdev_get_page(batch, virt, false, &gfn);
882 op->source.u.gmfn = gfn;
883 op->source.domid = DOMID_SELF;
884 op->source.offset = off;
887 if (seg->flags & GNTCOPY_dest_gref) {
888 op->dest.u.ref = seg->dest.foreign.ref;
889 op->dest.domid = seg->dest.foreign.domid;
890 op->dest.offset = seg->dest.foreign.offset + copied;
891 op->flags |= GNTCOPY_dest_gref;
893 virt = seg->dest.virt + copied;
894 off = (unsigned long)virt & ~XEN_PAGE_MASK;
895 len = min(len, (size_t)XEN_PAGE_SIZE - off);
897 ret = gntdev_get_page(batch, virt, true, &gfn);
901 op->dest.u.gmfn = gfn;
902 op->dest.domid = DOMID_SELF;
903 op->dest.offset = off;
909 batch->status[batch->nr_ops] = status;
916 static long gntdev_ioctl_grant_copy(struct gntdev_priv *priv, void __user *u)
918 struct ioctl_gntdev_grant_copy copy;
919 struct gntdev_copy_batch batch;
923 if (copy_from_user(©, u, sizeof(copy)))
929 for (i = 0; i < copy.count; i++) {
930 struct gntdev_grant_copy_segment seg;
932 if (copy_from_user(&seg, ©.segments[i], sizeof(seg))) {
937 ret = gntdev_grant_copy_seg(&batch, &seg, ©.segments[i].status);
944 ret = gntdev_copy(&batch);
948 gntdev_put_pages(&batch);
952 static long gntdev_ioctl(struct file *flip,
953 unsigned int cmd, unsigned long arg)
955 struct gntdev_priv *priv = flip->private_data;
956 void __user *ptr = (void __user *)arg;
959 case IOCTL_GNTDEV_MAP_GRANT_REF:
960 return gntdev_ioctl_map_grant_ref(priv, ptr);
962 case IOCTL_GNTDEV_UNMAP_GRANT_REF:
963 return gntdev_ioctl_unmap_grant_ref(priv, ptr);
965 case IOCTL_GNTDEV_GET_OFFSET_FOR_VADDR:
966 return gntdev_ioctl_get_offset_for_vaddr(priv, ptr);
968 case IOCTL_GNTDEV_SET_UNMAP_NOTIFY:
969 return gntdev_ioctl_notify(priv, ptr);
971 case IOCTL_GNTDEV_GRANT_COPY:
972 return gntdev_ioctl_grant_copy(priv, ptr);
975 pr_debug("priv %p, unknown cmd %x\n", priv, cmd);
982 static int gntdev_mmap(struct file *flip, struct vm_area_struct *vma)
984 struct gntdev_priv *priv = flip->private_data;
985 int index = vma->vm_pgoff;
986 int count = vma_pages(vma);
987 struct grant_map *map;
988 int i, err = -EINVAL;
990 if ((vma->vm_flags & VM_WRITE) && !(vma->vm_flags & VM_SHARED))
993 pr_debug("map %d+%d at %lx (pgoff %lx)\n",
994 index, count, vma->vm_start, vma->vm_pgoff);
996 mutex_lock(&priv->lock);
997 map = gntdev_find_map_index(priv, index, count);
1000 if (use_ptemod && map->vma)
1002 if (use_ptemod && priv->mm != vma->vm_mm) {
1003 pr_warn("Huh? Other mm?\n");
1007 atomic_inc(&map->users);
1009 vma->vm_ops = &gntdev_vmops;
1011 vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP | VM_MIXEDMAP;
1014 vma->vm_flags |= VM_DONTCOPY;
1016 vma->vm_private_data = map;
1022 if ((vma->vm_flags & VM_WRITE) &&
1023 (map->flags & GNTMAP_readonly))
1024 goto out_unlock_put;
1026 map->flags = GNTMAP_host_map;
1027 if (!(vma->vm_flags & VM_WRITE))
1028 map->flags |= GNTMAP_readonly;
1031 mutex_unlock(&priv->lock);
1034 err = apply_to_page_range(vma->vm_mm, vma->vm_start,
1035 vma->vm_end - vma->vm_start,
1036 find_grant_ptes, map);
1038 pr_warn("find_grant_ptes() failure.\n");
1043 err = map_grant_pages(map);
1048 for (i = 0; i < count; i++) {
1049 err = vm_insert_page(vma, vma->vm_start + i*PAGE_SIZE,
1057 * If the PTEs were not made special by the grant map
1058 * hypercall, do so here.
1060 * This is racy since the mapping is already visible
1061 * to userspace but userspace should be well-behaved
1062 * enough to not touch it until the mmap() call
1065 if (!xen_feature(XENFEAT_gnttab_map_avail_bits)) {
1066 apply_to_page_range(vma->vm_mm, vma->vm_start,
1067 vma->vm_end - vma->vm_start,
1068 set_grant_ptes_as_special, NULL);
1071 map->pages_vm_start = vma->vm_start;
1077 mutex_unlock(&priv->lock);
1081 mutex_unlock(&priv->lock);
1085 gntdev_put_map(priv, map);
1089 static const struct file_operations gntdev_fops = {
1090 .owner = THIS_MODULE,
1091 .open = gntdev_open,
1092 .release = gntdev_release,
1093 .mmap = gntdev_mmap,
1094 .unlocked_ioctl = gntdev_ioctl
1097 static struct miscdevice gntdev_miscdev = {
1098 .minor = MISC_DYNAMIC_MINOR,
1099 .name = "xen/gntdev",
1100 .fops = &gntdev_fops,
1103 /* ------------------------------------------------------------------ */
1105 static int __init gntdev_init(void)
1112 use_ptemod = !xen_feature(XENFEAT_auto_translated_physmap);
1114 err = misc_register(&gntdev_miscdev);
1116 pr_err("Could not register gntdev device\n");
1122 static void __exit gntdev_exit(void)
1124 misc_deregister(&gntdev_miscdev);
1127 module_init(gntdev_init);
1128 module_exit(gntdev_exit);
1130 /* ------------------------------------------------------------------ */