1 // SPDX-License-Identifier: GPL-2.0
4 * Xen dma-buf functionality for gntdev.
6 * DMA buffer implementation is based on drivers/gpu/drm/drm_prime.c.
8 * Copyright (c) 2018 Oleksandr Andrushchenko, EPAM Systems Inc.
11 #include <linux/kernel.h>
12 #include <linux/errno.h>
13 #include <linux/dma-buf.h>
14 #include <linux/slab.h>
15 #include <linux/types.h>
16 #include <linux/uaccess.h>
19 #include <xen/grant_table.h>
21 #include "gntdev-common.h"
22 #include "gntdev-dmabuf.h"
24 #ifndef GRANT_INVALID_REF
26 * Note on usage of grant reference 0 as invalid grant reference:
27 * grant reference 0 is valid, but never exposed to a driver,
28 * because of the fact it is already in use/reserved by the PV console.
30 #define GRANT_INVALID_REF 0
33 struct gntdev_dmabuf
{
34 struct gntdev_dmabuf_priv
*priv
;
35 struct dma_buf
*dmabuf
;
36 struct list_head next
;
41 /* Exported buffers are reference counted. */
44 struct gntdev_priv
*priv
;
45 struct gntdev_grant_map
*map
;
48 /* Granted references of the imported buffer. */
50 /* Scatter-gather table of the imported buffer. */
52 /* dma-buf attachment of the imported buffer. */
53 struct dma_buf_attachment
*attach
;
57 /* Number of pages this buffer has. */
59 /* Pages of this buffer. */
63 struct gntdev_dmabuf_wait_obj
{
64 struct list_head next
;
65 struct gntdev_dmabuf
*gntdev_dmabuf
;
66 struct completion completion
;
69 struct gntdev_dmabuf_attachment
{
71 enum dma_data_direction dir
;
74 struct gntdev_dmabuf_priv
{
75 /* List of exported DMA buffers. */
76 struct list_head exp_list
;
77 /* List of wait objects. */
78 struct list_head exp_wait_list
;
79 /* List of imported DMA buffers. */
80 struct list_head imp_list
;
81 /* This is the lock which protects dma_buf_xxx lists. */
84 * We reference this file while exporting dma-bufs, so
85 * the grant device context is not destroyed while there are
86 * external users alive.
91 /* DMA buffer export support. */
93 /* Implementation of wait for exported DMA buffer to be released. */
95 static void dmabuf_exp_release(struct kref
*kref
);
97 static struct gntdev_dmabuf_wait_obj
*
98 dmabuf_exp_wait_obj_new(struct gntdev_dmabuf_priv
*priv
,
99 struct gntdev_dmabuf
*gntdev_dmabuf
)
101 struct gntdev_dmabuf_wait_obj
*obj
;
103 obj
= kzalloc(sizeof(*obj
), GFP_KERNEL
);
105 return ERR_PTR(-ENOMEM
);
107 init_completion(&obj
->completion
);
108 obj
->gntdev_dmabuf
= gntdev_dmabuf
;
110 mutex_lock(&priv
->lock
);
111 list_add(&obj
->next
, &priv
->exp_wait_list
);
112 /* Put our reference and wait for gntdev_dmabuf's release to fire. */
113 kref_put(&gntdev_dmabuf
->u
.exp
.refcount
, dmabuf_exp_release
);
114 mutex_unlock(&priv
->lock
);
118 static void dmabuf_exp_wait_obj_free(struct gntdev_dmabuf_priv
*priv
,
119 struct gntdev_dmabuf_wait_obj
*obj
)
121 mutex_lock(&priv
->lock
);
122 list_del(&obj
->next
);
123 mutex_unlock(&priv
->lock
);
127 static int dmabuf_exp_wait_obj_wait(struct gntdev_dmabuf_wait_obj
*obj
,
130 if (wait_for_completion_timeout(&obj
->completion
,
131 msecs_to_jiffies(wait_to_ms
)) <= 0)
137 static void dmabuf_exp_wait_obj_signal(struct gntdev_dmabuf_priv
*priv
,
138 struct gntdev_dmabuf
*gntdev_dmabuf
)
140 struct gntdev_dmabuf_wait_obj
*obj
;
142 list_for_each_entry(obj
, &priv
->exp_wait_list
, next
)
143 if (obj
->gntdev_dmabuf
== gntdev_dmabuf
) {
144 pr_debug("Found gntdev_dmabuf in the wait list, wake\n");
145 complete_all(&obj
->completion
);
150 static struct gntdev_dmabuf
*
151 dmabuf_exp_wait_obj_get_dmabuf(struct gntdev_dmabuf_priv
*priv
, int fd
)
153 struct gntdev_dmabuf
*gntdev_dmabuf
, *ret
= ERR_PTR(-ENOENT
);
155 mutex_lock(&priv
->lock
);
156 list_for_each_entry(gntdev_dmabuf
, &priv
->exp_list
, next
)
157 if (gntdev_dmabuf
->fd
== fd
) {
158 pr_debug("Found gntdev_dmabuf in the wait list\n");
159 kref_get(&gntdev_dmabuf
->u
.exp
.refcount
);
163 mutex_unlock(&priv
->lock
);
167 static int dmabuf_exp_wait_released(struct gntdev_dmabuf_priv
*priv
, int fd
,
170 struct gntdev_dmabuf
*gntdev_dmabuf
;
171 struct gntdev_dmabuf_wait_obj
*obj
;
174 pr_debug("Will wait for dma-buf with fd %d\n", fd
);
176 * Try to find the DMA buffer: if not found means that
177 * either the buffer has already been released or file descriptor
180 gntdev_dmabuf
= dmabuf_exp_wait_obj_get_dmabuf(priv
, fd
);
181 if (IS_ERR(gntdev_dmabuf
))
182 return PTR_ERR(gntdev_dmabuf
);
185 * gntdev_dmabuf still exists and is reference count locked by us now,
186 * so prepare to wait: allocate wait object and add it to the wait list,
187 * so we can find it on release.
189 obj
= dmabuf_exp_wait_obj_new(priv
, gntdev_dmabuf
);
193 ret
= dmabuf_exp_wait_obj_wait(obj
, wait_to_ms
);
194 dmabuf_exp_wait_obj_free(priv
, obj
);
198 /* DMA buffer export support. */
200 static struct sg_table
*
201 dmabuf_pages_to_sgt(struct page
**pages
, unsigned int nr_pages
)
203 struct sg_table
*sgt
;
206 sgt
= kmalloc(sizeof(*sgt
), GFP_KERNEL
);
212 ret
= sg_alloc_table_from_pages(sgt
, pages
, nr_pages
, 0,
213 nr_pages
<< PAGE_SHIFT
,
225 static int dmabuf_exp_ops_attach(struct dma_buf
*dma_buf
,
226 struct dma_buf_attachment
*attach
)
228 struct gntdev_dmabuf_attachment
*gntdev_dmabuf_attach
;
230 gntdev_dmabuf_attach
= kzalloc(sizeof(*gntdev_dmabuf_attach
),
232 if (!gntdev_dmabuf_attach
)
235 gntdev_dmabuf_attach
->dir
= DMA_NONE
;
236 attach
->priv
= gntdev_dmabuf_attach
;
240 static void dmabuf_exp_ops_detach(struct dma_buf
*dma_buf
,
241 struct dma_buf_attachment
*attach
)
243 struct gntdev_dmabuf_attachment
*gntdev_dmabuf_attach
= attach
->priv
;
245 if (gntdev_dmabuf_attach
) {
246 struct sg_table
*sgt
= gntdev_dmabuf_attach
->sgt
;
249 if (gntdev_dmabuf_attach
->dir
!= DMA_NONE
)
250 dma_unmap_sg_attrs(attach
->dev
, sgt
->sgl
,
252 gntdev_dmabuf_attach
->dir
,
253 DMA_ATTR_SKIP_CPU_SYNC
);
258 kfree(gntdev_dmabuf_attach
);
263 static struct sg_table
*
264 dmabuf_exp_ops_map_dma_buf(struct dma_buf_attachment
*attach
,
265 enum dma_data_direction dir
)
267 struct gntdev_dmabuf_attachment
*gntdev_dmabuf_attach
= attach
->priv
;
268 struct gntdev_dmabuf
*gntdev_dmabuf
= attach
->dmabuf
->priv
;
269 struct sg_table
*sgt
;
271 pr_debug("Mapping %d pages for dev %p\n", gntdev_dmabuf
->nr_pages
,
274 if (dir
== DMA_NONE
|| !gntdev_dmabuf_attach
)
275 return ERR_PTR(-EINVAL
);
277 /* Return the cached mapping when possible. */
278 if (gntdev_dmabuf_attach
->dir
== dir
)
279 return gntdev_dmabuf_attach
->sgt
;
282 * Two mappings with different directions for the same attachment are
285 if (gntdev_dmabuf_attach
->dir
!= DMA_NONE
)
286 return ERR_PTR(-EBUSY
);
288 sgt
= dmabuf_pages_to_sgt(gntdev_dmabuf
->pages
,
289 gntdev_dmabuf
->nr_pages
);
291 if (!dma_map_sg_attrs(attach
->dev
, sgt
->sgl
, sgt
->nents
, dir
,
292 DMA_ATTR_SKIP_CPU_SYNC
)) {
295 sgt
= ERR_PTR(-ENOMEM
);
297 gntdev_dmabuf_attach
->sgt
= sgt
;
298 gntdev_dmabuf_attach
->dir
= dir
;
302 pr_debug("Failed to map sg table for dev %p\n", attach
->dev
);
306 static void dmabuf_exp_ops_unmap_dma_buf(struct dma_buf_attachment
*attach
,
307 struct sg_table
*sgt
,
308 enum dma_data_direction dir
)
310 /* Not implemented. The unmap is done at dmabuf_exp_ops_detach(). */
313 static void dmabuf_exp_release(struct kref
*kref
)
315 struct gntdev_dmabuf
*gntdev_dmabuf
=
316 container_of(kref
, struct gntdev_dmabuf
, u
.exp
.refcount
);
318 dmabuf_exp_wait_obj_signal(gntdev_dmabuf
->priv
, gntdev_dmabuf
);
319 list_del(&gntdev_dmabuf
->next
);
320 fput(gntdev_dmabuf
->priv
->filp
);
321 kfree(gntdev_dmabuf
);
324 static void dmabuf_exp_remove_map(struct gntdev_priv
*priv
,
325 struct gntdev_grant_map
*map
)
327 mutex_lock(&priv
->lock
);
328 list_del(&map
->next
);
329 gntdev_put_map(NULL
/* already removed */, map
);
330 mutex_unlock(&priv
->lock
);
333 static void dmabuf_exp_ops_release(struct dma_buf
*dma_buf
)
335 struct gntdev_dmabuf
*gntdev_dmabuf
= dma_buf
->priv
;
336 struct gntdev_dmabuf_priv
*priv
= gntdev_dmabuf
->priv
;
338 dmabuf_exp_remove_map(gntdev_dmabuf
->u
.exp
.priv
,
339 gntdev_dmabuf
->u
.exp
.map
);
340 mutex_lock(&priv
->lock
);
341 kref_put(&gntdev_dmabuf
->u
.exp
.refcount
, dmabuf_exp_release
);
342 mutex_unlock(&priv
->lock
);
345 static const struct dma_buf_ops dmabuf_exp_ops
= {
346 .attach
= dmabuf_exp_ops_attach
,
347 .detach
= dmabuf_exp_ops_detach
,
348 .map_dma_buf
= dmabuf_exp_ops_map_dma_buf
,
349 .unmap_dma_buf
= dmabuf_exp_ops_unmap_dma_buf
,
350 .release
= dmabuf_exp_ops_release
,
353 struct gntdev_dmabuf_export_args
{
354 struct gntdev_priv
*priv
;
355 struct gntdev_grant_map
*map
;
356 struct gntdev_dmabuf_priv
*dmabuf_priv
;
363 static int dmabuf_exp_from_pages(struct gntdev_dmabuf_export_args
*args
)
365 DEFINE_DMA_BUF_EXPORT_INFO(exp_info
);
366 struct gntdev_dmabuf
*gntdev_dmabuf
;
369 gntdev_dmabuf
= kzalloc(sizeof(*gntdev_dmabuf
), GFP_KERNEL
);
373 kref_init(&gntdev_dmabuf
->u
.exp
.refcount
);
375 gntdev_dmabuf
->priv
= args
->dmabuf_priv
;
376 gntdev_dmabuf
->nr_pages
= args
->count
;
377 gntdev_dmabuf
->pages
= args
->pages
;
378 gntdev_dmabuf
->u
.exp
.priv
= args
->priv
;
379 gntdev_dmabuf
->u
.exp
.map
= args
->map
;
381 exp_info
.exp_name
= KBUILD_MODNAME
;
382 if (args
->dev
->driver
&& args
->dev
->driver
->owner
)
383 exp_info
.owner
= args
->dev
->driver
->owner
;
385 exp_info
.owner
= THIS_MODULE
;
386 exp_info
.ops
= &dmabuf_exp_ops
;
387 exp_info
.size
= args
->count
<< PAGE_SHIFT
;
388 exp_info
.flags
= O_RDWR
;
389 exp_info
.priv
= gntdev_dmabuf
;
391 gntdev_dmabuf
->dmabuf
= dma_buf_export(&exp_info
);
392 if (IS_ERR(gntdev_dmabuf
->dmabuf
)) {
393 ret
= PTR_ERR(gntdev_dmabuf
->dmabuf
);
394 gntdev_dmabuf
->dmabuf
= NULL
;
398 ret
= dma_buf_fd(gntdev_dmabuf
->dmabuf
, O_CLOEXEC
);
402 gntdev_dmabuf
->fd
= ret
;
405 pr_debug("Exporting DMA buffer with fd %d\n", ret
);
407 mutex_lock(&args
->dmabuf_priv
->lock
);
408 list_add(&gntdev_dmabuf
->next
, &args
->dmabuf_priv
->exp_list
);
409 mutex_unlock(&args
->dmabuf_priv
->lock
);
410 get_file(gntdev_dmabuf
->priv
->filp
);
414 if (gntdev_dmabuf
->dmabuf
)
415 dma_buf_put(gntdev_dmabuf
->dmabuf
);
416 kfree(gntdev_dmabuf
);
420 static struct gntdev_grant_map
*
421 dmabuf_exp_alloc_backing_storage(struct gntdev_priv
*priv
, int dmabuf_flags
,
424 struct gntdev_grant_map
*map
;
426 if (unlikely(gntdev_test_page_count(count
)))
427 return ERR_PTR(-EINVAL
);
429 if ((dmabuf_flags
& GNTDEV_DMA_FLAG_WC
) &&
430 (dmabuf_flags
& GNTDEV_DMA_FLAG_COHERENT
)) {
431 pr_debug("Wrong dma-buf flags: 0x%x\n", dmabuf_flags
);
432 return ERR_PTR(-EINVAL
);
435 map
= gntdev_alloc_map(priv
, count
, dmabuf_flags
);
437 return ERR_PTR(-ENOMEM
);
442 static int dmabuf_exp_from_refs(struct gntdev_priv
*priv
, int flags
,
443 int count
, u32 domid
, u32
*refs
, u32
*fd
)
445 struct gntdev_grant_map
*map
;
446 struct gntdev_dmabuf_export_args args
;
449 map
= dmabuf_exp_alloc_backing_storage(priv
, flags
, count
);
453 for (i
= 0; i
< count
; i
++) {
454 map
->grants
[i
].domid
= domid
;
455 map
->grants
[i
].ref
= refs
[i
];
458 mutex_lock(&priv
->lock
);
459 gntdev_add_map(priv
, map
);
460 mutex_unlock(&priv
->lock
);
462 map
->flags
|= GNTMAP_host_map
;
463 #if defined(CONFIG_X86)
464 map
->flags
|= GNTMAP_device_map
;
467 ret
= gntdev_map_grant_pages(map
);
473 args
.dev
= priv
->dma_dev
;
474 args
.dmabuf_priv
= priv
->dmabuf_priv
;
475 args
.count
= map
->count
;
476 args
.pages
= map
->pages
;
477 args
.fd
= -1; /* Shut up unnecessary gcc warning for i386 */
479 ret
= dmabuf_exp_from_pages(&args
);
487 dmabuf_exp_remove_map(priv
, map
);
491 /* DMA buffer import support. */
494 dmabuf_imp_grant_foreign_access(struct page
**pages
, u32
*refs
,
495 int count
, int domid
)
497 grant_ref_t priv_gref_head
;
500 ret
= gnttab_alloc_grant_references(count
, &priv_gref_head
);
502 pr_debug("Cannot allocate grant references, ret %d\n", ret
);
506 for (i
= 0; i
< count
; i
++) {
509 cur_ref
= gnttab_claim_grant_reference(&priv_gref_head
);
512 pr_debug("Cannot claim grant reference, ret %d\n", ret
);
516 gnttab_grant_foreign_access_ref(cur_ref
, domid
,
517 xen_page_to_gfn(pages
[i
]), 0);
524 gnttab_free_grant_references(priv_gref_head
);
528 static void dmabuf_imp_end_foreign_access(u32
*refs
, int count
)
532 for (i
= 0; i
< count
; i
++)
533 if (refs
[i
] != GRANT_INVALID_REF
)
534 gnttab_end_foreign_access(refs
[i
], 0, 0UL);
537 static void dmabuf_imp_free_storage(struct gntdev_dmabuf
*gntdev_dmabuf
)
539 kfree(gntdev_dmabuf
->pages
);
540 kfree(gntdev_dmabuf
->u
.imp
.refs
);
541 kfree(gntdev_dmabuf
);
544 static struct gntdev_dmabuf
*dmabuf_imp_alloc_storage(int count
)
546 struct gntdev_dmabuf
*gntdev_dmabuf
;
549 gntdev_dmabuf
= kzalloc(sizeof(*gntdev_dmabuf
), GFP_KERNEL
);
553 gntdev_dmabuf
->u
.imp
.refs
= kcalloc(count
,
554 sizeof(gntdev_dmabuf
->u
.imp
.refs
[0]),
556 if (!gntdev_dmabuf
->u
.imp
.refs
)
559 gntdev_dmabuf
->pages
= kcalloc(count
,
560 sizeof(gntdev_dmabuf
->pages
[0]),
562 if (!gntdev_dmabuf
->pages
)
565 gntdev_dmabuf
->nr_pages
= count
;
567 for (i
= 0; i
< count
; i
++)
568 gntdev_dmabuf
->u
.imp
.refs
[i
] = GRANT_INVALID_REF
;
570 return gntdev_dmabuf
;
573 dmabuf_imp_free_storage(gntdev_dmabuf
);
575 return ERR_PTR(-ENOMEM
);
578 static struct gntdev_dmabuf
*
579 dmabuf_imp_to_refs(struct gntdev_dmabuf_priv
*priv
, struct device
*dev
,
580 int fd
, int count
, int domid
)
582 struct gntdev_dmabuf
*gntdev_dmabuf
, *ret
;
583 struct dma_buf
*dma_buf
;
584 struct dma_buf_attachment
*attach
;
585 struct sg_table
*sgt
;
586 struct sg_page_iter sg_iter
;
589 dma_buf
= dma_buf_get(fd
);
591 return ERR_CAST(dma_buf
);
593 gntdev_dmabuf
= dmabuf_imp_alloc_storage(count
);
594 if (IS_ERR(gntdev_dmabuf
)) {
599 gntdev_dmabuf
->priv
= priv
;
600 gntdev_dmabuf
->fd
= fd
;
602 attach
= dma_buf_attach(dma_buf
, dev
);
603 if (IS_ERR(attach
)) {
604 ret
= ERR_CAST(attach
);
608 gntdev_dmabuf
->u
.imp
.attach
= attach
;
610 sgt
= dma_buf_map_attachment(attach
, DMA_BIDIRECTIONAL
);
616 /* Check number of pages that imported buffer has. */
617 if (attach
->dmabuf
->size
!= gntdev_dmabuf
->nr_pages
<< PAGE_SHIFT
) {
618 ret
= ERR_PTR(-EINVAL
);
619 pr_debug("DMA buffer has %zu pages, user-space expects %d\n",
620 attach
->dmabuf
->size
, gntdev_dmabuf
->nr_pages
);
624 gntdev_dmabuf
->u
.imp
.sgt
= sgt
;
626 /* Now convert sgt to array of pages and check for page validity. */
628 for_each_sg_page(sgt
->sgl
, &sg_iter
, sgt
->nents
, 0) {
629 struct page
*page
= sg_page_iter_page(&sg_iter
);
631 * Check if page is valid: this can happen if we are given
632 * a page from VRAM or other resources which are not backed
635 if (!pfn_valid(page_to_pfn(page
))) {
636 ret
= ERR_PTR(-EINVAL
);
640 gntdev_dmabuf
->pages
[i
++] = page
;
643 ret
= ERR_PTR(dmabuf_imp_grant_foreign_access(gntdev_dmabuf
->pages
,
644 gntdev_dmabuf
->u
.imp
.refs
,
647 goto fail_end_access
;
649 pr_debug("Imported DMA buffer with fd %d\n", fd
);
651 mutex_lock(&priv
->lock
);
652 list_add(&gntdev_dmabuf
->next
, &priv
->imp_list
);
653 mutex_unlock(&priv
->lock
);
655 return gntdev_dmabuf
;
658 dmabuf_imp_end_foreign_access(gntdev_dmabuf
->u
.imp
.refs
, count
);
660 dma_buf_unmap_attachment(attach
, sgt
, DMA_BIDIRECTIONAL
);
662 dma_buf_detach(dma_buf
, attach
);
664 dmabuf_imp_free_storage(gntdev_dmabuf
);
666 dma_buf_put(dma_buf
);
671 * Find the hyper dma-buf by its file descriptor and remove
672 * it from the buffer's list.
674 static struct gntdev_dmabuf
*
675 dmabuf_imp_find_unlink(struct gntdev_dmabuf_priv
*priv
, int fd
)
677 struct gntdev_dmabuf
*q
, *gntdev_dmabuf
, *ret
= ERR_PTR(-ENOENT
);
679 mutex_lock(&priv
->lock
);
680 list_for_each_entry_safe(gntdev_dmabuf
, q
, &priv
->imp_list
, next
) {
681 if (gntdev_dmabuf
->fd
== fd
) {
682 pr_debug("Found gntdev_dmabuf in the import list\n");
684 list_del(&gntdev_dmabuf
->next
);
688 mutex_unlock(&priv
->lock
);
692 static int dmabuf_imp_release(struct gntdev_dmabuf_priv
*priv
, u32 fd
)
694 struct gntdev_dmabuf
*gntdev_dmabuf
;
695 struct dma_buf_attachment
*attach
;
696 struct dma_buf
*dma_buf
;
698 gntdev_dmabuf
= dmabuf_imp_find_unlink(priv
, fd
);
699 if (IS_ERR(gntdev_dmabuf
))
700 return PTR_ERR(gntdev_dmabuf
);
702 pr_debug("Releasing DMA buffer with fd %d\n", fd
);
704 dmabuf_imp_end_foreign_access(gntdev_dmabuf
->u
.imp
.refs
,
705 gntdev_dmabuf
->nr_pages
);
707 attach
= gntdev_dmabuf
->u
.imp
.attach
;
709 if (gntdev_dmabuf
->u
.imp
.sgt
)
710 dma_buf_unmap_attachment(attach
, gntdev_dmabuf
->u
.imp
.sgt
,
712 dma_buf
= attach
->dmabuf
;
713 dma_buf_detach(attach
->dmabuf
, attach
);
714 dma_buf_put(dma_buf
);
716 dmabuf_imp_free_storage(gntdev_dmabuf
);
720 static void dmabuf_imp_release_all(struct gntdev_dmabuf_priv
*priv
)
722 struct gntdev_dmabuf
*q
, *gntdev_dmabuf
;
724 list_for_each_entry_safe(gntdev_dmabuf
, q
, &priv
->imp_list
, next
)
725 dmabuf_imp_release(priv
, gntdev_dmabuf
->fd
);
728 /* DMA buffer IOCTL support. */
730 long gntdev_ioctl_dmabuf_exp_from_refs(struct gntdev_priv
*priv
, int use_ptemod
,
731 struct ioctl_gntdev_dmabuf_exp_from_refs __user
*u
)
733 struct ioctl_gntdev_dmabuf_exp_from_refs op
;
738 pr_debug("Cannot provide dma-buf: use_ptemode %d\n",
743 if (copy_from_user(&op
, u
, sizeof(op
)) != 0)
746 if (unlikely(gntdev_test_page_count(op
.count
)))
749 refs
= kcalloc(op
.count
, sizeof(*refs
), GFP_KERNEL
);
753 if (copy_from_user(refs
, u
->refs
, sizeof(*refs
) * op
.count
) != 0) {
758 ret
= dmabuf_exp_from_refs(priv
, op
.flags
, op
.count
,
759 op
.domid
, refs
, &op
.fd
);
763 if (copy_to_user(u
, &op
, sizeof(op
)) != 0)
771 long gntdev_ioctl_dmabuf_exp_wait_released(struct gntdev_priv
*priv
,
772 struct ioctl_gntdev_dmabuf_exp_wait_released __user
*u
)
774 struct ioctl_gntdev_dmabuf_exp_wait_released op
;
776 if (copy_from_user(&op
, u
, sizeof(op
)) != 0)
779 return dmabuf_exp_wait_released(priv
->dmabuf_priv
, op
.fd
,
783 long gntdev_ioctl_dmabuf_imp_to_refs(struct gntdev_priv
*priv
,
784 struct ioctl_gntdev_dmabuf_imp_to_refs __user
*u
)
786 struct ioctl_gntdev_dmabuf_imp_to_refs op
;
787 struct gntdev_dmabuf
*gntdev_dmabuf
;
790 if (copy_from_user(&op
, u
, sizeof(op
)) != 0)
793 if (unlikely(gntdev_test_page_count(op
.count
)))
796 gntdev_dmabuf
= dmabuf_imp_to_refs(priv
->dmabuf_priv
,
797 priv
->dma_dev
, op
.fd
,
799 if (IS_ERR(gntdev_dmabuf
))
800 return PTR_ERR(gntdev_dmabuf
);
802 if (copy_to_user(u
->refs
, gntdev_dmabuf
->u
.imp
.refs
,
803 sizeof(*u
->refs
) * op
.count
) != 0) {
810 dmabuf_imp_release(priv
->dmabuf_priv
, op
.fd
);
814 long gntdev_ioctl_dmabuf_imp_release(struct gntdev_priv
*priv
,
815 struct ioctl_gntdev_dmabuf_imp_release __user
*u
)
817 struct ioctl_gntdev_dmabuf_imp_release op
;
819 if (copy_from_user(&op
, u
, sizeof(op
)) != 0)
822 return dmabuf_imp_release(priv
->dmabuf_priv
, op
.fd
);
825 struct gntdev_dmabuf_priv
*gntdev_dmabuf_init(struct file
*filp
)
827 struct gntdev_dmabuf_priv
*priv
;
829 priv
= kzalloc(sizeof(*priv
), GFP_KERNEL
);
831 return ERR_PTR(-ENOMEM
);
833 mutex_init(&priv
->lock
);
834 INIT_LIST_HEAD(&priv
->exp_list
);
835 INIT_LIST_HEAD(&priv
->exp_wait_list
);
836 INIT_LIST_HEAD(&priv
->imp_list
);
843 void gntdev_dmabuf_fini(struct gntdev_dmabuf_priv
*priv
)
845 dmabuf_imp_release_all(priv
);