xen-front-pgdir-shbuf.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  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 <[email protected]>
  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[1]; /* 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 = kcalloc(buf->num_pages, sizeof(*unmap_ops),
  183. GFP_KERNEL);
  184. if (!unmap_ops)
  185. return -ENOMEM;
  186. for (i = 0; i < buf->num_pages; i++) {
  187. phys_addr_t addr;
  188. addr = xen_page_to_vaddr(buf->pages[i]);
  189. gnttab_set_unmap_op(&unmap_ops[i], addr, GNTMAP_host_map,
  190. buf->backend_map_handles[i]);
  191. }
  192. ret = gnttab_unmap_refs(unmap_ops, NULL, buf->pages,
  193. buf->num_pages);
  194. for (i = 0; i < buf->num_pages; i++) {
  195. if (unlikely(unmap_ops[i].status != GNTST_okay))
  196. dev_err(&buf->xb_dev->dev,
  197. "Failed to unmap page %d: %d\n",
  198. i, unmap_ops[i].status);
  199. }
  200. if (ret)
  201. dev_err(&buf->xb_dev->dev,
  202. "Failed to unmap grant references, ret %d", ret);
  203. kfree(unmap_ops);
  204. kfree(buf->backend_map_handles);
  205. buf->backend_map_handles = NULL;
  206. return ret;
  207. }
  208. /**
  209. * Map the buffer with grant references provided by the backend.
  210. *
  211. * \param buf shared buffer.
  212. * \return zero on success or a negative number on failure.
  213. */
  214. static int backend_map(struct xen_front_pgdir_shbuf *buf)
  215. {
  216. struct gnttab_map_grant_ref *map_ops = NULL;
  217. unsigned char *ptr;
  218. int ret, cur_gref, cur_dir_page, cur_page, grefs_left;
  219. map_ops = kcalloc(buf->num_pages, sizeof(*map_ops), GFP_KERNEL);
  220. if (!map_ops)
  221. return -ENOMEM;
  222. buf->backend_map_handles = kcalloc(buf->num_pages,
  223. sizeof(*buf->backend_map_handles),
  224. GFP_KERNEL);
  225. if (!buf->backend_map_handles) {
  226. kfree(map_ops);
  227. return -ENOMEM;
  228. }
  229. /*
  230. * Read page directory to get grefs from the backend: for external
  231. * buffer we only allocate buf->grefs for the page directory,
  232. * so buf->num_grefs has number of pages in the page directory itself.
  233. */
  234. ptr = buf->directory;
  235. grefs_left = buf->num_pages;
  236. cur_page = 0;
  237. for (cur_dir_page = 0; cur_dir_page < buf->num_grefs; cur_dir_page++) {
  238. struct xen_page_directory *page_dir =
  239. (struct xen_page_directory *)ptr;
  240. int to_copy = XEN_NUM_GREFS_PER_PAGE;
  241. if (to_copy > grefs_left)
  242. to_copy = grefs_left;
  243. for (cur_gref = 0; cur_gref < to_copy; cur_gref++) {
  244. phys_addr_t addr;
  245. addr = xen_page_to_vaddr(buf->pages[cur_page]);
  246. gnttab_set_map_op(&map_ops[cur_page], addr,
  247. GNTMAP_host_map,
  248. page_dir->gref[cur_gref],
  249. buf->xb_dev->otherend_id);
  250. cur_page++;
  251. }
  252. grefs_left -= to_copy;
  253. ptr += PAGE_SIZE;
  254. }
  255. ret = gnttab_map_refs(map_ops, NULL, buf->pages, buf->num_pages);
  256. /* Save handles even if error, so we can unmap. */
  257. for (cur_page = 0; cur_page < buf->num_pages; cur_page++) {
  258. if (likely(map_ops[cur_page].status == GNTST_okay)) {
  259. buf->backend_map_handles[cur_page] =
  260. map_ops[cur_page].handle;
  261. } else {
  262. buf->backend_map_handles[cur_page] =
  263. INVALID_GRANT_HANDLE;
  264. if (!ret)
  265. ret = -ENXIO;
  266. dev_err(&buf->xb_dev->dev,
  267. "Failed to map page %d: %d\n",
  268. cur_page, map_ops[cur_page].status);
  269. }
  270. }
  271. if (ret) {
  272. dev_err(&buf->xb_dev->dev,
  273. "Failed to map grant references, ret %d", ret);
  274. backend_unmap(buf);
  275. }
  276. kfree(map_ops);
  277. return ret;
  278. }
  279. /**
  280. * Fill page directory with grant references to the pages of the
  281. * page directory itself.
  282. *
  283. * The grant references to the buffer pages are provided by the
  284. * backend in this case.
  285. *
  286. * \param buf shared buffer.
  287. */
  288. static void backend_fill_page_dir(struct xen_front_pgdir_shbuf *buf)
  289. {
  290. struct xen_page_directory *page_dir;
  291. unsigned char *ptr;
  292. int i, num_pages_dir;
  293. ptr = buf->directory;
  294. num_pages_dir = get_num_pages_dir(buf);
  295. /* Fill only grefs for the page directory itself. */
  296. for (i = 0; i < num_pages_dir - 1; i++) {
  297. page_dir = (struct xen_page_directory *)ptr;
  298. page_dir->gref_dir_next_page = buf->grefs[i + 1];
  299. ptr += PAGE_SIZE;
  300. }
  301. /* Last page must say there is no more pages. */
  302. page_dir = (struct xen_page_directory *)ptr;
  303. page_dir->gref_dir_next_page = XEN_GREF_LIST_END;
  304. }
  305. /**
  306. * Fill page directory with grant references to the pages of the
  307. * page directory and the buffer we share with the backend.
  308. *
  309. * \param buf shared buffer.
  310. */
  311. static void guest_fill_page_dir(struct xen_front_pgdir_shbuf *buf)
  312. {
  313. unsigned char *ptr;
  314. int cur_gref, grefs_left, to_copy, i, num_pages_dir;
  315. ptr = buf->directory;
  316. num_pages_dir = get_num_pages_dir(buf);
  317. /*
  318. * While copying, skip grefs at start, they are for pages
  319. * granted for the page directory itself.
  320. */
  321. cur_gref = num_pages_dir;
  322. grefs_left = buf->num_pages;
  323. for (i = 0; i < num_pages_dir; i++) {
  324. struct xen_page_directory *page_dir =
  325. (struct xen_page_directory *)ptr;
  326. if (grefs_left <= XEN_NUM_GREFS_PER_PAGE) {
  327. to_copy = grefs_left;
  328. page_dir->gref_dir_next_page = XEN_GREF_LIST_END;
  329. } else {
  330. to_copy = XEN_NUM_GREFS_PER_PAGE;
  331. page_dir->gref_dir_next_page = buf->grefs[i + 1];
  332. }
  333. memcpy(&page_dir->gref, &buf->grefs[cur_gref],
  334. to_copy * sizeof(grant_ref_t));
  335. ptr += PAGE_SIZE;
  336. grefs_left -= to_copy;
  337. cur_gref += to_copy;
  338. }
  339. }
  340. /**
  341. * Grant references to the frontend's buffer pages.
  342. *
  343. * These will be shared with the backend, so it can
  344. * access the buffer's data.
  345. *
  346. * \param buf shared buffer.
  347. * \return zero on success or a negative number on failure.
  348. */
  349. static int guest_grant_refs_for_buffer(struct xen_front_pgdir_shbuf *buf,
  350. grant_ref_t *priv_gref_head,
  351. int gref_idx)
  352. {
  353. int i, cur_ref, otherend_id;
  354. otherend_id = buf->xb_dev->otherend_id;
  355. for (i = 0; i < buf->num_pages; i++) {
  356. cur_ref = gnttab_claim_grant_reference(priv_gref_head);
  357. if (cur_ref < 0)
  358. return cur_ref;
  359. gnttab_grant_foreign_access_ref(cur_ref, otherend_id,
  360. xen_page_to_gfn(buf->pages[i]),
  361. 0);
  362. buf->grefs[gref_idx++] = cur_ref;
  363. }
  364. return 0;
  365. }
  366. /**
  367. * Grant all the references needed to share the buffer.
  368. *
  369. * Grant references to the page directory pages and, if
  370. * needed, also to the pages of the shared buffer data.
  371. *
  372. * \param buf shared buffer.
  373. * \return zero on success or a negative number on failure.
  374. */
  375. static int grant_references(struct xen_front_pgdir_shbuf *buf)
  376. {
  377. grant_ref_t priv_gref_head;
  378. int ret, i, j, cur_ref;
  379. int otherend_id, num_pages_dir;
  380. ret = gnttab_alloc_grant_references(buf->num_grefs, &priv_gref_head);
  381. if (ret < 0) {
  382. dev_err(&buf->xb_dev->dev,
  383. "Cannot allocate grant references\n");
  384. return ret;
  385. }
  386. otherend_id = buf->xb_dev->otherend_id;
  387. j = 0;
  388. num_pages_dir = get_num_pages_dir(buf);
  389. for (i = 0; i < num_pages_dir; i++) {
  390. unsigned long frame;
  391. cur_ref = gnttab_claim_grant_reference(&priv_gref_head);
  392. if (cur_ref < 0)
  393. return cur_ref;
  394. frame = xen_page_to_gfn(virt_to_page(buf->directory +
  395. PAGE_SIZE * i));
  396. gnttab_grant_foreign_access_ref(cur_ref, otherend_id, frame, 0);
  397. buf->grefs[j++] = cur_ref;
  398. }
  399. if (buf->ops->grant_refs_for_buffer) {
  400. ret = buf->ops->grant_refs_for_buffer(buf, &priv_gref_head, j);
  401. if (ret)
  402. return ret;
  403. }
  404. gnttab_free_grant_references(priv_gref_head);
  405. return 0;
  406. }
  407. /**
  408. * Allocate all required structures to mange shared buffer.
  409. *
  410. * \param buf shared buffer.
  411. * \return zero on success or a negative number on failure.
  412. */
  413. static int alloc_storage(struct xen_front_pgdir_shbuf *buf)
  414. {
  415. buf->grefs = kcalloc(buf->num_grefs, sizeof(*buf->grefs), GFP_KERNEL);
  416. if (!buf->grefs)
  417. return -ENOMEM;
  418. buf->directory = kcalloc(get_num_pages_dir(buf), PAGE_SIZE, GFP_KERNEL);
  419. if (!buf->directory)
  420. return -ENOMEM;
  421. return 0;
  422. }
  423. /*
  424. * For backend allocated buffers we don't need grant_refs_for_buffer
  425. * as those grant references are allocated at backend side.
  426. */
  427. static const struct xen_front_pgdir_shbuf_ops backend_ops = {
  428. .calc_num_grefs = backend_calc_num_grefs,
  429. .fill_page_dir = backend_fill_page_dir,
  430. .map = backend_map,
  431. .unmap = backend_unmap
  432. };
  433. /*
  434. * For locally granted references we do not need to map/unmap
  435. * the references.
  436. */
  437. static const struct xen_front_pgdir_shbuf_ops local_ops = {
  438. .calc_num_grefs = guest_calc_num_grefs,
  439. .fill_page_dir = guest_fill_page_dir,
  440. .grant_refs_for_buffer = guest_grant_refs_for_buffer,
  441. };
  442. /**
  443. * Allocate a new instance of a shared buffer.
  444. *
  445. * \param cfg configuration to be used while allocating a new shared buffer.
  446. * \return zero on success or a negative number on failure.
  447. */
  448. int xen_front_pgdir_shbuf_alloc(struct xen_front_pgdir_shbuf_cfg *cfg)
  449. {
  450. struct xen_front_pgdir_shbuf *buf = cfg->pgdir;
  451. int ret;
  452. if (cfg->be_alloc)
  453. buf->ops = &backend_ops;
  454. else
  455. buf->ops = &local_ops;
  456. buf->xb_dev = cfg->xb_dev;
  457. buf->num_pages = cfg->num_pages;
  458. buf->pages = cfg->pages;
  459. buf->ops->calc_num_grefs(buf);
  460. ret = alloc_storage(buf);
  461. if (ret)
  462. goto fail;
  463. ret = grant_references(buf);
  464. if (ret)
  465. goto fail;
  466. buf->ops->fill_page_dir(buf);
  467. return 0;
  468. fail:
  469. xen_front_pgdir_shbuf_free(buf);
  470. return ret;
  471. }
  472. EXPORT_SYMBOL_GPL(xen_front_pgdir_shbuf_alloc);
  473. MODULE_DESCRIPTION("Xen frontend/backend page directory based "
  474. "shared buffer handling");
  475. MODULE_AUTHOR("Oleksandr Andrushchenko");
  476. MODULE_LICENSE("GPL");