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