]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/xen/gntdev.c
xen-gntdev: Avoid unmapping ranges twice
[mirror_ubuntu-bionic-kernel.git] / drivers / xen / gntdev.c
CommitLineData
ab31523c
GH
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>
aab8f11a 35#include <linux/highmem.h>
ab31523c
GH
36
37#include <xen/xen.h>
38#include <xen/grant_table.h>
39#include <xen/gntdev.h>
bdc612dc 40#include <xen/events.h>
ab31523c
GH
41#include <asm/xen/hypervisor.h>
42#include <asm/xen/hypercall.h>
43#include <asm/xen/page.h>
44
45MODULE_LICENSE("GPL");
46MODULE_AUTHOR("Derek G. Murray <Derek.Murray@cl.cam.ac.uk>, "
47 "Gerd Hoffmann <kraxel@redhat.com>");
48MODULE_DESCRIPTION("User-space granted page access driver");
49
ef91082e 50static int limit = 1024*1024;
ab31523c 51module_param(limit, int, 0644);
ef91082e
DDG
52MODULE_PARM_DESC(limit, "Maximum number of grants that may be mapped by "
53 "the gntdev device");
54
55static atomic_t pages_mapped = ATOMIC_INIT(0);
ab31523c 56
aab8f11a
DDG
57static int use_ptemod;
58
ab31523c
GH
59struct gntdev_priv {
60 struct list_head maps;
ab31523c
GH
61 /* lock protects maps from concurrent changes */
62 spinlock_t lock;
63 struct mm_struct *mm;
64 struct mmu_notifier mn;
65};
66
bdc612dc
DDG
67struct unmap_notify {
68 int flags;
69 /* Address relative to the start of the grant_map */
70 int addr;
71 int event;
72};
73
ab31523c
GH
74struct grant_map {
75 struct list_head next;
ab31523c
GH
76 struct vm_area_struct *vma;
77 int index;
78 int count;
79 int flags;
68b025c8 80 atomic_t users;
bdc612dc 81 struct unmap_notify notify;
ab31523c
GH
82 struct ioctl_gntdev_grant_ref *grants;
83 struct gnttab_map_grant_ref *map_ops;
84 struct gnttab_unmap_grant_ref *unmap_ops;
a12b4eb3 85 struct page **pages;
ab31523c
GH
86};
87
aab8f11a
DDG
88static int unmap_grant_pages(struct grant_map *map, int offset, int pages);
89
ab31523c
GH
90/* ------------------------------------------------------------------ */
91
92static void gntdev_print_maps(struct gntdev_priv *priv,
93 char *text, int text_index)
94{
95#ifdef DEBUG
96 struct grant_map *map;
97
ef91082e 98 pr_debug("%s: maps list (priv %p)\n", __func__, priv);
ab31523c
GH
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
106static struct grant_map *gntdev_alloc_map(struct gntdev_priv *priv, int count)
107{
108 struct grant_map *add;
a12b4eb3 109 int i;
ab31523c
GH
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);
a12b4eb3
SS
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)
ab31523c
GH
123 goto err;
124
a12b4eb3
SS
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
ab31523c
GH
131 add->index = 0;
132 add->count = count;
68b025c8 133 atomic_set(&add->users, 1);
ab31523c 134
ab31523c
GH
135 return add;
136
137err:
a12b4eb3
SS
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);
ab31523c
GH
144 kfree(add->grants);
145 kfree(add->map_ops);
146 kfree(add->unmap_ops);
147 kfree(add);
148 return NULL;
149}
150
151static 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
164done:
ab31523c
GH
165 gntdev_print_maps(priv, "[new]", add->index);
166}
167
168static 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;
bdc612dc 176 if (count && map->count != count)
ab31523c
GH
177 continue;
178 return map;
179 }
180 return NULL;
181}
182
68b025c8 183static void gntdev_put_map(struct grant_map *map)
ab31523c 184{
a12b4eb3
SS
185 int i;
186
ab31523c
GH
187 if (!map)
188 return;
a12b4eb3 189
68b025c8
DDG
190 if (!atomic_dec_and_test(&map->users))
191 return;
192
193 atomic_sub(map->count, &pages_mapped);
194
bdc612dc
DDG
195 if (map->notify.flags & UNMAP_NOTIFY_SEND_EVENT) {
196 notify_remote_via_evtchn(map->notify.event);
197 }
198
aab8f11a
DDG
199 if (map->pages) {
200 if (!use_ptemod)
201 unmap_grant_pages(map, 0, map->count);
202
a12b4eb3 203 for (i = 0; i < map->count; i++) {
aab8f11a
DDG
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)
a12b4eb3 221 __free_page(map->pages[i]);
aab8f11a
DDG
222 else
223 pr_debug("Discard page %d=%ld\n", i,
224 page_to_pfn(map->pages[i]));
a12b4eb3 225 }
aab8f11a 226 }
a12b4eb3 227 kfree(map->pages);
ab31523c
GH
228 kfree(map->grants);
229 kfree(map->map_ops);
230 kfree(map->unmap_ops);
231 kfree(map);
232}
233
234/* ------------------------------------------------------------------ */
235
236static 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;
aab8f11a 241 int flags = map->flags | GNTMAP_application_map | GNTMAP_contains_pte;
ab31523c
GH
242 u64 pte_maddr;
243
244 BUG_ON(pgnr >= map->count);
ba5d1012
JF
245 pte_maddr = arbitrary_virt_to_machine(pte).maddr;
246
aab8f11a 247 gnttab_set_map_op(&map->map_ops[pgnr], pte_maddr, flags,
ab31523c
GH
248 map->grants[pgnr].ref,
249 map->grants[pgnr].domid);
aab8f11a 250 gnttab_set_unmap_op(&map->unmap_ops[pgnr], pte_maddr, flags,
ab31523c
GH
251 0 /* handle */);
252 return 0;
253}
254
255static int map_grant_pages(struct grant_map *map)
256{
257 int i, err = 0;
aab8f11a
DDG
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 }
ab31523c
GH
271
272 pr_debug("map %d+%d\n", map->index, map->count);
a12b4eb3 273 err = gnttab_map_refs(map->map_ops, map->pages, map->count);
ab31523c
GH
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
b57c1869 285static int __unmap_grant_pages(struct grant_map *map, int offset, int pages)
ab31523c
GH
286{
287 int i, err = 0;
288
bdc612dc
DDG
289 if (map->notify.flags & UNMAP_NOTIFY_CLEAR_BYTE) {
290 int pgno = (map->notify.addr >> PAGE_SHIFT);
0ea22f07
DDG
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) {
bdc612dc
DDG
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
0ea22f07 304 err = gnttab_unmap_refs(map->unmap_ops + offset, map->pages + offset, pages);
ab31523c
GH
305 if (err)
306 return err;
307
308 for (i = 0; i < pages; i++) {
309 if (map->unmap_ops[offset+i].status)
310 err = -EINVAL;
311 map->unmap_ops[offset+i].handle = 0;
312 }
313 return err;
314}
315
b57c1869
DDG
316static int unmap_grant_pages(struct grant_map *map, int offset, int pages)
317{
318 int range, err = 0;
319
320 pr_debug("unmap %d+%d [%d+%d]\n", map->index, map->count, offset, pages);
321
322 /* It is possible the requested range will have a "hole" where we
323 * already unmapped some of the grants. Only unmap valid ranges.
324 */
325 while (pages && !err) {
326 while (pages && !map->unmap_ops[offset].handle) {
327 offset++;
328 pages--;
329 }
330 range = 0;
331 while (range < pages) {
332 if (!map->unmap_ops[offset+range].handle) {
333 range--;
334 break;
335 }
336 range++;
337 }
338 err = __unmap_grant_pages(map, offset, range);
339 offset += range;
340 pages -= range;
341 }
342
343 return err;
344}
345
ab31523c
GH
346/* ------------------------------------------------------------------ */
347
348static void gntdev_vma_close(struct vm_area_struct *vma)
349{
350 struct grant_map *map = vma->vm_private_data;
351
352 pr_debug("close %p\n", vma);
ab31523c
GH
353 map->vma = NULL;
354 vma->vm_private_data = NULL;
68b025c8 355 gntdev_put_map(map);
ab31523c
GH
356}
357
ab31523c
GH
358static struct vm_operations_struct gntdev_vmops = {
359 .close = gntdev_vma_close,
ab31523c
GH
360};
361
362/* ------------------------------------------------------------------ */
363
364static void mn_invl_range_start(struct mmu_notifier *mn,
365 struct mm_struct *mm,
366 unsigned long start, unsigned long end)
367{
368 struct gntdev_priv *priv = container_of(mn, struct gntdev_priv, mn);
369 struct grant_map *map;
370 unsigned long mstart, mend;
371 int err;
372
373 spin_lock(&priv->lock);
374 list_for_each_entry(map, &priv->maps, next) {
375 if (!map->vma)
376 continue;
ab31523c
GH
377 if (map->vma->vm_start >= end)
378 continue;
379 if (map->vma->vm_end <= start)
380 continue;
381 mstart = max(start, map->vma->vm_start);
382 mend = min(end, map->vma->vm_end);
383 pr_debug("map %d+%d (%lx %lx), range %lx %lx, mrange %lx %lx\n",
384 map->index, map->count,
385 map->vma->vm_start, map->vma->vm_end,
386 start, end, mstart, mend);
387 err = unmap_grant_pages(map,
388 (mstart - map->vma->vm_start) >> PAGE_SHIFT,
389 (mend - mstart) >> PAGE_SHIFT);
390 WARN_ON(err);
391 }
392 spin_unlock(&priv->lock);
393}
394
395static void mn_invl_page(struct mmu_notifier *mn,
396 struct mm_struct *mm,
397 unsigned long address)
398{
399 mn_invl_range_start(mn, mm, address, address + PAGE_SIZE);
400}
401
402static void mn_release(struct mmu_notifier *mn,
403 struct mm_struct *mm)
404{
405 struct gntdev_priv *priv = container_of(mn, struct gntdev_priv, mn);
406 struct grant_map *map;
407 int err;
408
409 spin_lock(&priv->lock);
410 list_for_each_entry(map, &priv->maps, next) {
411 if (!map->vma)
412 continue;
413 pr_debug("map %d+%d (%lx %lx)\n",
414 map->index, map->count,
415 map->vma->vm_start, map->vma->vm_end);
416 err = unmap_grant_pages(map, /* offset */ 0, map->count);
417 WARN_ON(err);
418 }
419 spin_unlock(&priv->lock);
420}
421
422struct mmu_notifier_ops gntdev_mmu_ops = {
423 .release = mn_release,
424 .invalidate_page = mn_invl_page,
425 .invalidate_range_start = mn_invl_range_start,
426};
427
428/* ------------------------------------------------------------------ */
429
430static int gntdev_open(struct inode *inode, struct file *flip)
431{
432 struct gntdev_priv *priv;
433 int ret = 0;
434
435 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
436 if (!priv)
437 return -ENOMEM;
438
439 INIT_LIST_HEAD(&priv->maps);
440 spin_lock_init(&priv->lock);
ab31523c 441
aab8f11a
DDG
442 if (use_ptemod) {
443 priv->mm = get_task_mm(current);
444 if (!priv->mm) {
445 kfree(priv);
446 return -ENOMEM;
447 }
448 priv->mn.ops = &gntdev_mmu_ops;
449 ret = mmu_notifier_register(&priv->mn, priv->mm);
450 mmput(priv->mm);
ab31523c 451 }
ab31523c
GH
452
453 if (ret) {
454 kfree(priv);
455 return ret;
456 }
457
458 flip->private_data = priv;
459 pr_debug("priv %p\n", priv);
460
461 return 0;
462}
463
464static int gntdev_release(struct inode *inode, struct file *flip)
465{
466 struct gntdev_priv *priv = flip->private_data;
467 struct grant_map *map;
ab31523c
GH
468
469 pr_debug("priv %p\n", priv);
470
471 spin_lock(&priv->lock);
472 while (!list_empty(&priv->maps)) {
473 map = list_entry(priv->maps.next, struct grant_map, next);
68b025c8
DDG
474 list_del(&map->next);
475 gntdev_put_map(map);
ab31523c
GH
476 }
477 spin_unlock(&priv->lock);
478
aab8f11a
DDG
479 if (use_ptemod)
480 mmu_notifier_unregister(&priv->mn, priv->mm);
ab31523c
GH
481 kfree(priv);
482 return 0;
483}
484
485static long gntdev_ioctl_map_grant_ref(struct gntdev_priv *priv,
486 struct ioctl_gntdev_map_grant_ref __user *u)
487{
488 struct ioctl_gntdev_map_grant_ref op;
489 struct grant_map *map;
490 int err;
491
492 if (copy_from_user(&op, u, sizeof(op)) != 0)
493 return -EFAULT;
494 pr_debug("priv %p, add %d\n", priv, op.count);
495 if (unlikely(op.count <= 0))
496 return -EINVAL;
ab31523c
GH
497
498 err = -ENOMEM;
499 map = gntdev_alloc_map(priv, op.count);
500 if (!map)
501 return err;
ef91082e 502
68b025c8
DDG
503 if (unlikely(atomic_add_return(op.count, &pages_mapped) > limit)) {
504 pr_debug("can't map: over limit\n");
505 gntdev_put_map(map);
ab31523c
GH
506 return err;
507 }
508
68b025c8
DDG
509 if (copy_from_user(map->grants, &u->refs,
510 sizeof(map->grants[0]) * op.count) != 0) {
511 gntdev_put_map(map);
ef91082e
DDG
512 return err;
513 }
514
ab31523c
GH
515 spin_lock(&priv->lock);
516 gntdev_add_map(priv, map);
517 op.index = map->index << PAGE_SHIFT;
518 spin_unlock(&priv->lock);
519
68b025c8
DDG
520 if (copy_to_user(u, &op, sizeof(op)) != 0)
521 return -EFAULT;
522
ab31523c
GH
523 return 0;
524}
525
526static long gntdev_ioctl_unmap_grant_ref(struct gntdev_priv *priv,
527 struct ioctl_gntdev_unmap_grant_ref __user *u)
528{
529 struct ioctl_gntdev_unmap_grant_ref op;
530 struct grant_map *map;
531 int err = -ENOENT;
532
533 if (copy_from_user(&op, u, sizeof(op)) != 0)
534 return -EFAULT;
535 pr_debug("priv %p, del %d+%d\n", priv, (int)op.index, (int)op.count);
536
537 spin_lock(&priv->lock);
538 map = gntdev_find_map_index(priv, op.index >> PAGE_SHIFT, op.count);
68b025c8
DDG
539 if (map) {
540 list_del(&map->next);
541 gntdev_put_map(map);
542 err = 0;
543 }
ab31523c 544 spin_unlock(&priv->lock);
ab31523c
GH
545 return err;
546}
547
548static long gntdev_ioctl_get_offset_for_vaddr(struct gntdev_priv *priv,
549 struct ioctl_gntdev_get_offset_for_vaddr __user *u)
550{
551 struct ioctl_gntdev_get_offset_for_vaddr op;
a879211b 552 struct vm_area_struct *vma;
ab31523c
GH
553 struct grant_map *map;
554
555 if (copy_from_user(&op, u, sizeof(op)) != 0)
556 return -EFAULT;
557 pr_debug("priv %p, offset for vaddr %lx\n", priv, (unsigned long)op.vaddr);
558
a879211b
DDG
559 vma = find_vma(current->mm, op.vaddr);
560 if (!vma || vma->vm_ops != &gntdev_vmops)
ab31523c 561 return -EINVAL;
a879211b
DDG
562
563 map = vma->vm_private_data;
564 if (!map)
565 return -EINVAL;
566
ab31523c
GH
567 op.offset = map->index << PAGE_SHIFT;
568 op.count = map->count;
ab31523c
GH
569
570 if (copy_to_user(u, &op, sizeof(op)) != 0)
571 return -EFAULT;
572 return 0;
573}
574
bdc612dc
DDG
575static long gntdev_ioctl_notify(struct gntdev_priv *priv, void __user *u)
576{
577 struct ioctl_gntdev_unmap_notify op;
578 struct grant_map *map;
579 int rc;
580
581 if (copy_from_user(&op, u, sizeof(op)))
582 return -EFAULT;
583
584 if (op.action & ~(UNMAP_NOTIFY_CLEAR_BYTE|UNMAP_NOTIFY_SEND_EVENT))
585 return -EINVAL;
586
587 spin_lock(&priv->lock);
588
589 list_for_each_entry(map, &priv->maps, next) {
590 uint64_t begin = map->index << PAGE_SHIFT;
591 uint64_t end = (map->index + map->count) << PAGE_SHIFT;
592 if (op.index >= begin && op.index < end)
593 goto found;
594 }
595 rc = -ENOENT;
596 goto unlock_out;
597
598 found:
599 map->notify.flags = op.action;
600 map->notify.addr = op.index - (map->index << PAGE_SHIFT);
601 map->notify.event = op.event_channel_port;
602 rc = 0;
603 unlock_out:
604 spin_unlock(&priv->lock);
605 return rc;
606}
607
ab31523c
GH
608static long gntdev_ioctl(struct file *flip,
609 unsigned int cmd, unsigned long arg)
610{
611 struct gntdev_priv *priv = flip->private_data;
612 void __user *ptr = (void __user *)arg;
613
614 switch (cmd) {
615 case IOCTL_GNTDEV_MAP_GRANT_REF:
616 return gntdev_ioctl_map_grant_ref(priv, ptr);
617
618 case IOCTL_GNTDEV_UNMAP_GRANT_REF:
619 return gntdev_ioctl_unmap_grant_ref(priv, ptr);
620
621 case IOCTL_GNTDEV_GET_OFFSET_FOR_VADDR:
622 return gntdev_ioctl_get_offset_for_vaddr(priv, ptr);
623
bdc612dc
DDG
624 case IOCTL_GNTDEV_SET_UNMAP_NOTIFY:
625 return gntdev_ioctl_notify(priv, ptr);
626
ab31523c
GH
627 default:
628 pr_debug("priv %p, unknown cmd %x\n", priv, cmd);
629 return -ENOIOCTLCMD;
630 }
631
632 return 0;
633}
634
635static int gntdev_mmap(struct file *flip, struct vm_area_struct *vma)
636{
637 struct gntdev_priv *priv = flip->private_data;
638 int index = vma->vm_pgoff;
639 int count = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
640 struct grant_map *map;
aab8f11a 641 int i, err = -EINVAL;
ab31523c
GH
642
643 if ((vma->vm_flags & VM_WRITE) && !(vma->vm_flags & VM_SHARED))
644 return -EINVAL;
645
646 pr_debug("map %d+%d at %lx (pgoff %lx)\n",
647 index, count, vma->vm_start, vma->vm_pgoff);
648
649 spin_lock(&priv->lock);
650 map = gntdev_find_map_index(priv, index, count);
651 if (!map)
652 goto unlock_out;
aab8f11a 653 if (use_ptemod && map->vma)
ab31523c 654 goto unlock_out;
aab8f11a 655 if (use_ptemod && priv->mm != vma->vm_mm) {
ab31523c
GH
656 printk(KERN_WARNING "Huh? Other mm?\n");
657 goto unlock_out;
658 }
659
68b025c8
DDG
660 atomic_inc(&map->users);
661
ab31523c
GH
662 vma->vm_ops = &gntdev_vmops;
663
8d3eaea2 664 vma->vm_flags |= VM_RESERVED|VM_DONTCOPY|VM_DONTEXPAND|VM_PFNMAP;
ab31523c
GH
665
666 vma->vm_private_data = map;
ab31523c 667
aab8f11a
DDG
668 if (use_ptemod)
669 map->vma = vma;
670
671 map->flags = GNTMAP_host_map;
ab31523c
GH
672 if (!(vma->vm_flags & VM_WRITE))
673 map->flags |= GNTMAP_readonly;
674
f0a70c88
DDG
675 spin_unlock(&priv->lock);
676
aab8f11a
DDG
677 if (use_ptemod) {
678 err = apply_to_page_range(vma->vm_mm, vma->vm_start,
679 vma->vm_end - vma->vm_start,
680 find_grant_ptes, map);
681 if (err) {
682 printk(KERN_WARNING "find_grant_ptes() failure.\n");
90b6f305 683 goto out_put_map;
aab8f11a 684 }
ab31523c
GH
685 }
686
687 err = map_grant_pages(map);
90b6f305
DDG
688 if (err)
689 goto out_put_map;
f0a70c88 690
aab8f11a
DDG
691 if (!use_ptemod) {
692 for (i = 0; i < count; i++) {
693 err = vm_insert_page(vma, vma->vm_start + i*PAGE_SIZE,
694 map->pages[i]);
695 if (err)
90b6f305 696 goto out_put_map;
aab8f11a
DDG
697 }
698 }
699
f0a70c88
DDG
700 return 0;
701
ab31523c
GH
702unlock_out:
703 spin_unlock(&priv->lock);
704 return err;
90b6f305
DDG
705
706out_put_map:
84e4075d
DDG
707 if (use_ptemod)
708 map->vma = NULL;
90b6f305
DDG
709 gntdev_put_map(map);
710 return err;
ab31523c
GH
711}
712
713static const struct file_operations gntdev_fops = {
714 .owner = THIS_MODULE,
715 .open = gntdev_open,
716 .release = gntdev_release,
717 .mmap = gntdev_mmap,
718 .unlocked_ioctl = gntdev_ioctl
719};
720
721static struct miscdevice gntdev_miscdev = {
722 .minor = MISC_DYNAMIC_MINOR,
723 .name = "xen/gntdev",
724 .fops = &gntdev_fops,
725};
726
727/* ------------------------------------------------------------------ */
728
729static int __init gntdev_init(void)
730{
731 int err;
732
733 if (!xen_domain())
734 return -ENODEV;
735
aab8f11a
DDG
736 use_ptemod = xen_pv_domain();
737
ab31523c
GH
738 err = misc_register(&gntdev_miscdev);
739 if (err != 0) {
740 printk(KERN_ERR "Could not register gntdev device\n");
741 return err;
742 }
743 return 0;
744}
745
746static void __exit gntdev_exit(void)
747{
748 misc_deregister(&gntdev_miscdev);
749}
750
751module_init(gntdev_init);
752module_exit(gntdev_exit);
753
754/* ------------------------------------------------------------------ */