Merge remote-tracking branch 'moduleh/module.h-split'
[linux-2.6/next.git] / drivers / xen / gntdev.c
blob772a5b8bbf2e8b16a2ca73ce204d3a403261f775
1 /******************************************************************************
2 * gntdev.c
4 * Device for accessing (in user-space) pages that have been granted by other
5 * domains.
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
20 #undef DEBUG
22 #include <linux/module.h>
23 #include <linux/kernel.h>
24 #include <linux/init.h>
25 #include <linux/miscdevice.h>
26 #include <linux/fs.h>
27 #include <linux/mm.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>
37 #include <xen/xen.h>
38 #include <xen/grant_table.h>
39 #include <xen/balloon.h>
40 #include <xen/gntdev.h>
41 #include <xen/events.h>
42 #include <asm/xen/hypervisor.h>
43 #include <asm/xen/hypercall.h>
44 #include <asm/xen/page.h>
46 MODULE_LICENSE("GPL");
47 MODULE_AUTHOR("Derek G. Murray <Derek.Murray@cl.cam.ac.uk>, "
48 "Gerd Hoffmann <kraxel@redhat.com>");
49 MODULE_DESCRIPTION("User-space granted page access driver");
51 static int limit = 1024*1024;
52 module_param(limit, int, 0644);
53 MODULE_PARM_DESC(limit, "Maximum number of grants that may be mapped by "
54 "the gntdev device");
56 static atomic_t pages_mapped = ATOMIC_INIT(0);
58 static int use_ptemod;
60 struct gntdev_priv {
61 struct list_head maps;
62 /* lock protects maps from concurrent changes */
63 spinlock_t lock;
64 struct mm_struct *mm;
65 struct mmu_notifier mn;
68 struct unmap_notify {
69 int flags;
70 /* Address relative to the start of the grant_map */
71 int addr;
72 int event;
75 struct grant_map {
76 struct list_head next;
77 struct vm_area_struct *vma;
78 int index;
79 int count;
80 int flags;
81 atomic_t users;
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;
86 struct page **pages;
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)
96 #ifdef DEBUG
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 : "");
104 #endif
107 static struct grant_map *gntdev_alloc_map(struct gntdev_priv *priv, int count)
109 struct grant_map *add;
110 int i;
112 add = kzalloc(sizeof(struct grant_map), GFP_KERNEL);
113 if (NULL == add)
114 return NULL;
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 ||
123 NULL == add->pages)
124 goto err;
126 if (alloc_xenballooned_pages(count, add->pages))
127 goto err;
129 for (i = 0; i < count; i++) {
130 add->map_ops[i].handle = -1;
131 add->unmap_ops[i].handle = -1;
134 add->index = 0;
135 add->count = count;
136 atomic_set(&add->users, 1);
138 return add;
140 err:
141 kfree(add->pages);
142 kfree(add->grants);
143 kfree(add->map_ops);
144 kfree(add->unmap_ops);
145 kfree(add);
146 return NULL;
149 static void gntdev_add_map(struct gntdev_priv *priv, struct grant_map *add)
151 struct grant_map *map;
153 list_for_each_entry(map, &priv->maps, next) {
154 if (add->index + add->count < map->index) {
155 list_add_tail(&add->next, &map->next);
156 goto done;
158 add->index = map->index + map->count;
160 list_add_tail(&add->next, &priv->maps);
162 done:
163 gntdev_print_maps(priv, "[new]", add->index);
166 static struct grant_map *gntdev_find_map_index(struct gntdev_priv *priv,
167 int index, int count)
169 struct grant_map *map;
171 list_for_each_entry(map, &priv->maps, next) {
172 if (map->index != index)
173 continue;
174 if (count && map->count != count)
175 continue;
176 return map;
178 return NULL;
181 static void gntdev_put_map(struct grant_map *map)
183 if (!map)
184 return;
186 if (!atomic_dec_and_test(&map->users))
187 return;
189 atomic_sub(map->count, &pages_mapped);
191 if (map->notify.flags & UNMAP_NOTIFY_SEND_EVENT)
192 notify_remote_via_evtchn(map->notify.event);
194 if (map->pages) {
195 if (!use_ptemod)
196 unmap_grant_pages(map, 0, map->count);
198 free_xenballooned_pages(map->count, map->pages);
200 kfree(map->pages);
201 kfree(map->grants);
202 kfree(map->map_ops);
203 kfree(map->unmap_ops);
204 kfree(map);
207 /* ------------------------------------------------------------------ */
209 static int find_grant_ptes(pte_t *pte, pgtable_t token,
210 unsigned long addr, void *data)
212 struct grant_map *map = data;
213 unsigned int pgnr = (addr - map->vma->vm_start) >> PAGE_SHIFT;
214 int flags = map->flags | GNTMAP_application_map | GNTMAP_contains_pte;
215 u64 pte_maddr;
217 BUG_ON(pgnr >= map->count);
218 pte_maddr = arbitrary_virt_to_machine(pte).maddr;
220 gnttab_set_map_op(&map->map_ops[pgnr], pte_maddr, flags,
221 map->grants[pgnr].ref,
222 map->grants[pgnr].domid);
223 gnttab_set_unmap_op(&map->unmap_ops[pgnr], pte_maddr, flags,
224 -1 /* handle */);
225 return 0;
228 static int map_grant_pages(struct grant_map *map)
230 int i, err = 0;
232 if (!use_ptemod) {
233 /* Note: it could already be mapped */
234 if (map->map_ops[0].handle != -1)
235 return 0;
236 for (i = 0; i < map->count; i++) {
237 unsigned long addr = (unsigned long)
238 pfn_to_kaddr(page_to_pfn(map->pages[i]));
239 gnttab_set_map_op(&map->map_ops[i], addr, map->flags,
240 map->grants[i].ref,
241 map->grants[i].domid);
242 gnttab_set_unmap_op(&map->unmap_ops[i], addr,
243 map->flags, -1 /* handle */);
247 pr_debug("map %d+%d\n", map->index, map->count);
248 err = gnttab_map_refs(map->map_ops, map->pages, map->count);
249 if (err)
250 return err;
252 for (i = 0; i < map->count; i++) {
253 if (map->map_ops[i].status)
254 err = -EINVAL;
255 else {
256 BUG_ON(map->map_ops[i].handle == -1);
257 map->unmap_ops[i].handle = map->map_ops[i].handle;
258 pr_debug("map handle=%d\n", map->map_ops[i].handle);
261 return err;
264 static int __unmap_grant_pages(struct grant_map *map, int offset, int pages)
266 int i, err = 0;
268 if (map->notify.flags & UNMAP_NOTIFY_CLEAR_BYTE) {
269 int pgno = (map->notify.addr >> PAGE_SHIFT);
270 if (pgno >= offset && pgno < offset + pages && use_ptemod) {
271 void __user *tmp = (void __user *)
272 map->vma->vm_start + map->notify.addr;
273 err = copy_to_user(tmp, &err, 1);
274 if (err)
275 return -EFAULT;
276 map->notify.flags &= ~UNMAP_NOTIFY_CLEAR_BYTE;
277 } else if (pgno >= offset && pgno < offset + pages) {
278 uint8_t *tmp = kmap(map->pages[pgno]);
279 tmp[map->notify.addr & (PAGE_SIZE-1)] = 0;
280 kunmap(map->pages[pgno]);
281 map->notify.flags &= ~UNMAP_NOTIFY_CLEAR_BYTE;
285 err = gnttab_unmap_refs(map->unmap_ops + offset, map->pages + offset, pages);
286 if (err)
287 return err;
289 for (i = 0; i < pages; i++) {
290 if (map->unmap_ops[offset+i].status)
291 err = -EINVAL;
292 pr_debug("unmap handle=%d st=%d\n",
293 map->unmap_ops[offset+i].handle,
294 map->unmap_ops[offset+i].status);
295 map->unmap_ops[offset+i].handle = -1;
297 return err;
300 static int unmap_grant_pages(struct grant_map *map, int offset, int pages)
302 int range, err = 0;
304 pr_debug("unmap %d+%d [%d+%d]\n", map->index, map->count, offset, pages);
306 /* It is possible the requested range will have a "hole" where we
307 * already unmapped some of the grants. Only unmap valid ranges.
309 while (pages && !err) {
310 while (pages && map->unmap_ops[offset].handle == -1) {
311 offset++;
312 pages--;
314 range = 0;
315 while (range < pages) {
316 if (map->unmap_ops[offset+range].handle == -1) {
317 range--;
318 break;
320 range++;
322 err = __unmap_grant_pages(map, offset, range);
323 offset += range;
324 pages -= range;
327 return err;
330 /* ------------------------------------------------------------------ */
332 static void gntdev_vma_open(struct vm_area_struct *vma)
334 struct grant_map *map = vma->vm_private_data;
336 pr_debug("gntdev_vma_open %p\n", vma);
337 atomic_inc(&map->users);
340 static void gntdev_vma_close(struct vm_area_struct *vma)
342 struct grant_map *map = vma->vm_private_data;
344 pr_debug("gntdev_vma_close %p\n", vma);
345 map->vma = NULL;
346 vma->vm_private_data = NULL;
347 gntdev_put_map(map);
350 static struct vm_operations_struct gntdev_vmops = {
351 .open = gntdev_vma_open,
352 .close = gntdev_vma_close,
355 /* ------------------------------------------------------------------ */
357 static void mn_invl_range_start(struct mmu_notifier *mn,
358 struct mm_struct *mm,
359 unsigned long start, unsigned long end)
361 struct gntdev_priv *priv = container_of(mn, struct gntdev_priv, mn);
362 struct grant_map *map;
363 unsigned long mstart, mend;
364 int err;
366 spin_lock(&priv->lock);
367 list_for_each_entry(map, &priv->maps, next) {
368 if (!map->vma)
369 continue;
370 if (map->vma->vm_start >= end)
371 continue;
372 if (map->vma->vm_end <= start)
373 continue;
374 mstart = max(start, map->vma->vm_start);
375 mend = min(end, map->vma->vm_end);
376 pr_debug("map %d+%d (%lx %lx), range %lx %lx, mrange %lx %lx\n",
377 map->index, map->count,
378 map->vma->vm_start, map->vma->vm_end,
379 start, end, mstart, mend);
380 err = unmap_grant_pages(map,
381 (mstart - map->vma->vm_start) >> PAGE_SHIFT,
382 (mend - mstart) >> PAGE_SHIFT);
383 WARN_ON(err);
385 spin_unlock(&priv->lock);
388 static void mn_invl_page(struct mmu_notifier *mn,
389 struct mm_struct *mm,
390 unsigned long address)
392 mn_invl_range_start(mn, mm, address, address + PAGE_SIZE);
395 static void mn_release(struct mmu_notifier *mn,
396 struct mm_struct *mm)
398 struct gntdev_priv *priv = container_of(mn, struct gntdev_priv, mn);
399 struct grant_map *map;
400 int err;
402 spin_lock(&priv->lock);
403 list_for_each_entry(map, &priv->maps, next) {
404 if (!map->vma)
405 continue;
406 pr_debug("map %d+%d (%lx %lx)\n",
407 map->index, map->count,
408 map->vma->vm_start, map->vma->vm_end);
409 err = unmap_grant_pages(map, /* offset */ 0, map->count);
410 WARN_ON(err);
412 spin_unlock(&priv->lock);
415 struct mmu_notifier_ops gntdev_mmu_ops = {
416 .release = mn_release,
417 .invalidate_page = mn_invl_page,
418 .invalidate_range_start = mn_invl_range_start,
421 /* ------------------------------------------------------------------ */
423 static int gntdev_open(struct inode *inode, struct file *flip)
425 struct gntdev_priv *priv;
426 int ret = 0;
428 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
429 if (!priv)
430 return -ENOMEM;
432 INIT_LIST_HEAD(&priv->maps);
433 spin_lock_init(&priv->lock);
435 if (use_ptemod) {
436 priv->mm = get_task_mm(current);
437 if (!priv->mm) {
438 kfree(priv);
439 return -ENOMEM;
441 priv->mn.ops = &gntdev_mmu_ops;
442 ret = mmu_notifier_register(&priv->mn, priv->mm);
443 mmput(priv->mm);
446 if (ret) {
447 kfree(priv);
448 return ret;
451 flip->private_data = priv;
452 pr_debug("priv %p\n", priv);
454 return 0;
457 static int gntdev_release(struct inode *inode, struct file *flip)
459 struct gntdev_priv *priv = flip->private_data;
460 struct grant_map *map;
462 pr_debug("priv %p\n", priv);
464 spin_lock(&priv->lock);
465 while (!list_empty(&priv->maps)) {
466 map = list_entry(priv->maps.next, struct grant_map, next);
467 list_del(&map->next);
468 gntdev_put_map(map);
470 spin_unlock(&priv->lock);
472 if (use_ptemod)
473 mmu_notifier_unregister(&priv->mn, priv->mm);
474 kfree(priv);
475 return 0;
478 static long gntdev_ioctl_map_grant_ref(struct gntdev_priv *priv,
479 struct ioctl_gntdev_map_grant_ref __user *u)
481 struct ioctl_gntdev_map_grant_ref op;
482 struct grant_map *map;
483 int err;
485 if (copy_from_user(&op, u, sizeof(op)) != 0)
486 return -EFAULT;
487 pr_debug("priv %p, add %d\n", priv, op.count);
488 if (unlikely(op.count <= 0))
489 return -EINVAL;
491 err = -ENOMEM;
492 map = gntdev_alloc_map(priv, op.count);
493 if (!map)
494 return err;
496 if (unlikely(atomic_add_return(op.count, &pages_mapped) > limit)) {
497 pr_debug("can't map: over limit\n");
498 gntdev_put_map(map);
499 return err;
502 if (copy_from_user(map->grants, &u->refs,
503 sizeof(map->grants[0]) * op.count) != 0) {
504 gntdev_put_map(map);
505 return err;
508 spin_lock(&priv->lock);
509 gntdev_add_map(priv, map);
510 op.index = map->index << PAGE_SHIFT;
511 spin_unlock(&priv->lock);
513 if (copy_to_user(u, &op, sizeof(op)) != 0)
514 return -EFAULT;
516 return 0;
519 static long gntdev_ioctl_unmap_grant_ref(struct gntdev_priv *priv,
520 struct ioctl_gntdev_unmap_grant_ref __user *u)
522 struct ioctl_gntdev_unmap_grant_ref op;
523 struct grant_map *map;
524 int err = -ENOENT;
526 if (copy_from_user(&op, u, sizeof(op)) != 0)
527 return -EFAULT;
528 pr_debug("priv %p, del %d+%d\n", priv, (int)op.index, (int)op.count);
530 spin_lock(&priv->lock);
531 map = gntdev_find_map_index(priv, op.index >> PAGE_SHIFT, op.count);
532 if (map) {
533 list_del(&map->next);
534 gntdev_put_map(map);
535 err = 0;
537 spin_unlock(&priv->lock);
538 return err;
541 static long gntdev_ioctl_get_offset_for_vaddr(struct gntdev_priv *priv,
542 struct ioctl_gntdev_get_offset_for_vaddr __user *u)
544 struct ioctl_gntdev_get_offset_for_vaddr op;
545 struct vm_area_struct *vma;
546 struct grant_map *map;
548 if (copy_from_user(&op, u, sizeof(op)) != 0)
549 return -EFAULT;
550 pr_debug("priv %p, offset for vaddr %lx\n", priv, (unsigned long)op.vaddr);
552 vma = find_vma(current->mm, op.vaddr);
553 if (!vma || vma->vm_ops != &gntdev_vmops)
554 return -EINVAL;
556 map = vma->vm_private_data;
557 if (!map)
558 return -EINVAL;
560 op.offset = map->index << PAGE_SHIFT;
561 op.count = map->count;
563 if (copy_to_user(u, &op, sizeof(op)) != 0)
564 return -EFAULT;
565 return 0;
568 static long gntdev_ioctl_notify(struct gntdev_priv *priv, void __user *u)
570 struct ioctl_gntdev_unmap_notify op;
571 struct grant_map *map;
572 int rc;
574 if (copy_from_user(&op, u, sizeof(op)))
575 return -EFAULT;
577 if (op.action & ~(UNMAP_NOTIFY_CLEAR_BYTE|UNMAP_NOTIFY_SEND_EVENT))
578 return -EINVAL;
580 spin_lock(&priv->lock);
582 list_for_each_entry(map, &priv->maps, next) {
583 uint64_t begin = map->index << PAGE_SHIFT;
584 uint64_t end = (map->index + map->count) << PAGE_SHIFT;
585 if (op.index >= begin && op.index < end)
586 goto found;
588 rc = -ENOENT;
589 goto unlock_out;
591 found:
592 if ((op.action & UNMAP_NOTIFY_CLEAR_BYTE) &&
593 (map->flags & GNTMAP_readonly)) {
594 rc = -EINVAL;
595 goto unlock_out;
598 map->notify.flags = op.action;
599 map->notify.addr = op.index - (map->index << PAGE_SHIFT);
600 map->notify.event = op.event_channel_port;
601 rc = 0;
602 unlock_out:
603 spin_unlock(&priv->lock);
604 return rc;
607 static long gntdev_ioctl(struct file *flip,
608 unsigned int cmd, unsigned long arg)
610 struct gntdev_priv *priv = flip->private_data;
611 void __user *ptr = (void __user *)arg;
613 switch (cmd) {
614 case IOCTL_GNTDEV_MAP_GRANT_REF:
615 return gntdev_ioctl_map_grant_ref(priv, ptr);
617 case IOCTL_GNTDEV_UNMAP_GRANT_REF:
618 return gntdev_ioctl_unmap_grant_ref(priv, ptr);
620 case IOCTL_GNTDEV_GET_OFFSET_FOR_VADDR:
621 return gntdev_ioctl_get_offset_for_vaddr(priv, ptr);
623 case IOCTL_GNTDEV_SET_UNMAP_NOTIFY:
624 return gntdev_ioctl_notify(priv, ptr);
626 default:
627 pr_debug("priv %p, unknown cmd %x\n", priv, cmd);
628 return -ENOIOCTLCMD;
631 return 0;
634 static int gntdev_mmap(struct file *flip, struct vm_area_struct *vma)
636 struct gntdev_priv *priv = flip->private_data;
637 int index = vma->vm_pgoff;
638 int count = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
639 struct grant_map *map;
640 int i, err = -EINVAL;
642 if ((vma->vm_flags & VM_WRITE) && !(vma->vm_flags & VM_SHARED))
643 return -EINVAL;
645 pr_debug("map %d+%d at %lx (pgoff %lx)\n",
646 index, count, vma->vm_start, vma->vm_pgoff);
648 spin_lock(&priv->lock);
649 map = gntdev_find_map_index(priv, index, count);
650 if (!map)
651 goto unlock_out;
652 if (use_ptemod && map->vma)
653 goto unlock_out;
654 if (use_ptemod && priv->mm != vma->vm_mm) {
655 printk(KERN_WARNING "Huh? Other mm?\n");
656 goto unlock_out;
659 atomic_inc(&map->users);
661 vma->vm_ops = &gntdev_vmops;
663 vma->vm_flags |= VM_RESERVED|VM_DONTEXPAND;
665 if (use_ptemod)
666 vma->vm_flags |= VM_DONTCOPY|VM_PFNMAP;
668 vma->vm_private_data = map;
670 if (use_ptemod)
671 map->vma = vma;
673 if (map->flags) {
674 if ((vma->vm_flags & VM_WRITE) &&
675 (map->flags & GNTMAP_readonly))
676 goto out_unlock_put;
677 } else {
678 map->flags = GNTMAP_host_map;
679 if (!(vma->vm_flags & VM_WRITE))
680 map->flags |= GNTMAP_readonly;
683 spin_unlock(&priv->lock);
685 if (use_ptemod) {
686 err = apply_to_page_range(vma->vm_mm, vma->vm_start,
687 vma->vm_end - vma->vm_start,
688 find_grant_ptes, map);
689 if (err) {
690 printk(KERN_WARNING "find_grant_ptes() failure.\n");
691 goto out_put_map;
695 err = map_grant_pages(map);
696 if (err)
697 goto out_put_map;
699 if (!use_ptemod) {
700 for (i = 0; i < count; i++) {
701 err = vm_insert_page(vma, vma->vm_start + i*PAGE_SIZE,
702 map->pages[i]);
703 if (err)
704 goto out_put_map;
708 return 0;
710 unlock_out:
711 spin_unlock(&priv->lock);
712 return err;
714 out_unlock_put:
715 spin_unlock(&priv->lock);
716 out_put_map:
717 if (use_ptemod)
718 map->vma = NULL;
719 gntdev_put_map(map);
720 return err;
723 static const struct file_operations gntdev_fops = {
724 .owner = THIS_MODULE,
725 .open = gntdev_open,
726 .release = gntdev_release,
727 .mmap = gntdev_mmap,
728 .unlocked_ioctl = gntdev_ioctl
731 static struct miscdevice gntdev_miscdev = {
732 .minor = MISC_DYNAMIC_MINOR,
733 .name = "xen/gntdev",
734 .fops = &gntdev_fops,
737 /* ------------------------------------------------------------------ */
739 static int __init gntdev_init(void)
741 int err;
743 if (!xen_domain())
744 return -ENODEV;
746 use_ptemod = xen_pv_domain();
748 err = misc_register(&gntdev_miscdev);
749 if (err != 0) {
750 printk(KERN_ERR "Could not register gntdev device\n");
751 return err;
753 return 0;
756 static void __exit gntdev_exit(void)
758 misc_deregister(&gntdev_miscdev);
761 module_init(gntdev_init);
762 module_exit(gntdev_exit);
764 /* ------------------------------------------------------------------ */