/****************************************************************************** * gntdev.c * * Device for accessing (in user-space) pages that have been granted by other * domains. * * Copyright (c) 2006-2007, D G Murray. * (c) 2009 Gerd Hoffmann <kraxel@redhat.com> * (c) 2018 Oleksandr Andrushchenko, EPAM Systems Inc. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #undef DEBUG #define pr_fmt(fmt) "xen:" KBUILD_MODNAME ": " fmt #include <linux/dma-mapping.h>#include <linux/module.h>#include <linux/kernel.h>#include <linux/init.h>#include <linux/miscdevice.h>#include <linux/fs.h>#include <linux/uaccess.h>#include <linux/sched.h>#include <linux/sched/mm.h>#include <linux/spinlock.h>#include <linux/slab.h>#include <linux/highmem.h>#include <linux/refcount.h>#include <linux/workqueue.h> #include <xen/xen.h>#include <xen/grant_table.h>#include <xen/balloon.h>#include <xen/gntdev.h>#include <xen/events.h>#include <xen/page.h>#include <asm/xen/hypervisor.h>#include <asm/xen/hypercall.h> #include "gntdev-common.h"#ifdef CONFIG_XEN_GNTDEV_DMABUF#include "gntdev-dmabuf.h"#endif MODULE_LICENSE("GPL");MODULE_AUTHOR("Derek G. Murray <Derek.Murray@cl.cam.ac.uk>, " "Gerd Hoffmann <kraxel@redhat.com>");MODULE_DESCRIPTION("User-space granted page access driver"); #define GNTDEV_COPY_BATCH 16 struct gntdev_copy_batch { struct gnttab_copy ops[GNTDEV_COPY_BATCH]; struct page *pages[GNTDEV_COPY_BATCH]; s16 __user *status[GNTDEV_COPY_BATCH]; unsigned int nr_ops; unsigned int nr_pages; bool writeable; struct gntdev_copy_batch *next;}; static unsigned int limit = 64*1024;module_param(limit, uint, 0644);MODULE_PARM_DESC(limit, "Maximum number of grants that may be mapped by one mapping request"); static void unmap_grant_pages(struct gntdev_grant_map *map, int offset, int pages); static struct miscdevice gntdev_miscdev; /* ------------------------------------------------------------------ */ bool gntdev_test_page_count(unsigned int count){ return !count || count > limit;} static void gntdev_print_maps(struct gntdev_priv *priv, char *text, int text_index){#ifdef DEBUG struct gntdev_grant_map *map; pr_debug("%s: maps list (priv %p)\n", __func__, priv); list_for_each_entry(map, &priv->maps, next) pr_debug(" index %2d, count %2d %s\n", map->index, map->count, map->index == text_index && text ? text : "");#endif} static void gntdev_free_map(struct gntdev_grant_map *map){ if (map == NULL) return; #ifdef CONFIG_XEN_GRANT_DMA_ALLOC if (map->dma_vaddr) { struct gnttab_dma_alloc_args args; args.dev = map->dma_dev; args.coherent = !!(map->dma_flags & GNTDEV_DMA_FLAG_COHERENT); args.nr_pages = map->count; args.pages = map->pages; args.frames = map->frames; args.vaddr = map->dma_vaddr; args.dev_bus_addr = map->dma_bus_addr; gnttab_dma_free_pages(&args); } else#endif if (map->pages) gnttab_free_pages(map->count, map->pages); #ifdef CONFIG_XEN_GRANT_DMA_ALLOC kvfree(map->frames);#endif kvfree(map->pages); kvfree(map->grants); kvfree(map->map_ops); kvfree(map->unmap_ops); kvfree(map->kmap_ops); kvfree(map->kunmap_ops); kvfree(map->being_removed); kfree(map);} struct gntdev_grant_map *gntdev_alloc_map(struct gntdev_priv *priv, int count, int dma_flags){ struct gntdev_grant_map *add; int i; add = kzalloc_obj(*add); if (NULL == add) return NULL; add->grants = kvmalloc_objs(add->grants[0], count); add->map_ops = kvmalloc_objs(add->map_ops[0], count); add->unmap_ops = kvmalloc_objs(add->unmap_ops[0], count); add->pages = kvzalloc_objs(add->pages[0], count); add->being_removed = kvzalloc_objs(add->being_removed[0], count); if (NULL == add->grants || NULL == add->map_ops || NULL == add->unmap_ops || NULL == add->pages || NULL == add->being_removed) goto err; if (xen_pv_domain()) { add->kmap_ops = kvmalloc_objs(add->kmap_ops[0], count); add->kunmap_ops = kvmalloc_objs(add->kunmap_ops[0], count); if (NULL == add->kmap_ops || NULL == add->kunmap_ops) goto err; } #ifdef CONFIG_XEN_GRANT_DMA_ALLOC add->dma_flags = dma_flags; /* * Check if this mapping is requested to be backed * by a DMA buffer. */ if (dma_flags & (GNTDEV_DMA_FLAG_WC | GNTDEV_DMA_FLAG_COHERENT)) { struct gnttab_dma_alloc_args args; add->frames = kvzalloc_objs(add->frames[0], count); if (!add->frames) goto err; /* Remember the device, so we can free DMA memory. */ add->dma_dev = priv->dma_dev; args.dev = priv->dma_dev; args.coherent = !!(dma_flags & GNTDEV_DMA_FLAG_COHERENT); args.nr_pages = count; args.pages = add->pages; args.frames = add->frames; if (gnttab_dma_alloc_pages(&args)) goto err; add->dma_vaddr = args.vaddr; add->dma_bus_addr = args.dev_bus_addr; } else#endif if (gnttab_alloc_pages(count, add->pages)) goto err; for (i = 0; i < count; i++) { add->grants[i].domid = DOMID_INVALID; add->grants[i].ref = INVALID_GRANT_REF; add->map_ops[i].handle = INVALID_GRANT_HANDLE; add->unmap_ops[i].handle = INVALID_GRANT_HANDLE; if (xen_pv_domain()) { add->kmap_ops[i].handle = INVALID_GRANT_HANDLE; add->kunmap_ops[i].handle = INVALID_GRANT_HANDLE; } } add->index = 0; add->count = count; refcount_set(&add->users, 1); return add; err: gntdev_free_map(add); return NULL;} void gntdev_add_map(struct gntdev_priv *priv, struct gntdev_grant_map *add){ struct gntdev_grant_map *map; list_for_each_entry(map, &priv->maps, next) { if (add->index + add->count < map->index) { list_add_tail(&add->next, &map->next); goto done; } add->index = map->index + map->count; } list_add_tail(&add->next, &priv->maps); done: gntdev_print_maps(priv, "[new]", add->index);} static struct gntdev_grant_map *gntdev_find_map_index(struct gntdev_priv *priv, int index, int count){ struct gntdev_grant_map *map; list_for_each_entry(map, &priv->maps, next) { if (map->index != index) continue; if (count && map->count != count) continue; return map; } return NULL;} void gntdev_put_map(struct gntdev_priv *priv, struct gntdev_grant_map *map){ if (!map) return; if (!refcount_dec_and_test(&map->users)) return; if (map->pages && !xen_pv_domain()) { /* * Increment the reference count. This ensures that the * subsequent call to unmap_grant_pages() will not wind up * re-entering itself. It *can* wind up calling * gntdev_put_map() recursively, but such calls will be with a * reference count greater than 1, so they will return before * this code is reached. The recursion depth is thus limited to * 1. Do NOT use refcount_inc() here, as it will detect that * the reference count is zero and WARN(). */ refcount_set(&map->users, 1); /* * Unmap the grants. This may or may not be asynchronous, so it * is possible that the reference count is 1 on return, but it * could also be greater than 1. */ unmap_grant_pages(map, 0, map->count); /* Check if the memory now needs to be freed */ if (!refcount_dec_and_test(&map->users)) return; /* * All pages have been returned to the hypervisor, so free the * map. */ } if (xen_pv_domain() && map->notifier_init) mmu_interval_notifier_remove(&map->notifier); if (map->notify.flags & UNMAP_NOTIFY_SEND_EVENT) { notify_remote_via_evtchn(map->notify.event); evtchn_put(map->notify.event); } gntdev_free_map(map);} /* ------------------------------------------------------------------ */ static int find_grant_ptes(pte_t *pte, unsigned long addr, void *data){ struct gntdev_grant_map *map = data; unsigned int pgnr = (addr - map->pages_vm_start) >> PAGE_SHIFT; int flags = map->flags | GNTMAP_application_map | GNTMAP_contains_pte | (1 << _GNTMAP_guest_avail0); u64 pte_maddr; BUG_ON(pgnr >= map->count); pte_maddr = arbitrary_virt_to_machine(pte).maddr; /* Note: this will perform a pte_mkspecial() through the hypercall. */ gnttab_set_map_op(&map->map_ops[pgnr], pte_maddr, flags, map->grants[pgnr].ref, map->grants[pgnr].domid); gnttab_set_unmap_op(&map->unmap_ops[pgnr], pte_maddr, flags, INVALID_GRANT_HANDLE); return 0;} int gntdev_map_grant_pages(struct gntdev_grant_map *map){ size_t alloced = 0; int i, err = 0; if (!xen_pv_domain()) { /* Note: it could already be mapped */ if (map->map_ops[0].handle != INVALID_GRANT_HANDLE) return 0; for (i = 0; i < map->count; i++) { unsigned long addr = (unsigned long) pfn_to_kaddr(page_to_pfn(map->pages[i])); gnttab_set_map_op(&map->map_ops[i], addr, map->flags, map->grants[i].ref, map->grants[i].domid); gnttab_set_unmap_op(&map->unmap_ops[i], addr, map->flags, INVALID_GRANT_HANDLE); } } else { /* * Setup the map_ops corresponding to the pte entries pointing * to the kernel linear addresses of the struct pages. * These ptes are completely different from the user ptes dealt * with find_grant_ptes. * Note that GNTMAP_device_map isn't needed here: The * dev_bus_addr output field gets consumed only from ->map_ops, * and by not requesting it when mapping we also avoid needing * to mirror dev_bus_addr into ->unmap_ops (and holding an extra * reference to the page in the hypervisor). */ unsigned int flags = (map->flags & ~GNTMAP_device_map) | GNTMAP_host_map; for (i = 0; i < map->count; i++) { unsigned long address = (unsigned long) pfn_to_kaddr(page_to_pfn(map->pages[i])); BUG_ON(PageHighMem(map->pages[i])); gnttab_set_map_op(&map->kmap_ops[i], address, flags, map->grants[i].ref, map->grants[i].domid); gnttab_set_unmap_op(&map->kunmap_ops[i], address, flags, INVALID_GRANT_HANDLE); } } pr_debug("map %d+%d\n", map->index, map->count); err = gnttab_map_refs(map->map_ops, map->kmap_ops, map->pages, map->count); for (i = 0; i < map->count; i++) { if (map->map_ops[i].status == GNTST_okay) { map->unmap_ops[i].handle = map->map_ops[i].handle; alloced++; } else if (!err) err = -EINVAL; if (map->flags & GNTMAP_device_map) map->unmap_ops[i].dev_bus_addr = map->map_ops[i].dev_bus_addr; if (xen_pv_domain()) { if (map->kmap_ops[i].status == GNTST_okay) { alloced++; map->kunmap_ops[i].handle = map->kmap_ops[i].handle; } else if (!err) err = -EINVAL; } } atomic_add(alloced, &map->live_grants); return err;} static void __unmap_grant_pages_done(int result, struct gntab_unmap_queue_data *data){ unsigned int i; struct gntdev_grant_map *map = data->data; unsigned int offset = data->unmap_ops - map->unmap_ops; int successful_unmaps = 0; int live_grants; for (i = 0; i < data->count; i++) { if (map->unmap_ops[offset + i].status == GNTST_okay && map->unmap_ops[offset + i].handle != INVALID_GRANT_HANDLE) successful_unmaps++; WARN_ON(map->unmap_ops[offset + i].status != GNTST_okay && map->unmap_ops[offset + i].handle != INVALID_GRANT_HANDLE); pr_debug("unmap handle=%d st=%d\n", map->unmap_ops[offset+i].handle, map->unmap_ops[offset+i].status); map->unmap_ops[offset+i].handle = INVALID_GRANT_HANDLE; if (xen_pv_domain()) { if (map->kunmap_ops[offset + i].status == GNTST_okay && map->kunmap_ops[offset + i].handle != INVALID_GRANT_HANDLE) successful_unmaps++; WARN_ON(map->kunmap_ops[offset + i].status != GNTST_okay && map->kunmap_ops[offset + i].handle != INVALID_GRANT_HANDLE); pr_debug("kunmap handle=%u st=%d\n", map->kunmap_ops[offset+i].handle, map->kunmap_ops[offset+i].status); map->kunmap_ops[offset+i].handle = INVALID_GRANT_HANDLE; } } /* * Decrease the live-grant counter. This must happen after the loop to * prevent premature reuse of the grants by gnttab_mmap(). */ live_grants = atomic_sub_return(successful_unmaps, &map->live_grants); if (WARN_ON(live_grants < 0)) pr_err("%s: live_grants became negative (%d) after unmapping %d pages!\n", __func__, live_grants, successful_unmaps); /* Release reference taken by __unmap_grant_pages */ gntdev_put_map(NULL, map);} static void __unmap_grant_pages(struct gntdev_grant_map *map, int offset, int pages){ if (map->notify.flags & UNMAP_NOTIFY_CLEAR_BYTE) { int pgno = (map->notify.addr >> PAGE_SHIFT); if (pgno >= offset && pgno < offset + pages) { /* No need for kmap, pages are in lowmem */ uint8_t *tmp = pfn_to_kaddr(page_to_pfn(map->pages[pgno])); tmp[map->notify.addr & (PAGE_SIZE-1)] = 0; map->notify.flags &= ~UNMAP_NOTIFY_CLEAR_BYTE; } } map->unmap_data.unmap_ops = map->unmap_ops + offset; map->unmap_data.kunmap_ops = xen_pv_domain() ? map->kunmap_ops + offset : NULL; map->unmap_data.pages = map->pages + offset; map->unmap_data.count = pages; map->unmap_data.done = __unmap_grant_pages_done; map->unmap_data.data = map; refcount_inc(&map->users); /* to keep map alive during async call below */ gnttab_unmap_refs_async(&map->unmap_data);} static void unmap_grant_pages(struct gntdev_grant_map *map, int offset, int pages){ int range; if (atomic_read(&map->live_grants) == 0) return; /* Nothing to do */ pr_debug("unmap %d+%d [%d+%d]\n", map->index, map->count, offset, pages); /* It is possible the requested range will have a "hole" where we * already unmapped some of the grants. Only unmap valid ranges. */ while (pages) { while (pages && map->being_removed[offset]) { offset++; pages--; } range = 0; while (range < pages) { if (map->being_removed[offset + range]) break; map->being_removed[offset + range] = true; range++; } if (range) __unmap_grant_pages(map, offset, range); offset += range; pages -= range; }} Lines 1–500 of 1198. Load more
/****************************************************************************** * gntdev.c * * Device for accessing (in user-space) pages that have been granted by other * domains. * * Copyright (c) 2006-2007, D G Murray. * (c) 2009 Gerd Hoffmann <kraxel@redhat.com> * (c) 2018 Oleksandr Andrushchenko, EPAM Systems Inc. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #undef DEBUG #define pr_fmt(fmt) "xen:" KBUILD_MODNAME ": " fmt #include <linux/dma-mapping.h> #include <linux/module.h> #include <linux/kernel.h> #include <linux/init.h> #include <linux/miscdevice.h> #include <linux/fs.h> #include <linux/uaccess.h> #include <linux/sched.h> #include <linux/sched/mm.h> #include <linux/spinlock.h> #include <linux/slab.h> #include <linux/highmem.h> #include <linux/refcount.h> #include <linux/workqueue.h> #include <xen/xen.h> #include <xen/grant_table.h> #include <xen/balloon.h> #include <xen/gntdev.h> #include <xen/events.h> #include <xen/page.h> #include <asm/xen/hypervisor.h> #include <asm/xen/hypercall.h> #include "gntdev-common.h" #ifdef CONFIG_XEN_GNTDEV_DMABUF #include "gntdev-dmabuf.h" #endif MODULE_LICENSE("GPL"); MODULE_AUTHOR("Derek G. Murray <Derek.Murray@cl.cam.ac.uk>, " "Gerd Hoffmann <kraxel@redhat.com>"); MODULE_DESCRIPTION("User-space granted page access driver"); #define GNTDEV_COPY_BATCH 16 struct gntdev_copy_batch { struct gnttab_copy ops[GNTDEV_COPY_BATCH]; struct page *pages[GNTDEV_COPY_BATCH]; s16 __user *status[GNTDEV_COPY_BATCH]; unsigned int nr_ops; unsigned int nr_pages; bool writeable; struct gntdev_copy_batch *next; }; static unsigned int limit = 64*1024; module_param(limit, uint, 0644); MODULE_PARM_DESC(limit, "Maximum number of grants that may be mapped by one mapping request"); static void unmap_grant_pages(struct gntdev_grant_map *map, int offset, int pages); static struct miscdevice gntdev_miscdev; /* ------------------------------------------------------------------ */ bool gntdev_test_page_count(unsigned int count) { return !count || count > limit; } static void gntdev_print_maps(struct gntdev_priv *priv, char *text, int text_index) { #ifdef DEBUG struct gntdev_grant_map *map; pr_debug("%s: maps list (priv %p)\n", __func__, priv); list_for_each_entry(map, &priv->maps, next) pr_debug(" index %2d, count %2d %s\n", map->index, map->count, map->index == text_index && text ? text : ""); #endif } static void gntdev_free_map(struct gntdev_grant_map *map) { if (map == NULL) return; #ifdef CONFIG_XEN_GRANT_DMA_ALLOC if (map->dma_vaddr) { struct gnttab_dma_alloc_args args; args.dev = map->dma_dev; args.coherent = !!(map->dma_flags & GNTDEV_DMA_FLAG_COHERENT); args.nr_pages = map->count; args.pages = map->pages; args.frames = map->frames; args.vaddr = map->dma_vaddr; args.dev_bus_addr = map->dma_bus_addr; gnttab_dma_free_pages(&args); } else #endif if (map->pages) gnttab_free_pages(map->count, map->pages); #ifdef CONFIG_XEN_GRANT_DMA_ALLOC kvfree(map->frames); #endif kvfree(map->pages); kvfree(map->grants); kvfree(map->map_ops); kvfree(map->unmap_ops); kvfree(map->kmap_ops); kvfree(map->kunmap_ops); kvfree(map->being_removed); kfree(map); } struct gntdev_grant_map *gntdev_alloc_map(struct gntdev_priv *priv, int count, int dma_flags) { struct gntdev_grant_map *add; int i; add = kzalloc_obj(*add); if (NULL == add) return NULL; add->grants = kvmalloc_objs(add->grants[0], count); add->map_ops = kvmalloc_objs(add->map_ops[0], count); add->unmap_ops = kvmalloc_objs(add->unmap_ops[0], count); add->pages = kvzalloc_objs(add->pages[0], count); add->being_removed = kvzalloc_objs(add->being_removed[0], count); if (NULL == add->grants || NULL == add->map_ops || NULL == add->unmap_ops || NULL == add->pages || NULL == add->being_removed) goto err; if (xen_pv_domain()) { add->kmap_ops = kvmalloc_objs(add->kmap_ops[0], count); add->kunmap_ops = kvmalloc_objs(add->kunmap_ops[0], count); if (NULL == add->kmap_ops || NULL == add->kunmap_ops) goto err; } #ifdef CONFIG_XEN_GRANT_DMA_ALLOC add->dma_flags = dma_flags; /* * Check if this mapping is requested to be backed * by a DMA buffer. */ if (dma_flags & (GNTDEV_DMA_FLAG_WC | GNTDEV_DMA_FLAG_COHERENT)) { struct gnttab_dma_alloc_args args; add->frames = kvzalloc_objs(add->frames[0], count); if (!add->frames) goto err; /* Remember the device, so we can free DMA memory. */ add->dma_dev = priv->dma_dev; args.dev = priv->dma_dev; args.coherent = !!(dma_flags & GNTDEV_DMA_FLAG_COHERENT); args.nr_pages = count; args.pages = add->pages; args.frames = add->frames; if (gnttab_dma_alloc_pages(&args)) goto err; add->dma_vaddr = args.vaddr; add->dma_bus_addr = args.dev_bus_addr; } else #endif if (gnttab_alloc_pages(count, add->pages)) goto err; for (i = 0; i < count; i++) { add->grants[i].domid = DOMID_INVALID; add->grants[i].ref = INVALID_GRANT_REF; add->map_ops[i].handle = INVALID_GRANT_HANDLE; add->unmap_ops[i].handle = INVALID_GRANT_HANDLE; if (xen_pv_domain()) { add->kmap_ops[i].handle = INVALID_GRANT_HANDLE; add->kunmap_ops[i].handle = INVALID_GRANT_HANDLE; } } add->index = 0; add->count = count; refcount_set(&add->users, 1); return add; err: gntdev_free_map(add); return NULL; } void gntdev_add_map(struct gntdev_priv *priv, struct gntdev_grant_map *add) { struct gntdev_grant_map *map; list_for_each_entry(map, &priv->maps, next) { if (add->index + add->count < map->index) { list_add_tail(&add->next, &map->next); goto done; } add->index = map->index + map->count; } list_add_tail(&add->next, &priv->maps); done: gntdev_print_maps(priv, "[new]", add->index); } static struct gntdev_grant_map *gntdev_find_map_index(struct gntdev_priv *priv, int index, int count) { struct gntdev_grant_map *map; list_for_each_entry(map, &priv->maps, next) { if (map->index != index) continue; if (count && map->count != count) continue; return map; } return NULL; } void gntdev_put_map(struct gntdev_priv *priv, struct gntdev_grant_map *map) { if (!map) return; if (!refcount_dec_and_test(&map->users)) return; if (map->pages && !xen_pv_domain()) { /* * Increment the reference count. This ensures that the * subsequent call to unmap_grant_pages() will not wind up * re-entering itself. It *can* wind up calling * gntdev_put_map() recursively, but such calls will be with a * reference count greater than 1, so they will return before * this code is reached. The recursion depth is thus limited to * 1. Do NOT use refcount_inc() here, as it will detect that * the reference count is zero and WARN(). */ refcount_set(&map->users, 1); /* * Unmap the grants. This may or may not be asynchronous, so it * is possible that the reference count is 1 on return, but it * could also be greater than 1. */ unmap_grant_pages(map, 0, map->count); /* Check if the memory now needs to be freed */ if (!refcount_dec_and_test(&map->users)) return; /* * All pages have been returned to the hypervisor, so free the * map. */ } if (xen_pv_domain() && map->notifier_init) mmu_interval_notifier_remove(&map->notifier); if (map->notify.flags & UNMAP_NOTIFY_SEND_EVENT) { notify_remote_via_evtchn(map->notify.event); evtchn_put(map->notify.event); } gntdev_free_map(map); } /* ------------------------------------------------------------------ */ static int find_grant_ptes(pte_t *pte, unsigned long addr, void *data) { struct gntdev_grant_map *map = data; unsigned int pgnr = (addr - map->pages_vm_start) >> PAGE_SHIFT; int flags = map->flags | GNTMAP_application_map | GNTMAP_contains_pte | (1 << _GNTMAP_guest_avail0); u64 pte_maddr; BUG_ON(pgnr >= map->count); pte_maddr = arbitrary_virt_to_machine(pte).maddr; /* Note: this will perform a pte_mkspecial() through the hypercall. */ gnttab_set_map_op(&map->map_ops[pgnr], pte_maddr, flags, map->grants[pgnr].ref, map->grants[pgnr].domid); gnttab_set_unmap_op(&map->unmap_ops[pgnr], pte_maddr, flags, INVALID_GRANT_HANDLE); return 0; } int gntdev_map_grant_pages(struct gntdev_grant_map *map) { size_t alloced = 0; int i, err = 0; if (!xen_pv_domain()) { /* Note: it could already be mapped */ if (map->map_ops[0].handle != INVALID_GRANT_HANDLE) return 0; for (i = 0; i < map->count; i++) { unsigned long addr = (unsigned long) pfn_to_kaddr(page_to_pfn(map->pages[i])); gnttab_set_map_op(&map->map_ops[i], addr, map->flags, map->grants[i].ref, map->grants[i].domid); gnttab_set_unmap_op(&map->unmap_ops[i], addr, map->flags, INVALID_GRANT_HANDLE); } } else { /* * Setup the map_ops corresponding to the pte entries pointing * to the kernel linear addresses of the struct pages. * These ptes are completely different from the user ptes dealt * with find_grant_ptes. * Note that GNTMAP_device_map isn't needed here: The * dev_bus_addr output field gets consumed only from ->map_ops, * and by not requesting it when mapping we also avoid needing * to mirror dev_bus_addr into ->unmap_ops (and holding an extra * reference to the page in the hypervisor). */ unsigned int flags = (map->flags & ~GNTMAP_device_map) | GNTMAP_host_map; for (i = 0; i < map->count; i++) { unsigned long address = (unsigned long) pfn_to_kaddr(page_to_pfn(map->pages[i])); BUG_ON(PageHighMem(map->pages[i])); gnttab_set_map_op(&map->kmap_ops[i], address, flags, map->grants[i].ref, map->grants[i].domid); gnttab_set_unmap_op(&map->kunmap_ops[i], address, flags, INVALID_GRANT_HANDLE); } } pr_debug("map %d+%d\n", map->index, map->count); err = gnttab_map_refs(map->map_ops, map->kmap_ops, map->pages, map->count); for (i = 0; i < map->count; i++) { if (map->map_ops[i].status == GNTST_okay) { map->unmap_ops[i].handle = map->map_ops[i].handle; alloced++; } else if (!err) err = -EINVAL; if (map->flags & GNTMAP_device_map) map->unmap_ops[i].dev_bus_addr = map->map_ops[i].dev_bus_addr; if (xen_pv_domain()) { if (map->kmap_ops[i].status == GNTST_okay) { alloced++; map->kunmap_ops[i].handle = map->kmap_ops[i].handle; } else if (!err) err = -EINVAL; } } atomic_add(alloced, &map->live_grants); return err; } static void __unmap_grant_pages_done(int result, struct gntab_unmap_queue_data *data) { unsigned int i; struct gntdev_grant_map *map = data->data; unsigned int offset = data->unmap_ops - map->unmap_ops; int successful_unmaps = 0; int live_grants; for (i = 0; i < data->count; i++) { if (map->unmap_ops[offset + i].status == GNTST_okay && map->unmap_ops[offset + i].handle != INVALID_GRANT_HANDLE) successful_unmaps++; WARN_ON(map->unmap_ops[offset + i].status != GNTST_okay && map->unmap_ops[offset + i].handle != INVALID_GRANT_HANDLE); pr_debug("unmap handle=%d st=%d\n", map->unmap_ops[offset+i].handle, map->unmap_ops[offset+i].status); map->unmap_ops[offset+i].handle = INVALID_GRANT_HANDLE; if (xen_pv_domain()) { if (map->kunmap_ops[offset + i].status == GNTST_okay && map->kunmap_ops[offset + i].handle != INVALID_GRANT_HANDLE) successful_unmaps++; WARN_ON(map->kunmap_ops[offset + i].status != GNTST_okay && map->kunmap_ops[offset + i].handle != INVALID_GRANT_HANDLE); pr_debug("kunmap handle=%u st=%d\n", map->kunmap_ops[offset+i].handle, map->kunmap_ops[offset+i].status); map->kunmap_ops[offset+i].handle = INVALID_GRANT_HANDLE; } } /* * Decrease the live-grant counter. This must happen after the loop to * prevent premature reuse of the grants by gnttab_mmap(). */ live_grants = atomic_sub_return(successful_unmaps, &map->live_grants); if (WARN_ON(live_grants < 0)) pr_err("%s: live_grants became negative (%d) after unmapping %d pages!\n", __func__, live_grants, successful_unmaps); /* Release reference taken by __unmap_grant_pages */ gntdev_put_map(NULL, map); } static void __unmap_grant_pages(struct gntdev_grant_map *map, int offset, int pages) { if (map->notify.flags & UNMAP_NOTIFY_CLEAR_BYTE) { int pgno = (map->notify.addr >> PAGE_SHIFT); if (pgno >= offset && pgno < offset + pages) { /* No need for kmap, pages are in lowmem */ uint8_t *tmp = pfn_to_kaddr(page_to_pfn(map->pages[pgno])); tmp[map->notify.addr & (PAGE_SIZE-1)] = 0; map->notify.flags &= ~UNMAP_NOTIFY_CLEAR_BYTE; } } map->unmap_data.unmap_ops = map->unmap_ops + offset; map->unmap_data.kunmap_ops = xen_pv_domain() ? map->kunmap_ops + offset : NULL; map->unmap_data.pages = map->pages + offset; map->unmap_data.count = pages; map->unmap_data.done = __unmap_grant_pages_done; map->unmap_data.data = map; refcount_inc(&map->users); /* to keep map alive during async call below */ gnttab_unmap_refs_async(&map->unmap_data); } static void unmap_grant_pages(struct gntdev_grant_map *map, int offset, int pages) { int range; if (atomic_read(&map->live_grants) == 0) return; /* Nothing to do */ pr_debug("unmap %d+%d [%d+%d]\n", map->index, map->count, offset, pages); /* It is possible the requested range will have a "hole" where we * already unmapped some of the grants. Only unmap valid ranges. */ while (pages) { while (pages && map->being_removed[offset]) { offset++; pages--; } range = 0; while (range < pages) { if (map->being_removed[offset + range]) break; map->being_removed[offset + range] = true; range++; } if (range) __unmap_grant_pages(map, offset, range); offset += range; pages -= range; } }