1// SPDX-License-Identifier: GPL-2.0
2
3/*
4 * Xen dma-buf functionality for gntdev.
5 *
6 * DMA buffer implementation is based on drivers/gpu/drm/drm_prime.c.
7 *
8 * Copyright (c) 2018 Oleksandr Andrushchenko, EPAM Systems Inc.
9 */
10
11#include <linux/kernel.h>
12#include <linux/errno.h>
13#include <linux/dma-buf.h>
14#include <linux/dma-direct.h>
15#include <linux/slab.h>
16#include <linux/types.h>
17#include <linux/uaccess.h>
18#include <linux/module.h>
19
20#include <xen/xen.h>
21#include <xen/grant_table.h>
22
23#include "gntdev-common.h"
24#include "gntdev-dmabuf.h"
25
26MODULE_IMPORT_NS("DMA_BUF");
27
28struct gntdev_dmabuf {
29 struct gntdev_dmabuf_priv *priv;
30 struct dma_buf *dmabuf;
31 struct list_head next;
32 int fd;
33
34 union {
35 struct {
36 /* Exported buffers are reference counted. */
37 struct kref refcount;
38
39 struct gntdev_priv *priv;
40 struct gntdev_grant_map *map;
41 } exp;
42 struct {
43 /* Granted references of the imported buffer. */
44 grant_ref_t *refs;
45 /* Scatter-gather table of the imported buffer. */
46 struct sg_table *sgt;
47 /* dma-buf attachment of the imported buffer. */
48 struct dma_buf_attachment *attach;
49 } imp;
50 } u;
51
52 /* Number of pages this buffer has. */
53 int nr_pages;
54 /* Pages of this buffer (only for dma-buf export). */
55 struct page **pages;
56};
57
58struct gntdev_dmabuf_wait_obj {
59 struct list_head next;
60 struct gntdev_dmabuf *gntdev_dmabuf;
61 struct completion completion;
62};
63
64struct gntdev_dmabuf_attachment {
65 struct sg_table *sgt;
66 enum dma_data_direction dir;
67};
68
69struct gntdev_dmabuf_priv {
70 /* List of exported DMA buffers. */
71 struct list_head exp_list;
72 /* List of wait objects. */
73 struct list_head exp_wait_list;
74 /* List of imported DMA buffers. */
75 struct list_head imp_list;
76 /* This is the lock which protects dma_buf_xxx lists. */
77 struct mutex lock;
78 /*
79 * We reference this file while exporting dma-bufs, so
80 * the grant device context is not destroyed while there are
81 * external users alive.
82 */
83 struct file *filp;
84};
85
86/* DMA buffer export support. */
87
88/* Implementation of wait for exported DMA buffer to be released. */
89
90static void dmabuf_exp_release(struct kref *kref);
91
92static struct gntdev_dmabuf_wait_obj *
93dmabuf_exp_wait_obj_new(struct gntdev_dmabuf_priv *priv,
94 struct gntdev_dmabuf *gntdev_dmabuf)
95{
96 struct gntdev_dmabuf_wait_obj *obj;
97
98 obj = kzalloc(sizeof(*obj), GFP_KERNEL);
99 if (!obj)
100 return ERR_PTR(error: -ENOMEM);
101
102 init_completion(x: &obj->completion);
103 obj->gntdev_dmabuf = gntdev_dmabuf;
104
105 mutex_lock(&priv->lock);
106 list_add(new: &obj->next, head: &priv->exp_wait_list);
107 /* Put our reference and wait for gntdev_dmabuf's release to fire. */
108 kref_put(kref: &gntdev_dmabuf->u.exp.refcount, release: dmabuf_exp_release);
109 mutex_unlock(lock: &priv->lock);
110 return obj;
111}
112
113static void dmabuf_exp_wait_obj_free(struct gntdev_dmabuf_priv *priv,
114 struct gntdev_dmabuf_wait_obj *obj)
115{
116 mutex_lock(&priv->lock);
117 list_del(entry: &obj->next);
118 mutex_unlock(lock: &priv->lock);
119 kfree(objp: obj);
120}
121
122static int dmabuf_exp_wait_obj_wait(struct gntdev_dmabuf_wait_obj *obj,
123 u32 wait_to_ms)
124{
125 if (wait_for_completion_timeout(x: &obj->completion,
126 timeout: msecs_to_jiffies(m: wait_to_ms)) <= 0)
127 return -ETIMEDOUT;
128
129 return 0;
130}
131
132static void dmabuf_exp_wait_obj_signal(struct gntdev_dmabuf_priv *priv,
133 struct gntdev_dmabuf *gntdev_dmabuf)
134{
135 struct gntdev_dmabuf_wait_obj *obj;
136
137 list_for_each_entry(obj, &priv->exp_wait_list, next)
138 if (obj->gntdev_dmabuf == gntdev_dmabuf) {
139 pr_debug("Found gntdev_dmabuf in the wait list, wake\n");
140 complete_all(&obj->completion);
141 break;
142 }
143}
144
145static struct gntdev_dmabuf *
146dmabuf_exp_wait_obj_get_dmabuf(struct gntdev_dmabuf_priv *priv, int fd)
147{
148 struct gntdev_dmabuf *gntdev_dmabuf, *ret = ERR_PTR(error: -ENOENT);
149
150 mutex_lock(&priv->lock);
151 list_for_each_entry(gntdev_dmabuf, &priv->exp_list, next)
152 if (gntdev_dmabuf->fd == fd) {
153 pr_debug("Found gntdev_dmabuf in the wait list\n");
154 kref_get(kref: &gntdev_dmabuf->u.exp.refcount);
155 ret = gntdev_dmabuf;
156 break;
157 }
158 mutex_unlock(lock: &priv->lock);
159 return ret;
160}
161
162static int dmabuf_exp_wait_released(struct gntdev_dmabuf_priv *priv, int fd,
163 int wait_to_ms)
164{
165 struct gntdev_dmabuf *gntdev_dmabuf;
166 struct gntdev_dmabuf_wait_obj *obj;
167 int ret;
168
169 pr_debug("Will wait for dma-buf with fd %d\n", fd);
170 /*
171 * Try to find the DMA buffer: if not found means that
172 * either the buffer has already been released or file descriptor
173 * provided is wrong.
174 */
175 gntdev_dmabuf = dmabuf_exp_wait_obj_get_dmabuf(priv, fd);
176 if (IS_ERR(ptr: gntdev_dmabuf))
177 return PTR_ERR(ptr: gntdev_dmabuf);
178
179 /*
180 * gntdev_dmabuf still exists and is reference count locked by us now,
181 * so prepare to wait: allocate wait object and add it to the wait list,
182 * so we can find it on release.
183 */
184 obj = dmabuf_exp_wait_obj_new(priv, gntdev_dmabuf);
185 if (IS_ERR(ptr: obj))
186 return PTR_ERR(ptr: obj);
187
188 ret = dmabuf_exp_wait_obj_wait(obj, wait_to_ms);
189 dmabuf_exp_wait_obj_free(priv, obj);
190 return ret;
191}
192
193/* DMA buffer export support. */
194
195static struct sg_table *
196dmabuf_pages_to_sgt(struct page **pages, unsigned int nr_pages)
197{
198 struct sg_table *sgt;
199 int ret;
200
201 sgt = kmalloc(sizeof(*sgt), GFP_KERNEL);
202 if (!sgt) {
203 ret = -ENOMEM;
204 goto out;
205 }
206
207 ret = sg_alloc_table_from_pages(sgt, pages, n_pages: nr_pages, offset: 0,
208 size: nr_pages << PAGE_SHIFT,
209 GFP_KERNEL);
210 if (ret)
211 goto out;
212
213 return sgt;
214
215out:
216 kfree(objp: sgt);
217 return ERR_PTR(error: ret);
218}
219
220static int dmabuf_exp_ops_attach(struct dma_buf *dma_buf,
221 struct dma_buf_attachment *attach)
222{
223 struct gntdev_dmabuf_attachment *gntdev_dmabuf_attach;
224
225 gntdev_dmabuf_attach = kzalloc(sizeof(*gntdev_dmabuf_attach),
226 GFP_KERNEL);
227 if (!gntdev_dmabuf_attach)
228 return -ENOMEM;
229
230 gntdev_dmabuf_attach->dir = DMA_NONE;
231 attach->priv = gntdev_dmabuf_attach;
232 return 0;
233}
234
235static void dmabuf_exp_ops_detach(struct dma_buf *dma_buf,
236 struct dma_buf_attachment *attach)
237{
238 struct gntdev_dmabuf_attachment *gntdev_dmabuf_attach = attach->priv;
239
240 if (gntdev_dmabuf_attach) {
241 struct sg_table *sgt = gntdev_dmabuf_attach->sgt;
242
243 if (sgt) {
244 if (gntdev_dmabuf_attach->dir != DMA_NONE)
245 dma_unmap_sgtable(dev: attach->dev, sgt,
246 dir: gntdev_dmabuf_attach->dir,
247 DMA_ATTR_SKIP_CPU_SYNC);
248 sg_free_table(sgt);
249 }
250
251 kfree(objp: sgt);
252 kfree(objp: gntdev_dmabuf_attach);
253 attach->priv = NULL;
254 }
255}
256
257static struct sg_table *
258dmabuf_exp_ops_map_dma_buf(struct dma_buf_attachment *attach,
259 enum dma_data_direction dir)
260{
261 struct gntdev_dmabuf_attachment *gntdev_dmabuf_attach = attach->priv;
262 struct gntdev_dmabuf *gntdev_dmabuf = attach->dmabuf->priv;
263 struct sg_table *sgt;
264
265 pr_debug("Mapping %d pages for dev %p\n", gntdev_dmabuf->nr_pages,
266 attach->dev);
267
268 if (dir == DMA_NONE || !gntdev_dmabuf_attach)
269 return ERR_PTR(error: -EINVAL);
270
271 /* Return the cached mapping when possible. */
272 if (gntdev_dmabuf_attach->dir == dir)
273 return gntdev_dmabuf_attach->sgt;
274
275 /*
276 * Two mappings with different directions for the same attachment are
277 * not allowed.
278 */
279 if (gntdev_dmabuf_attach->dir != DMA_NONE)
280 return ERR_PTR(error: -EBUSY);
281
282 sgt = dmabuf_pages_to_sgt(pages: gntdev_dmabuf->pages,
283 nr_pages: gntdev_dmabuf->nr_pages);
284 if (!IS_ERR(ptr: sgt)) {
285 if (dma_map_sgtable(dev: attach->dev, sgt, dir,
286 DMA_ATTR_SKIP_CPU_SYNC)) {
287 sg_free_table(sgt);
288 kfree(objp: sgt);
289 sgt = ERR_PTR(error: -ENOMEM);
290 } else {
291 gntdev_dmabuf_attach->sgt = sgt;
292 gntdev_dmabuf_attach->dir = dir;
293 }
294 }
295 if (IS_ERR(ptr: sgt))
296 pr_debug("Failed to map sg table for dev %p\n", attach->dev);
297 return sgt;
298}
299
300static void dmabuf_exp_ops_unmap_dma_buf(struct dma_buf_attachment *attach,
301 struct sg_table *sgt,
302 enum dma_data_direction dir)
303{
304 /* Not implemented. The unmap is done at dmabuf_exp_ops_detach(). */
305}
306
307static void dmabuf_exp_release(struct kref *kref)
308{
309 struct gntdev_dmabuf *gntdev_dmabuf =
310 container_of(kref, struct gntdev_dmabuf, u.exp.refcount);
311
312 dmabuf_exp_wait_obj_signal(priv: gntdev_dmabuf->priv, gntdev_dmabuf);
313 list_del(entry: &gntdev_dmabuf->next);
314 fput(gntdev_dmabuf->priv->filp);
315 kfree(objp: gntdev_dmabuf);
316}
317
318static void dmabuf_exp_remove_map(struct gntdev_priv *priv,
319 struct gntdev_grant_map *map)
320{
321 mutex_lock(&priv->lock);
322 list_del(entry: &map->next);
323 gntdev_put_map(NULL /* already removed */, map);
324 mutex_unlock(lock: &priv->lock);
325}
326
327static void dmabuf_exp_ops_release(struct dma_buf *dma_buf)
328{
329 struct gntdev_dmabuf *gntdev_dmabuf = dma_buf->priv;
330 struct gntdev_dmabuf_priv *priv = gntdev_dmabuf->priv;
331
332 dmabuf_exp_remove_map(priv: gntdev_dmabuf->u.exp.priv,
333 map: gntdev_dmabuf->u.exp.map);
334 mutex_lock(&priv->lock);
335 kref_put(kref: &gntdev_dmabuf->u.exp.refcount, release: dmabuf_exp_release);
336 mutex_unlock(lock: &priv->lock);
337}
338
339static const struct dma_buf_ops dmabuf_exp_ops = {
340 .attach = dmabuf_exp_ops_attach,
341 .detach = dmabuf_exp_ops_detach,
342 .map_dma_buf = dmabuf_exp_ops_map_dma_buf,
343 .unmap_dma_buf = dmabuf_exp_ops_unmap_dma_buf,
344 .release = dmabuf_exp_ops_release,
345};
346
347struct gntdev_dmabuf_export_args {
348 struct gntdev_priv *priv;
349 struct gntdev_grant_map *map;
350 struct gntdev_dmabuf_priv *dmabuf_priv;
351 struct device *dev;
352 int count;
353 struct page **pages;
354 u32 fd;
355};
356
357static int dmabuf_exp_from_pages(struct gntdev_dmabuf_export_args *args)
358{
359 DEFINE_DMA_BUF_EXPORT_INFO(exp_info);
360 struct gntdev_dmabuf *gntdev_dmabuf __free(kfree) = NULL;
361 CLASS(get_unused_fd, ret)(O_CLOEXEC);
362
363 if (ret < 0)
364 return ret;
365
366 gntdev_dmabuf = kzalloc(sizeof(*gntdev_dmabuf), GFP_KERNEL);
367 if (!gntdev_dmabuf)
368 return -ENOMEM;
369
370 kref_init(kref: &gntdev_dmabuf->u.exp.refcount);
371
372 gntdev_dmabuf->priv = args->dmabuf_priv;
373 gntdev_dmabuf->nr_pages = args->count;
374 gntdev_dmabuf->pages = args->pages;
375 gntdev_dmabuf->u.exp.priv = args->priv;
376 gntdev_dmabuf->u.exp.map = args->map;
377
378 exp_info.exp_name = KBUILD_MODNAME;
379 if (args->dev->driver && args->dev->driver->owner)
380 exp_info.owner = args->dev->driver->owner;
381 else
382 exp_info.owner = THIS_MODULE;
383 exp_info.ops = &dmabuf_exp_ops;
384 exp_info.size = args->count << PAGE_SHIFT;
385 exp_info.flags = O_RDWR;
386 exp_info.priv = gntdev_dmabuf;
387
388 gntdev_dmabuf->dmabuf = dma_buf_export(exp_info: &exp_info);
389 if (IS_ERR(ptr: gntdev_dmabuf->dmabuf))
390 return PTR_ERR(ptr: gntdev_dmabuf->dmabuf);
391
392 gntdev_dmabuf->fd = ret;
393 args->fd = ret;
394
395 pr_debug("Exporting DMA buffer with fd %d\n", ret);
396
397 get_file(f: gntdev_dmabuf->priv->filp);
398 mutex_lock(&args->dmabuf_priv->lock);
399 list_add(new: &gntdev_dmabuf->next, head: &args->dmabuf_priv->exp_list);
400 mutex_unlock(lock: &args->dmabuf_priv->lock);
401
402 fd_install(take_fd(ret), no_free_ptr(gntdev_dmabuf)->dmabuf->file);
403 return 0;
404}
405
406static struct gntdev_grant_map *
407dmabuf_exp_alloc_backing_storage(struct gntdev_priv *priv, int dmabuf_flags,
408 int count)
409{
410 struct gntdev_grant_map *map;
411
412 if (unlikely(gntdev_test_page_count(count)))
413 return ERR_PTR(error: -EINVAL);
414
415 if ((dmabuf_flags & GNTDEV_DMA_FLAG_WC) &&
416 (dmabuf_flags & GNTDEV_DMA_FLAG_COHERENT)) {
417 pr_debug("Wrong dma-buf flags: 0x%x\n", dmabuf_flags);
418 return ERR_PTR(error: -EINVAL);
419 }
420
421 map = gntdev_alloc_map(priv, count, dma_flags: dmabuf_flags);
422 if (!map)
423 return ERR_PTR(error: -ENOMEM);
424
425 return map;
426}
427
428static int dmabuf_exp_from_refs(struct gntdev_priv *priv, int flags,
429 int count, u32 domid, u32 *refs, u32 *fd)
430{
431 struct gntdev_grant_map *map;
432 struct gntdev_dmabuf_export_args args;
433 int i, ret;
434
435 map = dmabuf_exp_alloc_backing_storage(priv, dmabuf_flags: flags, count);
436 if (IS_ERR(ptr: map))
437 return PTR_ERR(ptr: map);
438
439 for (i = 0; i < count; i++) {
440 map->grants[i].domid = domid;
441 map->grants[i].ref = refs[i];
442 }
443
444 mutex_lock(&priv->lock);
445 gntdev_add_map(priv, add: map);
446 mutex_unlock(lock: &priv->lock);
447
448 map->flags |= GNTMAP_host_map;
449#if defined(CONFIG_X86)
450 map->flags |= GNTMAP_device_map;
451#endif
452
453 ret = gntdev_map_grant_pages(map);
454 if (ret < 0)
455 goto out;
456
457 args.priv = priv;
458 args.map = map;
459 args.dev = priv->dma_dev;
460 args.dmabuf_priv = priv->dmabuf_priv;
461 args.count = map->count;
462 args.pages = map->pages;
463 args.fd = -1; /* Shut up unnecessary gcc warning for i386 */
464
465 ret = dmabuf_exp_from_pages(args: &args);
466 if (ret < 0)
467 goto out;
468
469 *fd = args.fd;
470 return 0;
471
472out:
473 dmabuf_exp_remove_map(priv, map);
474 return ret;
475}
476
477/* DMA buffer import support. */
478
479static int
480dmabuf_imp_grant_foreign_access(unsigned long *gfns, u32 *refs,
481 int count, int domid)
482{
483 grant_ref_t priv_gref_head;
484 int i, ret;
485
486 ret = gnttab_alloc_grant_references(count, pprivate_head: &priv_gref_head);
487 if (ret < 0) {
488 pr_debug("Cannot allocate grant references, ret %d\n", ret);
489 return ret;
490 }
491
492 for (i = 0; i < count; i++) {
493 int cur_ref;
494
495 cur_ref = gnttab_claim_grant_reference(pprivate_head: &priv_gref_head);
496 if (cur_ref < 0) {
497 ret = cur_ref;
498 pr_debug("Cannot claim grant reference, ret %d\n", ret);
499 goto out;
500 }
501
502 gnttab_grant_foreign_access_ref(ref: cur_ref, domid,
503 frame: gfns[i], readonly: 0);
504 refs[i] = cur_ref;
505 }
506
507 return 0;
508
509out:
510 gnttab_free_grant_references(head: priv_gref_head);
511 return ret;
512}
513
514static void dmabuf_imp_end_foreign_access(u32 *refs, int count)
515{
516 int i;
517
518 for (i = 0; i < count; i++)
519 if (refs[i] != INVALID_GRANT_REF)
520 gnttab_end_foreign_access(ref: refs[i], NULL);
521}
522
523static void dmabuf_imp_free_storage(struct gntdev_dmabuf *gntdev_dmabuf)
524{
525 kfree(objp: gntdev_dmabuf->u.imp.refs);
526 kfree(objp: gntdev_dmabuf);
527}
528
529static struct gntdev_dmabuf *dmabuf_imp_alloc_storage(int count)
530{
531 struct gntdev_dmabuf *gntdev_dmabuf;
532 int i;
533
534 gntdev_dmabuf = kzalloc(sizeof(*gntdev_dmabuf), GFP_KERNEL);
535 if (!gntdev_dmabuf)
536 goto fail_no_free;
537
538 gntdev_dmabuf->u.imp.refs = kcalloc(count,
539 sizeof(gntdev_dmabuf->u.imp.refs[0]),
540 GFP_KERNEL);
541 if (!gntdev_dmabuf->u.imp.refs)
542 goto fail;
543
544 gntdev_dmabuf->nr_pages = count;
545
546 for (i = 0; i < count; i++)
547 gntdev_dmabuf->u.imp.refs[i] = INVALID_GRANT_REF;
548
549 return gntdev_dmabuf;
550
551fail:
552 dmabuf_imp_free_storage(gntdev_dmabuf);
553fail_no_free:
554 return ERR_PTR(error: -ENOMEM);
555}
556
557static struct gntdev_dmabuf *
558dmabuf_imp_to_refs(struct gntdev_dmabuf_priv *priv, struct device *dev,
559 int fd, int count, int domid)
560{
561 struct gntdev_dmabuf *gntdev_dmabuf, *ret;
562 struct dma_buf *dma_buf;
563 struct dma_buf_attachment *attach;
564 struct sg_table *sgt;
565 struct sg_dma_page_iter sg_iter;
566 unsigned long *gfns;
567 int i;
568
569 dma_buf = dma_buf_get(fd);
570 if (IS_ERR(ptr: dma_buf))
571 return ERR_CAST(ptr: dma_buf);
572
573 gntdev_dmabuf = dmabuf_imp_alloc_storage(count);
574 if (IS_ERR(ptr: gntdev_dmabuf)) {
575 ret = gntdev_dmabuf;
576 goto fail_put;
577 }
578
579 gntdev_dmabuf->priv = priv;
580 gntdev_dmabuf->fd = fd;
581
582 attach = dma_buf_attach(dmabuf: dma_buf, dev);
583 if (IS_ERR(ptr: attach)) {
584 ret = ERR_CAST(ptr: attach);
585 goto fail_free_obj;
586 }
587
588 gntdev_dmabuf->u.imp.attach = attach;
589
590 sgt = dma_buf_map_attachment_unlocked(attach, direction: DMA_BIDIRECTIONAL);
591 if (IS_ERR(ptr: sgt)) {
592 ret = ERR_CAST(ptr: sgt);
593 goto fail_detach;
594 }
595
596 /* Check that we have zero offset. */
597 if (sgt->sgl->offset) {
598 ret = ERR_PTR(error: -EINVAL);
599 pr_debug("DMA buffer has %d bytes offset, user-space expects 0\n",
600 sgt->sgl->offset);
601 goto fail_unmap;
602 }
603
604 /* Check number of pages that imported buffer has. */
605 if (attach->dmabuf->size != gntdev_dmabuf->nr_pages << PAGE_SHIFT) {
606 ret = ERR_PTR(error: -EINVAL);
607 pr_debug("DMA buffer has %zu pages, user-space expects %d\n",
608 attach->dmabuf->size, gntdev_dmabuf->nr_pages);
609 goto fail_unmap;
610 }
611
612 gntdev_dmabuf->u.imp.sgt = sgt;
613
614 gfns = kcalloc(count, sizeof(*gfns), GFP_KERNEL);
615 if (!gfns) {
616 ret = ERR_PTR(error: -ENOMEM);
617 goto fail_unmap;
618 }
619
620 /*
621 * Now convert sgt to array of gfns without accessing underlying pages.
622 * It is not allowed to access the underlying struct page of an sg table
623 * exported by DMA-buf, but since we deal with special Xen dma device here
624 * (not a normal physical one) look at the dma addresses in the sg table
625 * and then calculate gfns directly from them.
626 */
627 i = 0;
628 for_each_sgtable_dma_page(sgt, &sg_iter, 0) {
629 dma_addr_t addr = sg_page_iter_dma_address(dma_iter: &sg_iter);
630 unsigned long pfn = bfn_to_pfn(XEN_PFN_DOWN(dma_to_phys(dev, addr)));
631
632 gfns[i++] = pfn_to_gfn(pfn);
633 }
634
635 ret = ERR_PTR(error: dmabuf_imp_grant_foreign_access(gfns,
636 refs: gntdev_dmabuf->u.imp.refs,
637 count, domid));
638 kfree(objp: gfns);
639 if (IS_ERR(ptr: ret))
640 goto fail_end_access;
641
642 pr_debug("Imported DMA buffer with fd %d\n", fd);
643
644 mutex_lock(&priv->lock);
645 list_add(new: &gntdev_dmabuf->next, head: &priv->imp_list);
646 mutex_unlock(lock: &priv->lock);
647
648 return gntdev_dmabuf;
649
650fail_end_access:
651 dmabuf_imp_end_foreign_access(refs: gntdev_dmabuf->u.imp.refs, count);
652fail_unmap:
653 dma_buf_unmap_attachment_unlocked(attach, sg_table: sgt, direction: DMA_BIDIRECTIONAL);
654fail_detach:
655 dma_buf_detach(dmabuf: dma_buf, attach);
656fail_free_obj:
657 dmabuf_imp_free_storage(gntdev_dmabuf);
658fail_put:
659 dma_buf_put(dmabuf: dma_buf);
660 return ret;
661}
662
663/*
664 * Find the hyper dma-buf by its file descriptor and remove
665 * it from the buffer's list.
666 */
667static struct gntdev_dmabuf *
668dmabuf_imp_find_unlink(struct gntdev_dmabuf_priv *priv, int fd)
669{
670 struct gntdev_dmabuf *q, *gntdev_dmabuf, *ret = ERR_PTR(error: -ENOENT);
671
672 mutex_lock(&priv->lock);
673 list_for_each_entry_safe(gntdev_dmabuf, q, &priv->imp_list, next) {
674 if (gntdev_dmabuf->fd == fd) {
675 pr_debug("Found gntdev_dmabuf in the import list\n");
676 ret = gntdev_dmabuf;
677 list_del(entry: &gntdev_dmabuf->next);
678 break;
679 }
680 }
681 mutex_unlock(lock: &priv->lock);
682 return ret;
683}
684
685static int dmabuf_imp_release(struct gntdev_dmabuf_priv *priv, u32 fd)
686{
687 struct gntdev_dmabuf *gntdev_dmabuf;
688 struct dma_buf_attachment *attach;
689 struct dma_buf *dma_buf;
690
691 gntdev_dmabuf = dmabuf_imp_find_unlink(priv, fd);
692 if (IS_ERR(ptr: gntdev_dmabuf))
693 return PTR_ERR(ptr: gntdev_dmabuf);
694
695 pr_debug("Releasing DMA buffer with fd %d\n", fd);
696
697 dmabuf_imp_end_foreign_access(refs: gntdev_dmabuf->u.imp.refs,
698 count: gntdev_dmabuf->nr_pages);
699
700 attach = gntdev_dmabuf->u.imp.attach;
701
702 if (gntdev_dmabuf->u.imp.sgt)
703 dma_buf_unmap_attachment_unlocked(attach, sg_table: gntdev_dmabuf->u.imp.sgt,
704 direction: DMA_BIDIRECTIONAL);
705 dma_buf = attach->dmabuf;
706 dma_buf_detach(dmabuf: attach->dmabuf, attach);
707 dma_buf_put(dmabuf: dma_buf);
708
709 dmabuf_imp_free_storage(gntdev_dmabuf);
710 return 0;
711}
712
713static void dmabuf_imp_release_all(struct gntdev_dmabuf_priv *priv)
714{
715 struct gntdev_dmabuf *q, *gntdev_dmabuf;
716
717 list_for_each_entry_safe(gntdev_dmabuf, q, &priv->imp_list, next)
718 dmabuf_imp_release(priv, fd: gntdev_dmabuf->fd);
719}
720
721/* DMA buffer IOCTL support. */
722
723long gntdev_ioctl_dmabuf_exp_from_refs(struct gntdev_priv *priv,
724 struct ioctl_gntdev_dmabuf_exp_from_refs __user *u)
725{
726 struct ioctl_gntdev_dmabuf_exp_from_refs op;
727 u32 *refs;
728 long ret;
729
730 if (xen_pv_domain()) {
731 pr_debug("Cannot provide dma-buf in a PV domain\n");
732 return -EINVAL;
733 }
734
735 if (copy_from_user(to: &op, from: u, n: sizeof(op)) != 0)
736 return -EFAULT;
737
738 if (unlikely(gntdev_test_page_count(op.count)))
739 return -EINVAL;
740
741 refs = kcalloc(op.count, sizeof(*refs), GFP_KERNEL);
742 if (!refs)
743 return -ENOMEM;
744
745 if (copy_from_user(to: refs, from: u->refs, n: sizeof(*refs) * op.count) != 0) {
746 ret = -EFAULT;
747 goto out;
748 }
749
750 ret = dmabuf_exp_from_refs(priv, flags: op.flags, count: op.count,
751 domid: op.domid, refs, fd: &op.fd);
752 if (ret)
753 goto out;
754
755 if (copy_to_user(to: u, from: &op, n: sizeof(op)) != 0)
756 ret = -EFAULT;
757
758out:
759 kfree(objp: refs);
760 return ret;
761}
762
763long gntdev_ioctl_dmabuf_exp_wait_released(struct gntdev_priv *priv,
764 struct ioctl_gntdev_dmabuf_exp_wait_released __user *u)
765{
766 struct ioctl_gntdev_dmabuf_exp_wait_released op;
767
768 if (copy_from_user(to: &op, from: u, n: sizeof(op)) != 0)
769 return -EFAULT;
770
771 return dmabuf_exp_wait_released(priv: priv->dmabuf_priv, fd: op.fd,
772 wait_to_ms: op.wait_to_ms);
773}
774
775long gntdev_ioctl_dmabuf_imp_to_refs(struct gntdev_priv *priv,
776 struct ioctl_gntdev_dmabuf_imp_to_refs __user *u)
777{
778 struct ioctl_gntdev_dmabuf_imp_to_refs op;
779 struct gntdev_dmabuf *gntdev_dmabuf;
780 long ret;
781
782 if (copy_from_user(to: &op, from: u, n: sizeof(op)) != 0)
783 return -EFAULT;
784
785 if (unlikely(gntdev_test_page_count(op.count)))
786 return -EINVAL;
787
788 gntdev_dmabuf = dmabuf_imp_to_refs(priv: priv->dmabuf_priv,
789 dev: priv->dma_dev, fd: op.fd,
790 count: op.count, domid: op.domid);
791 if (IS_ERR(ptr: gntdev_dmabuf))
792 return PTR_ERR(ptr: gntdev_dmabuf);
793
794 if (copy_to_user(to: u->refs, from: gntdev_dmabuf->u.imp.refs,
795 n: sizeof(*u->refs) * op.count) != 0) {
796 ret = -EFAULT;
797 goto out_release;
798 }
799 return 0;
800
801out_release:
802 dmabuf_imp_release(priv: priv->dmabuf_priv, fd: op.fd);
803 return ret;
804}
805
806long gntdev_ioctl_dmabuf_imp_release(struct gntdev_priv *priv,
807 struct ioctl_gntdev_dmabuf_imp_release __user *u)
808{
809 struct ioctl_gntdev_dmabuf_imp_release op;
810
811 if (copy_from_user(to: &op, from: u, n: sizeof(op)) != 0)
812 return -EFAULT;
813
814 return dmabuf_imp_release(priv: priv->dmabuf_priv, fd: op.fd);
815}
816
817struct gntdev_dmabuf_priv *gntdev_dmabuf_init(struct file *filp)
818{
819 struct gntdev_dmabuf_priv *priv;
820
821 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
822 if (!priv)
823 return ERR_PTR(error: -ENOMEM);
824
825 mutex_init(&priv->lock);
826 INIT_LIST_HEAD(list: &priv->exp_list);
827 INIT_LIST_HEAD(list: &priv->exp_wait_list);
828 INIT_LIST_HEAD(list: &priv->imp_list);
829
830 priv->filp = filp;
831
832 return priv;
833}
834
835void gntdev_dmabuf_fini(struct gntdev_dmabuf_priv *priv)
836{
837 dmabuf_imp_release_all(priv);
838 kfree(objp: priv);
839}
840

source code of linux/drivers/xen/gntdev-dmabuf.c