xref: /linux/drivers/xen/gntdev-dmabuf.c (revision 2a0d3d73c112e1ffbb8cc01098b9bd2a3087b40f)
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 
26 MODULE_IMPORT_NS("DMA_BUF");
27 
28 struct 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 
58 struct gntdev_dmabuf_wait_obj {
59 	struct list_head next;
60 	struct gntdev_dmabuf *gntdev_dmabuf;
61 	struct completion completion;
62 };
63 
64 struct gntdev_dmabuf_attachment {
65 	struct sg_table *sgt;
66 	enum dma_data_direction dir;
67 };
68 
69 struct 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 
90 static void dmabuf_exp_release(struct kref *kref);
91 
92 static struct gntdev_dmabuf_wait_obj *
dmabuf_exp_wait_obj_new(struct gntdev_dmabuf_priv * priv,struct gntdev_dmabuf * gntdev_dmabuf)93 dmabuf_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_obj(*obj);
99 	if (!obj) {
100 		kref_put(&gntdev_dmabuf->u.exp.refcount, dmabuf_exp_release);
101 		return ERR_PTR(-ENOMEM);
102 	}
103 
104 	init_completion(&obj->completion);
105 	obj->gntdev_dmabuf = gntdev_dmabuf;
106 
107 	mutex_lock(&priv->lock);
108 	list_add(&obj->next, &priv->exp_wait_list);
109 	/* Put our reference and wait for gntdev_dmabuf's release to fire. */
110 	kref_put(&gntdev_dmabuf->u.exp.refcount, dmabuf_exp_release);
111 	mutex_unlock(&priv->lock);
112 	return obj;
113 }
114 
dmabuf_exp_wait_obj_free(struct gntdev_dmabuf_priv * priv,struct gntdev_dmabuf_wait_obj * obj)115 static void dmabuf_exp_wait_obj_free(struct gntdev_dmabuf_priv *priv,
116 				     struct gntdev_dmabuf_wait_obj *obj)
117 {
118 	mutex_lock(&priv->lock);
119 	list_del(&obj->next);
120 	mutex_unlock(&priv->lock);
121 	kfree(obj);
122 }
123 
dmabuf_exp_wait_obj_wait(struct gntdev_dmabuf_wait_obj * obj,u32 wait_to_ms)124 static int dmabuf_exp_wait_obj_wait(struct gntdev_dmabuf_wait_obj *obj,
125 				    u32 wait_to_ms)
126 {
127 	if (wait_for_completion_timeout(&obj->completion,
128 			msecs_to_jiffies(wait_to_ms)) <= 0)
129 		return -ETIMEDOUT;
130 
131 	return 0;
132 }
133 
dmabuf_exp_wait_obj_signal(struct gntdev_dmabuf_priv * priv,struct gntdev_dmabuf * gntdev_dmabuf)134 static void dmabuf_exp_wait_obj_signal(struct gntdev_dmabuf_priv *priv,
135 				       struct gntdev_dmabuf *gntdev_dmabuf)
136 {
137 	struct gntdev_dmabuf_wait_obj *obj;
138 
139 	list_for_each_entry(obj, &priv->exp_wait_list, next)
140 		if (obj->gntdev_dmabuf == gntdev_dmabuf) {
141 			pr_debug("Found gntdev_dmabuf in the wait list, wake\n");
142 			complete_all(&obj->completion);
143 			break;
144 		}
145 }
146 
147 static struct gntdev_dmabuf *
dmabuf_exp_wait_obj_get_dmabuf(struct gntdev_dmabuf_priv * priv,int fd)148 dmabuf_exp_wait_obj_get_dmabuf(struct gntdev_dmabuf_priv *priv, int fd)
149 {
150 	struct gntdev_dmabuf *gntdev_dmabuf, *ret = ERR_PTR(-ENOENT);
151 
152 	mutex_lock(&priv->lock);
153 	list_for_each_entry(gntdev_dmabuf, &priv->exp_list, next)
154 		if (gntdev_dmabuf->fd == fd) {
155 			pr_debug("Found gntdev_dmabuf in the wait list\n");
156 			kref_get(&gntdev_dmabuf->u.exp.refcount);
157 			ret = gntdev_dmabuf;
158 			break;
159 		}
160 	mutex_unlock(&priv->lock);
161 	return ret;
162 }
163 
dmabuf_exp_wait_released(struct gntdev_dmabuf_priv * priv,int fd,int wait_to_ms)164 static int dmabuf_exp_wait_released(struct gntdev_dmabuf_priv *priv, int fd,
165 				    int wait_to_ms)
166 {
167 	struct gntdev_dmabuf *gntdev_dmabuf;
168 	struct gntdev_dmabuf_wait_obj *obj;
169 	int ret;
170 
171 	pr_debug("Will wait for dma-buf with fd %d\n", fd);
172 	/*
173 	 * Try to find the DMA buffer: if not found means that
174 	 * either the buffer has already been released or file descriptor
175 	 * provided is wrong.
176 	 */
177 	gntdev_dmabuf = dmabuf_exp_wait_obj_get_dmabuf(priv, fd);
178 	if (IS_ERR(gntdev_dmabuf))
179 		return PTR_ERR(gntdev_dmabuf);
180 
181 	/*
182 	 * gntdev_dmabuf still exists and is reference count locked by us now,
183 	 * so prepare to wait: allocate wait object and add it to the wait list,
184 	 * so we can find it on release.
185 	 */
186 	obj = dmabuf_exp_wait_obj_new(priv, gntdev_dmabuf);
187 	if (IS_ERR(obj))
188 		return PTR_ERR(obj);
189 
190 	ret = dmabuf_exp_wait_obj_wait(obj, wait_to_ms);
191 	dmabuf_exp_wait_obj_free(priv, obj);
192 	return ret;
193 }
194 
195 /* DMA buffer export support. */
196 
197 static struct sg_table *
dmabuf_pages_to_sgt(struct page ** pages,unsigned int nr_pages)198 dmabuf_pages_to_sgt(struct page **pages, unsigned int nr_pages)
199 {
200 	struct sg_table *sgt;
201 	int ret;
202 
203 	sgt = kmalloc_obj(*sgt);
204 	if (!sgt) {
205 		ret = -ENOMEM;
206 		goto out;
207 	}
208 
209 	ret = sg_alloc_table_from_pages(sgt, pages, nr_pages, 0,
210 					nr_pages << PAGE_SHIFT,
211 					GFP_KERNEL);
212 	if (ret)
213 		goto out;
214 
215 	return sgt;
216 
217 out:
218 	kfree(sgt);
219 	return ERR_PTR(ret);
220 }
221 
dmabuf_exp_ops_attach(struct dma_buf * dma_buf,struct dma_buf_attachment * attach)222 static int dmabuf_exp_ops_attach(struct dma_buf *dma_buf,
223 				 struct dma_buf_attachment *attach)
224 {
225 	struct gntdev_dmabuf_attachment *gntdev_dmabuf_attach;
226 
227 	gntdev_dmabuf_attach = kzalloc_obj(*gntdev_dmabuf_attach);
228 	if (!gntdev_dmabuf_attach)
229 		return -ENOMEM;
230 
231 	gntdev_dmabuf_attach->dir = DMA_NONE;
232 	attach->priv = gntdev_dmabuf_attach;
233 	return 0;
234 }
235 
dmabuf_exp_ops_detach(struct dma_buf * dma_buf,struct dma_buf_attachment * attach)236 static void dmabuf_exp_ops_detach(struct dma_buf *dma_buf,
237 				  struct dma_buf_attachment *attach)
238 {
239 	struct gntdev_dmabuf_attachment *gntdev_dmabuf_attach = attach->priv;
240 
241 	if (gntdev_dmabuf_attach) {
242 		struct sg_table *sgt = gntdev_dmabuf_attach->sgt;
243 
244 		if (sgt) {
245 			if (gntdev_dmabuf_attach->dir != DMA_NONE)
246 				dma_unmap_sgtable(attach->dev, sgt,
247 						  gntdev_dmabuf_attach->dir,
248 						  DMA_ATTR_SKIP_CPU_SYNC);
249 			sg_free_table(sgt);
250 		}
251 
252 		kfree(sgt);
253 		kfree(gntdev_dmabuf_attach);
254 		attach->priv = NULL;
255 	}
256 }
257 
258 static struct sg_table *
dmabuf_exp_ops_map_dma_buf(struct dma_buf_attachment * attach,enum dma_data_direction dir)259 dmabuf_exp_ops_map_dma_buf(struct dma_buf_attachment *attach,
260 			   enum dma_data_direction dir)
261 {
262 	struct gntdev_dmabuf_attachment *gntdev_dmabuf_attach = attach->priv;
263 	struct gntdev_dmabuf *gntdev_dmabuf = attach->dmabuf->priv;
264 	struct sg_table *sgt;
265 
266 	pr_debug("Mapping %d pages for dev %p\n", gntdev_dmabuf->nr_pages,
267 		 attach->dev);
268 
269 	if (dir == DMA_NONE || !gntdev_dmabuf_attach)
270 		return ERR_PTR(-EINVAL);
271 
272 	/* Return the cached mapping when possible. */
273 	if (gntdev_dmabuf_attach->dir == dir)
274 		return gntdev_dmabuf_attach->sgt;
275 
276 	/*
277 	 * Two mappings with different directions for the same attachment are
278 	 * not allowed.
279 	 */
280 	if (gntdev_dmabuf_attach->dir != DMA_NONE)
281 		return ERR_PTR(-EBUSY);
282 
283 	sgt = dmabuf_pages_to_sgt(gntdev_dmabuf->pages,
284 				  gntdev_dmabuf->nr_pages);
285 	if (!IS_ERR(sgt)) {
286 		if (dma_map_sgtable(attach->dev, sgt, dir,
287 				    DMA_ATTR_SKIP_CPU_SYNC)) {
288 			sg_free_table(sgt);
289 			kfree(sgt);
290 			sgt = ERR_PTR(-ENOMEM);
291 		} else {
292 			gntdev_dmabuf_attach->sgt = sgt;
293 			gntdev_dmabuf_attach->dir = dir;
294 		}
295 	}
296 	if (IS_ERR(sgt))
297 		pr_debug("Failed to map sg table for dev %p\n", attach->dev);
298 	return sgt;
299 }
300 
dmabuf_exp_ops_unmap_dma_buf(struct dma_buf_attachment * attach,struct sg_table * sgt,enum dma_data_direction dir)301 static void dmabuf_exp_ops_unmap_dma_buf(struct dma_buf_attachment *attach,
302 					 struct sg_table *sgt,
303 					 enum dma_data_direction dir)
304 {
305 	/* Not implemented. The unmap is done at dmabuf_exp_ops_detach(). */
306 }
307 
dmabuf_exp_release(struct kref * kref)308 static void dmabuf_exp_release(struct kref *kref)
309 {
310 	struct gntdev_dmabuf *gntdev_dmabuf =
311 		container_of(kref, struct gntdev_dmabuf, u.exp.refcount);
312 
313 	dmabuf_exp_wait_obj_signal(gntdev_dmabuf->priv, gntdev_dmabuf);
314 	list_del(&gntdev_dmabuf->next);
315 	fput(gntdev_dmabuf->priv->filp);
316 	kfree(gntdev_dmabuf);
317 }
318 
dmabuf_exp_remove_map(struct gntdev_priv * priv,struct gntdev_grant_map * map)319 static void dmabuf_exp_remove_map(struct gntdev_priv *priv,
320 				  struct gntdev_grant_map *map)
321 {
322 	mutex_lock(&priv->lock);
323 	list_del(&map->next);
324 	gntdev_put_map(NULL /* already removed */, map);
325 	mutex_unlock(&priv->lock);
326 }
327 
dmabuf_exp_ops_release(struct dma_buf * dma_buf)328 static void dmabuf_exp_ops_release(struct dma_buf *dma_buf)
329 {
330 	struct gntdev_dmabuf *gntdev_dmabuf = dma_buf->priv;
331 	struct gntdev_dmabuf_priv *priv = gntdev_dmabuf->priv;
332 
333 	dmabuf_exp_remove_map(gntdev_dmabuf->u.exp.priv,
334 			      gntdev_dmabuf->u.exp.map);
335 	mutex_lock(&priv->lock);
336 	kref_put(&gntdev_dmabuf->u.exp.refcount, dmabuf_exp_release);
337 	mutex_unlock(&priv->lock);
338 }
339 
340 static const struct dma_buf_ops dmabuf_exp_ops =  {
341 	.attach = dmabuf_exp_ops_attach,
342 	.detach = dmabuf_exp_ops_detach,
343 	.map_dma_buf = dmabuf_exp_ops_map_dma_buf,
344 	.unmap_dma_buf = dmabuf_exp_ops_unmap_dma_buf,
345 	.release = dmabuf_exp_ops_release,
346 };
347 
348 struct gntdev_dmabuf_export_args {
349 	struct gntdev_priv *priv;
350 	struct gntdev_grant_map *map;
351 	struct gntdev_dmabuf_priv *dmabuf_priv;
352 	struct device *dev;
353 	int count;
354 	struct page **pages;
355 	u32 fd;
356 };
357 
dmabuf_exp_from_pages(struct gntdev_dmabuf_export_args * args)358 static int dmabuf_exp_from_pages(struct gntdev_dmabuf_export_args *args)
359 {
360 	DEFINE_DMA_BUF_EXPORT_INFO(exp_info);
361 	struct gntdev_dmabuf *gntdev_dmabuf __free(kfree) = NULL;
362 	CLASS(get_unused_fd, ret)(O_CLOEXEC);
363 
364 	if (ret < 0)
365 		return ret;
366 
367 	gntdev_dmabuf = kzalloc_obj(*gntdev_dmabuf);
368 	if (!gntdev_dmabuf)
369 		return -ENOMEM;
370 
371 	kref_init(&gntdev_dmabuf->u.exp.refcount);
372 
373 	gntdev_dmabuf->priv = args->dmabuf_priv;
374 	gntdev_dmabuf->nr_pages = args->count;
375 	gntdev_dmabuf->pages = args->pages;
376 	gntdev_dmabuf->u.exp.priv = args->priv;
377 	gntdev_dmabuf->u.exp.map = args->map;
378 
379 	exp_info.exp_name = KBUILD_MODNAME;
380 	if (args->dev->driver && args->dev->driver->owner)
381 		exp_info.owner = args->dev->driver->owner;
382 	else
383 		exp_info.owner = THIS_MODULE;
384 	exp_info.ops = &dmabuf_exp_ops;
385 	exp_info.size = args->count << PAGE_SHIFT;
386 	exp_info.flags = O_RDWR;
387 	exp_info.priv = gntdev_dmabuf;
388 
389 	gntdev_dmabuf->dmabuf = dma_buf_export(&exp_info);
390 	if (IS_ERR(gntdev_dmabuf->dmabuf))
391 		return PTR_ERR(gntdev_dmabuf->dmabuf);
392 
393 	gntdev_dmabuf->fd = ret;
394 	args->fd = ret;
395 
396 	pr_debug("Exporting DMA buffer with fd %d\n", ret);
397 
398 	get_file(gntdev_dmabuf->priv->filp);
399 	mutex_lock(&args->dmabuf_priv->lock);
400 	list_add(&gntdev_dmabuf->next, &args->dmabuf_priv->exp_list);
401 	mutex_unlock(&args->dmabuf_priv->lock);
402 
403 	fd_install(take_fd(ret), no_free_ptr(gntdev_dmabuf)->dmabuf->file);
404 	return 0;
405 }
406 
407 static struct gntdev_grant_map *
dmabuf_exp_alloc_backing_storage(struct gntdev_priv * priv,int dmabuf_flags,int count)408 dmabuf_exp_alloc_backing_storage(struct gntdev_priv *priv, int dmabuf_flags,
409 				 int count)
410 {
411 	struct gntdev_grant_map *map;
412 
413 	if (unlikely(gntdev_test_page_count(count)))
414 		return ERR_PTR(-EINVAL);
415 
416 	if ((dmabuf_flags & GNTDEV_DMA_FLAG_WC) &&
417 	    (dmabuf_flags & GNTDEV_DMA_FLAG_COHERENT)) {
418 		pr_debug("Wrong dma-buf flags: 0x%x\n", dmabuf_flags);
419 		return ERR_PTR(-EINVAL);
420 	}
421 
422 	map = gntdev_alloc_map(priv, count, dmabuf_flags);
423 	if (!map)
424 		return ERR_PTR(-ENOMEM);
425 
426 	return map;
427 }
428 
dmabuf_exp_from_refs(struct gntdev_priv * priv,int flags,int count,u32 domid,u32 * refs,u32 * fd)429 static int dmabuf_exp_from_refs(struct gntdev_priv *priv, int flags,
430 				int count, u32 domid, u32 *refs, u32 *fd)
431 {
432 	struct gntdev_grant_map *map;
433 	struct gntdev_dmabuf_export_args args;
434 	int i, ret;
435 
436 	map = dmabuf_exp_alloc_backing_storage(priv, flags, count);
437 	if (IS_ERR(map))
438 		return PTR_ERR(map);
439 
440 	for (i = 0; i < count; i++) {
441 		map->grants[i].domid = domid;
442 		map->grants[i].ref = refs[i];
443 	}
444 
445 	mutex_lock(&priv->lock);
446 	gntdev_add_map(priv, map);
447 	mutex_unlock(&priv->lock);
448 
449 	map->flags |= GNTMAP_host_map;
450 #if defined(CONFIG_X86)
451 	map->flags |= GNTMAP_device_map;
452 #endif
453 
454 	ret = gntdev_map_grant_pages(map);
455 	if (ret < 0)
456 		goto out;
457 
458 	args.priv = priv;
459 	args.map = map;
460 	args.dev = priv->dma_dev;
461 	args.dmabuf_priv = priv->dmabuf_priv;
462 	args.count = map->count;
463 	args.pages = map->pages;
464 	args.fd = -1; /* Shut up unnecessary gcc warning for i386 */
465 
466 	ret = dmabuf_exp_from_pages(&args);
467 	if (ret < 0)
468 		goto out;
469 
470 	*fd = args.fd;
471 	return 0;
472 
473 out:
474 	dmabuf_exp_remove_map(priv, map);
475 	return ret;
476 }
477 
478 /* DMA buffer import support. */
479 
480 static int
dmabuf_imp_grant_foreign_access(unsigned long * gfns,u32 * refs,int count,int domid)481 dmabuf_imp_grant_foreign_access(unsigned long *gfns, u32 *refs,
482 				int count, int domid)
483 {
484 	grant_ref_t priv_gref_head;
485 	int i, ret;
486 
487 	ret = gnttab_alloc_grant_references(count, &priv_gref_head);
488 	if (ret < 0) {
489 		pr_debug("Cannot allocate grant references, ret %d\n", ret);
490 		return ret;
491 	}
492 
493 	for (i = 0; i < count; i++) {
494 		int cur_ref;
495 
496 		cur_ref = gnttab_claim_grant_reference(&priv_gref_head);
497 		if (cur_ref < 0) {
498 			ret = cur_ref;
499 			pr_debug("Cannot claim grant reference, ret %d\n", ret);
500 			goto out;
501 		}
502 
503 		gnttab_grant_foreign_access_ref(cur_ref, domid,
504 						gfns[i], 0);
505 		refs[i] = cur_ref;
506 	}
507 
508 	return 0;
509 
510 out:
511 	gnttab_free_grant_references(priv_gref_head);
512 	return ret;
513 }
514 
dmabuf_imp_end_foreign_access(u32 * refs,int count)515 static void dmabuf_imp_end_foreign_access(u32 *refs, int count)
516 {
517 	int i;
518 
519 	for (i = 0; i < count; i++)
520 		if (refs[i] != INVALID_GRANT_REF)
521 			gnttab_end_foreign_access(refs[i], NULL);
522 }
523 
dmabuf_imp_free_storage(struct gntdev_dmabuf * gntdev_dmabuf)524 static void dmabuf_imp_free_storage(struct gntdev_dmabuf *gntdev_dmabuf)
525 {
526 	kfree(gntdev_dmabuf->u.imp.refs);
527 	kfree(gntdev_dmabuf);
528 }
529 
dmabuf_imp_alloc_storage(int count)530 static struct gntdev_dmabuf *dmabuf_imp_alloc_storage(int count)
531 {
532 	struct gntdev_dmabuf *gntdev_dmabuf;
533 	int i;
534 
535 	gntdev_dmabuf = kzalloc_obj(*gntdev_dmabuf);
536 	if (!gntdev_dmabuf)
537 		goto fail_no_free;
538 
539 	gntdev_dmabuf->u.imp.refs = kzalloc_objs(gntdev_dmabuf->u.imp.refs[0],
540 						 count);
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 
551 fail:
552 	dmabuf_imp_free_storage(gntdev_dmabuf);
553 fail_no_free:
554 	return ERR_PTR(-ENOMEM);
555 }
556 
557 static struct gntdev_dmabuf *
dmabuf_imp_to_refs(struct gntdev_dmabuf_priv * priv,struct device * dev,int fd,int count,int domid)558 dmabuf_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(dma_buf))
571 		return ERR_CAST(dma_buf);
572 
573 	gntdev_dmabuf = dmabuf_imp_alloc_storage(count);
574 	if (IS_ERR(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(dma_buf, dev);
583 	if (IS_ERR(attach)) {
584 		ret = ERR_CAST(attach);
585 		goto fail_free_obj;
586 	}
587 
588 	gntdev_dmabuf->u.imp.attach = attach;
589 
590 	sgt = dma_buf_map_attachment_unlocked(attach, DMA_BIDIRECTIONAL);
591 	if (IS_ERR(sgt)) {
592 		ret = ERR_CAST(sgt);
593 		goto fail_detach;
594 	}
595 
596 	/* Check that we have zero offset. */
597 	if (sgt->sgl->offset) {
598 		ret = ERR_PTR(-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(-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(-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(&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(dmabuf_imp_grant_foreign_access(gfns,
636 						      gntdev_dmabuf->u.imp.refs,
637 						      count, domid));
638 	kfree(gfns);
639 	if (IS_ERR(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(&gntdev_dmabuf->next, &priv->imp_list);
646 	mutex_unlock(&priv->lock);
647 
648 	return gntdev_dmabuf;
649 
650 fail_end_access:
651 	dmabuf_imp_end_foreign_access(gntdev_dmabuf->u.imp.refs, count);
652 fail_unmap:
653 	dma_buf_unmap_attachment_unlocked(attach, sgt, DMA_BIDIRECTIONAL);
654 fail_detach:
655 	dma_buf_detach(dma_buf, attach);
656 fail_free_obj:
657 	dmabuf_imp_free_storage(gntdev_dmabuf);
658 fail_put:
659 	dma_buf_put(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  */
667 static struct gntdev_dmabuf *
dmabuf_imp_find_unlink(struct gntdev_dmabuf_priv * priv,int fd)668 dmabuf_imp_find_unlink(struct gntdev_dmabuf_priv *priv, int fd)
669 {
670 	struct gntdev_dmabuf *q, *gntdev_dmabuf, *ret = ERR_PTR(-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(&gntdev_dmabuf->next);
678 			break;
679 		}
680 	}
681 	mutex_unlock(&priv->lock);
682 	return ret;
683 }
684 
dmabuf_imp_release(struct gntdev_dmabuf_priv * priv,u32 fd)685 static 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(gntdev_dmabuf))
693 		return PTR_ERR(gntdev_dmabuf);
694 
695 	pr_debug("Releasing DMA buffer with fd %d\n", fd);
696 
697 	dmabuf_imp_end_foreign_access(gntdev_dmabuf->u.imp.refs,
698 				      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, gntdev_dmabuf->u.imp.sgt,
704 						  DMA_BIDIRECTIONAL);
705 	dma_buf = attach->dmabuf;
706 	dma_buf_detach(attach->dmabuf, attach);
707 	dma_buf_put(dma_buf);
708 
709 	dmabuf_imp_free_storage(gntdev_dmabuf);
710 	return 0;
711 }
712 
dmabuf_imp_release_all(struct gntdev_dmabuf_priv * priv)713 static 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, gntdev_dmabuf->fd);
719 }
720 
721 /* DMA buffer IOCTL support. */
722 
gntdev_ioctl_dmabuf_exp_from_refs(struct gntdev_priv * priv,struct ioctl_gntdev_dmabuf_exp_from_refs __user * u)723 long 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(&op, u, 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(refs, u->refs, sizeof(*refs) * op.count) != 0) {
746 		ret = -EFAULT;
747 		goto out;
748 	}
749 
750 	ret = dmabuf_exp_from_refs(priv, op.flags, op.count,
751 				   op.domid, refs, &op.fd);
752 	if (ret)
753 		goto out;
754 
755 	if (copy_to_user(u, &op, sizeof(op)) != 0)
756 		ret = -EFAULT;
757 
758 out:
759 	kfree(refs);
760 	return ret;
761 }
762 
gntdev_ioctl_dmabuf_exp_wait_released(struct gntdev_priv * priv,struct ioctl_gntdev_dmabuf_exp_wait_released __user * u)763 long 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(&op, u, sizeof(op)) != 0)
769 		return -EFAULT;
770 
771 	return dmabuf_exp_wait_released(priv->dmabuf_priv, op.fd,
772 					op.wait_to_ms);
773 }
774 
gntdev_ioctl_dmabuf_imp_to_refs(struct gntdev_priv * priv,struct ioctl_gntdev_dmabuf_imp_to_refs __user * u)775 long 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(&op, u, 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->dmabuf_priv,
789 					   priv->dma_dev, op.fd,
790 					   op.count, op.domid);
791 	if (IS_ERR(gntdev_dmabuf))
792 		return PTR_ERR(gntdev_dmabuf);
793 
794 	if (copy_to_user(u->refs, gntdev_dmabuf->u.imp.refs,
795 			 sizeof(*u->refs) * op.count) != 0) {
796 		ret = -EFAULT;
797 		goto out_release;
798 	}
799 	return 0;
800 
801 out_release:
802 	dmabuf_imp_release(priv->dmabuf_priv, op.fd);
803 	return ret;
804 }
805 
gntdev_ioctl_dmabuf_imp_release(struct gntdev_priv * priv,struct ioctl_gntdev_dmabuf_imp_release __user * u)806 long 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(&op, u, sizeof(op)) != 0)
812 		return -EFAULT;
813 
814 	return dmabuf_imp_release(priv->dmabuf_priv, op.fd);
815 }
816 
gntdev_dmabuf_init(struct file * filp)817 struct gntdev_dmabuf_priv *gntdev_dmabuf_init(struct file *filp)
818 {
819 	struct gntdev_dmabuf_priv *priv;
820 
821 	priv = kzalloc_obj(*priv);
822 	if (!priv)
823 		return ERR_PTR(-ENOMEM);
824 
825 	mutex_init(&priv->lock);
826 	INIT_LIST_HEAD(&priv->exp_list);
827 	INIT_LIST_HEAD(&priv->exp_wait_list);
828 	INIT_LIST_HEAD(&priv->imp_list);
829 
830 	priv->filp = filp;
831 
832 	return priv;
833 }
834 
gntdev_dmabuf_fini(struct gntdev_dmabuf_priv * priv)835 void gntdev_dmabuf_fini(struct gntdev_dmabuf_priv *priv)
836 {
837 	dmabuf_imp_release_all(priv);
838 	kfree(priv);
839 }
840