]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - drivers/xen/gntdev.c
clk: sifive: Fix kernel-doc
[mirror_ubuntu-jammy-kernel.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
0cc678f8 253 if (map->notify.flags & UNMAP_NOTIFY_SEND_EVENT) {
bdc612dc 254 notify_remote_via_evtchn(map->notify.event);
0cc678f8
DDG
255 evtchn_put(map->notify.event);
256 }
bdc612dc 257
a67baeb7
DV
258 if (map->pages && !use_ptemod)
259 unmap_grant_pages(map, 0, map->count);
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;
aab8f11a 269 int flags = map->flags | GNTMAP_application_map | GNTMAP_contains_pte;
ab31523c
GH
270 u64 pte_maddr;
271
272 BUG_ON(pgnr >= map->count);
ba5d1012
JF
273 pte_maddr = arbitrary_virt_to_machine(pte).maddr;
274
923b2919
DV
275 /*
276 * Set the PTE as special to force get_user_pages_fast() fall
277 * back to the slow path. If this is not supported as part of
278 * the grant map, it will be done afterwards.
279 */
280 if (xen_feature(XENFEAT_gnttab_map_avail_bits))
281 flags |= (1 << _GNTMAP_guest_avail0);
282
aab8f11a 283 gnttab_set_map_op(&map->map_ops[pgnr], pte_maddr, flags,
ab31523c
GH
284 map->grants[pgnr].ref,
285 map->grants[pgnr].domid);
aab8f11a 286 gnttab_set_unmap_op(&map->unmap_ops[pgnr], pte_maddr, flags,
bce21a2b 287 INVALID_GRANT_HANDLE);
ab31523c
GH
288 return 0;
289}
290
923b2919 291#ifdef CONFIG_X86
8b1e0f81 292static int set_grant_ptes_as_special(pte_t *pte, unsigned long addr, void *data)
923b2919
DV
293{
294 set_pte_at(current->mm, addr, pte, pte_mkspecial(*pte));
295 return 0;
296}
297#endif
298
1d314567 299int gntdev_map_grant_pages(struct gntdev_grant_map *map)
ab31523c
GH
300{
301 int i, err = 0;
aab8f11a
DDG
302
303 if (!use_ptemod) {
12996fc3 304 /* Note: it could already be mapped */
bce21a2b 305 if (map->map_ops[0].handle != INVALID_GRANT_HANDLE)
12996fc3 306 return 0;
aab8f11a 307 for (i = 0; i < map->count; i++) {
38eaeb0f 308 unsigned long addr = (unsigned long)
aab8f11a
DDG
309 pfn_to_kaddr(page_to_pfn(map->pages[i]));
310 gnttab_set_map_op(&map->map_ops[i], addr, map->flags,
311 map->grants[i].ref,
312 map->grants[i].domid);
313 gnttab_set_unmap_op(&map->unmap_ops[i], addr,
bce21a2b 314 map->flags, INVALID_GRANT_HANDLE);
aab8f11a 315 }
0930bba6
SS
316 } else {
317 /*
318 * Setup the map_ops corresponding to the pte entries pointing
319 * to the kernel linear addresses of the struct pages.
320 * These ptes are completely different from the user ptes dealt
321 * with find_grant_ptes.
dbe52836
JB
322 * Note that GNTMAP_device_map isn't needed here: The
323 * dev_bus_addr output field gets consumed only from ->map_ops,
324 * and by not requesting it when mapping we also avoid needing
325 * to mirror dev_bus_addr into ->unmap_ops (and holding an extra
326 * reference to the page in the hypervisor).
0930bba6 327 */
dbe52836
JB
328 unsigned int flags = (map->flags & ~GNTMAP_device_map) |
329 GNTMAP_host_map;
330
0930bba6 331 for (i = 0; i < map->count; i++) {
0930bba6
SS
332 unsigned long address = (unsigned long)
333 pfn_to_kaddr(page_to_pfn(map->pages[i]));
0930bba6
SS
334 BUG_ON(PageHighMem(map->pages[i]));
335
dbe52836 336 gnttab_set_map_op(&map->kmap_ops[i], address, flags,
0930bba6
SS
337 map->grants[i].ref,
338 map->grants[i].domid);
853d0289 339 gnttab_set_unmap_op(&map->kunmap_ops[i], address,
bce21a2b 340 flags, INVALID_GRANT_HANDLE);
0930bba6 341 }
aab8f11a 342 }
ab31523c
GH
343
344 pr_debug("map %d+%d\n", map->index, map->count);
36caa3fe
JB
345 err = gnttab_map_refs(map->map_ops, map->kmap_ops, map->pages,
346 map->count);
ab31523c
GH
347
348 for (i = 0; i < map->count; i++) {
ebee0eab
JB
349 if (map->map_ops[i].status == GNTST_okay)
350 map->unmap_ops[i].handle = map->map_ops[i].handle;
351 else if (!err)
ab31523c 352 err = -EINVAL;
853d0289 353
dbe52836
JB
354 if (map->flags & GNTMAP_device_map)
355 map->unmap_ops[i].dev_bus_addr = map->map_ops[i].dev_bus_addr;
356
ebee0eab
JB
357 if (use_ptemod) {
358 if (map->kmap_ops[i].status == GNTST_okay)
359 map->kunmap_ops[i].handle = map->kmap_ops[i].handle;
360 else if (!err)
361 err = -EINVAL;
362 }
ab31523c
GH
363 }
364 return err;
365}
366
1d314567
OA
367static int __unmap_grant_pages(struct gntdev_grant_map *map, int offset,
368 int pages)
ab31523c
GH
369{
370 int i, err = 0;
74528225 371 struct gntab_unmap_queue_data unmap_data;
ab31523c 372
bdc612dc
DDG
373 if (map->notify.flags & UNMAP_NOTIFY_CLEAR_BYTE) {
374 int pgno = (map->notify.addr >> PAGE_SHIFT);
1affa98d
DDG
375 if (pgno >= offset && pgno < offset + pages) {
376 /* No need for kmap, pages are in lowmem */
377 uint8_t *tmp = pfn_to_kaddr(page_to_pfn(map->pages[pgno]));
bdc612dc 378 tmp[map->notify.addr & (PAGE_SIZE-1)] = 0;
bdc612dc
DDG
379 map->notify.flags &= ~UNMAP_NOTIFY_CLEAR_BYTE;
380 }
381 }
382
74528225
JH
383 unmap_data.unmap_ops = map->unmap_ops + offset;
384 unmap_data.kunmap_ops = use_ptemod ? map->kunmap_ops + offset : NULL;
385 unmap_data.pages = map->pages + offset;
386 unmap_data.count = pages;
387
b44166cd
BL
388 err = gnttab_unmap_refs_sync(&unmap_data);
389 if (err)
390 return err;
ab31523c
GH
391
392 for (i = 0; i < pages; i++) {
393 if (map->unmap_ops[offset+i].status)
394 err = -EINVAL;
77c35acb
DDG
395 pr_debug("unmap handle=%d st=%d\n",
396 map->unmap_ops[offset+i].handle,
397 map->unmap_ops[offset+i].status);
bce21a2b 398 map->unmap_ops[offset+i].handle = INVALID_GRANT_HANDLE;
ab31523c
GH
399 }
400 return err;
401}
402
1d314567
OA
403static int unmap_grant_pages(struct gntdev_grant_map *map, int offset,
404 int pages)
b57c1869
DDG
405{
406 int range, err = 0;
407
408 pr_debug("unmap %d+%d [%d+%d]\n", map->index, map->count, offset, pages);
409
410 /* It is possible the requested range will have a "hole" where we
411 * already unmapped some of the grants. Only unmap valid ranges.
412 */
413 while (pages && !err) {
bce21a2b
JB
414 while (pages &&
415 map->unmap_ops[offset].handle == INVALID_GRANT_HANDLE) {
b57c1869
DDG
416 offset++;
417 pages--;
418 }
419 range = 0;
420 while (range < pages) {
bce21a2b
JB
421 if (map->unmap_ops[offset + range].handle ==
422 INVALID_GRANT_HANDLE)
b57c1869 423 break;
b57c1869
DDG
424 range++;
425 }
426 err = __unmap_grant_pages(map, offset, range);
427 offset += range;
428 pages -= range;
429 }
430
431 return err;
432}
433
ab31523c
GH
434/* ------------------------------------------------------------------ */
435
d79647ae
DDG
436static void gntdev_vma_open(struct vm_area_struct *vma)
437{
1d314567 438 struct gntdev_grant_map *map = vma->vm_private_data;
d79647ae
DDG
439
440 pr_debug("gntdev_vma_open %p\n", vma);
c5f7c5a9 441 refcount_inc(&map->users);
d79647ae
DDG
442}
443
ab31523c
GH
444static void gntdev_vma_close(struct vm_area_struct *vma)
445{
1d314567 446 struct gntdev_grant_map *map = vma->vm_private_data;
16a1d022
DDG
447 struct file *file = vma->vm_file;
448 struct gntdev_priv *priv = file->private_data;
ab31523c 449
d79647ae 450 pr_debug("gntdev_vma_close %p\n", vma);
2512f298 451 if (use_ptemod) {
d3eeb1d7
JG
452 WARN_ON(map->vma != vma);
453 mmu_interval_notifier_remove(&map->notifier);
2512f298 454 map->vma = NULL;
2512f298 455 }
ab31523c 456 vma->vm_private_data = NULL;
16a1d022 457 gntdev_put_map(priv, map);
ab31523c
GH
458}
459
dab069c6
DV
460static struct page *gntdev_vma_find_special_page(struct vm_area_struct *vma,
461 unsigned long addr)
462{
1d314567 463 struct gntdev_grant_map *map = vma->vm_private_data;
dab069c6
DV
464
465 return map->pages[(addr - map->pages_vm_start) >> PAGE_SHIFT];
466}
467
7cbea8dc 468static const struct vm_operations_struct gntdev_vmops = {
d79647ae 469 .open = gntdev_vma_open,
ab31523c 470 .close = gntdev_vma_close,
dab069c6 471 .find_special_page = gntdev_vma_find_special_page,
ab31523c
GH
472};
473
474/* ------------------------------------------------------------------ */
475
d3eeb1d7
JG
476static bool gntdev_invalidate(struct mmu_interval_notifier *mn,
477 const struct mmu_notifier_range *range,
478 unsigned long cur_seq)
16a1d022 479{
d3eeb1d7
JG
480 struct gntdev_grant_map *map =
481 container_of(mn, struct gntdev_grant_map, notifier);
16a1d022
DDG
482 unsigned long mstart, mend;
483 int err;
484
d3eeb1d7
JG
485 if (!mmu_notifier_range_blockable(range))
486 return false;
58a57569 487
d3eeb1d7
JG
488 /*
489 * If the VMA is split or otherwise changed the notifier is not
490 * updated, but we don't want to process VA's outside the modified
491 * VMA. FIXME: It would be much more understandable to just prevent
492 * modifying the VMA in the first place.
493 */
494 if (map->vma->vm_start >= range->end ||
495 map->vma->vm_end <= range->start)
496 return true;
58a57569 497
d3eeb1d7
JG
498 mstart = max(range->start, map->vma->vm_start);
499 mend = min(range->end, map->vma->vm_end);
16a1d022
DDG
500 pr_debug("map %d+%d (%lx %lx), range %lx %lx, mrange %lx %lx\n",
501 map->index, map->count,
502 map->vma->vm_start, map->vma->vm_end,
d3eeb1d7 503 range->start, range->end, mstart, mend);
16a1d022
DDG
504 err = unmap_grant_pages(map,
505 (mstart - map->vma->vm_start) >> PAGE_SHIFT,
506 (mend - mstart) >> PAGE_SHIFT);
507 WARN_ON(err);
58a57569 508
d3eeb1d7 509 return true;
ab31523c
GH
510}
511
d3eeb1d7
JG
512static const struct mmu_interval_notifier_ops gntdev_mmu_ops = {
513 .invalidate = gntdev_invalidate,
ab31523c
GH
514};
515
516/* ------------------------------------------------------------------ */
517
518static int gntdev_open(struct inode *inode, struct file *flip)
519{
520 struct gntdev_priv *priv;
ab31523c
GH
521
522 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
523 if (!priv)
524 return -ENOMEM;
525
526 INIT_LIST_HEAD(&priv->maps);
1401c00e 527 mutex_init(&priv->lock);
ab31523c 528
932d6562 529#ifdef CONFIG_XEN_GNTDEV_DMABUF
fa13e665 530 priv->dmabuf_priv = gntdev_dmabuf_init(flip);
932d6562 531 if (IS_ERR(priv->dmabuf_priv)) {
d41b26d8 532 int ret = PTR_ERR(priv->dmabuf_priv);
932d6562 533
ab31523c
GH
534 kfree(priv);
535 return ret;
536 }
d41b26d8 537#endif
ab31523c
GH
538
539 flip->private_data = priv;
975ef7ff
OA
540#ifdef CONFIG_XEN_GRANT_DMA_ALLOC
541 priv->dma_dev = gntdev_miscdev.this_device;
ee7f5225 542 dma_coerce_mask_and_coherent(priv->dma_dev, DMA_BIT_MASK(64));
975ef7ff 543#endif
ab31523c
GH
544 pr_debug("priv %p\n", priv);
545
546 return 0;
547}
548
549static int gntdev_release(struct inode *inode, struct file *flip)
550{
551 struct gntdev_priv *priv = flip->private_data;
1d314567 552 struct gntdev_grant_map *map;
ab31523c
GH
553
554 pr_debug("priv %p\n", priv);
555
30b03d05 556 mutex_lock(&priv->lock);
ab31523c 557 while (!list_empty(&priv->maps)) {
1d314567
OA
558 map = list_entry(priv->maps.next,
559 struct gntdev_grant_map, next);
68b025c8 560 list_del(&map->next);
16a1d022 561 gntdev_put_map(NULL /* already removed */, map);
ab31523c 562 }
30b03d05 563 mutex_unlock(&priv->lock);
ab31523c 564
932d6562
OA
565#ifdef CONFIG_XEN_GNTDEV_DMABUF
566 gntdev_dmabuf_fini(priv->dmabuf_priv);
567#endif
568
ab31523c
GH
569 kfree(priv);
570 return 0;
571}
572
573static long gntdev_ioctl_map_grant_ref(struct gntdev_priv *priv,
574 struct ioctl_gntdev_map_grant_ref __user *u)
575{
576 struct ioctl_gntdev_map_grant_ref op;
1d314567 577 struct gntdev_grant_map *map;
ab31523c
GH
578 int err;
579
580 if (copy_from_user(&op, u, sizeof(op)) != 0)
581 return -EFAULT;
582 pr_debug("priv %p, add %d\n", priv, op.count);
3b06ac67 583 if (unlikely(gntdev_test_page_count(op.count)))
ab31523c 584 return -EINVAL;
ab31523c
GH
585
586 err = -ENOMEM;
975ef7ff 587 map = gntdev_alloc_map(priv, op.count, 0 /* This is not a dma-buf. */);
ab31523c
GH
588 if (!map)
589 return err;
ef91082e 590
68b025c8
DDG
591 if (copy_from_user(map->grants, &u->refs,
592 sizeof(map->grants[0]) * op.count) != 0) {
16a1d022
DDG
593 gntdev_put_map(NULL, map);
594 return -EFAULT;
ef91082e
DDG
595 }
596
1401c00e 597 mutex_lock(&priv->lock);
ab31523c
GH
598 gntdev_add_map(priv, map);
599 op.index = map->index << PAGE_SHIFT;
1401c00e 600 mutex_unlock(&priv->lock);
ab31523c 601
68b025c8
DDG
602 if (copy_to_user(u, &op, sizeof(op)) != 0)
603 return -EFAULT;
604
ab31523c
GH
605 return 0;
606}
607
608static long gntdev_ioctl_unmap_grant_ref(struct gntdev_priv *priv,
609 struct ioctl_gntdev_unmap_grant_ref __user *u)
610{
611 struct ioctl_gntdev_unmap_grant_ref op;
1d314567 612 struct gntdev_grant_map *map;
ab31523c
GH
613 int err = -ENOENT;
614
615 if (copy_from_user(&op, u, sizeof(op)) != 0)
616 return -EFAULT;
617 pr_debug("priv %p, del %d+%d\n", priv, (int)op.index, (int)op.count);
618
1401c00e 619 mutex_lock(&priv->lock);
ab31523c 620 map = gntdev_find_map_index(priv, op.index >> PAGE_SHIFT, op.count);
68b025c8
DDG
621 if (map) {
622 list_del(&map->next);
68b025c8
DDG
623 err = 0;
624 }
1401c00e 625 mutex_unlock(&priv->lock);
1f1503ba 626 if (map)
16a1d022 627 gntdev_put_map(priv, map);
ab31523c
GH
628 return err;
629}
630
631static long gntdev_ioctl_get_offset_for_vaddr(struct gntdev_priv *priv,
632 struct ioctl_gntdev_get_offset_for_vaddr __user *u)
633{
634 struct ioctl_gntdev_get_offset_for_vaddr op;
a879211b 635 struct vm_area_struct *vma;
1d314567 636 struct gntdev_grant_map *map;
2512f298 637 int rv = -EINVAL;
ab31523c
GH
638
639 if (copy_from_user(&op, u, sizeof(op)) != 0)
640 return -EFAULT;
641 pr_debug("priv %p, offset for vaddr %lx\n", priv, (unsigned long)op.vaddr);
642
d8ed45c5 643 mmap_read_lock(current->mm);
a879211b
DDG
644 vma = find_vma(current->mm, op.vaddr);
645 if (!vma || vma->vm_ops != &gntdev_vmops)
2512f298 646 goto out_unlock;
a879211b
DDG
647
648 map = vma->vm_private_data;
649 if (!map)
2512f298 650 goto out_unlock;
a879211b 651
ab31523c
GH
652 op.offset = map->index << PAGE_SHIFT;
653 op.count = map->count;
2512f298 654 rv = 0;
ab31523c 655
2512f298 656 out_unlock:
d8ed45c5 657 mmap_read_unlock(current->mm);
2512f298
DDG
658
659 if (rv == 0 && copy_to_user(u, &op, sizeof(op)) != 0)
ab31523c 660 return -EFAULT;
2512f298 661 return rv;
ab31523c
GH
662}
663
bdc612dc
DDG
664static long gntdev_ioctl_notify(struct gntdev_priv *priv, void __user *u)
665{
666 struct ioctl_gntdev_unmap_notify op;
1d314567 667 struct gntdev_grant_map *map;
bdc612dc 668 int rc;
0cc678f8 669 int out_flags;
0102e4ef 670 evtchn_port_t out_event;
bdc612dc
DDG
671
672 if (copy_from_user(&op, u, sizeof(op)))
673 return -EFAULT;
674
675 if (op.action & ~(UNMAP_NOTIFY_CLEAR_BYTE|UNMAP_NOTIFY_SEND_EVENT))
676 return -EINVAL;
677
0cc678f8
DDG
678 /* We need to grab a reference to the event channel we are going to use
679 * to send the notify before releasing the reference we may already have
680 * (if someone has called this ioctl twice). This is required so that
681 * it is possible to change the clear_byte part of the notification
682 * without disturbing the event channel part, which may now be the last
683 * reference to that event channel.
684 */
685 if (op.action & UNMAP_NOTIFY_SEND_EVENT) {
686 if (evtchn_get(op.event_channel_port))
687 return -EINVAL;
688 }
689
690 out_flags = op.action;
691 out_event = op.event_channel_port;
692
1401c00e 693 mutex_lock(&priv->lock);
bdc612dc
DDG
694
695 list_for_each_entry(map, &priv->maps, next) {
696 uint64_t begin = map->index << PAGE_SHIFT;
697 uint64_t end = (map->index + map->count) << PAGE_SHIFT;
698 if (op.index >= begin && op.index < end)
699 goto found;
700 }
701 rc = -ENOENT;
702 goto unlock_out;
703
704 found:
9960be97
DDG
705 if ((op.action & UNMAP_NOTIFY_CLEAR_BYTE) &&
706 (map->flags & GNTMAP_readonly)) {
707 rc = -EINVAL;
708 goto unlock_out;
709 }
710
0cc678f8
DDG
711 out_flags = map->notify.flags;
712 out_event = map->notify.event;
713
bdc612dc
DDG
714 map->notify.flags = op.action;
715 map->notify.addr = op.index - (map->index << PAGE_SHIFT);
716 map->notify.event = op.event_channel_port;
0cc678f8 717
bdc612dc 718 rc = 0;
0cc678f8 719
bdc612dc 720 unlock_out:
1401c00e 721 mutex_unlock(&priv->lock);
0cc678f8
DDG
722
723 /* Drop the reference to the event channel we did not save in the map */
724 if (out_flags & UNMAP_NOTIFY_SEND_EVENT)
725 evtchn_put(out_event);
726
bdc612dc
DDG
727 return rc;
728}
729
36ae220a 730#define GNTDEV_COPY_BATCH 16
a4cdb556
DV
731
732struct gntdev_copy_batch {
733 struct gnttab_copy ops[GNTDEV_COPY_BATCH];
734 struct page *pages[GNTDEV_COPY_BATCH];
735 s16 __user *status[GNTDEV_COPY_BATCH];
736 unsigned int nr_ops;
737 unsigned int nr_pages;
77905584 738 bool writeable;
a4cdb556
DV
739};
740
741static int gntdev_get_page(struct gntdev_copy_batch *batch, void __user *virt,
77905584 742 unsigned long *gfn)
a4cdb556
DV
743{
744 unsigned long addr = (unsigned long)virt;
745 struct page *page;
746 unsigned long xen_pfn;
747 int ret;
748
d6bbc2ff 749 ret = pin_user_pages_fast(addr, 1, batch->writeable ? FOLL_WRITE : 0, &page);
a4cdb556
DV
750 if (ret < 0)
751 return ret;
752
753 batch->pages[batch->nr_pages++] = page;
754
755 xen_pfn = page_to_xen_pfn(page) + XEN_PFN_DOWN(addr & ~PAGE_MASK);
756 *gfn = pfn_to_gfn(xen_pfn);
757
758 return 0;
759}
760
761static void gntdev_put_pages(struct gntdev_copy_batch *batch)
762{
d6bbc2ff 763 unpin_user_pages_dirty_lock(batch->pages, batch->nr_pages, batch->writeable);
a4cdb556 764 batch->nr_pages = 0;
77905584 765 batch->writeable = false;
a4cdb556
DV
766}
767
768static int gntdev_copy(struct gntdev_copy_batch *batch)
769{
770 unsigned int i;
771
772 gnttab_batch_copy(batch->ops, batch->nr_ops);
773 gntdev_put_pages(batch);
774
775 /*
776 * For each completed op, update the status if the op failed
777 * and all previous ops for the segment were successful.
778 */
779 for (i = 0; i < batch->nr_ops; i++) {
780 s16 status = batch->ops[i].status;
781 s16 old_status;
782
783 if (status == GNTST_okay)
784 continue;
785
786 if (__get_user(old_status, batch->status[i]))
787 return -EFAULT;
788
789 if (old_status != GNTST_okay)
790 continue;
791
792 if (__put_user(status, batch->status[i]))
793 return -EFAULT;
794 }
795
796 batch->nr_ops = 0;
797 return 0;
798}
799
800static int gntdev_grant_copy_seg(struct gntdev_copy_batch *batch,
801 struct gntdev_grant_copy_segment *seg,
802 s16 __user *status)
803{
804 uint16_t copied = 0;
805
806 /*
807 * Disallow local -> local copies since there is only space in
808 * batch->pages for one page per-op and this would be a very
809 * expensive memcpy().
810 */
811 if (!(seg->flags & (GNTCOPY_source_gref | GNTCOPY_dest_gref)))
812 return -EINVAL;
813
814 /* Can't cross page if source/dest is a grant ref. */
815 if (seg->flags & GNTCOPY_source_gref) {
816 if (seg->source.foreign.offset + seg->len > XEN_PAGE_SIZE)
817 return -EINVAL;
818 }
819 if (seg->flags & GNTCOPY_dest_gref) {
820 if (seg->dest.foreign.offset + seg->len > XEN_PAGE_SIZE)
821 return -EINVAL;
822 }
823
824 if (put_user(GNTST_okay, status))
825 return -EFAULT;
826
827 while (copied < seg->len) {
828 struct gnttab_copy *op;
829 void __user *virt;
830 size_t len, off;
831 unsigned long gfn;
832 int ret;
833
834 if (batch->nr_ops >= GNTDEV_COPY_BATCH) {
835 ret = gntdev_copy(batch);
836 if (ret < 0)
837 return ret;
838 }
839
840 len = seg->len - copied;
841
842 op = &batch->ops[batch->nr_ops];
843 op->flags = 0;
844
845 if (seg->flags & GNTCOPY_source_gref) {
846 op->source.u.ref = seg->source.foreign.ref;
847 op->source.domid = seg->source.foreign.domid;
848 op->source.offset = seg->source.foreign.offset + copied;
849 op->flags |= GNTCOPY_source_gref;
850 } else {
851 virt = seg->source.virt + copied;
852 off = (unsigned long)virt & ~XEN_PAGE_MASK;
853 len = min(len, (size_t)XEN_PAGE_SIZE - off);
77905584 854 batch->writeable = false;
a4cdb556 855
77905584 856 ret = gntdev_get_page(batch, virt, &gfn);
a4cdb556
DV
857 if (ret < 0)
858 return ret;
859
860 op->source.u.gmfn = gfn;
861 op->source.domid = DOMID_SELF;
862 op->source.offset = off;
863 }
864
865 if (seg->flags & GNTCOPY_dest_gref) {
866 op->dest.u.ref = seg->dest.foreign.ref;
867 op->dest.domid = seg->dest.foreign.domid;
868 op->dest.offset = seg->dest.foreign.offset + copied;
869 op->flags |= GNTCOPY_dest_gref;
870 } else {
871 virt = seg->dest.virt + copied;
872 off = (unsigned long)virt & ~XEN_PAGE_MASK;
873 len = min(len, (size_t)XEN_PAGE_SIZE - off);
77905584 874 batch->writeable = true;
a4cdb556 875
77905584 876 ret = gntdev_get_page(batch, virt, &gfn);
a4cdb556
DV
877 if (ret < 0)
878 return ret;
879
880 op->dest.u.gmfn = gfn;
881 op->dest.domid = DOMID_SELF;
882 op->dest.offset = off;
883 }
884
885 op->len = len;
886 copied += len;
887
888 batch->status[batch->nr_ops] = status;
889 batch->nr_ops++;
890 }
891
892 return 0;
893}
894
895static long gntdev_ioctl_grant_copy(struct gntdev_priv *priv, void __user *u)
896{
897 struct ioctl_gntdev_grant_copy copy;
898 struct gntdev_copy_batch batch;
899 unsigned int i;
900 int ret = 0;
901
902 if (copy_from_user(&copy, u, sizeof(copy)))
903 return -EFAULT;
904
905 batch.nr_ops = 0;
906 batch.nr_pages = 0;
907
908 for (i = 0; i < copy.count; i++) {
909 struct gntdev_grant_copy_segment seg;
910
911 if (copy_from_user(&seg, &copy.segments[i], sizeof(seg))) {
912 ret = -EFAULT;
913 goto out;
914 }
915
916 ret = gntdev_grant_copy_seg(&batch, &seg, &copy.segments[i].status);
917 if (ret < 0)
918 goto out;
919
920 cond_resched();
921 }
922 if (batch.nr_ops)
923 ret = gntdev_copy(&batch);
924 return ret;
925
926 out:
927 gntdev_put_pages(&batch);
928 return ret;
929}
930
ab31523c
GH
931static long gntdev_ioctl(struct file *flip,
932 unsigned int cmd, unsigned long arg)
933{
934 struct gntdev_priv *priv = flip->private_data;
935 void __user *ptr = (void __user *)arg;
936
937 switch (cmd) {
938 case IOCTL_GNTDEV_MAP_GRANT_REF:
939 return gntdev_ioctl_map_grant_ref(priv, ptr);
940
941 case IOCTL_GNTDEV_UNMAP_GRANT_REF:
942 return gntdev_ioctl_unmap_grant_ref(priv, ptr);
943
944 case IOCTL_GNTDEV_GET_OFFSET_FOR_VADDR:
945 return gntdev_ioctl_get_offset_for_vaddr(priv, ptr);
946
bdc612dc
DDG
947 case IOCTL_GNTDEV_SET_UNMAP_NOTIFY:
948 return gntdev_ioctl_notify(priv, ptr);
949
a4cdb556
DV
950 case IOCTL_GNTDEV_GRANT_COPY:
951 return gntdev_ioctl_grant_copy(priv, ptr);
952
932d6562
OA
953#ifdef CONFIG_XEN_GNTDEV_DMABUF
954 case IOCTL_GNTDEV_DMABUF_EXP_FROM_REFS:
955 return gntdev_ioctl_dmabuf_exp_from_refs(priv, use_ptemod, ptr);
956
957 case IOCTL_GNTDEV_DMABUF_EXP_WAIT_RELEASED:
958 return gntdev_ioctl_dmabuf_exp_wait_released(priv, ptr);
959
960 case IOCTL_GNTDEV_DMABUF_IMP_TO_REFS:
961 return gntdev_ioctl_dmabuf_imp_to_refs(priv, ptr);
962
963 case IOCTL_GNTDEV_DMABUF_IMP_RELEASE:
964 return gntdev_ioctl_dmabuf_imp_release(priv, ptr);
965#endif
966
ab31523c
GH
967 default:
968 pr_debug("priv %p, unknown cmd %x\n", priv, cmd);
969 return -ENOIOCTLCMD;
970 }
971
972 return 0;
973}
974
975static int gntdev_mmap(struct file *flip, struct vm_area_struct *vma)
976{
977 struct gntdev_priv *priv = flip->private_data;
978 int index = vma->vm_pgoff;
c7ebf9d9 979 int count = vma_pages(vma);
1d314567 980 struct gntdev_grant_map *map;
df9bde01 981 int err = -EINVAL;
ab31523c
GH
982
983 if ((vma->vm_flags & VM_WRITE) && !(vma->vm_flags & VM_SHARED))
984 return -EINVAL;
985
986 pr_debug("map %d+%d at %lx (pgoff %lx)\n",
987 index, count, vma->vm_start, vma->vm_pgoff);
988
1401c00e 989 mutex_lock(&priv->lock);
ab31523c
GH
990 map = gntdev_find_map_index(priv, index, count);
991 if (!map)
992 goto unlock_out;
aab8f11a 993 if (use_ptemod && map->vma)
ab31523c 994 goto unlock_out;
c5f7c5a9 995 refcount_inc(&map->users);
68b025c8 996
ab31523c
GH
997 vma->vm_ops = &gntdev_vmops;
998
30faaafd 999 vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP | VM_MIXEDMAP;
d79647ae
DDG
1000
1001 if (use_ptemod)
e8e937be 1002 vma->vm_flags |= VM_DONTCOPY;
ab31523c
GH
1003
1004 vma->vm_private_data = map;
12996fc3
DDG
1005 if (map->flags) {
1006 if ((vma->vm_flags & VM_WRITE) &&
1007 (map->flags & GNTMAP_readonly))
a93e20a8 1008 goto out_unlock_put;
12996fc3
DDG
1009 } else {
1010 map->flags = GNTMAP_host_map;
1011 if (!(vma->vm_flags & VM_WRITE))
1012 map->flags |= GNTMAP_readonly;
1013 }
ab31523c 1014
d3eeb1d7
JG
1015 if (use_ptemod) {
1016 map->vma = vma;
1017 err = mmu_interval_notifier_insert_locked(
1018 &map->notifier, vma->vm_mm, vma->vm_start,
1019 vma->vm_end - vma->vm_start, &gntdev_mmu_ops);
1020 if (err)
1021 goto out_unlock_put;
1022 }
1401c00e 1023 mutex_unlock(&priv->lock);
f0a70c88 1024
aab8f11a 1025 if (use_ptemod) {
92937241
BO
1026 /*
1027 * gntdev takes the address of the PTE in find_grant_ptes() and
1028 * passes it to the hypervisor in gntdev_map_grant_pages(). The
1029 * purpose of the notifier is to prevent the hypervisor pointer
1030 * to the PTE from going stale.
1031 *
1032 * Since this vma's mappings can't be touched without the
c1e8d7c6 1033 * mmap_lock, and we are holding it now, there is no need for
92937241
BO
1034 * the notifier_range locking pattern.
1035 */
1036 mmu_interval_read_begin(&map->notifier);
1037
298d275d 1038 map->pages_vm_start = vma->vm_start;
aab8f11a
DDG
1039 err = apply_to_page_range(vma->vm_mm, vma->vm_start,
1040 vma->vm_end - vma->vm_start,
1041 find_grant_ptes, map);
1042 if (err) {
283c0972 1043 pr_warn("find_grant_ptes() failure.\n");
90b6f305 1044 goto out_put_map;
aab8f11a 1045 }
ab31523c
GH
1046 }
1047
1d314567 1048 err = gntdev_map_grant_pages(map);
90b6f305
DDG
1049 if (err)
1050 goto out_put_map;
f0a70c88 1051
aab8f11a 1052 if (!use_ptemod) {
8d1502f6 1053 err = vm_map_pages_zero(vma, map->pages, map->count);
df9bde01
SJ
1054 if (err)
1055 goto out_put_map;
923b2919
DV
1056 } else {
1057#ifdef CONFIG_X86
1058 /*
1059 * If the PTEs were not made special by the grant map
1060 * hypercall, do so here.
1061 *
1062 * This is racy since the mapping is already visible
1063 * to userspace but userspace should be well-behaved
1064 * enough to not touch it until the mmap() call
1065 * returns.
1066 */
1067 if (!xen_feature(XENFEAT_gnttab_map_avail_bits)) {
1068 apply_to_page_range(vma->vm_mm, vma->vm_start,
1069 vma->vm_end - vma->vm_start,
1070 set_grant_ptes_as_special, NULL);
1071 }
1072#endif
aab8f11a
DDG
1073 }
1074
f0a70c88
DDG
1075 return 0;
1076
ab31523c 1077unlock_out:
1401c00e 1078 mutex_unlock(&priv->lock);
ab31523c 1079 return err;
90b6f305 1080
a93e20a8 1081out_unlock_put:
1401c00e 1082 mutex_unlock(&priv->lock);
90b6f305 1083out_put_map:
cf2acf66 1084 if (use_ptemod) {
cf2acf66 1085 unmap_grant_pages(map, 0, map->count);
d3eeb1d7
JG
1086 if (map->vma) {
1087 mmu_interval_notifier_remove(&map->notifier);
1088 map->vma = NULL;
1089 }
cf2acf66 1090 }
16a1d022 1091 gntdev_put_map(priv, map);
90b6f305 1092 return err;
ab31523c
GH
1093}
1094
1095static const struct file_operations gntdev_fops = {
1096 .owner = THIS_MODULE,
1097 .open = gntdev_open,
1098 .release = gntdev_release,
1099 .mmap = gntdev_mmap,
1100 .unlocked_ioctl = gntdev_ioctl
1101};
1102
1103static struct miscdevice gntdev_miscdev = {
1104 .minor = MISC_DYNAMIC_MINOR,
1105 .name = "xen/gntdev",
1106 .fops = &gntdev_fops,
1107};
1108
1109/* ------------------------------------------------------------------ */
1110
1111static int __init gntdev_init(void)
1112{
1113 int err;
1114
1115 if (!xen_domain())
1116 return -ENODEV;
1117
6926f6d6 1118 use_ptemod = !xen_feature(XENFEAT_auto_translated_physmap);
aab8f11a 1119
ab31523c
GH
1120 err = misc_register(&gntdev_miscdev);
1121 if (err != 0) {
283c0972 1122 pr_err("Could not register gntdev device\n");
ab31523c
GH
1123 return err;
1124 }
1125 return 0;
1126}
1127
1128static void __exit gntdev_exit(void)
1129{
1130 misc_deregister(&gntdev_miscdev);
1131}
1132
1133module_init(gntdev_init);
1134module_exit(gntdev_exit);
1135
1136/* ------------------------------------------------------------------ */