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 = kvcalloc(count, sizeof(add->grants[0]), GFP_KERNEL);
140 add->map_ops = kvcalloc(count, sizeof(add->map_ops[0]), GFP_KERNEL);
141 add->unmap_ops = kvcalloc(count, sizeof(add->unmap_ops[0]), GFP_KERNEL);
142 add->kmap_ops = kvcalloc(count, sizeof(add->kmap_ops[0]), GFP_KERNEL);
143 add->kunmap_ops = kvcalloc(count,
144 sizeof(add->kunmap_ops[0]), GFP_KERNEL);
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->kmap_ops ||
152 NULL == add->kunmap_ops ||
153 NULL == add->pages ||
154 NULL == add->being_removed)
157 #ifdef CONFIG_XEN_GRANT_DMA_ALLOC
158 add->dma_flags = dma_flags;
161 * Check if this mapping is requested to be backed
164 if (dma_flags & (GNTDEV_DMA_FLAG_WC | GNTDEV_DMA_FLAG_COHERENT)) {
165 struct gnttab_dma_alloc_args args;
167 add->frames = kvcalloc(count, sizeof(add->frames[0]),
172 /* Remember the device, so we can free DMA memory. */
173 add->dma_dev = priv->dma_dev;
175 args.dev = priv->dma_dev;
176 args.coherent = !!(dma_flags & GNTDEV_DMA_FLAG_COHERENT);
177 args.nr_pages = count;
178 args.pages = add->pages;
179 args.frames = add->frames;
181 if (gnttab_dma_alloc_pages(&args))
184 add->dma_vaddr = args.vaddr;
185 add->dma_bus_addr = args.dev_bus_addr;
188 if (gnttab_alloc_pages(count, add->pages))
191 for (i = 0; i < count; i++) {
192 add->map_ops[i].handle = -1;
193 add->unmap_ops[i].handle = -1;
194 add->kmap_ops[i].handle = -1;
195 add->kunmap_ops[i].handle = -1;
200 refcount_set(&add->users, 1);
205 gntdev_free_map(add);
209 void gntdev_add_map(struct gntdev_priv *priv, struct gntdev_grant_map *add)
211 struct gntdev_grant_map *map;
213 list_for_each_entry(map, &priv->maps, next) {
214 if (add->index + add->count < map->index) {
215 list_add_tail(&add->next, &map->next);
218 add->index = map->index + map->count;
220 list_add_tail(&add->next, &priv->maps);
223 gntdev_print_maps(priv, "[new]", add->index);
226 static struct gntdev_grant_map *gntdev_find_map_index(struct gntdev_priv *priv,
227 int index, int count)
229 struct gntdev_grant_map *map;
231 list_for_each_entry(map, &priv->maps, next) {
232 if (map->index != index)
234 if (count && map->count != count)
241 void gntdev_put_map(struct gntdev_priv *priv, struct gntdev_grant_map *map)
246 if (!refcount_dec_and_test(&map->users))
249 if (map->pages && !use_ptemod) {
251 * Increment the reference count. This ensures that the
252 * subsequent call to unmap_grant_pages() will not wind up
253 * re-entering itself. It *can* wind up calling
254 * gntdev_put_map() recursively, but such calls will be with a
255 * reference count greater than 1, so they will return before
256 * this code is reached. The recursion depth is thus limited to
257 * 1. Do NOT use refcount_inc() here, as it will detect that
258 * the reference count is zero and WARN().
260 refcount_set(&map->users, 1);
263 * Unmap the grants. This may or may not be asynchronous, so it
264 * is possible that the reference count is 1 on return, but it
265 * could also be greater than 1.
267 unmap_grant_pages(map, 0, map->count);
269 /* Check if the memory now needs to be freed */
270 if (!refcount_dec_and_test(&map->users))
274 * All pages have been returned to the hypervisor, so free the
279 if (use_ptemod && map->notifier_init)
280 mmu_interval_notifier_remove(&map->notifier);
282 if (map->notify.flags & UNMAP_NOTIFY_SEND_EVENT) {
283 notify_remote_via_evtchn(map->notify.event);
284 evtchn_put(map->notify.event);
286 gntdev_free_map(map);
289 /* ------------------------------------------------------------------ */
291 static int find_grant_ptes(pte_t *pte, unsigned long addr, void *data)
293 struct gntdev_grant_map *map = data;
294 unsigned int pgnr = (addr - map->pages_vm_start) >> PAGE_SHIFT;
295 int flags = map->flags | GNTMAP_application_map | GNTMAP_contains_pte |
296 (1 << _GNTMAP_guest_avail0);
299 BUG_ON(pgnr >= map->count);
300 pte_maddr = arbitrary_virt_to_machine(pte).maddr;
302 gnttab_set_map_op(&map->map_ops[pgnr], pte_maddr, flags,
303 map->grants[pgnr].ref,
304 map->grants[pgnr].domid);
305 gnttab_set_unmap_op(&map->unmap_ops[pgnr], pte_maddr, flags,
310 int gntdev_map_grant_pages(struct gntdev_grant_map *map)
316 /* Note: it could already be mapped */
317 if (map->map_ops[0].handle != -1)
319 for (i = 0; i < map->count; i++) {
320 unsigned long addr = (unsigned long)
321 pfn_to_kaddr(page_to_pfn(map->pages[i]));
322 gnttab_set_map_op(&map->map_ops[i], addr, map->flags,
324 map->grants[i].domid);
325 gnttab_set_unmap_op(&map->unmap_ops[i], addr,
326 map->flags, -1 /* handle */);
330 * Setup the map_ops corresponding to the pte entries pointing
331 * to the kernel linear addresses of the struct pages.
332 * These ptes are completely different from the user ptes dealt
333 * with find_grant_ptes.
334 * Note that GNTMAP_device_map isn't needed here: The
335 * dev_bus_addr output field gets consumed only from ->map_ops,
336 * and by not requesting it when mapping we also avoid needing
337 * to mirror dev_bus_addr into ->unmap_ops (and holding an extra
338 * reference to the page in the hypervisor).
340 unsigned int flags = (map->flags & ~GNTMAP_device_map) |
343 for (i = 0; i < map->count; i++) {
344 unsigned long address = (unsigned long)
345 pfn_to_kaddr(page_to_pfn(map->pages[i]));
346 BUG_ON(PageHighMem(map->pages[i]));
348 gnttab_set_map_op(&map->kmap_ops[i], address, flags,
350 map->grants[i].domid);
351 gnttab_set_unmap_op(&map->kunmap_ops[i], address,
356 pr_debug("map %d+%d\n", map->index, map->count);
357 err = gnttab_map_refs(map->map_ops, use_ptemod ? map->kmap_ops : NULL,
358 map->pages, map->count);
360 for (i = 0; i < map->count; i++) {
361 if (map->map_ops[i].status == GNTST_okay) {
362 map->unmap_ops[i].handle = map->map_ops[i].handle;
367 if (map->flags & GNTMAP_device_map)
368 map->unmap_ops[i].dev_bus_addr = map->map_ops[i].dev_bus_addr;
371 if (map->kmap_ops[i].status == GNTST_okay) {
373 map->kunmap_ops[i].handle = map->kmap_ops[i].handle;
378 atomic_add(alloced, &map->live_grants);
382 static void __unmap_grant_pages_done(int result,
383 struct gntab_unmap_queue_data *data)
386 struct gntdev_grant_map *map = data->data;
387 unsigned int offset = data->unmap_ops - map->unmap_ops;
388 int successful_unmaps = 0;
391 for (i = 0; i < data->count; i++) {
392 if (map->unmap_ops[offset + i].status == GNTST_okay &&
393 map->unmap_ops[offset + i].handle != -1)
396 WARN_ON(map->unmap_ops[offset+i].status &&
397 map->unmap_ops[offset+i].handle != -1);
398 pr_debug("unmap handle=%d st=%d\n",
399 map->unmap_ops[offset+i].handle,
400 map->unmap_ops[offset+i].status);
401 map->unmap_ops[offset+i].handle = -1;
403 if (map->kunmap_ops[offset + i].status == GNTST_okay &&
404 map->kunmap_ops[offset + i].handle != -1)
407 WARN_ON(map->kunmap_ops[offset+i].status &&
408 map->kunmap_ops[offset+i].handle != -1);
409 pr_debug("kunmap handle=%u st=%d\n",
410 map->kunmap_ops[offset+i].handle,
411 map->kunmap_ops[offset+i].status);
412 map->kunmap_ops[offset+i].handle = -1;
417 * Decrease the live-grant counter. This must happen after the loop to
418 * prevent premature reuse of the grants by gnttab_mmap().
420 live_grants = atomic_sub_return(successful_unmaps, &map->live_grants);
421 if (WARN_ON(live_grants < 0))
422 pr_err("%s: live_grants became negative (%d) after unmapping %d pages!\n",
423 __func__, live_grants, successful_unmaps);
425 /* Release reference taken by __unmap_grant_pages */
426 gntdev_put_map(NULL, map);
429 static void __unmap_grant_pages(struct gntdev_grant_map *map, int offset,
432 if (map->notify.flags & UNMAP_NOTIFY_CLEAR_BYTE) {
433 int pgno = (map->notify.addr >> PAGE_SHIFT);
435 if (pgno >= offset && pgno < offset + pages) {
436 /* No need for kmap, pages are in lowmem */
437 uint8_t *tmp = pfn_to_kaddr(page_to_pfn(map->pages[pgno]));
439 tmp[map->notify.addr & (PAGE_SIZE-1)] = 0;
440 map->notify.flags &= ~UNMAP_NOTIFY_CLEAR_BYTE;
444 map->unmap_data.unmap_ops = map->unmap_ops + offset;
445 map->unmap_data.kunmap_ops = use_ptemod ? map->kunmap_ops + offset : NULL;
446 map->unmap_data.pages = map->pages + offset;
447 map->unmap_data.count = pages;
448 map->unmap_data.done = __unmap_grant_pages_done;
449 map->unmap_data.data = map;
450 refcount_inc(&map->users); /* to keep map alive during async call below */
452 gnttab_unmap_refs_async(&map->unmap_data);
455 static void unmap_grant_pages(struct gntdev_grant_map *map, int offset,
460 if (atomic_read(&map->live_grants) == 0)
461 return; /* Nothing to do */
463 pr_debug("unmap %d+%d [%d+%d]\n", map->index, map->count, offset, pages);
465 /* It is possible the requested range will have a "hole" where we
466 * already unmapped some of the grants. Only unmap valid ranges.
469 while (pages && map->being_removed[offset]) {
474 while (range < pages) {
475 if (map->being_removed[offset + range])
477 map->being_removed[offset + range] = true;
481 __unmap_grant_pages(map, offset, range);
487 /* ------------------------------------------------------------------ */
489 static void gntdev_vma_open(struct vm_area_struct *vma)
491 struct gntdev_grant_map *map = vma->vm_private_data;
493 pr_debug("gntdev_vma_open %p\n", vma);
494 refcount_inc(&map->users);
497 static void gntdev_vma_close(struct vm_area_struct *vma)
499 struct gntdev_grant_map *map = vma->vm_private_data;
500 struct file *file = vma->vm_file;
501 struct gntdev_priv *priv = file->private_data;
503 pr_debug("gntdev_vma_close %p\n", vma);
505 vma->vm_private_data = NULL;
506 gntdev_put_map(priv, map);
509 static struct page *gntdev_vma_find_special_page(struct vm_area_struct *vma,
512 struct gntdev_grant_map *map = vma->vm_private_data;
514 return map->pages[(addr - map->pages_vm_start) >> PAGE_SHIFT];
517 static const struct vm_operations_struct gntdev_vmops = {
518 .open = gntdev_vma_open,
519 .close = gntdev_vma_close,
520 .find_special_page = gntdev_vma_find_special_page,
523 /* ------------------------------------------------------------------ */
525 static bool gntdev_invalidate(struct mmu_interval_notifier *mn,
526 const struct mmu_notifier_range *range,
527 unsigned long cur_seq)
529 struct gntdev_grant_map *map =
530 container_of(mn, struct gntdev_grant_map, notifier);
531 unsigned long mstart, mend;
532 unsigned long map_start, map_end;
534 if (!mmu_notifier_range_blockable(range))
537 map_start = map->pages_vm_start;
538 map_end = map->pages_vm_start + (map->count << PAGE_SHIFT);
541 * If the VMA is split or otherwise changed the notifier is not
542 * updated, but we don't want to process VA's outside the modified
543 * VMA. FIXME: It would be much more understandable to just prevent
544 * modifying the VMA in the first place.
546 if (map_start >= range->end || map_end <= range->start)
549 mstart = max(range->start, map_start);
550 mend = min(range->end, map_end);
551 pr_debug("map %d+%d (%lx %lx), range %lx %lx, mrange %lx %lx\n",
552 map->index, map->count, map_start, map_end,
553 range->start, range->end, mstart, mend);
554 unmap_grant_pages(map, (mstart - map_start) >> PAGE_SHIFT,
555 (mend - mstart) >> PAGE_SHIFT);
560 static const struct mmu_interval_notifier_ops gntdev_mmu_ops = {
561 .invalidate = gntdev_invalidate,
564 /* ------------------------------------------------------------------ */
566 static int gntdev_open(struct inode *inode, struct file *flip)
568 struct gntdev_priv *priv;
570 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
574 INIT_LIST_HEAD(&priv->maps);
575 mutex_init(&priv->lock);
577 #ifdef CONFIG_XEN_GNTDEV_DMABUF
578 priv->dmabuf_priv = gntdev_dmabuf_init(flip);
579 if (IS_ERR(priv->dmabuf_priv)) {
580 int ret = PTR_ERR(priv->dmabuf_priv);
587 flip->private_data = priv;
588 #ifdef CONFIG_XEN_GRANT_DMA_ALLOC
589 priv->dma_dev = gntdev_miscdev.this_device;
590 dma_coerce_mask_and_coherent(priv->dma_dev, DMA_BIT_MASK(64));
592 pr_debug("priv %p\n", priv);
597 static int gntdev_release(struct inode *inode, struct file *flip)
599 struct gntdev_priv *priv = flip->private_data;
600 struct gntdev_grant_map *map;
602 pr_debug("priv %p\n", priv);
604 mutex_lock(&priv->lock);
605 while (!list_empty(&priv->maps)) {
606 map = list_entry(priv->maps.next,
607 struct gntdev_grant_map, next);
608 list_del(&map->next);
609 gntdev_put_map(NULL /* already removed */, map);
611 mutex_unlock(&priv->lock);
613 #ifdef CONFIG_XEN_GNTDEV_DMABUF
614 gntdev_dmabuf_fini(priv->dmabuf_priv);
621 static long gntdev_ioctl_map_grant_ref(struct gntdev_priv *priv,
622 struct ioctl_gntdev_map_grant_ref __user *u)
624 struct ioctl_gntdev_map_grant_ref op;
625 struct gntdev_grant_map *map;
628 if (copy_from_user(&op, u, sizeof(op)) != 0)
630 pr_debug("priv %p, add %d\n", priv, op.count);
631 if (unlikely(gntdev_test_page_count(op.count)))
635 map = gntdev_alloc_map(priv, op.count, 0 /* This is not a dma-buf. */);
639 if (copy_from_user(map->grants, &u->refs,
640 sizeof(map->grants[0]) * op.count) != 0) {
641 gntdev_put_map(NULL, map);
645 mutex_lock(&priv->lock);
646 gntdev_add_map(priv, map);
647 op.index = map->index << PAGE_SHIFT;
648 mutex_unlock(&priv->lock);
650 if (copy_to_user(u, &op, sizeof(op)) != 0)
656 static long gntdev_ioctl_unmap_grant_ref(struct gntdev_priv *priv,
657 struct ioctl_gntdev_unmap_grant_ref __user *u)
659 struct ioctl_gntdev_unmap_grant_ref op;
660 struct gntdev_grant_map *map;
663 if (copy_from_user(&op, u, sizeof(op)) != 0)
665 pr_debug("priv %p, del %d+%d\n", priv, (int)op.index, (int)op.count);
667 mutex_lock(&priv->lock);
668 map = gntdev_find_map_index(priv, op.index >> PAGE_SHIFT, op.count);
670 list_del(&map->next);
673 mutex_unlock(&priv->lock);
675 gntdev_put_map(priv, map);
679 static long gntdev_ioctl_get_offset_for_vaddr(struct gntdev_priv *priv,
680 struct ioctl_gntdev_get_offset_for_vaddr __user *u)
682 struct ioctl_gntdev_get_offset_for_vaddr op;
683 struct vm_area_struct *vma;
684 struct gntdev_grant_map *map;
687 if (copy_from_user(&op, u, sizeof(op)) != 0)
689 pr_debug("priv %p, offset for vaddr %lx\n", priv, (unsigned long)op.vaddr);
691 mmap_read_lock(current->mm);
692 vma = find_vma(current->mm, op.vaddr);
693 if (!vma || vma->vm_ops != &gntdev_vmops)
696 map = vma->vm_private_data;
700 op.offset = map->index << PAGE_SHIFT;
701 op.count = map->count;
705 mmap_read_unlock(current->mm);
707 if (rv == 0 && copy_to_user(u, &op, sizeof(op)) != 0)
712 static long gntdev_ioctl_notify(struct gntdev_priv *priv, void __user *u)
714 struct ioctl_gntdev_unmap_notify op;
715 struct gntdev_grant_map *map;
718 evtchn_port_t out_event;
720 if (copy_from_user(&op, u, sizeof(op)))
723 if (op.action & ~(UNMAP_NOTIFY_CLEAR_BYTE|UNMAP_NOTIFY_SEND_EVENT))
726 /* We need to grab a reference to the event channel we are going to use
727 * to send the notify before releasing the reference we may already have
728 * (if someone has called this ioctl twice). This is required so that
729 * it is possible to change the clear_byte part of the notification
730 * without disturbing the event channel part, which may now be the last
731 * reference to that event channel.
733 if (op.action & UNMAP_NOTIFY_SEND_EVENT) {
734 if (evtchn_get(op.event_channel_port))
738 out_flags = op.action;
739 out_event = op.event_channel_port;
741 mutex_lock(&priv->lock);
743 list_for_each_entry(map, &priv->maps, next) {
744 uint64_t begin = map->index << PAGE_SHIFT;
745 uint64_t end = (map->index + map->count) << PAGE_SHIFT;
746 if (op.index >= begin && op.index < end)
753 if ((op.action & UNMAP_NOTIFY_CLEAR_BYTE) &&
754 (map->flags & GNTMAP_readonly)) {
759 out_flags = map->notify.flags;
760 out_event = map->notify.event;
762 map->notify.flags = op.action;
763 map->notify.addr = op.index - (map->index << PAGE_SHIFT);
764 map->notify.event = op.event_channel_port;
769 mutex_unlock(&priv->lock);
771 /* Drop the reference to the event channel we did not save in the map */
772 if (out_flags & UNMAP_NOTIFY_SEND_EVENT)
773 evtchn_put(out_event);
778 #define GNTDEV_COPY_BATCH 16
780 struct gntdev_copy_batch {
781 struct gnttab_copy ops[GNTDEV_COPY_BATCH];
782 struct page *pages[GNTDEV_COPY_BATCH];
783 s16 __user *status[GNTDEV_COPY_BATCH];
785 unsigned int nr_pages;
789 static int gntdev_get_page(struct gntdev_copy_batch *batch, void __user *virt,
792 unsigned long addr = (unsigned long)virt;
794 unsigned long xen_pfn;
797 ret = pin_user_pages_fast(addr, 1, batch->writeable ? FOLL_WRITE : 0, &page);
801 batch->pages[batch->nr_pages++] = page;
803 xen_pfn = page_to_xen_pfn(page) + XEN_PFN_DOWN(addr & ~PAGE_MASK);
804 *gfn = pfn_to_gfn(xen_pfn);
809 static void gntdev_put_pages(struct gntdev_copy_batch *batch)
811 unpin_user_pages_dirty_lock(batch->pages, batch->nr_pages, batch->writeable);
813 batch->writeable = false;
816 static int gntdev_copy(struct gntdev_copy_batch *batch)
820 gnttab_batch_copy(batch->ops, batch->nr_ops);
821 gntdev_put_pages(batch);
824 * For each completed op, update the status if the op failed
825 * and all previous ops for the segment were successful.
827 for (i = 0; i < batch->nr_ops; i++) {
828 s16 status = batch->ops[i].status;
831 if (status == GNTST_okay)
834 if (__get_user(old_status, batch->status[i]))
837 if (old_status != GNTST_okay)
840 if (__put_user(status, batch->status[i]))
848 static int gntdev_grant_copy_seg(struct gntdev_copy_batch *batch,
849 struct gntdev_grant_copy_segment *seg,
855 * Disallow local -> local copies since there is only space in
856 * batch->pages for one page per-op and this would be a very
857 * expensive memcpy().
859 if (!(seg->flags & (GNTCOPY_source_gref | GNTCOPY_dest_gref)))
862 /* Can't cross page if source/dest is a grant ref. */
863 if (seg->flags & GNTCOPY_source_gref) {
864 if (seg->source.foreign.offset + seg->len > XEN_PAGE_SIZE)
867 if (seg->flags & GNTCOPY_dest_gref) {
868 if (seg->dest.foreign.offset + seg->len > XEN_PAGE_SIZE)
872 if (put_user(GNTST_okay, status))
875 while (copied < seg->len) {
876 struct gnttab_copy *op;
882 if (batch->nr_ops >= GNTDEV_COPY_BATCH) {
883 ret = gntdev_copy(batch);
888 len = seg->len - copied;
890 op = &batch->ops[batch->nr_ops];
893 if (seg->flags & GNTCOPY_source_gref) {
894 op->source.u.ref = seg->source.foreign.ref;
895 op->source.domid = seg->source.foreign.domid;
896 op->source.offset = seg->source.foreign.offset + copied;
897 op->flags |= GNTCOPY_source_gref;
899 virt = seg->source.virt + copied;
900 off = (unsigned long)virt & ~XEN_PAGE_MASK;
901 len = min(len, (size_t)XEN_PAGE_SIZE - off);
902 batch->writeable = false;
904 ret = gntdev_get_page(batch, virt, &gfn);
908 op->source.u.gmfn = gfn;
909 op->source.domid = DOMID_SELF;
910 op->source.offset = off;
913 if (seg->flags & GNTCOPY_dest_gref) {
914 op->dest.u.ref = seg->dest.foreign.ref;
915 op->dest.domid = seg->dest.foreign.domid;
916 op->dest.offset = seg->dest.foreign.offset + copied;
917 op->flags |= GNTCOPY_dest_gref;
919 virt = seg->dest.virt + copied;
920 off = (unsigned long)virt & ~XEN_PAGE_MASK;
921 len = min(len, (size_t)XEN_PAGE_SIZE - off);
922 batch->writeable = true;
924 ret = gntdev_get_page(batch, virt, &gfn);
928 op->dest.u.gmfn = gfn;
929 op->dest.domid = DOMID_SELF;
930 op->dest.offset = off;
936 batch->status[batch->nr_ops] = status;
943 static long gntdev_ioctl_grant_copy(struct gntdev_priv *priv, void __user *u)
945 struct ioctl_gntdev_grant_copy copy;
946 struct gntdev_copy_batch batch;
950 if (copy_from_user(©, u, sizeof(copy)))
956 for (i = 0; i < copy.count; i++) {
957 struct gntdev_grant_copy_segment seg;
959 if (copy_from_user(&seg, ©.segments[i], sizeof(seg))) {
964 ret = gntdev_grant_copy_seg(&batch, &seg, ©.segments[i].status);
971 ret = gntdev_copy(&batch);
975 gntdev_put_pages(&batch);
979 static long gntdev_ioctl(struct file *flip,
980 unsigned int cmd, unsigned long arg)
982 struct gntdev_priv *priv = flip->private_data;
983 void __user *ptr = (void __user *)arg;
986 case IOCTL_GNTDEV_MAP_GRANT_REF:
987 return gntdev_ioctl_map_grant_ref(priv, ptr);
989 case IOCTL_GNTDEV_UNMAP_GRANT_REF:
990 return gntdev_ioctl_unmap_grant_ref(priv, ptr);
992 case IOCTL_GNTDEV_GET_OFFSET_FOR_VADDR:
993 return gntdev_ioctl_get_offset_for_vaddr(priv, ptr);
995 case IOCTL_GNTDEV_SET_UNMAP_NOTIFY:
996 return gntdev_ioctl_notify(priv, ptr);
998 case IOCTL_GNTDEV_GRANT_COPY:
999 return gntdev_ioctl_grant_copy(priv, ptr);
1001 #ifdef CONFIG_XEN_GNTDEV_DMABUF
1002 case IOCTL_GNTDEV_DMABUF_EXP_FROM_REFS:
1003 return gntdev_ioctl_dmabuf_exp_from_refs(priv, use_ptemod, ptr);
1005 case IOCTL_GNTDEV_DMABUF_EXP_WAIT_RELEASED:
1006 return gntdev_ioctl_dmabuf_exp_wait_released(priv, ptr);
1008 case IOCTL_GNTDEV_DMABUF_IMP_TO_REFS:
1009 return gntdev_ioctl_dmabuf_imp_to_refs(priv, ptr);
1011 case IOCTL_GNTDEV_DMABUF_IMP_RELEASE:
1012 return gntdev_ioctl_dmabuf_imp_release(priv, ptr);
1016 pr_debug("priv %p, unknown cmd %x\n", priv, cmd);
1017 return -ENOIOCTLCMD;
1023 static int gntdev_mmap(struct file *flip, struct vm_area_struct *vma)
1025 struct gntdev_priv *priv = flip->private_data;
1026 int index = vma->vm_pgoff;
1027 int count = vma_pages(vma);
1028 struct gntdev_grant_map *map;
1031 if ((vma->vm_flags & VM_WRITE) && !(vma->vm_flags & VM_SHARED))
1034 pr_debug("map %d+%d at %lx (pgoff %lx)\n",
1035 index, count, vma->vm_start, vma->vm_pgoff);
1037 mutex_lock(&priv->lock);
1038 map = gntdev_find_map_index(priv, index, count);
1041 if (!atomic_add_unless(&map->in_use, 1, 1))
1044 refcount_inc(&map->users);
1046 vma->vm_ops = &gntdev_vmops;
1048 vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP | VM_MIXEDMAP;
1051 vma->vm_flags |= VM_DONTCOPY;
1053 vma->vm_private_data = map;
1055 if ((vma->vm_flags & VM_WRITE) &&
1056 (map->flags & GNTMAP_readonly))
1057 goto out_unlock_put;
1059 map->flags = GNTMAP_host_map;
1060 if (!(vma->vm_flags & VM_WRITE))
1061 map->flags |= GNTMAP_readonly;
1064 map->pages_vm_start = vma->vm_start;
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);
1071 goto out_unlock_put;
1073 map->notifier_init = true;
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 err = apply_to_page_range(vma->vm_mm, vma->vm_start,
1091 vma->vm_end - vma->vm_start,
1092 find_grant_ptes, map);
1094 pr_warn("find_grant_ptes() failure.\n");
1099 err = gntdev_map_grant_pages(map);
1104 err = vm_map_pages_zero(vma, map->pages, map->count);
1112 mutex_unlock(&priv->lock);
1116 mutex_unlock(&priv->lock);
1119 unmap_grant_pages(map, 0, map->count);
1120 gntdev_put_map(priv, map);
1124 static const struct file_operations gntdev_fops = {
1125 .owner = THIS_MODULE,
1126 .open = gntdev_open,
1127 .release = gntdev_release,
1128 .mmap = gntdev_mmap,
1129 .unlocked_ioctl = gntdev_ioctl
1132 static struct miscdevice gntdev_miscdev = {
1133 .minor = MISC_DYNAMIC_MINOR,
1134 .name = "xen/gntdev",
1135 .fops = &gntdev_fops,
1138 /* ------------------------------------------------------------------ */
1140 static int __init gntdev_init(void)
1147 use_ptemod = !xen_feature(XENFEAT_auto_translated_physmap);
1149 err = misc_register(&gntdev_miscdev);
1151 pr_err("Could not register gntdev device\n");
1157 static void __exit gntdev_exit(void)
1159 misc_deregister(&gntdev_miscdev);
1162 module_init(gntdev_init);
1163 module_exit(gntdev_exit);
1165 /* ------------------------------------------------------------------ */