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 #include <linux/module.h>
23 #include <linux/kernel.h>
24 #include <linux/init.h>
25 #include <linux/miscdevice.h>
28 #include <linux/mman.h>
29 #include <linux/mmu_notifier.h>
30 #include <linux/types.h>
31 #include <linux/uaccess.h>
32 #include <linux/sched.h>
33 #include <linux/spinlock.h>
34 #include <linux/slab.h>
35 #include <linux/highmem.h>
38 #include <xen/grant_table.h>
39 #include <xen/gntdev.h>
40 #include <xen/events.h>
41 #include <asm/xen/hypervisor.h>
42 #include <asm/xen/hypercall.h>
43 #include <asm/xen/page.h>
45 MODULE_LICENSE("GPL");
46 MODULE_AUTHOR("Derek G. Murray <Derek.Murray@cl.cam.ac.uk>, "
47 "Gerd Hoffmann <kraxel@redhat.com>");
48 MODULE_DESCRIPTION("User-space granted page access driver");
50 static int limit = 1024*1024;
51 module_param(limit, int, 0644);
52 MODULE_PARM_DESC(limit, "Maximum number of grants that may be mapped by "
55 static atomic_t pages_mapped = ATOMIC_INIT(0);
57 static int use_ptemod;
60 struct list_head maps;
61 /* lock protects maps from concurrent changes */
64 struct mmu_notifier mn;
69 /* Address relative to the start of the grant_map */
75 struct list_head next;
76 struct vm_area_struct *vma;
82 struct unmap_notify notify;
83 struct ioctl_gntdev_grant_ref *grants;
84 struct gnttab_map_grant_ref *map_ops;
85 struct gnttab_unmap_grant_ref *unmap_ops;
89 static int unmap_grant_pages(struct grant_map *map, int offset, int pages);
91 /* ------------------------------------------------------------------ */
93 static void gntdev_print_maps(struct gntdev_priv *priv,
94 char *text, int text_index)
97 struct grant_map *map;
99 pr_debug("%s: maps list (priv %p)\n", __func__, priv);
100 list_for_each_entry(map, &priv->maps, next)
101 pr_debug(" index %2d, count %2d %s\n",
102 map->index, map->count,
103 map->index == text_index && text ? text : "");
107 static struct grant_map *gntdev_alloc_map(struct gntdev_priv *priv, int count)
109 struct grant_map *add;
112 add = kzalloc(sizeof(struct grant_map), GFP_KERNEL);
116 add->grants = kzalloc(sizeof(add->grants[0]) * count, GFP_KERNEL);
117 add->map_ops = kzalloc(sizeof(add->map_ops[0]) * count, GFP_KERNEL);
118 add->unmap_ops = kzalloc(sizeof(add->unmap_ops[0]) * count, GFP_KERNEL);
119 add->pages = kzalloc(sizeof(add->pages[0]) * count, GFP_KERNEL);
120 if (NULL == add->grants ||
121 NULL == add->map_ops ||
122 NULL == add->unmap_ops ||
126 for (i = 0; i < count; i++) {
127 add->pages[i] = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
128 if (add->pages[i] == NULL)
134 atomic_set(&add->users, 1);
140 for (i = 0; i < count; i++) {
142 __free_page(add->pages[i]);
147 kfree(add->unmap_ops);
152 static void gntdev_add_map(struct gntdev_priv *priv, struct grant_map *add)
154 struct grant_map *map;
156 list_for_each_entry(map, &priv->maps, next) {
157 if (add->index + add->count < map->index) {
158 list_add_tail(&add->next, &map->next);
161 add->index = map->index + map->count;
163 list_add_tail(&add->next, &priv->maps);
166 gntdev_print_maps(priv, "[new]", add->index);
169 static struct grant_map *gntdev_find_map_index(struct gntdev_priv *priv,
170 int index, int count)
172 struct grant_map *map;
174 list_for_each_entry(map, &priv->maps, next) {
175 if (map->index != index)
177 if (count && map->count != count)
184 static void gntdev_put_map(struct grant_map *map)
191 if (!atomic_dec_and_test(&map->users))
194 atomic_sub(map->count, &pages_mapped);
196 if (map->notify.flags & UNMAP_NOTIFY_SEND_EVENT) {
197 notify_remote_via_evtchn(map->notify.event);
202 unmap_grant_pages(map, 0, map->count);
204 for (i = 0; i < map->count; i++) {
205 uint32_t check, *tmp;
208 /* XXX When unmapping in an HVM domain, Xen will
209 * sometimes end up mapping the GFN to an invalid MFN.
210 * In this case, writes will be discarded and reads will
211 * return all 0xFF bytes. Leak these unusable GFNs
212 * until Xen supports fixing their p2m mapping.
214 * Confirmed present in Xen 4.1-RC3 with HVM source
216 tmp = kmap(map->pages[i]);
220 kunmap(map->pages[i]);
221 if (check == 0xdeaddead)
222 __free_page(map->pages[i]);
224 pr_debug("Discard page %d=%ld\n", i,
225 page_to_pfn(map->pages[i]));
231 kfree(map->unmap_ops);
235 /* ------------------------------------------------------------------ */
237 static int find_grant_ptes(pte_t *pte, pgtable_t token,
238 unsigned long addr, void *data)
240 struct grant_map *map = data;
241 unsigned int pgnr = (addr - map->vma->vm_start) >> PAGE_SHIFT;
242 int flags = map->flags | GNTMAP_application_map | GNTMAP_contains_pte;
245 BUG_ON(pgnr >= map->count);
246 pte_maddr = arbitrary_virt_to_machine(pte).maddr;
248 gnttab_set_map_op(&map->map_ops[pgnr], pte_maddr, flags,
249 map->grants[pgnr].ref,
250 map->grants[pgnr].domid);
251 gnttab_set_unmap_op(&map->unmap_ops[pgnr], pte_maddr, flags,
256 static int map_grant_pages(struct grant_map *map)
262 for (i = 0; i < map->count; i++) {
264 pfn_to_kaddr(page_to_pfn(map->pages[i]));
265 gnttab_set_map_op(&map->map_ops[i], addr, map->flags,
267 map->grants[i].domid);
268 gnttab_set_unmap_op(&map->unmap_ops[i], addr,
269 map->flags, 0 /* handle */);
273 pr_debug("map %d+%d\n", map->index, map->count);
274 err = gnttab_map_refs(map->map_ops, map->pages, map->count);
278 for (i = 0; i < map->count; i++) {
279 if (map->map_ops[i].status)
281 map->unmap_ops[i].handle = map->map_ops[i].handle;
286 static int unmap_grant_pages(struct grant_map *map, int offset, int pages)
290 if (map->notify.flags & UNMAP_NOTIFY_CLEAR_BYTE) {
291 int pgno = (map->notify.addr >> PAGE_SHIFT);
292 if (pgno >= offset && pgno < offset + pages) {
293 uint8_t *tmp = kmap(map->pages[pgno]);
294 tmp[map->notify.addr & (PAGE_SIZE-1)] = 0;
295 kunmap(map->pages[pgno]);
296 map->notify.flags &= ~UNMAP_NOTIFY_CLEAR_BYTE;
300 pr_debug("map %d+%d [%d+%d]\n", map->index, map->count, offset, pages);
301 err = gnttab_unmap_refs(map->unmap_ops + offset, map->pages, pages);
305 for (i = 0; i < pages; i++) {
306 if (map->unmap_ops[offset+i].status)
308 map->unmap_ops[offset+i].handle = 0;
313 /* ------------------------------------------------------------------ */
315 static void gntdev_vma_close(struct vm_area_struct *vma)
317 struct grant_map *map = vma->vm_private_data;
319 pr_debug("close %p\n", vma);
322 vma->vm_private_data = NULL;
326 static struct vm_operations_struct gntdev_vmops = {
327 .close = gntdev_vma_close,
330 /* ------------------------------------------------------------------ */
332 static void mn_invl_range_start(struct mmu_notifier *mn,
333 struct mm_struct *mm,
334 unsigned long start, unsigned long end)
336 struct gntdev_priv *priv = container_of(mn, struct gntdev_priv, mn);
337 struct grant_map *map;
338 unsigned long mstart, mend;
341 spin_lock(&priv->lock);
342 list_for_each_entry(map, &priv->maps, next) {
347 if (map->vma->vm_start >= end)
349 if (map->vma->vm_end <= start)
351 mstart = max(start, map->vma->vm_start);
352 mend = min(end, map->vma->vm_end);
353 pr_debug("map %d+%d (%lx %lx), range %lx %lx, mrange %lx %lx\n",
354 map->index, map->count,
355 map->vma->vm_start, map->vma->vm_end,
356 start, end, mstart, mend);
357 err = unmap_grant_pages(map,
358 (mstart - map->vma->vm_start) >> PAGE_SHIFT,
359 (mend - mstart) >> PAGE_SHIFT);
362 spin_unlock(&priv->lock);
365 static void mn_invl_page(struct mmu_notifier *mn,
366 struct mm_struct *mm,
367 unsigned long address)
369 mn_invl_range_start(mn, mm, address, address + PAGE_SIZE);
372 static void mn_release(struct mmu_notifier *mn,
373 struct mm_struct *mm)
375 struct gntdev_priv *priv = container_of(mn, struct gntdev_priv, mn);
376 struct grant_map *map;
379 spin_lock(&priv->lock);
380 list_for_each_entry(map, &priv->maps, next) {
383 pr_debug("map %d+%d (%lx %lx)\n",
384 map->index, map->count,
385 map->vma->vm_start, map->vma->vm_end);
386 err = unmap_grant_pages(map, /* offset */ 0, map->count);
389 spin_unlock(&priv->lock);
392 struct mmu_notifier_ops gntdev_mmu_ops = {
393 .release = mn_release,
394 .invalidate_page = mn_invl_page,
395 .invalidate_range_start = mn_invl_range_start,
398 /* ------------------------------------------------------------------ */
400 static int gntdev_open(struct inode *inode, struct file *flip)
402 struct gntdev_priv *priv;
405 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
409 INIT_LIST_HEAD(&priv->maps);
410 spin_lock_init(&priv->lock);
413 priv->mm = get_task_mm(current);
418 priv->mn.ops = &gntdev_mmu_ops;
419 ret = mmu_notifier_register(&priv->mn, priv->mm);
428 flip->private_data = priv;
429 pr_debug("priv %p\n", priv);
434 static int gntdev_release(struct inode *inode, struct file *flip)
436 struct gntdev_priv *priv = flip->private_data;
437 struct grant_map *map;
439 pr_debug("priv %p\n", priv);
441 spin_lock(&priv->lock);
442 while (!list_empty(&priv->maps)) {
443 map = list_entry(priv->maps.next, struct grant_map, next);
444 list_del(&map->next);
447 spin_unlock(&priv->lock);
450 mmu_notifier_unregister(&priv->mn, priv->mm);
455 static long gntdev_ioctl_map_grant_ref(struct gntdev_priv *priv,
456 struct ioctl_gntdev_map_grant_ref __user *u)
458 struct ioctl_gntdev_map_grant_ref op;
459 struct grant_map *map;
462 if (copy_from_user(&op, u, sizeof(op)) != 0)
464 pr_debug("priv %p, add %d\n", priv, op.count);
465 if (unlikely(op.count <= 0))
469 map = gntdev_alloc_map(priv, op.count);
473 if (unlikely(atomic_add_return(op.count, &pages_mapped) > limit)) {
474 pr_debug("can't map: over limit\n");
479 if (copy_from_user(map->grants, &u->refs,
480 sizeof(map->grants[0]) * op.count) != 0) {
485 spin_lock(&priv->lock);
486 gntdev_add_map(priv, map);
487 op.index = map->index << PAGE_SHIFT;
488 spin_unlock(&priv->lock);
490 if (copy_to_user(u, &op, sizeof(op)) != 0)
496 static long gntdev_ioctl_unmap_grant_ref(struct gntdev_priv *priv,
497 struct ioctl_gntdev_unmap_grant_ref __user *u)
499 struct ioctl_gntdev_unmap_grant_ref op;
500 struct grant_map *map;
503 if (copy_from_user(&op, u, sizeof(op)) != 0)
505 pr_debug("priv %p, del %d+%d\n", priv, (int)op.index, (int)op.count);
507 spin_lock(&priv->lock);
508 map = gntdev_find_map_index(priv, op.index >> PAGE_SHIFT, op.count);
510 list_del(&map->next);
514 spin_unlock(&priv->lock);
518 static long gntdev_ioctl_get_offset_for_vaddr(struct gntdev_priv *priv,
519 struct ioctl_gntdev_get_offset_for_vaddr __user *u)
521 struct ioctl_gntdev_get_offset_for_vaddr op;
522 struct vm_area_struct *vma;
523 struct grant_map *map;
525 if (copy_from_user(&op, u, sizeof(op)) != 0)
527 pr_debug("priv %p, offset for vaddr %lx\n", priv, (unsigned long)op.vaddr);
529 vma = find_vma(current->mm, op.vaddr);
530 if (!vma || vma->vm_ops != &gntdev_vmops)
533 map = vma->vm_private_data;
537 op.offset = map->index << PAGE_SHIFT;
538 op.count = map->count;
540 if (copy_to_user(u, &op, sizeof(op)) != 0)
545 static long gntdev_ioctl_notify(struct gntdev_priv *priv, void __user *u)
547 struct ioctl_gntdev_unmap_notify op;
548 struct grant_map *map;
551 if (copy_from_user(&op, u, sizeof(op)))
554 if (op.action & ~(UNMAP_NOTIFY_CLEAR_BYTE|UNMAP_NOTIFY_SEND_EVENT))
557 spin_lock(&priv->lock);
559 list_for_each_entry(map, &priv->maps, next) {
560 uint64_t begin = map->index << PAGE_SHIFT;
561 uint64_t end = (map->index + map->count) << PAGE_SHIFT;
562 if (op.index >= begin && op.index < end)
569 map->notify.flags = op.action;
570 map->notify.addr = op.index - (map->index << PAGE_SHIFT);
571 map->notify.event = op.event_channel_port;
574 spin_unlock(&priv->lock);
578 static long gntdev_ioctl(struct file *flip,
579 unsigned int cmd, unsigned long arg)
581 struct gntdev_priv *priv = flip->private_data;
582 void __user *ptr = (void __user *)arg;
585 case IOCTL_GNTDEV_MAP_GRANT_REF:
586 return gntdev_ioctl_map_grant_ref(priv, ptr);
588 case IOCTL_GNTDEV_UNMAP_GRANT_REF:
589 return gntdev_ioctl_unmap_grant_ref(priv, ptr);
591 case IOCTL_GNTDEV_GET_OFFSET_FOR_VADDR:
592 return gntdev_ioctl_get_offset_for_vaddr(priv, ptr);
594 case IOCTL_GNTDEV_SET_UNMAP_NOTIFY:
595 return gntdev_ioctl_notify(priv, ptr);
598 pr_debug("priv %p, unknown cmd %x\n", priv, cmd);
605 static int gntdev_mmap(struct file *flip, struct vm_area_struct *vma)
607 struct gntdev_priv *priv = flip->private_data;
608 int index = vma->vm_pgoff;
609 int count = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
610 struct grant_map *map;
611 int i, err = -EINVAL;
613 if ((vma->vm_flags & VM_WRITE) && !(vma->vm_flags & VM_SHARED))
616 pr_debug("map %d+%d at %lx (pgoff %lx)\n",
617 index, count, vma->vm_start, vma->vm_pgoff);
619 spin_lock(&priv->lock);
620 map = gntdev_find_map_index(priv, index, count);
623 if (use_ptemod && map->vma)
625 if (use_ptemod && priv->mm != vma->vm_mm) {
626 printk(KERN_WARNING "Huh? Other mm?\n");
630 atomic_inc(&map->users);
632 vma->vm_ops = &gntdev_vmops;
634 vma->vm_flags |= VM_RESERVED|VM_DONTCOPY|VM_DONTEXPAND|VM_PFNMAP;
636 vma->vm_private_data = map;
641 map->flags = GNTMAP_host_map;
642 if (!(vma->vm_flags & VM_WRITE))
643 map->flags |= GNTMAP_readonly;
645 spin_unlock(&priv->lock);
648 err = apply_to_page_range(vma->vm_mm, vma->vm_start,
649 vma->vm_end - vma->vm_start,
650 find_grant_ptes, map);
652 printk(KERN_WARNING "find_grant_ptes() failure.\n");
657 err = map_grant_pages(map);
659 printk(KERN_WARNING "map_grant_pages() failure.\n");
666 for (i = 0; i < count; i++) {
667 err = vm_insert_page(vma, vma->vm_start + i*PAGE_SIZE,
677 spin_unlock(&priv->lock);
681 static const struct file_operations gntdev_fops = {
682 .owner = THIS_MODULE,
684 .release = gntdev_release,
686 .unlocked_ioctl = gntdev_ioctl
689 static struct miscdevice gntdev_miscdev = {
690 .minor = MISC_DYNAMIC_MINOR,
691 .name = "xen/gntdev",
692 .fops = &gntdev_fops,
695 /* ------------------------------------------------------------------ */
697 static int __init gntdev_init(void)
704 use_ptemod = xen_pv_domain();
706 err = misc_register(&gntdev_miscdev);
708 printk(KERN_ERR "Could not register gntdev device\n");
714 static void __exit gntdev_exit(void)
716 misc_deregister(&gntdev_miscdev);
719 module_init(gntdev_init);
720 module_exit(gntdev_exit);
722 /* ------------------------------------------------------------------ */