Skip to contentSkip to conversation

nbd/server: do not poll within a coroutine context

PATCH Superseded Eric Blake 2 participants 3 messages Apr 4, 2024 – Apr 5, 2024

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Eric Blake

author
#0 lore raw

PATCH v3 1/1 nbd/server: do not poll within a coroutine context

From: Zhu Yangyang <[email protected]>

Coroutines are not supposed to block. Instead, they should yield.

The client performs TLS upgrade outside of an AIOContext, during synchronous handshake; this still requires g_main_loop. But the server responds to TLS upgrade inside a coroutine, so a nested g_main_loop is wrong. Since the two callbacks no longer share more than the setting of data.complete and data.error, it's just as easy to use static helpers instead of trying to share a common code path.

Fixes f95910f ("nbd: implement TLS support in the protocol negotiation")
[eblake: move callbacks to their use point]

---

After looking at this more, I'm less convinced that there is enough common code here to even be worth trying to share in common.c. This takes the essence of the v2 patch, but refactors it a bit.

v2: https://lists.gnu.org/archive/html/qemu-devel/2024-04/msg00019.html

 nbd/nbd-internal.h | 20 ++++++++++----------
 nbd/client.c       | 21 +++++++++++++++++----
 nbd/common.c       | 11 -----------
 nbd/server.c       | 21 ++++++++++++++++-----
 4 files changed, 43 insertions(+), 30 deletions(-)
nbd/nbd-internal.h +10 -10
index dfa02f77ee4..087c6bfc002 100644
@@ -63,6 +63,16 @@
 #define NBD_SET_TIMEOUT             _IO(0xab, 9)
 #define NBD_SET_FLAGS               _IO(0xab, 10)
 
+/* Used in NBD_OPT_STARTTLS handling */
+struct NBDTLSHandshakeData {
+    bool complete;
+    Error *error;
+    union {
+        GMainLoop *loop;
+        Coroutine *co;
+    } u;
+};
+
 /* nbd_write
  * Writes @size bytes to @ioc. Returns 0 on success.
  */
@@ -72,16 +82,6 @@ static inline int nbd_write(QIOChannel *ioc, const void *buffer, size_t size,
     return qio_channel_write_all(ioc, buffer, size, errp) < 0 ? -EIO : 0;
 }
 
-struct NBDTLSHandshakeData {
-    GMainLoop *loop;
-    bool complete;
-    Error *error;
-};
-
-
-void nbd_tls_handshake(QIOTask *task,
-                       void *opaque);
-
 int nbd_drop(QIOChannel *ioc, size_t size, Error **errp);
 
 #endif
nbd/client.c +17 -4
index 29ffc609a4b..c9dc5265404 100644
@@ -596,6 +596,18 @@ static int nbd_request_simple_option(QIOChannel *ioc, int opt, bool strict,
     return 1;
 }
 
+/* Callback to learn when QIO TLS upgrade is complete */
+static void nbd_client_tls_handshake(QIOTask *task, void *opaque)
+{
+    struct NBDTLSHandshakeData *data = opaque;
+
+    qio_task_propagate_error(task, &data->error);
+    data->complete = true;
+    if (data->u.loop) {
+        g_main_loop_quit(data->u.loop);
+    }
+}
+
 static QIOChannel *nbd_receive_starttls(QIOChannel *ioc,
                                         QCryptoTLSCreds *tlscreds,
                                         const char *hostname, Error **errp)
@@ -619,18 +631,19 @@ static QIOChannel *nbd_receive_starttls(QIOChannel *ioc,
         return NULL;
     }
     qio_channel_set_name(QIO_CHANNEL(tioc), "nbd-client-tls");
-    data.loop = g_main_loop_new(g_main_context_default(), FALSE);
     trace_nbd_receive_starttls_tls_handshake();
     qio_channel_tls_handshake(tioc,
-                              nbd_tls_handshake,
+                              nbd_client_tls_handshake,
                               &data,
                               NULL,
                               NULL);
 
     if (!data.complete) {
-        g_main_loop_run(data.loop);
+        data.u.loop = g_main_loop_new(g_main_context_default(), FALSE);
+        g_main_loop_run(data.u.loop);
+        g_main_loop_unref(data.u.loop);
     }
-    g_main_loop_unref(data.loop);
+
     if (data.error) {
         error_propagate(errp, data.error);
         object_unref(OBJECT(tioc));
nbd/common.c +0 -11
index 3247c1d618a..589a748cfe6 100644
@@ -47,17 +47,6 @@ int nbd_drop(QIOChannel *ioc, size_t size, Error **errp)
 }
 
 
-void nbd_tls_handshake(QIOTask *task,
-                       void *opaque)
-{
-    struct NBDTLSHandshakeData *data = opaque;
-
-    qio_task_propagate_error(task, &data->error);
-    data->complete = true;
-    g_main_loop_quit(data->loop);
-}
-
-
 const char *nbd_opt_lookup(uint32_t opt)
 {
     switch (opt) {
nbd/server.c +16 -5
index c3484cc1ebc..d16726a6326 100644
@@ -748,6 +748,17 @@ static int nbd_negotiate_handle_info(NBDClient *client, Error **errp)
     return rc;
 }
 
+/* Callback to learn when QIO TLS upgrade is complete */
+static void nbd_server_tls_handshake(QIOTask *task, void *opaque)
+{
+    struct NBDTLSHandshakeData *data = opaque;
+
+    qio_task_propagate_error(task, &data->error);
+    data->complete = true;
+    if (!qemu_coroutine_entered(data->u.co)) {
+        aio_co_wake(data->u.co);
+    }
+}
 
 /* Handle NBD_OPT_STARTTLS. Return NULL to drop connection, or else the
  * new channel for all further (now-encrypted) communication. */
@@ -777,17 +788,17 @@ static QIOChannel *nbd_negotiate_handle_starttls(NBDClient *client,
 
     qio_channel_set_name(QIO_CHANNEL(tioc), "nbd-server-tls");
     trace_nbd_negotiate_handle_starttls_handshake();
-    data.loop = g_main_loop_new(g_main_context_default(), FALSE);
+    data.u.co = qemu_coroutine_self();
     qio_channel_tls_handshake(tioc,
-                              nbd_tls_handshake,
+                              nbd_server_tls_handshake,
                               &data,
                               NULL,
                               NULL);
 
-    if (!data.complete) {
-        g_main_loop_run(data.loop);
+    while (!data.complete) {
+        qemu_coroutine_yield();
     }
-    g_main_loop_unref(data.loop);
+
     if (data.error) {
         object_unref(OBJECT(tioc));
         error_propagate(errp, data.error);
Signature · 1 line
2.44.0
#1 lore raw
Quoted 15 lines · Eric Blake

From: Zhu Yangyang <[email protected]>

Coroutines are not supposed to block. Instead, they should yield.

The client performs TLS upgrade outside of an AIOContext, during synchronous handshake; this still requires g_main_loop. But the server responds to TLS upgrade inside a coroutine, so a nested g_main_loop is wrong. Since the two callbacks no longer share more than the setting of data.complete and data.error, it's just as easy to use static helpers instead of trying to share a common code path.

Fixes f95910f ("nbd: implement TLS support in the protocol negotiation")
[eblake: move callbacks to their use point]
Quoted 5 lines

---

After looking at this more, I'm less convinced that there is enough common code here to even be worth trying to share in common.c. This takes the essence of the v2 patch, but refactors it a bit.

Maybe, do the complete split, and make separate structure definitions in client.c and server.c, and don't make shared NBDTLSHandshakeData with union? Finally, it's just a simple opaque-structure for static callback function, seems good to keep it in .c as well.

Quoted 161 lines

v2: https://lists.gnu.org/archive/html/qemu-devel/2024-04/msg00019.html

  nbd/nbd-internal.h | 20 ++++++++++----------
  nbd/client.c       | 21 +++++++++++++++++----
  nbd/common.c       | 11 -----------
  nbd/server.c       | 21 ++++++++++++++++-----
  4 files changed, 43 insertions(+), 30 deletions(-)
nbd/nbd-internal.h +10 -10
index dfa02f77ee4..087c6bfc002 100644
@@ -63,6 +63,16 @@
  #define NBD_SET_TIMEOUT             _IO(0xab, 9)
  #define NBD_SET_FLAGS               _IO(0xab, 10)
 
+/* Used in NBD_OPT_STARTTLS handling */
+struct NBDTLSHandshakeData {
+    bool complete;
+    Error *error;
+    union {
+        GMainLoop *loop;
+        Coroutine *co;
+    } u;
+};
+
  /* nbd_write
   * Writes @size bytes to @ioc. Returns 0 on success.
   */
@@ -72,16 +82,6 @@ static inline int nbd_write(QIOChannel *ioc, const void *buffer, size_t size,
      return qio_channel_write_all(ioc, buffer, size, errp) < 0 ? -EIO : 0;
  }
 
-struct NBDTLSHandshakeData {
-    GMainLoop *loop;
-    bool complete;
-    Error *error;
-};
-
-
-void nbd_tls_handshake(QIOTask *task,
-                       void *opaque);
-
  int nbd_drop(QIOChannel *ioc, size_t size, Error **errp);
 
  #endif
nbd/client.c +17 -4
index 29ffc609a4b..c9dc5265404 100644
@@ -596,6 +596,18 @@ static int nbd_request_simple_option(QIOChannel *ioc, int opt, bool strict,
      return 1;
  }
 
+/* Callback to learn when QIO TLS upgrade is complete */
+static void nbd_client_tls_handshake(QIOTask *task, void *opaque)
+{
+    struct NBDTLSHandshakeData *data = opaque;
+
+    qio_task_propagate_error(task, &data->error);
+    data->complete = true;
+    if (data->u.loop) {
+        g_main_loop_quit(data->u.loop);
+    }
+}
+
  static QIOChannel *nbd_receive_starttls(QIOChannel *ioc,
                                          QCryptoTLSCreds *tlscreds,
                                          const char *hostname, Error **errp)
@@ -619,18 +631,19 @@ static QIOChannel *nbd_receive_starttls(QIOChannel *ioc,
          return NULL;
      }
      qio_channel_set_name(QIO_CHANNEL(tioc), "nbd-client-tls");
-    data.loop = g_main_loop_new(g_main_context_default(), FALSE);
      trace_nbd_receive_starttls_tls_handshake();
      qio_channel_tls_handshake(tioc,
-                              nbd_tls_handshake,
+                              nbd_client_tls_handshake,
                                &data,
                                NULL,
                                NULL);
 
      if (!data.complete) {
-        g_main_loop_run(data.loop);
+        data.u.loop = g_main_loop_new(g_main_context_default(), FALSE);
+        g_main_loop_run(data.u.loop);
+        g_main_loop_unref(data.u.loop);
      }
-    g_main_loop_unref(data.loop);
+
      if (data.error) {
          error_propagate(errp, data.error);
          object_unref(OBJECT(tioc));
nbd/common.c +0 -11
index 3247c1d618a..589a748cfe6 100644
@@ -47,17 +47,6 @@ int nbd_drop(QIOChannel *ioc, size_t size, Error **errp)
  }
 
 
-void nbd_tls_handshake(QIOTask *task,
-                       void *opaque)
-{
-    struct NBDTLSHandshakeData *data = opaque;
-
-    qio_task_propagate_error(task, &data->error);
-    data->complete = true;
-    g_main_loop_quit(data->loop);
-}
-
-
  const char *nbd_opt_lookup(uint32_t opt)
  {
      switch (opt) {
nbd/server.c +16 -5
index c3484cc1ebc..d16726a6326 100644
@@ -748,6 +748,17 @@ static int nbd_negotiate_handle_info(NBDClient *client, Error **errp)
      return rc;
  }
 
+/* Callback to learn when QIO TLS upgrade is complete */
+static void nbd_server_tls_handshake(QIOTask *task, void *opaque)
+{
+    struct NBDTLSHandshakeData *data = opaque;
+
+    qio_task_propagate_error(task, &data->error);
+    data->complete = true;
+    if (!qemu_coroutine_entered(data->u.co)) {
+        aio_co_wake(data->u.co);
+    }
+}
 
  /* Handle NBD_OPT_STARTTLS. Return NULL to drop connection, or else the
   * new channel for all further (now-encrypted) communication. */
@@ -777,17 +788,17 @@ static QIOChannel *nbd_negotiate_handle_starttls(NBDClient *client,
 
      qio_channel_set_name(QIO_CHANNEL(tioc), "nbd-server-tls");
      trace_nbd_negotiate_handle_starttls_handshake();
-    data.loop = g_main_loop_new(g_main_context_default(), FALSE);
+    data.u.co = qemu_coroutine_self();
      qio_channel_tls_handshake(tioc,
-                              nbd_tls_handshake,
+                              nbd_server_tls_handshake,
                                &data,
                                NULL,
                                NULL);
 
-    if (!data.complete) {
-        g_main_loop_run(data.loop);
+    while (!data.complete) {
+        qemu_coroutine_yield();
      }
-    g_main_loop_unref(data.loop);
+
      if (data.error) {
          object_unref(OBJECT(tioc));
          error_propagate(errp, data.error);
Signature · 2 lines
Best regards,
Vladimir
author
#2 lore raw
Quoted 18 lines · Vladimir Sementsov-Ogievskiy
Quoted 17 lines · Eric Blake

From: Zhu Yangyang <[email protected]>

Coroutines are not supposed to block. Instead, they should yield.

The client performs TLS upgrade outside of an AIOContext, during synchronous handshake; this still requires g_main_loop. But the server responds to TLS upgrade inside a coroutine, so a nested g_main_loop is wrong. Since the two callbacks no longer share more than the setting of data.complete and data.error, it's just as easy to use static helpers instead of trying to share a common code path.

Fixes f95910f ("nbd: implement TLS support in the protocol negotiation")
[eblake: move callbacks to their use point]

---

Quoted 5 lines
Quoted 3 lines

After looking at this more, I'm less convinced that there is enough common code here to even be worth trying to share in common.c. This takes the essence of the v2 patch, but refactors it a bit.

Maybe, do the complete split, and make separate structure definitions in client.c and server.c, and don't make shared NBDTLSHandshakeData with union? Finally, it's just a simple opaque-structure for static callback function, seems good to keep it in .c as well.

Sure, v4 coming up along those lines.

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.
Virtualization:  qemu.org | libguestfs.org