1 // SPDX-License-Identifier: GPL-2.0 OR MIT
4 * Xen frontend/backend page directory based shared buffer
7 * Copyright (C) 2018 EPAM Systems Inc.
9 * Author: Oleksandr Andrushchenko <oleksandr_andrushchenko@epam.com>
12 #include <linux/module.h>
13 #include <linux/errno.h>
16 #include <asm/xen/hypervisor.h>
17 #include <xen/balloon.h>
19 #include <xen/xenbus.h>
20 #include <xen/interface/io/ring.h>
22 #include <xen/xen-front-pgdir-shbuf.h>
25 * This structure represents the structure of a shared page
26 * that contains grant references to the pages of the shared
27 * buffer. This structure is common to many Xen para-virtualized
28 * protocols at include/xen/interface/io/
30 struct xen_page_directory
{
31 grant_ref_t gref_dir_next_page
;
32 #define XEN_GREF_LIST_END 0
33 grant_ref_t gref
[]; /* Variable length */
37 * Shared buffer ops which are differently implemented
38 * depending on the allocation mode, e.g. if the buffer
39 * is allocated by the corresponding backend or frontend.
40 * Some of the operations.
42 struct xen_front_pgdir_shbuf_ops
{
44 * Calculate number of grefs required to handle this buffer,
45 * e.g. if grefs are required for page directory only or the buffer
48 void (*calc_num_grefs
)(struct xen_front_pgdir_shbuf
*buf
);
50 /* Fill page directory according to para-virtual display protocol. */
51 void (*fill_page_dir
)(struct xen_front_pgdir_shbuf
*buf
);
53 /* Claim grant references for the pages of the buffer. */
54 int (*grant_refs_for_buffer
)(struct xen_front_pgdir_shbuf
*buf
,
55 grant_ref_t
*priv_gref_head
, int gref_idx
);
57 /* Map grant references of the buffer. */
58 int (*map
)(struct xen_front_pgdir_shbuf
*buf
);
60 /* Unmap grant references of the buffer. */
61 int (*unmap
)(struct xen_front_pgdir_shbuf
*buf
);
65 * Get granted reference to the very first page of the
66 * page directory. Usually this is passed to the backend,
67 * so it can find/fill the grant references to the buffer's
70 * \param buf shared buffer which page directory is of interest.
71 * \return granted reference to the very first page of the
75 xen_front_pgdir_shbuf_get_dir_start(struct xen_front_pgdir_shbuf
*buf
)
78 return INVALID_GRANT_REF
;
82 EXPORT_SYMBOL_GPL(xen_front_pgdir_shbuf_get_dir_start
);
85 * Map granted references of the shared buffer.
87 * Depending on the shared buffer mode of allocation
88 * (be_alloc flag) this can either do nothing (for buffers
89 * shared by the frontend itself) or map the provided granted
90 * references onto the backing storage (buf->pages).
92 * \param buf shared buffer which grants to be mapped.
93 * \return zero on success or a negative number on failure.
95 int xen_front_pgdir_shbuf_map(struct xen_front_pgdir_shbuf
*buf
)
97 if (buf
->ops
&& buf
->ops
->map
)
98 return buf
->ops
->map(buf
);
100 /* No need to map own grant references. */
103 EXPORT_SYMBOL_GPL(xen_front_pgdir_shbuf_map
);
106 * Unmap granted references of the shared buffer.
108 * Depending on the shared buffer mode of allocation
109 * (be_alloc flag) this can either do nothing (for buffers
110 * shared by the frontend itself) or unmap the provided granted
113 * \param buf shared buffer which grants to be unmapped.
114 * \return zero on success or a negative number on failure.
116 int xen_front_pgdir_shbuf_unmap(struct xen_front_pgdir_shbuf
*buf
)
118 if (buf
->ops
&& buf
->ops
->unmap
)
119 return buf
->ops
->unmap(buf
);
121 /* No need to unmap own grant references. */
124 EXPORT_SYMBOL_GPL(xen_front_pgdir_shbuf_unmap
);
127 * Free all the resources of the shared buffer.
129 * \param buf shared buffer which resources to be freed.
131 void xen_front_pgdir_shbuf_free(struct xen_front_pgdir_shbuf
*buf
)
136 for (i
= 0; i
< buf
->num_grefs
; i
++)
137 if (buf
->grefs
[i
] != INVALID_GRANT_REF
)
138 gnttab_end_foreign_access(buf
->grefs
[i
], NULL
);
141 kfree(buf
->directory
);
143 EXPORT_SYMBOL_GPL(xen_front_pgdir_shbuf_free
);
146 * Number of grefs a page can hold with respect to the
147 * struct xen_page_directory header.
149 #define XEN_NUM_GREFS_PER_PAGE ((PAGE_SIZE - \
150 offsetof(struct xen_page_directory, \
151 gref)) / sizeof(grant_ref_t))
154 * Get the number of pages the page directory consumes itself.
156 * \param buf shared buffer.
158 static int get_num_pages_dir(struct xen_front_pgdir_shbuf
*buf
)
160 return DIV_ROUND_UP(buf
->num_pages
, XEN_NUM_GREFS_PER_PAGE
);
164 * Calculate the number of grant references needed to share the buffer
165 * and its pages when backend allocates the buffer.
167 * \param buf shared buffer.
169 static void backend_calc_num_grefs(struct xen_front_pgdir_shbuf
*buf
)
171 /* Only for pages the page directory consumes itself. */
172 buf
->num_grefs
= get_num_pages_dir(buf
);
176 * Calculate the number of grant references needed to share the buffer
177 * and its pages when frontend allocates the buffer.
179 * \param buf shared buffer.
181 static void guest_calc_num_grefs(struct xen_front_pgdir_shbuf
*buf
)
184 * Number of pages the page directory consumes itself
185 * plus grefs for the buffer pages.
187 buf
->num_grefs
= get_num_pages_dir(buf
) + buf
->num_pages
;
190 #define xen_page_to_vaddr(page) \
191 ((uintptr_t)pfn_to_kaddr(page_to_xen_pfn(page)))
194 * Unmap the buffer previously mapped with grant references
195 * provided by the backend.
197 * \param buf shared buffer.
198 * \return zero on success or a negative number on failure.
200 static int backend_unmap(struct xen_front_pgdir_shbuf
*buf
)
202 struct gnttab_unmap_grant_ref
*unmap_ops
;
205 if (!buf
->pages
|| !buf
->backend_map_handles
|| !buf
->grefs
)
208 unmap_ops
= kcalloc(buf
->num_pages
, sizeof(*unmap_ops
),
213 for (i
= 0; i
< buf
->num_pages
; i
++) {
216 addr
= xen_page_to_vaddr(buf
->pages
[i
]);
217 gnttab_set_unmap_op(&unmap_ops
[i
], addr
, GNTMAP_host_map
,
218 buf
->backend_map_handles
[i
]);
221 ret
= gnttab_unmap_refs(unmap_ops
, NULL
, buf
->pages
,
224 for (i
= 0; i
< buf
->num_pages
; i
++) {
225 if (unlikely(unmap_ops
[i
].status
!= GNTST_okay
))
226 dev_err(&buf
->xb_dev
->dev
,
227 "Failed to unmap page %d: %d\n",
228 i
, unmap_ops
[i
].status
);
232 dev_err(&buf
->xb_dev
->dev
,
233 "Failed to unmap grant references, ret %d", ret
);
236 kfree(buf
->backend_map_handles
);
237 buf
->backend_map_handles
= NULL
;
242 * Map the buffer with grant references provided by the backend.
244 * \param buf shared buffer.
245 * \return zero on success or a negative number on failure.
247 static int backend_map(struct xen_front_pgdir_shbuf
*buf
)
249 struct gnttab_map_grant_ref
*map_ops
= NULL
;
251 int ret
, cur_gref
, cur_dir_page
, cur_page
, grefs_left
;
253 map_ops
= kcalloc(buf
->num_pages
, sizeof(*map_ops
), GFP_KERNEL
);
257 buf
->backend_map_handles
= kcalloc(buf
->num_pages
,
258 sizeof(*buf
->backend_map_handles
),
260 if (!buf
->backend_map_handles
) {
266 * Read page directory to get grefs from the backend: for external
267 * buffer we only allocate buf->grefs for the page directory,
268 * so buf->num_grefs has number of pages in the page directory itself.
270 ptr
= buf
->directory
;
271 grefs_left
= buf
->num_pages
;
273 for (cur_dir_page
= 0; cur_dir_page
< buf
->num_grefs
; cur_dir_page
++) {
274 struct xen_page_directory
*page_dir
=
275 (struct xen_page_directory
*)ptr
;
276 int to_copy
= XEN_NUM_GREFS_PER_PAGE
;
278 if (to_copy
> grefs_left
)
279 to_copy
= grefs_left
;
281 for (cur_gref
= 0; cur_gref
< to_copy
; cur_gref
++) {
284 addr
= xen_page_to_vaddr(buf
->pages
[cur_page
]);
285 gnttab_set_map_op(&map_ops
[cur_page
], addr
,
287 page_dir
->gref
[cur_gref
],
288 buf
->xb_dev
->otherend_id
);
292 grefs_left
-= to_copy
;
295 ret
= gnttab_map_refs(map_ops
, NULL
, buf
->pages
, buf
->num_pages
);
297 /* Save handles even if error, so we can unmap. */
298 for (cur_page
= 0; cur_page
< buf
->num_pages
; cur_page
++) {
299 if (likely(map_ops
[cur_page
].status
== GNTST_okay
)) {
300 buf
->backend_map_handles
[cur_page
] =
301 map_ops
[cur_page
].handle
;
303 buf
->backend_map_handles
[cur_page
] =
304 INVALID_GRANT_HANDLE
;
307 dev_err(&buf
->xb_dev
->dev
,
308 "Failed to map page %d: %d\n",
309 cur_page
, map_ops
[cur_page
].status
);
314 dev_err(&buf
->xb_dev
->dev
,
315 "Failed to map grant references, ret %d", ret
);
324 * Fill page directory with grant references to the pages of the
325 * page directory itself.
327 * The grant references to the buffer pages are provided by the
328 * backend in this case.
330 * \param buf shared buffer.
332 static void backend_fill_page_dir(struct xen_front_pgdir_shbuf
*buf
)
334 struct xen_page_directory
*page_dir
;
336 int i
, num_pages_dir
;
338 ptr
= buf
->directory
;
339 num_pages_dir
= get_num_pages_dir(buf
);
341 /* Fill only grefs for the page directory itself. */
342 for (i
= 0; i
< num_pages_dir
- 1; i
++) {
343 page_dir
= (struct xen_page_directory
*)ptr
;
345 page_dir
->gref_dir_next_page
= buf
->grefs
[i
+ 1];
348 /* Last page must say there is no more pages. */
349 page_dir
= (struct xen_page_directory
*)ptr
;
350 page_dir
->gref_dir_next_page
= XEN_GREF_LIST_END
;
354 * Fill page directory with grant references to the pages of the
355 * page directory and the buffer we share with the backend.
357 * \param buf shared buffer.
359 static void guest_fill_page_dir(struct xen_front_pgdir_shbuf
*buf
)
362 int cur_gref
, grefs_left
, to_copy
, i
, num_pages_dir
;
364 ptr
= buf
->directory
;
365 num_pages_dir
= get_num_pages_dir(buf
);
368 * While copying, skip grefs at start, they are for pages
369 * granted for the page directory itself.
371 cur_gref
= num_pages_dir
;
372 grefs_left
= buf
->num_pages
;
373 for (i
= 0; i
< num_pages_dir
; i
++) {
374 struct xen_page_directory
*page_dir
=
375 (struct xen_page_directory
*)ptr
;
377 if (grefs_left
<= XEN_NUM_GREFS_PER_PAGE
) {
378 to_copy
= grefs_left
;
379 page_dir
->gref_dir_next_page
= XEN_GREF_LIST_END
;
381 to_copy
= XEN_NUM_GREFS_PER_PAGE
;
382 page_dir
->gref_dir_next_page
= buf
->grefs
[i
+ 1];
384 memcpy(&page_dir
->gref
, &buf
->grefs
[cur_gref
],
385 to_copy
* sizeof(grant_ref_t
));
387 grefs_left
-= to_copy
;
393 * Grant references to the frontend's buffer pages.
395 * These will be shared with the backend, so it can
396 * access the buffer's data.
398 * \param buf shared buffer.
399 * \return zero on success or a negative number on failure.
401 static int guest_grant_refs_for_buffer(struct xen_front_pgdir_shbuf
*buf
,
402 grant_ref_t
*priv_gref_head
,
405 int i
, cur_ref
, otherend_id
;
407 otherend_id
= buf
->xb_dev
->otherend_id
;
408 for (i
= 0; i
< buf
->num_pages
; i
++) {
409 cur_ref
= gnttab_claim_grant_reference(priv_gref_head
);
413 gnttab_grant_foreign_access_ref(cur_ref
, otherend_id
,
414 xen_page_to_gfn(buf
->pages
[i
]),
416 buf
->grefs
[gref_idx
++] = cur_ref
;
422 * Grant all the references needed to share the buffer.
424 * Grant references to the page directory pages and, if
425 * needed, also to the pages of the shared buffer data.
427 * \param buf shared buffer.
428 * \return zero on success or a negative number on failure.
430 static int grant_references(struct xen_front_pgdir_shbuf
*buf
)
432 grant_ref_t priv_gref_head
;
433 int ret
, i
, j
, cur_ref
;
434 int otherend_id
, num_pages_dir
;
436 ret
= gnttab_alloc_grant_references(buf
->num_grefs
, &priv_gref_head
);
438 dev_err(&buf
->xb_dev
->dev
,
439 "Cannot allocate grant references\n");
443 otherend_id
= buf
->xb_dev
->otherend_id
;
445 num_pages_dir
= get_num_pages_dir(buf
);
446 for (i
= 0; i
< num_pages_dir
; i
++) {
449 cur_ref
= gnttab_claim_grant_reference(&priv_gref_head
);
453 frame
= xen_page_to_gfn(virt_to_page(buf
->directory
+
455 gnttab_grant_foreign_access_ref(cur_ref
, otherend_id
, frame
, 0);
456 buf
->grefs
[j
++] = cur_ref
;
459 if (buf
->ops
->grant_refs_for_buffer
) {
460 ret
= buf
->ops
->grant_refs_for_buffer(buf
, &priv_gref_head
, j
);
465 gnttab_free_grant_references(priv_gref_head
);
470 * Allocate all required structures to mange shared buffer.
472 * \param buf shared buffer.
473 * \return zero on success or a negative number on failure.
475 static int alloc_storage(struct xen_front_pgdir_shbuf
*buf
)
477 buf
->grefs
= kcalloc(buf
->num_grefs
, sizeof(*buf
->grefs
), GFP_KERNEL
);
481 buf
->directory
= kcalloc(get_num_pages_dir(buf
), PAGE_SIZE
, GFP_KERNEL
);
489 * For backend allocated buffers we don't need grant_refs_for_buffer
490 * as those grant references are allocated at backend side.
492 static const struct xen_front_pgdir_shbuf_ops backend_ops
= {
493 .calc_num_grefs
= backend_calc_num_grefs
,
494 .fill_page_dir
= backend_fill_page_dir
,
496 .unmap
= backend_unmap
500 * For locally granted references we do not need to map/unmap
503 static const struct xen_front_pgdir_shbuf_ops local_ops
= {
504 .calc_num_grefs
= guest_calc_num_grefs
,
505 .fill_page_dir
= guest_fill_page_dir
,
506 .grant_refs_for_buffer
= guest_grant_refs_for_buffer
,
510 * Allocate a new instance of a shared buffer.
512 * \param cfg configuration to be used while allocating a new shared buffer.
513 * \return zero on success or a negative number on failure.
515 int xen_front_pgdir_shbuf_alloc(struct xen_front_pgdir_shbuf_cfg
*cfg
)
517 struct xen_front_pgdir_shbuf
*buf
= cfg
->pgdir
;
521 buf
->ops
= &backend_ops
;
523 buf
->ops
= &local_ops
;
524 buf
->xb_dev
= cfg
->xb_dev
;
525 buf
->num_pages
= cfg
->num_pages
;
526 buf
->pages
= cfg
->pages
;
528 buf
->ops
->calc_num_grefs(buf
);
530 ret
= alloc_storage(buf
);
534 ret
= grant_references(buf
);
538 buf
->ops
->fill_page_dir(buf
);
543 xen_front_pgdir_shbuf_free(buf
);
546 EXPORT_SYMBOL_GPL(xen_front_pgdir_shbuf_alloc
);
548 MODULE_DESCRIPTION("Xen frontend/backend page directory based "
549 "shared buffer handling");
550 MODULE_AUTHOR("Oleksandr Andrushchenko");
551 MODULE_LICENSE("GPL");