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);
400 pr_debug("unmap handle=%d st=%d\n",
401 map->unmap_ops[offset+i].handle,
402 map->unmap_ops[offset+i].status);
403 map->unmap_ops[offset+i].handle = INVALID_GRANT_HANDLE;
405 WARN_ON(map->kunmap_ops[offset+i].status);
406 pr_debug("kunmap handle=%u st=%d\n",
407 map->kunmap_ops[offset+i].handle,
408 map->kunmap_ops[offset+i].status);
409 map->kunmap_ops[offset+i].handle = INVALID_GRANT_HANDLE;
413 * Decrease the live-grant counter. This must happen after the loop to
414 * prevent premature reuse of the grants by gnttab_mmap().
416 atomic_sub(data->count, &map->live_grants);
418 /* Release reference taken by __unmap_grant_pages */
419 gntdev_put_map(NULL, map);
422 static void __unmap_grant_pages(struct gntdev_grant_map *map, int offset,
425 if (map->notify.flags & UNMAP_NOTIFY_CLEAR_BYTE) {
426 int pgno = (map->notify.addr >> PAGE_SHIFT);
428 if (pgno >= offset && pgno < offset + pages) {
429 /* No need for kmap, pages are in lowmem */
430 uint8_t *tmp = pfn_to_kaddr(page_to_pfn(map->pages[pgno]));
432 tmp[map->notify.addr & (PAGE_SIZE-1)] = 0;
433 map->notify.flags &= ~UNMAP_NOTIFY_CLEAR_BYTE;
437 map->unmap_data.unmap_ops = map->unmap_ops + offset;
438 map->unmap_data.kunmap_ops = use_ptemod ? map->kunmap_ops + offset : NULL;
439 map->unmap_data.pages = map->pages + offset;
440 map->unmap_data.count = pages;
441 map->unmap_data.done = __unmap_grant_pages_done;
442 map->unmap_data.data = map;
443 refcount_inc(&map->users); /* to keep map alive during async call below */
445 gnttab_unmap_refs_async(&map->unmap_data);
448 static void unmap_grant_pages(struct gntdev_grant_map *map, int offset,
453 if (atomic_read(&map->live_grants) == 0)
454 return; /* Nothing to do */
456 pr_debug("unmap %d+%d [%d+%d]\n", map->index, map->count, offset, pages);
458 /* It is possible the requested range will have a "hole" where we
459 * already unmapped some of the grants. Only unmap valid ranges.
462 while (pages && map->being_removed[offset]) {
467 while (range < pages) {
468 if (map->being_removed[offset + range])
470 map->being_removed[offset + range] = true;
474 __unmap_grant_pages(map, offset, range);
480 /* ------------------------------------------------------------------ */
482 static void gntdev_vma_open(struct vm_area_struct *vma)
484 struct gntdev_grant_map *map = vma->vm_private_data;
486 pr_debug("gntdev_vma_open %p\n", vma);
487 refcount_inc(&map->users);
490 static void gntdev_vma_close(struct vm_area_struct *vma)
492 struct gntdev_grant_map *map = vma->vm_private_data;
493 struct file *file = vma->vm_file;
494 struct gntdev_priv *priv = file->private_data;
496 pr_debug("gntdev_vma_close %p\n", vma);
498 WARN_ON(map->vma != vma);
499 mmu_interval_notifier_remove(&map->notifier);
502 vma->vm_private_data = NULL;
503 gntdev_put_map(priv, map);
506 static struct page *gntdev_vma_find_special_page(struct vm_area_struct *vma,
509 struct gntdev_grant_map *map = vma->vm_private_data;
511 return map->pages[(addr - map->pages_vm_start) >> PAGE_SHIFT];
514 static const struct vm_operations_struct gntdev_vmops = {
515 .open = gntdev_vma_open,
516 .close = gntdev_vma_close,
517 .find_special_page = gntdev_vma_find_special_page,
520 /* ------------------------------------------------------------------ */
522 static bool gntdev_invalidate(struct mmu_interval_notifier *mn,
523 const struct mmu_notifier_range *range,
524 unsigned long cur_seq)
526 struct gntdev_grant_map *map =
527 container_of(mn, struct gntdev_grant_map, notifier);
528 unsigned long mstart, mend;
530 if (!mmu_notifier_range_blockable(range))
534 * If the VMA is split or otherwise changed the notifier is not
535 * updated, but we don't want to process VA's outside the modified
536 * VMA. FIXME: It would be much more understandable to just prevent
537 * modifying the VMA in the first place.
539 if (map->vma->vm_start >= range->end ||
540 map->vma->vm_end <= range->start)
543 mstart = max(range->start, map->vma->vm_start);
544 mend = min(range->end, map->vma->vm_end);
545 pr_debug("map %d+%d (%lx %lx), range %lx %lx, mrange %lx %lx\n",
546 map->index, map->count,
547 map->vma->vm_start, map->vma->vm_end,
548 range->start, range->end, mstart, mend);
549 unmap_grant_pages(map,
550 (mstart - map->vma->vm_start) >> PAGE_SHIFT,
551 (mend - mstart) >> PAGE_SHIFT);
556 static const struct mmu_interval_notifier_ops gntdev_mmu_ops = {
557 .invalidate = gntdev_invalidate,
560 /* ------------------------------------------------------------------ */
562 static int gntdev_open(struct inode *inode, struct file *flip)
564 struct gntdev_priv *priv;
566 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
570 INIT_LIST_HEAD(&priv->maps);
571 mutex_init(&priv->lock);
573 #ifdef CONFIG_XEN_GNTDEV_DMABUF
574 priv->dmabuf_priv = gntdev_dmabuf_init(flip);
575 if (IS_ERR(priv->dmabuf_priv)) {
576 int ret = PTR_ERR(priv->dmabuf_priv);
583 flip->private_data = priv;
584 #ifdef CONFIG_XEN_GRANT_DMA_ALLOC
585 priv->dma_dev = gntdev_miscdev.this_device;
586 dma_coerce_mask_and_coherent(priv->dma_dev, DMA_BIT_MASK(64));
588 pr_debug("priv %p\n", priv);
593 static int gntdev_release(struct inode *inode, struct file *flip)
595 struct gntdev_priv *priv = flip->private_data;
596 struct gntdev_grant_map *map;
598 pr_debug("priv %p\n", priv);
600 mutex_lock(&priv->lock);
601 while (!list_empty(&priv->maps)) {
602 map = list_entry(priv->maps.next,
603 struct gntdev_grant_map, next);
604 list_del(&map->next);
605 gntdev_put_map(NULL /* already removed */, map);
607 mutex_unlock(&priv->lock);
609 #ifdef CONFIG_XEN_GNTDEV_DMABUF
610 gntdev_dmabuf_fini(priv->dmabuf_priv);
617 static long gntdev_ioctl_map_grant_ref(struct gntdev_priv *priv,
618 struct ioctl_gntdev_map_grant_ref __user *u)
620 struct ioctl_gntdev_map_grant_ref op;
621 struct gntdev_grant_map *map;
624 if (copy_from_user(&op, u, sizeof(op)) != 0)
626 pr_debug("priv %p, add %d\n", priv, op.count);
627 if (unlikely(gntdev_test_page_count(op.count)))
631 map = gntdev_alloc_map(priv, op.count, 0 /* This is not a dma-buf. */);
635 if (copy_from_user(map->grants, &u->refs,
636 sizeof(map->grants[0]) * op.count) != 0) {
637 gntdev_put_map(NULL, map);
641 mutex_lock(&priv->lock);
642 gntdev_add_map(priv, map);
643 op.index = map->index << PAGE_SHIFT;
644 mutex_unlock(&priv->lock);
646 if (copy_to_user(u, &op, sizeof(op)) != 0)
652 static long gntdev_ioctl_unmap_grant_ref(struct gntdev_priv *priv,
653 struct ioctl_gntdev_unmap_grant_ref __user *u)
655 struct ioctl_gntdev_unmap_grant_ref op;
656 struct gntdev_grant_map *map;
659 if (copy_from_user(&op, u, sizeof(op)) != 0)
661 pr_debug("priv %p, del %d+%d\n", priv, (int)op.index, (int)op.count);
663 mutex_lock(&priv->lock);
664 map = gntdev_find_map_index(priv, op.index >> PAGE_SHIFT, op.count);
666 list_del(&map->next);
669 mutex_unlock(&priv->lock);
671 gntdev_put_map(priv, map);
675 static long gntdev_ioctl_get_offset_for_vaddr(struct gntdev_priv *priv,
676 struct ioctl_gntdev_get_offset_for_vaddr __user *u)
678 struct ioctl_gntdev_get_offset_for_vaddr op;
679 struct vm_area_struct *vma;
680 struct gntdev_grant_map *map;
683 if (copy_from_user(&op, u, sizeof(op)) != 0)
685 pr_debug("priv %p, offset for vaddr %lx\n", priv, (unsigned long)op.vaddr);
687 mmap_read_lock(current->mm);
688 vma = find_vma(current->mm, op.vaddr);
689 if (!vma || vma->vm_ops != &gntdev_vmops)
692 map = vma->vm_private_data;
696 op.offset = map->index << PAGE_SHIFT;
697 op.count = map->count;
701 mmap_read_unlock(current->mm);
703 if (rv == 0 && copy_to_user(u, &op, sizeof(op)) != 0)
708 static long gntdev_ioctl_notify(struct gntdev_priv *priv, void __user *u)
710 struct ioctl_gntdev_unmap_notify op;
711 struct gntdev_grant_map *map;
714 evtchn_port_t out_event;
716 if (copy_from_user(&op, u, sizeof(op)))
719 if (op.action & ~(UNMAP_NOTIFY_CLEAR_BYTE|UNMAP_NOTIFY_SEND_EVENT))
722 /* We need to grab a reference to the event channel we are going to use
723 * to send the notify before releasing the reference we may already have
724 * (if someone has called this ioctl twice). This is required so that
725 * it is possible to change the clear_byte part of the notification
726 * without disturbing the event channel part, which may now be the last
727 * reference to that event channel.
729 if (op.action & UNMAP_NOTIFY_SEND_EVENT) {
730 if (evtchn_get(op.event_channel_port))
734 out_flags = op.action;
735 out_event = op.event_channel_port;
737 mutex_lock(&priv->lock);
739 list_for_each_entry(map, &priv->maps, next) {
740 uint64_t begin = map->index << PAGE_SHIFT;
741 uint64_t end = (map->index + map->count) << PAGE_SHIFT;
742 if (op.index >= begin && op.index < end)
749 if ((op.action & UNMAP_NOTIFY_CLEAR_BYTE) &&
750 (map->flags & GNTMAP_readonly)) {
755 out_flags = map->notify.flags;
756 out_event = map->notify.event;
758 map->notify.flags = op.action;
759 map->notify.addr = op.index - (map->index << PAGE_SHIFT);
760 map->notify.event = op.event_channel_port;
765 mutex_unlock(&priv->lock);
767 /* Drop the reference to the event channel we did not save in the map */
768 if (out_flags & UNMAP_NOTIFY_SEND_EVENT)
769 evtchn_put(out_event);
774 #define GNTDEV_COPY_BATCH 16
776 struct gntdev_copy_batch {
777 struct gnttab_copy ops[GNTDEV_COPY_BATCH];
778 struct page *pages[GNTDEV_COPY_BATCH];
779 s16 __user *status[GNTDEV_COPY_BATCH];
781 unsigned int nr_pages;
785 static int gntdev_get_page(struct gntdev_copy_batch *batch, void __user *virt,
788 unsigned long addr = (unsigned long)virt;
790 unsigned long xen_pfn;
793 ret = pin_user_pages_fast(addr, 1, batch->writeable ? FOLL_WRITE : 0, &page);
797 batch->pages[batch->nr_pages++] = page;
799 xen_pfn = page_to_xen_pfn(page) + XEN_PFN_DOWN(addr & ~PAGE_MASK);
800 *gfn = pfn_to_gfn(xen_pfn);
805 static void gntdev_put_pages(struct gntdev_copy_batch *batch)
807 unpin_user_pages_dirty_lock(batch->pages, batch->nr_pages, batch->writeable);
809 batch->writeable = false;
812 static int gntdev_copy(struct gntdev_copy_batch *batch)
816 gnttab_batch_copy(batch->ops, batch->nr_ops);
817 gntdev_put_pages(batch);
820 * For each completed op, update the status if the op failed
821 * and all previous ops for the segment were successful.
823 for (i = 0; i < batch->nr_ops; i++) {
824 s16 status = batch->ops[i].status;
827 if (status == GNTST_okay)
830 if (__get_user(old_status, batch->status[i]))
833 if (old_status != GNTST_okay)
836 if (__put_user(status, batch->status[i]))
844 static int gntdev_grant_copy_seg(struct gntdev_copy_batch *batch,
845 struct gntdev_grant_copy_segment *seg,
851 * Disallow local -> local copies since there is only space in
852 * batch->pages for one page per-op and this would be a very
853 * expensive memcpy().
855 if (!(seg->flags & (GNTCOPY_source_gref | GNTCOPY_dest_gref)))
858 /* Can't cross page if source/dest is a grant ref. */
859 if (seg->flags & GNTCOPY_source_gref) {
860 if (seg->source.foreign.offset + seg->len > XEN_PAGE_SIZE)
863 if (seg->flags & GNTCOPY_dest_gref) {
864 if (seg->dest.foreign.offset + seg->len > XEN_PAGE_SIZE)
868 if (put_user(GNTST_okay, status))
871 while (copied < seg->len) {
872 struct gnttab_copy *op;
878 if (batch->nr_ops >= GNTDEV_COPY_BATCH) {
879 ret = gntdev_copy(batch);
884 len = seg->len - copied;
886 op = &batch->ops[batch->nr_ops];
889 if (seg->flags & GNTCOPY_source_gref) {
890 op->source.u.ref = seg->source.foreign.ref;
891 op->source.domid = seg->source.foreign.domid;
892 op->source.offset = seg->source.foreign.offset + copied;
893 op->flags |= GNTCOPY_source_gref;
895 virt = seg->source.virt + copied;
896 off = (unsigned long)virt & ~XEN_PAGE_MASK;
897 len = min(len, (size_t)XEN_PAGE_SIZE - off);
898 batch->writeable = false;
900 ret = gntdev_get_page(batch, virt, &gfn);
904 op->source.u.gmfn = gfn;
905 op->source.domid = DOMID_SELF;
906 op->source.offset = off;
909 if (seg->flags & GNTCOPY_dest_gref) {
910 op->dest.u.ref = seg->dest.foreign.ref;
911 op->dest.domid = seg->dest.foreign.domid;
912 op->dest.offset = seg->dest.foreign.offset + copied;
913 op->flags |= GNTCOPY_dest_gref;
915 virt = seg->dest.virt + copied;
916 off = (unsigned long)virt & ~XEN_PAGE_MASK;
917 len = min(len, (size_t)XEN_PAGE_SIZE - off);
918 batch->writeable = true;
920 ret = gntdev_get_page(batch, virt, &gfn);
924 op->dest.u.gmfn = gfn;
925 op->dest.domid = DOMID_SELF;
926 op->dest.offset = off;
932 batch->status[batch->nr_ops] = status;
939 static long gntdev_ioctl_grant_copy(struct gntdev_priv *priv, void __user *u)
941 struct ioctl_gntdev_grant_copy copy;
942 struct gntdev_copy_batch batch;
946 if (copy_from_user(©, u, sizeof(copy)))
952 for (i = 0; i < copy.count; i++) {
953 struct gntdev_grant_copy_segment seg;
955 if (copy_from_user(&seg, ©.segments[i], sizeof(seg))) {
960 ret = gntdev_grant_copy_seg(&batch, &seg, ©.segments[i].status);
967 ret = gntdev_copy(&batch);
971 gntdev_put_pages(&batch);
975 static long gntdev_ioctl(struct file *flip,
976 unsigned int cmd, unsigned long arg)
978 struct gntdev_priv *priv = flip->private_data;
979 void __user *ptr = (void __user *)arg;
982 case IOCTL_GNTDEV_MAP_GRANT_REF:
983 return gntdev_ioctl_map_grant_ref(priv, ptr);
985 case IOCTL_GNTDEV_UNMAP_GRANT_REF:
986 return gntdev_ioctl_unmap_grant_ref(priv, ptr);
988 case IOCTL_GNTDEV_GET_OFFSET_FOR_VADDR:
989 return gntdev_ioctl_get_offset_for_vaddr(priv, ptr);
991 case IOCTL_GNTDEV_SET_UNMAP_NOTIFY:
992 return gntdev_ioctl_notify(priv, ptr);
994 case IOCTL_GNTDEV_GRANT_COPY:
995 return gntdev_ioctl_grant_copy(priv, ptr);
997 #ifdef CONFIG_XEN_GNTDEV_DMABUF
998 case IOCTL_GNTDEV_DMABUF_EXP_FROM_REFS:
999 return gntdev_ioctl_dmabuf_exp_from_refs(priv, use_ptemod, ptr);
1001 case IOCTL_GNTDEV_DMABUF_EXP_WAIT_RELEASED:
1002 return gntdev_ioctl_dmabuf_exp_wait_released(priv, ptr);
1004 case IOCTL_GNTDEV_DMABUF_IMP_TO_REFS:
1005 return gntdev_ioctl_dmabuf_imp_to_refs(priv, ptr);
1007 case IOCTL_GNTDEV_DMABUF_IMP_RELEASE:
1008 return gntdev_ioctl_dmabuf_imp_release(priv, ptr);
1012 pr_debug("priv %p, unknown cmd %x\n", priv, cmd);
1013 return -ENOIOCTLCMD;
1019 static int gntdev_mmap(struct file *flip, struct vm_area_struct *vma)
1021 struct gntdev_priv *priv = flip->private_data;
1022 int index = vma->vm_pgoff;
1023 int count = vma_pages(vma);
1024 struct gntdev_grant_map *map;
1027 if ((vma->vm_flags & VM_WRITE) && !(vma->vm_flags & VM_SHARED))
1030 pr_debug("map %d+%d at %lx (pgoff %lx)\n",
1031 index, count, vma->vm_start, vma->vm_pgoff);
1033 mutex_lock(&priv->lock);
1034 map = gntdev_find_map_index(priv, index, count);
1037 if (use_ptemod && map->vma)
1039 if (atomic_read(&map->live_grants)) {
1043 refcount_inc(&map->users);
1045 vma->vm_ops = &gntdev_vmops;
1047 vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP | VM_MIXEDMAP;
1050 vma->vm_flags |= VM_DONTCOPY;
1052 vma->vm_private_data = map;
1054 if ((vma->vm_flags & VM_WRITE) &&
1055 (map->flags & GNTMAP_readonly))
1056 goto out_unlock_put;
1058 map->flags = GNTMAP_host_map;
1059 if (!(vma->vm_flags & VM_WRITE))
1060 map->flags |= GNTMAP_readonly;
1065 err = mmu_interval_notifier_insert_locked(
1066 &map->notifier, vma->vm_mm, vma->vm_start,
1067 vma->vm_end - vma->vm_start, &gntdev_mmu_ops);
1070 goto out_unlock_put;
1073 mutex_unlock(&priv->lock);
1077 * gntdev takes the address of the PTE in find_grant_ptes() and
1078 * passes it to the hypervisor in gntdev_map_grant_pages(). The
1079 * purpose of the notifier is to prevent the hypervisor pointer
1080 * to the PTE from going stale.
1082 * Since this vma's mappings can't be touched without the
1083 * mmap_lock, and we are holding it now, there is no need for
1084 * the notifier_range locking pattern.
1086 mmu_interval_read_begin(&map->notifier);
1088 map->pages_vm_start = vma->vm_start;
1089 err = apply_to_page_range(vma->vm_mm, vma->vm_start,
1090 vma->vm_end - vma->vm_start,
1091 find_grant_ptes, map);
1093 pr_warn("find_grant_ptes() failure.\n");
1098 err = gntdev_map_grant_pages(map);
1103 err = vm_map_pages_zero(vma, map->pages, map->count);
1111 mutex_unlock(&priv->lock);
1115 mutex_unlock(&priv->lock);
1118 unmap_grant_pages(map, 0, map->count);
1120 mmu_interval_notifier_remove(&map->notifier);
1124 gntdev_put_map(priv, map);
1128 static const struct file_operations gntdev_fops = {
1129 .owner = THIS_MODULE,
1130 .open = gntdev_open,
1131 .release = gntdev_release,
1132 .mmap = gntdev_mmap,
1133 .unlocked_ioctl = gntdev_ioctl
1136 static struct miscdevice gntdev_miscdev = {
1137 .minor = MISC_DYNAMIC_MINOR,
1138 .name = "xen/gntdev",
1139 .fops = &gntdev_fops,
1142 /* ------------------------------------------------------------------ */
1144 static int __init gntdev_init(void)
1151 use_ptemod = !xen_feature(XENFEAT_auto_translated_physmap);
1153 err = misc_register(&gntdev_miscdev);
1155 pr_err("Could not register gntdev device\n");
1161 static void __exit gntdev_exit(void)
1163 misc_deregister(&gntdev_miscdev);
1166 module_init(gntdev_init);
1167 module_exit(gntdev_exit);
1169 /* ------------------------------------------------------------------ */