linux/drivers/xen/gntdev.c
<<
>>
Prefs
   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 <kraxel@redhat.com>
   9 *
  10 * This program is distributed in the hope that it will be useful,
  11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13 * GNU General Public License for more details.
  14 *
  15 * You should have received a copy of the GNU General Public License
  16 * along with this program; if not, write to the Free Software
  17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  18 */
  19
  20#undef DEBUG
  21
  22#define pr_fmt(fmt) "xen:" KBUILD_MODNAME ": " fmt
  23
  24#include <linux/module.h>
  25#include <linux/kernel.h>
  26#include <linux/init.h>
  27#include <linux/miscdevice.h>
  28#include <linux/fs.h>
  29#include <linux/mm.h>
  30#include <linux/mman.h>
  31#include <linux/mmu_notifier.h>
  32#include <linux/types.h>
  33#include <linux/uaccess.h>
  34#include <linux/sched.h>
  35#include <linux/sched/mm.h>
  36#include <linux/spinlock.h>
  37#include <linux/slab.h>
  38#include <linux/highmem.h>
  39#include <linux/refcount.h>
  40
  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>
  49
  50MODULE_LICENSE("GPL");
  51MODULE_AUTHOR("Derek G. Murray <Derek.Murray@cl.cam.ac.uk>, "
  52              "Gerd Hoffmann <kraxel@redhat.com>");
  53MODULE_DESCRIPTION("User-space granted page access driver");
  54
  55static int limit = 1024*1024;
  56module_param(limit, int, 0644);
  57MODULE_PARM_DESC(limit, "Maximum number of grants that may be mapped by "
  58                "the gntdev device");
  59
  60static atomic_t pages_mapped = ATOMIC_INIT(0);
  61
  62static int use_ptemod;
  63#define populate_freeable_maps use_ptemod
  64
  65struct gntdev_priv {
  66        /* maps with visible offsets in the file descriptor */
  67        struct list_head maps;
  68        /* maps that are not visible; will be freed on munmap.
  69         * Only populated if populate_freeable_maps == 1 */
  70        struct list_head freeable_maps;
  71        /* lock protects maps and freeable_maps */
  72        struct mutex lock;
  73        struct mm_struct *mm;
  74        struct mmu_notifier mn;
  75};
  76
  77struct unmap_notify {
  78        int flags;
  79        /* Address relative to the start of the grant_map */
  80        int addr;
  81        int event;
  82};
  83
  84struct grant_map {
  85        struct list_head next;
  86        struct vm_area_struct *vma;
  87        int index;
  88        int count;
  89        int flags;
  90        refcount_t users;
  91        struct unmap_notify notify;
  92        struct ioctl_gntdev_grant_ref *grants;
  93        struct gnttab_map_grant_ref   *map_ops;
  94        struct gnttab_unmap_grant_ref *unmap_ops;
  95        struct gnttab_map_grant_ref   *kmap_ops;
  96        struct gnttab_unmap_grant_ref *kunmap_ops;
  97        struct page **pages;
  98        unsigned long pages_vm_start;
  99};
 100
 101static int unmap_grant_pages(struct grant_map *map, int offset, int pages);
 102
 103/* ------------------------------------------------------------------ */
 104
 105static void gntdev_print_maps(struct gntdev_priv *priv,
 106                              char *text, int text_index)
 107{
 108#ifdef DEBUG
 109        struct grant_map *map;
 110
 111        pr_debug("%s: maps list (priv %p)\n", __func__, priv);
 112        list_for_each_entry(map, &priv->maps, next)
 113                pr_debug("  index %2d, count %2d %s\n",
 114                       map->index, map->count,
 115                       map->index == text_index && text ? text : "");
 116#endif
 117}
 118
 119static void gntdev_free_map(struct grant_map *map)
 120{
 121        if (map == NULL)
 122                return;
 123
 124        if (map->pages)
 125                gnttab_free_pages(map->count, map->pages);
 126        kfree(map->pages);
 127        kfree(map->grants);
 128        kfree(map->map_ops);
 129        kfree(map->unmap_ops);
 130        kfree(map->kmap_ops);
 131        kfree(map->kunmap_ops);
 132        kfree(map);
 133}
 134
 135static struct grant_map *gntdev_alloc_map(struct gntdev_priv *priv, int count)
 136{
 137        struct grant_map *add;
 138        int i;
 139
 140        add = kzalloc(sizeof(struct grant_map), GFP_KERNEL);
 141        if (NULL == add)
 142                return NULL;
 143
 144        add->grants    = kcalloc(count, sizeof(add->grants[0]), GFP_KERNEL);
 145        add->map_ops   = kcalloc(count, sizeof(add->map_ops[0]), GFP_KERNEL);
 146        add->unmap_ops = kcalloc(count, sizeof(add->unmap_ops[0]), GFP_KERNEL);
 147        add->kmap_ops  = kcalloc(count, sizeof(add->kmap_ops[0]), GFP_KERNEL);
 148        add->kunmap_ops = kcalloc(count, sizeof(add->kunmap_ops[0]), GFP_KERNEL);
 149        add->pages     = kcalloc(count, sizeof(add->pages[0]), GFP_KERNEL);
 150        if (NULL == add->grants    ||
 151            NULL == add->map_ops   ||
 152            NULL == add->unmap_ops ||
 153            NULL == add->kmap_ops  ||
 154            NULL == add->kunmap_ops ||
 155            NULL == add->pages)
 156                goto err;
 157
 158        if (gnttab_alloc_pages(count, add->pages))
 159                goto err;
 160
 161        for (i = 0; i < count; i++) {
 162                add->map_ops[i].handle = -1;
 163                add->unmap_ops[i].handle = -1;
 164                add->kmap_ops[i].handle = -1;
 165                add->kunmap_ops[i].handle = -1;
 166        }
 167
 168        add->index = 0;
 169        add->count = count;
 170        refcount_set(&add->users, 1);
 171
 172        return add;
 173
 174err:
 175        gntdev_free_map(add);
 176        return NULL;
 177}
 178
 179static void gntdev_add_map(struct gntdev_priv *priv, struct grant_map *add)
 180{
 181        struct grant_map *map;
 182
 183        list_for_each_entry(map, &priv->maps, next) {
 184                if (add->index + add->count < map->index) {
 185                        list_add_tail(&add->next, &map->next);
 186                        goto done;
 187                }
 188                add->index = map->index + map->count;
 189        }
 190        list_add_tail(&add->next, &priv->maps);
 191
 192done:
 193        gntdev_print_maps(priv, "[new]", add->index);
 194}
 195
 196static struct grant_map *gntdev_find_map_index(struct gntdev_priv *priv,
 197                int index, int count)
 198{
 199        struct grant_map *map;
 200
 201        list_for_each_entry(map, &priv->maps, next) {
 202                if (map->index != index)
 203                        continue;
 204                if (count && map->count != count)
 205                        continue;
 206                return map;
 207        }
 208        return NULL;
 209}
 210
 211static void gntdev_put_map(struct gntdev_priv *priv, struct grant_map *map)
 212{
 213        if (!map)
 214                return;
 215
 216        if (!refcount_dec_and_test(&map->users))
 217                return;
 218
 219        atomic_sub(map->count, &pages_mapped);
 220
 221        if (map->notify.flags & UNMAP_NOTIFY_SEND_EVENT) {
 222                notify_remote_via_evtchn(map->notify.event);
 223                evtchn_put(map->notify.event);
 224        }
 225
 226        if (populate_freeable_maps && priv) {
 227                mutex_lock(&priv->lock);
 228                list_del(&map->next);
 229                mutex_unlock(&priv->lock);
 230        }
 231
 232        if (map->pages && !use_ptemod)
 233                unmap_grant_pages(map, 0, map->count);
 234        gntdev_free_map(map);
 235}
 236
 237/* ------------------------------------------------------------------ */
 238
 239static int find_grant_ptes(pte_t *pte, unsigned long addr, void *data)
 240{
 241        struct grant_map *map = data;
 242        unsigned int pgnr = (addr - map->vma->vm_start) >> PAGE_SHIFT;
 243        int flags = map->flags | GNTMAP_application_map | GNTMAP_contains_pte;
 244        u64 pte_maddr;
 245
 246        BUG_ON(pgnr >= map->count);
 247        pte_maddr = arbitrary_virt_to_machine(pte).maddr;
 248
 249        /*
 250         * Set the PTE as special to force get_user_pages_fast() fall
 251         * back to the slow path.  If this is not supported as part of
 252         * the grant map, it will be done afterwards.
 253         */
 254        if (xen_feature(XENFEAT_gnttab_map_avail_bits))
 255                flags |= (1 << _GNTMAP_guest_avail0);
 256
 257        gnttab_set_map_op(&map->map_ops[pgnr], pte_maddr, flags,
 258                          map->grants[pgnr].ref,
 259                          map->grants[pgnr].domid);
 260        gnttab_set_unmap_op(&map->unmap_ops[pgnr], pte_maddr, flags,
 261                            -1 /* handle */);
 262        return 0;
 263}
 264
 265#ifdef CONFIG_X86
 266static int set_grant_ptes_as_special(pte_t *pte, unsigned long addr, void *data)
 267{
 268        set_pte_at(current->mm, addr, pte, pte_mkspecial(*pte));
 269        return 0;
 270}
 271#endif
 272
 273static int map_grant_pages(struct grant_map *map)
 274{
 275        int i, err = 0;
 276
 277        if (!use_ptemod) {
 278                /* Note: it could already be mapped */
 279                if (map->map_ops[0].handle != -1)
 280                        return 0;
 281                for (i = 0; i < map->count; i++) {
 282                        unsigned long addr = (unsigned long)
 283                                pfn_to_kaddr(page_to_pfn(map->pages[i]));
 284                        gnttab_set_map_op(&map->map_ops[i], addr, map->flags,
 285                                map->grants[i].ref,
 286                                map->grants[i].domid);
 287                        gnttab_set_unmap_op(&map->unmap_ops[i], addr,
 288                                map->flags, -1 /* handle */);
 289                }
 290        } else {
 291                /*
 292                 * Setup the map_ops corresponding to the pte entries pointing
 293                 * to the kernel linear addresses of the struct pages.
 294                 * These ptes are completely different from the user ptes dealt
 295                 * with find_grant_ptes.
 296                 */
 297                for (i = 0; i < map->count; i++) {
 298                        unsigned long address = (unsigned long)
 299                                pfn_to_kaddr(page_to_pfn(map->pages[i]));
 300                        BUG_ON(PageHighMem(map->pages[i]));
 301
 302                        gnttab_set_map_op(&map->kmap_ops[i], address,
 303                                map->flags | GNTMAP_host_map,
 304                                map->grants[i].ref,
 305                                map->grants[i].domid);
 306                        gnttab_set_unmap_op(&map->kunmap_ops[i], address,
 307                                map->flags | GNTMAP_host_map, -1);
 308                }
 309        }
 310
 311        pr_debug("map %d+%d\n", map->index, map->count);
 312        err = gnttab_map_refs(map->map_ops, use_ptemod ? map->kmap_ops : NULL,
 313                        map->pages, map->count);
 314        if (err)
 315                return err;
 316
 317        for (i = 0; i < map->count; i++) {
 318                if (map->map_ops[i].status) {
 319                        err = -EINVAL;
 320                        continue;
 321                }
 322
 323                map->unmap_ops[i].handle = map->map_ops[i].handle;
 324                if (use_ptemod)
 325                        map->kunmap_ops[i].handle = map->kmap_ops[i].handle;
 326        }
 327        return err;
 328}
 329
 330static int __unmap_grant_pages(struct grant_map *map, int offset, int pages)
 331{
 332        int i, err = 0;
 333        struct gntab_unmap_queue_data unmap_data;
 334
 335        if (map->notify.flags & UNMAP_NOTIFY_CLEAR_BYTE) {
 336                int pgno = (map->notify.addr >> PAGE_SHIFT);
 337                if (pgno >= offset && pgno < offset + pages) {
 338                        /* No need for kmap, pages are in lowmem */
 339                        uint8_t *tmp = pfn_to_kaddr(page_to_pfn(map->pages[pgno]));
 340                        tmp[map->notify.addr & (PAGE_SIZE-1)] = 0;
 341                        map->notify.flags &= ~UNMAP_NOTIFY_CLEAR_BYTE;
 342                }
 343        }
 344
 345        unmap_data.unmap_ops = map->unmap_ops + offset;
 346        unmap_data.kunmap_ops = use_ptemod ? map->kunmap_ops + offset : NULL;
 347        unmap_data.pages = map->pages + offset;
 348        unmap_data.count = pages;
 349
 350        err = gnttab_unmap_refs_sync(&unmap_data);
 351        if (err)
 352                return err;
 353
 354        for (i = 0; i < pages; i++) {
 355                if (map->unmap_ops[offset+i].status)
 356                        err = -EINVAL;
 357                pr_debug("unmap handle=%d st=%d\n",
 358                        map->unmap_ops[offset+i].handle,
 359                        map->unmap_ops[offset+i].status);
 360                map->unmap_ops[offset+i].handle = -1;
 361        }
 362        return err;
 363}
 364
 365static int unmap_grant_pages(struct grant_map *map, int offset, int pages)
 366{
 367        int range, err = 0;
 368
 369        pr_debug("unmap %d+%d [%d+%d]\n", map->index, map->count, offset, pages);
 370
 371        /* It is possible the requested range will have a "hole" where we
 372         * already unmapped some of the grants. Only unmap valid ranges.
 373         */
 374        while (pages && !err) {
 375                while (pages && map->unmap_ops[offset].handle == -1) {
 376                        offset++;
 377                        pages--;
 378                }
 379                range = 0;
 380                while (range < pages) {
 381                        if (map->unmap_ops[offset+range].handle == -1)
 382                                break;
 383                        range++;
 384                }
 385                err = __unmap_grant_pages(map, offset, range);
 386                offset += range;
 387                pages -= range;
 388        }
 389
 390        return err;
 391}
 392
 393/* ------------------------------------------------------------------ */
 394
 395static void gntdev_vma_open(struct vm_area_struct *vma)
 396{
 397        struct grant_map *map = vma->vm_private_data;
 398
 399        pr_debug("gntdev_vma_open %p\n", vma);
 400        refcount_inc(&map->users);
 401}
 402
 403static void gntdev_vma_close(struct vm_area_struct *vma)
 404{
 405        struct grant_map *map = vma->vm_private_data;
 406        struct file *file = vma->vm_file;
 407        struct gntdev_priv *priv = file->private_data;
 408
 409        pr_debug("gntdev_vma_close %p\n", vma);
 410        if (use_ptemod) {
 411                /* It is possible that an mmu notifier could be running
 412                 * concurrently, so take priv->lock to ensure that the vma won't
 413                 * vanishing during the unmap_grant_pages call, since we will
 414                 * spin here until that completes. Such a concurrent call will
 415                 * not do any unmapping, since that has been done prior to
 416                 * closing the vma, but it may still iterate the unmap_ops list.
 417                 */
 418                mutex_lock(&priv->lock);
 419                map->vma = NULL;
 420                mutex_unlock(&priv->lock);
 421        }
 422        vma->vm_private_data = NULL;
 423        gntdev_put_map(priv, map);
 424}
 425
 426static struct page *gntdev_vma_find_special_page(struct vm_area_struct *vma,
 427                                                 unsigned long addr)
 428{
 429        struct grant_map *map = vma->vm_private_data;
 430
 431        return map->pages[(addr - map->pages_vm_start) >> PAGE_SHIFT];
 432}
 433
 434static const struct vm_operations_struct gntdev_vmops = {
 435        .open = gntdev_vma_open,
 436        .close = gntdev_vma_close,
 437        .find_special_page = gntdev_vma_find_special_page,
 438};
 439
 440/* ------------------------------------------------------------------ */
 441
 442static void unmap_if_in_range(struct grant_map *map,
 443                              unsigned long start, unsigned long end)
 444{
 445        unsigned long mstart, mend;
 446        int err;
 447
 448        if (!map->vma)
 449                return;
 450        if (map->vma->vm_start >= end)
 451                return;
 452        if (map->vma->vm_end <= start)
 453                return;
 454        mstart = max(start, map->vma->vm_start);
 455        mend   = min(end,   map->vma->vm_end);
 456        pr_debug("map %d+%d (%lx %lx), range %lx %lx, mrange %lx %lx\n",
 457                        map->index, map->count,
 458                        map->vma->vm_start, map->vma->vm_end,
 459                        start, end, mstart, mend);
 460        err = unmap_grant_pages(map,
 461                                (mstart - map->vma->vm_start) >> PAGE_SHIFT,
 462                                (mend - mstart) >> PAGE_SHIFT);
 463        WARN_ON(err);
 464}
 465
 466static void mn_invl_range_start(struct mmu_notifier *mn,
 467                                struct mm_struct *mm,
 468                                unsigned long start, unsigned long end)
 469{
 470        struct gntdev_priv *priv = container_of(mn, struct gntdev_priv, mn);
 471        struct grant_map *map;
 472
 473        mutex_lock(&priv->lock);
 474        list_for_each_entry(map, &priv->maps, next) {
 475                unmap_if_in_range(map, start, end);
 476        }
 477        list_for_each_entry(map, &priv->freeable_maps, next) {
 478                unmap_if_in_range(map, start, end);
 479        }
 480        mutex_unlock(&priv->lock);
 481}
 482
 483static void mn_release(struct mmu_notifier *mn,
 484                       struct mm_struct *mm)
 485{
 486        struct gntdev_priv *priv = container_of(mn, struct gntdev_priv, mn);
 487        struct grant_map *map;
 488        int err;
 489
 490        mutex_lock(&priv->lock);
 491        list_for_each_entry(map, &priv->maps, next) {
 492                if (!map->vma)
 493                        continue;
 494                pr_debug("map %d+%d (%lx %lx)\n",
 495                                map->index, map->count,
 496                                map->vma->vm_start, map->vma->vm_end);
 497                err = unmap_grant_pages(map, /* offset */ 0, map->count);
 498                WARN_ON(err);
 499        }
 500        list_for_each_entry(map, &priv->freeable_maps, next) {
 501                if (!map->vma)
 502                        continue;
 503                pr_debug("map %d+%d (%lx %lx)\n",
 504                                map->index, map->count,
 505                                map->vma->vm_start, map->vma->vm_end);
 506                err = unmap_grant_pages(map, /* offset */ 0, map->count);
 507                WARN_ON(err);
 508        }
 509        mutex_unlock(&priv->lock);
 510}
 511
 512static const struct mmu_notifier_ops gntdev_mmu_ops = {
 513        .release                = mn_release,
 514        .invalidate_range_start = mn_invl_range_start,
 515};
 516
 517/* ------------------------------------------------------------------ */
 518
 519static int gntdev_open(struct inode *inode, struct file *flip)
 520{
 521        struct gntdev_priv *priv;
 522        int ret = 0;
 523
 524        priv = kzalloc(sizeof(*priv), GFP_KERNEL);
 525        if (!priv)
 526                return -ENOMEM;
 527
 528        INIT_LIST_HEAD(&priv->maps);
 529        INIT_LIST_HEAD(&priv->freeable_maps);
 530        mutex_init(&priv->lock);
 531
 532        if (use_ptemod) {
 533                priv->mm = get_task_mm(current);
 534                if (!priv->mm) {
 535                        kfree(priv);
 536                        return -ENOMEM;
 537                }
 538                priv->mn.ops = &gntdev_mmu_ops;
 539                ret = mmu_notifier_register(&priv->mn, priv->mm);
 540                mmput(priv->mm);
 541        }
 542
 543        if (ret) {
 544                kfree(priv);
 545                return ret;
 546        }
 547
 548        flip->private_data = priv;
 549        pr_debug("priv %p\n", priv);
 550
 551        return 0;
 552}
 553
 554static int gntdev_release(struct inode *inode, struct file *flip)
 555{
 556        struct gntdev_priv *priv = flip->private_data;
 557        struct grant_map *map;
 558
 559        pr_debug("priv %p\n", priv);
 560
 561        mutex_lock(&priv->lock);
 562        while (!list_empty(&priv->maps)) {
 563                map = list_entry(priv->maps.next, struct grant_map, next);
 564                list_del(&map->next);
 565                gntdev_put_map(NULL /* already removed */, map);
 566        }
 567        WARN_ON(!list_empty(&priv->freeable_maps));
 568        mutex_unlock(&priv->lock);
 569
 570        if (use_ptemod)
 571                mmu_notifier_unregister(&priv->mn, priv->mm);
 572        kfree(priv);
 573        return 0;
 574}
 575
 576static long gntdev_ioctl_map_grant_ref(struct gntdev_priv *priv,
 577                                       struct ioctl_gntdev_map_grant_ref __user *u)
 578{
 579        struct ioctl_gntdev_map_grant_ref op;
 580        struct grant_map *map;
 581        int err;
 582
 583        if (copy_from_user(&op, u, sizeof(op)) != 0)
 584                return -EFAULT;
 585        pr_debug("priv %p, add %d\n", priv, op.count);
 586        if (unlikely(op.count <= 0))
 587                return -EINVAL;
 588
 589        err = -ENOMEM;
 590        map = gntdev_alloc_map(priv, op.count);
 591        if (!map)
 592                return err;
 593
 594        if (unlikely(atomic_add_return(op.count, &pages_mapped) > limit)) {
 595                pr_debug("can't map: over limit\n");
 596                gntdev_put_map(NULL, map);
 597                return err;
 598        }
 599
 600        if (copy_from_user(map->grants, &u->refs,
 601                           sizeof(map->grants[0]) * op.count) != 0) {
 602                gntdev_put_map(NULL, map);
 603                return -EFAULT;
 604        }
 605
 606        mutex_lock(&priv->lock);
 607        gntdev_add_map(priv, map);
 608        op.index = map->index << PAGE_SHIFT;
 609        mutex_unlock(&priv->lock);
 610
 611        if (copy_to_user(u, &op, sizeof(op)) != 0)
 612                return -EFAULT;
 613
 614        return 0;
 615}
 616
 617static long gntdev_ioctl_unmap_grant_ref(struct gntdev_priv *priv,
 618                                         struct ioctl_gntdev_unmap_grant_ref __user *u)
 619{
 620        struct ioctl_gntdev_unmap_grant_ref op;
 621        struct grant_map *map;
 622        int err = -ENOENT;
 623
 624        if (copy_from_user(&op, u, sizeof(op)) != 0)
 625                return -EFAULT;
 626        pr_debug("priv %p, del %d+%d\n", priv, (int)op.index, (int)op.count);
 627
 628        mutex_lock(&priv->lock);
 629        map = gntdev_find_map_index(priv, op.index >> PAGE_SHIFT, op.count);
 630        if (map) {
 631                list_del(&map->next);
 632                if (populate_freeable_maps)
 633                        list_add_tail(&map->next, &priv->freeable_maps);
 634                err = 0;
 635        }
 636        mutex_unlock(&priv->lock);
 637        if (map)
 638                gntdev_put_map(priv, map);
 639        return err;
 640}
 641
 642static long gntdev_ioctl_get_offset_for_vaddr(struct gntdev_priv *priv,
 643                                              struct ioctl_gntdev_get_offset_for_vaddr __user *u)
 644{
 645        struct ioctl_gntdev_get_offset_for_vaddr op;
 646        struct vm_area_struct *vma;
 647        struct grant_map *map;
 648        int rv = -EINVAL;
 649
 650        if (copy_from_user(&op, u, sizeof(op)) != 0)
 651                return -EFAULT;
 652        pr_debug("priv %p, offset for vaddr %lx\n", priv, (unsigned long)op.vaddr);
 653
 654        down_read(&current->mm->mmap_sem);
 655        vma = find_vma(current->mm, op.vaddr);
 656        if (!vma || vma->vm_ops != &gntdev_vmops)
 657                goto out_unlock;
 658
 659        map = vma->vm_private_data;
 660        if (!map)
 661                goto out_unlock;
 662
 663        op.offset = map->index << PAGE_SHIFT;
 664        op.count = map->count;
 665        rv = 0;
 666
 667 out_unlock:
 668        up_read(&current->mm->mmap_sem);
 669
 670        if (rv == 0 && copy_to_user(u, &op, sizeof(op)) != 0)
 671                return -EFAULT;
 672        return rv;
 673}
 674
 675static long gntdev_ioctl_notify(struct gntdev_priv *priv, void __user *u)
 676{
 677        struct ioctl_gntdev_unmap_notify op;
 678        struct grant_map *map;
 679        int rc;
 680        int out_flags;
 681        unsigned int out_event;
 682
 683        if (copy_from_user(&op, u, sizeof(op)))
 684                return -EFAULT;
 685
 686        if (op.action & ~(UNMAP_NOTIFY_CLEAR_BYTE|UNMAP_NOTIFY_SEND_EVENT))
 687                return -EINVAL;
 688
 689        /* We need to grab a reference to the event channel we are going to use
 690         * to send the notify before releasing the reference we may already have
 691         * (if someone has called this ioctl twice). This is required so that
 692         * it is possible to change the clear_byte part of the notification
 693         * without disturbing the event channel part, which may now be the last
 694         * reference to that event channel.
 695         */
 696        if (op.action & UNMAP_NOTIFY_SEND_EVENT) {
 697                if (evtchn_get(op.event_channel_port))
 698                        return -EINVAL;
 699        }
 700
 701        out_flags = op.action;
 702        out_event = op.event_channel_port;
 703
 704        mutex_lock(&priv->lock);
 705
 706        list_for_each_entry(map, &priv->maps, next) {
 707                uint64_t begin = map->index << PAGE_SHIFT;
 708                uint64_t end = (map->index + map->count) << PAGE_SHIFT;
 709                if (op.index >= begin && op.index < end)
 710                        goto found;
 711        }
 712        rc = -ENOENT;
 713        goto unlock_out;
 714
 715 found:
 716        if ((op.action & UNMAP_NOTIFY_CLEAR_BYTE) &&
 717                        (map->flags & GNTMAP_readonly)) {
 718                rc = -EINVAL;
 719                goto unlock_out;
 720        }
 721
 722        out_flags = map->notify.flags;
 723        out_event = map->notify.event;
 724
 725        map->notify.flags = op.action;
 726        map->notify.addr = op.index - (map->index << PAGE_SHIFT);
 727        map->notify.event = op.event_channel_port;
 728
 729        rc = 0;
 730
 731 unlock_out:
 732        mutex_unlock(&priv->lock);
 733
 734        /* Drop the reference to the event channel we did not save in the map */
 735        if (out_flags & UNMAP_NOTIFY_SEND_EVENT)
 736                evtchn_put(out_event);
 737
 738        return rc;
 739}
 740
 741#define GNTDEV_COPY_BATCH 16
 742
 743struct gntdev_copy_batch {
 744        struct gnttab_copy ops[GNTDEV_COPY_BATCH];
 745        struct page *pages[GNTDEV_COPY_BATCH];
 746        s16 __user *status[GNTDEV_COPY_BATCH];
 747        unsigned int nr_ops;
 748        unsigned int nr_pages;
 749};
 750
 751static int gntdev_get_page(struct gntdev_copy_batch *batch, void __user *virt,
 752                           bool writeable, unsigned long *gfn)
 753{
 754        unsigned long addr = (unsigned long)virt;
 755        struct page *page;
 756        unsigned long xen_pfn;
 757        int ret;
 758
 759        ret = get_user_pages_fast(addr, 1, writeable ? FOLL_WRITE : 0, &page);
 760        if (ret < 0)
 761                return ret;
 762
 763        batch->pages[batch->nr_pages++] = page;
 764
 765        xen_pfn = page_to_xen_pfn(page) + XEN_PFN_DOWN(addr & ~PAGE_MASK);
 766        *gfn = pfn_to_gfn(xen_pfn);
 767
 768        return 0;
 769}
 770
 771static void gntdev_put_pages(struct gntdev_copy_batch *batch)
 772{
 773        unsigned int i;
 774
 775        for (i = 0; i < batch->nr_pages; i++)
 776                put_page(batch->pages[i]);
 777        batch->nr_pages = 0;
 778}
 779
 780static int gntdev_copy(struct gntdev_copy_batch *batch)
 781{
 782        unsigned int i;
 783
 784        gnttab_batch_copy(batch->ops, batch->nr_ops);
 785        gntdev_put_pages(batch);
 786
 787        /*
 788         * For each completed op, update the status if the op failed
 789         * and all previous ops for the segment were successful.
 790         */
 791        for (i = 0; i < batch->nr_ops; i++) {
 792                s16 status = batch->ops[i].status;
 793                s16 old_status;
 794
 795                if (status == GNTST_okay)
 796                        continue;
 797
 798                if (__get_user(old_status, batch->status[i]))
 799                        return -EFAULT;
 800
 801                if (old_status != GNTST_okay)
 802                        continue;
 803
 804                if (__put_user(status, batch->status[i]))
 805                        return -EFAULT;
 806        }
 807
 808        batch->nr_ops = 0;
 809        return 0;
 810}
 811
 812static int gntdev_grant_copy_seg(struct gntdev_copy_batch *batch,
 813                                 struct gntdev_grant_copy_segment *seg,
 814                                 s16 __user *status)
 815{
 816        uint16_t copied = 0;
 817
 818        /*
 819         * Disallow local -> local copies since there is only space in
 820         * batch->pages for one page per-op and this would be a very
 821         * expensive memcpy().
 822         */
 823        if (!(seg->flags & (GNTCOPY_source_gref | GNTCOPY_dest_gref)))
 824                return -EINVAL;
 825
 826        /* Can't cross page if source/dest is a grant ref. */
 827        if (seg->flags & GNTCOPY_source_gref) {
 828                if (seg->source.foreign.offset + seg->len > XEN_PAGE_SIZE)
 829                        return -EINVAL;
 830        }
 831        if (seg->flags & GNTCOPY_dest_gref) {
 832                if (seg->dest.foreign.offset + seg->len > XEN_PAGE_SIZE)
 833                        return -EINVAL;
 834        }
 835
 836        if (put_user(GNTST_okay, status))
 837                return -EFAULT;
 838
 839        while (copied < seg->len) {
 840                struct gnttab_copy *op;
 841                void __user *virt;
 842                size_t len, off;
 843                unsigned long gfn;
 844                int ret;
 845
 846                if (batch->nr_ops >= GNTDEV_COPY_BATCH) {
 847                        ret = gntdev_copy(batch);
 848                        if (ret < 0)
 849                                return ret;
 850                }
 851
 852                len = seg->len - copied;
 853
 854                op = &batch->ops[batch->nr_ops];
 855                op->flags = 0;
 856
 857                if (seg->flags & GNTCOPY_source_gref) {
 858                        op->source.u.ref = seg->source.foreign.ref;
 859                        op->source.domid = seg->source.foreign.domid;
 860                        op->source.offset = seg->source.foreign.offset + copied;
 861                        op->flags |= GNTCOPY_source_gref;
 862                } else {
 863                        virt = seg->source.virt + copied;
 864                        off = (unsigned long)virt & ~XEN_PAGE_MASK;
 865                        len = min(len, (size_t)XEN_PAGE_SIZE - off);
 866
 867                        ret = gntdev_get_page(batch, virt, false, &gfn);
 868                        if (ret < 0)
 869                                return ret;
 870
 871                        op->source.u.gmfn = gfn;
 872                        op->source.domid = DOMID_SELF;
 873                        op->source.offset = off;
 874                }
 875
 876                if (seg->flags & GNTCOPY_dest_gref) {
 877                        op->dest.u.ref = seg->dest.foreign.ref;
 878                        op->dest.domid = seg->dest.foreign.domid;
 879                        op->dest.offset = seg->dest.foreign.offset + copied;
 880                        op->flags |= GNTCOPY_dest_gref;
 881                } else {
 882                        virt = seg->dest.virt + copied;
 883                        off = (unsigned long)virt & ~XEN_PAGE_MASK;
 884                        len = min(len, (size_t)XEN_PAGE_SIZE - off);
 885
 886                        ret = gntdev_get_page(batch, virt, true, &gfn);
 887                        if (ret < 0)
 888                                return ret;
 889
 890                        op->dest.u.gmfn = gfn;
 891                        op->dest.domid = DOMID_SELF;
 892                        op->dest.offset = off;
 893                }
 894
 895                op->len = len;
 896                copied += len;
 897
 898                batch->status[batch->nr_ops] = status;
 899                batch->nr_ops++;
 900        }
 901
 902        return 0;
 903}
 904
 905static long gntdev_ioctl_grant_copy(struct gntdev_priv *priv, void __user *u)
 906{
 907        struct ioctl_gntdev_grant_copy copy;
 908        struct gntdev_copy_batch batch;
 909        unsigned int i;
 910        int ret = 0;
 911
 912        if (copy_from_user(&copy, u, sizeof(copy)))
 913                return -EFAULT;
 914
 915        batch.nr_ops = 0;
 916        batch.nr_pages = 0;
 917
 918        for (i = 0; i < copy.count; i++) {
 919                struct gntdev_grant_copy_segment seg;
 920
 921                if (copy_from_user(&seg, &copy.segments[i], sizeof(seg))) {
 922                        ret = -EFAULT;
 923                        goto out;
 924                }
 925
 926                ret = gntdev_grant_copy_seg(&batch, &seg, &copy.segments[i].status);
 927                if (ret < 0)
 928                        goto out;
 929
 930                cond_resched();
 931        }
 932        if (batch.nr_ops)
 933                ret = gntdev_copy(&batch);
 934        return ret;
 935
 936  out:
 937        gntdev_put_pages(&batch);
 938        return ret;
 939}
 940
 941static long gntdev_ioctl(struct file *flip,
 942                         unsigned int cmd, unsigned long arg)
 943{
 944        struct gntdev_priv *priv = flip->private_data;
 945        void __user *ptr = (void __user *)arg;
 946
 947        switch (cmd) {
 948        case IOCTL_GNTDEV_MAP_GRANT_REF:
 949                return gntdev_ioctl_map_grant_ref(priv, ptr);
 950
 951        case IOCTL_GNTDEV_UNMAP_GRANT_REF:
 952                return gntdev_ioctl_unmap_grant_ref(priv, ptr);
 953
 954        case IOCTL_GNTDEV_GET_OFFSET_FOR_VADDR:
 955                return gntdev_ioctl_get_offset_for_vaddr(priv, ptr);
 956
 957        case IOCTL_GNTDEV_SET_UNMAP_NOTIFY:
 958                return gntdev_ioctl_notify(priv, ptr);
 959
 960        case IOCTL_GNTDEV_GRANT_COPY:
 961                return gntdev_ioctl_grant_copy(priv, ptr);
 962
 963        default:
 964                pr_debug("priv %p, unknown cmd %x\n", priv, cmd);
 965                return -ENOIOCTLCMD;
 966        }
 967
 968        return 0;
 969}
 970
 971static int gntdev_mmap(struct file *flip, struct vm_area_struct *vma)
 972{
 973        struct gntdev_priv *priv = flip->private_data;
 974        int index = vma->vm_pgoff;
 975        int count = vma_pages(vma);
 976        struct grant_map *map;
 977        int i, err = -EINVAL;
 978
 979        if ((vma->vm_flags & VM_WRITE) && !(vma->vm_flags & VM_SHARED))
 980                return -EINVAL;
 981
 982        pr_debug("map %d+%d at %lx (pgoff %lx)\n",
 983                        index, count, vma->vm_start, vma->vm_pgoff);
 984
 985        mutex_lock(&priv->lock);
 986        map = gntdev_find_map_index(priv, index, count);
 987        if (!map)
 988                goto unlock_out;
 989        if (use_ptemod && map->vma)
 990                goto unlock_out;
 991        if (use_ptemod && priv->mm != vma->vm_mm) {
 992                pr_warn("Huh? Other mm?\n");
 993                goto unlock_out;
 994        }
 995
 996        refcount_inc(&map->users);
 997
 998        vma->vm_ops = &gntdev_vmops;
 999
1000        vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP | VM_MIXEDMAP;
1001
1002        if (use_ptemod)
1003                vma->vm_flags |= VM_DONTCOPY;
1004
1005        vma->vm_private_data = map;
1006
1007        if (use_ptemod)
1008                map->vma = vma;
1009
1010        if (map->flags) {
1011                if ((vma->vm_flags & VM_WRITE) &&
1012                                (map->flags & GNTMAP_readonly))
1013                        goto out_unlock_put;
1014        } else {
1015                map->flags = GNTMAP_host_map;
1016                if (!(vma->vm_flags & VM_WRITE))
1017                        map->flags |= GNTMAP_readonly;
1018        }
1019
1020        mutex_unlock(&priv->lock);
1021
1022        if (use_ptemod) {
1023                map->pages_vm_start = vma->vm_start;
1024                err = apply_to_page_range(vma->vm_mm, vma->vm_start,
1025                                          vma->vm_end - vma->vm_start,
1026                                          find_grant_ptes, map);
1027                if (err) {
1028                        pr_warn("find_grant_ptes() failure.\n");
1029                        goto out_put_map;
1030                }
1031        }
1032
1033        err = map_grant_pages(map);
1034        if (err)
1035                goto out_put_map;
1036
1037        if (!use_ptemod) {
1038                for (i = 0; i < count; i++) {
1039                        err = vm_insert_page(vma, vma->vm_start + i*PAGE_SIZE,
1040                                map->pages[i]);
1041                        if (err)
1042                                goto out_put_map;
1043                }
1044        } else {
1045#ifdef CONFIG_X86
1046                /*
1047                 * If the PTEs were not made special by the grant map
1048                 * hypercall, do so here.
1049                 *
1050                 * This is racy since the mapping is already visible
1051                 * to userspace but userspace should be well-behaved
1052                 * enough to not touch it until the mmap() call
1053                 * returns.
1054                 */
1055                if (!xen_feature(XENFEAT_gnttab_map_avail_bits)) {
1056                        apply_to_page_range(vma->vm_mm, vma->vm_start,
1057                                            vma->vm_end - vma->vm_start,
1058                                            set_grant_ptes_as_special, NULL);
1059                }
1060#endif
1061        }
1062
1063        return 0;
1064
1065unlock_out:
1066        mutex_unlock(&priv->lock);
1067        return err;
1068
1069out_unlock_put:
1070        mutex_unlock(&priv->lock);
1071out_put_map:
1072        if (use_ptemod) {
1073                map->vma = NULL;
1074                unmap_grant_pages(map, 0, map->count);
1075        }
1076        gntdev_put_map(priv, map);
1077        return err;
1078}
1079
1080static const struct file_operations gntdev_fops = {
1081        .owner = THIS_MODULE,
1082        .open = gntdev_open,
1083        .release = gntdev_release,
1084        .mmap = gntdev_mmap,
1085        .unlocked_ioctl = gntdev_ioctl
1086};
1087
1088static struct miscdevice gntdev_miscdev = {
1089        .minor        = MISC_DYNAMIC_MINOR,
1090        .name         = "xen/gntdev",
1091        .fops         = &gntdev_fops,
1092};
1093
1094/* ------------------------------------------------------------------ */
1095
1096static int __init gntdev_init(void)
1097{
1098        int err;
1099
1100        if (!xen_domain())
1101                return -ENODEV;
1102
1103        use_ptemod = !xen_feature(XENFEAT_auto_translated_physmap);
1104
1105        err = misc_register(&gntdev_miscdev);
1106        if (err != 0) {
1107                pr_err("Could not register gntdev device\n");
1108                return err;
1109        }
1110        return 0;
1111}
1112
1113static void __exit gntdev_exit(void)
1114{
1115        misc_deregister(&gntdev_miscdev);
1116}
1117
1118module_init(gntdev_init);
1119module_exit(gntdev_exit);
1120
1121/* ------------------------------------------------------------------ */
1122