Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 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 | #include <linux/module.h> |
| 23 | #include <linux/kernel.h> |
| 24 | #include <linux/init.h> |
| 25 | #include <linux/miscdevice.h> |
| 26 | #include <linux/fs.h> |
| 27 | #include <linux/mm.h> |
| 28 | #include <linux/mman.h> |
| 29 | #include <linux/mmu_notifier.h> |
| 30 | #include <linux/types.h> |
| 31 | #include <linux/uaccess.h> |
| 32 | #include <linux/sched.h> |
| 33 | #include <linux/spinlock.h> |
| 34 | #include <linux/slab.h> |
Daniel De Graaf | aab8f11 | 2011-02-03 12:19:02 -0500 | [diff] [blame] | 35 | #include <linux/highmem.h> |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 36 | |
| 37 | #include <xen/xen.h> |
| 38 | #include <xen/grant_table.h> |
| 39 | #include <xen/gntdev.h> |
Daniel De Graaf | bdc612d | 2011-02-03 12:19:04 -0500 | [diff] [blame] | 40 | #include <xen/events.h> |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 41 | #include <asm/xen/hypervisor.h> |
| 42 | #include <asm/xen/hypercall.h> |
| 43 | #include <asm/xen/page.h> |
| 44 | |
| 45 | MODULE_LICENSE("GPL"); |
| 46 | MODULE_AUTHOR("Derek G. Murray <Derek.Murray@cl.cam.ac.uk>, " |
| 47 | "Gerd Hoffmann <kraxel@redhat.com>"); |
| 48 | MODULE_DESCRIPTION("User-space granted page access driver"); |
| 49 | |
Daniel De Graaf | ef91082 | 2011-02-03 12:18:59 -0500 | [diff] [blame] | 50 | static int limit = 1024*1024; |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 51 | module_param(limit, int, 0644); |
Daniel De Graaf | ef91082 | 2011-02-03 12:18:59 -0500 | [diff] [blame] | 52 | MODULE_PARM_DESC(limit, "Maximum number of grants that may be mapped by " |
| 53 | "the gntdev device"); |
| 54 | |
| 55 | static atomic_t pages_mapped = ATOMIC_INIT(0); |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 56 | |
Daniel De Graaf | aab8f11 | 2011-02-03 12:19:02 -0500 | [diff] [blame] | 57 | static int use_ptemod; |
| 58 | |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 59 | struct gntdev_priv { |
| 60 | struct list_head maps; |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 61 | /* lock protects maps from concurrent changes */ |
| 62 | spinlock_t lock; |
| 63 | struct mm_struct *mm; |
| 64 | struct mmu_notifier mn; |
| 65 | }; |
| 66 | |
Daniel De Graaf | bdc612d | 2011-02-03 12:19:04 -0500 | [diff] [blame] | 67 | struct unmap_notify { |
| 68 | int flags; |
| 69 | /* Address relative to the start of the grant_map */ |
| 70 | int addr; |
| 71 | int event; |
| 72 | }; |
| 73 | |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 74 | struct grant_map { |
| 75 | struct list_head next; |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 76 | struct vm_area_struct *vma; |
| 77 | int index; |
| 78 | int count; |
| 79 | int flags; |
Daniel De Graaf | 68b025c | 2011-02-03 12:19:01 -0500 | [diff] [blame] | 80 | atomic_t users; |
Daniel De Graaf | bdc612d | 2011-02-03 12:19:04 -0500 | [diff] [blame] | 81 | struct unmap_notify notify; |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 82 | struct ioctl_gntdev_grant_ref *grants; |
| 83 | struct gnttab_map_grant_ref *map_ops; |
| 84 | struct gnttab_unmap_grant_ref *unmap_ops; |
Stefano Stabellini | a12b4eb | 2010-12-10 14:56:42 +0000 | [diff] [blame] | 85 | struct page **pages; |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 86 | }; |
| 87 | |
Daniel De Graaf | aab8f11 | 2011-02-03 12:19:02 -0500 | [diff] [blame] | 88 | static int unmap_grant_pages(struct grant_map *map, int offset, int pages); |
| 89 | |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 90 | /* ------------------------------------------------------------------ */ |
| 91 | |
| 92 | static void gntdev_print_maps(struct gntdev_priv *priv, |
| 93 | char *text, int text_index) |
| 94 | { |
| 95 | #ifdef DEBUG |
| 96 | struct grant_map *map; |
| 97 | |
Daniel De Graaf | ef91082 | 2011-02-03 12:18:59 -0500 | [diff] [blame] | 98 | pr_debug("%s: maps list (priv %p)\n", __func__, priv); |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 99 | list_for_each_entry(map, &priv->maps, next) |
| 100 | pr_debug(" index %2d, count %2d %s\n", |
| 101 | map->index, map->count, |
| 102 | map->index == text_index && text ? text : ""); |
| 103 | #endif |
| 104 | } |
| 105 | |
| 106 | static struct grant_map *gntdev_alloc_map(struct gntdev_priv *priv, int count) |
| 107 | { |
| 108 | struct grant_map *add; |
Stefano Stabellini | a12b4eb | 2010-12-10 14:56:42 +0000 | [diff] [blame] | 109 | int i; |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 110 | |
| 111 | add = kzalloc(sizeof(struct grant_map), GFP_KERNEL); |
| 112 | if (NULL == add) |
| 113 | return NULL; |
| 114 | |
| 115 | add->grants = kzalloc(sizeof(add->grants[0]) * count, GFP_KERNEL); |
| 116 | add->map_ops = kzalloc(sizeof(add->map_ops[0]) * count, GFP_KERNEL); |
| 117 | add->unmap_ops = kzalloc(sizeof(add->unmap_ops[0]) * count, GFP_KERNEL); |
Stefano Stabellini | a12b4eb | 2010-12-10 14:56:42 +0000 | [diff] [blame] | 118 | add->pages = kzalloc(sizeof(add->pages[0]) * count, GFP_KERNEL); |
| 119 | if (NULL == add->grants || |
| 120 | NULL == add->map_ops || |
| 121 | NULL == add->unmap_ops || |
| 122 | NULL == add->pages) |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 123 | goto err; |
| 124 | |
Stefano Stabellini | a12b4eb | 2010-12-10 14:56:42 +0000 | [diff] [blame] | 125 | for (i = 0; i < count; i++) { |
| 126 | add->pages[i] = alloc_page(GFP_KERNEL | __GFP_HIGHMEM); |
| 127 | if (add->pages[i] == NULL) |
| 128 | goto err; |
| 129 | } |
| 130 | |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 131 | add->index = 0; |
| 132 | add->count = count; |
Daniel De Graaf | 68b025c | 2011-02-03 12:19:01 -0500 | [diff] [blame] | 133 | atomic_set(&add->users, 1); |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 134 | |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 135 | return add; |
| 136 | |
| 137 | err: |
Stefano Stabellini | a12b4eb | 2010-12-10 14:56:42 +0000 | [diff] [blame] | 138 | if (add->pages) |
| 139 | for (i = 0; i < count; i++) { |
| 140 | if (add->pages[i]) |
| 141 | __free_page(add->pages[i]); |
| 142 | } |
| 143 | kfree(add->pages); |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 144 | kfree(add->grants); |
| 145 | kfree(add->map_ops); |
| 146 | kfree(add->unmap_ops); |
| 147 | kfree(add); |
| 148 | return NULL; |
| 149 | } |
| 150 | |
| 151 | static void gntdev_add_map(struct gntdev_priv *priv, struct grant_map *add) |
| 152 | { |
| 153 | struct grant_map *map; |
| 154 | |
| 155 | list_for_each_entry(map, &priv->maps, next) { |
| 156 | if (add->index + add->count < map->index) { |
| 157 | list_add_tail(&add->next, &map->next); |
| 158 | goto done; |
| 159 | } |
| 160 | add->index = map->index + map->count; |
| 161 | } |
| 162 | list_add_tail(&add->next, &priv->maps); |
| 163 | |
| 164 | done: |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 165 | gntdev_print_maps(priv, "[new]", add->index); |
| 166 | } |
| 167 | |
| 168 | static struct grant_map *gntdev_find_map_index(struct gntdev_priv *priv, |
| 169 | int index, int count) |
| 170 | { |
| 171 | struct grant_map *map; |
| 172 | |
| 173 | list_for_each_entry(map, &priv->maps, next) { |
| 174 | if (map->index != index) |
| 175 | continue; |
Daniel De Graaf | bdc612d | 2011-02-03 12:19:04 -0500 | [diff] [blame] | 176 | if (count && map->count != count) |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 177 | continue; |
| 178 | return map; |
| 179 | } |
| 180 | return NULL; |
| 181 | } |
| 182 | |
Daniel De Graaf | 68b025c | 2011-02-03 12:19:01 -0500 | [diff] [blame] | 183 | static void gntdev_put_map(struct grant_map *map) |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 184 | { |
Stefano Stabellini | a12b4eb | 2010-12-10 14:56:42 +0000 | [diff] [blame] | 185 | int i; |
| 186 | |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 187 | if (!map) |
| 188 | return; |
Stefano Stabellini | a12b4eb | 2010-12-10 14:56:42 +0000 | [diff] [blame] | 189 | |
Daniel De Graaf | 68b025c | 2011-02-03 12:19:01 -0500 | [diff] [blame] | 190 | if (!atomic_dec_and_test(&map->users)) |
| 191 | return; |
| 192 | |
| 193 | atomic_sub(map->count, &pages_mapped); |
| 194 | |
Daniel De Graaf | bdc612d | 2011-02-03 12:19:04 -0500 | [diff] [blame] | 195 | if (map->notify.flags & UNMAP_NOTIFY_SEND_EVENT) { |
| 196 | notify_remote_via_evtchn(map->notify.event); |
| 197 | } |
| 198 | |
Daniel De Graaf | aab8f11 | 2011-02-03 12:19:02 -0500 | [diff] [blame] | 199 | if (map->pages) { |
| 200 | if (!use_ptemod) |
| 201 | unmap_grant_pages(map, 0, map->count); |
| 202 | |
Stefano Stabellini | a12b4eb | 2010-12-10 14:56:42 +0000 | [diff] [blame] | 203 | for (i = 0; i < map->count; i++) { |
Daniel De Graaf | aab8f11 | 2011-02-03 12:19:02 -0500 | [diff] [blame] | 204 | uint32_t check, *tmp; |
| 205 | if (!map->pages[i]) |
| 206 | continue; |
| 207 | /* XXX When unmapping in an HVM domain, Xen will |
| 208 | * sometimes end up mapping the GFN to an invalid MFN. |
| 209 | * In this case, writes will be discarded and reads will |
| 210 | * return all 0xFF bytes. Leak these unusable GFNs |
| 211 | * until Xen supports fixing their p2m mapping. |
| 212 | * |
| 213 | * Confirmed present in Xen 4.1-RC3 with HVM source |
| 214 | */ |
| 215 | tmp = kmap(map->pages[i]); |
| 216 | *tmp = 0xdeaddead; |
| 217 | mb(); |
| 218 | check = *tmp; |
| 219 | kunmap(map->pages[i]); |
| 220 | if (check == 0xdeaddead) |
Stefano Stabellini | a12b4eb | 2010-12-10 14:56:42 +0000 | [diff] [blame] | 221 | __free_page(map->pages[i]); |
Daniel De Graaf | aab8f11 | 2011-02-03 12:19:02 -0500 | [diff] [blame] | 222 | else |
| 223 | pr_debug("Discard page %d=%ld\n", i, |
| 224 | page_to_pfn(map->pages[i])); |
Stefano Stabellini | a12b4eb | 2010-12-10 14:56:42 +0000 | [diff] [blame] | 225 | } |
Daniel De Graaf | aab8f11 | 2011-02-03 12:19:02 -0500 | [diff] [blame] | 226 | } |
Stefano Stabellini | a12b4eb | 2010-12-10 14:56:42 +0000 | [diff] [blame] | 227 | kfree(map->pages); |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 228 | kfree(map->grants); |
| 229 | kfree(map->map_ops); |
| 230 | kfree(map->unmap_ops); |
| 231 | kfree(map); |
| 232 | } |
| 233 | |
| 234 | /* ------------------------------------------------------------------ */ |
| 235 | |
| 236 | static int find_grant_ptes(pte_t *pte, pgtable_t token, |
| 237 | unsigned long addr, void *data) |
| 238 | { |
| 239 | struct grant_map *map = data; |
| 240 | unsigned int pgnr = (addr - map->vma->vm_start) >> PAGE_SHIFT; |
Daniel De Graaf | aab8f11 | 2011-02-03 12:19:02 -0500 | [diff] [blame] | 241 | int flags = map->flags | GNTMAP_application_map | GNTMAP_contains_pte; |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 242 | u64 pte_maddr; |
| 243 | |
| 244 | BUG_ON(pgnr >= map->count); |
Jeremy Fitzhardinge | ba5d101 | 2010-12-08 10:54:32 -0800 | [diff] [blame] | 245 | pte_maddr = arbitrary_virt_to_machine(pte).maddr; |
| 246 | |
Daniel De Graaf | aab8f11 | 2011-02-03 12:19:02 -0500 | [diff] [blame] | 247 | gnttab_set_map_op(&map->map_ops[pgnr], pte_maddr, flags, |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 248 | map->grants[pgnr].ref, |
| 249 | map->grants[pgnr].domid); |
Daniel De Graaf | aab8f11 | 2011-02-03 12:19:02 -0500 | [diff] [blame] | 250 | gnttab_set_unmap_op(&map->unmap_ops[pgnr], pte_maddr, flags, |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 251 | 0 /* handle */); |
| 252 | return 0; |
| 253 | } |
| 254 | |
| 255 | static int map_grant_pages(struct grant_map *map) |
| 256 | { |
| 257 | int i, err = 0; |
Daniel De Graaf | aab8f11 | 2011-02-03 12:19:02 -0500 | [diff] [blame] | 258 | phys_addr_t addr; |
| 259 | |
| 260 | if (!use_ptemod) { |
| 261 | for (i = 0; i < map->count; i++) { |
| 262 | addr = (phys_addr_t) |
| 263 | pfn_to_kaddr(page_to_pfn(map->pages[i])); |
| 264 | gnttab_set_map_op(&map->map_ops[i], addr, map->flags, |
| 265 | map->grants[i].ref, |
| 266 | map->grants[i].domid); |
| 267 | gnttab_set_unmap_op(&map->unmap_ops[i], addr, |
| 268 | map->flags, 0 /* handle */); |
| 269 | } |
| 270 | } |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 271 | |
| 272 | pr_debug("map %d+%d\n", map->index, map->count); |
Stefano Stabellini | a12b4eb | 2010-12-10 14:56:42 +0000 | [diff] [blame] | 273 | err = gnttab_map_refs(map->map_ops, map->pages, map->count); |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 274 | if (err) |
| 275 | return err; |
| 276 | |
| 277 | for (i = 0; i < map->count; i++) { |
| 278 | if (map->map_ops[i].status) |
| 279 | err = -EINVAL; |
| 280 | map->unmap_ops[i].handle = map->map_ops[i].handle; |
| 281 | } |
| 282 | return err; |
| 283 | } |
| 284 | |
| 285 | static int unmap_grant_pages(struct grant_map *map, int offset, int pages) |
| 286 | { |
| 287 | int i, err = 0; |
| 288 | |
Daniel De Graaf | bdc612d | 2011-02-03 12:19:04 -0500 | [diff] [blame] | 289 | if (map->notify.flags & UNMAP_NOTIFY_CLEAR_BYTE) { |
| 290 | int pgno = (map->notify.addr >> PAGE_SHIFT); |
Daniel De Graaf | 0ea22f0 | 2011-02-08 09:14:06 -0500 | [diff] [blame] | 291 | if (pgno >= offset && pgno < offset + pages && use_ptemod) { |
| 292 | void __user *tmp; |
| 293 | tmp = map->vma->vm_start + map->notify.addr; |
| 294 | copy_to_user(tmp, &err, 1); |
| 295 | map->notify.flags &= ~UNMAP_NOTIFY_CLEAR_BYTE; |
| 296 | } else if (pgno >= offset && pgno < offset + pages) { |
Daniel De Graaf | bdc612d | 2011-02-03 12:19:04 -0500 | [diff] [blame] | 297 | uint8_t *tmp = kmap(map->pages[pgno]); |
| 298 | tmp[map->notify.addr & (PAGE_SIZE-1)] = 0; |
| 299 | kunmap(map->pages[pgno]); |
| 300 | map->notify.flags &= ~UNMAP_NOTIFY_CLEAR_BYTE; |
| 301 | } |
| 302 | } |
| 303 | |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 304 | pr_debug("map %d+%d [%d+%d]\n", map->index, map->count, offset, pages); |
Daniel De Graaf | 0ea22f0 | 2011-02-08 09:14:06 -0500 | [diff] [blame] | 305 | err = gnttab_unmap_refs(map->unmap_ops + offset, map->pages + offset, pages); |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 306 | if (err) |
| 307 | return err; |
| 308 | |
| 309 | for (i = 0; i < pages; i++) { |
| 310 | if (map->unmap_ops[offset+i].status) |
| 311 | err = -EINVAL; |
| 312 | map->unmap_ops[offset+i].handle = 0; |
| 313 | } |
| 314 | return err; |
| 315 | } |
| 316 | |
| 317 | /* ------------------------------------------------------------------ */ |
| 318 | |
| 319 | static void gntdev_vma_close(struct vm_area_struct *vma) |
| 320 | { |
| 321 | struct grant_map *map = vma->vm_private_data; |
| 322 | |
| 323 | pr_debug("close %p\n", vma); |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 324 | map->vma = NULL; |
| 325 | vma->vm_private_data = NULL; |
Daniel De Graaf | 68b025c | 2011-02-03 12:19:01 -0500 | [diff] [blame] | 326 | gntdev_put_map(map); |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 327 | } |
| 328 | |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 329 | static struct vm_operations_struct gntdev_vmops = { |
| 330 | .close = gntdev_vma_close, |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 331 | }; |
| 332 | |
| 333 | /* ------------------------------------------------------------------ */ |
| 334 | |
| 335 | static void mn_invl_range_start(struct mmu_notifier *mn, |
| 336 | struct mm_struct *mm, |
| 337 | unsigned long start, unsigned long end) |
| 338 | { |
| 339 | struct gntdev_priv *priv = container_of(mn, struct gntdev_priv, mn); |
| 340 | struct grant_map *map; |
| 341 | unsigned long mstart, mend; |
| 342 | int err; |
| 343 | |
| 344 | spin_lock(&priv->lock); |
| 345 | list_for_each_entry(map, &priv->maps, next) { |
| 346 | if (!map->vma) |
| 347 | continue; |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 348 | if (map->vma->vm_start >= end) |
| 349 | continue; |
| 350 | if (map->vma->vm_end <= start) |
| 351 | continue; |
| 352 | mstart = max(start, map->vma->vm_start); |
| 353 | mend = min(end, map->vma->vm_end); |
| 354 | pr_debug("map %d+%d (%lx %lx), range %lx %lx, mrange %lx %lx\n", |
| 355 | map->index, map->count, |
| 356 | map->vma->vm_start, map->vma->vm_end, |
| 357 | start, end, mstart, mend); |
| 358 | err = unmap_grant_pages(map, |
| 359 | (mstart - map->vma->vm_start) >> PAGE_SHIFT, |
| 360 | (mend - mstart) >> PAGE_SHIFT); |
| 361 | WARN_ON(err); |
| 362 | } |
| 363 | spin_unlock(&priv->lock); |
| 364 | } |
| 365 | |
| 366 | static void mn_invl_page(struct mmu_notifier *mn, |
| 367 | struct mm_struct *mm, |
| 368 | unsigned long address) |
| 369 | { |
| 370 | mn_invl_range_start(mn, mm, address, address + PAGE_SIZE); |
| 371 | } |
| 372 | |
| 373 | static void mn_release(struct mmu_notifier *mn, |
| 374 | struct mm_struct *mm) |
| 375 | { |
| 376 | struct gntdev_priv *priv = container_of(mn, struct gntdev_priv, mn); |
| 377 | struct grant_map *map; |
| 378 | int err; |
| 379 | |
| 380 | spin_lock(&priv->lock); |
| 381 | list_for_each_entry(map, &priv->maps, next) { |
| 382 | if (!map->vma) |
| 383 | continue; |
| 384 | pr_debug("map %d+%d (%lx %lx)\n", |
| 385 | map->index, map->count, |
| 386 | map->vma->vm_start, map->vma->vm_end); |
| 387 | err = unmap_grant_pages(map, /* offset */ 0, map->count); |
| 388 | WARN_ON(err); |
| 389 | } |
| 390 | spin_unlock(&priv->lock); |
| 391 | } |
| 392 | |
| 393 | struct mmu_notifier_ops gntdev_mmu_ops = { |
| 394 | .release = mn_release, |
| 395 | .invalidate_page = mn_invl_page, |
| 396 | .invalidate_range_start = mn_invl_range_start, |
| 397 | }; |
| 398 | |
| 399 | /* ------------------------------------------------------------------ */ |
| 400 | |
| 401 | static int gntdev_open(struct inode *inode, struct file *flip) |
| 402 | { |
| 403 | struct gntdev_priv *priv; |
| 404 | int ret = 0; |
| 405 | |
| 406 | priv = kzalloc(sizeof(*priv), GFP_KERNEL); |
| 407 | if (!priv) |
| 408 | return -ENOMEM; |
| 409 | |
| 410 | INIT_LIST_HEAD(&priv->maps); |
| 411 | spin_lock_init(&priv->lock); |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 412 | |
Daniel De Graaf | aab8f11 | 2011-02-03 12:19:02 -0500 | [diff] [blame] | 413 | if (use_ptemod) { |
| 414 | priv->mm = get_task_mm(current); |
| 415 | if (!priv->mm) { |
| 416 | kfree(priv); |
| 417 | return -ENOMEM; |
| 418 | } |
| 419 | priv->mn.ops = &gntdev_mmu_ops; |
| 420 | ret = mmu_notifier_register(&priv->mn, priv->mm); |
| 421 | mmput(priv->mm); |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 422 | } |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 423 | |
| 424 | if (ret) { |
| 425 | kfree(priv); |
| 426 | return ret; |
| 427 | } |
| 428 | |
| 429 | flip->private_data = priv; |
| 430 | pr_debug("priv %p\n", priv); |
| 431 | |
| 432 | return 0; |
| 433 | } |
| 434 | |
| 435 | static int gntdev_release(struct inode *inode, struct file *flip) |
| 436 | { |
| 437 | struct gntdev_priv *priv = flip->private_data; |
| 438 | struct grant_map *map; |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 439 | |
| 440 | pr_debug("priv %p\n", priv); |
| 441 | |
| 442 | spin_lock(&priv->lock); |
| 443 | while (!list_empty(&priv->maps)) { |
| 444 | map = list_entry(priv->maps.next, struct grant_map, next); |
Daniel De Graaf | 68b025c | 2011-02-03 12:19:01 -0500 | [diff] [blame] | 445 | list_del(&map->next); |
| 446 | gntdev_put_map(map); |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 447 | } |
| 448 | spin_unlock(&priv->lock); |
| 449 | |
Daniel De Graaf | aab8f11 | 2011-02-03 12:19:02 -0500 | [diff] [blame] | 450 | if (use_ptemod) |
| 451 | mmu_notifier_unregister(&priv->mn, priv->mm); |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 452 | kfree(priv); |
| 453 | return 0; |
| 454 | } |
| 455 | |
| 456 | static long gntdev_ioctl_map_grant_ref(struct gntdev_priv *priv, |
| 457 | struct ioctl_gntdev_map_grant_ref __user *u) |
| 458 | { |
| 459 | struct ioctl_gntdev_map_grant_ref op; |
| 460 | struct grant_map *map; |
| 461 | int err; |
| 462 | |
| 463 | if (copy_from_user(&op, u, sizeof(op)) != 0) |
| 464 | return -EFAULT; |
| 465 | pr_debug("priv %p, add %d\n", priv, op.count); |
| 466 | if (unlikely(op.count <= 0)) |
| 467 | return -EINVAL; |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 468 | |
| 469 | err = -ENOMEM; |
| 470 | map = gntdev_alloc_map(priv, op.count); |
| 471 | if (!map) |
| 472 | return err; |
Daniel De Graaf | ef91082 | 2011-02-03 12:18:59 -0500 | [diff] [blame] | 473 | |
Daniel De Graaf | 68b025c | 2011-02-03 12:19:01 -0500 | [diff] [blame] | 474 | if (unlikely(atomic_add_return(op.count, &pages_mapped) > limit)) { |
| 475 | pr_debug("can't map: over limit\n"); |
| 476 | gntdev_put_map(map); |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 477 | return err; |
| 478 | } |
| 479 | |
Daniel De Graaf | 68b025c | 2011-02-03 12:19:01 -0500 | [diff] [blame] | 480 | if (copy_from_user(map->grants, &u->refs, |
| 481 | sizeof(map->grants[0]) * op.count) != 0) { |
| 482 | gntdev_put_map(map); |
Daniel De Graaf | ef91082 | 2011-02-03 12:18:59 -0500 | [diff] [blame] | 483 | return err; |
| 484 | } |
| 485 | |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 486 | spin_lock(&priv->lock); |
| 487 | gntdev_add_map(priv, map); |
| 488 | op.index = map->index << PAGE_SHIFT; |
| 489 | spin_unlock(&priv->lock); |
| 490 | |
Daniel De Graaf | 68b025c | 2011-02-03 12:19:01 -0500 | [diff] [blame] | 491 | if (copy_to_user(u, &op, sizeof(op)) != 0) |
| 492 | return -EFAULT; |
| 493 | |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 494 | return 0; |
| 495 | } |
| 496 | |
| 497 | static long gntdev_ioctl_unmap_grant_ref(struct gntdev_priv *priv, |
| 498 | struct ioctl_gntdev_unmap_grant_ref __user *u) |
| 499 | { |
| 500 | struct ioctl_gntdev_unmap_grant_ref op; |
| 501 | struct grant_map *map; |
| 502 | int err = -ENOENT; |
| 503 | |
| 504 | if (copy_from_user(&op, u, sizeof(op)) != 0) |
| 505 | return -EFAULT; |
| 506 | pr_debug("priv %p, del %d+%d\n", priv, (int)op.index, (int)op.count); |
| 507 | |
| 508 | spin_lock(&priv->lock); |
| 509 | map = gntdev_find_map_index(priv, op.index >> PAGE_SHIFT, op.count); |
Daniel De Graaf | 68b025c | 2011-02-03 12:19:01 -0500 | [diff] [blame] | 510 | if (map) { |
| 511 | list_del(&map->next); |
| 512 | gntdev_put_map(map); |
| 513 | err = 0; |
| 514 | } |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 515 | spin_unlock(&priv->lock); |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 516 | return err; |
| 517 | } |
| 518 | |
| 519 | static long gntdev_ioctl_get_offset_for_vaddr(struct gntdev_priv *priv, |
| 520 | struct ioctl_gntdev_get_offset_for_vaddr __user *u) |
| 521 | { |
| 522 | struct ioctl_gntdev_get_offset_for_vaddr op; |
Daniel De Graaf | a879211 | 2011-02-03 12:19:00 -0500 | [diff] [blame] | 523 | struct vm_area_struct *vma; |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 524 | struct grant_map *map; |
| 525 | |
| 526 | if (copy_from_user(&op, u, sizeof(op)) != 0) |
| 527 | return -EFAULT; |
| 528 | pr_debug("priv %p, offset for vaddr %lx\n", priv, (unsigned long)op.vaddr); |
| 529 | |
Daniel De Graaf | a879211 | 2011-02-03 12:19:00 -0500 | [diff] [blame] | 530 | vma = find_vma(current->mm, op.vaddr); |
| 531 | if (!vma || vma->vm_ops != &gntdev_vmops) |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 532 | return -EINVAL; |
Daniel De Graaf | a879211 | 2011-02-03 12:19:00 -0500 | [diff] [blame] | 533 | |
| 534 | map = vma->vm_private_data; |
| 535 | if (!map) |
| 536 | return -EINVAL; |
| 537 | |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 538 | op.offset = map->index << PAGE_SHIFT; |
| 539 | op.count = map->count; |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 540 | |
| 541 | if (copy_to_user(u, &op, sizeof(op)) != 0) |
| 542 | return -EFAULT; |
| 543 | return 0; |
| 544 | } |
| 545 | |
Daniel De Graaf | bdc612d | 2011-02-03 12:19:04 -0500 | [diff] [blame] | 546 | static long gntdev_ioctl_notify(struct gntdev_priv *priv, void __user *u) |
| 547 | { |
| 548 | struct ioctl_gntdev_unmap_notify op; |
| 549 | struct grant_map *map; |
| 550 | int rc; |
| 551 | |
| 552 | if (copy_from_user(&op, u, sizeof(op))) |
| 553 | return -EFAULT; |
| 554 | |
| 555 | if (op.action & ~(UNMAP_NOTIFY_CLEAR_BYTE|UNMAP_NOTIFY_SEND_EVENT)) |
| 556 | return -EINVAL; |
| 557 | |
| 558 | spin_lock(&priv->lock); |
| 559 | |
| 560 | list_for_each_entry(map, &priv->maps, next) { |
| 561 | uint64_t begin = map->index << PAGE_SHIFT; |
| 562 | uint64_t end = (map->index + map->count) << PAGE_SHIFT; |
| 563 | if (op.index >= begin && op.index < end) |
| 564 | goto found; |
| 565 | } |
| 566 | rc = -ENOENT; |
| 567 | goto unlock_out; |
| 568 | |
| 569 | found: |
| 570 | map->notify.flags = op.action; |
| 571 | map->notify.addr = op.index - (map->index << PAGE_SHIFT); |
| 572 | map->notify.event = op.event_channel_port; |
| 573 | rc = 0; |
| 574 | unlock_out: |
| 575 | spin_unlock(&priv->lock); |
| 576 | return rc; |
| 577 | } |
| 578 | |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 579 | static long gntdev_ioctl(struct file *flip, |
| 580 | unsigned int cmd, unsigned long arg) |
| 581 | { |
| 582 | struct gntdev_priv *priv = flip->private_data; |
| 583 | void __user *ptr = (void __user *)arg; |
| 584 | |
| 585 | switch (cmd) { |
| 586 | case IOCTL_GNTDEV_MAP_GRANT_REF: |
| 587 | return gntdev_ioctl_map_grant_ref(priv, ptr); |
| 588 | |
| 589 | case IOCTL_GNTDEV_UNMAP_GRANT_REF: |
| 590 | return gntdev_ioctl_unmap_grant_ref(priv, ptr); |
| 591 | |
| 592 | case IOCTL_GNTDEV_GET_OFFSET_FOR_VADDR: |
| 593 | return gntdev_ioctl_get_offset_for_vaddr(priv, ptr); |
| 594 | |
Daniel De Graaf | bdc612d | 2011-02-03 12:19:04 -0500 | [diff] [blame] | 595 | case IOCTL_GNTDEV_SET_UNMAP_NOTIFY: |
| 596 | return gntdev_ioctl_notify(priv, ptr); |
| 597 | |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 598 | default: |
| 599 | pr_debug("priv %p, unknown cmd %x\n", priv, cmd); |
| 600 | return -ENOIOCTLCMD; |
| 601 | } |
| 602 | |
| 603 | return 0; |
| 604 | } |
| 605 | |
| 606 | static int gntdev_mmap(struct file *flip, struct vm_area_struct *vma) |
| 607 | { |
| 608 | struct gntdev_priv *priv = flip->private_data; |
| 609 | int index = vma->vm_pgoff; |
| 610 | int count = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT; |
| 611 | struct grant_map *map; |
Daniel De Graaf | aab8f11 | 2011-02-03 12:19:02 -0500 | [diff] [blame] | 612 | int i, err = -EINVAL; |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 613 | |
| 614 | if ((vma->vm_flags & VM_WRITE) && !(vma->vm_flags & VM_SHARED)) |
| 615 | return -EINVAL; |
| 616 | |
| 617 | pr_debug("map %d+%d at %lx (pgoff %lx)\n", |
| 618 | index, count, vma->vm_start, vma->vm_pgoff); |
| 619 | |
| 620 | spin_lock(&priv->lock); |
| 621 | map = gntdev_find_map_index(priv, index, count); |
| 622 | if (!map) |
| 623 | goto unlock_out; |
Daniel De Graaf | aab8f11 | 2011-02-03 12:19:02 -0500 | [diff] [blame] | 624 | if (use_ptemod && map->vma) |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 625 | goto unlock_out; |
Daniel De Graaf | aab8f11 | 2011-02-03 12:19:02 -0500 | [diff] [blame] | 626 | if (use_ptemod && priv->mm != vma->vm_mm) { |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 627 | printk(KERN_WARNING "Huh? Other mm?\n"); |
| 628 | goto unlock_out; |
| 629 | } |
| 630 | |
Daniel De Graaf | 68b025c | 2011-02-03 12:19:01 -0500 | [diff] [blame] | 631 | atomic_inc(&map->users); |
| 632 | |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 633 | vma->vm_ops = &gntdev_vmops; |
| 634 | |
Jeremy Fitzhardinge | 8d3eaea | 2010-11-11 14:39:12 -0800 | [diff] [blame] | 635 | vma->vm_flags |= VM_RESERVED|VM_DONTCOPY|VM_DONTEXPAND|VM_PFNMAP; |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 636 | |
| 637 | vma->vm_private_data = map; |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 638 | |
Daniel De Graaf | aab8f11 | 2011-02-03 12:19:02 -0500 | [diff] [blame] | 639 | if (use_ptemod) |
| 640 | map->vma = vma; |
| 641 | |
| 642 | map->flags = GNTMAP_host_map; |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 643 | if (!(vma->vm_flags & VM_WRITE)) |
| 644 | map->flags |= GNTMAP_readonly; |
| 645 | |
Daniel De Graaf | f0a70c8 | 2011-01-07 11:51:47 +0000 | [diff] [blame] | 646 | spin_unlock(&priv->lock); |
| 647 | |
Daniel De Graaf | aab8f11 | 2011-02-03 12:19:02 -0500 | [diff] [blame] | 648 | if (use_ptemod) { |
| 649 | err = apply_to_page_range(vma->vm_mm, vma->vm_start, |
| 650 | vma->vm_end - vma->vm_start, |
| 651 | find_grant_ptes, map); |
| 652 | if (err) { |
| 653 | printk(KERN_WARNING "find_grant_ptes() failure.\n"); |
Daniel De Graaf | 90b6f30 | 2011-02-03 14:16:54 -0500 | [diff] [blame] | 654 | goto out_put_map; |
Daniel De Graaf | aab8f11 | 2011-02-03 12:19:02 -0500 | [diff] [blame] | 655 | } |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 656 | } |
| 657 | |
| 658 | err = map_grant_pages(map); |
Daniel De Graaf | 90b6f30 | 2011-02-03 14:16:54 -0500 | [diff] [blame] | 659 | if (err) |
| 660 | goto out_put_map; |
Daniel De Graaf | f0a70c8 | 2011-01-07 11:51:47 +0000 | [diff] [blame] | 661 | |
Daniel De Graaf | aab8f11 | 2011-02-03 12:19:02 -0500 | [diff] [blame] | 662 | if (!use_ptemod) { |
| 663 | for (i = 0; i < count; i++) { |
| 664 | err = vm_insert_page(vma, vma->vm_start + i*PAGE_SIZE, |
| 665 | map->pages[i]); |
| 666 | if (err) |
Daniel De Graaf | 90b6f30 | 2011-02-03 14:16:54 -0500 | [diff] [blame] | 667 | goto out_put_map; |
Daniel De Graaf | aab8f11 | 2011-02-03 12:19:02 -0500 | [diff] [blame] | 668 | } |
| 669 | } |
| 670 | |
Daniel De Graaf | f0a70c8 | 2011-01-07 11:51:47 +0000 | [diff] [blame] | 671 | return 0; |
| 672 | |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 673 | unlock_out: |
| 674 | spin_unlock(&priv->lock); |
| 675 | return err; |
Daniel De Graaf | 90b6f30 | 2011-02-03 14:16:54 -0500 | [diff] [blame] | 676 | |
| 677 | out_put_map: |
Daniel De Graaf | 84e4075 | 2011-02-09 15:11:59 -0500 | [diff] [blame^] | 678 | if (use_ptemod) |
| 679 | map->vma = NULL; |
Daniel De Graaf | 90b6f30 | 2011-02-03 14:16:54 -0500 | [diff] [blame] | 680 | gntdev_put_map(map); |
| 681 | return err; |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 682 | } |
| 683 | |
| 684 | static const struct file_operations gntdev_fops = { |
| 685 | .owner = THIS_MODULE, |
| 686 | .open = gntdev_open, |
| 687 | .release = gntdev_release, |
| 688 | .mmap = gntdev_mmap, |
| 689 | .unlocked_ioctl = gntdev_ioctl |
| 690 | }; |
| 691 | |
| 692 | static struct miscdevice gntdev_miscdev = { |
| 693 | .minor = MISC_DYNAMIC_MINOR, |
| 694 | .name = "xen/gntdev", |
| 695 | .fops = &gntdev_fops, |
| 696 | }; |
| 697 | |
| 698 | /* ------------------------------------------------------------------ */ |
| 699 | |
| 700 | static int __init gntdev_init(void) |
| 701 | { |
| 702 | int err; |
| 703 | |
| 704 | if (!xen_domain()) |
| 705 | return -ENODEV; |
| 706 | |
Daniel De Graaf | aab8f11 | 2011-02-03 12:19:02 -0500 | [diff] [blame] | 707 | use_ptemod = xen_pv_domain(); |
| 708 | |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 709 | err = misc_register(&gntdev_miscdev); |
| 710 | if (err != 0) { |
| 711 | printk(KERN_ERR "Could not register gntdev device\n"); |
| 712 | return err; |
| 713 | } |
| 714 | return 0; |
| 715 | } |
| 716 | |
| 717 | static void __exit gntdev_exit(void) |
| 718 | { |
| 719 | misc_deregister(&gntdev_miscdev); |
| 720 | } |
| 721 | |
| 722 | module_init(gntdev_init); |
| 723 | module_exit(gntdev_exit); |
| 724 | |
| 725 | /* ------------------------------------------------------------------ */ |