gntdev-dmabuf.c 20 KB

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