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;
76 /* ------------------------------------------------------------------ */
78 static void gntdev_print_maps(struct gntdev_priv *priv,
79 char *text, int text_index)
82 struct grant_map *map;
84 pr_debug("maps list (priv %p, usage %d/%d)\n",
85 priv, priv->used, priv->limit);
87 list_for_each_entry(map, &priv->maps, next)
88 pr_debug(" index %2d, count %2d %s\n",
89 map->index, map->count,
90 map->index == text_index && text ? text : "");
94 static struct grant_map *gntdev_alloc_map(struct gntdev_priv *priv, int count)
96 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 if (NULL == add->grants ||
106 NULL == add->map_ops ||
107 NULL == add->unmap_ops)
114 if (add->count + priv->used > priv->limit)
122 kfree(add->unmap_ops);
127 static void gntdev_add_map(struct gntdev_priv *priv, struct grant_map *add)
129 struct grant_map *map;
131 list_for_each_entry(map, &priv->maps, next) {
132 if (add->index + add->count < map->index) {
133 list_add_tail(&add->next, &map->next);
136 add->index = map->index + map->count;
138 list_add_tail(&add->next, &priv->maps);
141 priv->used += add->count;
142 gntdev_print_maps(priv, "[new]", add->index);
145 static struct grant_map *gntdev_find_map_index(struct gntdev_priv *priv,
146 int index, int count)
148 struct grant_map *map;
150 list_for_each_entry(map, &priv->maps, next) {
151 if (map->index != index)
153 if (map->count != count)
160 static struct grant_map *gntdev_find_map_vaddr(struct gntdev_priv *priv,
163 struct grant_map *map;
165 list_for_each_entry(map, &priv->maps, next) {
168 if (vaddr < map->vma->vm_start)
170 if (vaddr >= map->vma->vm_end)
177 static int gntdev_del_map(struct grant_map *map)
183 for (i = 0; i < map->count; i++)
184 if (map->unmap_ops[i].handle)
187 map->priv->used -= map->count;
188 list_del(&map->next);
192 static void gntdev_free_map(struct grant_map *map)
198 kfree(map->unmap_ops);
202 /* ------------------------------------------------------------------ */
204 static int find_grant_ptes(pte_t *pte, pgtable_t token,
205 unsigned long addr, void *data)
207 struct grant_map *map = data;
208 unsigned int pgnr = (addr - map->vma->vm_start) >> PAGE_SHIFT;
211 BUG_ON(pgnr >= map->count);
212 pte_maddr = (u64)pfn_to_mfn(page_to_pfn(token)) << PAGE_SHIFT;
213 pte_maddr += (unsigned long)pte & ~PAGE_MASK;
214 gnttab_set_map_op(&map->map_ops[pgnr], pte_maddr, map->flags,
215 map->grants[pgnr].ref,
216 map->grants[pgnr].domid);
217 gnttab_set_unmap_op(&map->unmap_ops[pgnr], pte_maddr, map->flags,
222 static int map_grant_pages(struct grant_map *map)
226 pr_debug("map %d+%d\n", map->index, map->count);
227 err = HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref,
228 map->map_ops, map->count);
232 for (i = 0; i < map->count; i++) {
233 if (map->map_ops[i].status)
235 map->unmap_ops[i].handle = map->map_ops[i].handle;
240 static int unmap_grant_pages(struct grant_map *map, int offset, int pages)
244 pr_debug("map %d+%d [%d+%d]\n", map->index, map->count, offset, pages);
245 err = HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref,
246 map->unmap_ops + offset, pages);
250 for (i = 0; i < pages; i++) {
251 if (map->unmap_ops[offset+i].status)
253 map->unmap_ops[offset+i].handle = 0;
258 /* ------------------------------------------------------------------ */
260 static void gntdev_vma_close(struct vm_area_struct *vma)
262 struct grant_map *map = vma->vm_private_data;
264 pr_debug("close %p\n", vma);
267 vma->vm_private_data = NULL;
270 static int gntdev_vma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
272 pr_debug("vaddr %p, pgoff %ld (shouldn't happen)\n",
273 vmf->virtual_address, vmf->pgoff);
274 vmf->flags = VM_FAULT_ERROR;
278 static struct vm_operations_struct gntdev_vmops = {
279 .close = gntdev_vma_close,
280 .fault = gntdev_vma_fault,
283 /* ------------------------------------------------------------------ */
285 static void mn_invl_range_start(struct mmu_notifier *mn,
286 struct mm_struct *mm,
287 unsigned long start, unsigned long end)
289 struct gntdev_priv *priv = container_of(mn, struct gntdev_priv, mn);
290 struct grant_map *map;
291 unsigned long mstart, mend;
294 spin_lock(&priv->lock);
295 list_for_each_entry(map, &priv->maps, next) {
300 if (map->vma->vm_start >= end)
302 if (map->vma->vm_end <= start)
304 mstart = max(start, map->vma->vm_start);
305 mend = min(end, map->vma->vm_end);
306 pr_debug("map %d+%d (%lx %lx), range %lx %lx, mrange %lx %lx\n",
307 map->index, map->count,
308 map->vma->vm_start, map->vma->vm_end,
309 start, end, mstart, mend);
310 err = unmap_grant_pages(map,
311 (mstart - map->vma->vm_start) >> PAGE_SHIFT,
312 (mend - mstart) >> PAGE_SHIFT);
315 spin_unlock(&priv->lock);
318 static void mn_invl_page(struct mmu_notifier *mn,
319 struct mm_struct *mm,
320 unsigned long address)
322 mn_invl_range_start(mn, mm, address, address + PAGE_SIZE);
325 static void mn_release(struct mmu_notifier *mn,
326 struct mm_struct *mm)
328 struct gntdev_priv *priv = container_of(mn, struct gntdev_priv, mn);
329 struct grant_map *map;
332 spin_lock(&priv->lock);
333 list_for_each_entry(map, &priv->maps, next) {
336 pr_debug("map %d+%d (%lx %lx)\n",
337 map->index, map->count,
338 map->vma->vm_start, map->vma->vm_end);
339 err = unmap_grant_pages(map, /* offset */ 0, map->count);
342 spin_unlock(&priv->lock);
345 struct mmu_notifier_ops gntdev_mmu_ops = {
346 .release = mn_release,
347 .invalidate_page = mn_invl_page,
348 .invalidate_range_start = mn_invl_range_start,
351 /* ------------------------------------------------------------------ */
353 static int gntdev_open(struct inode *inode, struct file *flip)
355 struct gntdev_priv *priv;
358 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
362 INIT_LIST_HEAD(&priv->maps);
363 spin_lock_init(&priv->lock);
366 priv->mm = get_task_mm(current);
371 priv->mn.ops = &gntdev_mmu_ops;
372 ret = mmu_notifier_register(&priv->mn, priv->mm);
380 flip->private_data = priv;
381 pr_debug("priv %p\n", priv);
386 static int gntdev_release(struct inode *inode, struct file *flip)
388 struct gntdev_priv *priv = flip->private_data;
389 struct grant_map *map;
392 pr_debug("priv %p\n", priv);
394 spin_lock(&priv->lock);
395 while (!list_empty(&priv->maps)) {
396 map = list_entry(priv->maps.next, struct grant_map, next);
397 err = gntdev_del_map(map);
399 gntdev_free_map(map);
402 spin_unlock(&priv->lock);
404 mmu_notifier_unregister(&priv->mn, priv->mm);
409 static long gntdev_ioctl_map_grant_ref(struct gntdev_priv *priv,
410 struct ioctl_gntdev_map_grant_ref __user *u)
412 struct ioctl_gntdev_map_grant_ref op;
413 struct grant_map *map;
416 if (copy_from_user(&op, u, sizeof(op)) != 0)
418 pr_debug("priv %p, add %d\n", priv, op.count);
419 if (unlikely(op.count <= 0))
421 if (unlikely(op.count > priv->limit))
425 map = gntdev_alloc_map(priv, op.count);
428 if (copy_from_user(map->grants, &u->refs,
429 sizeof(map->grants[0]) * op.count) != 0) {
430 gntdev_free_map(map);
434 spin_lock(&priv->lock);
435 gntdev_add_map(priv, map);
436 op.index = map->index << PAGE_SHIFT;
437 spin_unlock(&priv->lock);
439 if (copy_to_user(u, &op, sizeof(op)) != 0) {
440 spin_lock(&priv->lock);
442 spin_unlock(&priv->lock);
443 gntdev_free_map(map);
449 static long gntdev_ioctl_unmap_grant_ref(struct gntdev_priv *priv,
450 struct ioctl_gntdev_unmap_grant_ref __user *u)
452 struct ioctl_gntdev_unmap_grant_ref op;
453 struct grant_map *map;
456 if (copy_from_user(&op, u, sizeof(op)) != 0)
458 pr_debug("priv %p, del %d+%d\n", priv, (int)op.index, (int)op.count);
460 spin_lock(&priv->lock);
461 map = gntdev_find_map_index(priv, op.index >> PAGE_SHIFT, op.count);
463 err = gntdev_del_map(map);
464 spin_unlock(&priv->lock);
466 gntdev_free_map(map);
470 static long gntdev_ioctl_get_offset_for_vaddr(struct gntdev_priv *priv,
471 struct ioctl_gntdev_get_offset_for_vaddr __user *u)
473 struct ioctl_gntdev_get_offset_for_vaddr op;
474 struct grant_map *map;
476 if (copy_from_user(&op, u, sizeof(op)) != 0)
478 pr_debug("priv %p, offset for vaddr %lx\n", priv, (unsigned long)op.vaddr);
480 spin_lock(&priv->lock);
481 map = gntdev_find_map_vaddr(priv, op.vaddr);
483 map->vma->vm_start != op.vaddr) {
484 spin_unlock(&priv->lock);
487 op.offset = map->index << PAGE_SHIFT;
488 op.count = map->count;
489 spin_unlock(&priv->lock);
491 if (copy_to_user(u, &op, sizeof(op)) != 0)
496 static long gntdev_ioctl_set_max_grants(struct gntdev_priv *priv,
497 struct ioctl_gntdev_set_max_grants __user *u)
499 struct ioctl_gntdev_set_max_grants op;
501 if (copy_from_user(&op, u, sizeof(op)) != 0)
503 pr_debug("priv %p, limit %d\n", priv, op.count);
504 if (op.count > limit)
507 spin_lock(&priv->lock);
508 priv->limit = op.count;
509 spin_unlock(&priv->lock);
513 static long gntdev_ioctl(struct file *flip,
514 unsigned int cmd, unsigned long arg)
516 struct gntdev_priv *priv = flip->private_data;
517 void __user *ptr = (void __user *)arg;
520 case IOCTL_GNTDEV_MAP_GRANT_REF:
521 return gntdev_ioctl_map_grant_ref(priv, ptr);
523 case IOCTL_GNTDEV_UNMAP_GRANT_REF:
524 return gntdev_ioctl_unmap_grant_ref(priv, ptr);
526 case IOCTL_GNTDEV_GET_OFFSET_FOR_VADDR:
527 return gntdev_ioctl_get_offset_for_vaddr(priv, ptr);
529 case IOCTL_GNTDEV_SET_MAX_GRANTS:
530 return gntdev_ioctl_set_max_grants(priv, ptr);
533 pr_debug("priv %p, unknown cmd %x\n", priv, cmd);
540 static int gntdev_mmap(struct file *flip, struct vm_area_struct *vma)
542 struct gntdev_priv *priv = flip->private_data;
543 int index = vma->vm_pgoff;
544 int count = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
545 struct grant_map *map;
548 if ((vma->vm_flags & VM_WRITE) && !(vma->vm_flags & VM_SHARED))
551 pr_debug("map %d+%d at %lx (pgoff %lx)\n",
552 index, count, vma->vm_start, vma->vm_pgoff);
554 spin_lock(&priv->lock);
555 map = gntdev_find_map_index(priv, index, count);
560 if (priv->mm != vma->vm_mm) {
561 printk(KERN_WARNING "Huh? Other mm?\n");
565 vma->vm_ops = &gntdev_vmops;
567 vma->vm_flags |= VM_RESERVED|VM_DONTCOPY|VM_DONTEXPAND;
569 vma->vm_private_data = map;
572 map->flags = GNTMAP_host_map | GNTMAP_application_map | GNTMAP_contains_pte;
573 if (!(vma->vm_flags & VM_WRITE))
574 map->flags |= GNTMAP_readonly;
576 err = apply_to_page_range(vma->vm_mm, vma->vm_start,
577 vma->vm_end - vma->vm_start,
578 find_grant_ptes, map);
580 printk(KERN_WARNING "find_grant_ptes() failure.\n");
584 err = map_grant_pages(map);
586 printk(KERN_WARNING "map_grant_pages() failure.\n");
592 spin_unlock(&priv->lock);
596 static const struct file_operations gntdev_fops = {
597 .owner = THIS_MODULE,
599 .release = gntdev_release,
601 .unlocked_ioctl = gntdev_ioctl
604 static struct miscdevice gntdev_miscdev = {
605 .minor = MISC_DYNAMIC_MINOR,
606 .name = "xen/gntdev",
607 .fops = &gntdev_fops,
610 /* ------------------------------------------------------------------ */
612 static int __init gntdev_init(void)
619 err = misc_register(&gntdev_miscdev);
621 printk(KERN_ERR "Could not register gntdev device\n");
627 static void __exit gntdev_exit(void)
629 misc_deregister(&gntdev_miscdev);
632 module_init(gntdev_init);
633 module_exit(gntdev_exit);
635 /* ------------------------------------------------------------------ */