qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Stefan Hajnoczi <stefanha@redhat.com>
To: qemu-devel@nongnu.org
Cc: oleksandr@redhat.com, Kevin Wolf <kwolf@redhat.com>,
	qemu-block@nongnu.org, Julia Suvorova <jusual@mail.ru>,
	Julia Suvorova <jusual@redhat.com>,
	Markus Armbruster <armbru@redhat.com>,
	Max Reitz <mreitz@redhat.com>,
	Stefan Hajnoczi <stefanha@redhat.com>,
	Paolo Bonzini <pbonzini@redhat.com>, Fam Zheng <fam@euphon.net>,
	Aarushi Mehta <mehta.aaru20@gmail.com>
Subject: [PATCH 16/16] block/io_uring: adds fd registration
Date: Mon,  7 Oct 2019 13:09:37 +0100	[thread overview]
Message-ID: <20191007120937.5862-17-stefanha@redhat.com> (raw)
In-Reply-To: <20191007120937.5862-1-stefanha@redhat.com>

From: Aarushi Mehta <mehta.aaru20@gmail.com>

File descriptor registration eliminates the need to get file descriptors
for each request on the host kernel side.  The host kernel keeps
references to a set of files that were registered by the application.
These files can be quickly accessed by index instead of a traditional
file descriptor number.

Signed-off-by: Aarushi Mehta <mehta.aaru20@gmail.com>
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
---
v10:
 * Add io_uring_register_files() return value to trace_luring_fd_register()
 * Fix indentation in luring_fd_unregister()
 * Set s->fd_reg.fd_array to NULL after g_free() to avoid dangling pointers
 * Simplify fd registration code
 * Add luring_fd_unregister() and call it from file-posix.c to prevent
   fd leaks
 * Add trace_luring_fd_unregister() trace event
---
 include/block/raw-aio.h |   1 +
 block/file-posix.c      |  18 +++++
 block/io_uring.c        | 150 +++++++++++++++++++++++++++++++++++++++-
 block/trace-events      |   2 +
 4 files changed, 170 insertions(+), 1 deletion(-)

diff --git a/include/block/raw-aio.h b/include/block/raw-aio.h
index 251b10d273..5fe6481bc4 100644
--- a/include/block/raw-aio.h
+++ b/include/block/raw-aio.h
@@ -68,6 +68,7 @@ void luring_detach_aio_context(LuringState *s, AioContext *old_context);
 void luring_attach_aio_context(LuringState *s, AioContext *new_context);
 void luring_io_plug(BlockDriverState *bs, LuringState *s);
 void luring_io_unplug(BlockDriverState *bs, LuringState *s);
+void luring_fd_unregister(LuringState *s, int fd);
 #endif
 
 #ifdef _WIN32
diff --git a/block/file-posix.c b/block/file-posix.c
index 264c44fc3f..604b5427c3 100644
--- a/block/file-posix.c
+++ b/block/file-posix.c
@@ -2010,6 +2010,20 @@ static int raw_co_flush_to_disk(BlockDriverState *bs)
     return raw_thread_pool_submit(bs, handle_aiocb_flush, &acb);
 }
 
+static void raw_aio_detach_aio_context(BlockDriverState *bs)
+{
+#ifdef CONFIG_LINUX_IO_URING
+    BDRVRawState *s = bs->opaque;
+
+    if (s->fd >= 0 && s->use_linux_io_uring) {
+        LuringState *luring;
+
+        luring = aio_get_linux_io_uring(bdrv_get_aio_context(bs));
+        luring_fd_unregister(luring, s->fd);
+    }
+#endif
+}
+
 static void raw_aio_attach_aio_context(BlockDriverState *bs,
                                        AioContext *new_context)
 {
@@ -2994,6 +3008,7 @@ BlockDriver bdrv_file = {
     .bdrv_refresh_limits = raw_refresh_limits,
     .bdrv_io_plug = raw_aio_plug,
     .bdrv_io_unplug = raw_aio_unplug,
+    .bdrv_detach_aio_context = raw_aio_detach_aio_context,
     .bdrv_attach_aio_context = raw_aio_attach_aio_context,
 
     .bdrv_co_truncate = raw_co_truncate,
@@ -3470,6 +3485,7 @@ static BlockDriver bdrv_host_device = {
     .bdrv_refresh_limits = raw_refresh_limits,
     .bdrv_io_plug = raw_aio_plug,
     .bdrv_io_unplug = raw_aio_unplug,
+    .bdrv_detach_aio_context = raw_aio_detach_aio_context,
     .bdrv_attach_aio_context = raw_aio_attach_aio_context,
 
     .bdrv_co_truncate       = raw_co_truncate,
@@ -3594,6 +3610,7 @@ static BlockDriver bdrv_host_cdrom = {
     .bdrv_refresh_limits = raw_refresh_limits,
     .bdrv_io_plug = raw_aio_plug,
     .bdrv_io_unplug = raw_aio_unplug,
+    .bdrv_detach_aio_context = raw_aio_detach_aio_context,
     .bdrv_attach_aio_context = raw_aio_attach_aio_context,
 
     .bdrv_co_truncate    = raw_co_truncate,
@@ -3726,6 +3743,7 @@ static BlockDriver bdrv_host_cdrom = {
     .bdrv_refresh_limits = raw_refresh_limits,
     .bdrv_io_plug = raw_aio_plug,
     .bdrv_io_unplug = raw_aio_unplug,
+    .bdrv_detach_aio_context = raw_aio_detach_aio_context,
     .bdrv_attach_aio_context = raw_aio_attach_aio_context,
 
     .bdrv_co_truncate    = raw_co_truncate,
diff --git a/block/io_uring.c b/block/io_uring.c
index 56892fd1ab..4d6e50ce19 100644
--- a/block/io_uring.c
+++ b/block/io_uring.c
@@ -46,10 +46,16 @@ typedef struct LuringQueue {
     QSIMPLEQ_HEAD(, LuringAIOCB) submit_queue;
 } LuringQueue;
 
+typedef struct LuringFd {
+    int *fd_array;
+    GHashTable *fd_lookup;
+} LuringFd;
+
 typedef struct LuringState {
     AioContext *aio_context;
 
     struct io_uring ring;
+    LuringFd fd_reg;
 
     /* io queue for submit at batch.  Protected by AioContext lock. */
     LuringQueue io_q;
@@ -298,6 +304,136 @@ static void ioq_init(LuringQueue *io_q)
     io_q->blocked = false;
 }
 
+/**
+ * luring_fd_unregister:
+ *
+ * Remove a file descriptor from the registered fds list.  This is a slow
+ * operation because all registered fds are refreshed, but this function should
+ * not be called often anyway.
+ *
+ * Only call this function while there are no requests in flight.
+ */
+void luring_fd_unregister(LuringState *s, int fd)
+{
+    LuringFd *fd_reg = &s->fd_reg;
+    void *value;
+    int idx;
+    int ret;
+    int nr;
+    int i;
+
+    if (!g_hash_table_lookup_extended(fd_reg->fd_lookup, GINT_TO_POINTER(fd),
+                                      NULL, &value)) {
+        return;
+    }
+
+    ret = io_uring_unregister_files(&s->ring);
+    if (ret < 0) {
+        return;
+    }
+
+    idx = GPOINTER_TO_INT(value);
+    nr = g_hash_table_size(fd_reg->fd_lookup) - 1; /* minus this fd */
+
+    trace_luring_fd_unregister(s, fd, idx);
+
+    memmove(&fd_reg->fd_array[idx], &fd_reg->fd_array[idx + 1], nr - idx);
+
+    /* Rebuild hash table */
+    g_hash_table_remove_all(fd_reg->fd_lookup);
+    for (i = 0; i < nr; i++) {
+        g_hash_table_insert(fd_reg->fd_lookup,
+                            GINT_TO_POINTER(fd_reg->fd_array[i]),
+                            GINT_TO_POINTER(i));
+    }
+
+    io_uring_register_files(&s->ring, fd_reg->fd_array, nr);
+}
+
+/**
+ * luring_fd_register:
+ *
+ * Register file descriptors, see luring_fd_lookup
+ */
+static int luring_fd_register(struct io_uring *ring, LuringFd *fd_reg, int fd)
+{
+    int ret, nr;
+    GHashTable *lookup = fd_reg->fd_lookup;
+    nr = g_hash_table_size(lookup);
+
+    /* If adding new, API requires older registrations to be removed */
+    if (nr) {
+        /*
+         * See linux b19062a56726, register needs the ring mutex, any
+         * submission in progress will complete before unregistering begins
+         * and new ones will have to wait.
+         */
+        ret = io_uring_unregister_files(ring);
+        if (ret < 0) {
+            return ret;
+        }
+    }
+
+    fd_reg->fd_array = g_realloc_n(fd_reg->fd_array, nr + 1, sizeof(int));
+    fd_reg->fd_array[nr] = fd;
+
+    g_hash_table_insert(lookup, GINT_TO_POINTER(fd), GINT_TO_POINTER(nr));
+    ret = io_uring_register_files(ring, fd_reg->fd_array, nr + 1);
+    trace_luring_fd_register(fd, nr, ret);
+    if (ret < 0) {
+        /* Leave fd_array[] alone, fd will be overwritten next time anyway */
+        g_hash_table_remove(lookup, GINT_TO_POINTER(fd));
+        return ret;
+    }
+    return nr;
+}
+
+/**
+ * luring_fd_init:
+ *
+ * Initialize file descriptors
+ */
+static void luring_fd_init(LuringState *s)
+{
+    s->fd_reg.fd_lookup = g_hash_table_new(g_direct_hash, g_direct_equal);
+}
+
+/**
+ * luring_fd_cleanup:
+ *
+ * Unregisters file descriptors, TODO: error handling
+ */
+static void luring_fd_cleanup(LuringState *s)
+{
+    io_uring_unregister_files(&s->ring);
+    g_hash_table_unref(s->fd_reg.fd_lookup);
+    g_free(s->fd_reg.fd_array);
+    s->fd_reg.fd_array = NULL;
+}
+
+/**
+ * luring_fd_lookup:
+ *
+ * Used to lookup fd index in registered array at submission time
+ * If the lookup table has not been created or the fd is not in the table,
+ * the fd is registered.
+ *
+ * If registration errors, this function returns -errno.
+ *
+ * Unregistering is done in luring_detach_aio_context().
+ */
+static int luring_fd_lookup(LuringState *s, int fd)
+{
+    void *index;
+
+    if (g_hash_table_lookup_extended(s->fd_reg.fd_lookup, GINT_TO_POINTER(fd),
+                                     NULL, &index)) {
+        return GPOINTER_TO_INT(index);
+    }
+
+    return luring_fd_register(&s->ring, &s->fd_reg, fd);
+}
+
 void luring_io_plug(BlockDriverState *bs, LuringState *s)
 {
     trace_luring_io_plug(s);
@@ -329,9 +465,14 @@ void luring_io_unplug(BlockDriverState *bs, LuringState *s)
 static int luring_do_submit(int fd, LuringAIOCB *luringcb, LuringState *s,
                             uint64_t offset, int type)
 {
-    int ret;
+    int ret, fd_index;
     struct io_uring_sqe *sqes = &luringcb->sqeq;
 
+    fd_index = luring_fd_lookup(s, fd);
+    if (fd_index >= 0) {
+        fd = fd_index;
+    }
+
     switch (type) {
     case QEMU_AIO_WRITE:
         io_uring_prep_writev(sqes, fd, luringcb->qiov->iov,
@@ -349,7 +490,11 @@ static int luring_do_submit(int fd, LuringAIOCB *luringcb, LuringState *s,
                         __func__, type);
         abort();
     }
+
     io_uring_sqe_set_data(sqes, luringcb);
+    if (fd_index >= 0) {
+        io_uring_sqe_set_flags(sqes, IOSQE_FIXED_FILE);
+    }
 
     QSIMPLEQ_INSERT_TAIL(&s->io_q.submit_queue, luringcb, next);
     s->io_q.in_queue++;
@@ -375,6 +520,7 @@ int coroutine_fn luring_co_submit(BlockDriverState *bs, LuringState *s, int fd,
         .qiov       = qiov,
         .is_read    = (type == QEMU_AIO_READ),
     };
+
     trace_luring_co_submit(bs, s, &luringcb, fd, offset, qiov ? qiov->size : 0,
                            type);
     ret = luring_do_submit(fd, &luringcb, s, offset, type);
@@ -391,6 +537,7 @@ int coroutine_fn luring_co_submit(BlockDriverState *bs, LuringState *s, int fd,
 
 void luring_detach_aio_context(LuringState *s, AioContext *old_context)
 {
+    luring_fd_cleanup(s);
     aio_set_fd_handler(old_context, s->ring.ring_fd, false, NULL, NULL, NULL,
                        s);
     qemu_bh_delete(s->completion_bh);
@@ -403,6 +550,7 @@ void luring_attach_aio_context(LuringState *s, AioContext *new_context)
     s->completion_bh = aio_bh_new(new_context, qemu_luring_completion_bh, s);
     aio_set_fd_handler(s->aio_context, s->ring.ring_fd, false,
                        qemu_luring_completion_cb, NULL, qemu_luring_poll_cb, s);
+    luring_fd_init(s);
 }
 
 LuringState *luring_init(Error **errp)
diff --git a/block/trace-events b/block/trace-events
index 2e3a9851b6..6a19cc00b7 100644
--- a/block/trace-events
+++ b/block/trace-events
@@ -72,6 +72,8 @@ luring_co_submit(void *bs, void *s, void *luringcb, int fd, uint64_t offset, siz
 luring_process_completion(void *s, void *aiocb, int ret) "LuringState %p luringcb %p ret %d"
 luring_io_uring_submit(void *s, int ret) "LuringState %p ret %d"
 luring_resubmit_short_read(void *s, void *luringcb, int nread) "LuringState %p luringcb %p nread %d"
+luring_fd_register(int fd, int index, int ret) "fd %d index %d ret %d"
+luring_fd_unregister(void *s, int fd, int index) "LuringState %p fd %d index %d"
 
 # qcow2.c
 qcow2_writev_start_req(void *co, int64_t offset, int bytes) "co %p offset 0x%" PRIx64 " bytes %d"
-- 
2.21.0



      parent reply	other threads:[~2019-10-07 12:30 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-10-07 12:09 [PATCH 00/16] io_uring: add Linux io_uring AIO engine Stefan Hajnoczi
2019-10-07 12:09 ` [PATCH 01/16] configure: permit use of io_uring Stefan Hajnoczi
2019-10-07 12:09 ` [PATCH 02/16] qapi/block-core: add option for io_uring Stefan Hajnoczi
2019-10-07 12:09 ` [PATCH 03/16] block/block: add BDRV flag " Stefan Hajnoczi
2019-10-07 12:09 ` [PATCH 04/16] block/io_uring: implements interfaces " Stefan Hajnoczi
2019-10-07 12:09 ` [PATCH 05/16] stubs: add stubs for io_uring interface Stefan Hajnoczi
2019-10-07 12:09 ` [PATCH 06/16] util/async: add aio interfaces for io_uring Stefan Hajnoczi
2019-10-07 12:09 ` [PATCH 07/16] blockdev: adds bdrv_parse_aio to use io_uring Stefan Hajnoczi
2019-10-07 12:09 ` [PATCH 08/16] block/file-posix.c: extend " Stefan Hajnoczi
2019-10-07 12:09 ` [PATCH 09/16] block: add trace events for io_uring Stefan Hajnoczi
2019-10-07 12:09 ` [PATCH 10/16] block/io_uring: adds userspace completion polling Stefan Hajnoczi
2019-10-07 12:09 ` [PATCH 11/16] qemu-io: adds option to use aio engine Stefan Hajnoczi
2019-10-07 12:09 ` [PATCH 12/16] qemu-img: adds option to use aio engine for benchmarking Stefan Hajnoczi
2019-10-07 12:09 ` [PATCH 13/16] qemu-nbd: adds option for aio engines Stefan Hajnoczi
2019-10-07 12:09 ` [PATCH 14/16] tests/qemu-iotests: enable testing with aio options Stefan Hajnoczi
2019-10-07 12:09 ` [PATCH 15/16] tests/qemu-iotests: use AIOMODE with various tests Stefan Hajnoczi
2019-10-07 12:09 ` Stefan Hajnoczi [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=20191007120937.5862-17-stefanha@redhat.com \
    --to=stefanha@redhat.com \
    --cc=armbru@redhat.com \
    --cc=fam@euphon.net \
    --cc=jusual@mail.ru \
    --cc=jusual@redhat.com \
    --cc=kwolf@redhat.com \
    --cc=mehta.aaru20@gmail.com \
    --cc=mreitz@redhat.com \
    --cc=oleksandr@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=qemu-block@nongnu.org \
    --cc=qemu-devel@nongnu.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;
as well as URLs for NNTP newsgroup(s).