Skip to contentSkip to conversation

virtiofs: map buffer out of virtqueue lock

RFC In review Eugenio Pérez 2 participants 12 messages Feb 21, 2025 – Feb 24, 2025

Subsystems (3)
lore
Share & tools
lore

Eugenio Pérez

author maintainer
#0 lore raw

v2 cover virtiofs: map buffer out of virtqueue lock

This is useful for some setups like swiotlb or VDUSE where the DMA operations are expensive and/or need to be performed with a write lock.

After applying these patches, fio read test goes from 1124MiB/s to 1263.14MiB/s.

v2:
* Follow current add_premapped virtio API
* Disable notification more aggressive too.

---
Sending this series to obtain feedback if this is the right way to go & to profile it properly.

TODO: Profile multiqueue.
TODO: Handling errors.
TODO: Do the same for hiprio queue.
TODO: Can we batch maps? virtiofs always sends many buffers.

Eugenio Pérez (5):
vduse: add virtio_fs to allowed dev id
virtiofs: Move stack sg to fuse_req
virtio_ring: add api for premapped out and in buffer chain
virtiofs: perform DMA operations out of the spinlock
virtiofs: Disable notifications more aggresively

 drivers/vdpa/vdpa_user/vduse_dev.c |  1 +
 drivers/virtio/virtio_ring.c       | 35 ++++++++++++
 fs/fuse/fuse_i.h                   |  7 +++
 fs/fuse/virtio_fs.c                | 85 +++++++++++++++++++++---------
 include/linux/virtio.h             |  7 +++
 5 files changed, 110 insertions(+), 25 deletions(-)
Signature · 1 line
2.48.1
author maintainer
#1 lore raw

RFC v2 1/5 vduse: add virtio_fs to allowed dev id

A VDUSE device that implements virtiofs device works fine just by adding the device id to the whitelist.

---
 drivers/vdpa/vdpa_user/vduse_dev.c | 1 +
 1 file changed, 1 insertion(+)
drivers/vdpa/vdpa_user/vduse_dev.c +1 -0
index 7ae99691efdf..6a9a37351310 100644
@@ -144,6 +144,7 @@ static struct workqueue_struct *vduse_irq_bound_wq;
 static u32 allowed_device_id[] = {
 	VIRTIO_ID_BLOCK,
 	VIRTIO_ID_NET,
+	VIRTIO_ID_FS,
 };
 
 static inline struct vduse_dev *vdpa_to_vduse(struct vdpa_device *vdpa)
Signature · 1 line
2.48.1
author maintainer
#4 lore raw

RFC v2 2/5 virtiofs: Move stack sg to fuse_req

In reply to Eugenio Pérez

We need to move the map and unmap out of virtiofs queue lock. We need to store the unmap sgs to call virtqueue_unmap_sgs, so use the request itself.

TODO: We need to store the forget sgs too. In my tests it is not called so we're safe. Find a way to force-call it.

---
 fs/fuse/fuse_i.h    |  7 +++++++
 fs/fuse/virtio_fs.c | 45 +++++++++++++++++++++------------------------
 2 files changed, 28 insertions(+), 24 deletions(-)
fs/fuse/fuse_i.h +7 -0
index fee96fe7887b..0a77e28d8da6 100644
@@ -18,6 +18,7 @@
 #include <linux/mount.h>
 #include <linux/wait.h>
 #include <linux/list.h>
+#include <linux/scatterlist.h>
 #include <linux/spinlock.h>
 #include <linux/mm.h>
 #include <linux/backing-dev.h>
@@ -434,6 +435,12 @@ struct fuse_req {
 #if IS_ENABLED(CONFIG_VIRTIO_FS)
 	/** virtio-fs's physically contiguous buffer for in and out args */
 	void *argbuf;
+
+	/** virtio-fs's pre-mapped stuff */
+	struct scatterlist sg_inline_data[6]; /* optimization for short requests */
+	struct scatterlist *sg;
+	unsigned int out_sgs;
+	unsigned int in_sgs;
 #endif
 
 	/** fuse_mount this request belongs to */
fs/fuse/virtio_fs.c +21 -24

89 lines not on this page · raw message

Signature · 1 line
2.48.1
author maintainer
#5 lore raw

RFC v2 3/5 virtio_ring: add api for premapped out and in buffer chain

In reply to Eugenio Pérez

Commit 3ef66af31fea ("virtio_ring: introduce add api for premapped") add functions to add premapped input or output chains to a virtqueue. Add a generic one that supports out+in buffers chains.

---
 drivers/virtio/virtio_ring.c | 35 +++++++++++++++++++++++++++++++++++
 include/linux/virtio.h       |  7 +++++++
 2 files changed, 42 insertions(+)
drivers/virtio/virtio_ring.c +35 -0
index fdd2d2b07b5a..7ef1f37e025f 100644
@@ -2318,6 +2318,41 @@ int virtqueue_add_sgs(struct virtqueue *_vq,
 }
 EXPORT_SYMBOL_GPL(virtqueue_add_sgs);
 
+/**
+ * virtqueue_add_sgs_premapped - expose buffers to other end
+ * @_vq: the struct virtqueue we're talking about.
+ * @sgs: array of terminated scatterlists.
+ * @out_sgs: the number of scatterlists readable by other side
+ * @in_sgs: the number of scatterlists which are writable (after readable ones)
+ * @data: the token identifying the buffer.
+ * @gfp: how to do memory allocations (if necessary).
+ *
+ * Caller must ensure we don't call this with other virtqueue operations
+ * at the same time (except where noted).
+ *
+ * Returns zero or a negative error (ie. ENOSPC, ENOMEM, EIO).
+ */
+int virtqueue_add_sgs_premapped(struct virtqueue *_vq,
+				struct scatterlist *sgs[],
+				unsigned int out_sgs,
+				unsigned int in_sgs,
+				void *data,
+				gfp_t gfp)
+{
+	unsigned int i, total_sg = 0;
+
+	/* Count them first. */
+	for (i = 0; i < out_sgs + in_sgs; i++) {
+		struct scatterlist *sg;
+
+		for (sg = sgs[i]; sg; sg = sg_next(sg))
+			total_sg++;
+	}
+	return virtqueue_add(_vq, sgs, total_sg, out_sgs, in_sgs,
+			     data, NULL, true, gfp);
+}
+EXPORT_SYMBOL_GPL(virtqueue_add_sgs_premapped);
+
 /**
  * virtqueue_add_outbuf - expose output buffers to other end
  * @vq: the struct virtqueue we're talking about.
include/linux/virtio.h +7 -0
index 4d16c13d0df5..7f61f66ddaa2 100644
@@ -74,6 +74,13 @@ int virtqueue_add_sgs(struct virtqueue *vq,
 		      void *data,
 		      gfp_t gfp);
 
+int virtqueue_add_sgs_premapped(struct virtqueue *vq,
+				struct scatterlist *sgs[],
+				unsigned int out_sgs,
+				unsigned int in_sgs,
+				void *data,
+				gfp_t gfp);
+
 struct device *virtqueue_dma_dev(struct virtqueue *vq);
 
 bool virtqueue_kick(struct virtqueue *vq);
Signature · 1 line
2.48.1
author maintainer
#6 lore raw

RFC v2 4/5 virtiofs: perform DMA operations out of the spinlock

In reply to Eugenio Pérez

This is useful for some setups like swiotlb or VDUSE where the DMA operations are expensive and/or need to be performed with a write lock.

After applying this patch, fio read test goes from 1124MiB/s to 1191MiB/s.

---
 fs/fuse/virtio_fs.c | 40 ++++++++++++++++++++++++++++++++++++++--
 1 file changed, 38 insertions(+), 2 deletions(-)
fs/fuse/virtio_fs.c +38 -2
index 1344c5782a7c..e19c78f2480e 100644
@@ -836,8 +836,19 @@ static void virtio_fs_requests_done_work(struct work_struct *work)
 
 	/* End requests */
 	list_for_each_entry_safe(req, next, &reqs, list) {
+		unsigned int total_sgs = req->out_sgs + req->in_sgs;
+
 		list_del_init(&req->list);
 
+		for (unsigned int i = 0; i < total_sgs; ++i) {
+			enum dma_data_direction dir = (i < req->out_sgs) ?
+				DMA_TO_DEVICE : DMA_FROM_DEVICE;
+			dma_unmap_page(vq->vdev->dev.parent,
+				       sg_dma_address(&req->sg[i]),
+				       sg_dma_len(&req->sg[i]), dir);
+
+		}
+
 		/* blocking async request completes in a worker context */
 		if (req->args->may_block) {
 			struct virtio_fs_req_work *w;
@@ -1426,6 +1437,24 @@ static int virtio_fs_enqueue_req(struct virtio_fs_vq *fsvq,
 		sgs[i] = &req->sg[i];
 	WARN_ON(req->out_sgs + req->in_sgs != total_sgs);
 
+	// TODO can we change this ptr out of the lock?
+	vq = fsvq->vq;
+	// TODO handle this and following errors
+	for (i = 0; i < total_sgs; i++) {
+		struct page *page = sg_page(&req->sg[i]);
+		enum dma_data_direction dir = (i < req->out_sgs) ?
+			DMA_TO_DEVICE : DMA_FROM_DEVICE;
+		dma_addr_t dma_addr = dma_map_page(vq->vdev->dev.parent, page,
+						   req->sg[i].offset, req->sg[i].length, dir);
+
+		if (dma_mapping_error(vq->vdev->dev.parent, dma_addr)) {
+			ret = -ENOMEM;
+			goto out;
+		}
+		sg_dma_address(&req->sg[i]) = dma_addr;
+		sg_dma_len(&req->sg[i]) = req->sg[i].length;
+	}
+
 	spin_lock(&fsvq->lock);
 
 	if (!fsvq->connected) {
@@ -1434,8 +1463,8 @@ static int virtio_fs_enqueue_req(struct virtio_fs_vq *fsvq,
 		goto out;
 	}
 
-	vq = fsvq->vq;
-	ret = virtqueue_add_sgs(vq, sgs, req->out_sgs, req->in_sgs, req, GFP_ATOMIC);
+	ret = virtqueue_add_sgs_premapped(vq, sgs, req->out_sgs,
+					  req->in_sgs, req, GFP_ATOMIC);
 	if (ret < 0) {
 		spin_unlock(&fsvq->lock);
 		goto out;
@@ -1460,6 +1489,13 @@ static int virtio_fs_enqueue_req(struct virtio_fs_vq *fsvq,
 		virtqueue_notify(vq);
 
 out:
+	for (unsigned int j = 0; ret && j < total_sgs; ++j) {
+		enum dma_data_direction dir = (j < req->out_sgs) ?
+			DMA_TO_DEVICE : DMA_FROM_DEVICE;
+		dma_unmap_page(vq->vdev->dev.parent,
+			       sg_dma_address(&req->sg[j]),
+			       sg_dma_len(&req->sg[j]), dir);
+	}
 	if (ret < 0 && req->argbuf) {
 		kfree(req->argbuf);
 		req->argbuf = NULL;
Signature · 1 line
2.48.1
author maintainer
#8 lore raw

RFC v2 5/5 virtiofs: Disable notifications more aggresively

In reply to Eugenio Pérez

Disable notifications right before scheduling, instead of waiting until the work is running.

After applying this patch, fio read test goes from 1191MiB/s to 1263.14Mib/s

---
 fs/fuse/virtio_fs.c | 2 ++
 1 file changed, 2 insertions(+)
fs/fuse/virtio_fs.c +2 -0
index e19c78f2480e..3d6981c0f3c3 100644
@@ -915,6 +915,8 @@ static void virtio_fs_vq_done(struct virtqueue *vq)
 
 	dev_dbg(&vq->vdev->dev, "%s %s\n", __func__, fsvq->name);
 
+	/* Shceduled, don't send more notifications */
+	virtqueue_disable_cb(vq);
 	schedule_work(&fsvq->done_work);
 }
 
Signature · 1 line
2.48.1
#9 lore raw

Re: [RFC v2 5/5] virtiofs: Disable notifications more aggresively

In reply to Eugenio Pérez
Quoted 6 lines · Eugenio Pérez

Disable notifications right before scheduling, instead of waiting until the work is running.

After applying this patch, fio read test goes from 1191MiB/s to 1263.14Mib/s

Let's describe more about the testing. E.g FIO parameters, test setups.

Quoted 11 lines
---
 fs/fuse/virtio_fs.c | 2 ++
 1 file changed, 2 insertions(+)
fs/fuse/virtio_fs.c +0 -0
index e19c78f2480e..3d6981c0f3c3 100644

fs/fuse/virtio_fs.c static void virtio_fs_vq_done(struct virtqueue *vq) · line 918

Quoted 4 lines
fs/fuse/virtio_fs.c +1 -0
@@ -915,6 +915,8 @@ static void virtio_fs_vq_done(struct virtqueue *vq)
 
        dev_dbg(&vq->vdev->dev, "%s %s\n", __func__, fsvq->name);
 
+       /* Shceduled, don't send more notifications */

Typo, and I think we can just drop this commnet.

fs/fuse/virtio_fs.c static void virtio_fs_vq_done(struct virtqueue *vq) · line 919

Quoted 1 line
fs/fuse/virtio_fs.c +1 -0
+       virtqueue_disable_cb(vq);

Do we need to tweak the virtio_fs_requests_done_work() as well?

fs/fuse/virtio_fs.c static void virtio_fs_vq_done(struct virtqueue *vq) · line 921

Quoted 2 lines
fs/fuse/virtio_fs.c +0 -0
        schedule_work(&fsvq->done_work);
 }

Thanks

Quoted 4 lines
fs/fuse/virtio_fs.c +0 -1
--

2.48.1

#7 lore raw

Re: [RFC v2 4/5] virtiofs: perform DMA operations out of the spinlock

In reply to Eugenio Pérez
Quoted 5 lines · Eugenio Pérez

This is useful for some setups like swiotlb or VDUSE where the DMA operations are expensive and/or need to be performed with a write lock.

After applying this patch, fio read test goes from 1124MiB/s to 1191MiB/s.

What FIO parameter have you used? It might be worth trying different sizes. It seems to be more obvious when using larger requests when I'm testing similar optimization for virtio-blk.

And we also need to test without VDUSE, to make sure no regression in classical setups.

Thanks

Quoted 83 lines
---
 fs/fuse/virtio_fs.c | 40 ++++++++++++++++++++++++++++++++++++++--
 1 file changed, 38 insertions(+), 2 deletions(-)
fs/fuse/virtio_fs.c +38 -3
index 1344c5782a7c..e19c78f2480e 100644
@@ -836,8 +836,19 @@ static void virtio_fs_requests_done_work(struct work_struct *work)
 
        /* End requests */
        list_for_each_entry_safe(req, next, &reqs, list) {
+               unsigned int total_sgs = req->out_sgs + req->in_sgs;
+
                list_del_init(&req->list);
 
+               for (unsigned int i = 0; i < total_sgs; ++i) {
+                       enum dma_data_direction dir = (i < req->out_sgs) ?
+                               DMA_TO_DEVICE : DMA_FROM_DEVICE;
+                       dma_unmap_page(vq->vdev->dev.parent,
+                                      sg_dma_address(&req->sg[i]),
+                                      sg_dma_len(&req->sg[i]), dir);
+
+               }
+
                /* blocking async request completes in a worker context */
                if (req->args->may_block) {
                        struct virtio_fs_req_work *w;
@@ -1426,6 +1437,24 @@ static int virtio_fs_enqueue_req(struct virtio_fs_vq *fsvq,
                sgs[i] = &req->sg[i];
        WARN_ON(req->out_sgs + req->in_sgs != total_sgs);
 
+       // TODO can we change this ptr out of the lock?
+       vq = fsvq->vq;
+       // TODO handle this and following errors
+       for (i = 0; i < total_sgs; i++) {
+               struct page *page = sg_page(&req->sg[i]);
+               enum dma_data_direction dir = (i < req->out_sgs) ?
+                       DMA_TO_DEVICE : DMA_FROM_DEVICE;
+               dma_addr_t dma_addr = dma_map_page(vq->vdev->dev.parent, page,
+                                                  req->sg[i].offset, req->sg[i].length, dir);
+
+               if (dma_mapping_error(vq->vdev->dev.parent, dma_addr)) {
+                       ret = -ENOMEM;
+                       goto out;
+               }
+               sg_dma_address(&req->sg[i]) = dma_addr;
+               sg_dma_len(&req->sg[i]) = req->sg[i].length;
+       }
+
        spin_lock(&fsvq->lock);
 
        if (!fsvq->connected) {
@@ -1434,8 +1463,8 @@ static int virtio_fs_enqueue_req(struct virtio_fs_vq *fsvq,
                goto out;
        }
 
-       vq = fsvq->vq;
-       ret = virtqueue_add_sgs(vq, sgs, req->out_sgs, req->in_sgs, req, GFP_ATOMIC);
+       ret = virtqueue_add_sgs_premapped(vq, sgs, req->out_sgs,
+                                         req->in_sgs, req, GFP_ATOMIC);
        if (ret < 0) {
                spin_unlock(&fsvq->lock);
                goto out;
@@ -1460,6 +1489,13 @@ static int virtio_fs_enqueue_req(struct virtio_fs_vq *fsvq,
                virtqueue_notify(vq);
 
 out:
+       for (unsigned int j = 0; ret && j < total_sgs; ++j) {
+               enum dma_data_direction dir = (j < req->out_sgs) ?
+                       DMA_TO_DEVICE : DMA_FROM_DEVICE;
+               dma_unmap_page(vq->vdev->dev.parent,
+                              sg_dma_address(&req->sg[j]),
+                              sg_dma_len(&req->sg[j]), dir);
+       }
        if (ret < 0 && req->argbuf) {
                kfree(req->argbuf);
                req->argbuf = NULL;
--

2.48.1

author maintainer
#3 lore raw

Re: [RFC v2 1/5] vduse: add virtio_fs to allowed dev id

In reply to Jason Wang
Quoted 12 lines · Jason Wang
Quoted 7 lines · Eugenio Pérez

A VDUSE device that implements virtiofs device works fine just by adding the device id to the whitelist.

---

Let's separate this from this series.

Right, actually I should have said this series should be applied on top of this sent patch:

https://lore.kernel.org/lkml/CAJaqyWdC-Tte+ao6pk22fq-mUym8C9guQFThSnG5gMxWNqWyXw@mail.gmail.com/T/

author maintainer
#10 lore raw

Re: [RFC v2 5/5] virtiofs: Disable notifications more aggresively

In reply to Jason Wang
Quoted 23 lines · Jason Wang
Quoted 6 lines · Eugenio Pérez

Disable notifications right before scheduling, instead of waiting until the work is running.

After applying this patch, fio read test goes from 1191MiB/s to 1263.14Mib/s

Let's describe more about the testing. E.g FIO parameters, test setups.

Quoted 12 lines
---
 fs/fuse/virtio_fs.c | 2 ++
 1 file changed, 2 insertions(+)
fs/fuse/virtio_fs.c +0 -0
index e19c78f2480e..3d6981c0f3c3 100644
@@ -915,6 +915,8 @@ static void virtio_fs_vq_done(struct virtqueue *vq)
 
Quoted 5 lines

fs/fuse/virtio_fs.c static void virtio_fs_vq_done(struct virtqueue *vq) · line 918

Quoted 3 lines
fs/fuse/virtio_fs.c +1 -0
        dev_dbg(&vq->vdev->dev, "%s %s\n", __func__, fsvq->name);
 
+       /* Shceduled, don't send more notifications */

Typo, and I think we can just drop this commnet.

Agree.

Quoted 4 lines

fs/fuse/virtio_fs.c static void virtio_fs_vq_done(struct virtqueue *vq) · line 919

Quoted 2 lines
fs/fuse/virtio_fs.c +1 -0
+       virtqueue_disable_cb(vq);

Do we need to tweak the virtio_fs_requests_done_work() as well?

I don't think so, as virtqueue_disable_cb can be called multiple times without consequences.

To control it in virtio_fs_requests_done_work imply to create a new boolean somewhere and add a conditional there, redundant with the conditional in virtio_fs_requests_done_work.

author maintainer
#11 lore raw

Re: [RFC v2 5/5] virtiofs: Disable notifications more aggresively

Quoted 36 lines · Eugenio Perez Martin
Quoted 23 lines · Jason Wang
Quoted 6 lines · Eugenio Pérez

Disable notifications right before scheduling, instead of waiting until the work is running.

After applying this patch, fio read test goes from 1191MiB/s to 1263.14Mib/s

Let's describe more about the testing. E.g FIO parameters, test setups.

Quoted 12 lines
---
 fs/fuse/virtio_fs.c | 2 ++
 1 file changed, 2 insertions(+)
fs/fuse/virtio_fs.c +0 -0
index e19c78f2480e..3d6981c0f3c3 100644
@@ -915,6 +915,8 @@ static void virtio_fs_vq_done(struct virtqueue *vq)
 
Quoted 5 lines

fs/fuse/virtio_fs.c static void virtio_fs_vq_done(struct virtqueue *vq) · line 918

Quoted 3 lines
fs/fuse/virtio_fs.c +1 -0
        dev_dbg(&vq->vdev->dev, "%s %s\n", __func__, fsvq->name);
 
+       /* Shceduled, don't send more notifications */

Typo, and I think we can just drop this commnet.

Agree.

Quoted 3 lines
Quoted 2 lines
fs/fuse/virtio_fs.c +1 -0
+       virtqueue_disable_cb(vq);
Quoted 8 lines
Quoted 1 line

Do we need to tweak the virtio_fs_requests_done_work() as well?

I don't think so, as virtqueue_disable_cb can be called multiple times without consequences.

To control it in virtio_fs_requests_done_work imply to create a new boolean somewhere and add a conditional there, redundant with the conditional in virtio_fs_requests_done_work.

I meant it would be redundant with the conditional in virtqueue_disable_cb_split and _packed, as they store the avail flags set so the operation is idempotent :).

Is it worth reordering this patch so it can be profiled and applied in current code?