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