]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/xen/gntdev.c
xen-balloon: Add interface to retrieve ballooned pages
[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;
77c35acb
DDG
129 add->map_ops[i].handle = -1;
130 add->unmap_ops[i].handle = -1;
a12b4eb3
SS
131 }
132
ab31523c
GH
133 add->index = 0;
134 add->count = count;
68b025c8 135 atomic_set(&add->users, 1);
ab31523c 136
ab31523c
GH
137 return add;
138
139err:
a12b4eb3
SS
140 if (add->pages)
141 for (i = 0; i < count; i++) {
142 if (add->pages[i])
143 __free_page(add->pages[i]);
144 }
145 kfree(add->pages);
ab31523c
GH
146 kfree(add->grants);
147 kfree(add->map_ops);
148 kfree(add->unmap_ops);
149 kfree(add);
150 return NULL;
151}
152
153static void gntdev_add_map(struct gntdev_priv *priv, struct grant_map *add)
154{
155 struct grant_map *map;
156
157 list_for_each_entry(map, &priv->maps, next) {
158 if (add->index + add->count < map->index) {
159 list_add_tail(&add->next, &map->next);
160 goto done;
161 }
162 add->index = map->index + map->count;
163 }
164 list_add_tail(&add->next, &priv->maps);
165
166done:
ab31523c
GH
167 gntdev_print_maps(priv, "[new]", add->index);
168}
169
170static struct grant_map *gntdev_find_map_index(struct gntdev_priv *priv,
171 int index, int count)
172{
173 struct grant_map *map;
174
175 list_for_each_entry(map, &priv->maps, next) {
176 if (map->index != index)
177 continue;
bdc612dc 178 if (count && map->count != count)
ab31523c
GH
179 continue;
180 return map;
181 }
182 return NULL;
183}
184
68b025c8 185static void gntdev_put_map(struct grant_map *map)
ab31523c 186{
a12b4eb3
SS
187 int i;
188
ab31523c
GH
189 if (!map)
190 return;
a12b4eb3 191
68b025c8
DDG
192 if (!atomic_dec_and_test(&map->users))
193 return;
194
195 atomic_sub(map->count, &pages_mapped);
196
bdc612dc
DDG
197 if (map->notify.flags & UNMAP_NOTIFY_SEND_EVENT) {
198 notify_remote_via_evtchn(map->notify.event);
199 }
200
aab8f11a
DDG
201 if (map->pages) {
202 if (!use_ptemod)
203 unmap_grant_pages(map, 0, map->count);
204
a12b4eb3 205 for (i = 0; i < map->count; i++) {
aab8f11a
DDG
206 uint32_t check, *tmp;
207 if (!map->pages[i])
208 continue;
209 /* XXX When unmapping in an HVM domain, Xen will
210 * sometimes end up mapping the GFN to an invalid MFN.
211 * In this case, writes will be discarded and reads will
212 * return all 0xFF bytes. Leak these unusable GFNs
213 * until Xen supports fixing their p2m mapping.
214 *
215 * Confirmed present in Xen 4.1-RC3 with HVM source
216 */
217 tmp = kmap(map->pages[i]);
218 *tmp = 0xdeaddead;
219 mb();
220 check = *tmp;
221 kunmap(map->pages[i]);
222 if (check == 0xdeaddead)
a12b4eb3 223 __free_page(map->pages[i]);
aab8f11a
DDG
224 else
225 pr_debug("Discard page %d=%ld\n", i,
226 page_to_pfn(map->pages[i]));
a12b4eb3 227 }
aab8f11a 228 }
a12b4eb3 229 kfree(map->pages);
ab31523c
GH
230 kfree(map->grants);
231 kfree(map->map_ops);
232 kfree(map->unmap_ops);
233 kfree(map);
234}
235
236/* ------------------------------------------------------------------ */
237
238static int find_grant_ptes(pte_t *pte, pgtable_t token,
239 unsigned long addr, void *data)
240{
241 struct grant_map *map = data;
242 unsigned int pgnr = (addr - map->vma->vm_start) >> PAGE_SHIFT;
aab8f11a 243 int flags = map->flags | GNTMAP_application_map | GNTMAP_contains_pte;
ab31523c
GH
244 u64 pte_maddr;
245
246 BUG_ON(pgnr >= map->count);
ba5d1012
JF
247 pte_maddr = arbitrary_virt_to_machine(pte).maddr;
248
aab8f11a 249 gnttab_set_map_op(&map->map_ops[pgnr], pte_maddr, flags,
ab31523c
GH
250 map->grants[pgnr].ref,
251 map->grants[pgnr].domid);
aab8f11a 252 gnttab_set_unmap_op(&map->unmap_ops[pgnr], pte_maddr, flags,
77c35acb 253 -1 /* handle */);
ab31523c
GH
254 return 0;
255}
256
257static int map_grant_pages(struct grant_map *map)
258{
259 int i, err = 0;
aab8f11a
DDG
260
261 if (!use_ptemod) {
12996fc3 262 /* Note: it could already be mapped */
77c35acb 263 if (map->map_ops[0].handle != -1)
12996fc3 264 return 0;
aab8f11a 265 for (i = 0; i < map->count; i++) {
38eaeb0f 266 unsigned long addr = (unsigned long)
aab8f11a
DDG
267 pfn_to_kaddr(page_to_pfn(map->pages[i]));
268 gnttab_set_map_op(&map->map_ops[i], addr, map->flags,
269 map->grants[i].ref,
270 map->grants[i].domid);
271 gnttab_set_unmap_op(&map->unmap_ops[i], addr,
77c35acb 272 map->flags, -1 /* handle */);
aab8f11a
DDG
273 }
274 }
ab31523c
GH
275
276 pr_debug("map %d+%d\n", map->index, map->count);
a12b4eb3 277 err = gnttab_map_refs(map->map_ops, map->pages, map->count);
ab31523c
GH
278 if (err)
279 return err;
280
281 for (i = 0; i < map->count; i++) {
282 if (map->map_ops[i].status)
283 err = -EINVAL;
77c35acb
DDG
284 else {
285 BUG_ON(map->map_ops[i].handle == -1);
286 map->unmap_ops[i].handle = map->map_ops[i].handle;
287 pr_debug("map handle=%d\n", map->map_ops[i].handle);
288 }
ab31523c
GH
289 }
290 return err;
291}
292
b57c1869 293static int __unmap_grant_pages(struct grant_map *map, int offset, int pages)
ab31523c
GH
294{
295 int i, err = 0;
296
bdc612dc
DDG
297 if (map->notify.flags & UNMAP_NOTIFY_CLEAR_BYTE) {
298 int pgno = (map->notify.addr >> PAGE_SHIFT);
0ea22f07 299 if (pgno >= offset && pgno < offset + pages && use_ptemod) {
f4ee4af4
DDG
300 void __user *tmp = (void __user *)
301 map->vma->vm_start + map->notify.addr;
9960be97
DDG
302 err = copy_to_user(tmp, &err, 1);
303 if (err)
304 return err;
0ea22f07
DDG
305 map->notify.flags &= ~UNMAP_NOTIFY_CLEAR_BYTE;
306 } else if (pgno >= offset && pgno < offset + pages) {
bdc612dc
DDG
307 uint8_t *tmp = kmap(map->pages[pgno]);
308 tmp[map->notify.addr & (PAGE_SIZE-1)] = 0;
309 kunmap(map->pages[pgno]);
310 map->notify.flags &= ~UNMAP_NOTIFY_CLEAR_BYTE;
311 }
312 }
313
0ea22f07 314 err = gnttab_unmap_refs(map->unmap_ops + offset, map->pages + offset, pages);
ab31523c
GH
315 if (err)
316 return err;
317
318 for (i = 0; i < pages; i++) {
319 if (map->unmap_ops[offset+i].status)
320 err = -EINVAL;
77c35acb
DDG
321 pr_debug("unmap handle=%d st=%d\n",
322 map->unmap_ops[offset+i].handle,
323 map->unmap_ops[offset+i].status);
324 map->unmap_ops[offset+i].handle = -1;
ab31523c
GH
325 }
326 return err;
327}
328
b57c1869
DDG
329static int unmap_grant_pages(struct grant_map *map, int offset, int pages)
330{
331 int range, err = 0;
332
333 pr_debug("unmap %d+%d [%d+%d]\n", map->index, map->count, offset, pages);
334
335 /* It is possible the requested range will have a "hole" where we
336 * already unmapped some of the grants. Only unmap valid ranges.
337 */
338 while (pages && !err) {
77c35acb 339 while (pages && map->unmap_ops[offset].handle == -1) {
b57c1869
DDG
340 offset++;
341 pages--;
342 }
343 range = 0;
344 while (range < pages) {
77c35acb 345 if (map->unmap_ops[offset+range].handle == -1) {
b57c1869
DDG
346 range--;
347 break;
348 }
349 range++;
350 }
351 err = __unmap_grant_pages(map, offset, range);
352 offset += range;
353 pages -= range;
354 }
355
356 return err;
357}
358
ab31523c
GH
359/* ------------------------------------------------------------------ */
360
361static void gntdev_vma_close(struct vm_area_struct *vma)
362{
363 struct grant_map *map = vma->vm_private_data;
364
365 pr_debug("close %p\n", vma);
ab31523c
GH
366 map->vma = NULL;
367 vma->vm_private_data = NULL;
68b025c8 368 gntdev_put_map(map);
ab31523c
GH
369}
370
ab31523c
GH
371static struct vm_operations_struct gntdev_vmops = {
372 .close = gntdev_vma_close,
ab31523c
GH
373};
374
375/* ------------------------------------------------------------------ */
376
377static void mn_invl_range_start(struct mmu_notifier *mn,
378 struct mm_struct *mm,
379 unsigned long start, unsigned long end)
380{
381 struct gntdev_priv *priv = container_of(mn, struct gntdev_priv, mn);
382 struct grant_map *map;
383 unsigned long mstart, mend;
384 int err;
385
386 spin_lock(&priv->lock);
387 list_for_each_entry(map, &priv->maps, next) {
388 if (!map->vma)
389 continue;
ab31523c
GH
390 if (map->vma->vm_start >= end)
391 continue;
392 if (map->vma->vm_end <= start)
393 continue;
394 mstart = max(start, map->vma->vm_start);
395 mend = min(end, map->vma->vm_end);
396 pr_debug("map %d+%d (%lx %lx), range %lx %lx, mrange %lx %lx\n",
397 map->index, map->count,
398 map->vma->vm_start, map->vma->vm_end,
399 start, end, mstart, mend);
400 err = unmap_grant_pages(map,
401 (mstart - map->vma->vm_start) >> PAGE_SHIFT,
402 (mend - mstart) >> PAGE_SHIFT);
403 WARN_ON(err);
404 }
405 spin_unlock(&priv->lock);
406}
407
408static void mn_invl_page(struct mmu_notifier *mn,
409 struct mm_struct *mm,
410 unsigned long address)
411{
412 mn_invl_range_start(mn, mm, address, address + PAGE_SIZE);
413}
414
415static void mn_release(struct mmu_notifier *mn,
416 struct mm_struct *mm)
417{
418 struct gntdev_priv *priv = container_of(mn, struct gntdev_priv, mn);
419 struct grant_map *map;
420 int err;
421
422 spin_lock(&priv->lock);
423 list_for_each_entry(map, &priv->maps, next) {
424 if (!map->vma)
425 continue;
426 pr_debug("map %d+%d (%lx %lx)\n",
427 map->index, map->count,
428 map->vma->vm_start, map->vma->vm_end);
429 err = unmap_grant_pages(map, /* offset */ 0, map->count);
430 WARN_ON(err);
431 }
432 spin_unlock(&priv->lock);
433}
434
435struct mmu_notifier_ops gntdev_mmu_ops = {
436 .release = mn_release,
437 .invalidate_page = mn_invl_page,
438 .invalidate_range_start = mn_invl_range_start,
439};
440
441/* ------------------------------------------------------------------ */
442
443static int gntdev_open(struct inode *inode, struct file *flip)
444{
445 struct gntdev_priv *priv;
446 int ret = 0;
447
448 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
449 if (!priv)
450 return -ENOMEM;
451
452 INIT_LIST_HEAD(&priv->maps);
453 spin_lock_init(&priv->lock);
ab31523c 454
aab8f11a
DDG
455 if (use_ptemod) {
456 priv->mm = get_task_mm(current);
457 if (!priv->mm) {
458 kfree(priv);
459 return -ENOMEM;
460 }
461 priv->mn.ops = &gntdev_mmu_ops;
462 ret = mmu_notifier_register(&priv->mn, priv->mm);
463 mmput(priv->mm);
ab31523c 464 }
ab31523c
GH
465
466 if (ret) {
467 kfree(priv);
468 return ret;
469 }
470
471 flip->private_data = priv;
472 pr_debug("priv %p\n", priv);
473
474 return 0;
475}
476
477static int gntdev_release(struct inode *inode, struct file *flip)
478{
479 struct gntdev_priv *priv = flip->private_data;
480 struct grant_map *map;
ab31523c
GH
481
482 pr_debug("priv %p\n", priv);
483
484 spin_lock(&priv->lock);
485 while (!list_empty(&priv->maps)) {
486 map = list_entry(priv->maps.next, struct grant_map, next);
68b025c8
DDG
487 list_del(&map->next);
488 gntdev_put_map(map);
ab31523c
GH
489 }
490 spin_unlock(&priv->lock);
491
aab8f11a
DDG
492 if (use_ptemod)
493 mmu_notifier_unregister(&priv->mn, priv->mm);
ab31523c
GH
494 kfree(priv);
495 return 0;
496}
497
498static long gntdev_ioctl_map_grant_ref(struct gntdev_priv *priv,
499 struct ioctl_gntdev_map_grant_ref __user *u)
500{
501 struct ioctl_gntdev_map_grant_ref op;
502 struct grant_map *map;
503 int err;
504
505 if (copy_from_user(&op, u, sizeof(op)) != 0)
506 return -EFAULT;
507 pr_debug("priv %p, add %d\n", priv, op.count);
508 if (unlikely(op.count <= 0))
509 return -EINVAL;
ab31523c
GH
510
511 err = -ENOMEM;
512 map = gntdev_alloc_map(priv, op.count);
513 if (!map)
514 return err;
ef91082e 515
68b025c8
DDG
516 if (unlikely(atomic_add_return(op.count, &pages_mapped) > limit)) {
517 pr_debug("can't map: over limit\n");
518 gntdev_put_map(map);
ab31523c
GH
519 return err;
520 }
521
68b025c8
DDG
522 if (copy_from_user(map->grants, &u->refs,
523 sizeof(map->grants[0]) * op.count) != 0) {
524 gntdev_put_map(map);
ef91082e
DDG
525 return err;
526 }
527
ab31523c
GH
528 spin_lock(&priv->lock);
529 gntdev_add_map(priv, map);
530 op.index = map->index << PAGE_SHIFT;
531 spin_unlock(&priv->lock);
532
68b025c8
DDG
533 if (copy_to_user(u, &op, sizeof(op)) != 0)
534 return -EFAULT;
535
ab31523c
GH
536 return 0;
537}
538
539static long gntdev_ioctl_unmap_grant_ref(struct gntdev_priv *priv,
540 struct ioctl_gntdev_unmap_grant_ref __user *u)
541{
542 struct ioctl_gntdev_unmap_grant_ref op;
543 struct grant_map *map;
544 int err = -ENOENT;
545
546 if (copy_from_user(&op, u, sizeof(op)) != 0)
547 return -EFAULT;
548 pr_debug("priv %p, del %d+%d\n", priv, (int)op.index, (int)op.count);
549
550 spin_lock(&priv->lock);
551 map = gntdev_find_map_index(priv, op.index >> PAGE_SHIFT, op.count);
68b025c8
DDG
552 if (map) {
553 list_del(&map->next);
554 gntdev_put_map(map);
555 err = 0;
556 }
ab31523c 557 spin_unlock(&priv->lock);
ab31523c
GH
558 return err;
559}
560
561static long gntdev_ioctl_get_offset_for_vaddr(struct gntdev_priv *priv,
562 struct ioctl_gntdev_get_offset_for_vaddr __user *u)
563{
564 struct ioctl_gntdev_get_offset_for_vaddr op;
a879211b 565 struct vm_area_struct *vma;
ab31523c
GH
566 struct grant_map *map;
567
568 if (copy_from_user(&op, u, sizeof(op)) != 0)
569 return -EFAULT;
570 pr_debug("priv %p, offset for vaddr %lx\n", priv, (unsigned long)op.vaddr);
571
a879211b
DDG
572 vma = find_vma(current->mm, op.vaddr);
573 if (!vma || vma->vm_ops != &gntdev_vmops)
ab31523c 574 return -EINVAL;
a879211b
DDG
575
576 map = vma->vm_private_data;
577 if (!map)
578 return -EINVAL;
579
ab31523c
GH
580 op.offset = map->index << PAGE_SHIFT;
581 op.count = map->count;
ab31523c
GH
582
583 if (copy_to_user(u, &op, sizeof(op)) != 0)
584 return -EFAULT;
585 return 0;
586}
587
bdc612dc
DDG
588static long gntdev_ioctl_notify(struct gntdev_priv *priv, void __user *u)
589{
590 struct ioctl_gntdev_unmap_notify op;
591 struct grant_map *map;
592 int rc;
593
594 if (copy_from_user(&op, u, sizeof(op)))
595 return -EFAULT;
596
597 if (op.action & ~(UNMAP_NOTIFY_CLEAR_BYTE|UNMAP_NOTIFY_SEND_EVENT))
598 return -EINVAL;
599
600 spin_lock(&priv->lock);
601
602 list_for_each_entry(map, &priv->maps, next) {
603 uint64_t begin = map->index << PAGE_SHIFT;
604 uint64_t end = (map->index + map->count) << PAGE_SHIFT;
605 if (op.index >= begin && op.index < end)
606 goto found;
607 }
608 rc = -ENOENT;
609 goto unlock_out;
610
611 found:
9960be97
DDG
612 if ((op.action & UNMAP_NOTIFY_CLEAR_BYTE) &&
613 (map->flags & GNTMAP_readonly)) {
614 rc = -EINVAL;
615 goto unlock_out;
616 }
617
bdc612dc
DDG
618 map->notify.flags = op.action;
619 map->notify.addr = op.index - (map->index << PAGE_SHIFT);
620 map->notify.event = op.event_channel_port;
621 rc = 0;
622 unlock_out:
623 spin_unlock(&priv->lock);
624 return rc;
625}
626
ab31523c
GH
627static long gntdev_ioctl(struct file *flip,
628 unsigned int cmd, unsigned long arg)
629{
630 struct gntdev_priv *priv = flip->private_data;
631 void __user *ptr = (void __user *)arg;
632
633 switch (cmd) {
634 case IOCTL_GNTDEV_MAP_GRANT_REF:
635 return gntdev_ioctl_map_grant_ref(priv, ptr);
636
637 case IOCTL_GNTDEV_UNMAP_GRANT_REF:
638 return gntdev_ioctl_unmap_grant_ref(priv, ptr);
639
640 case IOCTL_GNTDEV_GET_OFFSET_FOR_VADDR:
641 return gntdev_ioctl_get_offset_for_vaddr(priv, ptr);
642
bdc612dc
DDG
643 case IOCTL_GNTDEV_SET_UNMAP_NOTIFY:
644 return gntdev_ioctl_notify(priv, ptr);
645
ab31523c
GH
646 default:
647 pr_debug("priv %p, unknown cmd %x\n", priv, cmd);
648 return -ENOIOCTLCMD;
649 }
650
651 return 0;
652}
653
654static int gntdev_mmap(struct file *flip, struct vm_area_struct *vma)
655{
656 struct gntdev_priv *priv = flip->private_data;
657 int index = vma->vm_pgoff;
658 int count = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
659 struct grant_map *map;
aab8f11a 660 int i, err = -EINVAL;
ab31523c
GH
661
662 if ((vma->vm_flags & VM_WRITE) && !(vma->vm_flags & VM_SHARED))
663 return -EINVAL;
664
665 pr_debug("map %d+%d at %lx (pgoff %lx)\n",
666 index, count, vma->vm_start, vma->vm_pgoff);
667
668 spin_lock(&priv->lock);
669 map = gntdev_find_map_index(priv, index, count);
670 if (!map)
671 goto unlock_out;
aab8f11a 672 if (use_ptemod && map->vma)
ab31523c 673 goto unlock_out;
aab8f11a 674 if (use_ptemod && priv->mm != vma->vm_mm) {
ab31523c
GH
675 printk(KERN_WARNING "Huh? Other mm?\n");
676 goto unlock_out;
677 }
678
68b025c8
DDG
679 atomic_inc(&map->users);
680
ab31523c
GH
681 vma->vm_ops = &gntdev_vmops;
682
8d3eaea2 683 vma->vm_flags |= VM_RESERVED|VM_DONTCOPY|VM_DONTEXPAND|VM_PFNMAP;
ab31523c
GH
684
685 vma->vm_private_data = map;
ab31523c 686
aab8f11a
DDG
687 if (use_ptemod)
688 map->vma = vma;
689
12996fc3
DDG
690 if (map->flags) {
691 if ((vma->vm_flags & VM_WRITE) &&
692 (map->flags & GNTMAP_readonly))
693 return -EINVAL;
694 } else {
695 map->flags = GNTMAP_host_map;
696 if (!(vma->vm_flags & VM_WRITE))
697 map->flags |= GNTMAP_readonly;
698 }
ab31523c 699
f0a70c88
DDG
700 spin_unlock(&priv->lock);
701
aab8f11a
DDG
702 if (use_ptemod) {
703 err = apply_to_page_range(vma->vm_mm, vma->vm_start,
704 vma->vm_end - vma->vm_start,
705 find_grant_ptes, map);
706 if (err) {
707 printk(KERN_WARNING "find_grant_ptes() failure.\n");
90b6f305 708 goto out_put_map;
aab8f11a 709 }
ab31523c
GH
710 }
711
712 err = map_grant_pages(map);
90b6f305
DDG
713 if (err)
714 goto out_put_map;
f0a70c88 715
aab8f11a
DDG
716 if (!use_ptemod) {
717 for (i = 0; i < count; i++) {
718 err = vm_insert_page(vma, vma->vm_start + i*PAGE_SIZE,
719 map->pages[i]);
720 if (err)
90b6f305 721 goto out_put_map;
aab8f11a
DDG
722 }
723 }
724
f0a70c88
DDG
725 return 0;
726
ab31523c
GH
727unlock_out:
728 spin_unlock(&priv->lock);
729 return err;
90b6f305
DDG
730
731out_put_map:
84e4075d
DDG
732 if (use_ptemod)
733 map->vma = NULL;
90b6f305
DDG
734 gntdev_put_map(map);
735 return err;
ab31523c
GH
736}
737
738static const struct file_operations gntdev_fops = {
739 .owner = THIS_MODULE,
740 .open = gntdev_open,
741 .release = gntdev_release,
742 .mmap = gntdev_mmap,
743 .unlocked_ioctl = gntdev_ioctl
744};
745
746static struct miscdevice gntdev_miscdev = {
747 .minor = MISC_DYNAMIC_MINOR,
748 .name = "xen/gntdev",
749 .fops = &gntdev_fops,
750};
751
752/* ------------------------------------------------------------------ */
753
754static int __init gntdev_init(void)
755{
756 int err;
757
758 if (!xen_domain())
759 return -ENODEV;
760
aab8f11a
DDG
761 use_ptemod = xen_pv_domain();
762
ab31523c
GH
763 err = misc_register(&gntdev_miscdev);
764 if (err != 0) {
765 printk(KERN_ERR "Could not register gntdev device\n");
766 return err;
767 }
768 return 0;
769}
770
771static void __exit gntdev_exit(void)
772{
773 misc_deregister(&gntdev_miscdev);
774}
775
776module_init(gntdev_init);
777module_exit(gntdev_exit);
778
779/* ------------------------------------------------------------------ */