Skip to contentSkip to conversation

nbd: wait all read/write requests finished when shutdowning nbd socket

PATCH In review Wen Congyang 2 participants 4 messages Sep 16, 2015

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 nbd: wait all read/write requests finished when shutdowning nbd socket

If the socket fd is shutdown, there may be some data which is received before shutdown. We will read the data and do read/write in nbd_trip(). But the exp's blk is NULL, and it will cause qemu crashed.

---
 nbd.c | 9 +++++++++
 1 file changed, 9 insertions(+)
nbd.c +9 -0
index 06b501b..d8586a1 100644
@@ -1132,6 +1132,7 @@ void nbd_export_close(NBDExport *exp)
     nbd_export_set_name(exp, NULL);
     nbd_export_put(exp);
     if (exp->blk) {
+        bdrv_drain(blk_bs(exp->blk));
         blk_remove_aio_context_notifier(exp->blk, blk_aio_attached,
                                         blk_aio_detach, exp);
         blk_unref(exp->blk);
@@ -1305,6 +1306,14 @@ static void nbd_trip(void *opaque)
         goto invalid_request;
     }
 
+    if (client->closing) {
+        /*
+         * The client may be closed when we are blocked in
+         * nbd_co_receive_request()
+         */
+        goto done;
+    }
+
     switch (command) {
     case NBD_CMD_READ:
         TRACE("Request type is READ");
Signature · 1 line
2.4.3
#1 lore raw
Quoted 15 lines · Wen Congyang

If the socket fd is shutdown, there may be some data which is received before shutdown. We will read the data and do read/write in nbd_trip(). But the exp's blk is NULL, and it will cause qemu crashed.

---
 nbd.c | 9 +++++++++
 1 file changed, 9 insertions(+)
nbd.c +0 -0
index 06b501b..d8586a1 100644

nbd.c void nbd_export_close(NBDExport *exp) · line 1138

Quoted 7 lines
nbd.c +1 -0
@@ -1132,6 +1132,7 @@ void nbd_export_close(NBDExport *exp)
     nbd_export_set_name(exp, NULL);
     nbd_export_put(exp);
     if (exp->blk) {
+        bdrv_drain(blk_bs(exp->blk));
         blk_remove_aio_context_notifier(exp->blk, blk_aio_attached,
                                         blk_aio_detach, exp);
         blk_unref(exp->blk);

I think a better fix is to move the whole "if" to nbd_export_put. In fact, nbd_export_close is wrong because exp can be freed by nbd_export_close's call to nbd_export_put.

nbd.c static void nbd_trip(void *opaque) · line 1315

Quoted 11 lines
nbd.c +7 -0
@@ -1305,6 +1306,14 @@ static void nbd_trip(void *opaque)
         goto invalid_request;
     }
 
+    if (client->closing) {
+        /*
+         * The client may be closed when we are blocked in
+         * nbd_co_receive_request()
+         */
+        goto done;
+    }

This is okay.

Paolo

Quoted 4 lines
nbd.c +0 -0
     switch (command) {
     case NBD_CMD_READ:
         TRACE("Request type is READ");
#2 lore raw
Quoted 13 lines · Paolo Bonzini
Quoted 10 lines · Wen Congyang

If the socket fd is shutdown, there may be some data which is received before shutdown. We will read the data and do read/write in nbd_trip(). But the exp's blk is NULL, and it will cause qemu crashed.

---
 nbd.c | 9 +++++++++
 1 file changed, 9 insertions(+)
Quoted 16 lines

nbd.c void nbd_export_close(NBDExport *exp) · line 1138

Quoted 12 lines
nbd.c +1 -0
index 06b501b..d8586a1 100644
@@ -1132,6 +1132,7 @@ void nbd_export_close(NBDExport *exp)
     nbd_export_set_name(exp, NULL);
     nbd_export_put(exp);
     if (exp->blk) {
+        bdrv_drain(blk_bs(exp->blk));
         blk_remove_aio_context_notifier(exp->blk, blk_aio_attached,
                                         blk_aio_detach, exp);
         blk_unref(exp->blk);

I think a better fix is to move the whole "if" to nbd_export_put. In fact, nbd_export_close is wrong because exp can be freed by nbd_export_close's call to nbd_export_put.

OK, I will fix it in the next version.

Thanks
Wen Congyang

Quoted 23 lines

nbd.c static void nbd_trip(void *opaque) · line 1315

Quoted 12 lines
nbd.c +7 -0
@@ -1305,6 +1306,14 @@ static void nbd_trip(void *opaque)
         goto invalid_request;
     }
 
+    if (client->closing) {
+        /*
+         * The client may be closed when we are blocked in
+         * nbd_co_receive_request()
+         */
+        goto done;
+    }

This is okay.

Paolo

nbd.c static void nbd_trip(void *opaque) · line 1319

Quoted 4 lines
nbd.c +0 -0
     switch (command) {
     case NBD_CMD_READ:
         TRACE("Request type is READ");

.

#3 lore raw
Quoted 10 lines · Wen Congyang
Quoted 9 lines
Quoted 9 lines
Quoted 5 lines
Quoted 5 lines
     if (exp->blk) {
+        bdrv_drain(blk_bs(exp->blk));
         blk_remove_aio_context_notifier(exp->blk, blk_aio_attached,
                                         blk_aio_detach, exp);
         blk_unref(exp->blk);

I think a better fix is to move the whole "if" to nbd_export_put. In fact, nbd_export_close is wrong because exp can be freed by nbd_export_close's call to nbd_export_put.

OK, I will fix it in the next version.

To be clear, the bug is pre-existing (commit 38b54b6, "nbd: use BlockDriverState refcnt", 2013-08-23).

Paolo