From: "Eugenio Pérez" <eperezma@redhat.com>
To: linux-kernel@vger.kernel.org, mst@redhat.com,
Hanna Reitz <hreitz@redhat.com>,
Stefano Garzarella <sgarzare@redhat.com>,
Xuan Zhuo <xuanzhuo@linux.alibaba.com>,
Jason Wang <jasowang@redhat.com>, Vivek Goyal <vgoyal@redhat.com>,
Miklos Szeredi <miklos@szeredi.hu>,
German Maglione <gmaglione@redhat.com>,
Stefan Hajnoczi <stefanha@redhat.com>,
virtualization@lists.linux.dev
Subject: [RFC 2/5] virtio_ring: introduce virtqueue_map/unmap_sgs()
Date: Wed, 22 Jan 2025 17:31:51 +0100 [thread overview]
Message-ID: <20250122163154.1225353-3-eperezma@redhat.com> (raw)
In-Reply-To: <20250122163154.1225353-1-eperezma@redhat.com>
From: Jason Wang <jasowang@redhat.com>
Introduce new virtqueue DMA operations which allows the drivers that
want to make use of the premapping API but operate at the sg level.
Note that we still follow the assumtions if virtqueue_add() so
dma_map_sg() is not used. This could be optimized in the future.
Signed-off-by: Jason Wang <jasowang@redhat.com>
Signed-off-by: Eugenio Pérez <eperezma@redhat.com>
--
Eugenio's changes: Remove blank
TODO: Should we call directly dma_map instead of this? XDP do the direct
call.
---
drivers/virtio/virtio_ring.c | 128 +++++++++++++++++++++++++++++++----
include/linux/virtio.h | 10 +++
2 files changed, 125 insertions(+), 13 deletions(-)
diff --git a/drivers/virtio/virtio_ring.c b/drivers/virtio/virtio_ring.c
index fdd2d2b07b5a..05729bc5cbb1 100644
--- a/drivers/virtio/virtio_ring.c
+++ b/drivers/virtio/virtio_ring.c
@@ -359,6 +359,26 @@ static struct device *vring_dma_dev(const struct vring_virtqueue *vq)
return vq->dma_dev;
}
+static int __vring_map_one_sg(const struct vring_virtqueue *vq,
+ struct scatterlist *sg,
+ enum dma_data_direction direction,
+ dma_addr_t *addr)
+{
+ /*
+ * We can't use dma_map_sg, because we don't use scatterlists in
+ * the way it expects (we don't guarantee that the scatterlist
+ * will exist for the lifetime of the mapping).
+ */
+ *addr = dma_map_page(vring_dma_dev(vq),
+ sg_page(sg), sg->offset, sg->length,
+ direction);
+
+ if (dma_mapping_error(vring_dma_dev(vq), *addr))
+ return -ENOMEM;
+
+ return 0;
+}
+
/* Map one sg entry. */
static int vring_map_one_sg(const struct vring_virtqueue *vq, struct scatterlist *sg,
enum dma_data_direction direction, dma_addr_t *addr,
@@ -383,19 +403,7 @@ static int vring_map_one_sg(const struct vring_virtqueue *vq, struct scatterlist
return 0;
}
- /*
- * We can't use dma_map_sg, because we don't use scatterlists in
- * the way it expects (we don't guarantee that the scatterlist
- * will exist for the lifetime of the mapping).
- */
- *addr = dma_map_page(vring_dma_dev(vq),
- sg_page(sg), sg->offset, sg->length,
- direction);
-
- if (dma_mapping_error(vring_dma_dev(vq), *addr))
- return -ENOMEM;
-
- return 0;
+ return __vring_map_one_sg(vq, sg, direction, addr);
}
static dma_addr_t vring_map_single(const struct vring_virtqueue *vq,
@@ -526,6 +534,100 @@ static inline unsigned int virtqueue_add_desc_split(struct virtqueue *vq,
return next;
}
+void virtqueue_unmap_sgs(struct virtqueue *_vq,
+ struct scatterlist *sgs[],
+ unsigned int out_sgs,
+ unsigned int in_sgs)
+{
+ struct vring_virtqueue *vq = to_vvq(_vq);
+ struct scatterlist *sg;
+ int n;
+
+ for (n = 0; n < out_sgs; n++) {
+ for (sg = sgs[n]; sg; sg = sg_next(sg)) {
+ dma_unmap_page(vring_dma_dev(vq),
+ sg_dma_address(sg),
+ sg->length,
+ DMA_TO_DEVICE);
+ }
+ }
+
+ for (; n < (out_sgs + in_sgs); n++) {
+ for (sg = sgs[n]; sg; sg = sg_next(sg)) {
+ dma_unmap_page(vring_dma_dev(vq),
+ sg_dma_address(sg),
+ sg->length,
+ DMA_FROM_DEVICE);
+ }
+ }
+}
+EXPORT_SYMBOL_GPL(virtqueue_unmap_sgs);
+
+int virtqueue_map_sgs(struct virtqueue *_vq,
+ struct scatterlist *sgs[],
+ unsigned int out_sgs,
+ unsigned int in_sgs)
+{
+ struct vring_virtqueue *vq = to_vvq(_vq);
+ int i, n, mapped_sg = 0;
+ struct scatterlist *sg;
+
+ for (n = 0; n < out_sgs; n++) {
+ for (sg = sgs[n]; sg; sg = sg_next(sg)) {
+ dma_addr_t addr;
+
+ if (__vring_map_one_sg(vq, sg, DMA_TO_DEVICE, &addr))
+ goto unmap_release;
+
+ sg_dma_address(sg) = addr;
+ mapped_sg++;
+ }
+ }
+
+ for (; n < (out_sgs + in_sgs); n++) {
+ for (sg = sgs[n]; sg; sg = sg_next(sg)) {
+ dma_addr_t addr;
+
+ if (__vring_map_one_sg(vq, sg, DMA_FROM_DEVICE, &addr))
+ goto unmap_release;
+
+ sg_dma_address(sg) = addr;
+ mapped_sg++;
+ }
+ }
+
+ return 0;
+
+unmap_release:
+ i = 0;
+
+ for (n = 0; n < out_sgs; n++) {
+ for (sg = sgs[n]; sg; sg = sg_next(sg)) {
+ if (i++ == mapped_sg)
+ goto out;
+ dma_unmap_page(vring_dma_dev(vq),
+ sg_dma_address(sg),
+ sg->length,
+ DMA_TO_DEVICE);
+ }
+ }
+
+ for (; n < (out_sgs + in_sgs); n++) {
+ for (sg = sgs[n]; sg; sg = sg_next(sg)) {
+
+ if (i++ == mapped_sg)
+ goto out;
+ dma_unmap_page(vring_dma_dev(vq),
+ sg_dma_address(sg),
+ sg->length,
+ DMA_FROM_DEVICE);
+ }
+ }
+out:
+ return -ENOMEM;
+}
+EXPORT_SYMBOL_GPL(virtqueue_map_sgs);
+
static inline int virtqueue_add_split(struct virtqueue *_vq,
struct scatterlist *sgs[],
unsigned int total_sg,
diff --git a/include/linux/virtio.h b/include/linux/virtio.h
index dd88682e27e3..28db998d691e 100644
--- a/include/linux/virtio.h
+++ b/include/linux/virtio.h
@@ -67,6 +67,16 @@ int virtqueue_add_outbuf_premapped(struct virtqueue *vq,
void *data,
gfp_t gfp);
+int virtqueue_map_sgs(struct virtqueue *_vq,
+ struct scatterlist *sgs[],
+ unsigned int out_sgs,
+ unsigned int in_sgs);
+
+void virtqueue_unmap_sgs(struct virtqueue *_vq,
+ struct scatterlist *sgs[],
+ unsigned int out_sgs,
+ unsigned int in_sgs);
+
int virtqueue_add_sgs(struct virtqueue *vq,
struct scatterlist *sgs[],
unsigned int out_sgs,
--
2.48.1
next prev parent reply other threads:[~2025-01-22 16:32 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-01-22 16:31 [RFC 0/5] virtiofs: map buffer out of virtqueue lock Eugenio Pérez
2025-01-22 16:31 ` [RFC 1/5] vduse: add virtio_fs to allowed dev id Eugenio Pérez
2025-01-23 1:48 ` Jason Wang
2025-01-22 16:31 ` Eugenio Pérez [this message]
2025-01-23 1:51 ` [RFC 2/5] virtio_ring: introduce virtqueue_map/unmap_sgs() Jason Wang
2025-01-23 7:31 ` Eugenio Perez Martin
2025-01-24 1:03 ` Jason Wang
2025-01-22 16:31 ` [RFC 3/5] virtiofs: Move stack sg to fuse_req Eugenio Pérez
2025-01-23 1:54 ` Jason Wang
2025-01-23 7:33 ` Eugenio Perez Martin
2025-01-22 16:31 ` [RFC 4/5] virtio_ring: add virtqueue premapped out/in buffers support Eugenio Pérez
2025-01-22 16:31 ` [RFC 5/5] virtiofs: perform DMA operations out of the spinlock Eugenio Pérez
2025-01-23 1:56 ` Jason Wang
2025-01-23 2:26 ` Jason Wang
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=20250122163154.1225353-3-eperezma@redhat.com \
--to=eperezma@redhat.com \
--cc=gmaglione@redhat.com \
--cc=hreitz@redhat.com \
--cc=jasowang@redhat.com \
--cc=linux-kernel@vger.kernel.org \
--cc=miklos@szeredi.hu \
--cc=mst@redhat.com \
--cc=sgarzare@redhat.com \
--cc=stefanha@redhat.com \
--cc=vgoyal@redhat.com \
--cc=virtualization@lists.linux.dev \
--cc=xuanzhuo@linux.alibaba.com \
/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
all inboxes | Powered by JetHome®