xen-front-pgdir-shbuf.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  1. // SPDX-License-Identifier: GPL-2.0 OR MIT
  2. /*
  3. * Xen frontend/backend page directory based shared buffer
  4. * helper module.
  5. *
  6. * Copyright (C) 2018 EPAM Systems Inc.
  7. *
  8. * Author: Oleksandr Andrushchenko <oleksandr_andrushchenko@epam.com>
  9. */
  10. #include <linux/module.h>
  11. #include <linux/errno.h>
  12. #include <linux/mm.h>
  13. #include <asm/xen/hypervisor.h>
  14. #include <xen/balloon.h>
  15. #include <xen/xen.h>
  16. #include <xen/xenbus.h>
  17. #include <xen/interface/io/ring.h>
  18. #include <xen/xen-front-pgdir-shbuf.h>
  19. /*
  20. * This structure represents the structure of a shared page
  21. * that contains grant references to the pages of the shared
  22. * buffer. This structure is common to many Xen para-virtualized
  23. * protocols at include/xen/interface/io/
  24. */
  25. struct xen_page_directory {
  26. grant_ref_t gref_dir_next_page;
  27. #define XEN_GREF_LIST_END 0
  28. grant_ref_t gref[]; /* Variable length */
  29. };
  30. /*
  31. * Shared buffer ops which are differently implemented
  32. * depending on the allocation mode, e.g. if the buffer
  33. * is allocated by the corresponding backend or frontend.
  34. * Some of the operations.
  35. */
  36. struct xen_front_pgdir_shbuf_ops {
  37. /*
  38. * Calculate number of grefs required to handle this buffer,
  39. * e.g. if grefs are required for page directory only or the buffer
  40. * pages as well.
  41. */
  42. void (*calc_num_grefs)(struct xen_front_pgdir_shbuf *buf);
  43. /* Fill page directory according to para-virtual display protocol. */
  44. void (*fill_page_dir)(struct xen_front_pgdir_shbuf *buf);
  45. /* Claim grant references for the pages of the buffer. */
  46. int (*grant_refs_for_buffer)(struct xen_front_pgdir_shbuf *buf,
  47. grant_ref_t *priv_gref_head, int gref_idx);
  48. /* Map grant references of the buffer. */
  49. int (*map)(struct xen_front_pgdir_shbuf *buf);
  50. /* Unmap grant references of the buffer. */
  51. int (*unmap)(struct xen_front_pgdir_shbuf *buf);
  52. };
  53. /*
  54. * Get granted reference to the very first page of the
  55. * page directory. Usually this is passed to the backend,
  56. * so it can find/fill the grant references to the buffer's
  57. * pages.
  58. *
  59. * \param buf shared buffer which page directory is of interest.
  60. * \return granted reference to the very first page of the
  61. * page directory.
  62. */
  63. grant_ref_t
  64. xen_front_pgdir_shbuf_get_dir_start(struct xen_front_pgdir_shbuf *buf)
  65. {
  66. if (!buf->grefs)
  67. return INVALID_GRANT_REF;
  68. return buf->grefs[0];
  69. }
  70. EXPORT_SYMBOL_GPL(xen_front_pgdir_shbuf_get_dir_start);
  71. /*
  72. * Map granted references of the shared buffer.
  73. *
  74. * Depending on the shared buffer mode of allocation
  75. * (be_alloc flag) this can either do nothing (for buffers
  76. * shared by the frontend itself) or map the provided granted
  77. * references onto the backing storage (buf->pages).
  78. *
  79. * \param buf shared buffer which grants to be mapped.
  80. * \return zero on success or a negative number on failure.
  81. */
  82. int xen_front_pgdir_shbuf_map(struct xen_front_pgdir_shbuf *buf)
  83. {
  84. if (buf->ops && buf->ops->map)
  85. return buf->ops->map(buf);
  86. /* No need to map own grant references. */
  87. return 0;
  88. }
  89. EXPORT_SYMBOL_GPL(xen_front_pgdir_shbuf_map);
  90. /*
  91. * Unmap granted references of the shared buffer.
  92. *
  93. * Depending on the shared buffer mode of allocation
  94. * (be_alloc flag) this can either do nothing (for buffers
  95. * shared by the frontend itself) or unmap the provided granted
  96. * references.
  97. *
  98. * \param buf shared buffer which grants to be unmapped.
  99. * \return zero on success or a negative number on failure.
  100. */
  101. int xen_front_pgdir_shbuf_unmap(struct xen_front_pgdir_shbuf *buf)
  102. {
  103. if (buf->ops && buf->ops->unmap)
  104. return buf->ops->unmap(buf);
  105. /* No need to unmap own grant references. */
  106. return 0;
  107. }
  108. EXPORT_SYMBOL_GPL(xen_front_pgdir_shbuf_unmap);
  109. /*
  110. * Free all the resources of the shared buffer.
  111. *
  112. * \param buf shared buffer which resources to be freed.
  113. */
  114. void xen_front_pgdir_shbuf_free(struct xen_front_pgdir_shbuf *buf)
  115. {
  116. if (buf->grefs) {
  117. int i;
  118. for (i = 0; i < buf->num_grefs; i++)
  119. if (buf->grefs[i] != INVALID_GRANT_REF)
  120. gnttab_end_foreign_access(buf->grefs[i], NULL);
  121. }
  122. kfree(buf->grefs);
  123. kfree(buf->directory);
  124. }
  125. EXPORT_SYMBOL_GPL(xen_front_pgdir_shbuf_free);
  126. /*
  127. * Number of grefs a page can hold with respect to the
  128. * struct xen_page_directory header.
  129. */
  130. #define XEN_NUM_GREFS_PER_PAGE ((PAGE_SIZE - \
  131. offsetof(struct xen_page_directory, \
  132. gref)) / sizeof(grant_ref_t))
  133. /*
  134. * Get the number of pages the page directory consumes itself.
  135. *
  136. * \param buf shared buffer.
  137. */
  138. static int get_num_pages_dir(struct xen_front_pgdir_shbuf *buf)
  139. {
  140. return DIV_ROUND_UP(buf->num_pages, XEN_NUM_GREFS_PER_PAGE);
  141. }
  142. /*
  143. * Calculate the number of grant references needed to share the buffer
  144. * and its pages when backend allocates the buffer.
  145. *
  146. * \param buf shared buffer.
  147. */
  148. static void backend_calc_num_grefs(struct xen_front_pgdir_shbuf *buf)
  149. {
  150. /* Only for pages the page directory consumes itself. */
  151. buf->num_grefs = get_num_pages_dir(buf);
  152. }
  153. /*
  154. * Calculate the number of grant references needed to share the buffer
  155. * and its pages when frontend allocates the buffer.
  156. *
  157. * \param buf shared buffer.
  158. */
  159. static void guest_calc_num_grefs(struct xen_front_pgdir_shbuf *buf)
  160. {
  161. /*
  162. * Number of pages the page directory consumes itself
  163. * plus grefs for the buffer pages.
  164. */
  165. buf->num_grefs = get_num_pages_dir(buf) + buf->num_pages;
  166. }
  167. #define xen_page_to_vaddr(page) \
  168. ((uintptr_t)pfn_to_kaddr(page_to_xen_pfn(page)))
  169. /*
  170. * Unmap the buffer previously mapped with grant references
  171. * provided by the backend.
  172. *
  173. * \param buf shared buffer.
  174. * \return zero on success or a negative number on failure.
  175. */
  176. static int backend_unmap(struct xen_front_pgdir_shbuf *buf)
  177. {
  178. struct gnttab_unmap_grant_ref *unmap_ops;
  179. int i, ret;
  180. if (!buf->pages || !buf->backend_map_handles || !buf->grefs)
  181. return 0;
  182. unmap_ops = kzalloc_objs(*unmap_ops, buf->num_pages);
  183. if (!unmap_ops)
  184. return -ENOMEM;
  185. for (i = 0; i < buf->num_pages; i++) {
  186. phys_addr_t addr;
  187. addr = xen_page_to_vaddr(buf->pages[i]);
  188. gnttab_set_unmap_op(&unmap_ops[i], addr, GNTMAP_host_map,
  189. buf->backend_map_handles[i]);
  190. }
  191. ret = gnttab_unmap_refs(unmap_ops, NULL, buf->pages,
  192. buf->num_pages);
  193. for (i = 0; i < buf->num_pages; i++) {
  194. if (unlikely(unmap_ops[i].status != GNTST_okay))
  195. dev_err(&buf->xb_dev->dev,
  196. "Failed to unmap page %d: %d\n",
  197. i, unmap_ops[i].status);
  198. }
  199. if (ret)
  200. dev_err(&buf->xb_dev->dev,
  201. "Failed to unmap grant references, ret %d", ret);
  202. kfree(unmap_ops);
  203. kfree(buf->backend_map_handles);
  204. buf->backend_map_handles = NULL;
  205. return ret;
  206. }
  207. /*
  208. * Map the buffer with grant references provided by the backend.
  209. *
  210. * \param buf shared buffer.
  211. * \return zero on success or a negative number on failure.
  212. */
  213. static int backend_map(struct xen_front_pgdir_shbuf *buf)
  214. {
  215. struct gnttab_map_grant_ref *map_ops = NULL;
  216. unsigned char *ptr;
  217. int ret, cur_gref, cur_dir_page, cur_page, grefs_left;
  218. map_ops = kzalloc_objs(*map_ops, buf->num_pages);
  219. if (!map_ops)
  220. return -ENOMEM;
  221. buf->backend_map_handles = kzalloc_objs(*buf->backend_map_handles,
  222. buf->num_pages);
  223. if (!buf->backend_map_handles) {
  224. kfree(map_ops);
  225. return -ENOMEM;
  226. }
  227. /*
  228. * Read page directory to get grefs from the backend: for external
  229. * buffer we only allocate buf->grefs for the page directory,
  230. * so buf->num_grefs has number of pages in the page directory itself.
  231. */
  232. ptr = buf->directory;
  233. grefs_left = buf->num_pages;
  234. cur_page = 0;
  235. for (cur_dir_page = 0; cur_dir_page < buf->num_grefs; cur_dir_page++) {
  236. struct xen_page_directory *page_dir =
  237. (struct xen_page_directory *)ptr;
  238. int to_copy = XEN_NUM_GREFS_PER_PAGE;
  239. if (to_copy > grefs_left)
  240. to_copy = grefs_left;
  241. for (cur_gref = 0; cur_gref < to_copy; cur_gref++) {
  242. phys_addr_t addr;
  243. addr = xen_page_to_vaddr(buf->pages[cur_page]);
  244. gnttab_set_map_op(&map_ops[cur_page], addr,
  245. GNTMAP_host_map,
  246. page_dir->gref[cur_gref],
  247. buf->xb_dev->otherend_id);
  248. cur_page++;
  249. }
  250. grefs_left -= to_copy;
  251. ptr += PAGE_SIZE;
  252. }
  253. ret = gnttab_map_refs(map_ops, NULL, buf->pages, buf->num_pages);
  254. /* Save handles even if error, so we can unmap. */
  255. for (cur_page = 0; cur_page < buf->num_pages; cur_page++) {
  256. if (likely(map_ops[cur_page].status == GNTST_okay)) {
  257. buf->backend_map_handles[cur_page] =
  258. map_ops[cur_page].handle;
  259. } else {
  260. buf->backend_map_handles[cur_page] =
  261. INVALID_GRANT_HANDLE;
  262. if (!ret)
  263. ret = -ENXIO;
  264. dev_err(&buf->xb_dev->dev,
  265. "Failed to map page %d: %d\n",
  266. cur_page, map_ops[cur_page].status);
  267. }
  268. }
  269. if (ret) {
  270. dev_err(&buf->xb_dev->dev,
  271. "Failed to map grant references, ret %d", ret);
  272. backend_unmap(buf);
  273. }
  274. kfree(map_ops);
  275. return ret;
  276. }
  277. /*
  278. * Fill page directory with grant references to the pages of the
  279. * page directory itself.
  280. *
  281. * The grant references to the buffer pages are provided by the
  282. * backend in this case.
  283. *
  284. * \param buf shared buffer.
  285. */
  286. static void backend_fill_page_dir(struct xen_front_pgdir_shbuf *buf)
  287. {
  288. struct xen_page_directory *page_dir;
  289. unsigned char *ptr;
  290. int i, num_pages_dir;
  291. ptr = buf->directory;
  292. num_pages_dir = get_num_pages_dir(buf);
  293. /* Fill only grefs for the page directory itself. */
  294. for (i = 0; i < num_pages_dir - 1; i++) {
  295. page_dir = (struct xen_page_directory *)ptr;
  296. page_dir->gref_dir_next_page = buf->grefs[i + 1];
  297. ptr += PAGE_SIZE;
  298. }
  299. /* Last page must say there is no more pages. */
  300. page_dir = (struct xen_page_directory *)ptr;
  301. page_dir->gref_dir_next_page = XEN_GREF_LIST_END;
  302. }
  303. /*
  304. * Fill page directory with grant references to the pages of the
  305. * page directory and the buffer we share with the backend.
  306. *
  307. * \param buf shared buffer.
  308. */
  309. static void guest_fill_page_dir(struct xen_front_pgdir_shbuf *buf)
  310. {
  311. unsigned char *ptr;
  312. int cur_gref, grefs_left, to_copy, i, num_pages_dir;
  313. ptr = buf->directory;
  314. num_pages_dir = get_num_pages_dir(buf);
  315. /*
  316. * While copying, skip grefs at start, they are for pages
  317. * granted for the page directory itself.
  318. */
  319. cur_gref = num_pages_dir;
  320. grefs_left = buf->num_pages;
  321. for (i = 0; i < num_pages_dir; i++) {
  322. struct xen_page_directory *page_dir =
  323. (struct xen_page_directory *)ptr;
  324. if (grefs_left <= XEN_NUM_GREFS_PER_PAGE) {
  325. to_copy = grefs_left;
  326. page_dir->gref_dir_next_page = XEN_GREF_LIST_END;
  327. } else {
  328. to_copy = XEN_NUM_GREFS_PER_PAGE;
  329. page_dir->gref_dir_next_page = buf->grefs[i + 1];
  330. }
  331. memcpy(&page_dir->gref, &buf->grefs[cur_gref],
  332. to_copy * sizeof(grant_ref_t));
  333. ptr += PAGE_SIZE;
  334. grefs_left -= to_copy;
  335. cur_gref += to_copy;
  336. }
  337. }
  338. /*
  339. * Grant references to the frontend's buffer pages.
  340. *
  341. * These will be shared with the backend, so it can
  342. * access the buffer's data.
  343. *
  344. * \param buf shared buffer.
  345. * \return zero on success or a negative number on failure.
  346. */
  347. static int guest_grant_refs_for_buffer(struct xen_front_pgdir_shbuf *buf,
  348. grant_ref_t *priv_gref_head,
  349. int gref_idx)
  350. {
  351. int i, cur_ref, otherend_id;
  352. otherend_id = buf->xb_dev->otherend_id;
  353. for (i = 0; i < buf->num_pages; i++) {
  354. cur_ref = gnttab_claim_grant_reference(priv_gref_head);
  355. if (cur_ref < 0)
  356. return cur_ref;
  357. gnttab_grant_foreign_access_ref(cur_ref, otherend_id,
  358. xen_page_to_gfn(buf->pages[i]),
  359. 0);
  360. buf->grefs[gref_idx++] = cur_ref;
  361. }
  362. return 0;
  363. }
  364. /*
  365. * Grant all the references needed to share the buffer.
  366. *
  367. * Grant references to the page directory pages and, if
  368. * needed, also to the pages of the shared buffer data.
  369. *
  370. * \param buf shared buffer.
  371. * \return zero on success or a negative number on failure.
  372. */
  373. static int grant_references(struct xen_front_pgdir_shbuf *buf)
  374. {
  375. grant_ref_t priv_gref_head;
  376. int ret, i, j, cur_ref;
  377. int otherend_id, num_pages_dir;
  378. ret = gnttab_alloc_grant_references(buf->num_grefs, &priv_gref_head);
  379. if (ret < 0) {
  380. dev_err(&buf->xb_dev->dev,
  381. "Cannot allocate grant references\n");
  382. return ret;
  383. }
  384. otherend_id = buf->xb_dev->otherend_id;
  385. j = 0;
  386. num_pages_dir = get_num_pages_dir(buf);
  387. for (i = 0; i < num_pages_dir; i++) {
  388. unsigned long frame;
  389. cur_ref = gnttab_claim_grant_reference(&priv_gref_head);
  390. if (cur_ref < 0)
  391. return cur_ref;
  392. frame = xen_page_to_gfn(virt_to_page(buf->directory +
  393. PAGE_SIZE * i));
  394. gnttab_grant_foreign_access_ref(cur_ref, otherend_id, frame, 0);
  395. buf->grefs[j++] = cur_ref;
  396. }
  397. if (buf->ops->grant_refs_for_buffer) {
  398. ret = buf->ops->grant_refs_for_buffer(buf, &priv_gref_head, j);
  399. if (ret)
  400. return ret;
  401. }
  402. gnttab_free_grant_references(priv_gref_head);
  403. return 0;
  404. }
  405. /*
  406. * Allocate all required structures to mange shared buffer.
  407. *
  408. * \param buf shared buffer.
  409. * \return zero on success or a negative number on failure.
  410. */
  411. static int alloc_storage(struct xen_front_pgdir_shbuf *buf)
  412. {
  413. buf->grefs = kzalloc_objs(*buf->grefs, buf->num_grefs);
  414. if (!buf->grefs)
  415. return -ENOMEM;
  416. buf->directory = kcalloc(get_num_pages_dir(buf), PAGE_SIZE, GFP_KERNEL);
  417. if (!buf->directory)
  418. return -ENOMEM;
  419. return 0;
  420. }
  421. /*
  422. * For backend allocated buffers we don't need grant_refs_for_buffer
  423. * as those grant references are allocated at backend side.
  424. */
  425. static const struct xen_front_pgdir_shbuf_ops backend_ops = {
  426. .calc_num_grefs = backend_calc_num_grefs,
  427. .fill_page_dir = backend_fill_page_dir,
  428. .map = backend_map,
  429. .unmap = backend_unmap
  430. };
  431. /*
  432. * For locally granted references we do not need to map/unmap
  433. * the references.
  434. */
  435. static const struct xen_front_pgdir_shbuf_ops local_ops = {
  436. .calc_num_grefs = guest_calc_num_grefs,
  437. .fill_page_dir = guest_fill_page_dir,
  438. .grant_refs_for_buffer = guest_grant_refs_for_buffer,
  439. };
  440. /*
  441. * Allocate a new instance of a shared buffer.
  442. *
  443. * \param cfg configuration to be used while allocating a new shared buffer.
  444. * \return zero on success or a negative number on failure.
  445. */
  446. int xen_front_pgdir_shbuf_alloc(struct xen_front_pgdir_shbuf_cfg *cfg)
  447. {
  448. struct xen_front_pgdir_shbuf *buf = cfg->pgdir;
  449. int ret;
  450. if (cfg->be_alloc)
  451. buf->ops = &backend_ops;
  452. else
  453. buf->ops = &local_ops;
  454. buf->xb_dev = cfg->xb_dev;
  455. buf->num_pages = cfg->num_pages;
  456. buf->pages = cfg->pages;
  457. buf->ops->calc_num_grefs(buf);
  458. ret = alloc_storage(buf);
  459. if (ret)
  460. goto fail;
  461. ret = grant_references(buf);
  462. if (ret)
  463. goto fail;
  464. buf->ops->fill_page_dir(buf);
  465. return 0;
  466. fail:
  467. xen_front_pgdir_shbuf_free(buf);
  468. return ret;
  469. }
  470. EXPORT_SYMBOL_GPL(xen_front_pgdir_shbuf_alloc);
  471. MODULE_DESCRIPTION("Xen frontend/backend page directory based "
  472. "shared buffer handling");
  473. MODULE_AUTHOR("Oleksandr Andrushchenko");
  474. MODULE_LICENSE("GPL");