qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Brian Song <hibriansong@gmail.com>
To: qemu-devel@nongnu.org
Cc: kwolf@redhat.com, stefanha@redhat.com,
	Brian Song <hibriansong@gmail.com>
Subject: [RFC PATCH 1/1] This work adds support for registering block file descriptors to the io_uring instance and uses IOSQE_FIXED_FILE in I/O requests (SQEs) to avoid the cost of fdget() in the kernel. It is a basic implementation for testing, and does not yet handle cases where block devices are removed.
Date: Mon, 14 Apr 2025 18:54:12 +0000	[thread overview]
Message-ID: <20250414185414.2922845-2-hibriansong@gmail.com> (raw)
In-Reply-To: <20250414185414.2922845-1-hibriansong@gmail.com>

Suggested-by: Kevin Wolf <kwolf@redhat.com>
Suggested-by: Stefan Hajnoczi <stefanha@redhat.com>
Signed-off-by: Brian Song <hibriansong@gmail.com>
---
 block/io_uring.c | 60 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 60 insertions(+)

diff --git a/block/io_uring.c b/block/io_uring.c
index dd4f304910..94a875fbae 100644
--- a/block/io_uring.c
+++ b/block/io_uring.c
@@ -58,6 +58,11 @@ struct LuringState {
     LuringQueue io_q;
 
     QEMUBH *completion_bh;
+
+    /* fixed file support */
+    int *registered_fds;
+    int nr_registered_fds;
+    int max_registered_fds; /* size of registered_fds */
 };
 
 /**
@@ -323,6 +328,41 @@ static void luring_deferred_fn(void *opaque)
     }
 }
 
+static int luring_register_fd(LuringState *s, int fd)
+{
+    int idx;
+    int *new_fds;
+    int ret;
+
+    for (idx = 0; idx < s->nr_registered_fds; idx++) {
+        if (s->registered_fds[idx] == fd) {
+            return idx;
+        }
+    }
+
+    /* Grow the array if needed */
+    if (s->nr_registered_fds >= s->max_registered_fds) {
+        int new_max = s->max_registered_fds * 2;
+        new_fds = g_realloc(s->registered_fds, sizeof(int) * new_max);
+        if (!new_fds) {
+            return -ENOMEM;
+        }
+        s->registered_fds = new_fds;
+        s->max_registered_fds = new_max;
+    }
+
+    idx = s->nr_registered_fds++;
+    s->registered_fds[idx] = fd;
+   
+    ret = io_uring_register_files(&s->ring, s->registered_fds, s->nr_registered_fds);
+    if (ret < 0) {
+        s->nr_registered_fds--;
+        return ret;
+    }
+
+    return idx;
+}
+
 /**
  * luring_do_submit:
  * @fd: file descriptor for I/O
@@ -339,6 +379,15 @@ static int luring_do_submit(int fd, LuringAIOCB *luringcb, LuringState *s,
 {
     int ret;
     struct io_uring_sqe *sqes = &luringcb->sqeq;
+    int fixed_fd_idx;
+   
+    fixed_fd_idx = luring_register_fd(s, fd);
+    if (fixed_fd_idx < 0) {
+        return fixed_fd_idx;
+    }
+   
+    sqes->flags |= IOSQE_FIXED_FILE;
+    sqes->fd = fixed_fd_idx;
 
     switch (type) {
     case QEMU_AIO_WRITE:
@@ -447,6 +496,11 @@ LuringState *luring_init(Error **errp)
         return NULL;
     }
 
+    /* Initialize fixed file support */
+    s->max_registered_fds = 1024;
+    s->registered_fds = g_new0(int, s->max_registered_fds);
+    s->nr_registered_fds = 0;
+
     ioq_init(&s->io_q);
     return s;
 
@@ -454,6 +508,12 @@ LuringState *luring_init(Error **errp)
 
 void luring_cleanup(LuringState *s)
 {
+    if (s->registered_fds) {
+        if (s->nr_registered_fds > 0) {
+            io_uring_unregister_files(&s->ring);
+        }
+        g_free(s->registered_fds);
+    }
     io_uring_queue_exit(&s->ring);
     trace_luring_cleanup_state(s);
     g_free(s);
-- 
2.43.0



      reply	other threads:[~2025-04-14 19:31 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-04-14 18:54 [RFC PATCH 0/1] add fixed file table support Brian Song
2025-04-14 18:54 ` Brian Song [this message]

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=20250414185414.2922845-2-hibriansong@gmail.com \
    --to=hibriansong@gmail.com \
    --cc=kwolf@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=stefanha@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).