proc: test /proc/thread-self symlink
[linux/fpc-iii.git] / drivers / xen / gntdev.c
blob57390c7666e5dd8d44bfe9bdf1e503afb13de189
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, pgtable_t token,
268 unsigned long addr, void *data)
270 struct gntdev_grant_map *map = data;
271 unsigned int pgnr = (addr - map->vma->vm_start) >> PAGE_SHIFT;
272 int flags = map->flags | GNTMAP_application_map | GNTMAP_contains_pte;
273 u64 pte_maddr;
275 BUG_ON(pgnr >= map->count);
276 pte_maddr = arbitrary_virt_to_machine(pte).maddr;
279 * Set the PTE as special to force get_user_pages_fast() fall
280 * back to the slow path. If this is not supported as part of
281 * the grant map, it will be done afterwards.
283 if (xen_feature(XENFEAT_gnttab_map_avail_bits))
284 flags |= (1 << _GNTMAP_guest_avail0);
286 gnttab_set_map_op(&map->map_ops[pgnr], pte_maddr, flags,
287 map->grants[pgnr].ref,
288 map->grants[pgnr].domid);
289 gnttab_set_unmap_op(&map->unmap_ops[pgnr], pte_maddr, flags,
290 -1 /* handle */);
291 return 0;
294 #ifdef CONFIG_X86
295 static int set_grant_ptes_as_special(pte_t *pte, pgtable_t token,
296 unsigned long addr, void *data)
298 set_pte_at(current->mm, addr, pte, pte_mkspecial(*pte));
299 return 0;
301 #endif
303 int gntdev_map_grant_pages(struct gntdev_grant_map *map)
305 int i, err = 0;
307 if (!use_ptemod) {
308 /* Note: it could already be mapped */
309 if (map->map_ops[0].handle != -1)
310 return 0;
311 for (i = 0; i < map->count; i++) {
312 unsigned long addr = (unsigned long)
313 pfn_to_kaddr(page_to_pfn(map->pages[i]));
314 gnttab_set_map_op(&map->map_ops[i], addr, map->flags,
315 map->grants[i].ref,
316 map->grants[i].domid);
317 gnttab_set_unmap_op(&map->unmap_ops[i], addr,
318 map->flags, -1 /* handle */);
320 } else {
322 * Setup the map_ops corresponding to the pte entries pointing
323 * to the kernel linear addresses of the struct pages.
324 * These ptes are completely different from the user ptes dealt
325 * with find_grant_ptes.
327 for (i = 0; i < map->count; i++) {
328 unsigned long address = (unsigned long)
329 pfn_to_kaddr(page_to_pfn(map->pages[i]));
330 BUG_ON(PageHighMem(map->pages[i]));
332 gnttab_set_map_op(&map->kmap_ops[i], address,
333 map->flags | GNTMAP_host_map,
334 map->grants[i].ref,
335 map->grants[i].domid);
336 gnttab_set_unmap_op(&map->kunmap_ops[i], address,
337 map->flags | GNTMAP_host_map, -1);
341 pr_debug("map %d+%d\n", map->index, map->count);
342 err = gnttab_map_refs(map->map_ops, use_ptemod ? map->kmap_ops : NULL,
343 map->pages, map->count);
344 if (err)
345 return err;
347 for (i = 0; i < map->count; i++) {
348 if (map->map_ops[i].status) {
349 err = -EINVAL;
350 continue;
353 map->unmap_ops[i].handle = map->map_ops[i].handle;
354 if (use_ptemod)
355 map->kunmap_ops[i].handle = map->kmap_ops[i].handle;
356 #ifdef CONFIG_XEN_GRANT_DMA_ALLOC
357 else if (map->dma_vaddr) {
358 unsigned long bfn;
360 bfn = pfn_to_bfn(page_to_pfn(map->pages[i]));
361 map->unmap_ops[i].dev_bus_addr = __pfn_to_phys(bfn);
363 #endif
365 return err;
368 static int __unmap_grant_pages(struct gntdev_grant_map *map, int offset,
369 int pages)
371 int i, err = 0;
372 struct gntab_unmap_queue_data unmap_data;
374 if (map->notify.flags & UNMAP_NOTIFY_CLEAR_BYTE) {
375 int pgno = (map->notify.addr >> PAGE_SHIFT);
376 if (pgno >= offset && pgno < offset + pages) {
377 /* No need for kmap, pages are in lowmem */
378 uint8_t *tmp = pfn_to_kaddr(page_to_pfn(map->pages[pgno]));
379 tmp[map->notify.addr & (PAGE_SIZE-1)] = 0;
380 map->notify.flags &= ~UNMAP_NOTIFY_CLEAR_BYTE;
384 unmap_data.unmap_ops = map->unmap_ops + offset;
385 unmap_data.kunmap_ops = use_ptemod ? map->kunmap_ops + offset : NULL;
386 unmap_data.pages = map->pages + offset;
387 unmap_data.count = pages;
389 err = gnttab_unmap_refs_sync(&unmap_data);
390 if (err)
391 return err;
393 for (i = 0; i < pages; i++) {
394 if (map->unmap_ops[offset+i].status)
395 err = -EINVAL;
396 pr_debug("unmap handle=%d st=%d\n",
397 map->unmap_ops[offset+i].handle,
398 map->unmap_ops[offset+i].status);
399 map->unmap_ops[offset+i].handle = -1;
401 return err;
404 static int unmap_grant_pages(struct gntdev_grant_map *map, int offset,
405 int pages)
407 int range, err = 0;
409 pr_debug("unmap %d+%d [%d+%d]\n", map->index, map->count, offset, pages);
411 /* It is possible the requested range will have a "hole" where we
412 * already unmapped some of the grants. Only unmap valid ranges.
414 while (pages && !err) {
415 while (pages && map->unmap_ops[offset].handle == -1) {
416 offset++;
417 pages--;
419 range = 0;
420 while (range < pages) {
421 if (map->unmap_ops[offset+range].handle == -1)
422 break;
423 range++;
425 err = __unmap_grant_pages(map, offset, range);
426 offset += range;
427 pages -= range;
430 return err;
433 /* ------------------------------------------------------------------ */
435 static void gntdev_vma_open(struct vm_area_struct *vma)
437 struct gntdev_grant_map *map = vma->vm_private_data;
439 pr_debug("gntdev_vma_open %p\n", vma);
440 refcount_inc(&map->users);
443 static void gntdev_vma_close(struct vm_area_struct *vma)
445 struct gntdev_grant_map *map = vma->vm_private_data;
446 struct file *file = vma->vm_file;
447 struct gntdev_priv *priv = file->private_data;
449 pr_debug("gntdev_vma_close %p\n", vma);
450 if (use_ptemod) {
451 /* It is possible that an mmu notifier could be running
452 * concurrently, so take priv->lock to ensure that the vma won't
453 * vanishing during the unmap_grant_pages call, since we will
454 * spin here until that completes. Such a concurrent call will
455 * not do any unmapping, since that has been done prior to
456 * closing the vma, but it may still iterate the unmap_ops list.
458 mutex_lock(&priv->lock);
459 map->vma = NULL;
460 mutex_unlock(&priv->lock);
462 vma->vm_private_data = NULL;
463 gntdev_put_map(priv, map);
466 static struct page *gntdev_vma_find_special_page(struct vm_area_struct *vma,
467 unsigned long addr)
469 struct gntdev_grant_map *map = vma->vm_private_data;
471 return map->pages[(addr - map->pages_vm_start) >> PAGE_SHIFT];
474 static const struct vm_operations_struct gntdev_vmops = {
475 .open = gntdev_vma_open,
476 .close = gntdev_vma_close,
477 .find_special_page = gntdev_vma_find_special_page,
480 /* ------------------------------------------------------------------ */
482 static bool in_range(struct gntdev_grant_map *map,
483 unsigned long start, unsigned long end)
485 if (!map->vma)
486 return false;
487 if (map->vma->vm_start >= end)
488 return false;
489 if (map->vma->vm_end <= start)
490 return false;
492 return true;
495 static void unmap_if_in_range(struct gntdev_grant_map *map,
496 unsigned long start, unsigned long end)
498 unsigned long mstart, mend;
499 int err;
501 mstart = max(start, map->vma->vm_start);
502 mend = min(end, map->vma->vm_end);
503 pr_debug("map %d+%d (%lx %lx), range %lx %lx, mrange %lx %lx\n",
504 map->index, map->count,
505 map->vma->vm_start, map->vma->vm_end,
506 start, end, mstart, mend);
507 err = unmap_grant_pages(map,
508 (mstart - map->vma->vm_start) >> PAGE_SHIFT,
509 (mend - mstart) >> PAGE_SHIFT);
510 WARN_ON(err);
513 static int mn_invl_range_start(struct mmu_notifier *mn,
514 struct mm_struct *mm,
515 unsigned long start, unsigned long end,
516 bool blockable)
518 struct gntdev_priv *priv = container_of(mn, struct gntdev_priv, mn);
519 struct gntdev_grant_map *map;
520 int ret = 0;
522 /* TODO do we really need a mutex here? */
523 if (blockable)
524 mutex_lock(&priv->lock);
525 else if (!mutex_trylock(&priv->lock))
526 return -EAGAIN;
528 list_for_each_entry(map, &priv->maps, next) {
529 if (in_range(map, start, end)) {
530 ret = -EAGAIN;
531 goto out_unlock;
533 unmap_if_in_range(map, start, end);
535 list_for_each_entry(map, &priv->freeable_maps, next) {
536 if (in_range(map, start, end)) {
537 ret = -EAGAIN;
538 goto out_unlock;
540 unmap_if_in_range(map, start, end);
543 out_unlock:
544 mutex_unlock(&priv->lock);
546 return ret;
549 static void mn_release(struct mmu_notifier *mn,
550 struct mm_struct *mm)
552 struct gntdev_priv *priv = container_of(mn, struct gntdev_priv, mn);
553 struct gntdev_grant_map *map;
554 int err;
556 mutex_lock(&priv->lock);
557 list_for_each_entry(map, &priv->maps, next) {
558 if (!map->vma)
559 continue;
560 pr_debug("map %d+%d (%lx %lx)\n",
561 map->index, map->count,
562 map->vma->vm_start, map->vma->vm_end);
563 err = unmap_grant_pages(map, /* offset */ 0, map->count);
564 WARN_ON(err);
566 list_for_each_entry(map, &priv->freeable_maps, next) {
567 if (!map->vma)
568 continue;
569 pr_debug("map %d+%d (%lx %lx)\n",
570 map->index, map->count,
571 map->vma->vm_start, map->vma->vm_end);
572 err = unmap_grant_pages(map, /* offset */ 0, map->count);
573 WARN_ON(err);
575 mutex_unlock(&priv->lock);
578 static const struct mmu_notifier_ops gntdev_mmu_ops = {
579 .release = mn_release,
580 .invalidate_range_start = mn_invl_range_start,
583 /* ------------------------------------------------------------------ */
585 static int gntdev_open(struct inode *inode, struct file *flip)
587 struct gntdev_priv *priv;
588 int ret = 0;
590 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
591 if (!priv)
592 return -ENOMEM;
594 INIT_LIST_HEAD(&priv->maps);
595 INIT_LIST_HEAD(&priv->freeable_maps);
596 mutex_init(&priv->lock);
598 #ifdef CONFIG_XEN_GNTDEV_DMABUF
599 priv->dmabuf_priv = gntdev_dmabuf_init();
600 if (IS_ERR(priv->dmabuf_priv)) {
601 ret = PTR_ERR(priv->dmabuf_priv);
602 kfree(priv);
603 return ret;
605 #endif
607 if (use_ptemod) {
608 priv->mm = get_task_mm(current);
609 if (!priv->mm) {
610 kfree(priv);
611 return -ENOMEM;
613 priv->mn.ops = &gntdev_mmu_ops;
614 ret = mmu_notifier_register(&priv->mn, priv->mm);
615 mmput(priv->mm);
618 if (ret) {
619 kfree(priv);
620 return ret;
623 flip->private_data = priv;
624 #ifdef CONFIG_XEN_GRANT_DMA_ALLOC
625 priv->dma_dev = gntdev_miscdev.this_device;
628 * The device is not spawn from a device tree, so arch_setup_dma_ops
629 * is not called, thus leaving the device with dummy DMA ops.
630 * Fix this by calling of_dma_configure() with a NULL node to set
631 * default DMA ops.
633 of_dma_configure(priv->dma_dev, NULL, true);
634 #endif
635 pr_debug("priv %p\n", priv);
637 return 0;
640 static int gntdev_release(struct inode *inode, struct file *flip)
642 struct gntdev_priv *priv = flip->private_data;
643 struct gntdev_grant_map *map;
645 pr_debug("priv %p\n", priv);
647 mutex_lock(&priv->lock);
648 while (!list_empty(&priv->maps)) {
649 map = list_entry(priv->maps.next,
650 struct gntdev_grant_map, next);
651 list_del(&map->next);
652 gntdev_put_map(NULL /* already removed */, map);
654 WARN_ON(!list_empty(&priv->freeable_maps));
655 mutex_unlock(&priv->lock);
657 #ifdef CONFIG_XEN_GNTDEV_DMABUF
658 gntdev_dmabuf_fini(priv->dmabuf_priv);
659 #endif
661 if (use_ptemod)
662 mmu_notifier_unregister(&priv->mn, priv->mm);
664 kfree(priv);
665 return 0;
668 static long gntdev_ioctl_map_grant_ref(struct gntdev_priv *priv,
669 struct ioctl_gntdev_map_grant_ref __user *u)
671 struct ioctl_gntdev_map_grant_ref op;
672 struct gntdev_grant_map *map;
673 int err;
675 if (copy_from_user(&op, u, sizeof(op)) != 0)
676 return -EFAULT;
677 pr_debug("priv %p, add %d\n", priv, op.count);
678 if (unlikely(op.count <= 0))
679 return -EINVAL;
681 err = -ENOMEM;
682 map = gntdev_alloc_map(priv, op.count, 0 /* This is not a dma-buf. */);
683 if (!map)
684 return err;
686 if (unlikely(gntdev_account_mapped_pages(op.count))) {
687 pr_debug("can't map: over limit\n");
688 gntdev_put_map(NULL, map);
689 return err;
692 if (copy_from_user(map->grants, &u->refs,
693 sizeof(map->grants[0]) * op.count) != 0) {
694 gntdev_put_map(NULL, map);
695 return -EFAULT;
698 mutex_lock(&priv->lock);
699 gntdev_add_map(priv, map);
700 op.index = map->index << PAGE_SHIFT;
701 mutex_unlock(&priv->lock);
703 if (copy_to_user(u, &op, sizeof(op)) != 0)
704 return -EFAULT;
706 return 0;
709 static long gntdev_ioctl_unmap_grant_ref(struct gntdev_priv *priv,
710 struct ioctl_gntdev_unmap_grant_ref __user *u)
712 struct ioctl_gntdev_unmap_grant_ref op;
713 struct gntdev_grant_map *map;
714 int err = -ENOENT;
716 if (copy_from_user(&op, u, sizeof(op)) != 0)
717 return -EFAULT;
718 pr_debug("priv %p, del %d+%d\n", priv, (int)op.index, (int)op.count);
720 mutex_lock(&priv->lock);
721 map = gntdev_find_map_index(priv, op.index >> PAGE_SHIFT, op.count);
722 if (map) {
723 list_del(&map->next);
724 if (populate_freeable_maps)
725 list_add_tail(&map->next, &priv->freeable_maps);
726 err = 0;
728 mutex_unlock(&priv->lock);
729 if (map)
730 gntdev_put_map(priv, map);
731 return err;
734 static long gntdev_ioctl_get_offset_for_vaddr(struct gntdev_priv *priv,
735 struct ioctl_gntdev_get_offset_for_vaddr __user *u)
737 struct ioctl_gntdev_get_offset_for_vaddr op;
738 struct vm_area_struct *vma;
739 struct gntdev_grant_map *map;
740 int rv = -EINVAL;
742 if (copy_from_user(&op, u, sizeof(op)) != 0)
743 return -EFAULT;
744 pr_debug("priv %p, offset for vaddr %lx\n", priv, (unsigned long)op.vaddr);
746 down_read(&current->mm->mmap_sem);
747 vma = find_vma(current->mm, op.vaddr);
748 if (!vma || vma->vm_ops != &gntdev_vmops)
749 goto out_unlock;
751 map = vma->vm_private_data;
752 if (!map)
753 goto out_unlock;
755 op.offset = map->index << PAGE_SHIFT;
756 op.count = map->count;
757 rv = 0;
759 out_unlock:
760 up_read(&current->mm->mmap_sem);
762 if (rv == 0 && copy_to_user(u, &op, sizeof(op)) != 0)
763 return -EFAULT;
764 return rv;
767 static long gntdev_ioctl_notify(struct gntdev_priv *priv, void __user *u)
769 struct ioctl_gntdev_unmap_notify op;
770 struct gntdev_grant_map *map;
771 int rc;
772 int out_flags;
773 unsigned int out_event;
775 if (copy_from_user(&op, u, sizeof(op)))
776 return -EFAULT;
778 if (op.action & ~(UNMAP_NOTIFY_CLEAR_BYTE|UNMAP_NOTIFY_SEND_EVENT))
779 return -EINVAL;
781 /* We need to grab a reference to the event channel we are going to use
782 * to send the notify before releasing the reference we may already have
783 * (if someone has called this ioctl twice). This is required so that
784 * it is possible to change the clear_byte part of the notification
785 * without disturbing the event channel part, which may now be the last
786 * reference to that event channel.
788 if (op.action & UNMAP_NOTIFY_SEND_EVENT) {
789 if (evtchn_get(op.event_channel_port))
790 return -EINVAL;
793 out_flags = op.action;
794 out_event = op.event_channel_port;
796 mutex_lock(&priv->lock);
798 list_for_each_entry(map, &priv->maps, next) {
799 uint64_t begin = map->index << PAGE_SHIFT;
800 uint64_t end = (map->index + map->count) << PAGE_SHIFT;
801 if (op.index >= begin && op.index < end)
802 goto found;
804 rc = -ENOENT;
805 goto unlock_out;
807 found:
808 if ((op.action & UNMAP_NOTIFY_CLEAR_BYTE) &&
809 (map->flags & GNTMAP_readonly)) {
810 rc = -EINVAL;
811 goto unlock_out;
814 out_flags = map->notify.flags;
815 out_event = map->notify.event;
817 map->notify.flags = op.action;
818 map->notify.addr = op.index - (map->index << PAGE_SHIFT);
819 map->notify.event = op.event_channel_port;
821 rc = 0;
823 unlock_out:
824 mutex_unlock(&priv->lock);
826 /* Drop the reference to the event channel we did not save in the map */
827 if (out_flags & UNMAP_NOTIFY_SEND_EVENT)
828 evtchn_put(out_event);
830 return rc;
833 #define GNTDEV_COPY_BATCH 16
835 struct gntdev_copy_batch {
836 struct gnttab_copy ops[GNTDEV_COPY_BATCH];
837 struct page *pages[GNTDEV_COPY_BATCH];
838 s16 __user *status[GNTDEV_COPY_BATCH];
839 unsigned int nr_ops;
840 unsigned int nr_pages;
843 static int gntdev_get_page(struct gntdev_copy_batch *batch, void __user *virt,
844 bool writeable, unsigned long *gfn)
846 unsigned long addr = (unsigned long)virt;
847 struct page *page;
848 unsigned long xen_pfn;
849 int ret;
851 ret = get_user_pages_fast(addr, 1, writeable, &page);
852 if (ret < 0)
853 return ret;
855 batch->pages[batch->nr_pages++] = page;
857 xen_pfn = page_to_xen_pfn(page) + XEN_PFN_DOWN(addr & ~PAGE_MASK);
858 *gfn = pfn_to_gfn(xen_pfn);
860 return 0;
863 static void gntdev_put_pages(struct gntdev_copy_batch *batch)
865 unsigned int i;
867 for (i = 0; i < batch->nr_pages; i++)
868 put_page(batch->pages[i]);
869 batch->nr_pages = 0;
872 static int gntdev_copy(struct gntdev_copy_batch *batch)
874 unsigned int i;
876 gnttab_batch_copy(batch->ops, batch->nr_ops);
877 gntdev_put_pages(batch);
880 * For each completed op, update the status if the op failed
881 * and all previous ops for the segment were successful.
883 for (i = 0; i < batch->nr_ops; i++) {
884 s16 status = batch->ops[i].status;
885 s16 old_status;
887 if (status == GNTST_okay)
888 continue;
890 if (__get_user(old_status, batch->status[i]))
891 return -EFAULT;
893 if (old_status != GNTST_okay)
894 continue;
896 if (__put_user(status, batch->status[i]))
897 return -EFAULT;
900 batch->nr_ops = 0;
901 return 0;
904 static int gntdev_grant_copy_seg(struct gntdev_copy_batch *batch,
905 struct gntdev_grant_copy_segment *seg,
906 s16 __user *status)
908 uint16_t copied = 0;
911 * Disallow local -> local copies since there is only space in
912 * batch->pages for one page per-op and this would be a very
913 * expensive memcpy().
915 if (!(seg->flags & (GNTCOPY_source_gref | GNTCOPY_dest_gref)))
916 return -EINVAL;
918 /* Can't cross page if source/dest is a grant ref. */
919 if (seg->flags & GNTCOPY_source_gref) {
920 if (seg->source.foreign.offset + seg->len > XEN_PAGE_SIZE)
921 return -EINVAL;
923 if (seg->flags & GNTCOPY_dest_gref) {
924 if (seg->dest.foreign.offset + seg->len > XEN_PAGE_SIZE)
925 return -EINVAL;
928 if (put_user(GNTST_okay, status))
929 return -EFAULT;
931 while (copied < seg->len) {
932 struct gnttab_copy *op;
933 void __user *virt;
934 size_t len, off;
935 unsigned long gfn;
936 int ret;
938 if (batch->nr_ops >= GNTDEV_COPY_BATCH) {
939 ret = gntdev_copy(batch);
940 if (ret < 0)
941 return ret;
944 len = seg->len - copied;
946 op = &batch->ops[batch->nr_ops];
947 op->flags = 0;
949 if (seg->flags & GNTCOPY_source_gref) {
950 op->source.u.ref = seg->source.foreign.ref;
951 op->source.domid = seg->source.foreign.domid;
952 op->source.offset = seg->source.foreign.offset + copied;
953 op->flags |= GNTCOPY_source_gref;
954 } else {
955 virt = seg->source.virt + copied;
956 off = (unsigned long)virt & ~XEN_PAGE_MASK;
957 len = min(len, (size_t)XEN_PAGE_SIZE - off);
959 ret = gntdev_get_page(batch, virt, false, &gfn);
960 if (ret < 0)
961 return ret;
963 op->source.u.gmfn = gfn;
964 op->source.domid = DOMID_SELF;
965 op->source.offset = off;
968 if (seg->flags & GNTCOPY_dest_gref) {
969 op->dest.u.ref = seg->dest.foreign.ref;
970 op->dest.domid = seg->dest.foreign.domid;
971 op->dest.offset = seg->dest.foreign.offset + copied;
972 op->flags |= GNTCOPY_dest_gref;
973 } else {
974 virt = seg->dest.virt + copied;
975 off = (unsigned long)virt & ~XEN_PAGE_MASK;
976 len = min(len, (size_t)XEN_PAGE_SIZE - off);
978 ret = gntdev_get_page(batch, virt, true, &gfn);
979 if (ret < 0)
980 return ret;
982 op->dest.u.gmfn = gfn;
983 op->dest.domid = DOMID_SELF;
984 op->dest.offset = off;
987 op->len = len;
988 copied += len;
990 batch->status[batch->nr_ops] = status;
991 batch->nr_ops++;
994 return 0;
997 static long gntdev_ioctl_grant_copy(struct gntdev_priv *priv, void __user *u)
999 struct ioctl_gntdev_grant_copy copy;
1000 struct gntdev_copy_batch batch;
1001 unsigned int i;
1002 int ret = 0;
1004 if (copy_from_user(&copy, u, sizeof(copy)))
1005 return -EFAULT;
1007 batch.nr_ops = 0;
1008 batch.nr_pages = 0;
1010 for (i = 0; i < copy.count; i++) {
1011 struct gntdev_grant_copy_segment seg;
1013 if (copy_from_user(&seg, &copy.segments[i], sizeof(seg))) {
1014 ret = -EFAULT;
1015 goto out;
1018 ret = gntdev_grant_copy_seg(&batch, &seg, &copy.segments[i].status);
1019 if (ret < 0)
1020 goto out;
1022 cond_resched();
1024 if (batch.nr_ops)
1025 ret = gntdev_copy(&batch);
1026 return ret;
1028 out:
1029 gntdev_put_pages(&batch);
1030 return ret;
1033 static long gntdev_ioctl(struct file *flip,
1034 unsigned int cmd, unsigned long arg)
1036 struct gntdev_priv *priv = flip->private_data;
1037 void __user *ptr = (void __user *)arg;
1039 switch (cmd) {
1040 case IOCTL_GNTDEV_MAP_GRANT_REF:
1041 return gntdev_ioctl_map_grant_ref(priv, ptr);
1043 case IOCTL_GNTDEV_UNMAP_GRANT_REF:
1044 return gntdev_ioctl_unmap_grant_ref(priv, ptr);
1046 case IOCTL_GNTDEV_GET_OFFSET_FOR_VADDR:
1047 return gntdev_ioctl_get_offset_for_vaddr(priv, ptr);
1049 case IOCTL_GNTDEV_SET_UNMAP_NOTIFY:
1050 return gntdev_ioctl_notify(priv, ptr);
1052 case IOCTL_GNTDEV_GRANT_COPY:
1053 return gntdev_ioctl_grant_copy(priv, ptr);
1055 #ifdef CONFIG_XEN_GNTDEV_DMABUF
1056 case IOCTL_GNTDEV_DMABUF_EXP_FROM_REFS:
1057 return gntdev_ioctl_dmabuf_exp_from_refs(priv, use_ptemod, ptr);
1059 case IOCTL_GNTDEV_DMABUF_EXP_WAIT_RELEASED:
1060 return gntdev_ioctl_dmabuf_exp_wait_released(priv, ptr);
1062 case IOCTL_GNTDEV_DMABUF_IMP_TO_REFS:
1063 return gntdev_ioctl_dmabuf_imp_to_refs(priv, ptr);
1065 case IOCTL_GNTDEV_DMABUF_IMP_RELEASE:
1066 return gntdev_ioctl_dmabuf_imp_release(priv, ptr);
1067 #endif
1069 default:
1070 pr_debug("priv %p, unknown cmd %x\n", priv, cmd);
1071 return -ENOIOCTLCMD;
1074 return 0;
1077 static int gntdev_mmap(struct file *flip, struct vm_area_struct *vma)
1079 struct gntdev_priv *priv = flip->private_data;
1080 int index = vma->vm_pgoff;
1081 int count = vma_pages(vma);
1082 struct gntdev_grant_map *map;
1083 int i, err = -EINVAL;
1085 if ((vma->vm_flags & VM_WRITE) && !(vma->vm_flags & VM_SHARED))
1086 return -EINVAL;
1088 pr_debug("map %d+%d at %lx (pgoff %lx)\n",
1089 index, count, vma->vm_start, vma->vm_pgoff);
1091 mutex_lock(&priv->lock);
1092 map = gntdev_find_map_index(priv, index, count);
1093 if (!map)
1094 goto unlock_out;
1095 if (use_ptemod && map->vma)
1096 goto unlock_out;
1097 if (use_ptemod && priv->mm != vma->vm_mm) {
1098 pr_warn("Huh? Other mm?\n");
1099 goto unlock_out;
1102 refcount_inc(&map->users);
1104 vma->vm_ops = &gntdev_vmops;
1106 vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP | VM_MIXEDMAP;
1108 if (use_ptemod)
1109 vma->vm_flags |= VM_DONTCOPY;
1111 vma->vm_private_data = map;
1113 if (use_ptemod)
1114 map->vma = vma;
1116 if (map->flags) {
1117 if ((vma->vm_flags & VM_WRITE) &&
1118 (map->flags & GNTMAP_readonly))
1119 goto out_unlock_put;
1120 } else {
1121 map->flags = GNTMAP_host_map;
1122 if (!(vma->vm_flags & VM_WRITE))
1123 map->flags |= GNTMAP_readonly;
1126 mutex_unlock(&priv->lock);
1128 if (use_ptemod) {
1129 map->pages_vm_start = vma->vm_start;
1130 err = apply_to_page_range(vma->vm_mm, vma->vm_start,
1131 vma->vm_end - vma->vm_start,
1132 find_grant_ptes, map);
1133 if (err) {
1134 pr_warn("find_grant_ptes() failure.\n");
1135 goto out_put_map;
1139 err = gntdev_map_grant_pages(map);
1140 if (err)
1141 goto out_put_map;
1143 if (!use_ptemod) {
1144 for (i = 0; i < count; i++) {
1145 err = vm_insert_page(vma, vma->vm_start + i*PAGE_SIZE,
1146 map->pages[i]);
1147 if (err)
1148 goto out_put_map;
1150 } else {
1151 #ifdef CONFIG_X86
1153 * If the PTEs were not made special by the grant map
1154 * hypercall, do so here.
1156 * This is racy since the mapping is already visible
1157 * to userspace but userspace should be well-behaved
1158 * enough to not touch it until the mmap() call
1159 * returns.
1161 if (!xen_feature(XENFEAT_gnttab_map_avail_bits)) {
1162 apply_to_page_range(vma->vm_mm, vma->vm_start,
1163 vma->vm_end - vma->vm_start,
1164 set_grant_ptes_as_special, NULL);
1166 #endif
1169 return 0;
1171 unlock_out:
1172 mutex_unlock(&priv->lock);
1173 return err;
1175 out_unlock_put:
1176 mutex_unlock(&priv->lock);
1177 out_put_map:
1178 if (use_ptemod) {
1179 map->vma = NULL;
1180 unmap_grant_pages(map, 0, map->count);
1182 gntdev_put_map(priv, map);
1183 return err;
1186 static const struct file_operations gntdev_fops = {
1187 .owner = THIS_MODULE,
1188 .open = gntdev_open,
1189 .release = gntdev_release,
1190 .mmap = gntdev_mmap,
1191 .unlocked_ioctl = gntdev_ioctl
1194 static struct miscdevice gntdev_miscdev = {
1195 .minor = MISC_DYNAMIC_MINOR,
1196 .name = "xen/gntdev",
1197 .fops = &gntdev_fops,
1200 /* ------------------------------------------------------------------ */
1202 static int __init gntdev_init(void)
1204 int err;
1206 if (!xen_domain())
1207 return -ENODEV;
1209 use_ptemod = !xen_feature(XENFEAT_auto_translated_physmap);
1211 err = misc_register(&gntdev_miscdev);
1212 if (err != 0) {
1213 pr_err("Could not register gntdev device\n");
1214 return err;
1216 return 0;
1219 static void __exit gntdev_exit(void)
1221 misc_deregister(&gntdev_miscdev);
1224 module_init(gntdev_init);
1225 module_exit(gntdev_exit);
1227 /* ------------------------------------------------------------------ */