From: "Eugenio Pérez" <eperezma@redhat.com>
To: Xuan Zhuo <xuanzhuo@linux.alibaba.com>,
Stefan Hajnoczi <stefanha@redhat.com>,
Hanna Reitz <hreitz@redhat.com>,
linux-kernel@vger.kernel.org,
German Maglione <gmaglione@redhat.com>,
virtualization@lists.linux.dev,
Stefano Garzarella <sgarzare@redhat.com>,
yama@redhat.com, Vivek Goyal <vgoyal@redhat.com>,
Miklos Szeredi <miklos@szeredi.hu>,
mst@redhat.com, Jason Wang <jasowang@redhat.com>
Subject: [RFC v2 2/5] virtiofs: Move stack sg to fuse_req
Date: Fri, 21 Feb 2025 18:06:23 +0100 [thread overview]
Message-ID: <20250221170626.261687-3-eperezma@redhat.com> (raw)
In-Reply-To: <20250221170626.261687-1-eperezma@redhat.com>
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.
Signed-off-by: Eugenio Pérez <eperezma@redhat.com>
---
fs/fuse/fuse_i.h | 7 +++++++
fs/fuse/virtio_fs.c | 45 +++++++++++++++++++++------------------------
2 files changed, 28 insertions(+), 24 deletions(-)
diff --git a/fs/fuse/fuse_i.h b/fs/fuse/fuse_i.h
index fee96fe7887b..0a77e28d8da6 100644
--- a/fs/fuse/fuse_i.h
+++ b/fs/fuse/fuse_i.h
@@ -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 */
diff --git a/fs/fuse/virtio_fs.c b/fs/fuse/virtio_fs.c
index 82afe78ec542..1344c5782a7c 100644
--- a/fs/fuse/virtio_fs.c
+++ b/fs/fuse/virtio_fs.c
@@ -1377,14 +1377,10 @@ static int virtio_fs_enqueue_req(struct virtio_fs_vq *fsvq,
{
/* requests need at least 4 elements */
struct scatterlist *stack_sgs[6];
- struct scatterlist stack_sg[ARRAY_SIZE(stack_sgs)];
struct scatterlist **sgs = stack_sgs;
- struct scatterlist *sg = stack_sg;
struct virtqueue *vq;
struct fuse_args *args = req->args;
unsigned int argbuf_used = 0;
- unsigned int out_sgs = 0;
- unsigned int in_sgs = 0;
unsigned int total_sgs;
unsigned int i;
int ret;
@@ -1392,11 +1388,13 @@ static int virtio_fs_enqueue_req(struct virtio_fs_vq *fsvq,
struct fuse_pqueue *fpq;
/* Does the sglist fit on the stack? */
+ /* TODO replace magic 6 by a macro */
+ req->sg = req->sg_inline_data;
total_sgs = sg_count_fuse_req(req);
- if (total_sgs > ARRAY_SIZE(stack_sgs)) {
+ if (total_sgs > 6) {
sgs = kmalloc_array(total_sgs, sizeof(sgs[0]), gfp);
- sg = kmalloc_array(total_sgs, sizeof(sg[0]), gfp);
- if (!sgs || !sg) {
+ req->sg = kmalloc_array(total_sgs, sizeof(req->sg[0]), gfp);
+ if (!sgs || !req->sg) {
ret = -ENOMEM;
goto out;
}
@@ -1408,26 +1406,25 @@ static int virtio_fs_enqueue_req(struct virtio_fs_vq *fsvq,
goto out;
/* Request elements */
- sg_init_one(&sg[out_sgs++], &req->in.h, sizeof(req->in.h));
- out_sgs += sg_init_fuse_args(&sg[out_sgs], req,
- (struct fuse_arg *)args->in_args,
- args->in_numargs, args->in_pages,
- req->argbuf, &argbuf_used);
+ sg_init_one(&req->sg[req->out_sgs++], &req->in.h, sizeof(req->in.h));
+ req->out_sgs += sg_init_fuse_args(&req->sg[req->out_sgs], req,
+ (struct fuse_arg *)args->in_args,
+ args->in_numargs, args->in_pages,
+ req->argbuf, &argbuf_used);
/* Reply elements */
if (test_bit(FR_ISREPLY, &req->flags)) {
- sg_init_one(&sg[out_sgs + in_sgs++],
+ sg_init_one(&req->sg[req->out_sgs + req->in_sgs++],
&req->out.h, sizeof(req->out.h));
- in_sgs += sg_init_fuse_args(&sg[out_sgs + in_sgs], req,
- args->out_args, args->out_numargs,
- args->out_pages,
- req->argbuf + argbuf_used, NULL);
+ req->in_sgs += sg_init_fuse_args(&req->sg[req->out_sgs + req->in_sgs], req,
+ args->out_args, args->out_numargs,
+ args->out_pages,
+ req->argbuf + argbuf_used, NULL);
}
- WARN_ON(out_sgs + in_sgs != total_sgs);
-
for (i = 0; i < total_sgs; i++)
- sgs[i] = &sg[i];
+ sgs[i] = &req->sg[i];
+ WARN_ON(req->out_sgs + req->in_sgs != total_sgs);
spin_lock(&fsvq->lock);
@@ -1438,7 +1435,7 @@ static int virtio_fs_enqueue_req(struct virtio_fs_vq *fsvq,
}
vq = fsvq->vq;
- ret = virtqueue_add_sgs(vq, sgs, out_sgs, in_sgs, req, GFP_ATOMIC);
+ ret = virtqueue_add_sgs(vq, sgs, req->out_sgs, req->in_sgs, req, GFP_ATOMIC);
if (ret < 0) {
spin_unlock(&fsvq->lock);
goto out;
@@ -1467,10 +1464,10 @@ static int virtio_fs_enqueue_req(struct virtio_fs_vq *fsvq,
kfree(req->argbuf);
req->argbuf = NULL;
}
- if (sgs != stack_sgs) {
+ if (ret < 0 && req->sg != req->sg_inline_data)
+ kfree(req->sg);
+ if (sgs != stack_sgs)
kfree(sgs);
- kfree(sg);
- }
return ret;
}
--
2.48.1
next prev parent reply other threads:[~2025-02-21 17:06 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-02-21 17:06 [RFC v2 0/5] virtiofs: map buffer out of virtqueue lock Eugenio Pérez
2025-02-21 17:06 ` [RFC v2 1/5] vduse: add virtio_fs to allowed dev id Eugenio Pérez
2025-02-24 1:57 ` Jason Wang
2025-02-24 6:42 ` Eugenio Perez Martin
2025-02-21 17:06 ` Eugenio Pérez [this message]
2025-02-21 17:06 ` [RFC v2 3/5] virtio_ring: add api for premapped out and in buffer chain Eugenio Pérez
2025-02-21 17:06 ` [RFC v2 4/5] virtiofs: perform DMA operations out of the spinlock Eugenio Pérez
2025-02-24 2:03 ` Jason Wang
2025-02-21 17:06 ` [RFC v2 5/5] virtiofs: Disable notifications more aggresively Eugenio Pérez
2025-02-24 2:01 ` Jason Wang
2025-02-24 6:44 ` Eugenio Perez Martin
2025-02-24 7:44 ` Eugenio Perez Martin
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=20250221170626.261687-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 \
--cc=yama@redhat.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;
as well as URLs for NNTP newsgroup(s).