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 * (c) 2018 Oleksandr Andrushchenko, EPAM Systems Inc.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21 #undef DEBUG
22
23 #define pr_fmt(fmt) "xen:" KBUILD_MODNAME ": " fmt
24
25 #include <linux/module.h>
26 #include <linux/kernel.h>
27 #include <linux/init.h>
28 #include <linux/miscdevice.h>
29 #include <linux/fs.h>
30 #include <linux/uaccess.h>
31 #include <linux/sched.h>
32 #include <linux/sched/mm.h>
33 #include <linux/spinlock.h>
34 #include <linux/slab.h>
35 #include <linux/highmem.h>
36 #include <linux/refcount.h>
37 #ifdef CONFIG_XEN_GRANT_DMA_ALLOC
38 #include <linux/of_device.h>
39 #endif
40
41 #include <xen/xen.h>
42 #include <xen/grant_table.h>
43 #include <xen/balloon.h>
44 #include <xen/gntdev.h>
45 #include <xen/events.h>
46 #include <xen/page.h>
47 #include <asm/xen/hypervisor.h>
48 #include <asm/xen/hypercall.h>
49
50 #include "gntdev-common.h"
51 #ifdef CONFIG_XEN_GNTDEV_DMABUF
52 #include "gntdev-dmabuf.h"
53 #endif
54
55 MODULE_LICENSE("GPL");
56 MODULE_AUTHOR("Derek G. Murray <Derek.Murray@cl.cam.ac.uk>, "
57 "Gerd Hoffmann <kraxel@redhat.com>");
58 MODULE_DESCRIPTION("User-space granted page access driver");
59
60 static int limit = 1024*1024;
61 module_param(limit, int, 0644);
62 MODULE_PARM_DESC(limit, "Maximum number of grants that may be mapped by "
63 "the gntdev device");
64
65 static atomic_t pages_mapped = ATOMIC_INIT(0);
66
67 static int use_ptemod;
68 #define populate_freeable_maps use_ptemod
69
70 static int unmap_grant_pages(struct gntdev_grant_map *map,
71 int offset, int pages);
72
73 static struct miscdevice gntdev_miscdev;
74
75 /* ------------------------------------------------------------------ */
76
gntdev_account_mapped_pages(int count)77 bool gntdev_account_mapped_pages(int count)
78 {
79 return atomic_add_return(count, &pages_mapped) > limit;
80 }
81
gntdev_print_maps(struct gntdev_priv * priv,char * text,int text_index)82 static void gntdev_print_maps(struct gntdev_priv *priv,
83 char *text, int text_index)
84 {
85 #ifdef DEBUG
86 struct gntdev_grant_map *map;
87
88 pr_debug("%s: maps list (priv %p)\n", __func__, priv);
89 list_for_each_entry(map, &priv->maps, next)
90 pr_debug(" index %2d, count %2d %s\n",
91 map->index, map->count,
92 map->index == text_index && text ? text : "");
93 #endif
94 }
95
gntdev_free_map(struct gntdev_grant_map * map)96 static void gntdev_free_map(struct gntdev_grant_map *map)
97 {
98 if (map == NULL)
99 return;
100
101 #ifdef CONFIG_XEN_GRANT_DMA_ALLOC
102 if (map->dma_vaddr) {
103 struct gnttab_dma_alloc_args args;
104
105 args.dev = map->dma_dev;
106 args.coherent = !!(map->dma_flags & GNTDEV_DMA_FLAG_COHERENT);
107 args.nr_pages = map->count;
108 args.pages = map->pages;
109 args.frames = map->frames;
110 args.vaddr = map->dma_vaddr;
111 args.dev_bus_addr = map->dma_bus_addr;
112
113 gnttab_dma_free_pages(&args);
114 } else
115 #endif
116 if (map->pages)
117 gnttab_free_pages(map->count, map->pages);
118
119 #ifdef CONFIG_XEN_GRANT_DMA_ALLOC
120 kfree(map->frames);
121 #endif
122 kfree(map->pages);
123 kfree(map->grants);
124 kfree(map->map_ops);
125 kfree(map->unmap_ops);
126 kfree(map->kmap_ops);
127 kfree(map->kunmap_ops);
128 kfree(map);
129 }
130
gntdev_alloc_map(struct gntdev_priv * priv,int count,int dma_flags)131 struct gntdev_grant_map *gntdev_alloc_map(struct gntdev_priv *priv, int count,
132 int dma_flags)
133 {
134 struct gntdev_grant_map *add;
135 int i;
136
137 add = kzalloc(sizeof(*add), GFP_KERNEL);
138 if (NULL == add)
139 return NULL;
140
141 add->grants = kcalloc(count, sizeof(add->grants[0]), GFP_KERNEL);
142 add->map_ops = kcalloc(count, sizeof(add->map_ops[0]), GFP_KERNEL);
143 add->unmap_ops = kcalloc(count, sizeof(add->unmap_ops[0]), GFP_KERNEL);
144 add->kmap_ops = kcalloc(count, sizeof(add->kmap_ops[0]), GFP_KERNEL);
145 add->kunmap_ops = kcalloc(count, sizeof(add->kunmap_ops[0]), GFP_KERNEL);
146 add->pages = kcalloc(count, sizeof(add->pages[0]), GFP_KERNEL);
147 if (NULL == add->grants ||
148 NULL == add->map_ops ||
149 NULL == add->unmap_ops ||
150 NULL == add->kmap_ops ||
151 NULL == add->kunmap_ops ||
152 NULL == add->pages)
153 goto err;
154
155 #ifdef CONFIG_XEN_GRANT_DMA_ALLOC
156 add->dma_flags = dma_flags;
157
158 /*
159 * Check if this mapping is requested to be backed
160 * by a DMA buffer.
161 */
162 if (dma_flags & (GNTDEV_DMA_FLAG_WC | GNTDEV_DMA_FLAG_COHERENT)) {
163 struct gnttab_dma_alloc_args args;
164
165 add->frames = kcalloc(count, sizeof(add->frames[0]),
166 GFP_KERNEL);
167 if (!add->frames)
168 goto err;
169
170 /* Remember the device, so we can free DMA memory. */
171 add->dma_dev = priv->dma_dev;
172
173 args.dev = priv->dma_dev;
174 args.coherent = !!(dma_flags & GNTDEV_DMA_FLAG_COHERENT);
175 args.nr_pages = count;
176 args.pages = add->pages;
177 args.frames = add->frames;
178
179 if (gnttab_dma_alloc_pages(&args))
180 goto err;
181
182 add->dma_vaddr = args.vaddr;
183 add->dma_bus_addr = args.dev_bus_addr;
184 } else
185 #endif
186 if (gnttab_alloc_pages(count, add->pages))
187 goto err;
188
189 for (i = 0; i < count; i++) {
190 add->map_ops[i].handle = -1;
191 add->unmap_ops[i].handle = -1;
192 add->kmap_ops[i].handle = -1;
193 add->kunmap_ops[i].handle = -1;
194 }
195
196 add->index = 0;
197 add->count = count;
198 refcount_set(&add->users, 1);
199
200 return add;
201
202 err:
203 gntdev_free_map(add);
204 return NULL;
205 }
206
gntdev_add_map(struct gntdev_priv * priv,struct gntdev_grant_map * add)207 void gntdev_add_map(struct gntdev_priv *priv, struct gntdev_grant_map *add)
208 {
209 struct gntdev_grant_map *map;
210
211 list_for_each_entry(map, &priv->maps, next) {
212 if (add->index + add->count < map->index) {
213 list_add_tail(&add->next, &map->next);
214 goto done;
215 }
216 add->index = map->index + map->count;
217 }
218 list_add_tail(&add->next, &priv->maps);
219
220 done:
221 gntdev_print_maps(priv, "[new]", add->index);
222 }
223
gntdev_find_map_index(struct gntdev_priv * priv,int index,int count)224 static struct gntdev_grant_map *gntdev_find_map_index(struct gntdev_priv *priv,
225 int index, int count)
226 {
227 struct gntdev_grant_map *map;
228
229 list_for_each_entry(map, &priv->maps, next) {
230 if (map->index != index)
231 continue;
232 if (count && map->count != count)
233 continue;
234 return map;
235 }
236 return NULL;
237 }
238
gntdev_put_map(struct gntdev_priv * priv,struct gntdev_grant_map * map)239 void gntdev_put_map(struct gntdev_priv *priv, struct gntdev_grant_map *map)
240 {
241 if (!map)
242 return;
243
244 if (!refcount_dec_and_test(&map->users))
245 return;
246
247 atomic_sub(map->count, &pages_mapped);
248
249 if (map->notify.flags & UNMAP_NOTIFY_SEND_EVENT) {
250 notify_remote_via_evtchn(map->notify.event);
251 evtchn_put(map->notify.event);
252 }
253
254 if (populate_freeable_maps && priv) {
255 mutex_lock(&priv->lock);
256 list_del(&map->next);
257 mutex_unlock(&priv->lock);
258 }
259
260 if (map->pages && !use_ptemod)
261 unmap_grant_pages(map, 0, map->count);
262 gntdev_free_map(map);
263 }
264
265 /* ------------------------------------------------------------------ */
266
find_grant_ptes(pte_t * pte,pgtable_t token,unsigned long addr,void * data)267 static int find_grant_ptes(pte_t *pte, pgtable_t token,
268 unsigned long addr, void *data)
269 {
270 struct gntdev_grant_map *map = data;
271 unsigned int pgnr = (addr - map->vma->vm_start) >> PAGE_SHIFT;
272 int flags = map->flags | GNTMAP_application_map | GNTMAP_contains_pte;
273 u64 pte_maddr;
274
275 BUG_ON(pgnr >= map->count);
276 pte_maddr = arbitrary_virt_to_machine(pte).maddr;
277
278 /*
279 * Set the PTE as special to force get_user_pages_fast() fall
280 * back to the slow path. If this is not supported as part of
281 * the grant map, it will be done afterwards.
282 */
283 if (xen_feature(XENFEAT_gnttab_map_avail_bits))
284 flags |= (1 << _GNTMAP_guest_avail0);
285
286 gnttab_set_map_op(&map->map_ops[pgnr], pte_maddr, flags,
287 map->grants[pgnr].ref,
288 map->grants[pgnr].domid);
289 gnttab_set_unmap_op(&map->unmap_ops[pgnr], pte_maddr, flags,
290 -1 /* handle */);
291 return 0;
292 }
293
294 #ifdef CONFIG_X86
set_grant_ptes_as_special(pte_t * pte,pgtable_t token,unsigned long addr,void * data)295 static int set_grant_ptes_as_special(pte_t *pte, pgtable_t token,
296 unsigned long addr, void *data)
297 {
298 set_pte_at(current->mm, addr, pte, pte_mkspecial(*pte));
299 return 0;
300 }
301 #endif
302
gntdev_map_grant_pages(struct gntdev_grant_map * map)303 int gntdev_map_grant_pages(struct gntdev_grant_map *map)
304 {
305 int i, err = 0;
306
307 if (!use_ptemod) {
308 /* Note: it could already be mapped */
309 if (map->map_ops[0].handle != -1)
310 return 0;
311 for (i = 0; i < map->count; i++) {
312 unsigned long addr = (unsigned long)
313 pfn_to_kaddr(page_to_pfn(map->pages[i]));
314 gnttab_set_map_op(&map->map_ops[i], addr, map->flags,
315 map->grants[i].ref,
316 map->grants[i].domid);
317 gnttab_set_unmap_op(&map->unmap_ops[i], addr,
318 map->flags, -1 /* handle */);
319 }
320 } else {
321 /*
322 * Setup the map_ops corresponding to the pte entries pointing
323 * to the kernel linear addresses of the struct pages.
324 * These ptes are completely different from the user ptes dealt
325 * with find_grant_ptes.
326 * Note that GNTMAP_device_map isn't needed here: The
327 * dev_bus_addr output field gets consumed only from ->map_ops,
328 * and by not requesting it when mapping we also avoid needing
329 * to mirror dev_bus_addr into ->unmap_ops (and holding an extra
330 * reference to the page in the hypervisor).
331 */
332 unsigned int flags = (map->flags & ~GNTMAP_device_map) |
333 GNTMAP_host_map;
334
335 for (i = 0; i < map->count; i++) {
336 unsigned long address = (unsigned long)
337 pfn_to_kaddr(page_to_pfn(map->pages[i]));
338 BUG_ON(PageHighMem(map->pages[i]));
339
340 gnttab_set_map_op(&map->kmap_ops[i], address, flags,
341 map->grants[i].ref,
342 map->grants[i].domid);
343 gnttab_set_unmap_op(&map->kunmap_ops[i], address,
344 flags, -1);
345 }
346 }
347
348 pr_debug("map %d+%d\n", map->index, map->count);
349 err = gnttab_map_refs(map->map_ops, use_ptemod ? map->kmap_ops : NULL,
350 map->pages, map->count);
351
352 for (i = 0; i < map->count; i++) {
353 if (map->map_ops[i].status == GNTST_okay)
354 map->unmap_ops[i].handle = map->map_ops[i].handle;
355 else if (!err)
356 err = -EINVAL;
357
358 if (map->flags & GNTMAP_device_map)
359 map->unmap_ops[i].dev_bus_addr = map->map_ops[i].dev_bus_addr;
360
361 if (use_ptemod) {
362 if (map->kmap_ops[i].status == GNTST_okay)
363 map->kunmap_ops[i].handle = map->kmap_ops[i].handle;
364 else if (!err)
365 err = -EINVAL;
366 }
367 }
368 return err;
369 }
370
__unmap_grant_pages(struct gntdev_grant_map * map,int offset,int pages)371 static int __unmap_grant_pages(struct gntdev_grant_map *map, int offset,
372 int pages)
373 {
374 int i, err = 0;
375 struct gntab_unmap_queue_data unmap_data;
376
377 if (map->notify.flags & UNMAP_NOTIFY_CLEAR_BYTE) {
378 int pgno = (map->notify.addr >> PAGE_SHIFT);
379 if (pgno >= offset && pgno < offset + pages) {
380 /* No need for kmap, pages are in lowmem */
381 uint8_t *tmp = pfn_to_kaddr(page_to_pfn(map->pages[pgno]));
382 tmp[map->notify.addr & (PAGE_SIZE-1)] = 0;
383 map->notify.flags &= ~UNMAP_NOTIFY_CLEAR_BYTE;
384 }
385 }
386
387 unmap_data.unmap_ops = map->unmap_ops + offset;
388 unmap_data.kunmap_ops = use_ptemod ? map->kunmap_ops + offset : NULL;
389 unmap_data.pages = map->pages + offset;
390 unmap_data.count = pages;
391
392 err = gnttab_unmap_refs_sync(&unmap_data);
393 if (err)
394 return err;
395
396 for (i = 0; i < pages; i++) {
397 if (map->unmap_ops[offset+i].status)
398 err = -EINVAL;
399 pr_debug("unmap handle=%d st=%d\n",
400 map->unmap_ops[offset+i].handle,
401 map->unmap_ops[offset+i].status);
402 map->unmap_ops[offset+i].handle = -1;
403 }
404 return err;
405 }
406
unmap_grant_pages(struct gntdev_grant_map * map,int offset,int pages)407 static int unmap_grant_pages(struct gntdev_grant_map *map, int offset,
408 int pages)
409 {
410 int range, err = 0;
411
412 pr_debug("unmap %d+%d [%d+%d]\n", map->index, map->count, offset, pages);
413
414 /* It is possible the requested range will have a "hole" where we
415 * already unmapped some of the grants. Only unmap valid ranges.
416 */
417 while (pages && !err) {
418 while (pages && map->unmap_ops[offset].handle == -1) {
419 offset++;
420 pages--;
421 }
422 range = 0;
423 while (range < pages) {
424 if (map->unmap_ops[offset+range].handle == -1)
425 break;
426 range++;
427 }
428 err = __unmap_grant_pages(map, offset, range);
429 offset += range;
430 pages -= range;
431 }
432
433 return err;
434 }
435
436 /* ------------------------------------------------------------------ */
437
gntdev_vma_open(struct vm_area_struct * vma)438 static void gntdev_vma_open(struct vm_area_struct *vma)
439 {
440 struct gntdev_grant_map *map = vma->vm_private_data;
441
442 pr_debug("gntdev_vma_open %p\n", vma);
443 refcount_inc(&map->users);
444 }
445
gntdev_vma_close(struct vm_area_struct * vma)446 static void gntdev_vma_close(struct vm_area_struct *vma)
447 {
448 struct gntdev_grant_map *map = vma->vm_private_data;
449 struct file *file = vma->vm_file;
450 struct gntdev_priv *priv = file->private_data;
451
452 pr_debug("gntdev_vma_close %p\n", vma);
453 if (use_ptemod) {
454 /* It is possible that an mmu notifier could be running
455 * concurrently, so take priv->lock to ensure that the vma won't
456 * vanishing during the unmap_grant_pages call, since we will
457 * spin here until that completes. Such a concurrent call will
458 * not do any unmapping, since that has been done prior to
459 * closing the vma, but it may still iterate the unmap_ops list.
460 */
461 mutex_lock(&priv->lock);
462 map->vma = NULL;
463 mutex_unlock(&priv->lock);
464 }
465 vma->vm_private_data = NULL;
466 gntdev_put_map(priv, map);
467 }
468
gntdev_vma_find_special_page(struct vm_area_struct * vma,unsigned long addr)469 static struct page *gntdev_vma_find_special_page(struct vm_area_struct *vma,
470 unsigned long addr)
471 {
472 struct gntdev_grant_map *map = vma->vm_private_data;
473
474 return map->pages[(addr - map->pages_vm_start) >> PAGE_SHIFT];
475 }
476
477 static const struct vm_operations_struct gntdev_vmops = {
478 .open = gntdev_vma_open,
479 .close = gntdev_vma_close,
480 .find_special_page = gntdev_vma_find_special_page,
481 };
482
483 /* ------------------------------------------------------------------ */
484
in_range(struct gntdev_grant_map * map,unsigned long start,unsigned long end)485 static bool in_range(struct gntdev_grant_map *map,
486 unsigned long start, unsigned long end)
487 {
488 if (!map->vma)
489 return false;
490 if (map->vma->vm_start >= end)
491 return false;
492 if (map->vma->vm_end <= start)
493 return false;
494
495 return true;
496 }
497
unmap_if_in_range(struct gntdev_grant_map * map,unsigned long start,unsigned long end,bool blockable)498 static int unmap_if_in_range(struct gntdev_grant_map *map,
499 unsigned long start, unsigned long end,
500 bool blockable)
501 {
502 unsigned long mstart, mend;
503 int err;
504
505 if (!in_range(map, start, end))
506 return 0;
507
508 if (!blockable)
509 return -EAGAIN;
510
511 mstart = max(start, map->vma->vm_start);
512 mend = min(end, map->vma->vm_end);
513 pr_debug("map %d+%d (%lx %lx), range %lx %lx, mrange %lx %lx\n",
514 map->index, map->count,
515 map->vma->vm_start, map->vma->vm_end,
516 start, end, mstart, mend);
517 err = unmap_grant_pages(map,
518 (mstart - map->vma->vm_start) >> PAGE_SHIFT,
519 (mend - mstart) >> PAGE_SHIFT);
520 WARN_ON(err);
521
522 return 0;
523 }
524
mn_invl_range_start(struct mmu_notifier * mn,struct mm_struct * mm,unsigned long start,unsigned long end,bool blockable)525 static int mn_invl_range_start(struct mmu_notifier *mn,
526 struct mm_struct *mm,
527 unsigned long start, unsigned long end,
528 bool blockable)
529 {
530 struct gntdev_priv *priv = container_of(mn, struct gntdev_priv, mn);
531 struct gntdev_grant_map *map;
532 int ret = 0;
533
534 if (blockable)
535 mutex_lock(&priv->lock);
536 else if (!mutex_trylock(&priv->lock))
537 return -EAGAIN;
538
539 list_for_each_entry(map, &priv->maps, next) {
540 ret = unmap_if_in_range(map, start, end, blockable);
541 if (ret)
542 goto out_unlock;
543 }
544 list_for_each_entry(map, &priv->freeable_maps, next) {
545 ret = unmap_if_in_range(map, start, end, blockable);
546 if (ret)
547 goto out_unlock;
548 }
549
550 out_unlock:
551 mutex_unlock(&priv->lock);
552
553 return ret;
554 }
555
mn_release(struct mmu_notifier * mn,struct mm_struct * mm)556 static void mn_release(struct mmu_notifier *mn,
557 struct mm_struct *mm)
558 {
559 struct gntdev_priv *priv = container_of(mn, struct gntdev_priv, mn);
560 struct gntdev_grant_map *map;
561 int err;
562
563 mutex_lock(&priv->lock);
564 list_for_each_entry(map, &priv->maps, next) {
565 if (!map->vma)
566 continue;
567 pr_debug("map %d+%d (%lx %lx)\n",
568 map->index, map->count,
569 map->vma->vm_start, map->vma->vm_end);
570 err = unmap_grant_pages(map, /* offset */ 0, map->count);
571 WARN_ON(err);
572 }
573 list_for_each_entry(map, &priv->freeable_maps, next) {
574 if (!map->vma)
575 continue;
576 pr_debug("map %d+%d (%lx %lx)\n",
577 map->index, map->count,
578 map->vma->vm_start, map->vma->vm_end);
579 err = unmap_grant_pages(map, /* offset */ 0, map->count);
580 WARN_ON(err);
581 }
582 mutex_unlock(&priv->lock);
583 }
584
585 static const struct mmu_notifier_ops gntdev_mmu_ops = {
586 .release = mn_release,
587 .invalidate_range_start = mn_invl_range_start,
588 };
589
590 /* ------------------------------------------------------------------ */
591
gntdev_open(struct inode * inode,struct file * flip)592 static int gntdev_open(struct inode *inode, struct file *flip)
593 {
594 struct gntdev_priv *priv;
595 int ret = 0;
596
597 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
598 if (!priv)
599 return -ENOMEM;
600
601 INIT_LIST_HEAD(&priv->maps);
602 INIT_LIST_HEAD(&priv->freeable_maps);
603 mutex_init(&priv->lock);
604
605 #ifdef CONFIG_XEN_GNTDEV_DMABUF
606 priv->dmabuf_priv = gntdev_dmabuf_init(flip);
607 if (IS_ERR(priv->dmabuf_priv)) {
608 ret = PTR_ERR(priv->dmabuf_priv);
609 kfree(priv);
610 return ret;
611 }
612 #endif
613
614 if (use_ptemod) {
615 priv->mm = get_task_mm(current);
616 if (!priv->mm) {
617 kfree(priv);
618 return -ENOMEM;
619 }
620 priv->mn.ops = &gntdev_mmu_ops;
621 ret = mmu_notifier_register(&priv->mn, priv->mm);
622 mmput(priv->mm);
623 }
624
625 if (ret) {
626 kfree(priv);
627 return ret;
628 }
629
630 flip->private_data = priv;
631 #ifdef CONFIG_XEN_GRANT_DMA_ALLOC
632 priv->dma_dev = gntdev_miscdev.this_device;
633
634 /*
635 * The device is not spawn from a device tree, so arch_setup_dma_ops
636 * is not called, thus leaving the device with dummy DMA ops.
637 * Fix this by calling of_dma_configure() with a NULL node to set
638 * default DMA ops.
639 */
640 of_dma_configure(priv->dma_dev, NULL, true);
641 #endif
642 pr_debug("priv %p\n", priv);
643
644 return 0;
645 }
646
gntdev_release(struct inode * inode,struct file * flip)647 static int gntdev_release(struct inode *inode, struct file *flip)
648 {
649 struct gntdev_priv *priv = flip->private_data;
650 struct gntdev_grant_map *map;
651
652 pr_debug("priv %p\n", priv);
653
654 mutex_lock(&priv->lock);
655 while (!list_empty(&priv->maps)) {
656 map = list_entry(priv->maps.next,
657 struct gntdev_grant_map, next);
658 list_del(&map->next);
659 gntdev_put_map(NULL /* already removed */, map);
660 }
661 WARN_ON(!list_empty(&priv->freeable_maps));
662 mutex_unlock(&priv->lock);
663
664 #ifdef CONFIG_XEN_GNTDEV_DMABUF
665 gntdev_dmabuf_fini(priv->dmabuf_priv);
666 #endif
667
668 if (use_ptemod)
669 mmu_notifier_unregister(&priv->mn, priv->mm);
670
671 kfree(priv);
672 return 0;
673 }
674
gntdev_ioctl_map_grant_ref(struct gntdev_priv * priv,struct ioctl_gntdev_map_grant_ref __user * u)675 static long gntdev_ioctl_map_grant_ref(struct gntdev_priv *priv,
676 struct ioctl_gntdev_map_grant_ref __user *u)
677 {
678 struct ioctl_gntdev_map_grant_ref op;
679 struct gntdev_grant_map *map;
680 int err;
681
682 if (copy_from_user(&op, u, sizeof(op)) != 0)
683 return -EFAULT;
684 pr_debug("priv %p, add %d\n", priv, op.count);
685 if (unlikely(op.count <= 0))
686 return -EINVAL;
687
688 err = -ENOMEM;
689 map = gntdev_alloc_map(priv, op.count, 0 /* This is not a dma-buf. */);
690 if (!map)
691 return err;
692
693 if (unlikely(gntdev_account_mapped_pages(op.count))) {
694 pr_debug("can't map: over limit\n");
695 gntdev_put_map(NULL, map);
696 return err;
697 }
698
699 if (copy_from_user(map->grants, &u->refs,
700 sizeof(map->grants[0]) * op.count) != 0) {
701 gntdev_put_map(NULL, map);
702 return -EFAULT;
703 }
704
705 mutex_lock(&priv->lock);
706 gntdev_add_map(priv, map);
707 op.index = map->index << PAGE_SHIFT;
708 mutex_unlock(&priv->lock);
709
710 if (copy_to_user(u, &op, sizeof(op)) != 0)
711 return -EFAULT;
712
713 return 0;
714 }
715
gntdev_ioctl_unmap_grant_ref(struct gntdev_priv * priv,struct ioctl_gntdev_unmap_grant_ref __user * u)716 static long gntdev_ioctl_unmap_grant_ref(struct gntdev_priv *priv,
717 struct ioctl_gntdev_unmap_grant_ref __user *u)
718 {
719 struct ioctl_gntdev_unmap_grant_ref op;
720 struct gntdev_grant_map *map;
721 int err = -ENOENT;
722
723 if (copy_from_user(&op, u, sizeof(op)) != 0)
724 return -EFAULT;
725 pr_debug("priv %p, del %d+%d\n", priv, (int)op.index, (int)op.count);
726
727 mutex_lock(&priv->lock);
728 map = gntdev_find_map_index(priv, op.index >> PAGE_SHIFT, op.count);
729 if (map) {
730 list_del(&map->next);
731 if (populate_freeable_maps)
732 list_add_tail(&map->next, &priv->freeable_maps);
733 err = 0;
734 }
735 mutex_unlock(&priv->lock);
736 if (map)
737 gntdev_put_map(priv, map);
738 return err;
739 }
740
gntdev_ioctl_get_offset_for_vaddr(struct gntdev_priv * priv,struct ioctl_gntdev_get_offset_for_vaddr __user * u)741 static long gntdev_ioctl_get_offset_for_vaddr(struct gntdev_priv *priv,
742 struct ioctl_gntdev_get_offset_for_vaddr __user *u)
743 {
744 struct ioctl_gntdev_get_offset_for_vaddr op;
745 struct vm_area_struct *vma;
746 struct gntdev_grant_map *map;
747 int rv = -EINVAL;
748
749 if (copy_from_user(&op, u, sizeof(op)) != 0)
750 return -EFAULT;
751 pr_debug("priv %p, offset for vaddr %lx\n", priv, (unsigned long)op.vaddr);
752
753 down_read(¤t->mm->mmap_sem);
754 vma = find_vma(current->mm, op.vaddr);
755 if (!vma || vma->vm_ops != &gntdev_vmops)
756 goto out_unlock;
757
758 map = vma->vm_private_data;
759 if (!map)
760 goto out_unlock;
761
762 op.offset = map->index << PAGE_SHIFT;
763 op.count = map->count;
764 rv = 0;
765
766 out_unlock:
767 up_read(¤t->mm->mmap_sem);
768
769 if (rv == 0 && copy_to_user(u, &op, sizeof(op)) != 0)
770 return -EFAULT;
771 return rv;
772 }
773
gntdev_ioctl_notify(struct gntdev_priv * priv,void __user * u)774 static long gntdev_ioctl_notify(struct gntdev_priv *priv, void __user *u)
775 {
776 struct ioctl_gntdev_unmap_notify op;
777 struct gntdev_grant_map *map;
778 int rc;
779 int out_flags;
780 unsigned int out_event;
781
782 if (copy_from_user(&op, u, sizeof(op)))
783 return -EFAULT;
784
785 if (op.action & ~(UNMAP_NOTIFY_CLEAR_BYTE|UNMAP_NOTIFY_SEND_EVENT))
786 return -EINVAL;
787
788 /* We need to grab a reference to the event channel we are going to use
789 * to send the notify before releasing the reference we may already have
790 * (if someone has called this ioctl twice). This is required so that
791 * it is possible to change the clear_byte part of the notification
792 * without disturbing the event channel part, which may now be the last
793 * reference to that event channel.
794 */
795 if (op.action & UNMAP_NOTIFY_SEND_EVENT) {
796 if (evtchn_get(op.event_channel_port))
797 return -EINVAL;
798 }
799
800 out_flags = op.action;
801 out_event = op.event_channel_port;
802
803 mutex_lock(&priv->lock);
804
805 list_for_each_entry(map, &priv->maps, next) {
806 uint64_t begin = map->index << PAGE_SHIFT;
807 uint64_t end = (map->index + map->count) << PAGE_SHIFT;
808 if (op.index >= begin && op.index < end)
809 goto found;
810 }
811 rc = -ENOENT;
812 goto unlock_out;
813
814 found:
815 if ((op.action & UNMAP_NOTIFY_CLEAR_BYTE) &&
816 (map->flags & GNTMAP_readonly)) {
817 rc = -EINVAL;
818 goto unlock_out;
819 }
820
821 out_flags = map->notify.flags;
822 out_event = map->notify.event;
823
824 map->notify.flags = op.action;
825 map->notify.addr = op.index - (map->index << PAGE_SHIFT);
826 map->notify.event = op.event_channel_port;
827
828 rc = 0;
829
830 unlock_out:
831 mutex_unlock(&priv->lock);
832
833 /* Drop the reference to the event channel we did not save in the map */
834 if (out_flags & UNMAP_NOTIFY_SEND_EVENT)
835 evtchn_put(out_event);
836
837 return rc;
838 }
839
840 #define GNTDEV_COPY_BATCH 16
841
842 struct gntdev_copy_batch {
843 struct gnttab_copy ops[GNTDEV_COPY_BATCH];
844 struct page *pages[GNTDEV_COPY_BATCH];
845 s16 __user *status[GNTDEV_COPY_BATCH];
846 unsigned int nr_ops;
847 unsigned int nr_pages;
848 };
849
gntdev_get_page(struct gntdev_copy_batch * batch,void __user * virt,bool writeable,unsigned long * gfn)850 static int gntdev_get_page(struct gntdev_copy_batch *batch, void __user *virt,
851 bool writeable, unsigned long *gfn)
852 {
853 unsigned long addr = (unsigned long)virt;
854 struct page *page;
855 unsigned long xen_pfn;
856 int ret;
857
858 ret = get_user_pages_fast(addr, 1, writeable, &page);
859 if (ret < 0)
860 return ret;
861
862 batch->pages[batch->nr_pages++] = page;
863
864 xen_pfn = page_to_xen_pfn(page) + XEN_PFN_DOWN(addr & ~PAGE_MASK);
865 *gfn = pfn_to_gfn(xen_pfn);
866
867 return 0;
868 }
869
gntdev_put_pages(struct gntdev_copy_batch * batch)870 static void gntdev_put_pages(struct gntdev_copy_batch *batch)
871 {
872 unsigned int i;
873
874 for (i = 0; i < batch->nr_pages; i++)
875 put_page(batch->pages[i]);
876 batch->nr_pages = 0;
877 }
878
gntdev_copy(struct gntdev_copy_batch * batch)879 static int gntdev_copy(struct gntdev_copy_batch *batch)
880 {
881 unsigned int i;
882
883 gnttab_batch_copy(batch->ops, batch->nr_ops);
884 gntdev_put_pages(batch);
885
886 /*
887 * For each completed op, update the status if the op failed
888 * and all previous ops for the segment were successful.
889 */
890 for (i = 0; i < batch->nr_ops; i++) {
891 s16 status = batch->ops[i].status;
892 s16 old_status;
893
894 if (status == GNTST_okay)
895 continue;
896
897 if (__get_user(old_status, batch->status[i]))
898 return -EFAULT;
899
900 if (old_status != GNTST_okay)
901 continue;
902
903 if (__put_user(status, batch->status[i]))
904 return -EFAULT;
905 }
906
907 batch->nr_ops = 0;
908 return 0;
909 }
910
gntdev_grant_copy_seg(struct gntdev_copy_batch * batch,struct gntdev_grant_copy_segment * seg,s16 __user * status)911 static int gntdev_grant_copy_seg(struct gntdev_copy_batch *batch,
912 struct gntdev_grant_copy_segment *seg,
913 s16 __user *status)
914 {
915 uint16_t copied = 0;
916
917 /*
918 * Disallow local -> local copies since there is only space in
919 * batch->pages for one page per-op and this would be a very
920 * expensive memcpy().
921 */
922 if (!(seg->flags & (GNTCOPY_source_gref | GNTCOPY_dest_gref)))
923 return -EINVAL;
924
925 /* Can't cross page if source/dest is a grant ref. */
926 if (seg->flags & GNTCOPY_source_gref) {
927 if (seg->source.foreign.offset + seg->len > XEN_PAGE_SIZE)
928 return -EINVAL;
929 }
930 if (seg->flags & GNTCOPY_dest_gref) {
931 if (seg->dest.foreign.offset + seg->len > XEN_PAGE_SIZE)
932 return -EINVAL;
933 }
934
935 if (put_user(GNTST_okay, status))
936 return -EFAULT;
937
938 while (copied < seg->len) {
939 struct gnttab_copy *op;
940 void __user *virt;
941 size_t len, off;
942 unsigned long gfn;
943 int ret;
944
945 if (batch->nr_ops >= GNTDEV_COPY_BATCH) {
946 ret = gntdev_copy(batch);
947 if (ret < 0)
948 return ret;
949 }
950
951 len = seg->len - copied;
952
953 op = &batch->ops[batch->nr_ops];
954 op->flags = 0;
955
956 if (seg->flags & GNTCOPY_source_gref) {
957 op->source.u.ref = seg->source.foreign.ref;
958 op->source.domid = seg->source.foreign.domid;
959 op->source.offset = seg->source.foreign.offset + copied;
960 op->flags |= GNTCOPY_source_gref;
961 } else {
962 virt = seg->source.virt + copied;
963 off = (unsigned long)virt & ~XEN_PAGE_MASK;
964 len = min(len, (size_t)XEN_PAGE_SIZE - off);
965
966 ret = gntdev_get_page(batch, virt, false, &gfn);
967 if (ret < 0)
968 return ret;
969
970 op->source.u.gmfn = gfn;
971 op->source.domid = DOMID_SELF;
972 op->source.offset = off;
973 }
974
975 if (seg->flags & GNTCOPY_dest_gref) {
976 op->dest.u.ref = seg->dest.foreign.ref;
977 op->dest.domid = seg->dest.foreign.domid;
978 op->dest.offset = seg->dest.foreign.offset + copied;
979 op->flags |= GNTCOPY_dest_gref;
980 } else {
981 virt = seg->dest.virt + copied;
982 off = (unsigned long)virt & ~XEN_PAGE_MASK;
983 len = min(len, (size_t)XEN_PAGE_SIZE - off);
984
985 ret = gntdev_get_page(batch, virt, true, &gfn);
986 if (ret < 0)
987 return ret;
988
989 op->dest.u.gmfn = gfn;
990 op->dest.domid = DOMID_SELF;
991 op->dest.offset = off;
992 }
993
994 op->len = len;
995 copied += len;
996
997 batch->status[batch->nr_ops] = status;
998 batch->nr_ops++;
999 }
1000
1001 return 0;
1002 }
1003
gntdev_ioctl_grant_copy(struct gntdev_priv * priv,void __user * u)1004 static long gntdev_ioctl_grant_copy(struct gntdev_priv *priv, void __user *u)
1005 {
1006 struct ioctl_gntdev_grant_copy copy;
1007 struct gntdev_copy_batch batch;
1008 unsigned int i;
1009 int ret = 0;
1010
1011 if (copy_from_user(©, u, sizeof(copy)))
1012 return -EFAULT;
1013
1014 batch.nr_ops = 0;
1015 batch.nr_pages = 0;
1016
1017 for (i = 0; i < copy.count; i++) {
1018 struct gntdev_grant_copy_segment seg;
1019
1020 if (copy_from_user(&seg, ©.segments[i], sizeof(seg))) {
1021 ret = -EFAULT;
1022 goto out;
1023 }
1024
1025 ret = gntdev_grant_copy_seg(&batch, &seg, ©.segments[i].status);
1026 if (ret < 0)
1027 goto out;
1028
1029 cond_resched();
1030 }
1031 if (batch.nr_ops)
1032 ret = gntdev_copy(&batch);
1033 return ret;
1034
1035 out:
1036 gntdev_put_pages(&batch);
1037 return ret;
1038 }
1039
gntdev_ioctl(struct file * flip,unsigned int cmd,unsigned long arg)1040 static long gntdev_ioctl(struct file *flip,
1041 unsigned int cmd, unsigned long arg)
1042 {
1043 struct gntdev_priv *priv = flip->private_data;
1044 void __user *ptr = (void __user *)arg;
1045
1046 switch (cmd) {
1047 case IOCTL_GNTDEV_MAP_GRANT_REF:
1048 return gntdev_ioctl_map_grant_ref(priv, ptr);
1049
1050 case IOCTL_GNTDEV_UNMAP_GRANT_REF:
1051 return gntdev_ioctl_unmap_grant_ref(priv, ptr);
1052
1053 case IOCTL_GNTDEV_GET_OFFSET_FOR_VADDR:
1054 return gntdev_ioctl_get_offset_for_vaddr(priv, ptr);
1055
1056 case IOCTL_GNTDEV_SET_UNMAP_NOTIFY:
1057 return gntdev_ioctl_notify(priv, ptr);
1058
1059 case IOCTL_GNTDEV_GRANT_COPY:
1060 return gntdev_ioctl_grant_copy(priv, ptr);
1061
1062 #ifdef CONFIG_XEN_GNTDEV_DMABUF
1063 case IOCTL_GNTDEV_DMABUF_EXP_FROM_REFS:
1064 return gntdev_ioctl_dmabuf_exp_from_refs(priv, use_ptemod, ptr);
1065
1066 case IOCTL_GNTDEV_DMABUF_EXP_WAIT_RELEASED:
1067 return gntdev_ioctl_dmabuf_exp_wait_released(priv, ptr);
1068
1069 case IOCTL_GNTDEV_DMABUF_IMP_TO_REFS:
1070 return gntdev_ioctl_dmabuf_imp_to_refs(priv, ptr);
1071
1072 case IOCTL_GNTDEV_DMABUF_IMP_RELEASE:
1073 return gntdev_ioctl_dmabuf_imp_release(priv, ptr);
1074 #endif
1075
1076 default:
1077 pr_debug("priv %p, unknown cmd %x\n", priv, cmd);
1078 return -ENOIOCTLCMD;
1079 }
1080
1081 return 0;
1082 }
1083
gntdev_mmap(struct file * flip,struct vm_area_struct * vma)1084 static int gntdev_mmap(struct file *flip, struct vm_area_struct *vma)
1085 {
1086 struct gntdev_priv *priv = flip->private_data;
1087 int index = vma->vm_pgoff;
1088 int count = vma_pages(vma);
1089 struct gntdev_grant_map *map;
1090 int i, err = -EINVAL;
1091
1092 if ((vma->vm_flags & VM_WRITE) && !(vma->vm_flags & VM_SHARED))
1093 return -EINVAL;
1094
1095 pr_debug("map %d+%d at %lx (pgoff %lx)\n",
1096 index, count, vma->vm_start, vma->vm_pgoff);
1097
1098 mutex_lock(&priv->lock);
1099 map = gntdev_find_map_index(priv, index, count);
1100 if (!map)
1101 goto unlock_out;
1102 if (use_ptemod && map->vma)
1103 goto unlock_out;
1104 if (use_ptemod && priv->mm != vma->vm_mm) {
1105 pr_warn("Huh? Other mm?\n");
1106 goto unlock_out;
1107 }
1108
1109 refcount_inc(&map->users);
1110
1111 vma->vm_ops = &gntdev_vmops;
1112
1113 vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP | VM_MIXEDMAP;
1114
1115 if (use_ptemod)
1116 vma->vm_flags |= VM_DONTCOPY;
1117
1118 vma->vm_private_data = map;
1119
1120 if (use_ptemod)
1121 map->vma = vma;
1122
1123 if (map->flags) {
1124 if ((vma->vm_flags & VM_WRITE) &&
1125 (map->flags & GNTMAP_readonly))
1126 goto out_unlock_put;
1127 } else {
1128 map->flags = GNTMAP_host_map;
1129 if (!(vma->vm_flags & VM_WRITE))
1130 map->flags |= GNTMAP_readonly;
1131 }
1132
1133 mutex_unlock(&priv->lock);
1134
1135 if (use_ptemod) {
1136 map->pages_vm_start = vma->vm_start;
1137 err = apply_to_page_range(vma->vm_mm, vma->vm_start,
1138 vma->vm_end - vma->vm_start,
1139 find_grant_ptes, map);
1140 if (err) {
1141 pr_warn("find_grant_ptes() failure.\n");
1142 goto out_put_map;
1143 }
1144 }
1145
1146 err = gntdev_map_grant_pages(map);
1147 if (err)
1148 goto out_put_map;
1149
1150 if (!use_ptemod) {
1151 for (i = 0; i < count; i++) {
1152 err = vm_insert_page(vma, vma->vm_start + i*PAGE_SIZE,
1153 map->pages[i]);
1154 if (err)
1155 goto out_put_map;
1156 }
1157 } else {
1158 #ifdef CONFIG_X86
1159 /*
1160 * If the PTEs were not made special by the grant map
1161 * hypercall, do so here.
1162 *
1163 * This is racy since the mapping is already visible
1164 * to userspace but userspace should be well-behaved
1165 * enough to not touch it until the mmap() call
1166 * returns.
1167 */
1168 if (!xen_feature(XENFEAT_gnttab_map_avail_bits)) {
1169 apply_to_page_range(vma->vm_mm, vma->vm_start,
1170 vma->vm_end - vma->vm_start,
1171 set_grant_ptes_as_special, NULL);
1172 }
1173 #endif
1174 }
1175
1176 return 0;
1177
1178 unlock_out:
1179 mutex_unlock(&priv->lock);
1180 return err;
1181
1182 out_unlock_put:
1183 mutex_unlock(&priv->lock);
1184 out_put_map:
1185 if (use_ptemod) {
1186 map->vma = NULL;
1187 unmap_grant_pages(map, 0, map->count);
1188 }
1189 gntdev_put_map(priv, map);
1190 return err;
1191 }
1192
1193 static const struct file_operations gntdev_fops = {
1194 .owner = THIS_MODULE,
1195 .open = gntdev_open,
1196 .release = gntdev_release,
1197 .mmap = gntdev_mmap,
1198 .unlocked_ioctl = gntdev_ioctl
1199 };
1200
1201 static struct miscdevice gntdev_miscdev = {
1202 .minor = MISC_DYNAMIC_MINOR,
1203 .name = "xen/gntdev",
1204 .fops = &gntdev_fops,
1205 };
1206
1207 /* ------------------------------------------------------------------ */
1208
gntdev_init(void)1209 static int __init gntdev_init(void)
1210 {
1211 int err;
1212
1213 if (!xen_domain())
1214 return -ENODEV;
1215
1216 use_ptemod = !xen_feature(XENFEAT_auto_translated_physmap);
1217
1218 err = misc_register(&gntdev_miscdev);
1219 if (err != 0) {
1220 pr_err("Could not register gntdev device\n");
1221 return err;
1222 }
1223 return 0;
1224 }
1225
gntdev_exit(void)1226 static void __exit gntdev_exit(void)
1227 {
1228 misc_deregister(&gntdev_miscdev);
1229 }
1230
1231 module_init(gntdev_init);
1232 module_exit(gntdev_exit);
1233
1234 /* ------------------------------------------------------------------ */
1235