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/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 "
56 static atomic_t pages_mapped
= ATOMIC_INIT(0);
58 static int use_ptemod
;
61 struct list_head maps
;
62 /* lock protects maps from concurrent changes */
65 struct mmu_notifier mn
;
70 /* Address relative to the start of the grant_map */
76 struct list_head next
;
77 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 if (alloc_xenballooned_pages(count
, add
->pages
))
129 for (i
= 0; i
< count
; i
++) {
130 add
->map_ops
[i
].handle
= -1;
131 add
->unmap_ops
[i
].handle
= -1;
136 atomic_set(&add
->users
, 1);
144 kfree(add
->unmap_ops
);
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
);
158 add
->index
= map
->index
+ map
->count
;
160 list_add_tail(&add
->next
, &priv
->maps
);
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
)
174 if (count
&& map
->count
!= count
)
181 static void gntdev_put_map(struct grant_map
*map
)
186 if (!atomic_dec_and_test(&map
->users
))
189 atomic_sub(map
->count
, &pages_mapped
);
191 if (map
->notify
.flags
& UNMAP_NOTIFY_SEND_EVENT
)
192 notify_remote_via_evtchn(map
->notify
.event
);
196 unmap_grant_pages(map
, 0, map
->count
);
198 free_xenballooned_pages(map
->count
, map
->pages
);
203 kfree(map
->unmap_ops
);
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
;
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
,
228 static int map_grant_pages(struct grant_map
*map
)
233 /* Note: it could already be mapped */
234 if (map
->map_ops
[0].handle
!= -1)
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
,
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
);
252 for (i
= 0; i
< map
->count
; i
++) {
253 if (map
->map_ops
[i
].status
)
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
);
264 static int __unmap_grant_pages(struct grant_map
*map
, int offset
, int pages
)
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);
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
);
289 for (i
= 0; i
< pages
; i
++) {
290 if (map
->unmap_ops
[offset
+i
].status
)
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;
300 static int unmap_grant_pages(struct grant_map
*map
, int offset
, int pages
)
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) {
315 while (range
< pages
) {
316 if (map
->unmap_ops
[offset
+range
].handle
== -1) {
322 err
= __unmap_grant_pages(map
, offset
, range
);
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
);
346 vma
->vm_private_data
= NULL
;
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
;
366 spin_lock(&priv
->lock
);
367 list_for_each_entry(map
, &priv
->maps
, next
) {
370 if (map
->vma
->vm_start
>= end
)
372 if (map
->vma
->vm_end
<= start
)
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
);
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
;
402 spin_lock(&priv
->lock
);
403 list_for_each_entry(map
, &priv
->maps
, next
) {
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
);
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
;
428 priv
= kzalloc(sizeof(*priv
), GFP_KERNEL
);
432 INIT_LIST_HEAD(&priv
->maps
);
433 spin_lock_init(&priv
->lock
);
436 priv
->mm
= get_task_mm(current
);
441 priv
->mn
.ops
= &gntdev_mmu_ops
;
442 ret
= mmu_notifier_register(&priv
->mn
, priv
->mm
);
451 flip
->private_data
= priv
;
452 pr_debug("priv %p\n", priv
);
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
);
470 spin_unlock(&priv
->lock
);
473 mmu_notifier_unregister(&priv
->mn
, priv
->mm
);
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
;
485 if (copy_from_user(&op
, u
, sizeof(op
)) != 0)
487 pr_debug("priv %p, add %d\n", priv
, op
.count
);
488 if (unlikely(op
.count
<= 0))
492 map
= gntdev_alloc_map(priv
, op
.count
);
496 if (unlikely(atomic_add_return(op
.count
, &pages_mapped
) > limit
)) {
497 pr_debug("can't map: over limit\n");
502 if (copy_from_user(map
->grants
, &u
->refs
,
503 sizeof(map
->grants
[0]) * op
.count
) != 0) {
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)
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
;
526 if (copy_from_user(&op
, u
, sizeof(op
)) != 0)
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
);
533 list_del(&map
->next
);
537 spin_unlock(&priv
->lock
);
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)
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
)
556 map
= vma
->vm_private_data
;
560 op
.offset
= map
->index
<< PAGE_SHIFT
;
561 op
.count
= map
->count
;
563 if (copy_to_user(u
, &op
, sizeof(op
)) != 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
;
574 if (copy_from_user(&op
, u
, sizeof(op
)))
577 if (op
.action
& ~(UNMAP_NOTIFY_CLEAR_BYTE
|UNMAP_NOTIFY_SEND_EVENT
))
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
)
592 if ((op
.action
& UNMAP_NOTIFY_CLEAR_BYTE
) &&
593 (map
->flags
& GNTMAP_readonly
)) {
598 map
->notify
.flags
= op
.action
;
599 map
->notify
.addr
= op
.index
- (map
->index
<< PAGE_SHIFT
);
600 map
->notify
.event
= op
.event_channel_port
;
603 spin_unlock(&priv
->lock
);
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
;
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
);
627 pr_debug("priv %p, unknown cmd %x\n", priv
, cmd
);
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
))
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
);
652 if (use_ptemod
&& map
->vma
)
654 if (use_ptemod
&& priv
->mm
!= vma
->vm_mm
) {
655 printk(KERN_WARNING
"Huh? Other mm?\n");
659 atomic_inc(&map
->users
);
661 vma
->vm_ops
= &gntdev_vmops
;
663 vma
->vm_flags
|= VM_RESERVED
|VM_DONTEXPAND
;
666 vma
->vm_flags
|= VM_DONTCOPY
|VM_PFNMAP
;
668 vma
->vm_private_data
= map
;
674 if ((vma
->vm_flags
& VM_WRITE
) &&
675 (map
->flags
& GNTMAP_readonly
))
678 map
->flags
= GNTMAP_host_map
;
679 if (!(vma
->vm_flags
& VM_WRITE
))
680 map
->flags
|= GNTMAP_readonly
;
683 spin_unlock(&priv
->lock
);
686 err
= apply_to_page_range(vma
->vm_mm
, vma
->vm_start
,
687 vma
->vm_end
- vma
->vm_start
,
688 find_grant_ptes
, map
);
690 printk(KERN_WARNING
"find_grant_ptes() failure.\n");
695 err
= map_grant_pages(map
);
700 for (i
= 0; i
< count
; i
++) {
701 err
= vm_insert_page(vma
, vma
->vm_start
+ i
*PAGE_SIZE
,
711 spin_unlock(&priv
->lock
);
715 spin_unlock(&priv
->lock
);
723 static const struct file_operations gntdev_fops
= {
724 .owner
= THIS_MODULE
,
726 .release
= gntdev_release
,
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)
746 use_ptemod
= xen_pv_domain();
748 err
= misc_register(&gntdev_miscdev
);
750 printk(KERN_ERR
"Could not register gntdev device\n");
756 static void __exit
gntdev_exit(void)
758 misc_deregister(&gntdev_miscdev
);
761 module_init(gntdev_init
);
762 module_exit(gntdev_exit
);
764 /* ------------------------------------------------------------------ */