Merge branches 'stable/backend.base.v3' and 'stable/gntalloc.v7' of git://git.kernel...
[pandora-kernel.git] / drivers / xen / gntdev.c
index 687761f..f914b26 100644 (file)
@@ -36,6 +36,7 @@
 
 #include <xen/xen.h>
 #include <xen/grant_table.h>
+#include <xen/balloon.h>
 #include <xen/gntdev.h>
 #include <xen/events.h>
 #include <asm/xen/hypervisor.h>
@@ -122,10 +123,10 @@ static struct grant_map *gntdev_alloc_map(struct gntdev_priv *priv, int count)
            NULL == add->pages)
                goto err;
 
+       if (alloc_xenballooned_pages(count, add->pages))
+               goto err;
+
        for (i = 0; i < count; i++) {
-               add->pages[i] = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
-               if (add->pages[i] == NULL)
-                       goto err;
                add->map_ops[i].handle = -1;
                add->unmap_ops[i].handle = -1;
        }
@@ -137,11 +138,6 @@ static struct grant_map *gntdev_alloc_map(struct gntdev_priv *priv, int count)
        return add;
 
 err:
-       if (add->pages)
-               for (i = 0; i < count; i++) {
-                       if (add->pages[i])
-                               __free_page(add->pages[i]);
-               }
        kfree(add->pages);
        kfree(add->grants);
        kfree(add->map_ops);
@@ -184,8 +180,6 @@ static struct grant_map *gntdev_find_map_index(struct gntdev_priv *priv,
 
 static void gntdev_put_map(struct grant_map *map)
 {
-       int i;
-
        if (!map)
                return;
 
@@ -202,29 +196,7 @@ static void gntdev_put_map(struct grant_map *map)
                if (!use_ptemod)
                        unmap_grant_pages(map, 0, map->count);
 
-               for (i = 0; i < map->count; i++) {
-                       uint32_t check, *tmp;
-                       if (!map->pages[i])
-                               continue;
-                       /* XXX When unmapping in an HVM domain, Xen will
-                        * sometimes end up mapping the GFN to an invalid MFN.
-                        * In this case, writes will be discarded and reads will
-                        * return all 0xFF bytes.  Leak these unusable GFNs
-                        * until Xen supports fixing their p2m mapping.
-                        *
-                        * Confirmed present in Xen 4.1-RC3 with HVM source
-                        */
-                       tmp = kmap(map->pages[i]);
-                       *tmp = 0xdeaddead;
-                       mb();
-                       check = *tmp;
-                       kunmap(map->pages[i]);
-                       if (check == 0xdeaddead)
-                               __free_page(map->pages[i]);
-                       else
-                               pr_debug("Discard page %d=%ld\n", i,
-                                       page_to_pfn(map->pages[i]));
-               }
+               free_xenballooned_pages(map->count, map->pages);
        }
        kfree(map->pages);
        kfree(map->grants);
@@ -301,7 +273,7 @@ static int __unmap_grant_pages(struct grant_map *map, int offset, int pages)
                                map->vma->vm_start + map->notify.addr;
                        err = copy_to_user(tmp, &err, 1);
                        if (err)
-                               return err;
+                               return -EFAULT;
                        map->notify.flags &= ~UNMAP_NOTIFY_CLEAR_BYTE;
                } else if (pgno >= offset && pgno < offset + pages) {
                        uint8_t *tmp = kmap(map->pages[pgno]);
@@ -702,7 +674,7 @@ static int gntdev_mmap(struct file *flip, struct vm_area_struct *vma)
        if (map->flags) {
                if ((vma->vm_flags & VM_WRITE) &&
                                (map->flags & GNTMAP_readonly))
-                       return -EINVAL;
+                       goto out_unlock_put;
        } else {
                map->flags = GNTMAP_host_map;
                if (!(vma->vm_flags & VM_WRITE))
@@ -740,6 +712,8 @@ unlock_out:
        spin_unlock(&priv->lock);
        return err;
 
+out_unlock_put:
+       spin_unlock(&priv->lock);
 out_put_map:
        if (use_ptemod)
                map->vma = NULL;