From: Juergen Gross <jgross@suse.com>
To: xen-devel@lists.xenproject.org, linux-kernel@vger.kernel.org
Cc: Juergen Gross <jgross@suse.com>,
Boris Ostrovsky <boris.ostrovsky@oracle.com>,
Stefano Stabellini <sstabellini@kernel.org>
Subject: [PATCH v3 12/21] xen/xenbus: add xenbus_setup_ring() service function
Date: Thu, 5 May 2022 10:16:31 +0200 [thread overview]
Message-ID: <20220505081640.17425-13-jgross@suse.com> (raw)
In-Reply-To: <20220505081640.17425-1-jgross@suse.com>
Most PV device frontends share very similar code for setting up shared
ring buffers:
- allocate page(s)
- init the ring admin data
- give the backend access to the ring via grants
Tearing down the ring requires similar actions in all frontends again:
- remove grants
- free the page(s)
Provide service functions xenbus_setup_ring() and xenbus_teardown_ring()
for that purpose.
Signed-off-by: Juergen Gross <jgross@suse.com>
---
drivers/xen/xenbus/xenbus_client.c | 69 ++++++++++++++++++++++++++++++
include/xen/xenbus.h | 4 ++
2 files changed, 73 insertions(+)
diff --git a/drivers/xen/xenbus/xenbus_client.c b/drivers/xen/xenbus/xenbus_client.c
index df6890681231..1a2e0d94ccd1 100644
--- a/drivers/xen/xenbus/xenbus_client.c
+++ b/drivers/xen/xenbus/xenbus_client.c
@@ -407,6 +407,75 @@ int xenbus_grant_ring(struct xenbus_device *dev, void *vaddr,
}
EXPORT_SYMBOL_GPL(xenbus_grant_ring);
+/*
+ * xenbus_setup_ring
+ * @dev: xenbus device
+ * @vaddr: pointer to starting virtual address of the ring
+ * @nr_pages: number of pages to be granted
+ * @grefs: grant reference array to be filled in
+ *
+ * Allocate physically contiguous pages for a shared ring buffer and grant it
+ * to the peer of the given device. The ring buffer is initially filled with
+ * zeroes. The virtual address of the ring is stored at @vaddr and the
+ * grant references are stored in the @grefs array. In case of error @vaddr
+ * will be set to NULL and @grefs will be filled with INVALID_GRANT_REF.
+ */
+int xenbus_setup_ring(struct xenbus_device *dev, gfp_t gfp, void **vaddr,
+ unsigned int nr_pages, grant_ref_t *grefs)
+{
+ unsigned long ring_size = nr_pages * XEN_PAGE_SIZE;
+ unsigned int i;
+ int ret;
+
+ *vaddr = alloc_pages_exact(ring_size, gfp | __GFP_ZERO);
+ if (!*vaddr) {
+ ret = -ENOMEM;
+ goto err;
+ }
+
+ ret = xenbus_grant_ring(dev, *vaddr, nr_pages, grefs);
+ if (ret)
+ goto err;
+
+ return 0;
+
+ err:
+ if (*vaddr)
+ free_pages_exact(*vaddr, ring_size);
+ for (i = 0; i < nr_pages; i++)
+ grefs[i] = INVALID_GRANT_REF;
+ *vaddr = NULL;
+
+ return ret;
+}
+EXPORT_SYMBOL_GPL(xenbus_setup_ring);
+
+/*
+ * xenbus_teardown_ring
+ * @vaddr: starting virtual address of the ring
+ * @nr_pages: number of pages
+ * @grefs: grant reference array
+ *
+ * Remove grants for the shared ring buffer and free the associated memory.
+ * On return the grant reference array is filled with INVALID_GRANT_REF.
+ */
+void xenbus_teardown_ring(void **vaddr, unsigned int nr_pages,
+ grant_ref_t *grefs)
+{
+ unsigned int i;
+
+ for (i = 0; i < nr_pages; i++) {
+ if (grefs[i] != INVALID_GRANT_REF) {
+ gnttab_end_foreign_access(grefs[i], 0);
+ grefs[i] = INVALID_GRANT_REF;
+ }
+ }
+
+ if (*vaddr)
+ free_pages_exact(*vaddr, nr_pages * XEN_PAGE_SIZE);
+ *vaddr = NULL;
+}
+EXPORT_SYMBOL_GPL(xenbus_teardown_ring);
/**
* Allocate an event channel for the given xenbus_device, assigning the newly
diff --git a/include/xen/xenbus.h b/include/xen/xenbus.h
index b13eb86395e0..b533b4adc835 100644
--- a/include/xen/xenbus.h
+++ b/include/xen/xenbus.h
@@ -226,6 +226,10 @@ int xenbus_watch_pathfmt(struct xenbus_device *dev, struct xenbus_watch *watch,
int xenbus_switch_state(struct xenbus_device *dev, enum xenbus_state new_state);
int xenbus_grant_ring(struct xenbus_device *dev, void *vaddr,
unsigned int nr_pages, grant_ref_t *grefs);
+int xenbus_setup_ring(struct xenbus_device *dev, gfp_t gfp, void **vaddr,
+ unsigned int nr_pages, grant_ref_t *grefs);
+void xenbus_teardown_ring(void **vaddr, unsigned int nr_pages,
+ grant_ref_t *grefs);
int xenbus_map_ring_valloc(struct xenbus_device *dev, grant_ref_t *gnt_refs,
unsigned int nr_grefs, void **vaddr);
--
2.35.3
next prev parent reply other threads:[~2022-05-05 8:18 UTC|newest]
Thread overview: 30+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-05-05 8:16 [PATCH v3 00/21] xen: simplify frontend side ring setup Juergen Gross
2022-05-05 8:16 ` [PATCH v3 01/21] xen: update grant_table.h Juergen Gross
2022-05-05 8:16 ` [PATCH v3 02/21] xen/grant-table: never put a reserved grant on the free list Juergen Gross
2022-05-05 8:16 ` [PATCH v3 03/21] xen/blkfront: switch blkfront to use INVALID_GRANT_REF Juergen Gross
2022-05-09 8:20 ` Roger Pau Monné
2022-05-05 8:16 ` [PATCH v3 04/21] xen/netfront: switch netfront " Juergen Gross
2022-05-05 8:16 ` [PATCH v3 05/21] xen/scsifront: remove unused GRANT_INVALID_REF definition Juergen Gross
2022-05-05 8:16 ` [PATCH v3 06/21] xen/usb: switch xen-hcd to use INVALID_GRANT_REF Juergen Gross
2022-05-05 8:16 ` [PATCH v3 07/21] xen/drm: switch xen_drm_front " Juergen Gross
2022-05-05 8:16 ` [PATCH v3 08/21] xen/sound: switch xen_snd_front " Juergen Gross
2022-05-05 8:16 ` [PATCH v3 09/21] xen/dmabuf: switch gntdev-dmabuf " Juergen Gross
2022-05-05 8:16 ` [PATCH v3 10/21] xen/shbuf: switch xen-front-pgdir-shbuf " Juergen Gross
2022-05-09 8:47 ` Oleksandr
2022-05-05 8:16 ` [PATCH v3 11/21] xen: update ring.h Juergen Gross
2022-05-05 8:16 ` Juergen Gross [this message]
2022-05-05 8:16 ` [PATCH v3 13/21] xen/blkfront: use xenbus_setup_ring() and xenbus_teardown_ring() Juergen Gross
2022-05-09 8:42 ` Roger Pau Monné
2022-05-05 8:16 ` [PATCH v3 14/21] xen/netfront: " Juergen Gross
2022-05-05 8:16 ` [PATCH v3 15/21] xen/tpmfront: " Juergen Gross
2022-05-06 22:32 ` Jarkko Sakkinen
2022-05-18 9:41 ` Juergen Gross
2022-05-18 14:57 ` Jarkko Sakkinen
2022-05-05 8:16 ` [PATCH v3 16/21] xen/drmfront: " Juergen Gross
2022-05-05 8:16 ` [PATCH v3 17/21] xen/pcifront: " Juergen Gross
2022-05-05 8:16 ` [PATCH v3 18/21] xen/scsifront: " Juergen Gross
2022-05-05 8:16 ` [PATCH v3 19/21] xen/usbfront: " Juergen Gross
2022-05-05 8:16 ` [PATCH v3 20/21] xen/sndfront: " Juergen Gross
2022-05-05 8:16 ` [PATCH v3 21/21] xen/xenbus: eliminate xenbus_grant_ring() Juergen Gross
2022-05-06 0:12 ` [PATCH v3 00/21] xen: simplify frontend side ring setup Boris Ostrovsky
2022-05-09 13:23 ` Oleksandr
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20220505081640.17425-13-jgross@suse.com \
--to=jgross@suse.com \
--cc=boris.ostrovsky@oracle.com \
--cc=linux-kernel@vger.kernel.org \
--cc=sstabellini@kernel.org \
--cc=xen-devel@lists.xenproject.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox