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>
39 #include <linux/refcount.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 MODULE_LICENSE("GPL");
51 MODULE_AUTHOR("Derek G. Murray <Derek.Murray@cl.cam.ac.uk>, "
52 "Gerd Hoffmann <kraxel@redhat.com>");
53 MODULE_DESCRIPTION("User-space granted page access driver");
55 static int limit = 1024*1024;
56 module_param(limit, int, 0644);
57 MODULE_PARM_DESC(limit, "Maximum number of grants that may be mapped by "
60 static atomic_t pages_mapped = ATOMIC_INIT(0);
62 /* True in PV mode, false otherwise */
63 static int use_ptemod;
64 #define populate_freeable_maps use_ptemod
67 /* maps with visible offsets in the file descriptor */
68 struct list_head maps;
69 /* maps that are not visible; will be freed on munmap.
70 * Only populated if populate_freeable_maps == 1 */
71 struct list_head freeable_maps;
72 /* lock protects maps and freeable_maps */
75 struct mmu_notifier mn;
80 /* Address relative to the start of the grant_map */
86 struct list_head next;
87 struct vm_area_struct *vma;
92 struct unmap_notify notify;
93 struct ioctl_gntdev_grant_ref *grants;
94 struct gnttab_map_grant_ref *map_ops;
95 struct gnttab_unmap_grant_ref *unmap_ops;
96 struct gnttab_map_grant_ref *kmap_ops;
97 struct gnttab_unmap_grant_ref *kunmap_ops;
100 unsigned long pages_vm_start;
101 /* Number of live grants */
102 atomic_t live_grants;
103 /* Needed to avoid allocation in unmap_grant_pages */
104 struct gntab_unmap_queue_data unmap_data;
107 static void unmap_grant_pages(struct grant_map *map, int offset, int pages);
109 /* ------------------------------------------------------------------ */
111 static void gntdev_print_maps(struct gntdev_priv *priv,
112 char *text, int text_index)
115 struct grant_map *map;
117 pr_debug("%s: maps list (priv %p)\n", __func__, priv);
118 list_for_each_entry(map, &priv->maps, next)
119 pr_debug(" index %2d, count %2d %s\n",
120 map->index, map->count,
121 map->index == text_index && text ? text : "");
125 static void gntdev_free_map(struct grant_map *map)
131 gnttab_free_pages(map->count, map->pages);
135 kfree(map->unmap_ops);
136 kfree(map->kmap_ops);
137 kfree(map->kunmap_ops);
138 kfree(map->being_removed);
142 static struct grant_map *gntdev_alloc_map(struct gntdev_priv *priv, int count)
144 struct grant_map *add;
147 add = kzalloc(sizeof(struct grant_map), GFP_KERNEL);
151 add->grants = kcalloc(count, sizeof(add->grants[0]), GFP_KERNEL);
152 add->map_ops = kcalloc(count, sizeof(add->map_ops[0]), GFP_KERNEL);
153 add->unmap_ops = kcalloc(count, sizeof(add->unmap_ops[0]), GFP_KERNEL);
154 add->kmap_ops = kcalloc(count, sizeof(add->kmap_ops[0]), GFP_KERNEL);
155 add->kunmap_ops = kcalloc(count, sizeof(add->kunmap_ops[0]), GFP_KERNEL);
156 add->pages = kcalloc(count, sizeof(add->pages[0]), GFP_KERNEL);
158 kcalloc(count, sizeof(add->being_removed[0]), GFP_KERNEL);
159 if (NULL == add->grants ||
160 NULL == add->map_ops ||
161 NULL == add->unmap_ops ||
162 NULL == add->kmap_ops ||
163 NULL == add->kunmap_ops ||
164 NULL == add->pages ||
165 NULL == add->being_removed)
168 if (gnttab_alloc_pages(count, add->pages))
171 for (i = 0; i < count; i++) {
172 add->map_ops[i].handle = -1;
173 add->unmap_ops[i].handle = -1;
174 add->kmap_ops[i].handle = -1;
175 add->kunmap_ops[i].handle = -1;
180 refcount_set(&add->users, 1);
185 gntdev_free_map(add);
189 static void gntdev_add_map(struct gntdev_priv *priv, struct grant_map *add)
191 struct grant_map *map;
193 list_for_each_entry(map, &priv->maps, next) {
194 if (add->index + add->count < map->index) {
195 list_add_tail(&add->next, &map->next);
198 add->index = map->index + map->count;
200 list_add_tail(&add->next, &priv->maps);
203 gntdev_print_maps(priv, "[new]", add->index);
206 static struct grant_map *gntdev_find_map_index(struct gntdev_priv *priv,
207 int index, int count)
209 struct grant_map *map;
211 list_for_each_entry(map, &priv->maps, next) {
212 if (map->index != index)
214 if (count && map->count != count)
221 static void gntdev_put_map(struct gntdev_priv *priv, struct grant_map *map)
226 if (!refcount_dec_and_test(&map->users))
229 atomic_sub(map->count, &pages_mapped);
230 if (map->pages && !use_ptemod) {
232 * Increment the reference count. This ensures that the
233 * subsequent call to unmap_grant_pages() will not wind up
234 * re-entering itself. It *can* wind up calling
235 * gntdev_put_map() recursively, but such calls will be with a
236 * reference count greater than 1, so they will return before
237 * this code is reached. The recursion depth is thus limited to
238 * 1. Do NOT use refcount_inc() here, as it will detect that
239 * the reference count is zero and WARN().
241 refcount_set(&map->users, 1);
244 * Unmap the grants. This may or may not be asynchronous, so it
245 * is possible that the reference count is 1 on return, but it
246 * could also be greater than 1.
248 unmap_grant_pages(map, 0, map->count);
250 /* Check if the memory now needs to be freed */
251 if (!refcount_dec_and_test(&map->users))
255 * All pages have been returned to the hypervisor, so free the
260 if (map->notify.flags & UNMAP_NOTIFY_SEND_EVENT) {
261 notify_remote_via_evtchn(map->notify.event);
262 evtchn_put(map->notify.event);
265 if (populate_freeable_maps && priv) {
266 mutex_lock(&priv->lock);
267 list_del(&map->next);
268 mutex_unlock(&priv->lock);
271 if (map->pages && !use_ptemod)
272 unmap_grant_pages(map, 0, map->count);
273 gntdev_free_map(map);
276 /* ------------------------------------------------------------------ */
278 static int find_grant_ptes(pte_t *pte, pgtable_t token,
279 unsigned long addr, void *data)
281 struct grant_map *map = data;
282 unsigned int pgnr = (addr - map->vma->vm_start) >> PAGE_SHIFT;
283 int flags = map->flags | GNTMAP_application_map | GNTMAP_contains_pte;
286 BUG_ON(pgnr >= map->count);
287 pte_maddr = arbitrary_virt_to_machine(pte).maddr;
290 * Set the PTE as special to force get_user_pages_fast() fall
291 * back to the slow path. If this is not supported as part of
292 * the grant map, it will be done afterwards.
294 if (xen_feature(XENFEAT_gnttab_map_avail_bits))
295 flags |= (1 << _GNTMAP_guest_avail0);
297 gnttab_set_map_op(&map->map_ops[pgnr], pte_maddr, flags,
298 map->grants[pgnr].ref,
299 map->grants[pgnr].domid);
300 gnttab_set_unmap_op(&map->unmap_ops[pgnr], pte_maddr, flags,
306 static int set_grant_ptes_as_special(pte_t *pte, pgtable_t token,
307 unsigned long addr, void *data)
309 set_pte_at(current->mm, addr, pte, pte_mkspecial(*pte));
314 static int map_grant_pages(struct grant_map *map)
320 /* Note: it could already be mapped */
321 if (map->map_ops[0].handle != -1)
323 for (i = 0; i < map->count; i++) {
324 unsigned long addr = (unsigned long)
325 pfn_to_kaddr(page_to_pfn(map->pages[i]));
326 gnttab_set_map_op(&map->map_ops[i], addr, map->flags,
328 map->grants[i].domid);
329 gnttab_set_unmap_op(&map->unmap_ops[i], addr,
330 map->flags, -1 /* handle */);
334 * Setup the map_ops corresponding to the pte entries pointing
335 * to the kernel linear addresses of the struct pages.
336 * These ptes are completely different from the user ptes dealt
337 * with find_grant_ptes.
338 * Note that GNTMAP_device_map isn't needed here: The
339 * dev_bus_addr output field gets consumed only from ->map_ops,
340 * and by not requesting it when mapping we also avoid needing
341 * to mirror dev_bus_addr into ->unmap_ops (and holding an extra
342 * reference to the page in the hypervisor).
344 unsigned int flags = (map->flags & ~GNTMAP_device_map) |
347 for (i = 0; i < map->count; i++) {
348 unsigned long address = (unsigned long)
349 pfn_to_kaddr(page_to_pfn(map->pages[i]));
350 BUG_ON(PageHighMem(map->pages[i]));
352 gnttab_set_map_op(&map->kmap_ops[i], address, flags,
354 map->grants[i].domid);
355 gnttab_set_unmap_op(&map->kunmap_ops[i], address,
360 pr_debug("map %d+%d\n", map->index, map->count);
361 err = gnttab_map_refs(map->map_ops, use_ptemod ? map->kmap_ops : NULL,
362 map->pages, map->count);
364 for (i = 0; i < map->count; i++) {
365 if (map->map_ops[i].status == GNTST_okay) {
366 map->unmap_ops[i].handle = map->map_ops[i].handle;
371 if (map->flags & GNTMAP_device_map)
372 map->unmap_ops[i].dev_bus_addr = map->map_ops[i].dev_bus_addr;
375 if (map->kmap_ops[i].status == GNTST_okay) {
377 map->kunmap_ops[i].handle = map->kmap_ops[i].handle;
382 atomic_add(alloced, &map->live_grants);
386 static void __unmap_grant_pages_done(int result,
387 struct gntab_unmap_queue_data *data)
390 struct grant_map *map = data->data;
391 unsigned int offset = data->unmap_ops - map->unmap_ops;
392 int successful_unmaps = 0;
395 for (i = 0; i < data->count; i++) {
396 if (map->unmap_ops[offset + i].status == GNTST_okay &&
397 map->unmap_ops[offset + i].handle != -1)
400 WARN_ON(map->unmap_ops[offset+i].status &&
401 map->unmap_ops[offset+i].handle != -1);
402 pr_debug("unmap handle=%d st=%d\n",
403 map->unmap_ops[offset+i].handle,
404 map->unmap_ops[offset+i].status);
405 map->unmap_ops[offset+i].handle = -1;
407 if (map->kunmap_ops[offset + i].status == GNTST_okay &&
408 map->kunmap_ops[offset + i].handle != -1)
411 WARN_ON(map->kunmap_ops[offset+i].status &&
412 map->kunmap_ops[offset+i].handle != -1);
413 pr_debug("kunmap handle=%u st=%d\n",
414 map->kunmap_ops[offset+i].handle,
415 map->kunmap_ops[offset+i].status);
416 map->kunmap_ops[offset+i].handle = -1;
421 * Decrease the live-grant counter. This must happen after the loop to
422 * prevent premature reuse of the grants by gnttab_mmap().
424 live_grants = atomic_sub_return(successful_unmaps, &map->live_grants);
425 if (WARN_ON(live_grants < 0))
426 pr_err("%s: live_grants became negative (%d) after unmapping %d pages!\n",
427 __func__, live_grants, successful_unmaps);
429 /* Release reference taken by unmap_grant_pages */
430 gntdev_put_map(NULL, map);
433 static void __unmap_grant_pages(struct grant_map *map, int offset, int pages)
435 if (map->notify.flags & UNMAP_NOTIFY_CLEAR_BYTE) {
436 int pgno = (map->notify.addr >> PAGE_SHIFT);
438 if (pgno >= offset && pgno < offset + pages) {
439 /* No need for kmap, pages are in lowmem */
440 uint8_t *tmp = pfn_to_kaddr(page_to_pfn(map->pages[pgno]));
442 tmp[map->notify.addr & (PAGE_SIZE-1)] = 0;
443 map->notify.flags &= ~UNMAP_NOTIFY_CLEAR_BYTE;
447 map->unmap_data.unmap_ops = map->unmap_ops + offset;
448 map->unmap_data.kunmap_ops = use_ptemod ? map->kunmap_ops + offset : NULL;
449 map->unmap_data.pages = map->pages + offset;
450 map->unmap_data.count = pages;
451 map->unmap_data.done = __unmap_grant_pages_done;
452 map->unmap_data.data = map;
453 refcount_inc(&map->users); /* to keep map alive during async call below */
455 gnttab_unmap_refs_async(&map->unmap_data);
458 static void unmap_grant_pages(struct grant_map *map, int offset, int pages)
462 if (atomic_read(&map->live_grants) == 0)
463 return; /* Nothing to do */
465 pr_debug("unmap %d+%d [%d+%d]\n", map->index, map->count, offset, pages);
467 /* It is possible the requested range will have a "hole" where we
468 * already unmapped some of the grants. Only unmap valid ranges.
471 while (pages && map->being_removed[offset]) {
476 while (range < pages) {
477 if (map->being_removed[offset + range])
479 map->being_removed[offset + range] = true;
483 __unmap_grant_pages(map, offset, range);
489 /* ------------------------------------------------------------------ */
491 static void gntdev_vma_open(struct vm_area_struct *vma)
493 struct grant_map *map = vma->vm_private_data;
495 pr_debug("gntdev_vma_open %p\n", vma);
496 refcount_inc(&map->users);
499 static void gntdev_vma_close(struct vm_area_struct *vma)
501 struct grant_map *map = vma->vm_private_data;
502 struct file *file = vma->vm_file;
503 struct gntdev_priv *priv = file->private_data;
505 pr_debug("gntdev_vma_close %p\n", vma);
507 /* It is possible that an mmu notifier could be running
508 * concurrently, so take priv->lock to ensure that the vma won't
509 * vanishing during the unmap_grant_pages call, since we will
510 * spin here until that completes. Such a concurrent call will
511 * not do any unmapping, since that has been done prior to
512 * closing the vma, but it may still iterate the unmap_ops list.
514 mutex_lock(&priv->lock);
516 mutex_unlock(&priv->lock);
518 vma->vm_private_data = NULL;
519 gntdev_put_map(priv, map);
522 static struct page *gntdev_vma_find_special_page(struct vm_area_struct *vma,
525 struct grant_map *map = vma->vm_private_data;
527 return map->pages[(addr - map->pages_vm_start) >> PAGE_SHIFT];
530 static const struct vm_operations_struct gntdev_vmops = {
531 .open = gntdev_vma_open,
532 .close = gntdev_vma_close,
533 .find_special_page = gntdev_vma_find_special_page,
536 /* ------------------------------------------------------------------ */
538 static void unmap_if_in_range(struct grant_map *map,
539 unsigned long start, unsigned long end)
541 unsigned long mstart, mend;
545 if (map->vma->vm_start >= end)
547 if (map->vma->vm_end <= start)
549 mstart = max(start, map->vma->vm_start);
550 mend = min(end, map->vma->vm_end);
551 pr_debug("map %d+%d (%lx %lx), range %lx %lx, mrange %lx %lx\n",
552 map->index, map->count,
553 map->vma->vm_start, map->vma->vm_end,
554 start, end, mstart, mend);
555 unmap_grant_pages(map,
556 (mstart - map->vma->vm_start) >> PAGE_SHIFT,
557 (mend - mstart) >> PAGE_SHIFT);
560 static void mn_invl_range_start(struct mmu_notifier *mn,
561 struct mm_struct *mm,
562 unsigned long start, unsigned long end)
564 struct gntdev_priv *priv = container_of(mn, struct gntdev_priv, mn);
565 struct grant_map *map;
567 mutex_lock(&priv->lock);
568 list_for_each_entry(map, &priv->maps, next) {
569 unmap_if_in_range(map, start, end);
571 list_for_each_entry(map, &priv->freeable_maps, next) {
572 unmap_if_in_range(map, start, end);
574 mutex_unlock(&priv->lock);
577 static void mn_release(struct mmu_notifier *mn,
578 struct mm_struct *mm)
580 struct gntdev_priv *priv = container_of(mn, struct gntdev_priv, mn);
581 struct grant_map *map;
583 mutex_lock(&priv->lock);
584 list_for_each_entry(map, &priv->maps, next) {
587 pr_debug("map %d+%d (%lx %lx)\n",
588 map->index, map->count,
589 map->vma->vm_start, map->vma->vm_end);
590 unmap_grant_pages(map, /* offset */ 0, map->count);
592 list_for_each_entry(map, &priv->freeable_maps, next) {
595 pr_debug("map %d+%d (%lx %lx)\n",
596 map->index, map->count,
597 map->vma->vm_start, map->vma->vm_end);
598 unmap_grant_pages(map, /* offset */ 0, map->count);
600 mutex_unlock(&priv->lock);
603 static const struct mmu_notifier_ops gntdev_mmu_ops = {
604 .release = mn_release,
605 .invalidate_range_start = mn_invl_range_start,
608 /* ------------------------------------------------------------------ */
610 static int gntdev_open(struct inode *inode, struct file *flip)
612 struct gntdev_priv *priv;
615 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
619 INIT_LIST_HEAD(&priv->maps);
620 INIT_LIST_HEAD(&priv->freeable_maps);
621 mutex_init(&priv->lock);
624 priv->mm = get_task_mm(current);
629 priv->mn.ops = &gntdev_mmu_ops;
630 ret = mmu_notifier_register(&priv->mn, priv->mm);
639 flip->private_data = priv;
640 pr_debug("priv %p\n", priv);
645 static int gntdev_release(struct inode *inode, struct file *flip)
647 struct gntdev_priv *priv = flip->private_data;
648 struct grant_map *map;
650 pr_debug("priv %p\n", priv);
652 mutex_lock(&priv->lock);
653 while (!list_empty(&priv->maps)) {
654 map = list_entry(priv->maps.next, struct grant_map, next);
655 list_del(&map->next);
656 gntdev_put_map(NULL /* already removed */, map);
658 WARN_ON(!list_empty(&priv->freeable_maps));
659 mutex_unlock(&priv->lock);
662 mmu_notifier_unregister(&priv->mn, priv->mm);
667 static long gntdev_ioctl_map_grant_ref(struct gntdev_priv *priv,
668 struct ioctl_gntdev_map_grant_ref __user *u)
670 struct ioctl_gntdev_map_grant_ref op;
671 struct grant_map *map;
674 if (copy_from_user(&op, u, sizeof(op)) != 0)
676 pr_debug("priv %p, add %d\n", priv, op.count);
677 if (unlikely(op.count <= 0))
681 map = gntdev_alloc_map(priv, op.count);
685 if (unlikely(atomic_add_return(op.count, &pages_mapped) > limit)) {
686 pr_debug("can't map: over limit\n");
687 gntdev_put_map(NULL, map);
691 if (copy_from_user(map->grants, &u->refs,
692 sizeof(map->grants[0]) * op.count) != 0) {
693 gntdev_put_map(NULL, map);
697 mutex_lock(&priv->lock);
698 gntdev_add_map(priv, map);
699 op.index = map->index << PAGE_SHIFT;
700 mutex_unlock(&priv->lock);
702 if (copy_to_user(u, &op, sizeof(op)) != 0)
708 static long gntdev_ioctl_unmap_grant_ref(struct gntdev_priv *priv,
709 struct ioctl_gntdev_unmap_grant_ref __user *u)
711 struct ioctl_gntdev_unmap_grant_ref op;
712 struct grant_map *map;
715 if (copy_from_user(&op, u, sizeof(op)) != 0)
717 pr_debug("priv %p, del %d+%d\n", priv, (int)op.index, (int)op.count);
719 mutex_lock(&priv->lock);
720 map = gntdev_find_map_index(priv, op.index >> PAGE_SHIFT, op.count);
722 list_del(&map->next);
723 if (populate_freeable_maps)
724 list_add_tail(&map->next, &priv->freeable_maps);
727 mutex_unlock(&priv->lock);
729 gntdev_put_map(priv, map);
733 static long gntdev_ioctl_get_offset_for_vaddr(struct gntdev_priv *priv,
734 struct ioctl_gntdev_get_offset_for_vaddr __user *u)
736 struct ioctl_gntdev_get_offset_for_vaddr op;
737 struct vm_area_struct *vma;
738 struct grant_map *map;
741 if (copy_from_user(&op, u, sizeof(op)) != 0)
743 pr_debug("priv %p, offset for vaddr %lx\n", priv, (unsigned long)op.vaddr);
745 down_read(¤t->mm->mmap_sem);
746 vma = find_vma(current->mm, op.vaddr);
747 if (!vma || vma->vm_ops != &gntdev_vmops)
750 map = vma->vm_private_data;
754 op.offset = map->index << PAGE_SHIFT;
755 op.count = map->count;
759 up_read(¤t->mm->mmap_sem);
761 if (rv == 0 && copy_to_user(u, &op, sizeof(op)) != 0)
766 static long gntdev_ioctl_notify(struct gntdev_priv *priv, void __user *u)
768 struct ioctl_gntdev_unmap_notify op;
769 struct grant_map *map;
772 unsigned int out_event;
774 if (copy_from_user(&op, u, sizeof(op)))
777 if (op.action & ~(UNMAP_NOTIFY_CLEAR_BYTE|UNMAP_NOTIFY_SEND_EVENT))
780 /* We need to grab a reference to the event channel we are going to use
781 * to send the notify before releasing the reference we may already have
782 * (if someone has called this ioctl twice). This is required so that
783 * it is possible to change the clear_byte part of the notification
784 * without disturbing the event channel part, which may now be the last
785 * reference to that event channel.
787 if (op.action & UNMAP_NOTIFY_SEND_EVENT) {
788 if (evtchn_get(op.event_channel_port))
792 out_flags = op.action;
793 out_event = op.event_channel_port;
795 mutex_lock(&priv->lock);
797 list_for_each_entry(map, &priv->maps, next) {
798 uint64_t begin = map->index << PAGE_SHIFT;
799 uint64_t end = (map->index + map->count) << PAGE_SHIFT;
800 if (op.index >= begin && op.index < end)
807 if ((op.action & UNMAP_NOTIFY_CLEAR_BYTE) &&
808 (map->flags & GNTMAP_readonly)) {
813 out_flags = map->notify.flags;
814 out_event = map->notify.event;
816 map->notify.flags = op.action;
817 map->notify.addr = op.index - (map->index << PAGE_SHIFT);
818 map->notify.event = op.event_channel_port;
823 mutex_unlock(&priv->lock);
825 /* Drop the reference to the event channel we did not save in the map */
826 if (out_flags & UNMAP_NOTIFY_SEND_EVENT)
827 evtchn_put(out_event);
832 #define GNTDEV_COPY_BATCH 16
834 struct gntdev_copy_batch {
835 struct gnttab_copy ops[GNTDEV_COPY_BATCH];
836 struct page *pages[GNTDEV_COPY_BATCH];
837 s16 __user *status[GNTDEV_COPY_BATCH];
839 unsigned int nr_pages;
842 static int gntdev_get_page(struct gntdev_copy_batch *batch, void __user *virt,
843 bool writeable, unsigned long *gfn)
845 unsigned long addr = (unsigned long)virt;
847 unsigned long xen_pfn;
850 ret = get_user_pages_fast(addr, 1, writeable, &page);
854 batch->pages[batch->nr_pages++] = page;
856 xen_pfn = page_to_xen_pfn(page) + XEN_PFN_DOWN(addr & ~PAGE_MASK);
857 *gfn = pfn_to_gfn(xen_pfn);
862 static void gntdev_put_pages(struct gntdev_copy_batch *batch)
866 for (i = 0; i < batch->nr_pages; i++)
867 put_page(batch->pages[i]);
871 static int gntdev_copy(struct gntdev_copy_batch *batch)
875 gnttab_batch_copy(batch->ops, batch->nr_ops);
876 gntdev_put_pages(batch);
879 * For each completed op, update the status if the op failed
880 * and all previous ops for the segment were successful.
882 for (i = 0; i < batch->nr_ops; i++) {
883 s16 status = batch->ops[i].status;
886 if (status == GNTST_okay)
889 if (__get_user(old_status, batch->status[i]))
892 if (old_status != GNTST_okay)
895 if (__put_user(status, batch->status[i]))
903 static int gntdev_grant_copy_seg(struct gntdev_copy_batch *batch,
904 struct gntdev_grant_copy_segment *seg,
910 * Disallow local -> local copies since there is only space in
911 * batch->pages for one page per-op and this would be a very
912 * expensive memcpy().
914 if (!(seg->flags & (GNTCOPY_source_gref | GNTCOPY_dest_gref)))
917 /* Can't cross page if source/dest is a grant ref. */
918 if (seg->flags & GNTCOPY_source_gref) {
919 if (seg->source.foreign.offset + seg->len > XEN_PAGE_SIZE)
922 if (seg->flags & GNTCOPY_dest_gref) {
923 if (seg->dest.foreign.offset + seg->len > XEN_PAGE_SIZE)
927 if (put_user(GNTST_okay, status))
930 while (copied < seg->len) {
931 struct gnttab_copy *op;
937 if (batch->nr_ops >= GNTDEV_COPY_BATCH) {
938 ret = gntdev_copy(batch);
943 len = seg->len - copied;
945 op = &batch->ops[batch->nr_ops];
948 if (seg->flags & GNTCOPY_source_gref) {
949 op->source.u.ref = seg->source.foreign.ref;
950 op->source.domid = seg->source.foreign.domid;
951 op->source.offset = seg->source.foreign.offset + copied;
952 op->flags |= GNTCOPY_source_gref;
954 virt = seg->source.virt + copied;
955 off = (unsigned long)virt & ~XEN_PAGE_MASK;
956 len = min(len, (size_t)XEN_PAGE_SIZE - off);
958 ret = gntdev_get_page(batch, virt, false, &gfn);
962 op->source.u.gmfn = gfn;
963 op->source.domid = DOMID_SELF;
964 op->source.offset = off;
967 if (seg->flags & GNTCOPY_dest_gref) {
968 op->dest.u.ref = seg->dest.foreign.ref;
969 op->dest.domid = seg->dest.foreign.domid;
970 op->dest.offset = seg->dest.foreign.offset + copied;
971 op->flags |= GNTCOPY_dest_gref;
973 virt = seg->dest.virt + copied;
974 off = (unsigned long)virt & ~XEN_PAGE_MASK;
975 len = min(len, (size_t)XEN_PAGE_SIZE - off);
977 ret = gntdev_get_page(batch, virt, true, &gfn);
981 op->dest.u.gmfn = gfn;
982 op->dest.domid = DOMID_SELF;
983 op->dest.offset = off;
989 batch->status[batch->nr_ops] = status;
996 static long gntdev_ioctl_grant_copy(struct gntdev_priv *priv, void __user *u)
998 struct ioctl_gntdev_grant_copy copy;
999 struct gntdev_copy_batch batch;
1003 if (copy_from_user(©, u, sizeof(copy)))
1009 for (i = 0; i < copy.count; i++) {
1010 struct gntdev_grant_copy_segment seg;
1012 if (copy_from_user(&seg, ©.segments[i], sizeof(seg))) {
1017 ret = gntdev_grant_copy_seg(&batch, &seg, ©.segments[i].status);
1024 ret = gntdev_copy(&batch);
1028 gntdev_put_pages(&batch);
1032 static long gntdev_ioctl(struct file *flip,
1033 unsigned int cmd, unsigned long arg)
1035 struct gntdev_priv *priv = flip->private_data;
1036 void __user *ptr = (void __user *)arg;
1039 case IOCTL_GNTDEV_MAP_GRANT_REF:
1040 return gntdev_ioctl_map_grant_ref(priv, ptr);
1042 case IOCTL_GNTDEV_UNMAP_GRANT_REF:
1043 return gntdev_ioctl_unmap_grant_ref(priv, ptr);
1045 case IOCTL_GNTDEV_GET_OFFSET_FOR_VADDR:
1046 return gntdev_ioctl_get_offset_for_vaddr(priv, ptr);
1048 case IOCTL_GNTDEV_SET_UNMAP_NOTIFY:
1049 return gntdev_ioctl_notify(priv, ptr);
1051 case IOCTL_GNTDEV_GRANT_COPY:
1052 return gntdev_ioctl_grant_copy(priv, ptr);
1055 pr_debug("priv %p, unknown cmd %x\n", priv, cmd);
1056 return -ENOIOCTLCMD;
1062 static int gntdev_mmap(struct file *flip, struct vm_area_struct *vma)
1064 struct gntdev_priv *priv = flip->private_data;
1065 int index = vma->vm_pgoff;
1066 int count = vma_pages(vma);
1067 struct grant_map *map;
1068 int i, err = -EINVAL;
1070 if ((vma->vm_flags & VM_WRITE) && !(vma->vm_flags & VM_SHARED))
1073 pr_debug("map %d+%d at %lx (pgoff %lx)\n",
1074 index, count, vma->vm_start, vma->vm_pgoff);
1076 mutex_lock(&priv->lock);
1077 map = gntdev_find_map_index(priv, index, count);
1080 if (use_ptemod && map->vma)
1082 if (use_ptemod && priv->mm != vma->vm_mm) {
1083 pr_warn("Huh? Other mm?\n");
1087 if (atomic_read(&map->live_grants)) {
1091 refcount_inc(&map->users);
1093 vma->vm_ops = &gntdev_vmops;
1095 vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP | VM_MIXEDMAP;
1098 vma->vm_flags |= VM_DONTCOPY;
1100 vma->vm_private_data = map;
1106 if ((vma->vm_flags & VM_WRITE) &&
1107 (map->flags & GNTMAP_readonly))
1108 goto out_unlock_put;
1110 map->flags = GNTMAP_host_map;
1111 if (!(vma->vm_flags & VM_WRITE))
1112 map->flags |= GNTMAP_readonly;
1115 mutex_unlock(&priv->lock);
1118 map->pages_vm_start = vma->vm_start;
1119 err = apply_to_page_range(vma->vm_mm, vma->vm_start,
1120 vma->vm_end - vma->vm_start,
1121 find_grant_ptes, map);
1123 pr_warn("find_grant_ptes() failure.\n");
1128 err = map_grant_pages(map);
1133 for (i = 0; i < count; i++) {
1134 err = vm_insert_page(vma, vma->vm_start + i*PAGE_SIZE,
1142 * If the PTEs were not made special by the grant map
1143 * hypercall, do so here.
1145 * This is racy since the mapping is already visible
1146 * to userspace but userspace should be well-behaved
1147 * enough to not touch it until the mmap() call
1150 if (!xen_feature(XENFEAT_gnttab_map_avail_bits)) {
1151 apply_to_page_range(vma->vm_mm, vma->vm_start,
1152 vma->vm_end - vma->vm_start,
1153 set_grant_ptes_as_special, NULL);
1161 mutex_unlock(&priv->lock);
1165 mutex_unlock(&priv->lock);
1169 unmap_grant_pages(map, 0, map->count);
1171 gntdev_put_map(priv, map);
1175 static const struct file_operations gntdev_fops = {
1176 .owner = THIS_MODULE,
1177 .open = gntdev_open,
1178 .release = gntdev_release,
1179 .mmap = gntdev_mmap,
1180 .unlocked_ioctl = gntdev_ioctl
1183 static struct miscdevice gntdev_miscdev = {
1184 .minor = MISC_DYNAMIC_MINOR,
1185 .name = "xen/gntdev",
1186 .fops = &gntdev_fops,
1189 /* ------------------------------------------------------------------ */
1191 static int __init gntdev_init(void)
1198 use_ptemod = !xen_feature(XENFEAT_auto_translated_physmap);
1200 err = misc_register(&gntdev_miscdev);
1202 pr_err("Could not register gntdev device\n");
1208 static void __exit gntdev_exit(void)
1210 misc_deregister(&gntdev_miscdev);
1213 module_init(gntdev_init);
1214 module_exit(gntdev_exit);
1216 /* ------------------------------------------------------------------ */