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*1024;
49 module_param(limit, int, 0644);
50 MODULE_PARM_DESC(limit, "Maximum number of grants that may be mapped by "
53 static atomic_t pages_mapped = ATOMIC_INIT(0);
56 struct list_head maps;
57 /* lock protects maps from concurrent changes */
60 struct mmu_notifier mn;
64 struct list_head next;
65 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("%s: maps list (priv %p)\n", __func__, priv);
86 list_for_each_entry(map, &priv->maps, next)
87 pr_debug(" index %2d, count %2d %s\n",
88 map->index, map->count,
89 map->index == text_index && text ? text : "");
93 static struct grant_map *gntdev_alloc_map(struct gntdev_priv *priv, int count)
95 struct grant_map *add;
98 add = kzalloc(sizeof(struct grant_map), GFP_KERNEL);
102 add->grants = kzalloc(sizeof(add->grants[0]) * count, GFP_KERNEL);
103 add->map_ops = kzalloc(sizeof(add->map_ops[0]) * count, GFP_KERNEL);
104 add->unmap_ops = kzalloc(sizeof(add->unmap_ops[0]) * count, GFP_KERNEL);
105 add->pages = kzalloc(sizeof(add->pages[0]) * count, GFP_KERNEL);
106 if (NULL == add->grants ||
107 NULL == add->map_ops ||
108 NULL == add->unmap_ops ||
112 for (i = 0; i < count; i++) {
113 add->pages[i] = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
114 if (add->pages[i] == NULL)
120 atomic_set(&add->users, 1);
126 for (i = 0; i < count; i++) {
128 __free_page(add->pages[i]);
133 kfree(add->unmap_ops);
138 static void gntdev_add_map(struct gntdev_priv *priv, struct grant_map *add)
140 struct grant_map *map;
142 list_for_each_entry(map, &priv->maps, next) {
143 if (add->index + add->count < map->index) {
144 list_add_tail(&add->next, &map->next);
147 add->index = map->index + map->count;
149 list_add_tail(&add->next, &priv->maps);
152 gntdev_print_maps(priv, "[new]", add->index);
155 static struct grant_map *gntdev_find_map_index(struct gntdev_priv *priv,
156 int index, int count)
158 struct grant_map *map;
160 list_for_each_entry(map, &priv->maps, next) {
161 if (map->index != index)
163 if (map->count != count)
170 static void gntdev_put_map(struct grant_map *map)
177 if (!atomic_dec_and_test(&map->users))
180 atomic_sub(map->count, &pages_mapped);
183 for (i = 0; i < map->count; i++) {
185 __free_page(map->pages[i]);
190 kfree(map->unmap_ops);
194 /* ------------------------------------------------------------------ */
196 static int find_grant_ptes(pte_t *pte, pgtable_t token,
197 unsigned long addr, void *data)
199 struct grant_map *map = data;
200 unsigned int pgnr = (addr - map->vma->vm_start) >> PAGE_SHIFT;
203 BUG_ON(pgnr >= map->count);
204 pte_maddr = arbitrary_virt_to_machine(pte).maddr;
206 gnttab_set_map_op(&map->map_ops[pgnr], pte_maddr,
207 GNTMAP_contains_pte | map->flags,
208 map->grants[pgnr].ref,
209 map->grants[pgnr].domid);
210 gnttab_set_unmap_op(&map->unmap_ops[pgnr], pte_maddr,
211 GNTMAP_contains_pte | map->flags,
216 static int map_grant_pages(struct grant_map *map)
220 pr_debug("map %d+%d\n", map->index, map->count);
221 err = gnttab_map_refs(map->map_ops, map->pages, map->count);
225 for (i = 0; i < map->count; i++) {
226 if (map->map_ops[i].status)
228 map->unmap_ops[i].handle = map->map_ops[i].handle;
233 static int unmap_grant_pages(struct grant_map *map, int offset, int pages)
237 pr_debug("map %d+%d [%d+%d]\n", map->index, map->count, offset, pages);
238 err = gnttab_unmap_refs(map->unmap_ops + offset, map->pages, pages);
242 for (i = 0; i < pages; i++) {
243 if (map->unmap_ops[offset+i].status)
245 map->unmap_ops[offset+i].handle = 0;
250 /* ------------------------------------------------------------------ */
252 static void gntdev_vma_close(struct vm_area_struct *vma)
254 struct grant_map *map = vma->vm_private_data;
256 pr_debug("close %p\n", vma);
259 vma->vm_private_data = NULL;
263 static int gntdev_vma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
265 pr_debug("vaddr %p, pgoff %ld (shouldn't happen)\n",
266 vmf->virtual_address, vmf->pgoff);
267 vmf->flags = VM_FAULT_ERROR;
271 static struct vm_operations_struct gntdev_vmops = {
272 .close = gntdev_vma_close,
273 .fault = gntdev_vma_fault,
276 /* ------------------------------------------------------------------ */
278 static void mn_invl_range_start(struct mmu_notifier *mn,
279 struct mm_struct *mm,
280 unsigned long start, unsigned long end)
282 struct gntdev_priv *priv = container_of(mn, struct gntdev_priv, mn);
283 struct grant_map *map;
284 unsigned long mstart, mend;
287 spin_lock(&priv->lock);
288 list_for_each_entry(map, &priv->maps, next) {
293 if (map->vma->vm_start >= end)
295 if (map->vma->vm_end <= start)
297 mstart = max(start, map->vma->vm_start);
298 mend = min(end, map->vma->vm_end);
299 pr_debug("map %d+%d (%lx %lx), range %lx %lx, mrange %lx %lx\n",
300 map->index, map->count,
301 map->vma->vm_start, map->vma->vm_end,
302 start, end, mstart, mend);
303 err = unmap_grant_pages(map,
304 (mstart - map->vma->vm_start) >> PAGE_SHIFT,
305 (mend - mstart) >> PAGE_SHIFT);
308 spin_unlock(&priv->lock);
311 static void mn_invl_page(struct mmu_notifier *mn,
312 struct mm_struct *mm,
313 unsigned long address)
315 mn_invl_range_start(mn, mm, address, address + PAGE_SIZE);
318 static void mn_release(struct mmu_notifier *mn,
319 struct mm_struct *mm)
321 struct gntdev_priv *priv = container_of(mn, struct gntdev_priv, mn);
322 struct grant_map *map;
325 spin_lock(&priv->lock);
326 list_for_each_entry(map, &priv->maps, next) {
329 pr_debug("map %d+%d (%lx %lx)\n",
330 map->index, map->count,
331 map->vma->vm_start, map->vma->vm_end);
332 err = unmap_grant_pages(map, /* offset */ 0, map->count);
335 spin_unlock(&priv->lock);
338 struct mmu_notifier_ops gntdev_mmu_ops = {
339 .release = mn_release,
340 .invalidate_page = mn_invl_page,
341 .invalidate_range_start = mn_invl_range_start,
344 /* ------------------------------------------------------------------ */
346 static int gntdev_open(struct inode *inode, struct file *flip)
348 struct gntdev_priv *priv;
351 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
355 INIT_LIST_HEAD(&priv->maps);
356 spin_lock_init(&priv->lock);
358 priv->mm = get_task_mm(current);
363 priv->mn.ops = &gntdev_mmu_ops;
364 ret = mmu_notifier_register(&priv->mn, priv->mm);
372 flip->private_data = priv;
373 pr_debug("priv %p\n", priv);
378 static int gntdev_release(struct inode *inode, struct file *flip)
380 struct gntdev_priv *priv = flip->private_data;
381 struct grant_map *map;
383 pr_debug("priv %p\n", priv);
385 spin_lock(&priv->lock);
386 while (!list_empty(&priv->maps)) {
387 map = list_entry(priv->maps.next, struct grant_map, next);
388 list_del(&map->next);
391 spin_unlock(&priv->lock);
393 mmu_notifier_unregister(&priv->mn, priv->mm);
398 static long gntdev_ioctl_map_grant_ref(struct gntdev_priv *priv,
399 struct ioctl_gntdev_map_grant_ref __user *u)
401 struct ioctl_gntdev_map_grant_ref op;
402 struct grant_map *map;
405 if (copy_from_user(&op, u, sizeof(op)) != 0)
407 pr_debug("priv %p, add %d\n", priv, op.count);
408 if (unlikely(op.count <= 0))
412 map = gntdev_alloc_map(priv, op.count);
416 if (unlikely(atomic_add_return(op.count, &pages_mapped) > limit)) {
417 pr_debug("can't map: over limit\n");
422 if (copy_from_user(map->grants, &u->refs,
423 sizeof(map->grants[0]) * op.count) != 0) {
428 spin_lock(&priv->lock);
429 gntdev_add_map(priv, map);
430 op.index = map->index << PAGE_SHIFT;
431 spin_unlock(&priv->lock);
433 if (copy_to_user(u, &op, sizeof(op)) != 0)
439 static long gntdev_ioctl_unmap_grant_ref(struct gntdev_priv *priv,
440 struct ioctl_gntdev_unmap_grant_ref __user *u)
442 struct ioctl_gntdev_unmap_grant_ref op;
443 struct grant_map *map;
446 if (copy_from_user(&op, u, sizeof(op)) != 0)
448 pr_debug("priv %p, del %d+%d\n", priv, (int)op.index, (int)op.count);
450 spin_lock(&priv->lock);
451 map = gntdev_find_map_index(priv, op.index >> PAGE_SHIFT, op.count);
453 list_del(&map->next);
457 spin_unlock(&priv->lock);
461 static long gntdev_ioctl_get_offset_for_vaddr(struct gntdev_priv *priv,
462 struct ioctl_gntdev_get_offset_for_vaddr __user *u)
464 struct ioctl_gntdev_get_offset_for_vaddr op;
465 struct vm_area_struct *vma;
466 struct grant_map *map;
468 if (copy_from_user(&op, u, sizeof(op)) != 0)
470 pr_debug("priv %p, offset for vaddr %lx\n", priv, (unsigned long)op.vaddr);
472 vma = find_vma(current->mm, op.vaddr);
473 if (!vma || vma->vm_ops != &gntdev_vmops)
476 map = vma->vm_private_data;
480 op.offset = map->index << PAGE_SHIFT;
481 op.count = map->count;
483 if (copy_to_user(u, &op, sizeof(op)) != 0)
488 static long gntdev_ioctl(struct file *flip,
489 unsigned int cmd, unsigned long arg)
491 struct gntdev_priv *priv = flip->private_data;
492 void __user *ptr = (void __user *)arg;
495 case IOCTL_GNTDEV_MAP_GRANT_REF:
496 return gntdev_ioctl_map_grant_ref(priv, ptr);
498 case IOCTL_GNTDEV_UNMAP_GRANT_REF:
499 return gntdev_ioctl_unmap_grant_ref(priv, ptr);
501 case IOCTL_GNTDEV_GET_OFFSET_FOR_VADDR:
502 return gntdev_ioctl_get_offset_for_vaddr(priv, ptr);
505 pr_debug("priv %p, unknown cmd %x\n", priv, cmd);
512 static int gntdev_mmap(struct file *flip, struct vm_area_struct *vma)
514 struct gntdev_priv *priv = flip->private_data;
515 int index = vma->vm_pgoff;
516 int count = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
517 struct grant_map *map;
520 if ((vma->vm_flags & VM_WRITE) && !(vma->vm_flags & VM_SHARED))
523 pr_debug("map %d+%d at %lx (pgoff %lx)\n",
524 index, count, vma->vm_start, vma->vm_pgoff);
526 spin_lock(&priv->lock);
527 map = gntdev_find_map_index(priv, index, count);
532 if (priv->mm != vma->vm_mm) {
533 printk(KERN_WARNING "Huh? Other mm?\n");
537 atomic_inc(&map->users);
539 vma->vm_ops = &gntdev_vmops;
541 vma->vm_flags |= VM_RESERVED|VM_DONTCOPY|VM_DONTEXPAND|VM_PFNMAP;
543 vma->vm_private_data = map;
546 map->flags = GNTMAP_host_map | GNTMAP_application_map;
547 if (!(vma->vm_flags & VM_WRITE))
548 map->flags |= GNTMAP_readonly;
550 spin_unlock(&priv->lock);
552 err = apply_to_page_range(vma->vm_mm, vma->vm_start,
553 vma->vm_end - vma->vm_start,
554 find_grant_ptes, map);
556 printk(KERN_WARNING "find_grant_ptes() failure.\n");
560 err = map_grant_pages(map);
562 printk(KERN_WARNING "map_grant_pages() failure.\n");
571 spin_unlock(&priv->lock);
575 static const struct file_operations gntdev_fops = {
576 .owner = THIS_MODULE,
578 .release = gntdev_release,
580 .unlocked_ioctl = gntdev_ioctl
583 static struct miscdevice gntdev_miscdev = {
584 .minor = MISC_DYNAMIC_MINOR,
585 .name = "xen/gntdev",
586 .fops = &gntdev_fops,
589 /* ------------------------------------------------------------------ */
591 static int __init gntdev_init(void)
598 err = misc_register(&gntdev_miscdev);
600 printk(KERN_ERR "Could not register gntdev device\n");
606 static void __exit gntdev_exit(void)
608 misc_deregister(&gntdev_miscdev);
611 module_init(gntdev_init);
612 module_exit(gntdev_exit);
614 /* ------------------------------------------------------------------ */