Skip to contentSkip to conversation

iothread: fix epollfd leak in the process of delIOThread

PATCH Superseded Jie Wang 3 participants 6 messages May 16, 2018 – May 17, 2018

v4 of 6 Superseded Latest is v6 →

lore
Share & tools
lore

Jie Wang

author
#0 lore raw

PATCH v4 1/1 iothread: fix epollfd leak in the process of delIOThread

When we call addIOThread, the epollfd created in aio_context_setup, but not close it in the process of delIOThread, so the epollfd will leak.

---
 include/block/aio.h | 8 ++++++++
 util/aio-posix.c    | 9 +++++++++
 util/aio-win32.c    | 4 ++++
 util/async.c        | 1 +
 4 files changed, 22 insertions(+)
include/block/aio.h +8 -0
index a1d6b9e..ae6f354 100644
@@ -555,6 +555,14 @@ static inline bool in_aio_context_home_thread(AioContext *ctx)
 void aio_context_setup(AioContext *ctx);
 
 /**
+ * aio_context_destroy:
+ * @ctx: the aio context
+ *
+ * Destroy the aio context.
+ */
+void aio_context_destroy(AioContext *ctx);
+
+/**
  * aio_context_set_poll_params:
  * @ctx: the aio context
  * @max_ns: how long to busy poll for, in nanoseconds
util/aio-posix.c +9 -0
index d8f0cb4..0ade2c7 100644
@@ -713,6 +713,15 @@ void aio_context_setup(AioContext *ctx)
 #endif
 }
 
+void aio_context_destroy(AioContext *ctx)
+{
+#ifdef CONFIG_EPOLL_CREATE1
+    if (ctx->epollfd >= 0) {
+        close(ctx->epollfd);
+    }
+#endif
+}
+
 void aio_context_set_poll_params(AioContext *ctx, int64_t max_ns,
                                  int64_t grow, int64_t shrink, Error **errp)
 {
util/aio-win32.c +4 -0
index a67b00c..e676a8d 100644
@@ -407,6 +407,10 @@ void aio_context_setup(AioContext *ctx)
 {
 }
 
+void aio_context_destroy(AioContext *ctx)
+{
+}
+
 void aio_context_set_poll_params(AioContext *ctx, int64_t max_ns,
                                  int64_t grow, int64_t shrink, Error **errp)
 {
util/async.c +1 -0
index 4dd9d95..03f6278 100644
@@ -298,6 +298,7 @@ aio_ctx_finalize(GSource     *source)
     qemu_rec_mutex_destroy(&ctx->lock);
     qemu_lockcnt_destroy(&ctx->list_lock);
     timerlistgroup_deinit(&ctx->tlg);
+    aio_context_destroy(ctx);
 }
 
 static GSourceFuncs aio_source_funcs = {
Signature · 1 line
1.8.3.1
#1 lore raw

Hi, Peter Xu:

	If call aio_epoll_disable() here, aio_epoll_disable() will return before close ctx->epollfd,
    Because the ctx->epoll_enabled is false in the moment.
	In the process of addIOThread, aio_context_setup created epoll without call aio_epoll_try_enable,
    so ctx->epoll_enabled have no chance to set true.
Quoted 11 lines · Jie Wang
util/aio-posix.c +9 -0
+void aio_context_destroy(AioContext *ctx)
+{
+#ifdef CONFIG_EPOLL_CREATE1
+    if (ctx->epollfd >= 0) {
+        close(ctx->epollfd);
+    }
+#endif
+}
+
 void aio_context_set_poll_params(AioContext *ctx, int64_t max_ns,
                                  int64_t grow, int64_t shrink, Error **errp)
#2 lore raw
Quoted 5 lines · WangJie (Pluto)

Hi, Peter Xu:

	If call aio_epoll_disable() here, aio_epoll_disable() will return before close ctx->epollfd,
    Because the ctx->epoll_enabled is false in the moment.
	In the process of addIOThread, aio_context_setup created epoll without call aio_epoll_try_enable,
    so ctx->epoll_enabled have no chance to set true.

I see that epoll_available will only be set if epollfd != -1, so it seems to me to make more sense if we swap the two variables in aio_epoll_disable(), from current version:

static void aio_epoll_disable(AioContext *ctx)

{
    ctx->epoll_available = false;
    if (!ctx->epoll_enabled) {
        return;
    }
    ctx->epoll_enabled = false;
    close(ctx->epollfd);
}

To:

static void aio_epoll_disable(AioContext *ctx)

{
    ctx->epoll_enabled = false;
    if (!ctx->epoll_available) {
        return;
    }
    ctx->epoll_available = false;
    close(ctx->epollfd);
}

What do you think? And Fam?

Quoted 14 lines · Jie Wang
Quoted 11 lines
util/aio-posix.c +9 -0
+void aio_context_destroy(AioContext *ctx)
+{
+#ifdef CONFIG_EPOLL_CREATE1
+    if (ctx->epollfd >= 0) {
+        close(ctx->epollfd);
+    }
+#endif
+}
+
 void aio_context_set_poll_params(AioContext *ctx, int64_t max_ns,
                                  int64_t grow, int64_t shrink, Error **errp)
Signature · 1 line
Peter Xu
#3 lore raw

I agree, wait for a reply from Fam

Quoted 50 lines · Peter Xu
Quoted 5 lines · WangJie (Pluto)

Hi, Peter Xu:

	If call aio_epoll_disable() here, aio_epoll_disable() will return before close ctx->epollfd,
    Because the ctx->epoll_enabled is false in the moment.
	In the process of addIOThread, aio_context_setup created epoll without call aio_epoll_try_enable,
    so ctx->epoll_enabled have no chance to set true.

I see that epoll_available will only be set if epollfd != -1, so it seems to me to make more sense if we swap the two variables in aio_epoll_disable(), from current version:

static void aio_epoll_disable(AioContext *ctx)

{
    ctx->epoll_available = false;
    if (!ctx->epoll_enabled) {
        return;
    }
    ctx->epoll_enabled = false;
    close(ctx->epollfd);
}

To:

static void aio_epoll_disable(AioContext *ctx)

{
    ctx->epoll_enabled = false;
    if (!ctx->epoll_available) {
        return;
    }
    ctx->epoll_available = false;
    close(ctx->epollfd);
}

What do you think? And Fam?

Quoted 14 lines · Jie Wang
Quoted 11 lines
util/aio-posix.c +9 -0
+void aio_context_destroy(AioContext *ctx)
+{
+#ifdef CONFIG_EPOLL_CREATE1
+    if (ctx->epollfd >= 0) {
+        close(ctx->epollfd);
+    }
+#endif
+}
+
 void aio_context_set_poll_params(AioContext *ctx, int64_t max_ns,
                                  int64_t grow, int64_t shrink, Error **errp)
#4 lore raw
In reply to Peter Xu
Quoted 33 lines · Peter Xu
Quoted 5 lines · WangJie (Pluto)

Hi, Peter Xu:

	If call aio_epoll_disable() here, aio_epoll_disable() will return before close ctx->epollfd,
    Because the ctx->epoll_enabled is false in the moment.
	In the process of addIOThread, aio_context_setup created epoll without call aio_epoll_try_enable,
    so ctx->epoll_enabled have no chance to set true.

I see that epoll_available will only be set if epollfd != -1, so it seems to me to make more sense if we swap the two variables in aio_epoll_disable(), from current version:

static void aio_epoll_disable(AioContext *ctx)

{
    ctx->epoll_available = false;
    if (!ctx->epoll_enabled) {
        return;
    }
    ctx->epoll_enabled = false;
    close(ctx->epollfd);
}

To:

static void aio_epoll_disable(AioContext *ctx)

{
    ctx->epoll_enabled = false;
    if (!ctx->epoll_available) {
        return;
    }
    ctx->epoll_available = false;
    close(ctx->epollfd);
}
Quoted 1 line

What do you think? And Fam?

Looks good.

Fam

Quoted 18 lines
Quoted 15 lines · Jie Wang
Quoted 11 lines
util/aio-posix.c +9 -0
+void aio_context_destroy(AioContext *ctx)
+{
+#ifdef CONFIG_EPOLL_CREATE1
+    if (ctx->epollfd >= 0) {
+        close(ctx->epollfd);
+    }
+#endif
+}
+
 void aio_context_set_poll_params(AioContext *ctx, int64_t max_ns,
                                  int64_t grow, int64_t shrink, Error **errp)
Signature · 1 line
Peter Xu
#5 lore raw
In reply to Jie Wang

Hi, Peter Xu:

	If call aio_epoll_disable() in aio_context_destroy, aio_epoll_disable() will return before close(ctx->epollfd),
    Because the ctx->epoll_enabled is false in the moment.
	In the process of addIOThread, aio_context_setup created epoll without call aio_epoll_try_enable,
    so ctx->epoll_enabled have no chance to set true.
Quoted 78 lines · Jie Wang

When we call addIOThread, the epollfd created in aio_context_setup, but not close it in the process of delIOThread, so the epollfd will leak.

---
 include/block/aio.h | 8 ++++++++
 util/aio-posix.c    | 9 +++++++++
 util/aio-win32.c    | 4 ++++
 util/async.c        | 1 +
 4 files changed, 22 insertions(+)
include/block/aio.h +8 -0
index a1d6b9e..ae6f354 100644
@@ -555,6 +555,14 @@ static inline bool in_aio_context_home_thread(AioContext *ctx)
 void aio_context_setup(AioContext *ctx);
 
 /**
+ * aio_context_destroy:
+ * @ctx: the aio context
+ *
+ * Destroy the aio context.
+ */
+void aio_context_destroy(AioContext *ctx);
+
+/**
  * aio_context_set_poll_params:
  * @ctx: the aio context
  * @max_ns: how long to busy poll for, in nanoseconds
util/aio-posix.c +9 -0
index d8f0cb4..0ade2c7 100644
@@ -713,6 +713,15 @@ void aio_context_setup(AioContext *ctx)
 #endif
 }
 
+void aio_context_destroy(AioContext *ctx)
+{
+#ifdef CONFIG_EPOLL_CREATE1
+    if (ctx->epollfd >= 0) {
+        close(ctx->epollfd);
+    }
+#endif
+}
+
 void aio_context_set_poll_params(AioContext *ctx, int64_t max_ns,
                                  int64_t grow, int64_t shrink, Error **errp)
 {
util/aio-win32.c +4 -0
index a67b00c..e676a8d 100644
@@ -407,6 +407,10 @@ void aio_context_setup(AioContext *ctx)
 {
 }
 
+void aio_context_destroy(AioContext *ctx)
+{
+}
+
 void aio_context_set_poll_params(AioContext *ctx, int64_t max_ns,
                                  int64_t grow, int64_t shrink, Error **errp)
 {
util/async.c +1 -0
index 4dd9d95..03f6278 100644
@@ -298,6 +298,7 @@ aio_ctx_finalize(GSource     *source)
     qemu_rec_mutex_destroy(&ctx->lock);
     qemu_lockcnt_destroy(&ctx->list_lock);
     timerlistgroup_deinit(&ctx->tlg);
+    aio_context_destroy(ctx);
 }
 
 static GSourceFuncs aio_source_funcs = {