]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/xen/gntdev.c
xen-gntdev: Change page limit to be global instead of per-open
[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>
35
36#include <xen/xen.h>
37#include <xen/grant_table.h>
38#include <xen/gntdev.h>
39#include <asm/xen/hypervisor.h>
40#include <asm/xen/hypercall.h>
41#include <asm/xen/page.h>
42
43MODULE_LICENSE("GPL");
44MODULE_AUTHOR("Derek G. Murray <Derek.Murray@cl.cam.ac.uk>, "
45 "Gerd Hoffmann <kraxel@redhat.com>");
46MODULE_DESCRIPTION("User-space granted page access driver");
47
ef91082e 48static int limit = 1024*1024;
ab31523c 49module_param(limit, int, 0644);
ef91082e
DDG
50MODULE_PARM_DESC(limit, "Maximum number of grants that may be mapped by "
51 "the gntdev device");
52
53static atomic_t pages_mapped = ATOMIC_INIT(0);
ab31523c
GH
54
55struct gntdev_priv {
56 struct list_head maps;
ab31523c
GH
57 /* lock protects maps from concurrent changes */
58 spinlock_t lock;
59 struct mm_struct *mm;
60 struct mmu_notifier mn;
61};
62
63struct grant_map {
64 struct list_head next;
65 struct gntdev_priv *priv;
66 struct vm_area_struct *vma;
67 int index;
68 int count;
69 int flags;
70 int is_mapped;
71 struct ioctl_gntdev_grant_ref *grants;
72 struct gnttab_map_grant_ref *map_ops;
73 struct gnttab_unmap_grant_ref *unmap_ops;
a12b4eb3 74 struct page **pages;
ab31523c
GH
75};
76
77/* ------------------------------------------------------------------ */
78
79static void gntdev_print_maps(struct gntdev_priv *priv,
80 char *text, int text_index)
81{
82#ifdef DEBUG
83 struct grant_map *map;
84
ef91082e 85 pr_debug("%s: maps list (priv %p)\n", __func__, priv);
ab31523c
GH
86 list_for_each_entry(map, &priv->maps, next)
87 pr_debug(" index %2d, count %2d %s\n",
88 map->index, map->count,
89 map->index == text_index && text ? text : "");
90#endif
91}
92
93static struct grant_map *gntdev_alloc_map(struct gntdev_priv *priv, int count)
94{
95 struct grant_map *add;
a12b4eb3 96 int i;
ab31523c
GH
97
98 add = kzalloc(sizeof(struct grant_map), GFP_KERNEL);
99 if (NULL == add)
100 return NULL;
101
102 add->grants = kzalloc(sizeof(add->grants[0]) * count, GFP_KERNEL);
103 add->map_ops = kzalloc(sizeof(add->map_ops[0]) * count, GFP_KERNEL);
104 add->unmap_ops = kzalloc(sizeof(add->unmap_ops[0]) * count, GFP_KERNEL);
a12b4eb3
SS
105 add->pages = kzalloc(sizeof(add->pages[0]) * count, GFP_KERNEL);
106 if (NULL == add->grants ||
107 NULL == add->map_ops ||
108 NULL == add->unmap_ops ||
109 NULL == add->pages)
ab31523c
GH
110 goto err;
111
a12b4eb3
SS
112 for (i = 0; i < count; i++) {
113 add->pages[i] = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
114 if (add->pages[i] == NULL)
115 goto err;
116 }
117
ab31523c
GH
118 add->index = 0;
119 add->count = count;
120 add->priv = priv;
121
ab31523c
GH
122 return add;
123
124err:
a12b4eb3
SS
125 if (add->pages)
126 for (i = 0; i < count; i++) {
127 if (add->pages[i])
128 __free_page(add->pages[i]);
129 }
130 kfree(add->pages);
ab31523c
GH
131 kfree(add->grants);
132 kfree(add->map_ops);
133 kfree(add->unmap_ops);
134 kfree(add);
135 return NULL;
136}
137
138static void gntdev_add_map(struct gntdev_priv *priv, struct grant_map *add)
139{
140 struct grant_map *map;
141
142 list_for_each_entry(map, &priv->maps, next) {
143 if (add->index + add->count < map->index) {
144 list_add_tail(&add->next, &map->next);
145 goto done;
146 }
147 add->index = map->index + map->count;
148 }
149 list_add_tail(&add->next, &priv->maps);
150
151done:
ab31523c
GH
152 gntdev_print_maps(priv, "[new]", add->index);
153}
154
155static struct grant_map *gntdev_find_map_index(struct gntdev_priv *priv,
156 int index, int count)
157{
158 struct grant_map *map;
159
160 list_for_each_entry(map, &priv->maps, next) {
161 if (map->index != index)
162 continue;
163 if (map->count != count)
164 continue;
165 return map;
166 }
167 return NULL;
168}
169
170static struct grant_map *gntdev_find_map_vaddr(struct gntdev_priv *priv,
171 unsigned long vaddr)
172{
173 struct grant_map *map;
174
175 list_for_each_entry(map, &priv->maps, next) {
176 if (!map->vma)
177 continue;
178 if (vaddr < map->vma->vm_start)
179 continue;
180 if (vaddr >= map->vma->vm_end)
181 continue;
182 return map;
183 }
184 return NULL;
185}
186
187static int gntdev_del_map(struct grant_map *map)
188{
189 int i;
190
191 if (map->vma)
192 return -EBUSY;
193 for (i = 0; i < map->count; i++)
194 if (map->unmap_ops[i].handle)
195 return -EBUSY;
196
ef91082e 197 atomic_sub(map->count, &pages_mapped);
ab31523c
GH
198 list_del(&map->next);
199 return 0;
200}
201
202static void gntdev_free_map(struct grant_map *map)
203{
a12b4eb3
SS
204 int i;
205
ab31523c
GH
206 if (!map)
207 return;
a12b4eb3
SS
208
209 if (map->pages)
210 for (i = 0; i < map->count; i++) {
211 if (map->pages[i])
212 __free_page(map->pages[i]);
213 }
214 kfree(map->pages);
ab31523c
GH
215 kfree(map->grants);
216 kfree(map->map_ops);
217 kfree(map->unmap_ops);
218 kfree(map);
219}
220
221/* ------------------------------------------------------------------ */
222
223static int find_grant_ptes(pte_t *pte, pgtable_t token,
224 unsigned long addr, void *data)
225{
226 struct grant_map *map = data;
227 unsigned int pgnr = (addr - map->vma->vm_start) >> PAGE_SHIFT;
228 u64 pte_maddr;
229
230 BUG_ON(pgnr >= map->count);
ba5d1012
JF
231 pte_maddr = arbitrary_virt_to_machine(pte).maddr;
232
9329e760
IC
233 gnttab_set_map_op(&map->map_ops[pgnr], pte_maddr,
234 GNTMAP_contains_pte | map->flags,
ab31523c
GH
235 map->grants[pgnr].ref,
236 map->grants[pgnr].domid);
9329e760
IC
237 gnttab_set_unmap_op(&map->unmap_ops[pgnr], pte_maddr,
238 GNTMAP_contains_pte | map->flags,
ab31523c
GH
239 0 /* handle */);
240 return 0;
241}
242
243static int map_grant_pages(struct grant_map *map)
244{
245 int i, err = 0;
246
247 pr_debug("map %d+%d\n", map->index, map->count);
a12b4eb3 248 err = gnttab_map_refs(map->map_ops, map->pages, map->count);
ab31523c
GH
249 if (err)
250 return err;
251
252 for (i = 0; i < map->count; i++) {
253 if (map->map_ops[i].status)
254 err = -EINVAL;
255 map->unmap_ops[i].handle = map->map_ops[i].handle;
256 }
257 return err;
258}
259
260static int unmap_grant_pages(struct grant_map *map, int offset, int pages)
261{
262 int i, err = 0;
263
264 pr_debug("map %d+%d [%d+%d]\n", map->index, map->count, offset, pages);
a12b4eb3 265 err = gnttab_unmap_refs(map->unmap_ops + offset, map->pages, pages);
ab31523c
GH
266 if (err)
267 return err;
268
269 for (i = 0; i < pages; i++) {
270 if (map->unmap_ops[offset+i].status)
271 err = -EINVAL;
272 map->unmap_ops[offset+i].handle = 0;
273 }
274 return err;
275}
276
277/* ------------------------------------------------------------------ */
278
279static void gntdev_vma_close(struct vm_area_struct *vma)
280{
281 struct grant_map *map = vma->vm_private_data;
282
283 pr_debug("close %p\n", vma);
284 map->is_mapped = 0;
285 map->vma = NULL;
286 vma->vm_private_data = NULL;
287}
288
289static int gntdev_vma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
290{
291 pr_debug("vaddr %p, pgoff %ld (shouldn't happen)\n",
292 vmf->virtual_address, vmf->pgoff);
293 vmf->flags = VM_FAULT_ERROR;
294 return 0;
295}
296
297static struct vm_operations_struct gntdev_vmops = {
298 .close = gntdev_vma_close,
299 .fault = gntdev_vma_fault,
300};
301
302/* ------------------------------------------------------------------ */
303
304static void mn_invl_range_start(struct mmu_notifier *mn,
305 struct mm_struct *mm,
306 unsigned long start, unsigned long end)
307{
308 struct gntdev_priv *priv = container_of(mn, struct gntdev_priv, mn);
309 struct grant_map *map;
310 unsigned long mstart, mend;
311 int err;
312
313 spin_lock(&priv->lock);
314 list_for_each_entry(map, &priv->maps, next) {
315 if (!map->vma)
316 continue;
317 if (!map->is_mapped)
318 continue;
319 if (map->vma->vm_start >= end)
320 continue;
321 if (map->vma->vm_end <= start)
322 continue;
323 mstart = max(start, map->vma->vm_start);
324 mend = min(end, map->vma->vm_end);
325 pr_debug("map %d+%d (%lx %lx), range %lx %lx, mrange %lx %lx\n",
326 map->index, map->count,
327 map->vma->vm_start, map->vma->vm_end,
328 start, end, mstart, mend);
329 err = unmap_grant_pages(map,
330 (mstart - map->vma->vm_start) >> PAGE_SHIFT,
331 (mend - mstart) >> PAGE_SHIFT);
332 WARN_ON(err);
333 }
334 spin_unlock(&priv->lock);
335}
336
337static void mn_invl_page(struct mmu_notifier *mn,
338 struct mm_struct *mm,
339 unsigned long address)
340{
341 mn_invl_range_start(mn, mm, address, address + PAGE_SIZE);
342}
343
344static void mn_release(struct mmu_notifier *mn,
345 struct mm_struct *mm)
346{
347 struct gntdev_priv *priv = container_of(mn, struct gntdev_priv, mn);
348 struct grant_map *map;
349 int err;
350
351 spin_lock(&priv->lock);
352 list_for_each_entry(map, &priv->maps, next) {
353 if (!map->vma)
354 continue;
355 pr_debug("map %d+%d (%lx %lx)\n",
356 map->index, map->count,
357 map->vma->vm_start, map->vma->vm_end);
358 err = unmap_grant_pages(map, /* offset */ 0, map->count);
359 WARN_ON(err);
360 }
361 spin_unlock(&priv->lock);
362}
363
364struct mmu_notifier_ops gntdev_mmu_ops = {
365 .release = mn_release,
366 .invalidate_page = mn_invl_page,
367 .invalidate_range_start = mn_invl_range_start,
368};
369
370/* ------------------------------------------------------------------ */
371
372static int gntdev_open(struct inode *inode, struct file *flip)
373{
374 struct gntdev_priv *priv;
375 int ret = 0;
376
377 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
378 if (!priv)
379 return -ENOMEM;
380
381 INIT_LIST_HEAD(&priv->maps);
382 spin_lock_init(&priv->lock);
ab31523c
GH
383
384 priv->mm = get_task_mm(current);
385 if (!priv->mm) {
386 kfree(priv);
387 return -ENOMEM;
388 }
389 priv->mn.ops = &gntdev_mmu_ops;
390 ret = mmu_notifier_register(&priv->mn, priv->mm);
391 mmput(priv->mm);
392
393 if (ret) {
394 kfree(priv);
395 return ret;
396 }
397
398 flip->private_data = priv;
399 pr_debug("priv %p\n", priv);
400
401 return 0;
402}
403
404static int gntdev_release(struct inode *inode, struct file *flip)
405{
406 struct gntdev_priv *priv = flip->private_data;
407 struct grant_map *map;
408 int err;
409
410 pr_debug("priv %p\n", priv);
411
412 spin_lock(&priv->lock);
413 while (!list_empty(&priv->maps)) {
414 map = list_entry(priv->maps.next, struct grant_map, next);
415 err = gntdev_del_map(map);
416 if (WARN_ON(err))
417 gntdev_free_map(map);
418
419 }
420 spin_unlock(&priv->lock);
421
422 mmu_notifier_unregister(&priv->mn, priv->mm);
423 kfree(priv);
424 return 0;
425}
426
427static long gntdev_ioctl_map_grant_ref(struct gntdev_priv *priv,
428 struct ioctl_gntdev_map_grant_ref __user *u)
429{
430 struct ioctl_gntdev_map_grant_ref op;
431 struct grant_map *map;
432 int err;
433
434 if (copy_from_user(&op, u, sizeof(op)) != 0)
435 return -EFAULT;
436 pr_debug("priv %p, add %d\n", priv, op.count);
437 if (unlikely(op.count <= 0))
438 return -EINVAL;
ab31523c
GH
439
440 err = -ENOMEM;
441 map = gntdev_alloc_map(priv, op.count);
442 if (!map)
443 return err;
ef91082e 444
ab31523c
GH
445 if (copy_from_user(map->grants, &u->refs,
446 sizeof(map->grants[0]) * op.count) != 0) {
447 gntdev_free_map(map);
448 return err;
449 }
450
ef91082e
DDG
451 if (unlikely(atomic_add_return(op.count, &pages_mapped) > limit)) {
452 pr_debug("can't map: over limit\n");
453 gntdev_free_map(map);
454 return err;
455 }
456
ab31523c
GH
457 spin_lock(&priv->lock);
458 gntdev_add_map(priv, map);
459 op.index = map->index << PAGE_SHIFT;
460 spin_unlock(&priv->lock);
461
462 if (copy_to_user(u, &op, sizeof(op)) != 0) {
463 spin_lock(&priv->lock);
464 gntdev_del_map(map);
465 spin_unlock(&priv->lock);
466 gntdev_free_map(map);
467 return err;
468 }
469 return 0;
470}
471
472static long gntdev_ioctl_unmap_grant_ref(struct gntdev_priv *priv,
473 struct ioctl_gntdev_unmap_grant_ref __user *u)
474{
475 struct ioctl_gntdev_unmap_grant_ref op;
476 struct grant_map *map;
477 int err = -ENOENT;
478
479 if (copy_from_user(&op, u, sizeof(op)) != 0)
480 return -EFAULT;
481 pr_debug("priv %p, del %d+%d\n", priv, (int)op.index, (int)op.count);
482
483 spin_lock(&priv->lock);
484 map = gntdev_find_map_index(priv, op.index >> PAGE_SHIFT, op.count);
485 if (map)
486 err = gntdev_del_map(map);
487 spin_unlock(&priv->lock);
488 if (!err)
489 gntdev_free_map(map);
490 return err;
491}
492
493static long gntdev_ioctl_get_offset_for_vaddr(struct gntdev_priv *priv,
494 struct ioctl_gntdev_get_offset_for_vaddr __user *u)
495{
496 struct ioctl_gntdev_get_offset_for_vaddr op;
497 struct grant_map *map;
498
499 if (copy_from_user(&op, u, sizeof(op)) != 0)
500 return -EFAULT;
501 pr_debug("priv %p, offset for vaddr %lx\n", priv, (unsigned long)op.vaddr);
502
503 spin_lock(&priv->lock);
504 map = gntdev_find_map_vaddr(priv, op.vaddr);
505 if (map == NULL ||
506 map->vma->vm_start != op.vaddr) {
507 spin_unlock(&priv->lock);
508 return -EINVAL;
509 }
510 op.offset = map->index << PAGE_SHIFT;
511 op.count = map->count;
512 spin_unlock(&priv->lock);
513
514 if (copy_to_user(u, &op, sizeof(op)) != 0)
515 return -EFAULT;
516 return 0;
517}
518
ab31523c
GH
519static long gntdev_ioctl(struct file *flip,
520 unsigned int cmd, unsigned long arg)
521{
522 struct gntdev_priv *priv = flip->private_data;
523 void __user *ptr = (void __user *)arg;
524
525 switch (cmd) {
526 case IOCTL_GNTDEV_MAP_GRANT_REF:
527 return gntdev_ioctl_map_grant_ref(priv, ptr);
528
529 case IOCTL_GNTDEV_UNMAP_GRANT_REF:
530 return gntdev_ioctl_unmap_grant_ref(priv, ptr);
531
532 case IOCTL_GNTDEV_GET_OFFSET_FOR_VADDR:
533 return gntdev_ioctl_get_offset_for_vaddr(priv, ptr);
534
ab31523c
GH
535 default:
536 pr_debug("priv %p, unknown cmd %x\n", priv, cmd);
537 return -ENOIOCTLCMD;
538 }
539
540 return 0;
541}
542
543static int gntdev_mmap(struct file *flip, struct vm_area_struct *vma)
544{
545 struct gntdev_priv *priv = flip->private_data;
546 int index = vma->vm_pgoff;
547 int count = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
548 struct grant_map *map;
549 int err = -EINVAL;
550
551 if ((vma->vm_flags & VM_WRITE) && !(vma->vm_flags & VM_SHARED))
552 return -EINVAL;
553
554 pr_debug("map %d+%d at %lx (pgoff %lx)\n",
555 index, count, vma->vm_start, vma->vm_pgoff);
556
557 spin_lock(&priv->lock);
558 map = gntdev_find_map_index(priv, index, count);
559 if (!map)
560 goto unlock_out;
561 if (map->vma)
562 goto unlock_out;
563 if (priv->mm != vma->vm_mm) {
564 printk(KERN_WARNING "Huh? Other mm?\n");
565 goto unlock_out;
566 }
567
568 vma->vm_ops = &gntdev_vmops;
569
8d3eaea2 570 vma->vm_flags |= VM_RESERVED|VM_DONTCOPY|VM_DONTEXPAND|VM_PFNMAP;
ab31523c
GH
571
572 vma->vm_private_data = map;
573 map->vma = vma;
574
9329e760 575 map->flags = GNTMAP_host_map | GNTMAP_application_map;
ab31523c
GH
576 if (!(vma->vm_flags & VM_WRITE))
577 map->flags |= GNTMAP_readonly;
578
f0a70c88
DDG
579 spin_unlock(&priv->lock);
580
ab31523c
GH
581 err = apply_to_page_range(vma->vm_mm, vma->vm_start,
582 vma->vm_end - vma->vm_start,
583 find_grant_ptes, map);
584 if (err) {
585 printk(KERN_WARNING "find_grant_ptes() failure.\n");
f0a70c88 586 return err;
ab31523c
GH
587 }
588
589 err = map_grant_pages(map);
590 if (err) {
591 printk(KERN_WARNING "map_grant_pages() failure.\n");
f0a70c88 592 return err;
ab31523c 593 }
f0a70c88 594
ab31523c
GH
595 map->is_mapped = 1;
596
f0a70c88
DDG
597 return 0;
598
ab31523c
GH
599unlock_out:
600 spin_unlock(&priv->lock);
601 return err;
602}
603
604static const struct file_operations gntdev_fops = {
605 .owner = THIS_MODULE,
606 .open = gntdev_open,
607 .release = gntdev_release,
608 .mmap = gntdev_mmap,
609 .unlocked_ioctl = gntdev_ioctl
610};
611
612static struct miscdevice gntdev_miscdev = {
613 .minor = MISC_DYNAMIC_MINOR,
614 .name = "xen/gntdev",
615 .fops = &gntdev_fops,
616};
617
618/* ------------------------------------------------------------------ */
619
620static int __init gntdev_init(void)
621{
622 int err;
623
624 if (!xen_domain())
625 return -ENODEV;
626
627 err = misc_register(&gntdev_miscdev);
628 if (err != 0) {
629 printk(KERN_ERR "Could not register gntdev device\n");
630 return err;
631 }
632 return 0;
633}
634
635static void __exit gntdev_exit(void)
636{
637 misc_deregister(&gntdev_miscdev);
638}
639
640module_init(gntdev_init);
641module_exit(gntdev_exit);
642
643/* ------------------------------------------------------------------ */