| 1 | |
| 2 | * gntdev.c
|
| 3 | *
|
| 4 | * Device for accessing (in user-space) pages that have been granted by other
|
| 5 | * domains.
|
| 6 | *
|
| 7 | * Copyright (c) 2006-2007, D G Murray.
|
| 8 | * (c) 2009 Gerd Hoffmann <[email protected]>
|
| 9 | * (c) 2018 Oleksandr Andrushchenko, EPAM Systems Inc.
|
| 10 | *
|
| 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.
|
| 15 | *
|
| 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
|
| 19 |
|
| 20 |
|
| 21 | #undef DEBUG
|
| 22 |
|
| 23 | #define pr_fmt(fmt) "xen:" KBUILD_MODNAME ": " fmt
|
| 24 |
|
| 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>
|
| 30 | #include <linux/fs.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>
|
| 39 |
|
| 40 | #include <xen/xen.h>
|
| 41 | #include <xen/grant_table.h>
|
| 42 | #include <xen/balloon.h>
|
| 43 | #include <xen/gntdev.h>
|
| 44 | #include <xen/events.h>
|
| 45 | #include <xen/page.h>
|
| 46 | #include <asm/xen/hypervisor.h>
|
| 47 | #include <asm/xen/hypercall.h>
|
| 48 |
|
| 49 | #include "gntdev-common.h"
|
| 50 | #ifdef CONFIG_XEN_GNTDEV_DMABUF
|
| 51 | #include "gntdev-dmabuf.h"
|
| 52 | #endif
|
| 53 |
|
| 54 | MODULE_LICENSE("GPL");
|
| 55 | MODULE_AUTHOR("Derek G. Murray <[email protected]>, "
|
| 56 | "Gerd Hoffmann <[email protected]>");
|
| 57 | MODULE_DESCRIPTION("User-space granted page access driver");
|
| 58 |
|
| 59 | #define GNTDEV_COPY_BATCH 16
|
| 60 |
|
| 61 | struct gntdev_copy_batch {
|
| 62 | struct gnttab_copy ops[GNTDEV_COPY_BATCH];
|
| 63 | struct page *pages[GNTDEV_COPY_BATCH];
|
| 64 | s16 __user *status[GNTDEV_COPY_BATCH];
|
| 65 | unsigned int nr_ops;
|
| 66 | unsigned int nr_pages;
|
| 67 | bool writeable;
|
| 68 | struct gntdev_copy_batch *next;
|
| 69 | };
|
| 70 |
|
| 71 | static unsigned int limit = 64*1024;
|
| 72 | module_param(limit, uint, 0644);
|
| 73 | MODULE_PARM_DESC(limit,
|
| 74 | "Maximum number of grants that may be mapped by one mapping request");
|
| 75 |
|
| 76 | static void unmap_grant_pages(struct gntdev_grant_map *map,
|
| 77 | int offset, int pages);
|
| 78 |
|
| 79 | static struct miscdevice gntdev_miscdev;
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 | bool gntdev_test_page_count(unsigned int count)
|
| 84 | {
|
| 85 | return !count || count > limit;
|
| 86 | }
|
| 87 |
|
| 88 | static void gntdev_print_maps(struct gntdev_priv *priv,
|
| 89 | char *text, int text_index)
|
| 90 | {
|
| 91 | #ifdef DEBUG
|
| 92 | struct gntdev_grant_map *map;
|
| 93 |
|
| 94 | pr_debug("%s: maps list (priv %p)\n", __func__, priv);
|
| 95 | list_for_each_entry(map, &priv->maps, next)
|
| 96 | pr_debug(" index %2d, count %2d %s\n",
|
| 97 | map->index, map->count,
|
| 98 | map->index == text_index && text ? text : "");
|
| 99 | #endif
|
| 100 | }
|
| 101 |
|
| 102 | static void gntdev_free_map(struct gntdev_grant_map *map)
|
| 103 | {
|
| 104 | if (map == NULL)
|
| 105 | return;
|
| 106 |
|
| 107 | #ifdef CONFIG_XEN_GRANT_DMA_ALLOC
|
| 108 | if (map->dma_vaddr) {
|
| 109 | struct gnttab_dma_alloc_args args;
|
| 110 |
|
| 111 | args.dev = map->dma_dev;
|
| 112 | args.coherent = !!(map->dma_flags & GNTDEV_DMA_FLAG_COHERENT);
|
| 113 | args.nr_pages = map->count;
|
| 114 | args.pages = map->pages;
|
| 115 | args.frames = map->frames;
|
| 116 | args.vaddr = map->dma_vaddr;
|
| 117 | args.dev_bus_addr = map->dma_bus_addr;
|
| 118 |
|
| 119 | gnttab_dma_free_pages(&args);
|
| 120 | } else
|
| 121 | #endif
|
| 122 | if (map->pages)
|
| 123 | gnttab_free_pages(map->count, map->pages);
|
| 124 |
|
| 125 | #ifdef CONFIG_XEN_GRANT_DMA_ALLOC
|
| 126 | kvfree(map->frames);
|
| 127 | #endif
|
| 128 | kvfree(map->pages);
|
| 129 | kvfree(map->grants);
|
| 130 | kvfree(map->map_ops);
|
| 131 | kvfree(map->unmap_ops);
|
| 132 | kvfree(map->kmap_ops);
|
| 133 | kvfree(map->kunmap_ops);
|
| 134 | kvfree(map->being_removed);
|
| 135 | kfree(map);
|
| 136 | }
|
| 137 |
|
| 138 | struct gntdev_grant_map *gntdev_alloc_map(struct gntdev_priv *priv, int count,
|
| 139 | int dma_flags)
|
| 140 | {
|
| 141 | struct gntdev_grant_map *add;
|
| 142 | int i;
|
| 143 |
|
| 144 | add = kzalloc_obj(*add);
|
| 145 | if (NULL == add)
|
| 146 | return NULL;
|
| 147 |
|
| 148 | add->grants = kvmalloc_objs(add->grants[0], count);
|
| 149 | add->map_ops = kvmalloc_objs(add->map_ops[0], count);
|
| 150 | add->unmap_ops = kvmalloc_objs(add->unmap_ops[0], count);
|
| 151 | add->pages = kvzalloc_objs(add->pages[0], count);
|
| 152 | add->being_removed =
|
| 153 | kvzalloc_objs(add->being_removed[0], count);
|
| 154 | if (NULL == add->grants ||
|
| 155 | NULL == add->map_ops ||
|
| 156 | NULL == add->unmap_ops ||
|
| 157 | NULL == add->pages ||
|
| 158 | NULL == add->being_removed)
|
| 159 | goto err;
|
| 160 | if (xen_pv_domain()) {
|
| 161 | add->kmap_ops = kvmalloc_objs(add->kmap_ops[0], count);
|
| 162 | add->kunmap_ops = kvmalloc_objs(add->kunmap_ops[0], count);
|
| 163 | if (NULL == add->kmap_ops || NULL == add->kunmap_ops)
|
| 164 | goto err;
|
| 165 | }
|
| 166 |
|
| 167 | #ifdef CONFIG_XEN_GRANT_DMA_ALLOC
|
| 168 | add->dma_flags = dma_flags;
|
| 169 |
|
| 170 | |
| 171 | * Check if this mapping is requested to be backed
|
| 172 | * by a DMA buffer.
|
| 173 |
|
| 174 | if (dma_flags & (GNTDEV_DMA_FLAG_WC | GNTDEV_DMA_FLAG_COHERENT)) {
|
| 175 | struct gnttab_dma_alloc_args args;
|
| 176 |
|
| 177 | add->frames = kvzalloc_objs(add->frames[0], count);
|
| 178 | if (!add->frames)
|
| 179 | goto err;
|
| 180 |
|
| 181 |
|
| 182 | add->dma_dev = priv->dma_dev;
|
| 183 |
|
| 184 | args.dev = priv->dma_dev;
|
| 185 | args.coherent = !!(dma_flags & GNTDEV_DMA_FLAG_COHERENT);
|
| 186 | args.nr_pages = count;
|
| 187 | args.pages = add->pages;
|
| 188 | args.frames = add->frames;
|
| 189 |
|
| 190 | if (gnttab_dma_alloc_pages(&args))
|
| 191 | goto err;
|
| 192 |
|
| 193 | add->dma_vaddr = args.vaddr;
|
| 194 | add->dma_bus_addr = args.dev_bus_addr;
|
| 195 | } else
|
| 196 | #endif
|
| 197 | if (gnttab_alloc_pages(count, add->pages))
|
| 198 | goto err;
|
| 199 |
|
| 200 | for (i = 0; i < count; i++) {
|
| 201 | add->grants[i].domid = DOMID_INVALID;
|
| 202 | add->grants[i].ref = INVALID_GRANT_REF;
|
| 203 | add->map_ops[i].handle = INVALID_GRANT_HANDLE;
|
| 204 | add->unmap_ops[i].handle = INVALID_GRANT_HANDLE;
|
| 205 | if (xen_pv_domain()) {
|
| 206 | add->kmap_ops[i].handle = INVALID_GRANT_HANDLE;
|
| 207 | add->kunmap_ops[i].handle = INVALID_GRANT_HANDLE;
|
| 208 | }
|
| 209 | }
|
| 210 |
|
| 211 | add->index = 0;
|
| 212 | add->count = count;
|
| 213 | refcount_set(&add->users, 1);
|
| 214 |
|
| 215 | return add;
|
| 216 |
|
| 217 | err:
|
| 218 | gntdev_free_map(add);
|
| 219 | return NULL;
|
| 220 | }
|
| 221 |
|
| 222 | void gntdev_add_map(struct gntdev_priv *priv, struct gntdev_grant_map *add)
|
| 223 | {
|
| 224 | struct gntdev_grant_map *map;
|
| 225 |
|
| 226 | list_for_each_entry(map, &priv->maps, next) {
|
| 227 | if (add->index + add->count < map->index) {
|
| 228 | list_add_tail(&add->next, &map->next);
|
| 229 | goto done;
|
| 230 | }
|
| 231 | add->index = map->index + map->count;
|
| 232 | }
|
| 233 | list_add_tail(&add->next, &priv->maps);
|
| 234 |
|
| 235 | done:
|
| 236 | gntdev_print_maps(priv, "[new]", add->index);
|
| 237 | }
|
| 238 |
|
| 239 | static struct gntdev_grant_map *gntdev_find_map_index(struct gntdev_priv *priv,
|
| 240 | int index, int count)
|
| 241 | {
|
| 242 | struct gntdev_grant_map *map;
|
| 243 |
|
| 244 | list_for_each_entry(map, &priv->maps, next) {
|
| 245 | if (map->index != index)
|
| 246 | continue;
|
| 247 | if (count && map->count != count)
|
| 248 | continue;
|
| 249 | return map;
|
| 250 | }
|
| 251 | return NULL;
|
| 252 | }
|
| 253 |
|
| 254 | void gntdev_put_map(struct gntdev_priv *priv, struct gntdev_grant_map *map)
|
| 255 | {
|
| 256 | if (!map)
|
| 257 | return;
|
| 258 |
|
| 259 | if (!refcount_dec_and_test(&map->users))
|
| 260 | return;
|
| 261 |
|
| 262 | if (map->pages && !xen_pv_domain()) {
|
| 263 | |
| 264 | * Increment the reference count. This ensures that the
|
| 265 | * subsequent call to unmap_grant_pages() will not wind up
|
| 266 | * re-entering itself. It *can* wind up calling
|
| 267 | * gntdev_put_map() recursively, but such calls will be with a
|
| 268 | * reference count greater than 1, so they will return before
|
| 269 | * this code is reached. The recursion depth is thus limited to
|
| 270 | * 1. Do NOT use refcount_inc() here, as it will detect that
|
| 271 | * the reference count is zero and WARN().
|
| 272 |
|
| 273 | refcount_set(&map->users, 1);
|
| 274 |
|
| 275 | |
| 276 | * Unmap the grants. This may or may not be asynchronous, so it
|
| 277 | * is possible that the reference count is 1 on return, but it
|
| 278 | * could also be greater than 1.
|
| 279 |
|
| 280 | unmap_grant_pages(map, 0, map->count);
|
| 281 |
|
| 282 |
|
| 283 | if (!refcount_dec_and_test(&map->users))
|
| 284 | return;
|
| 285 |
|
| 286 | |
| 287 | * All pages have been returned to the hypervisor, so free the
|
| 288 | * map.
|
| 289 |
|
| 290 | }
|
| 291 |
|
| 292 | if (xen_pv_domain() && map->notifier_init)
|
| 293 | mmu_interval_notifier_remove(&map->notifier);
|
| 294 |
|
| 295 | if (map->notify.flags & UNMAP_NOTIFY_SEND_EVENT) {
|
| 296 | notify_remote_via_evtchn(map->notify.event);
|
| 297 | evtchn_put(map->notify.event);
|
| 298 | }
|
| 299 | gntdev_free_map(map);
|
| 300 | }
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 | static int find_grant_ptes(pte_t *pte, unsigned long addr, void *data)
|
| 305 | {
|
| 306 | struct gntdev_grant_map *map = data;
|
| 307 | unsigned int pgnr = (addr - map->pages_vm_start) >> PAGE_SHIFT;
|
| 308 | int flags = map->flags | GNTMAP_application_map | GNTMAP_contains_pte |
|
| 309 | (1 << _GNTMAP_guest_avail0);
|
| 310 | u64 pte_maddr;
|
| 311 |
|
| 312 | BUG_ON(pgnr >= map->count);
|
| 313 | pte_maddr = arbitrary_virt_to_machine(pte).maddr;
|
| 314 |
|
| 315 |
|
| 316 | gnttab_set_map_op(&map->map_ops[pgnr], pte_maddr, flags,
|
| 317 | map->grants[pgnr].ref,
|
| 318 | map->grants[pgnr].domid);
|
| 319 | gnttab_set_unmap_op(&map->unmap_ops[pgnr], pte_maddr, flags,
|
| 320 | INVALID_GRANT_HANDLE);
|
| 321 | return 0;
|
| 322 | }
|
| 323 |
|
| 324 | int gntdev_map_grant_pages(struct gntdev_grant_map *map)
|
| 325 | {
|
| 326 | size_t alloced = 0;
|
| 327 | int i, err = 0;
|
| 328 |
|
| 329 | if (!xen_pv_domain()) {
|
| 330 |
|
| 331 | if (map->map_ops[0].handle != INVALID_GRANT_HANDLE)
|
| 332 | return 0;
|
| 333 | for (i = 0; i < map->count; i++) {
|
| 334 | unsigned long addr = (unsigned long)
|
| 335 | pfn_to_kaddr(page_to_pfn(map->pages[i]));
|
| 336 | gnttab_set_map_op(&map->map_ops[i], addr, map->flags,
|
| 337 | map->grants[i].ref,
|
| 338 | map->grants[i].domid);
|
| 339 | gnttab_set_unmap_op(&map->unmap_ops[i], addr,
|
| 340 | map->flags, INVALID_GRANT_HANDLE);
|
| 341 | }
|
| 342 | } else {
|
| 343 | |
| 344 | * Setup the map_ops corresponding to the pte entries pointing
|
| 345 | * to the kernel linear addresses of the struct pages.
|
| 346 | * These ptes are completely different from the user ptes dealt
|
| 347 | * with find_grant_ptes.
|
| 348 | * Note that GNTMAP_device_map isn't needed here: The
|
| 349 | * dev_bus_addr output field gets consumed only from ->map_ops,
|
| 350 | * and by not requesting it when mapping we also avoid needing
|
| 351 | * to mirror dev_bus_addr into ->unmap_ops (and holding an extra
|
| 352 | * reference to the page in the hypervisor).
|
| 353 |
|
| 354 | unsigned int flags = (map->flags & ~GNTMAP_device_map) |
|
| 355 | GNTMAP_host_map;
|
| 356 |
|
| 357 | for (i = 0; i < map->count; i++) {
|
| 358 | unsigned long address = (unsigned long)
|
| 359 | pfn_to_kaddr(page_to_pfn(map->pages[i]));
|
| 360 | BUG_ON(PageHighMem(map->pages[i]));
|
| 361 |
|
| 362 | gnttab_set_map_op(&map->kmap_ops[i], address, flags,
|
| 363 | map->grants[i].ref,
|
| 364 | map->grants[i].domid);
|
| 365 | gnttab_set_unmap_op(&map->kunmap_ops[i], address,
|
| 366 | flags, INVALID_GRANT_HANDLE);
|
| 367 | }
|
| 368 | }
|
| 369 |
|
| 370 | pr_debug("map %d+%d\n", map->index, map->count);
|
| 371 | err = gnttab_map_refs(map->map_ops, map->kmap_ops, map->pages,
|
| 372 | map->count);
|
| 373 |
|
| 374 | for (i = 0; i < map->count; i++) {
|
| 375 | if (map->map_ops[i].status == GNTST_okay) {
|
| 376 | map->unmap_ops[i].handle = map->map_ops[i].handle;
|
| 377 | alloced++;
|
| 378 | } else if (!err)
|
| 379 | err = -EINVAL;
|
| 380 |
|
| 381 | if (map->flags & GNTMAP_device_map)
|
| 382 | map->unmap_ops[i].dev_bus_addr = map->map_ops[i].dev_bus_addr;
|
| 383 |
|
| 384 | if (xen_pv_domain()) {
|
| 385 | if (map->kmap_ops[i].status == GNTST_okay) {
|
| 386 | alloced++;
|
| 387 | map->kunmap_ops[i].handle = map->kmap_ops[i].handle;
|
| 388 | } else if (!err)
|
| 389 | err = -EINVAL;
|
| 390 | }
|
| 391 | }
|
| 392 | atomic_add(alloced, &map->live_grants);
|
| 393 | return err;
|
| 394 | }
|
| 395 |
|
| 396 | static void __unmap_grant_pages_done(int result,
|
| 397 | struct gntab_unmap_queue_data *data)
|
| 398 | {
|
| 399 | unsigned int i;
|
| 400 | struct gntdev_grant_map *map = data->data;
|
| 401 | unsigned int offset = data->unmap_ops - map->unmap_ops;
|
| 402 | int successful_unmaps = 0;
|
| 403 | int live_grants;
|
| 404 |
|
| 405 | for (i = 0; i < data->count; i++) {
|
| 406 | if (map->unmap_ops[offset + i].status == GNTST_okay &&
|
| 407 | map->unmap_ops[offset + i].handle != INVALID_GRANT_HANDLE)
|
| 408 | successful_unmaps++;
|
| 409 |
|
| 410 | WARN_ON(map->unmap_ops[offset + i].status != GNTST_okay &&
|
| 411 | map->unmap_ops[offset + i].handle != INVALID_GRANT_HANDLE);
|
| 412 | pr_debug("unmap handle=%d st=%d\n",
|
| 413 | map->unmap_ops[offset+i].handle,
|
| 414 | map->unmap_ops[offset+i].status);
|
| 415 | map->unmap_ops[offset+i].handle = INVALID_GRANT_HANDLE;
|
| 416 | if (xen_pv_domain()) {
|
| 417 | if (map->kunmap_ops[offset + i].status == GNTST_okay &&
|
| 418 | map->kunmap_ops[offset + i].handle != INVALID_GRANT_HANDLE)
|
| 419 | successful_unmaps++;
|
| 420 |
|
| 421 | WARN_ON(map->kunmap_ops[offset + i].status != GNTST_okay &&
|
| 422 | map->kunmap_ops[offset + i].handle != INVALID_GRANT_HANDLE);
|
| 423 | pr_debug("kunmap handle=%u st=%d\n",
|
| 424 | map->kunmap_ops[offset+i].handle,
|
| 425 | map->kunmap_ops[offset+i].status);
|
| 426 | map->kunmap_ops[offset+i].handle = INVALID_GRANT_HANDLE;
|
| 427 | }
|
| 428 | }
|
| 429 |
|
| 430 | |
| 431 | * Decrease the live-grant counter. This must happen after the loop to
|
| 432 | * prevent premature reuse of the grants by gnttab_mmap().
|
| 433 |
|
| 434 | live_grants = atomic_sub_return(successful_unmaps, &map->live_grants);
|
| 435 | if (WARN_ON(live_grants < 0))
|
| 436 | pr_err("%s: live_grants became negative (%d) after unmapping %d pages!\n",
|
| 437 | __func__, live_grants, successful_unmaps);
|
| 438 |
|
| 439 |
|
| 440 | gntdev_put_map(NULL, map);
|
| 441 | }
|
| 442 |
|
| 443 | static void __unmap_grant_pages(struct gntdev_grant_map *map, int offset,
|
| 444 | int pages)
|
| 445 | {
|
| 446 | if (map->notify.flags & UNMAP_NOTIFY_CLEAR_BYTE) {
|
| 447 | int pgno = (map->notify.addr >> PAGE_SHIFT);
|
| 448 |
|
| 449 | if (pgno >= offset && pgno < offset + pages) {
|
| 450 |
|
| 451 | uint8_t *tmp = pfn_to_kaddr(page_to_pfn(map->pages[pgno]));
|
| 452 |
|
| 453 | tmp[map->notify.addr & (PAGE_SIZE-1)] = 0;
|
| 454 | map->notify.flags &= ~UNMAP_NOTIFY_CLEAR_BYTE;
|
| 455 | }
|
| 456 | }
|
| 457 |
|
| 458 | map->unmap_data.unmap_ops = map->unmap_ops + offset;
|
| 459 | map->unmap_data.kunmap_ops = xen_pv_domain() ? map->kunmap_ops + offset : NULL;
|
| 460 | map->unmap_data.pages = map->pages + offset;
|
| 461 | map->unmap_data.count = pages;
|
| 462 | map->unmap_data.done = __unmap_grant_pages_done;
|
| 463 | map->unmap_data.data = map;
|
| 464 | refcount_inc(&map->users);
|
| 465 |
|
| 466 | gnttab_unmap_refs_async(&map->unmap_data);
|
| 467 | }
|
| 468 |
|
| 469 | static void unmap_grant_pages(struct gntdev_grant_map *map, int offset,
|
| 470 | int pages)
|
| 471 | {
|
| 472 | int range;
|
| 473 |
|
| 474 | if (atomic_read(&map->live_grants) == 0)
|
| 475 | return;
|
| 476 |
|
| 477 | pr_debug("unmap %d+%d [%d+%d]\n", map->index, map->count, offset, pages);
|
| 478 |
|
| 479 | |
| 480 | * already unmapped some of the grants. Only unmap valid ranges.
|
| 481 |
|
| 482 | while (pages) {
|
| 483 | while (pages && map->being_removed[offset]) {
|
| 484 | offset++;
|
| 485 | pages--;
|
| 486 | }
|
| 487 | range = 0;
|
| 488 | while (range < pages) {
|
| 489 | if (map->being_removed[offset + range])
|
| 490 | break;
|
| 491 | map->being_removed[offset + range] = true;
|
| 492 | range++;
|
| 493 | }
|
| 494 | if (range)
|
| 495 | __unmap_grant_pages(map, offset, range);
|
| 496 | offset += range;
|
| 497 | pages -= range;
|
| 498 | }
|
| 499 | }
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 | static void gntdev_vma_open(struct vm_area_struct *vma)
|
| 504 | {
|
| 505 | struct gntdev_grant_map *map = vma->vm_private_data;
|
| 506 |
|
| 507 | pr_debug("gntdev_vma_open %p\n", vma);
|
| 508 | refcount_inc(&map->users);
|
| 509 | }
|
| 510 |
|
| 511 | static void gntdev_vma_close(struct vm_area_struct *vma)
|
| 512 | {
|
| 513 | struct gntdev_grant_map *map = vma->vm_private_data;
|
| 514 | struct file *file = vma->vm_file;
|
| 515 | struct gntdev_priv *priv = file->private_data;
|
| 516 |
|
| 517 | pr_debug("gntdev_vma_close %p\n", vma);
|
| 518 |
|
| 519 | vma->vm_private_data = NULL;
|
| 520 | gntdev_put_map(priv, map);
|
| 521 | }
|
| 522 |
|
| 523 | static struct page *gntdev_vma_find_normal_page(struct vm_area_struct *vma,
|
| 524 | unsigned long addr)
|
| 525 | {
|
| 526 | struct gntdev_grant_map *map = vma->vm_private_data;
|
| 527 |
|
| 528 | return map->pages[(addr - map->pages_vm_start) >> PAGE_SHIFT];
|
| 529 | }
|
| 530 |
|
| 531 | static const struct vm_operations_struct gntdev_vmops = {
|
| 532 | .open = gntdev_vma_open,
|
| 533 | .close = gntdev_vma_close,
|
| 534 | .find_normal_page = gntdev_vma_find_normal_page,
|
| 535 | };
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 | static bool gntdev_invalidate(struct mmu_interval_notifier *mn,
|
| 540 | const struct mmu_notifier_range *range,
|
| 541 | unsigned long cur_seq)
|
| 542 | {
|
| 543 | struct gntdev_grant_map *map =
|
| 544 | container_of(mn, struct gntdev_grant_map, notifier);
|
| 545 | unsigned long mstart, mend;
|
| 546 | unsigned long map_start, map_end;
|
| 547 |
|
| 548 | if (!mmu_notifier_range_blockable(range))
|
| 549 | return false;
|
| 550 |
|
| 551 | map_start = map->pages_vm_start;
|
| 552 | map_end = map->pages_vm_start + (map->count << PAGE_SHIFT);
|
| 553 |
|
| 554 | |
| 555 | * If the VMA is split or otherwise changed the notifier is not
|
| 556 | * updated, but we don't want to process VA's outside the modified
|
| 557 | * VMA. FIXME: It would be much more understandable to just prevent
|
| 558 | * modifying the VMA in the first place.
|
| 559 |
|
| 560 | if (map_start >= range->end || map_end <= range->start)
|
| 561 | return true;
|
| 562 |
|
| 563 | mstart = max(range->start, map_start);
|
| 564 | mend = min(range->end, map_end);
|
| 565 | pr_debug("map %d+%d (%lx %lx), range %lx %lx, mrange %lx %lx\n",
|
| 566 | map->index, map->count, map_start, map_end,
|
| 567 | range->start, range->end, mstart, mend);
|
| 568 | unmap_grant_pages(map, (mstart - map_start) >> PAGE_SHIFT,
|
| 569 | (mend - mstart) >> PAGE_SHIFT);
|
| 570 |
|
| 571 | return true;
|
| 572 | }
|
| 573 |
|
| 574 | static const struct mmu_interval_notifier_ops gntdev_mmu_ops = {
|
| 575 | .invalidate = gntdev_invalidate,
|
| 576 | };
|
| 577 |
|
| 578 |
|
| 579 |
|
| 580 | static int gntdev_open(struct inode *inode, struct file *flip)
|
| 581 | {
|
| 582 | struct gntdev_priv *priv;
|
| 583 |
|
| 584 | priv = kzalloc_obj(*priv);
|
| 585 | if (!priv)
|
| 586 | return -ENOMEM;
|
| 587 |
|
| 588 | INIT_LIST_HEAD(&priv->maps);
|
| 589 | mutex_init(&priv->lock);
|
| 590 |
|
| 591 | mutex_init(&priv->batch_lock);
|
| 592 |
|
| 593 | #ifdef CONFIG_XEN_GNTDEV_DMABUF
|
| 594 | priv->dmabuf_priv = gntdev_dmabuf_init(flip);
|
| 595 | if (IS_ERR(priv->dmabuf_priv)) {
|
| 596 | int ret = PTR_ERR(priv->dmabuf_priv);
|
| 597 |
|
| 598 | kfree(priv);
|
| 599 | return ret;
|
| 600 | }
|
| 601 | #endif
|
| 602 |
|
| 603 | flip->private_data = priv;
|
| 604 | #ifdef CONFIG_XEN_GRANT_DMA_ALLOC
|
| 605 | priv->dma_dev = gntdev_miscdev.this_device;
|
| 606 | dma_coerce_mask_and_coherent(priv->dma_dev, DMA_BIT_MASK(64));
|
| 607 | #endif
|
| 608 | pr_debug("priv %p\n", priv);
|
| 609 |
|
| 610 | return 0;
|
| 611 | }
|
| 612 |
|
| 613 | static int gntdev_release(struct inode *inode, struct file *flip)
|
| 614 | {
|
| 615 | struct gntdev_priv *priv = flip->private_data;
|
| 616 | struct gntdev_grant_map *map;
|
| 617 | struct gntdev_copy_batch *batch;
|
| 618 |
|
| 619 | pr_debug("priv %p\n", priv);
|
| 620 |
|
| 621 | mutex_lock(&priv->lock);
|
| 622 | while (!list_empty(&priv->maps)) {
|
| 623 | map = list_entry(priv->maps.next,
|
| 624 | struct gntdev_grant_map, next);
|
| 625 | list_del(&map->next);
|
| 626 | gntdev_put_map(NULL , map);
|
| 627 | }
|
| 628 | mutex_unlock(&priv->lock);
|
| 629 |
|
| 630 | mutex_lock(&priv->batch_lock);
|
| 631 | while (priv->batch) {
|
| 632 | batch = priv->batch;
|
| 633 | priv->batch = batch->next;
|
| 634 | kfree(batch);
|
| 635 | }
|
| 636 | mutex_unlock(&priv->batch_lock);
|
| 637 |
|
| 638 | #ifdef CONFIG_XEN_GNTDEV_DMABUF
|
| 639 | gntdev_dmabuf_fini(priv->dmabuf_priv);
|
| 640 | #endif
|
| 641 |
|
| 642 | kfree(priv);
|
| 643 | return 0;
|
| 644 | }
|
| 645 |
|
| 646 | static long gntdev_ioctl_map_grant_ref(struct gntdev_priv *priv,
|
| 647 | struct ioctl_gntdev_map_grant_ref __user *u)
|
| 648 | {
|
| 649 | struct ioctl_gntdev_map_grant_ref op;
|
| 650 | struct gntdev_grant_map *map;
|
| 651 | int err;
|
| 652 |
|
| 653 | if (copy_from_user(&op, u, sizeof(op)) != 0)
|
| 654 | return -EFAULT;
|
| 655 | pr_debug("priv %p, add %d\n", priv, op.count);
|
| 656 | if (unlikely(gntdev_test_page_count(op.count)))
|
| 657 | return -EINVAL;
|
| 658 |
|
| 659 | err = -ENOMEM;
|
| 660 | map = gntdev_alloc_map(priv, op.count, 0 );
|
| 661 | if (!map)
|
| 662 | return err;
|
| 663 |
|
| 664 | if (copy_from_user(map->grants, &u->refs,
|
| 665 | sizeof(map->grants[0]) * op.count) != 0) {
|
| 666 | gntdev_put_map(NULL, map);
|
| 667 | return -EFAULT;
|
| 668 | }
|
| 669 |
|
| 670 | mutex_lock(&priv->lock);
|
| 671 | gntdev_add_map(priv, map);
|
| 672 | op.index = map->index << PAGE_SHIFT;
|
| 673 |
|
| 674 | if (copy_to_user(u, &op, sizeof(op)) != 0) {
|
| 675 | list_del(&map->next);
|
| 676 | mutex_unlock(&priv->lock);
|
| 677 | gntdev_put_map(priv, map);
|
| 678 | return -EFAULT;
|
| 679 | }
|
| 680 |
|
| 681 | mutex_unlock(&priv->lock);
|
| 682 | return 0;
|
| 683 | }
|
| 684 |
|
| 685 | static long gntdev_ioctl_unmap_grant_ref(struct gntdev_priv *priv,
|
| 686 | struct ioctl_gntdev_unmap_grant_ref __user *u)
|
| 687 | {
|
| 688 | struct ioctl_gntdev_unmap_grant_ref op;
|
| 689 | struct gntdev_grant_map *map;
|
| 690 | int err = -ENOENT;
|
| 691 |
|
| 692 | if (copy_from_user(&op, u, sizeof(op)) != 0)
|
| 693 | return -EFAULT;
|
| 694 | pr_debug("priv %p, del %d+%d\n", priv, (int)op.index, (int)op.count);
|
| 695 |
|
| 696 | mutex_lock(&priv->lock);
|
| 697 | map = gntdev_find_map_index(priv, op.index >> PAGE_SHIFT, op.count);
|
| 698 | if (map) {
|
| 699 | list_del(&map->next);
|
| 700 | err = 0;
|
| 701 | }
|
| 702 | mutex_unlock(&priv->lock);
|
| 703 | if (map)
|
| 704 | gntdev_put_map(priv, map);
|
| 705 | return err;
|
| 706 | }
|
| 707 |
|
| 708 | static long gntdev_ioctl_get_offset_for_vaddr(struct gntdev_priv *priv,
|
| 709 | struct ioctl_gntdev_get_offset_for_vaddr __user *u)
|
| 710 | {
|
| 711 | struct ioctl_gntdev_get_offset_for_vaddr op;
|
| 712 | struct vm_area_struct *vma;
|
| 713 | struct gntdev_grant_map *map;
|
| 714 | int rv = -EINVAL;
|
| 715 |
|
| 716 | if (copy_from_user(&op, u, sizeof(op)) != 0)
|
| 717 | return -EFAULT;
|
| 718 | pr_debug("priv %p, offset for vaddr %lx\n", priv, (unsigned long)op.vaddr);
|
| 719 |
|
| 720 | mmap_read_lock(current->mm);
|
| 721 | vma = find_vma(current->mm, op.vaddr);
|
| 722 | if (!vma || vma->vm_ops != &gntdev_vmops)
|
| 723 | goto out_unlock;
|
| 724 |
|
| 725 | map = vma->vm_private_data;
|
| 726 | if (!map)
|
| 727 | goto out_unlock;
|
| 728 |
|
| 729 | op.offset = map->index << PAGE_SHIFT;
|
| 730 | op.count = map->count;
|
| 731 | rv = 0;
|
| 732 |
|
| 733 | out_unlock:
|
| 734 | mmap_read_unlock(current->mm);
|
| 735 |
|
| 736 | if (rv == 0 && copy_to_user(u, &op, sizeof(op)) != 0)
|
| 737 | return -EFAULT;
|
| 738 | return rv;
|
| 739 | }
|
| 740 |
|
| 741 | static long gntdev_ioctl_notify(struct gntdev_priv *priv, void __user *u)
|
| 742 | {
|
| 743 | struct ioctl_gntdev_unmap_notify op;
|
| 744 | struct gntdev_grant_map *map;
|
| 745 | int rc;
|
| 746 | int out_flags;
|
| 747 | evtchn_port_t out_event;
|
| 748 |
|
| 749 | if (copy_from_user(&op, u, sizeof(op)))
|
| 750 | return -EFAULT;
|
| 751 |
|
| 752 | if (op.action & ~(UNMAP_NOTIFY_CLEAR_BYTE|UNMAP_NOTIFY_SEND_EVENT))
|
| 753 | return -EINVAL;
|
| 754 |
|
| 755 | |
| 756 | * to send the notify before releasing the reference we may already have
|
| 757 | * (if someone has called this ioctl twice). This is required so that
|
| 758 | * it is possible to change the clear_byte part of the notification
|
| 759 | * without disturbing the event channel part, which may now be the last
|
| 760 | * reference to that event channel.
|
| 761 |
|
| 762 | if (op.action & UNMAP_NOTIFY_SEND_EVENT) {
|
| 763 | if (evtchn_get(op.event_channel_port))
|
| 764 | return -EINVAL;
|
| 765 | }
|
| 766 |
|
| 767 | out_flags = op.action;
|
| 768 | out_event = op.event_channel_port;
|
| 769 |
|
| 770 | mutex_lock(&priv->lock);
|
| 771 |
|
| 772 | list_for_each_entry(map, &priv->maps, next) {
|
| 773 | uint64_t begin = map->index << PAGE_SHIFT;
|
| 774 | uint64_t end = (map->index + map->count) << PAGE_SHIFT;
|
| 775 | if (op.index >= begin && op.index < end)
|
| 776 | goto found;
|
| 777 | }
|
| 778 | rc = -ENOENT;
|
| 779 | goto unlock_out;
|
| 780 |
|
| 781 | found:
|
| 782 | if ((op.action & UNMAP_NOTIFY_CLEAR_BYTE) &&
|
| 783 | (map->flags & GNTMAP_readonly)) {
|
| 784 | rc = -EINVAL;
|
| 785 | goto unlock_out;
|
| 786 | }
|
| 787 |
|
| 788 | out_flags = map->notify.flags;
|
| 789 | out_event = map->notify.event;
|
| 790 |
|
| 791 | map->notify.flags = op.action;
|
| 792 | map->notify.addr = op.index - (map->index << PAGE_SHIFT);
|
| 793 | map->notify.event = op.event_channel_port;
|
| 794 |
|
| 795 | rc = 0;
|
| 796 |
|
| 797 | unlock_out:
|
| 798 | mutex_unlock(&priv->lock);
|
| 799 |
|
| 800 |
|
| 801 | if (out_flags & UNMAP_NOTIFY_SEND_EVENT)
|
| 802 | evtchn_put(out_event);
|
| 803 |
|
| 804 | return rc;
|
| 805 | }
|
| 806 |
|
| 807 | static int gntdev_get_page(struct gntdev_copy_batch *batch, void __user *virt,
|
| 808 | unsigned long *gfn)
|
| 809 | {
|
| 810 | unsigned long addr = (unsigned long)virt;
|
| 811 | struct page *page;
|
| 812 | unsigned long xen_pfn;
|
| 813 | int ret;
|
| 814 |
|
| 815 | ret = pin_user_pages_fast(addr, 1, batch->writeable ? FOLL_WRITE : 0, &page);
|
| 816 | if (ret < 0)
|
| 817 | return ret;
|
| 818 |
|
| 819 | batch->pages[batch->nr_pages++] = page;
|
| 820 |
|
| 821 | xen_pfn = page_to_xen_pfn(page) + XEN_PFN_DOWN(addr & ~PAGE_MASK);
|
| 822 | *gfn = pfn_to_gfn(xen_pfn);
|
| 823 |
|
| 824 | return 0;
|
| 825 | }
|
| 826 |
|
| 827 | static void gntdev_put_pages(struct gntdev_copy_batch *batch)
|
| 828 | {
|
| 829 | unpin_user_pages_dirty_lock(batch->pages, batch->nr_pages, batch->writeable);
|
| 830 | batch->nr_pages = 0;
|
| 831 | batch->writeable = false;
|
| 832 | }
|
| 833 |
|
| 834 | static int gntdev_copy(struct gntdev_copy_batch *batch)
|
| 835 | {
|
| 836 | unsigned int i;
|
| 837 |
|
| 838 | gnttab_batch_copy(batch->ops, batch->nr_ops);
|
| 839 | gntdev_put_pages(batch);
|
| 840 |
|
| 841 | |
| 842 | * For each completed op, update the status if the op failed
|
| 843 | * and all previous ops for the segment were successful.
|
| 844 |
|
| 845 | for (i = 0; i < batch->nr_ops; i++) {
|
| 846 | s16 status = batch->ops[i].status;
|
| 847 | s16 old_status;
|
| 848 |
|
| 849 | if (status == GNTST_okay)
|
| 850 | continue;
|
| 851 |
|
| 852 | if (__get_user(old_status, batch->status[i]))
|
| 853 | return -EFAULT;
|
| 854 |
|
| 855 | if (old_status != GNTST_okay)
|
| 856 | continue;
|
| 857 |
|
| 858 | if (__put_user(status, batch->status[i]))
|
| 859 | return -EFAULT;
|
| 860 | }
|
| 861 |
|
| 862 | batch->nr_ops = 0;
|
| 863 | return 0;
|
| 864 | }
|
| 865 |
|
| 866 | static int gntdev_grant_copy_seg(struct gntdev_copy_batch *batch,
|
| 867 | struct gntdev_grant_copy_segment *seg,
|
| 868 | s16 __user *status)
|
| 869 | {
|
| 870 | uint16_t copied = 0;
|
| 871 |
|
| 872 | |
| 873 | * Disallow local -> local copies since there is only space in
|
| 874 | * batch->pages for one page per-op and this would be a very
|
| 875 | * expensive memcpy().
|
| 876 |
|
| 877 | if (!(seg->flags & (GNTCOPY_source_gref | GNTCOPY_dest_gref)))
|
| 878 | return -EINVAL;
|
| 879 |
|
| 880 |
|
| 881 | if (seg->flags & GNTCOPY_source_gref) {
|
| 882 | if (seg->source.foreign.offset + seg->len > XEN_PAGE_SIZE)
|
| 883 | return -EINVAL;
|
| 884 | }
|
| 885 | if (seg->flags & GNTCOPY_dest_gref) {
|
| 886 | if (seg->dest.foreign.offset + seg->len > XEN_PAGE_SIZE)
|
| 887 | return -EINVAL;
|
| 888 | }
|
| 889 |
|
| 890 | if (put_user(GNTST_okay, status))
|
| 891 | return -EFAULT;
|
| 892 |
|
| 893 | while (copied < seg->len) {
|
| 894 | struct gnttab_copy *op;
|
| 895 | void __user *virt;
|
| 896 | size_t len, off;
|
| 897 | unsigned long gfn;
|
| 898 | int ret;
|
| 899 |
|
| 900 | if (batch->nr_ops >= GNTDEV_COPY_BATCH) {
|
| 901 | ret = gntdev_copy(batch);
|
| 902 | if (ret < 0)
|
| 903 | return ret;
|
| 904 | }
|
| 905 |
|
| 906 | len = seg->len - copied;
|
| 907 |
|
| 908 | op = &batch->ops[batch->nr_ops];
|
| 909 | op->flags = 0;
|
| 910 |
|
| 911 | if (seg->flags & GNTCOPY_source_gref) {
|
| 912 | op->source.u.ref = seg->source.foreign.ref;
|
| 913 | op->source.domid = seg->source.foreign.domid;
|
| 914 | op->source.offset = seg->source.foreign.offset + copied;
|
| 915 | op->flags |= GNTCOPY_source_gref;
|
| 916 | } else {
|
| 917 | virt = seg->source.virt + copied;
|
| 918 | off = (unsigned long)virt & ~XEN_PAGE_MASK;
|
| 919 | len = min(len, (size_t)XEN_PAGE_SIZE - off);
|
| 920 | batch->writeable = false;
|
| 921 |
|
| 922 | ret = gntdev_get_page(batch, virt, &gfn);
|
| 923 | if (ret < 0)
|
| 924 | return ret;
|
| 925 |
|
| 926 | op->source.u.gmfn = gfn;
|
| 927 | op->source.domid = DOMID_SELF;
|
| 928 | op->source.offset = off;
|
| 929 | }
|
| 930 |
|
| 931 | if (seg->flags & GNTCOPY_dest_gref) {
|
| 932 | op->dest.u.ref = seg->dest.foreign.ref;
|
| 933 | op->dest.domid = seg->dest.foreign.domid;
|
| 934 | op->dest.offset = seg->dest.foreign.offset + copied;
|
| 935 | op->flags |= GNTCOPY_dest_gref;
|
| 936 | } else {
|
| 937 | virt = seg->dest.virt + copied;
|
| 938 | off = (unsigned long)virt & ~XEN_PAGE_MASK;
|
| 939 | len = min(len, (size_t)XEN_PAGE_SIZE - off);
|
| 940 | batch->writeable = true;
|
| 941 |
|
| 942 | ret = gntdev_get_page(batch, virt, &gfn);
|
| 943 | if (ret < 0)
|
| 944 | return ret;
|
| 945 |
|
| 946 | op->dest.u.gmfn = gfn;
|
| 947 | op->dest.domid = DOMID_SELF;
|
| 948 | op->dest.offset = off;
|
| 949 | }
|
| 950 |
|
| 951 | op->len = len;
|
| 952 | copied += len;
|
| 953 |
|
| 954 | batch->status[batch->nr_ops] = status;
|
| 955 | batch->nr_ops++;
|
| 956 | }
|
| 957 |
|
| 958 | return 0;
|
| 959 | }
|
| 960 |
|
| 961 | static long gntdev_ioctl_grant_copy(struct gntdev_priv *priv, void __user *u)
|
| 962 | {
|
| 963 | struct ioctl_gntdev_grant_copy copy;
|
| 964 | struct gntdev_copy_batch *batch;
|
| 965 | unsigned int i;
|
| 966 | int ret = 0;
|
| 967 |
|
| 968 | if (copy_from_user(©, u, sizeof(copy)))
|
| 969 | return -EFAULT;
|
| 970 |
|
| 971 | mutex_lock(&priv->batch_lock);
|
| 972 | if (!priv->batch) {
|
| 973 | batch = kmalloc_obj(*batch);
|
| 974 | } else {
|
| 975 | batch = priv->batch;
|
| 976 | priv->batch = batch->next;
|
| 977 | }
|
| 978 | mutex_unlock(&priv->batch_lock);
|
| 979 | if (!batch)
|
| 980 | return -ENOMEM;
|
| 981 |
|
| 982 | batch->nr_ops = 0;
|
| 983 | batch->nr_pages = 0;
|
| 984 |
|
| 985 | for (i = 0; i < copy.count; i++) {
|
| 986 | struct gntdev_grant_copy_segment seg;
|
| 987 |
|
| 988 | if (copy_from_user(&seg, ©.segments[i], sizeof(seg))) {
|
| 989 | ret = -EFAULT;
|
| 990 | gntdev_put_pages(batch);
|
| 991 | goto out;
|
| 992 | }
|
| 993 |
|
| 994 | ret = gntdev_grant_copy_seg(batch, &seg, ©.segments[i].status);
|
| 995 | if (ret < 0) {
|
| 996 | gntdev_put_pages(batch);
|
| 997 | goto out;
|
| 998 | }
|
| 999 |
|
| 1000 | cond_resched();
|
| 1001 | }
|
| 1002 | if (batch->nr_ops)
|
| 1003 | ret = gntdev_copy(batch);
|
| 1004 |
|
| 1005 | out:
|
| 1006 | mutex_lock(&priv->batch_lock);
|
| 1007 | batch->next = priv->batch;
|
| 1008 | priv->batch = batch;
|
| 1009 | mutex_unlock(&priv->batch_lock);
|
| 1010 |
|
| 1011 | return ret;
|
| 1012 | }
|
| 1013 |
|
| 1014 | static long gntdev_ioctl(struct file *flip,
|
| 1015 | unsigned int cmd, unsigned long arg)
|
| 1016 | {
|
| 1017 | struct gntdev_priv *priv = flip->private_data;
|
| 1018 | void __user *ptr = (void __user *)arg;
|
| 1019 |
|
| 1020 | switch (cmd) {
|
| 1021 | case IOCTL_GNTDEV_MAP_GRANT_REF:
|
| 1022 | return gntdev_ioctl_map_grant_ref(priv, ptr);
|
| 1023 |
|
| 1024 | case IOCTL_GNTDEV_UNMAP_GRANT_REF:
|
| 1025 | return gntdev_ioctl_unmap_grant_ref(priv, ptr);
|
| 1026 |
|
| 1027 | case IOCTL_GNTDEV_GET_OFFSET_FOR_VADDR:
|
| 1028 | return gntdev_ioctl_get_offset_for_vaddr(priv, ptr);
|
| 1029 |
|
| 1030 | case IOCTL_GNTDEV_SET_UNMAP_NOTIFY:
|
| 1031 | return gntdev_ioctl_notify(priv, ptr);
|
| 1032 |
|
| 1033 | case IOCTL_GNTDEV_GRANT_COPY:
|
| 1034 | return gntdev_ioctl_grant_copy(priv, ptr);
|
| 1035 |
|
| 1036 | #ifdef CONFIG_XEN_GNTDEV_DMABUF
|
| 1037 | case IOCTL_GNTDEV_DMABUF_EXP_FROM_REFS:
|
| 1038 | return gntdev_ioctl_dmabuf_exp_from_refs(priv, ptr);
|
| 1039 |
|
| 1040 | case IOCTL_GNTDEV_DMABUF_EXP_WAIT_RELEASED:
|
| 1041 | return gntdev_ioctl_dmabuf_exp_wait_released(priv, ptr);
|
| 1042 |
|
| 1043 | case IOCTL_GNTDEV_DMABUF_IMP_TO_REFS:
|
| 1044 | return gntdev_ioctl_dmabuf_imp_to_refs(priv, ptr);
|
| 1045 |
|
| 1046 | case IOCTL_GNTDEV_DMABUF_IMP_RELEASE:
|
| 1047 | return gntdev_ioctl_dmabuf_imp_release(priv, ptr);
|
| 1048 | #endif
|
| 1049 |
|
| 1050 | default:
|
| 1051 | pr_debug("priv %p, unknown cmd %x\n", priv, cmd);
|
| 1052 | return -ENOIOCTLCMD;
|
| 1053 | }
|
| 1054 |
|
| 1055 | return 0;
|
| 1056 | }
|
| 1057 |
|
| 1058 | static int gntdev_mmap(struct file *flip, struct vm_area_struct *vma)
|
| 1059 | {
|
| 1060 | struct gntdev_priv *priv = flip->private_data;
|
| 1061 | int index = vma->vm_pgoff;
|
| 1062 | int count = vma_pages(vma);
|
| 1063 | struct gntdev_grant_map *map;
|
| 1064 | int err = -EINVAL;
|
| 1065 |
|
| 1066 | if ((vma->vm_flags & VM_WRITE) && !(vma->vm_flags & VM_SHARED))
|
| 1067 | return -EINVAL;
|
| 1068 |
|
| 1069 | |
| 1070 | * Private mappings of foreign grant pages must never become
|
| 1071 | * writable: they would take the COW path on granted pages.
|
| 1072 |
|
| 1073 | if (!(vma->vm_flags & VM_SHARED))
|
| 1074 | vm_flags_clear(vma, VM_MAYWRITE);
|
| 1075 |
|
| 1076 | pr_debug("map %d+%d at %lx (pgoff %lx)\n",
|
| 1077 | index, count, vma->vm_start, vma->vm_pgoff);
|
| 1078 |
|
| 1079 | mutex_lock(&priv->lock);
|
| 1080 | map = gntdev_find_map_index(priv, index, count);
|
| 1081 | if (!map)
|
| 1082 | goto unlock_out;
|
| 1083 | if (!atomic_add_unless(&map->in_use, 1, 1))
|
| 1084 | goto unlock_out;
|
| 1085 |
|
| 1086 | refcount_inc(&map->users);
|
| 1087 |
|
| 1088 | vma->vm_ops = &gntdev_vmops;
|
| 1089 |
|
| 1090 | vm_flags_set(vma, VM_DONTEXPAND | VM_DONTDUMP | VM_MIXEDMAP);
|
| 1091 |
|
| 1092 | if (xen_pv_domain())
|
| 1093 | vm_flags_set(vma, VM_DONTCOPY);
|
| 1094 |
|
| 1095 | vma->vm_private_data = map;
|
| 1096 | if (map->flags) {
|
| 1097 | if ((vma->vm_flags & VM_WRITE) &&
|
| 1098 | (map->flags & GNTMAP_readonly))
|
| 1099 | goto out_unlock_put;
|
| 1100 | } else {
|
| 1101 | map->flags = GNTMAP_host_map;
|
| 1102 | if (!(vma->vm_flags & VM_WRITE))
|
| 1103 | map->flags |= GNTMAP_readonly;
|
| 1104 | }
|
| 1105 |
|
| 1106 | map->pages_vm_start = vma->vm_start;
|
| 1107 |
|
| 1108 | if (xen_pv_domain()) {
|
| 1109 | err = mmu_interval_notifier_insert_locked(
|
| 1110 | &map->notifier, vma->vm_mm, vma->vm_start,
|
| 1111 | vma->vm_end - vma->vm_start, &gntdev_mmu_ops);
|
| 1112 | if (err)
|
| 1113 | goto out_unlock_put;
|
| 1114 |
|
| 1115 | map->notifier_init = true;
|
| 1116 | }
|
| 1117 | mutex_unlock(&priv->lock);
|
| 1118 |
|
| 1119 | if (xen_pv_domain()) {
|
| 1120 | |
| 1121 | * gntdev takes the address of the PTE in find_grant_ptes() and
|
| 1122 | * passes it to the hypervisor in gntdev_map_grant_pages(). The
|
| 1123 | * purpose of the notifier is to prevent the hypervisor pointer
|
| 1124 | * to the PTE from going stale.
|
| 1125 | *
|
| 1126 | * Since this vma's mappings can't be touched without the
|
| 1127 | * mmap_lock, and we are holding it now, there is no need for
|
| 1128 | * the notifier_range locking pattern.
|
| 1129 |
|
| 1130 | mmu_interval_read_begin(&map->notifier);
|
| 1131 |
|
| 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;
|
| 1138 | }
|
| 1139 | }
|
| 1140 |
|
| 1141 | err = gntdev_map_grant_pages(map);
|
| 1142 | if (err)
|
| 1143 | goto out_put_map;
|
| 1144 |
|
| 1145 | if (!xen_pv_domain()) {
|
| 1146 | err = vm_map_pages_zero(vma, map->pages, map->count);
|
| 1147 | if (err)
|
| 1148 | goto out_put_map;
|
| 1149 | }
|
| 1150 |
|
| 1151 | return 0;
|
| 1152 |
|
| 1153 | unlock_out:
|
| 1154 | mutex_unlock(&priv->lock);
|
| 1155 | return err;
|
| 1156 |
|
| 1157 | out_unlock_put:
|
| 1158 | mutex_unlock(&priv->lock);
|
| 1159 | out_put_map:
|
| 1160 | if (xen_pv_domain())
|
| 1161 | unmap_grant_pages(map, 0, map->count);
|
| 1162 | gntdev_put_map(priv, map);
|
| 1163 | return err;
|
| 1164 | }
|
| 1165 |
|
| 1166 | static const struct file_operations gntdev_fops = {
|
| 1167 | .owner = THIS_MODULE,
|
| 1168 | .open = gntdev_open,
|
| 1169 | .release = gntdev_release,
|
| 1170 | .mmap = gntdev_mmap,
|
| 1171 | .unlocked_ioctl = gntdev_ioctl
|
| 1172 | };
|
| 1173 |
|
| 1174 | static struct miscdevice gntdev_miscdev = {
|
| 1175 | .minor = MISC_DYNAMIC_MINOR,
|
| 1176 | .name = "xen/gntdev",
|
| 1177 | .fops = &gntdev_fops,
|
| 1178 | };
|
| 1179 |
|
| 1180 |
|
| 1181 |
|
| 1182 | static int __init gntdev_init(void)
|
| 1183 | {
|
| 1184 | int err;
|
| 1185 |
|
| 1186 | if (!xen_domain())
|
| 1187 | return -ENODEV;
|
| 1188 |
|
| 1189 | err = misc_register(&gntdev_miscdev);
|
| 1190 | if (err != 0) {
|
| 1191 | pr_err("Could not register gntdev device\n");
|
| 1192 | return err;
|
| 1193 | }
|
| 1194 | return 0;
|
| 1195 | }
|
| 1196 |
|
| 1197 | static void __exit gntdev_exit(void)
|
| 1198 | {
|
| 1199 | misc_deregister(&gntdev_miscdev);
|
| 1200 | }
|
| 1201 |
|
| 1202 | module_init(gntdev_init);
|
| 1203 | module_exit(gntdev_exit);
|
| 1204 |
|
| 1205 |
|