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>
37 #include <xen/grant_table.h>
38 #include <xen/gntdev.h>
39 #include <asm/xen/hypervisor.h>
40 #include <asm/xen/hypercall.h>
41 #include <asm/xen/page.h>
43 MODULE_LICENSE("GPL");
44 MODULE_AUTHOR("Derek G. Murray <Derek.Murray@cl.cam.ac.uk>, "
45 "Gerd Hoffmann <kraxel@redhat.com>");
46 MODULE_DESCRIPTION("User-space granted page access driver");
48 static int limit = 1024;
49 module_param(limit, int, 0644);
50 MODULE_PARM_DESC(limit, "Maximum number of grants that may be mapped at "
51 "once by a gntdev instance");
54 struct list_head maps;
57 /* lock protects maps from concurrent changes */
60 struct mmu_notifier mn;
64 struct list_head next;
65 struct gntdev_priv *priv;
66 struct vm_area_struct *vma;
71 struct ioctl_gntdev_grant_ref *grants;
72 struct gnttab_map_grant_ref *map_ops;
73 struct gnttab_unmap_grant_ref *unmap_ops;
77 /* ------------------------------------------------------------------ */
79 static void gntdev_print_maps(struct gntdev_priv *priv,
80 char *text, int text_index)
83 struct grant_map *map;
85 pr_debug("maps list (priv %p, usage %d/%d)\n",
86 priv, priv->used, priv->limit);
88 list_for_each_entry(map, &priv->maps, next)
89 pr_debug(" index %2d, count %2d %s\n",
90 map->index, map->count,
91 map->index == text_index && text ? text : "");
95 static struct grant_map *gntdev_alloc_map(struct gntdev_priv *priv, int count)
97 struct grant_map *add;
100 add = kzalloc(sizeof(struct grant_map), GFP_KERNEL);
104 add->grants = kzalloc(sizeof(add->grants[0]) * count, GFP_KERNEL);
105 add->map_ops = kzalloc(sizeof(add->map_ops[0]) * count, GFP_KERNEL);
106 add->unmap_ops = kzalloc(sizeof(add->unmap_ops[0]) * count, GFP_KERNEL);
107 add->pages = kzalloc(sizeof(add->pages[0]) * count, GFP_KERNEL);
108 if (NULL == add->grants ||
109 NULL == add->map_ops ||
110 NULL == add->unmap_ops ||
114 for (i = 0; i < count; i++) {
115 add->pages[i] = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
116 if (add->pages[i] == NULL)
124 if (add->count + priv->used > priv->limit)
131 for (i = 0; i < count; i++) {
133 __free_page(add->pages[i]);
138 kfree(add->unmap_ops);
143 static void gntdev_add_map(struct gntdev_priv *priv, struct grant_map *add)
145 struct grant_map *map;
147 list_for_each_entry(map, &priv->maps, next) {
148 if (add->index + add->count < map->index) {
149 list_add_tail(&add->next, &map->next);
152 add->index = map->index + map->count;
154 list_add_tail(&add->next, &priv->maps);
157 priv->used += add->count;
158 gntdev_print_maps(priv, "[new]", add->index);
161 static struct grant_map *gntdev_find_map_index(struct gntdev_priv *priv,
162 int index, int count)
164 struct grant_map *map;
166 list_for_each_entry(map, &priv->maps, next) {
167 if (map->index != index)
169 if (map->count != count)
176 static struct grant_map *gntdev_find_map_vaddr(struct gntdev_priv *priv,
179 struct grant_map *map;
181 list_for_each_entry(map, &priv->maps, next) {
184 if (vaddr < map->vma->vm_start)
186 if (vaddr >= map->vma->vm_end)
193 static int gntdev_del_map(struct grant_map *map)
199 for (i = 0; i < map->count; i++)
200 if (map->unmap_ops[i].handle)
203 map->priv->used -= map->count;
204 list_del(&map->next);
208 static void gntdev_free_map(struct grant_map *map)
216 for (i = 0; i < map->count; i++) {
218 __free_page(map->pages[i]);
223 kfree(map->unmap_ops);
227 /* ------------------------------------------------------------------ */
229 static int find_grant_ptes(pte_t *pte, pgtable_t token,
230 unsigned long addr, void *data)
232 struct grant_map *map = data;
233 unsigned int pgnr = (addr - map->vma->vm_start) >> PAGE_SHIFT;
236 BUG_ON(pgnr >= map->count);
237 pte_maddr = arbitrary_virt_to_machine(pte).maddr;
239 gnttab_set_map_op(&map->map_ops[pgnr], pte_maddr,
240 GNTMAP_contains_pte | map->flags,
241 map->grants[pgnr].ref,
242 map->grants[pgnr].domid);
243 gnttab_set_unmap_op(&map->unmap_ops[pgnr], pte_maddr,
244 GNTMAP_contains_pte | map->flags,
249 static int map_grant_pages(struct grant_map *map)
253 pr_debug("map %d+%d\n", map->index, map->count);
254 err = gnttab_map_refs(map->map_ops, map->pages, map->count);
258 for (i = 0; i < map->count; i++) {
259 if (map->map_ops[i].status)
261 map->unmap_ops[i].handle = map->map_ops[i].handle;
266 static int unmap_grant_pages(struct grant_map *map, int offset, int pages)
270 pr_debug("map %d+%d [%d+%d]\n", map->index, map->count, offset, pages);
271 err = gnttab_unmap_refs(map->unmap_ops + offset, map->pages, pages);
275 for (i = 0; i < pages; i++) {
276 if (map->unmap_ops[offset+i].status)
278 map->unmap_ops[offset+i].handle = 0;
283 /* ------------------------------------------------------------------ */
285 static void gntdev_vma_close(struct vm_area_struct *vma)
287 struct grant_map *map = vma->vm_private_data;
289 pr_debug("close %p\n", vma);
292 vma->vm_private_data = NULL;
295 static int gntdev_vma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
297 pr_debug("vaddr %p, pgoff %ld (shouldn't happen)\n",
298 vmf->virtual_address, vmf->pgoff);
299 vmf->flags = VM_FAULT_ERROR;
303 static struct vm_operations_struct gntdev_vmops = {
304 .close = gntdev_vma_close,
305 .fault = gntdev_vma_fault,
308 /* ------------------------------------------------------------------ */
310 static void mn_invl_range_start(struct mmu_notifier *mn,
311 struct mm_struct *mm,
312 unsigned long start, unsigned long end)
314 struct gntdev_priv *priv = container_of(mn, struct gntdev_priv, mn);
315 struct grant_map *map;
316 unsigned long mstart, mend;
319 spin_lock(&priv->lock);
320 list_for_each_entry(map, &priv->maps, next) {
325 if (map->vma->vm_start >= end)
327 if (map->vma->vm_end <= start)
329 mstart = max(start, map->vma->vm_start);
330 mend = min(end, map->vma->vm_end);
331 pr_debug("map %d+%d (%lx %lx), range %lx %lx, mrange %lx %lx\n",
332 map->index, map->count,
333 map->vma->vm_start, map->vma->vm_end,
334 start, end, mstart, mend);
335 err = unmap_grant_pages(map,
336 (mstart - map->vma->vm_start) >> PAGE_SHIFT,
337 (mend - mstart) >> PAGE_SHIFT);
340 spin_unlock(&priv->lock);
343 static void mn_invl_page(struct mmu_notifier *mn,
344 struct mm_struct *mm,
345 unsigned long address)
347 mn_invl_range_start(mn, mm, address, address + PAGE_SIZE);
350 static void mn_release(struct mmu_notifier *mn,
351 struct mm_struct *mm)
353 struct gntdev_priv *priv = container_of(mn, struct gntdev_priv, mn);
354 struct grant_map *map;
357 spin_lock(&priv->lock);
358 list_for_each_entry(map, &priv->maps, next) {
361 pr_debug("map %d+%d (%lx %lx)\n",
362 map->index, map->count,
363 map->vma->vm_start, map->vma->vm_end);
364 err = unmap_grant_pages(map, /* offset */ 0, map->count);
367 spin_unlock(&priv->lock);
370 struct mmu_notifier_ops gntdev_mmu_ops = {
371 .release = mn_release,
372 .invalidate_page = mn_invl_page,
373 .invalidate_range_start = mn_invl_range_start,
376 /* ------------------------------------------------------------------ */
378 static int gntdev_open(struct inode *inode, struct file *flip)
380 struct gntdev_priv *priv;
383 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
387 INIT_LIST_HEAD(&priv->maps);
388 spin_lock_init(&priv->lock);
391 priv->mm = get_task_mm(current);
396 priv->mn.ops = &gntdev_mmu_ops;
397 ret = mmu_notifier_register(&priv->mn, priv->mm);
405 flip->private_data = priv;
406 pr_debug("priv %p\n", priv);
411 static int gntdev_release(struct inode *inode, struct file *flip)
413 struct gntdev_priv *priv = flip->private_data;
414 struct grant_map *map;
417 pr_debug("priv %p\n", priv);
419 spin_lock(&priv->lock);
420 while (!list_empty(&priv->maps)) {
421 map = list_entry(priv->maps.next, struct grant_map, next);
422 err = gntdev_del_map(map);
424 gntdev_free_map(map);
427 spin_unlock(&priv->lock);
429 mmu_notifier_unregister(&priv->mn, priv->mm);
434 static long gntdev_ioctl_map_grant_ref(struct gntdev_priv *priv,
435 struct ioctl_gntdev_map_grant_ref __user *u)
437 struct ioctl_gntdev_map_grant_ref op;
438 struct grant_map *map;
441 if (copy_from_user(&op, u, sizeof(op)) != 0)
443 pr_debug("priv %p, add %d\n", priv, op.count);
444 if (unlikely(op.count <= 0))
446 if (unlikely(op.count > priv->limit))
450 map = gntdev_alloc_map(priv, op.count);
453 if (copy_from_user(map->grants, &u->refs,
454 sizeof(map->grants[0]) * op.count) != 0) {
455 gntdev_free_map(map);
459 spin_lock(&priv->lock);
460 gntdev_add_map(priv, map);
461 op.index = map->index << PAGE_SHIFT;
462 spin_unlock(&priv->lock);
464 if (copy_to_user(u, &op, sizeof(op)) != 0) {
465 spin_lock(&priv->lock);
467 spin_unlock(&priv->lock);
468 gntdev_free_map(map);
474 static long gntdev_ioctl_unmap_grant_ref(struct gntdev_priv *priv,
475 struct ioctl_gntdev_unmap_grant_ref __user *u)
477 struct ioctl_gntdev_unmap_grant_ref op;
478 struct grant_map *map;
481 if (copy_from_user(&op, u, sizeof(op)) != 0)
483 pr_debug("priv %p, del %d+%d\n", priv, (int)op.index, (int)op.count);
485 spin_lock(&priv->lock);
486 map = gntdev_find_map_index(priv, op.index >> PAGE_SHIFT, op.count);
488 err = gntdev_del_map(map);
489 spin_unlock(&priv->lock);
491 gntdev_free_map(map);
495 static long gntdev_ioctl_get_offset_for_vaddr(struct gntdev_priv *priv,
496 struct ioctl_gntdev_get_offset_for_vaddr __user *u)
498 struct ioctl_gntdev_get_offset_for_vaddr op;
499 struct grant_map *map;
501 if (copy_from_user(&op, u, sizeof(op)) != 0)
503 pr_debug("priv %p, offset for vaddr %lx\n", priv, (unsigned long)op.vaddr);
505 spin_lock(&priv->lock);
506 map = gntdev_find_map_vaddr(priv, op.vaddr);
508 map->vma->vm_start != op.vaddr) {
509 spin_unlock(&priv->lock);
512 op.offset = map->index << PAGE_SHIFT;
513 op.count = map->count;
514 spin_unlock(&priv->lock);
516 if (copy_to_user(u, &op, sizeof(op)) != 0)
521 static long gntdev_ioctl_set_max_grants(struct gntdev_priv *priv,
522 struct ioctl_gntdev_set_max_grants __user *u)
524 struct ioctl_gntdev_set_max_grants op;
526 if (copy_from_user(&op, u, sizeof(op)) != 0)
528 pr_debug("priv %p, limit %d\n", priv, op.count);
529 if (op.count > limit)
532 spin_lock(&priv->lock);
533 priv->limit = op.count;
534 spin_unlock(&priv->lock);
538 static long gntdev_ioctl(struct file *flip,
539 unsigned int cmd, unsigned long arg)
541 struct gntdev_priv *priv = flip->private_data;
542 void __user *ptr = (void __user *)arg;
545 case IOCTL_GNTDEV_MAP_GRANT_REF:
546 return gntdev_ioctl_map_grant_ref(priv, ptr);
548 case IOCTL_GNTDEV_UNMAP_GRANT_REF:
549 return gntdev_ioctl_unmap_grant_ref(priv, ptr);
551 case IOCTL_GNTDEV_GET_OFFSET_FOR_VADDR:
552 return gntdev_ioctl_get_offset_for_vaddr(priv, ptr);
554 case IOCTL_GNTDEV_SET_MAX_GRANTS:
555 return gntdev_ioctl_set_max_grants(priv, ptr);
558 pr_debug("priv %p, unknown cmd %x\n", priv, cmd);
565 static int gntdev_mmap(struct file *flip, struct vm_area_struct *vma)
567 struct gntdev_priv *priv = flip->private_data;
568 int index = vma->vm_pgoff;
569 int count = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
570 struct grant_map *map;
573 if ((vma->vm_flags & VM_WRITE) && !(vma->vm_flags & VM_SHARED))
576 pr_debug("map %d+%d at %lx (pgoff %lx)\n",
577 index, count, vma->vm_start, vma->vm_pgoff);
579 spin_lock(&priv->lock);
580 map = gntdev_find_map_index(priv, index, count);
585 if (priv->mm != vma->vm_mm) {
586 printk(KERN_WARNING "Huh? Other mm?\n");
590 vma->vm_ops = &gntdev_vmops;
592 vma->vm_flags |= VM_RESERVED|VM_DONTCOPY|VM_DONTEXPAND|VM_PFNMAP;
594 vma->vm_private_data = map;
597 map->flags = GNTMAP_host_map | GNTMAP_application_map;
598 if (!(vma->vm_flags & VM_WRITE))
599 map->flags |= GNTMAP_readonly;
601 spin_unlock(&priv->lock);
603 err = apply_to_page_range(vma->vm_mm, vma->vm_start,
604 vma->vm_end - vma->vm_start,
605 find_grant_ptes, map);
607 printk(KERN_WARNING "find_grant_ptes() failure.\n");
611 err = map_grant_pages(map);
613 printk(KERN_WARNING "map_grant_pages() failure.\n");
622 spin_unlock(&priv->lock);
626 static const struct file_operations gntdev_fops = {
627 .owner = THIS_MODULE,
629 .release = gntdev_release,
631 .unlocked_ioctl = gntdev_ioctl
634 static struct miscdevice gntdev_miscdev = {
635 .minor = MISC_DYNAMIC_MINOR,
636 .name = "xen/gntdev",
637 .fops = &gntdev_fops,
640 /* ------------------------------------------------------------------ */
642 static int __init gntdev_init(void)
649 err = misc_register(&gntdev_miscdev);
651 printk(KERN_ERR "Could not register gntdev device\n");
657 static void __exit gntdev_exit(void)
659 misc_deregister(&gntdev_miscdev);
662 module_init(gntdev_init);
663 module_exit(gntdev_exit);
665 /* ------------------------------------------------------------------ */