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