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