gntdev-dmabuf.c 20 KB

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