spi: sprd: adi: Add a reset reason for watchdog mode
[linux/fpc-iii.git] / drivers / xen / gntdev.c
blob4c339c7e66e50d79312e00eee4c82492be5accab
1 /******************************************************************************
2 * gntdev.c
4 * Device for accessing (in user-space) pages that have been granted by other
5 * domains.
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
21 #undef DEBUG
23 #define pr_fmt(fmt) "xen:" KBUILD_MODNAME ": " fmt
25 #include <linux/module.h>
26 #include <linux/kernel.h>
27 #include <linux/init.h>
28 #include <linux/miscdevice.h>
29 #include <linux/fs.h>
30 #include <linux/uaccess.h>
31 #include <linux/sched.h>
32 #include <linux/sched/mm.h>
33 #include <linux/spinlock.h>
34 #include <linux/slab.h>
35 #include <linux/highmem.h>
36 #include <linux/refcount.h>
37 #ifdef CONFIG_XEN_GRANT_DMA_ALLOC
38 #include <linux/of_device.h>
39 #endif
41 #include <xen/xen.h>
42 #include <xen/grant_table.h>
43 #include <xen/balloon.h>
44 #include <xen/gntdev.h>
45 #include <xen/events.h>
46 #include <xen/page.h>
47 #include <asm/xen/hypervisor.h>
48 #include <asm/xen/hypercall.h>
50 #include "gntdev-common.h"
51 #ifdef CONFIG_XEN_GNTDEV_DMABUF
52 #include "gntdev-dmabuf.h"
53 #endif
55 MODULE_LICENSE("GPL");
56 MODULE_AUTHOR("Derek G. Murray <Derek.Murray@cl.cam.ac.uk>, "
57 "Gerd Hoffmann <kraxel@redhat.com>");
58 MODULE_DESCRIPTION("User-space granted page access driver");
60 static int limit = 1024*1024;
61 module_param(limit, int, 0644);
62 MODULE_PARM_DESC(limit, "Maximum number of grants that may be mapped by "
63 "the gntdev device");
65 static atomic_t pages_mapped = ATOMIC_INIT(0);
67 static int use_ptemod;
68 #define populate_freeable_maps use_ptemod
70 static int unmap_grant_pages(struct gntdev_grant_map *map,
71 int offset, int pages);
73 static struct miscdevice gntdev_miscdev;
75 /* ------------------------------------------------------------------ */
77 bool gntdev_account_mapped_pages(int count)
79 return atomic_add_return(count, &pages_mapped) > limit;
82 static void gntdev_print_maps(struct gntdev_priv *priv,
83 char *text, int text_index)
85 #ifdef DEBUG
86 struct gntdev_grant_map *map;
88 pr_debug("%s: maps list (priv %p)\n", __func__, priv);
89 list_for_each_entry(map, &priv->maps, next)
90 pr_debug(" index %2d, count %2d %s\n",
91 map->index, map->count,
92 map->index == text_index && text ? text : "");
93 #endif
96 static void gntdev_free_map(struct gntdev_grant_map *map)
98 if (map == NULL)
99 return;
101 #ifdef CONFIG_XEN_GRANT_DMA_ALLOC
102 if (map->dma_vaddr) {
103 struct gnttab_dma_alloc_args args;
105 args.dev = map->dma_dev;
106 args.coherent = !!(map->dma_flags & GNTDEV_DMA_FLAG_COHERENT);
107 args.nr_pages = map->count;
108 args.pages = map->pages;
109 args.frames = map->frames;
110 args.vaddr = map->dma_vaddr;
111 args.dev_bus_addr = map->dma_bus_addr;
113 gnttab_dma_free_pages(&args);
114 } else
115 #endif
116 if (map->pages)
117 gnttab_free_pages(map->count, map->pages);
119 #ifdef CONFIG_XEN_GRANT_DMA_ALLOC
120 kfree(map->frames);
121 #endif
122 kfree(map->pages);
123 kfree(map->grants);
124 kfree(map->map_ops);
125 kfree(map->unmap_ops);
126 kfree(map->kmap_ops);
127 kfree(map->kunmap_ops);
128 kfree(map);
131 struct gntdev_grant_map *gntdev_alloc_map(struct gntdev_priv *priv, int count,
132 int dma_flags)
134 struct gntdev_grant_map *add;
135 int i;
137 add = kzalloc(sizeof(*add), GFP_KERNEL);
138 if (NULL == add)
139 return NULL;
141 add->grants = kcalloc(count, sizeof(add->grants[0]), GFP_KERNEL);
142 add->map_ops = kcalloc(count, sizeof(add->map_ops[0]), GFP_KERNEL);
143 add->unmap_ops = kcalloc(count, sizeof(add->unmap_ops[0]), GFP_KERNEL);
144 add->kmap_ops = kcalloc(count, sizeof(add->kmap_ops[0]), GFP_KERNEL);
145 add->kunmap_ops = kcalloc(count, sizeof(add->kunmap_ops[0]), GFP_KERNEL);
146 add->pages = kcalloc(count, sizeof(add->pages[0]), GFP_KERNEL);
147 if (NULL == add->grants ||
148 NULL == add->map_ops ||
149 NULL == add->unmap_ops ||
150 NULL == add->kmap_ops ||
151 NULL == add->kunmap_ops ||
152 NULL == add->pages)
153 goto err;
155 #ifdef CONFIG_XEN_GRANT_DMA_ALLOC
156 add->dma_flags = dma_flags;
159 * Check if this mapping is requested to be backed
160 * by a DMA buffer.
162 if (dma_flags & (GNTDEV_DMA_FLAG_WC | GNTDEV_DMA_FLAG_COHERENT)) {
163 struct gnttab_dma_alloc_args args;
165 add->frames = kcalloc(count, sizeof(add->frames[0]),
166 GFP_KERNEL);
167 if (!add->frames)
168 goto err;
170 /* Remember the device, so we can free DMA memory. */
171 add->dma_dev = priv->dma_dev;
173 args.dev = priv->dma_dev;
174 args.coherent = !!(dma_flags & GNTDEV_DMA_FLAG_COHERENT);
175 args.nr_pages = count;
176 args.pages = add->pages;
177 args.frames = add->frames;
179 if (gnttab_dma_alloc_pages(&args))
180 goto err;
182 add->dma_vaddr = args.vaddr;
183 add->dma_bus_addr = args.dev_bus_addr;
184 } else
185 #endif
186 if (gnttab_alloc_pages(count, add->pages))
187 goto err;
189 for (i = 0; i < count; i++) {
190 add->map_ops[i].handle = -1;
191 add->unmap_ops[i].handle = -1;
192 add->kmap_ops[i].handle = -1;
193 add->kunmap_ops[i].handle = -1;
196 add->index = 0;
197 add->count = count;
198 refcount_set(&add->users, 1);
200 return add;
202 err:
203 gntdev_free_map(add);
204 return NULL;
207 void gntdev_add_map(struct gntdev_priv *priv, struct gntdev_grant_map *add)
209 struct gntdev_grant_map *map;
211 list_for_each_entry(map, &priv->maps, next) {
212 if (add->index + add->count < map->index) {
213 list_add_tail(&add->next, &map->next);
214 goto done;
216 add->index = map->index + map->count;
218 list_add_tail(&add->next, &priv->maps);
220 done:
221 gntdev_print_maps(priv, "[new]", add->index);
224 static struct gntdev_grant_map *gntdev_find_map_index(struct gntdev_priv *priv,
225 int index, int count)
227 struct gntdev_grant_map *map;
229 list_for_each_entry(map, &priv->maps, next) {
230 if (map->index != index)
231 continue;
232 if (count && map->count != count)
233 continue;
234 return map;
236 return NULL;
239 void gntdev_put_map(struct gntdev_priv *priv, struct gntdev_grant_map *map)
241 if (!map)
242 return;
244 if (!refcount_dec_and_test(&map->users))
245 return;
247 atomic_sub(map->count, &pages_mapped);
249 if (map->notify.flags & UNMAP_NOTIFY_SEND_EVENT) {
250 notify_remote_via_evtchn(map->notify.event);
251 evtchn_put(map->notify.event);
254 if (populate_freeable_maps && priv) {
255 mutex_lock(&priv->lock);
256 list_del(&map->next);
257 mutex_unlock(&priv->lock);
260 if (map->pages && !use_ptemod)
261 unmap_grant_pages(map, 0, map->count);
262 gntdev_free_map(map);
265 /* ------------------------------------------------------------------ */
267 static int find_grant_ptes(pte_t *pte, unsigned long addr, void *data)
269 struct gntdev_grant_map *map = data;
270 unsigned int pgnr = (addr - map->vma->vm_start) >> PAGE_SHIFT;
271 int flags = map->flags | GNTMAP_application_map | GNTMAP_contains_pte;
272 u64 pte_maddr;
274 BUG_ON(pgnr >= map->count);
275 pte_maddr = arbitrary_virt_to_machine(pte).maddr;
278 * Set the PTE as special to force get_user_pages_fast() fall
279 * back to the slow path. If this is not supported as part of
280 * the grant map, it will be done afterwards.
282 if (xen_feature(XENFEAT_gnttab_map_avail_bits))
283 flags |= (1 << _GNTMAP_guest_avail0);
285 gnttab_set_map_op(&map->map_ops[pgnr], pte_maddr, flags,
286 map->grants[pgnr].ref,
287 map->grants[pgnr].domid);
288 gnttab_set_unmap_op(&map->unmap_ops[pgnr], pte_maddr, flags,
289 -1 /* handle */);
290 return 0;
293 #ifdef CONFIG_X86
294 static int set_grant_ptes_as_special(pte_t *pte, unsigned long addr, void *data)
296 set_pte_at(current->mm, addr, pte, pte_mkspecial(*pte));
297 return 0;
299 #endif
301 int gntdev_map_grant_pages(struct gntdev_grant_map *map)
303 int i, err = 0;
305 if (!use_ptemod) {
306 /* Note: it could already be mapped */
307 if (map->map_ops[0].handle != -1)
308 return 0;
309 for (i = 0; i < map->count; i++) {
310 unsigned long addr = (unsigned long)
311 pfn_to_kaddr(page_to_pfn(map->pages[i]));
312 gnttab_set_map_op(&map->map_ops[i], addr, map->flags,
313 map->grants[i].ref,
314 map->grants[i].domid);
315 gnttab_set_unmap_op(&map->unmap_ops[i], addr,
316 map->flags, -1 /* handle */);
318 } else {
320 * Setup the map_ops corresponding to the pte entries pointing
321 * to the kernel linear addresses of the struct pages.
322 * These ptes are completely different from the user ptes dealt
323 * with find_grant_ptes.
325 for (i = 0; i < map->count; i++) {
326 unsigned long address = (unsigned long)
327 pfn_to_kaddr(page_to_pfn(map->pages[i]));
328 BUG_ON(PageHighMem(map->pages[i]));
330 gnttab_set_map_op(&map->kmap_ops[i], address,
331 map->flags | GNTMAP_host_map,
332 map->grants[i].ref,
333 map->grants[i].domid);
334 gnttab_set_unmap_op(&map->kunmap_ops[i], address,
335 map->flags | GNTMAP_host_map, -1);
339 pr_debug("map %d+%d\n", map->index, map->count);
340 err = gnttab_map_refs(map->map_ops, use_ptemod ? map->kmap_ops : NULL,
341 map->pages, map->count);
342 if (err)
343 return err;
345 for (i = 0; i < map->count; i++) {
346 if (map->map_ops[i].status) {
347 err = -EINVAL;
348 continue;
351 map->unmap_ops[i].handle = map->map_ops[i].handle;
352 if (use_ptemod)
353 map->kunmap_ops[i].handle = map->kmap_ops[i].handle;
354 #ifdef CONFIG_XEN_GRANT_DMA_ALLOC
355 else if (map->dma_vaddr) {
356 unsigned long bfn;
358 bfn = pfn_to_bfn(page_to_pfn(map->pages[i]));
359 map->unmap_ops[i].dev_bus_addr = __pfn_to_phys(bfn);
361 #endif
363 return err;
366 static int __unmap_grant_pages(struct gntdev_grant_map *map, int offset,
367 int pages)
369 int i, err = 0;
370 struct gntab_unmap_queue_data unmap_data;
372 if (map->notify.flags & UNMAP_NOTIFY_CLEAR_BYTE) {
373 int pgno = (map->notify.addr >> PAGE_SHIFT);
374 if (pgno >= offset && pgno < offset + pages) {
375 /* No need for kmap, pages are in lowmem */
376 uint8_t *tmp = pfn_to_kaddr(page_to_pfn(map->pages[pgno]));
377 tmp[map->notify.addr & (PAGE_SIZE-1)] = 0;
378 map->notify.flags &= ~UNMAP_NOTIFY_CLEAR_BYTE;
382 unmap_data.unmap_ops = map->unmap_ops + offset;
383 unmap_data.kunmap_ops = use_ptemod ? map->kunmap_ops + offset : NULL;
384 unmap_data.pages = map->pages + offset;
385 unmap_data.count = pages;
387 err = gnttab_unmap_refs_sync(&unmap_data);
388 if (err)
389 return err;
391 for (i = 0; i < pages; i++) {
392 if (map->unmap_ops[offset+i].status)
393 err = -EINVAL;
394 pr_debug("unmap handle=%d st=%d\n",
395 map->unmap_ops[offset+i].handle,
396 map->unmap_ops[offset+i].status);
397 map->unmap_ops[offset+i].handle = -1;
399 return err;
402 static int unmap_grant_pages(struct gntdev_grant_map *map, int offset,
403 int pages)
405 int range, err = 0;
407 pr_debug("unmap %d+%d [%d+%d]\n", map->index, map->count, offset, pages);
409 /* It is possible the requested range will have a "hole" where we
410 * already unmapped some of the grants. Only unmap valid ranges.
412 while (pages && !err) {
413 while (pages && map->unmap_ops[offset].handle == -1) {
414 offset++;
415 pages--;
417 range = 0;
418 while (range < pages) {
419 if (map->unmap_ops[offset+range].handle == -1)
420 break;
421 range++;
423 err = __unmap_grant_pages(map, offset, range);
424 offset += range;
425 pages -= range;
428 return err;
431 /* ------------------------------------------------------------------ */
433 static void gntdev_vma_open(struct vm_area_struct *vma)
435 struct gntdev_grant_map *map = vma->vm_private_data;
437 pr_debug("gntdev_vma_open %p\n", vma);
438 refcount_inc(&map->users);
441 static void gntdev_vma_close(struct vm_area_struct *vma)
443 struct gntdev_grant_map *map = vma->vm_private_data;
444 struct file *file = vma->vm_file;
445 struct gntdev_priv *priv = file->private_data;
447 pr_debug("gntdev_vma_close %p\n", vma);
448 if (use_ptemod) {
449 /* It is possible that an mmu notifier could be running
450 * concurrently, so take priv->lock to ensure that the vma won't
451 * vanishing during the unmap_grant_pages call, since we will
452 * spin here until that completes. Such a concurrent call will
453 * not do any unmapping, since that has been done prior to
454 * closing the vma, but it may still iterate the unmap_ops list.
456 mutex_lock(&priv->lock);
457 map->vma = NULL;
458 mutex_unlock(&priv->lock);
460 vma->vm_private_data = NULL;
461 gntdev_put_map(priv, map);
464 static struct page *gntdev_vma_find_special_page(struct vm_area_struct *vma,
465 unsigned long addr)
467 struct gntdev_grant_map *map = vma->vm_private_data;
469 return map->pages[(addr - map->pages_vm_start) >> PAGE_SHIFT];
472 static const struct vm_operations_struct gntdev_vmops = {
473 .open = gntdev_vma_open,
474 .close = gntdev_vma_close,
475 .find_special_page = gntdev_vma_find_special_page,
478 /* ------------------------------------------------------------------ */
480 static bool in_range(struct gntdev_grant_map *map,
481 unsigned long start, unsigned long end)
483 if (!map->vma)
484 return false;
485 if (map->vma->vm_start >= end)
486 return false;
487 if (map->vma->vm_end <= start)
488 return false;
490 return true;
493 static int unmap_if_in_range(struct gntdev_grant_map *map,
494 unsigned long start, unsigned long end,
495 bool blockable)
497 unsigned long mstart, mend;
498 int err;
500 if (!in_range(map, start, end))
501 return 0;
503 if (!blockable)
504 return -EAGAIN;
506 mstart = max(start, map->vma->vm_start);
507 mend = min(end, map->vma->vm_end);
508 pr_debug("map %d+%d (%lx %lx), range %lx %lx, mrange %lx %lx\n",
509 map->index, map->count,
510 map->vma->vm_start, map->vma->vm_end,
511 start, end, mstart, mend);
512 err = unmap_grant_pages(map,
513 (mstart - map->vma->vm_start) >> PAGE_SHIFT,
514 (mend - mstart) >> PAGE_SHIFT);
515 WARN_ON(err);
517 return 0;
520 static int mn_invl_range_start(struct mmu_notifier *mn,
521 const struct mmu_notifier_range *range)
523 struct gntdev_priv *priv = container_of(mn, struct gntdev_priv, mn);
524 struct gntdev_grant_map *map;
525 int ret = 0;
527 if (mmu_notifier_range_blockable(range))
528 mutex_lock(&priv->lock);
529 else if (!mutex_trylock(&priv->lock))
530 return -EAGAIN;
532 list_for_each_entry(map, &priv->maps, next) {
533 ret = unmap_if_in_range(map, range->start, range->end,
534 mmu_notifier_range_blockable(range));
535 if (ret)
536 goto out_unlock;
538 list_for_each_entry(map, &priv->freeable_maps, next) {
539 ret = unmap_if_in_range(map, range->start, range->end,
540 mmu_notifier_range_blockable(range));
541 if (ret)
542 goto out_unlock;
545 out_unlock:
546 mutex_unlock(&priv->lock);
548 return ret;
551 static void mn_release(struct mmu_notifier *mn,
552 struct mm_struct *mm)
554 struct gntdev_priv *priv = container_of(mn, struct gntdev_priv, mn);
555 struct gntdev_grant_map *map;
556 int err;
558 mutex_lock(&priv->lock);
559 list_for_each_entry(map, &priv->maps, next) {
560 if (!map->vma)
561 continue;
562 pr_debug("map %d+%d (%lx %lx)\n",
563 map->index, map->count,
564 map->vma->vm_start, map->vma->vm_end);
565 err = unmap_grant_pages(map, /* offset */ 0, map->count);
566 WARN_ON(err);
568 list_for_each_entry(map, &priv->freeable_maps, next) {
569 if (!map->vma)
570 continue;
571 pr_debug("map %d+%d (%lx %lx)\n",
572 map->index, map->count,
573 map->vma->vm_start, map->vma->vm_end);
574 err = unmap_grant_pages(map, /* offset */ 0, map->count);
575 WARN_ON(err);
577 mutex_unlock(&priv->lock);
580 static const struct mmu_notifier_ops gntdev_mmu_ops = {
581 .release = mn_release,
582 .invalidate_range_start = mn_invl_range_start,
585 /* ------------------------------------------------------------------ */
587 static int gntdev_open(struct inode *inode, struct file *flip)
589 struct gntdev_priv *priv;
590 int ret = 0;
592 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
593 if (!priv)
594 return -ENOMEM;
596 INIT_LIST_HEAD(&priv->maps);
597 INIT_LIST_HEAD(&priv->freeable_maps);
598 mutex_init(&priv->lock);
600 #ifdef CONFIG_XEN_GNTDEV_DMABUF
601 priv->dmabuf_priv = gntdev_dmabuf_init(flip);
602 if (IS_ERR(priv->dmabuf_priv)) {
603 ret = PTR_ERR(priv->dmabuf_priv);
604 kfree(priv);
605 return ret;
607 #endif
609 if (use_ptemod) {
610 priv->mm = get_task_mm(current);
611 if (!priv->mm) {
612 kfree(priv);
613 return -ENOMEM;
615 priv->mn.ops = &gntdev_mmu_ops;
616 ret = mmu_notifier_register(&priv->mn, priv->mm);
617 mmput(priv->mm);
620 if (ret) {
621 kfree(priv);
622 return ret;
625 flip->private_data = priv;
626 #ifdef CONFIG_XEN_GRANT_DMA_ALLOC
627 priv->dma_dev = gntdev_miscdev.this_device;
630 * The device is not spawn from a device tree, so arch_setup_dma_ops
631 * is not called, thus leaving the device with dummy DMA ops.
632 * Fix this by calling of_dma_configure() with a NULL node to set
633 * default DMA ops.
635 of_dma_configure(priv->dma_dev, NULL, true);
636 #endif
637 pr_debug("priv %p\n", priv);
639 return 0;
642 static int gntdev_release(struct inode *inode, struct file *flip)
644 struct gntdev_priv *priv = flip->private_data;
645 struct gntdev_grant_map *map;
647 pr_debug("priv %p\n", priv);
649 mutex_lock(&priv->lock);
650 while (!list_empty(&priv->maps)) {
651 map = list_entry(priv->maps.next,
652 struct gntdev_grant_map, next);
653 list_del(&map->next);
654 gntdev_put_map(NULL /* already removed */, map);
656 WARN_ON(!list_empty(&priv->freeable_maps));
657 mutex_unlock(&priv->lock);
659 #ifdef CONFIG_XEN_GNTDEV_DMABUF
660 gntdev_dmabuf_fini(priv->dmabuf_priv);
661 #endif
663 if (use_ptemod)
664 mmu_notifier_unregister(&priv->mn, priv->mm);
666 kfree(priv);
667 return 0;
670 static long gntdev_ioctl_map_grant_ref(struct gntdev_priv *priv,
671 struct ioctl_gntdev_map_grant_ref __user *u)
673 struct ioctl_gntdev_map_grant_ref op;
674 struct gntdev_grant_map *map;
675 int err;
677 if (copy_from_user(&op, u, sizeof(op)) != 0)
678 return -EFAULT;
679 pr_debug("priv %p, add %d\n", priv, op.count);
680 if (unlikely(op.count <= 0))
681 return -EINVAL;
683 err = -ENOMEM;
684 map = gntdev_alloc_map(priv, op.count, 0 /* This is not a dma-buf. */);
685 if (!map)
686 return err;
688 if (unlikely(gntdev_account_mapped_pages(op.count))) {
689 pr_debug("can't map: over limit\n");
690 gntdev_put_map(NULL, map);
691 return err;
694 if (copy_from_user(map->grants, &u->refs,
695 sizeof(map->grants[0]) * op.count) != 0) {
696 gntdev_put_map(NULL, map);
697 return -EFAULT;
700 mutex_lock(&priv->lock);
701 gntdev_add_map(priv, map);
702 op.index = map->index << PAGE_SHIFT;
703 mutex_unlock(&priv->lock);
705 if (copy_to_user(u, &op, sizeof(op)) != 0)
706 return -EFAULT;
708 return 0;
711 static long gntdev_ioctl_unmap_grant_ref(struct gntdev_priv *priv,
712 struct ioctl_gntdev_unmap_grant_ref __user *u)
714 struct ioctl_gntdev_unmap_grant_ref op;
715 struct gntdev_grant_map *map;
716 int err = -ENOENT;
718 if (copy_from_user(&op, u, sizeof(op)) != 0)
719 return -EFAULT;
720 pr_debug("priv %p, del %d+%d\n", priv, (int)op.index, (int)op.count);
722 mutex_lock(&priv->lock);
723 map = gntdev_find_map_index(priv, op.index >> PAGE_SHIFT, op.count);
724 if (map) {
725 list_del(&map->next);
726 if (populate_freeable_maps)
727 list_add_tail(&map->next, &priv->freeable_maps);
728 err = 0;
730 mutex_unlock(&priv->lock);
731 if (map)
732 gntdev_put_map(priv, map);
733 return err;
736 static long gntdev_ioctl_get_offset_for_vaddr(struct gntdev_priv *priv,
737 struct ioctl_gntdev_get_offset_for_vaddr __user *u)
739 struct ioctl_gntdev_get_offset_for_vaddr op;
740 struct vm_area_struct *vma;
741 struct gntdev_grant_map *map;
742 int rv = -EINVAL;
744 if (copy_from_user(&op, u, sizeof(op)) != 0)
745 return -EFAULT;
746 pr_debug("priv %p, offset for vaddr %lx\n", priv, (unsigned long)op.vaddr);
748 down_read(&current->mm->mmap_sem);
749 vma = find_vma(current->mm, op.vaddr);
750 if (!vma || vma->vm_ops != &gntdev_vmops)
751 goto out_unlock;
753 map = vma->vm_private_data;
754 if (!map)
755 goto out_unlock;
757 op.offset = map->index << PAGE_SHIFT;
758 op.count = map->count;
759 rv = 0;
761 out_unlock:
762 up_read(&current->mm->mmap_sem);
764 if (rv == 0 && copy_to_user(u, &op, sizeof(op)) != 0)
765 return -EFAULT;
766 return rv;
769 static long gntdev_ioctl_notify(struct gntdev_priv *priv, void __user *u)
771 struct ioctl_gntdev_unmap_notify op;
772 struct gntdev_grant_map *map;
773 int rc;
774 int out_flags;
775 unsigned int out_event;
777 if (copy_from_user(&op, u, sizeof(op)))
778 return -EFAULT;
780 if (op.action & ~(UNMAP_NOTIFY_CLEAR_BYTE|UNMAP_NOTIFY_SEND_EVENT))
781 return -EINVAL;
783 /* We need to grab a reference to the event channel we are going to use
784 * to send the notify before releasing the reference we may already have
785 * (if someone has called this ioctl twice). This is required so that
786 * it is possible to change the clear_byte part of the notification
787 * without disturbing the event channel part, which may now be the last
788 * reference to that event channel.
790 if (op.action & UNMAP_NOTIFY_SEND_EVENT) {
791 if (evtchn_get(op.event_channel_port))
792 return -EINVAL;
795 out_flags = op.action;
796 out_event = op.event_channel_port;
798 mutex_lock(&priv->lock);
800 list_for_each_entry(map, &priv->maps, next) {
801 uint64_t begin = map->index << PAGE_SHIFT;
802 uint64_t end = (map->index + map->count) << PAGE_SHIFT;
803 if (op.index >= begin && op.index < end)
804 goto found;
806 rc = -ENOENT;
807 goto unlock_out;
809 found:
810 if ((op.action & UNMAP_NOTIFY_CLEAR_BYTE) &&
811 (map->flags & GNTMAP_readonly)) {
812 rc = -EINVAL;
813 goto unlock_out;
816 out_flags = map->notify.flags;
817 out_event = map->notify.event;
819 map->notify.flags = op.action;
820 map->notify.addr = op.index - (map->index << PAGE_SHIFT);
821 map->notify.event = op.event_channel_port;
823 rc = 0;
825 unlock_out:
826 mutex_unlock(&priv->lock);
828 /* Drop the reference to the event channel we did not save in the map */
829 if (out_flags & UNMAP_NOTIFY_SEND_EVENT)
830 evtchn_put(out_event);
832 return rc;
835 #define GNTDEV_COPY_BATCH 16
837 struct gntdev_copy_batch {
838 struct gnttab_copy ops[GNTDEV_COPY_BATCH];
839 struct page *pages[GNTDEV_COPY_BATCH];
840 s16 __user *status[GNTDEV_COPY_BATCH];
841 unsigned int nr_ops;
842 unsigned int nr_pages;
845 static int gntdev_get_page(struct gntdev_copy_batch *batch, void __user *virt,
846 bool writeable, unsigned long *gfn)
848 unsigned long addr = (unsigned long)virt;
849 struct page *page;
850 unsigned long xen_pfn;
851 int ret;
853 ret = get_user_pages_fast(addr, 1, writeable ? FOLL_WRITE : 0, &page);
854 if (ret < 0)
855 return ret;
857 batch->pages[batch->nr_pages++] = page;
859 xen_pfn = page_to_xen_pfn(page) + XEN_PFN_DOWN(addr & ~PAGE_MASK);
860 *gfn = pfn_to_gfn(xen_pfn);
862 return 0;
865 static void gntdev_put_pages(struct gntdev_copy_batch *batch)
867 unsigned int i;
869 for (i = 0; i < batch->nr_pages; i++)
870 put_page(batch->pages[i]);
871 batch->nr_pages = 0;
874 static int gntdev_copy(struct gntdev_copy_batch *batch)
876 unsigned int i;
878 gnttab_batch_copy(batch->ops, batch->nr_ops);
879 gntdev_put_pages(batch);
882 * For each completed op, update the status if the op failed
883 * and all previous ops for the segment were successful.
885 for (i = 0; i < batch->nr_ops; i++) {
886 s16 status = batch->ops[i].status;
887 s16 old_status;
889 if (status == GNTST_okay)
890 continue;
892 if (__get_user(old_status, batch->status[i]))
893 return -EFAULT;
895 if (old_status != GNTST_okay)
896 continue;
898 if (__put_user(status, batch->status[i]))
899 return -EFAULT;
902 batch->nr_ops = 0;
903 return 0;
906 static int gntdev_grant_copy_seg(struct gntdev_copy_batch *batch,
907 struct gntdev_grant_copy_segment *seg,
908 s16 __user *status)
910 uint16_t copied = 0;
913 * Disallow local -> local copies since there is only space in
914 * batch->pages for one page per-op and this would be a very
915 * expensive memcpy().
917 if (!(seg->flags & (GNTCOPY_source_gref | GNTCOPY_dest_gref)))
918 return -EINVAL;
920 /* Can't cross page if source/dest is a grant ref. */
921 if (seg->flags & GNTCOPY_source_gref) {
922 if (seg->source.foreign.offset + seg->len > XEN_PAGE_SIZE)
923 return -EINVAL;
925 if (seg->flags & GNTCOPY_dest_gref) {
926 if (seg->dest.foreign.offset + seg->len > XEN_PAGE_SIZE)
927 return -EINVAL;
930 if (put_user(GNTST_okay, status))
931 return -EFAULT;
933 while (copied < seg->len) {
934 struct gnttab_copy *op;
935 void __user *virt;
936 size_t len, off;
937 unsigned long gfn;
938 int ret;
940 if (batch->nr_ops >= GNTDEV_COPY_BATCH) {
941 ret = gntdev_copy(batch);
942 if (ret < 0)
943 return ret;
946 len = seg->len - copied;
948 op = &batch->ops[batch->nr_ops];
949 op->flags = 0;
951 if (seg->flags & GNTCOPY_source_gref) {
952 op->source.u.ref = seg->source.foreign.ref;
953 op->source.domid = seg->source.foreign.domid;
954 op->source.offset = seg->source.foreign.offset + copied;
955 op->flags |= GNTCOPY_source_gref;
956 } else {
957 virt = seg->source.virt + copied;
958 off = (unsigned long)virt & ~XEN_PAGE_MASK;
959 len = min(len, (size_t)XEN_PAGE_SIZE - off);
961 ret = gntdev_get_page(batch, virt, false, &gfn);
962 if (ret < 0)
963 return ret;
965 op->source.u.gmfn = gfn;
966 op->source.domid = DOMID_SELF;
967 op->source.offset = off;
970 if (seg->flags & GNTCOPY_dest_gref) {
971 op->dest.u.ref = seg->dest.foreign.ref;
972 op->dest.domid = seg->dest.foreign.domid;
973 op->dest.offset = seg->dest.foreign.offset + copied;
974 op->flags |= GNTCOPY_dest_gref;
975 } else {
976 virt = seg->dest.virt + copied;
977 off = (unsigned long)virt & ~XEN_PAGE_MASK;
978 len = min(len, (size_t)XEN_PAGE_SIZE - off);
980 ret = gntdev_get_page(batch, virt, true, &gfn);
981 if (ret < 0)
982 return ret;
984 op->dest.u.gmfn = gfn;
985 op->dest.domid = DOMID_SELF;
986 op->dest.offset = off;
989 op->len = len;
990 copied += len;
992 batch->status[batch->nr_ops] = status;
993 batch->nr_ops++;
996 return 0;
999 static long gntdev_ioctl_grant_copy(struct gntdev_priv *priv, void __user *u)
1001 struct ioctl_gntdev_grant_copy copy;
1002 struct gntdev_copy_batch batch;
1003 unsigned int i;
1004 int ret = 0;
1006 if (copy_from_user(&copy, u, sizeof(copy)))
1007 return -EFAULT;
1009 batch.nr_ops = 0;
1010 batch.nr_pages = 0;
1012 for (i = 0; i < copy.count; i++) {
1013 struct gntdev_grant_copy_segment seg;
1015 if (copy_from_user(&seg, &copy.segments[i], sizeof(seg))) {
1016 ret = -EFAULT;
1017 goto out;
1020 ret = gntdev_grant_copy_seg(&batch, &seg, &copy.segments[i].status);
1021 if (ret < 0)
1022 goto out;
1024 cond_resched();
1026 if (batch.nr_ops)
1027 ret = gntdev_copy(&batch);
1028 return ret;
1030 out:
1031 gntdev_put_pages(&batch);
1032 return ret;
1035 static long gntdev_ioctl(struct file *flip,
1036 unsigned int cmd, unsigned long arg)
1038 struct gntdev_priv *priv = flip->private_data;
1039 void __user *ptr = (void __user *)arg;
1041 switch (cmd) {
1042 case IOCTL_GNTDEV_MAP_GRANT_REF:
1043 return gntdev_ioctl_map_grant_ref(priv, ptr);
1045 case IOCTL_GNTDEV_UNMAP_GRANT_REF:
1046 return gntdev_ioctl_unmap_grant_ref(priv, ptr);
1048 case IOCTL_GNTDEV_GET_OFFSET_FOR_VADDR:
1049 return gntdev_ioctl_get_offset_for_vaddr(priv, ptr);
1051 case IOCTL_GNTDEV_SET_UNMAP_NOTIFY:
1052 return gntdev_ioctl_notify(priv, ptr);
1054 case IOCTL_GNTDEV_GRANT_COPY:
1055 return gntdev_ioctl_grant_copy(priv, ptr);
1057 #ifdef CONFIG_XEN_GNTDEV_DMABUF
1058 case IOCTL_GNTDEV_DMABUF_EXP_FROM_REFS:
1059 return gntdev_ioctl_dmabuf_exp_from_refs(priv, use_ptemod, ptr);
1061 case IOCTL_GNTDEV_DMABUF_EXP_WAIT_RELEASED:
1062 return gntdev_ioctl_dmabuf_exp_wait_released(priv, ptr);
1064 case IOCTL_GNTDEV_DMABUF_IMP_TO_REFS:
1065 return gntdev_ioctl_dmabuf_imp_to_refs(priv, ptr);
1067 case IOCTL_GNTDEV_DMABUF_IMP_RELEASE:
1068 return gntdev_ioctl_dmabuf_imp_release(priv, ptr);
1069 #endif
1071 default:
1072 pr_debug("priv %p, unknown cmd %x\n", priv, cmd);
1073 return -ENOIOCTLCMD;
1076 return 0;
1079 static int gntdev_mmap(struct file *flip, struct vm_area_struct *vma)
1081 struct gntdev_priv *priv = flip->private_data;
1082 int index = vma->vm_pgoff;
1083 int count = vma_pages(vma);
1084 struct gntdev_grant_map *map;
1085 int err = -EINVAL;
1087 if ((vma->vm_flags & VM_WRITE) && !(vma->vm_flags & VM_SHARED))
1088 return -EINVAL;
1090 pr_debug("map %d+%d at %lx (pgoff %lx)\n",
1091 index, count, vma->vm_start, vma->vm_pgoff);
1093 mutex_lock(&priv->lock);
1094 map = gntdev_find_map_index(priv, index, count);
1095 if (!map)
1096 goto unlock_out;
1097 if (use_ptemod && map->vma)
1098 goto unlock_out;
1099 if (use_ptemod && priv->mm != vma->vm_mm) {
1100 pr_warn("Huh? Other mm?\n");
1101 goto unlock_out;
1104 refcount_inc(&map->users);
1106 vma->vm_ops = &gntdev_vmops;
1108 vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP | VM_MIXEDMAP;
1110 if (use_ptemod)
1111 vma->vm_flags |= VM_DONTCOPY;
1113 vma->vm_private_data = map;
1115 if (use_ptemod)
1116 map->vma = vma;
1118 if (map->flags) {
1119 if ((vma->vm_flags & VM_WRITE) &&
1120 (map->flags & GNTMAP_readonly))
1121 goto out_unlock_put;
1122 } else {
1123 map->flags = GNTMAP_host_map;
1124 if (!(vma->vm_flags & VM_WRITE))
1125 map->flags |= GNTMAP_readonly;
1128 mutex_unlock(&priv->lock);
1130 if (use_ptemod) {
1131 map->pages_vm_start = vma->vm_start;
1132 err = apply_to_page_range(vma->vm_mm, vma->vm_start,
1133 vma->vm_end - vma->vm_start,
1134 find_grant_ptes, map);
1135 if (err) {
1136 pr_warn("find_grant_ptes() failure.\n");
1137 goto out_put_map;
1141 err = gntdev_map_grant_pages(map);
1142 if (err)
1143 goto out_put_map;
1145 if (!use_ptemod) {
1146 err = vm_map_pages(vma, map->pages, map->count);
1147 if (err)
1148 goto out_put_map;
1149 } else {
1150 #ifdef CONFIG_X86
1152 * If the PTEs were not made special by the grant map
1153 * hypercall, do so here.
1155 * This is racy since the mapping is already visible
1156 * to userspace but userspace should be well-behaved
1157 * enough to not touch it until the mmap() call
1158 * returns.
1160 if (!xen_feature(XENFEAT_gnttab_map_avail_bits)) {
1161 apply_to_page_range(vma->vm_mm, vma->vm_start,
1162 vma->vm_end - vma->vm_start,
1163 set_grant_ptes_as_special, NULL);
1165 #endif
1168 return 0;
1170 unlock_out:
1171 mutex_unlock(&priv->lock);
1172 return err;
1174 out_unlock_put:
1175 mutex_unlock(&priv->lock);
1176 out_put_map:
1177 if (use_ptemod) {
1178 map->vma = NULL;
1179 unmap_grant_pages(map, 0, map->count);
1181 gntdev_put_map(priv, map);
1182 return err;
1185 static const struct file_operations gntdev_fops = {
1186 .owner = THIS_MODULE,
1187 .open = gntdev_open,
1188 .release = gntdev_release,
1189 .mmap = gntdev_mmap,
1190 .unlocked_ioctl = gntdev_ioctl
1193 static struct miscdevice gntdev_miscdev = {
1194 .minor = MISC_DYNAMIC_MINOR,
1195 .name = "xen/gntdev",
1196 .fops = &gntdev_fops,
1199 /* ------------------------------------------------------------------ */
1201 static int __init gntdev_init(void)
1203 int err;
1205 if (!xen_domain())
1206 return -ENODEV;
1208 use_ptemod = !xen_feature(XENFEAT_auto_translated_physmap);
1210 err = misc_register(&gntdev_miscdev);
1211 if (err != 0) {
1212 pr_err("Could not register gntdev device\n");
1213 return err;
1215 return 0;
1218 static void __exit gntdev_exit(void)
1220 misc_deregister(&gntdev_miscdev);
1223 module_init(gntdev_init);
1224 module_exit(gntdev_exit);
1226 /* ------------------------------------------------------------------ */