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