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