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>
9 * (c) 2018 Oleksandr Andrushchenko, EPAM Systems Inc.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #define pr_fmt(fmt) "xen:" KBUILD_MODNAME ": " fmt
25 #include <linux/dma-mapping.h>
26 #include <linux/module.h>
27 #include <linux/kernel.h>
28 #include <linux/init.h>
29 #include <linux/miscdevice.h>
31 #include <linux/uaccess.h>
32 #include <linux/sched.h>
33 #include <linux/sched/mm.h>
34 #include <linux/spinlock.h>
35 #include <linux/slab.h>
36 #include <linux/highmem.h>
37 #include <linux/refcount.h>
38 #include <linux/workqueue.h>
41 #include <xen/grant_table.h>
42 #include <xen/balloon.h>
43 #include <xen/gntdev.h>
44 #include <xen/events.h>
46 #include <asm/xen/hypervisor.h>
47 #include <asm/xen/hypercall.h>
49 #include "gntdev-common.h"
50 #ifdef CONFIG_XEN_GNTDEV_DMABUF
51 #include "gntdev-dmabuf.h"
54 MODULE_LICENSE("GPL");
55 MODULE_AUTHOR("Derek G. Murray <Derek.Murray@cl.cam.ac.uk>, "
56 "Gerd Hoffmann <kraxel@redhat.com>");
57 MODULE_DESCRIPTION("User-space granted page access driver");
59 static unsigned int limit = 64*1024;
60 module_param(limit, uint, 0644);
61 MODULE_PARM_DESC(limit,
62 "Maximum number of grants that may be mapped by one mapping request");
64 /* True in PV mode, false otherwise */
65 static int use_ptemod;
67 static void unmap_grant_pages(struct gntdev_grant_map *map,
68 int offset, int pages);
70 static struct miscdevice gntdev_miscdev;
72 /* ------------------------------------------------------------------ */
74 bool gntdev_test_page_count(unsigned int count)
76 return !count || count > limit;
79 static void gntdev_print_maps(struct gntdev_priv *priv,
80 char *text, int text_index)
83 struct gntdev_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 void gntdev_free_map(struct gntdev_grant_map *map)
98 #ifdef CONFIG_XEN_GRANT_DMA_ALLOC
100 struct gnttab_dma_alloc_args args;
102 args.dev = map->dma_dev;
103 args.coherent = !!(map->dma_flags & GNTDEV_DMA_FLAG_COHERENT);
104 args.nr_pages = map->count;
105 args.pages = map->pages;
106 args.frames = map->frames;
107 args.vaddr = map->dma_vaddr;
108 args.dev_bus_addr = map->dma_bus_addr;
110 gnttab_dma_free_pages(&args);
114 gnttab_free_pages(map->count, map->pages);
116 #ifdef CONFIG_XEN_GRANT_DMA_ALLOC
121 kvfree(map->map_ops);
122 kvfree(map->unmap_ops);
123 kvfree(map->kmap_ops);
124 kvfree(map->kunmap_ops);
125 kvfree(map->being_removed);
129 struct gntdev_grant_map *gntdev_alloc_map(struct gntdev_priv *priv, int count,
132 struct gntdev_grant_map *add;
135 add = kzalloc(sizeof(*add), GFP_KERNEL);
139 add->grants = kvmalloc_array(count, sizeof(add->grants[0]),
141 add->map_ops = kvmalloc_array(count, sizeof(add->map_ops[0]),
143 add->unmap_ops = kvmalloc_array(count, sizeof(add->unmap_ops[0]),
145 add->pages = kvcalloc(count, sizeof(add->pages[0]), GFP_KERNEL);
147 kvcalloc(count, sizeof(add->being_removed[0]), GFP_KERNEL);
148 if (NULL == add->grants ||
149 NULL == add->map_ops ||
150 NULL == add->unmap_ops ||
151 NULL == add->pages ||
152 NULL == add->being_removed)
155 add->kmap_ops = kvmalloc_array(count, sizeof(add->kmap_ops[0]),
157 add->kunmap_ops = kvmalloc_array(count, sizeof(add->kunmap_ops[0]),
159 if (NULL == add->kmap_ops || NULL == add->kunmap_ops)
163 #ifdef CONFIG_XEN_GRANT_DMA_ALLOC
164 add->dma_flags = dma_flags;
167 * Check if this mapping is requested to be backed
170 if (dma_flags & (GNTDEV_DMA_FLAG_WC | GNTDEV_DMA_FLAG_COHERENT)) {
171 struct gnttab_dma_alloc_args args;
173 add->frames = kvcalloc(count, sizeof(add->frames[0]),
178 /* Remember the device, so we can free DMA memory. */
179 add->dma_dev = priv->dma_dev;
181 args.dev = priv->dma_dev;
182 args.coherent = !!(dma_flags & GNTDEV_DMA_FLAG_COHERENT);
183 args.nr_pages = count;
184 args.pages = add->pages;
185 args.frames = add->frames;
187 if (gnttab_dma_alloc_pages(&args))
190 add->dma_vaddr = args.vaddr;
191 add->dma_bus_addr = args.dev_bus_addr;
194 if (gnttab_alloc_pages(count, add->pages))
197 for (i = 0; i < count; i++) {
198 add->grants[i].domid = DOMID_INVALID;
199 add->grants[i].ref = INVALID_GRANT_REF;
200 add->map_ops[i].handle = INVALID_GRANT_HANDLE;
201 add->unmap_ops[i].handle = INVALID_GRANT_HANDLE;
203 add->kmap_ops[i].handle = INVALID_GRANT_HANDLE;
204 add->kunmap_ops[i].handle = INVALID_GRANT_HANDLE;
210 refcount_set(&add->users, 1);
215 gntdev_free_map(add);
219 void gntdev_add_map(struct gntdev_priv *priv, struct gntdev_grant_map *add)
221 struct gntdev_grant_map *map;
223 list_for_each_entry(map, &priv->maps, next) {
224 if (add->index + add->count < map->index) {
225 list_add_tail(&add->next, &map->next);
228 add->index = map->index + map->count;
230 list_add_tail(&add->next, &priv->maps);
233 gntdev_print_maps(priv, "[new]", add->index);
236 static struct gntdev_grant_map *gntdev_find_map_index(struct gntdev_priv *priv,
237 int index, int count)
239 struct gntdev_grant_map *map;
241 list_for_each_entry(map, &priv->maps, next) {
242 if (map->index != index)
244 if (count && map->count != count)
251 void gntdev_put_map(struct gntdev_priv *priv, struct gntdev_grant_map *map)
256 if (!refcount_dec_and_test(&map->users))
259 if (map->pages && !use_ptemod) {
261 * Increment the reference count. This ensures that the
262 * subsequent call to unmap_grant_pages() will not wind up
263 * re-entering itself. It *can* wind up calling
264 * gntdev_put_map() recursively, but such calls will be with a
265 * reference count greater than 1, so they will return before
266 * this code is reached. The recursion depth is thus limited to
267 * 1. Do NOT use refcount_inc() here, as it will detect that
268 * the reference count is zero and WARN().
270 refcount_set(&map->users, 1);
273 * Unmap the grants. This may or may not be asynchronous, so it
274 * is possible that the reference count is 1 on return, but it
275 * could also be greater than 1.
277 unmap_grant_pages(map, 0, map->count);
279 /* Check if the memory now needs to be freed */
280 if (!refcount_dec_and_test(&map->users))
284 * All pages have been returned to the hypervisor, so free the
289 if (map->notify.flags & UNMAP_NOTIFY_SEND_EVENT) {
290 notify_remote_via_evtchn(map->notify.event);
291 evtchn_put(map->notify.event);
293 gntdev_free_map(map);
296 /* ------------------------------------------------------------------ */
298 static int find_grant_ptes(pte_t *pte, unsigned long addr, void *data)
300 struct gntdev_grant_map *map = data;
301 unsigned int pgnr = (addr - map->vma->vm_start) >> PAGE_SHIFT;
302 int flags = map->flags | GNTMAP_application_map | GNTMAP_contains_pte |
303 (1 << _GNTMAP_guest_avail0);
306 BUG_ON(pgnr >= map->count);
307 pte_maddr = arbitrary_virt_to_machine(pte).maddr;
309 gnttab_set_map_op(&map->map_ops[pgnr], pte_maddr, flags,
310 map->grants[pgnr].ref,
311 map->grants[pgnr].domid);
312 gnttab_set_unmap_op(&map->unmap_ops[pgnr], pte_maddr, flags,
313 INVALID_GRANT_HANDLE);
317 int gntdev_map_grant_pages(struct gntdev_grant_map *map)
323 /* Note: it could already be mapped */
324 if (map->map_ops[0].handle != INVALID_GRANT_HANDLE)
326 for (i = 0; i < map->count; i++) {
327 unsigned long addr = (unsigned long)
328 pfn_to_kaddr(page_to_pfn(map->pages[i]));
329 gnttab_set_map_op(&map->map_ops[i], addr, map->flags,
331 map->grants[i].domid);
332 gnttab_set_unmap_op(&map->unmap_ops[i], addr,
333 map->flags, INVALID_GRANT_HANDLE);
337 * Setup the map_ops corresponding to the pte entries pointing
338 * to the kernel linear addresses of the struct pages.
339 * These ptes are completely different from the user ptes dealt
340 * with find_grant_ptes.
341 * Note that GNTMAP_device_map isn't needed here: The
342 * dev_bus_addr output field gets consumed only from ->map_ops,
343 * and by not requesting it when mapping we also avoid needing
344 * to mirror dev_bus_addr into ->unmap_ops (and holding an extra
345 * reference to the page in the hypervisor).
347 unsigned int flags = (map->flags & ~GNTMAP_device_map) |
350 for (i = 0; i < map->count; i++) {
351 unsigned long address = (unsigned long)
352 pfn_to_kaddr(page_to_pfn(map->pages[i]));
353 BUG_ON(PageHighMem(map->pages[i]));
355 gnttab_set_map_op(&map->kmap_ops[i], address, flags,
357 map->grants[i].domid);
358 gnttab_set_unmap_op(&map->kunmap_ops[i], address,
359 flags, INVALID_GRANT_HANDLE);
363 pr_debug("map %d+%d\n", map->index, map->count);
364 err = gnttab_map_refs(map->map_ops, map->kmap_ops, map->pages,
367 for (i = 0; i < map->count; i++) {
368 if (map->map_ops[i].status == GNTST_okay) {
369 map->unmap_ops[i].handle = map->map_ops[i].handle;
375 if (map->flags & GNTMAP_device_map)
376 map->unmap_ops[i].dev_bus_addr = map->map_ops[i].dev_bus_addr;
379 if (map->kmap_ops[i].status == GNTST_okay) {
380 if (map->map_ops[i].status == GNTST_okay)
382 map->kunmap_ops[i].handle = map->kmap_ops[i].handle;
387 atomic_add(alloced, &map->live_grants);
391 static void __unmap_grant_pages_done(int result,
392 struct gntab_unmap_queue_data *data)
395 struct gntdev_grant_map *map = data->data;
396 unsigned int offset = data->unmap_ops - map->unmap_ops;
398 for (i = 0; i < data->count; i++) {
399 WARN_ON(map->unmap_ops[offset + i].status != GNTST_okay &&
400 map->unmap_ops[offset + i].handle != INVALID_GRANT_HANDLE);
401 pr_debug("unmap handle=%d st=%d\n",
402 map->unmap_ops[offset+i].handle,
403 map->unmap_ops[offset+i].status);
404 map->unmap_ops[offset+i].handle = INVALID_GRANT_HANDLE;
406 WARN_ON(map->kunmap_ops[offset + i].status != GNTST_okay &&
407 map->kunmap_ops[offset + i].handle != INVALID_GRANT_HANDLE);
408 pr_debug("kunmap handle=%u st=%d\n",
409 map->kunmap_ops[offset+i].handle,
410 map->kunmap_ops[offset+i].status);
411 map->kunmap_ops[offset+i].handle = INVALID_GRANT_HANDLE;
415 * Decrease the live-grant counter. This must happen after the loop to
416 * prevent premature reuse of the grants by gnttab_mmap().
418 atomic_sub(data->count, &map->live_grants);
420 /* Release reference taken by __unmap_grant_pages */
421 gntdev_put_map(NULL, map);
424 static void __unmap_grant_pages(struct gntdev_grant_map *map, int offset,
427 if (map->notify.flags & UNMAP_NOTIFY_CLEAR_BYTE) {
428 int pgno = (map->notify.addr >> PAGE_SHIFT);
430 if (pgno >= offset && pgno < offset + pages) {
431 /* No need for kmap, pages are in lowmem */
432 uint8_t *tmp = pfn_to_kaddr(page_to_pfn(map->pages[pgno]));
434 tmp[map->notify.addr & (PAGE_SIZE-1)] = 0;
435 map->notify.flags &= ~UNMAP_NOTIFY_CLEAR_BYTE;
439 map->unmap_data.unmap_ops = map->unmap_ops + offset;
440 map->unmap_data.kunmap_ops = use_ptemod ? map->kunmap_ops + offset : NULL;
441 map->unmap_data.pages = map->pages + offset;
442 map->unmap_data.count = pages;
443 map->unmap_data.done = __unmap_grant_pages_done;
444 map->unmap_data.data = map;
445 refcount_inc(&map->users); /* to keep map alive during async call below */
447 gnttab_unmap_refs_async(&map->unmap_data);
450 static void unmap_grant_pages(struct gntdev_grant_map *map, int offset,
455 if (atomic_read(&map->live_grants) == 0)
456 return; /* Nothing to do */
458 pr_debug("unmap %d+%d [%d+%d]\n", map->index, map->count, offset, pages);
460 /* It is possible the requested range will have a "hole" where we
461 * already unmapped some of the grants. Only unmap valid ranges.
464 while (pages && map->being_removed[offset]) {
469 while (range < pages) {
470 if (map->being_removed[offset + range])
472 map->being_removed[offset + range] = true;
476 __unmap_grant_pages(map, offset, range);
482 /* ------------------------------------------------------------------ */
484 static void gntdev_vma_open(struct vm_area_struct *vma)
486 struct gntdev_grant_map *map = vma->vm_private_data;
488 pr_debug("gntdev_vma_open %p\n", vma);
489 refcount_inc(&map->users);
492 static void gntdev_vma_close(struct vm_area_struct *vma)
494 struct gntdev_grant_map *map = vma->vm_private_data;
495 struct file *file = vma->vm_file;
496 struct gntdev_priv *priv = file->private_data;
498 pr_debug("gntdev_vma_close %p\n", vma);
500 WARN_ON(map->vma != vma);
501 mmu_interval_notifier_remove(&map->notifier);
504 vma->vm_private_data = NULL;
505 gntdev_put_map(priv, map);
508 static struct page *gntdev_vma_find_special_page(struct vm_area_struct *vma,
511 struct gntdev_grant_map *map = vma->vm_private_data;
513 return map->pages[(addr - map->pages_vm_start) >> PAGE_SHIFT];
516 static const struct vm_operations_struct gntdev_vmops = {
517 .open = gntdev_vma_open,
518 .close = gntdev_vma_close,
519 .find_special_page = gntdev_vma_find_special_page,
522 /* ------------------------------------------------------------------ */
524 static bool gntdev_invalidate(struct mmu_interval_notifier *mn,
525 const struct mmu_notifier_range *range,
526 unsigned long cur_seq)
528 struct gntdev_grant_map *map =
529 container_of(mn, struct gntdev_grant_map, notifier);
530 unsigned long mstart, mend;
532 if (!mmu_notifier_range_blockable(range))
536 * If the VMA is split or otherwise changed the notifier is not
537 * updated, but we don't want to process VA's outside the modified
538 * VMA. FIXME: It would be much more understandable to just prevent
539 * modifying the VMA in the first place.
541 if (map->vma->vm_start >= range->end ||
542 map->vma->vm_end <= range->start)
545 mstart = max(range->start, map->vma->vm_start);
546 mend = min(range->end, map->vma->vm_end);
547 pr_debug("map %d+%d (%lx %lx), range %lx %lx, mrange %lx %lx\n",
548 map->index, map->count,
549 map->vma->vm_start, map->vma->vm_end,
550 range->start, range->end, mstart, mend);
551 unmap_grant_pages(map,
552 (mstart - map->vma->vm_start) >> PAGE_SHIFT,
553 (mend - mstart) >> PAGE_SHIFT);
558 static const struct mmu_interval_notifier_ops gntdev_mmu_ops = {
559 .invalidate = gntdev_invalidate,
562 /* ------------------------------------------------------------------ */
564 static int gntdev_open(struct inode *inode, struct file *flip)
566 struct gntdev_priv *priv;
568 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
572 INIT_LIST_HEAD(&priv->maps);
573 mutex_init(&priv->lock);
575 #ifdef CONFIG_XEN_GNTDEV_DMABUF
576 priv->dmabuf_priv = gntdev_dmabuf_init(flip);
577 if (IS_ERR(priv->dmabuf_priv)) {
578 int ret = PTR_ERR(priv->dmabuf_priv);
585 flip->private_data = priv;
586 #ifdef CONFIG_XEN_GRANT_DMA_ALLOC
587 priv->dma_dev = gntdev_miscdev.this_device;
588 dma_coerce_mask_and_coherent(priv->dma_dev, DMA_BIT_MASK(64));
590 pr_debug("priv %p\n", priv);
595 static int gntdev_release(struct inode *inode, struct file *flip)
597 struct gntdev_priv *priv = flip->private_data;
598 struct gntdev_grant_map *map;
600 pr_debug("priv %p\n", priv);
602 mutex_lock(&priv->lock);
603 while (!list_empty(&priv->maps)) {
604 map = list_entry(priv->maps.next,
605 struct gntdev_grant_map, next);
606 list_del(&map->next);
607 gntdev_put_map(NULL /* already removed */, map);
609 mutex_unlock(&priv->lock);
611 #ifdef CONFIG_XEN_GNTDEV_DMABUF
612 gntdev_dmabuf_fini(priv->dmabuf_priv);
619 static long gntdev_ioctl_map_grant_ref(struct gntdev_priv *priv,
620 struct ioctl_gntdev_map_grant_ref __user *u)
622 struct ioctl_gntdev_map_grant_ref op;
623 struct gntdev_grant_map *map;
626 if (copy_from_user(&op, u, sizeof(op)) != 0)
628 pr_debug("priv %p, add %d\n", priv, op.count);
629 if (unlikely(gntdev_test_page_count(op.count)))
633 map = gntdev_alloc_map(priv, op.count, 0 /* This is not a dma-buf. */);
637 if (copy_from_user(map->grants, &u->refs,
638 sizeof(map->grants[0]) * op.count) != 0) {
639 gntdev_put_map(NULL, map);
643 mutex_lock(&priv->lock);
644 gntdev_add_map(priv, map);
645 op.index = map->index << PAGE_SHIFT;
646 mutex_unlock(&priv->lock);
648 if (copy_to_user(u, &op, sizeof(op)) != 0)
654 static long gntdev_ioctl_unmap_grant_ref(struct gntdev_priv *priv,
655 struct ioctl_gntdev_unmap_grant_ref __user *u)
657 struct ioctl_gntdev_unmap_grant_ref op;
658 struct gntdev_grant_map *map;
661 if (copy_from_user(&op, u, sizeof(op)) != 0)
663 pr_debug("priv %p, del %d+%d\n", priv, (int)op.index, (int)op.count);
665 mutex_lock(&priv->lock);
666 map = gntdev_find_map_index(priv, op.index >> PAGE_SHIFT, op.count);
668 list_del(&map->next);
671 mutex_unlock(&priv->lock);
673 gntdev_put_map(priv, map);
677 static long gntdev_ioctl_get_offset_for_vaddr(struct gntdev_priv *priv,
678 struct ioctl_gntdev_get_offset_for_vaddr __user *u)
680 struct ioctl_gntdev_get_offset_for_vaddr op;
681 struct vm_area_struct *vma;
682 struct gntdev_grant_map *map;
685 if (copy_from_user(&op, u, sizeof(op)) != 0)
687 pr_debug("priv %p, offset for vaddr %lx\n", priv, (unsigned long)op.vaddr);
689 mmap_read_lock(current->mm);
690 vma = find_vma(current->mm, op.vaddr);
691 if (!vma || vma->vm_ops != &gntdev_vmops)
694 map = vma->vm_private_data;
698 op.offset = map->index << PAGE_SHIFT;
699 op.count = map->count;
703 mmap_read_unlock(current->mm);
705 if (rv == 0 && copy_to_user(u, &op, sizeof(op)) != 0)
710 static long gntdev_ioctl_notify(struct gntdev_priv *priv, void __user *u)
712 struct ioctl_gntdev_unmap_notify op;
713 struct gntdev_grant_map *map;
716 evtchn_port_t out_event;
718 if (copy_from_user(&op, u, sizeof(op)))
721 if (op.action & ~(UNMAP_NOTIFY_CLEAR_BYTE|UNMAP_NOTIFY_SEND_EVENT))
724 /* We need to grab a reference to the event channel we are going to use
725 * to send the notify before releasing the reference we may already have
726 * (if someone has called this ioctl twice). This is required so that
727 * it is possible to change the clear_byte part of the notification
728 * without disturbing the event channel part, which may now be the last
729 * reference to that event channel.
731 if (op.action & UNMAP_NOTIFY_SEND_EVENT) {
732 if (evtchn_get(op.event_channel_port))
736 out_flags = op.action;
737 out_event = op.event_channel_port;
739 mutex_lock(&priv->lock);
741 list_for_each_entry(map, &priv->maps, next) {
742 uint64_t begin = map->index << PAGE_SHIFT;
743 uint64_t end = (map->index + map->count) << PAGE_SHIFT;
744 if (op.index >= begin && op.index < end)
751 if ((op.action & UNMAP_NOTIFY_CLEAR_BYTE) &&
752 (map->flags & GNTMAP_readonly)) {
757 out_flags = map->notify.flags;
758 out_event = map->notify.event;
760 map->notify.flags = op.action;
761 map->notify.addr = op.index - (map->index << PAGE_SHIFT);
762 map->notify.event = op.event_channel_port;
767 mutex_unlock(&priv->lock);
769 /* Drop the reference to the event channel we did not save in the map */
770 if (out_flags & UNMAP_NOTIFY_SEND_EVENT)
771 evtchn_put(out_event);
776 #define GNTDEV_COPY_BATCH 16
778 struct gntdev_copy_batch {
779 struct gnttab_copy ops[GNTDEV_COPY_BATCH];
780 struct page *pages[GNTDEV_COPY_BATCH];
781 s16 __user *status[GNTDEV_COPY_BATCH];
783 unsigned int nr_pages;
787 static int gntdev_get_page(struct gntdev_copy_batch *batch, void __user *virt,
790 unsigned long addr = (unsigned long)virt;
792 unsigned long xen_pfn;
795 ret = pin_user_pages_fast(addr, 1, batch->writeable ? FOLL_WRITE : 0, &page);
799 batch->pages[batch->nr_pages++] = page;
801 xen_pfn = page_to_xen_pfn(page) + XEN_PFN_DOWN(addr & ~PAGE_MASK);
802 *gfn = pfn_to_gfn(xen_pfn);
807 static void gntdev_put_pages(struct gntdev_copy_batch *batch)
809 unpin_user_pages_dirty_lock(batch->pages, batch->nr_pages, batch->writeable);
811 batch->writeable = false;
814 static int gntdev_copy(struct gntdev_copy_batch *batch)
818 gnttab_batch_copy(batch->ops, batch->nr_ops);
819 gntdev_put_pages(batch);
822 * For each completed op, update the status if the op failed
823 * and all previous ops for the segment were successful.
825 for (i = 0; i < batch->nr_ops; i++) {
826 s16 status = batch->ops[i].status;
829 if (status == GNTST_okay)
832 if (__get_user(old_status, batch->status[i]))
835 if (old_status != GNTST_okay)
838 if (__put_user(status, batch->status[i]))
846 static int gntdev_grant_copy_seg(struct gntdev_copy_batch *batch,
847 struct gntdev_grant_copy_segment *seg,
853 * Disallow local -> local copies since there is only space in
854 * batch->pages for one page per-op and this would be a very
855 * expensive memcpy().
857 if (!(seg->flags & (GNTCOPY_source_gref | GNTCOPY_dest_gref)))
860 /* Can't cross page if source/dest is a grant ref. */
861 if (seg->flags & GNTCOPY_source_gref) {
862 if (seg->source.foreign.offset + seg->len > XEN_PAGE_SIZE)
865 if (seg->flags & GNTCOPY_dest_gref) {
866 if (seg->dest.foreign.offset + seg->len > XEN_PAGE_SIZE)
870 if (put_user(GNTST_okay, status))
873 while (copied < seg->len) {
874 struct gnttab_copy *op;
880 if (batch->nr_ops >= GNTDEV_COPY_BATCH) {
881 ret = gntdev_copy(batch);
886 len = seg->len - copied;
888 op = &batch->ops[batch->nr_ops];
891 if (seg->flags & GNTCOPY_source_gref) {
892 op->source.u.ref = seg->source.foreign.ref;
893 op->source.domid = seg->source.foreign.domid;
894 op->source.offset = seg->source.foreign.offset + copied;
895 op->flags |= GNTCOPY_source_gref;
897 virt = seg->source.virt + copied;
898 off = (unsigned long)virt & ~XEN_PAGE_MASK;
899 len = min(len, (size_t)XEN_PAGE_SIZE - off);
900 batch->writeable = false;
902 ret = gntdev_get_page(batch, virt, &gfn);
906 op->source.u.gmfn = gfn;
907 op->source.domid = DOMID_SELF;
908 op->source.offset = off;
911 if (seg->flags & GNTCOPY_dest_gref) {
912 op->dest.u.ref = seg->dest.foreign.ref;
913 op->dest.domid = seg->dest.foreign.domid;
914 op->dest.offset = seg->dest.foreign.offset + copied;
915 op->flags |= GNTCOPY_dest_gref;
917 virt = seg->dest.virt + copied;
918 off = (unsigned long)virt & ~XEN_PAGE_MASK;
919 len = min(len, (size_t)XEN_PAGE_SIZE - off);
920 batch->writeable = true;
922 ret = gntdev_get_page(batch, virt, &gfn);
926 op->dest.u.gmfn = gfn;
927 op->dest.domid = DOMID_SELF;
928 op->dest.offset = off;
934 batch->status[batch->nr_ops] = status;
941 static long gntdev_ioctl_grant_copy(struct gntdev_priv *priv, void __user *u)
943 struct ioctl_gntdev_grant_copy copy;
944 struct gntdev_copy_batch batch;
948 if (copy_from_user(©, u, sizeof(copy)))
954 for (i = 0; i < copy.count; i++) {
955 struct gntdev_grant_copy_segment seg;
957 if (copy_from_user(&seg, ©.segments[i], sizeof(seg))) {
962 ret = gntdev_grant_copy_seg(&batch, &seg, ©.segments[i].status);
969 ret = gntdev_copy(&batch);
973 gntdev_put_pages(&batch);
977 static long gntdev_ioctl(struct file *flip,
978 unsigned int cmd, unsigned long arg)
980 struct gntdev_priv *priv = flip->private_data;
981 void __user *ptr = (void __user *)arg;
984 case IOCTL_GNTDEV_MAP_GRANT_REF:
985 return gntdev_ioctl_map_grant_ref(priv, ptr);
987 case IOCTL_GNTDEV_UNMAP_GRANT_REF:
988 return gntdev_ioctl_unmap_grant_ref(priv, ptr);
990 case IOCTL_GNTDEV_GET_OFFSET_FOR_VADDR:
991 return gntdev_ioctl_get_offset_for_vaddr(priv, ptr);
993 case IOCTL_GNTDEV_SET_UNMAP_NOTIFY:
994 return gntdev_ioctl_notify(priv, ptr);
996 case IOCTL_GNTDEV_GRANT_COPY:
997 return gntdev_ioctl_grant_copy(priv, ptr);
999 #ifdef CONFIG_XEN_GNTDEV_DMABUF
1000 case IOCTL_GNTDEV_DMABUF_EXP_FROM_REFS:
1001 return gntdev_ioctl_dmabuf_exp_from_refs(priv, use_ptemod, ptr);
1003 case IOCTL_GNTDEV_DMABUF_EXP_WAIT_RELEASED:
1004 return gntdev_ioctl_dmabuf_exp_wait_released(priv, ptr);
1006 case IOCTL_GNTDEV_DMABUF_IMP_TO_REFS:
1007 return gntdev_ioctl_dmabuf_imp_to_refs(priv, ptr);
1009 case IOCTL_GNTDEV_DMABUF_IMP_RELEASE:
1010 return gntdev_ioctl_dmabuf_imp_release(priv, ptr);
1014 pr_debug("priv %p, unknown cmd %x\n", priv, cmd);
1015 return -ENOIOCTLCMD;
1021 static int gntdev_mmap(struct file *flip, struct vm_area_struct *vma)
1023 struct gntdev_priv *priv = flip->private_data;
1024 int index = vma->vm_pgoff;
1025 int count = vma_pages(vma);
1026 struct gntdev_grant_map *map;
1029 if ((vma->vm_flags & VM_WRITE) && !(vma->vm_flags & VM_SHARED))
1032 pr_debug("map %d+%d at %lx (pgoff %lx)\n",
1033 index, count, vma->vm_start, vma->vm_pgoff);
1035 mutex_lock(&priv->lock);
1036 map = gntdev_find_map_index(priv, index, count);
1039 if (use_ptemod && map->vma)
1041 if (atomic_read(&map->live_grants)) {
1045 refcount_inc(&map->users);
1047 vma->vm_ops = &gntdev_vmops;
1049 vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP | VM_MIXEDMAP;
1052 vma->vm_flags |= VM_DONTCOPY;
1054 vma->vm_private_data = map;
1056 if ((vma->vm_flags & VM_WRITE) &&
1057 (map->flags & GNTMAP_readonly))
1058 goto out_unlock_put;
1060 map->flags = GNTMAP_host_map;
1061 if (!(vma->vm_flags & VM_WRITE))
1062 map->flags |= GNTMAP_readonly;
1067 err = mmu_interval_notifier_insert_locked(
1068 &map->notifier, vma->vm_mm, vma->vm_start,
1069 vma->vm_end - vma->vm_start, &gntdev_mmu_ops);
1072 goto out_unlock_put;
1075 mutex_unlock(&priv->lock);
1079 * gntdev takes the address of the PTE in find_grant_ptes() and
1080 * passes it to the hypervisor in gntdev_map_grant_pages(). The
1081 * purpose of the notifier is to prevent the hypervisor pointer
1082 * to the PTE from going stale.
1084 * Since this vma's mappings can't be touched without the
1085 * mmap_lock, and we are holding it now, there is no need for
1086 * the notifier_range locking pattern.
1088 mmu_interval_read_begin(&map->notifier);
1090 map->pages_vm_start = vma->vm_start;
1091 err = apply_to_page_range(vma->vm_mm, vma->vm_start,
1092 vma->vm_end - vma->vm_start,
1093 find_grant_ptes, map);
1095 pr_warn("find_grant_ptes() failure.\n");
1100 err = gntdev_map_grant_pages(map);
1105 err = vm_map_pages_zero(vma, map->pages, map->count);
1113 mutex_unlock(&priv->lock);
1117 mutex_unlock(&priv->lock);
1120 unmap_grant_pages(map, 0, map->count);
1122 mmu_interval_notifier_remove(&map->notifier);
1126 gntdev_put_map(priv, map);
1130 static const struct file_operations gntdev_fops = {
1131 .owner = THIS_MODULE,
1132 .open = gntdev_open,
1133 .release = gntdev_release,
1134 .mmap = gntdev_mmap,
1135 .unlocked_ioctl = gntdev_ioctl
1138 static struct miscdevice gntdev_miscdev = {
1139 .minor = MISC_DYNAMIC_MINOR,
1140 .name = "xen/gntdev",
1141 .fops = &gntdev_fops,
1144 /* ------------------------------------------------------------------ */
1146 static int __init gntdev_init(void)
1153 use_ptemod = !xen_feature(XENFEAT_auto_translated_physmap);
1155 err = misc_register(&gntdev_miscdev);
1157 pr_err("Could not register gntdev device\n");
1163 static void __exit gntdev_exit(void)
1165 misc_deregister(&gntdev_miscdev);
1168 module_init(gntdev_init);
1169 module_exit(gntdev_exit);
1171 /* ------------------------------------------------------------------ */