Skip to contentSkip to conversation

nbd: fix assert() on qemu-nbd stop

PATCH In review Denis V. Lunev 3 participants 5 messages Apr 14, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 nbd: fix assert() on qemu-nbd stop

From: Pavel Butsykin <[email protected]>

Quoted 1 line

From time to time qemu-nbd is crashing on the following assert:

    assert(state == TERMINATING);
    nbd_export_closed
    nbd_export_put
    main

and the state at the moment of the crash is evaluated to TERMINATE.

During shutdown process of the client the nbd_client_thread thread sends SIGTERM signal and the main thread calls the nbd_client_closed callback. If the SIGTERM callback will be executed after change the state to TERMINATING, then the state will once again be TERMINATE.

To solve the issue, we must change the state to TERMINATE only if the state is RUNNING. In the other case we are shutting down already.

---
 qemu-nbd.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
qemu-nbd.c +1 -1
index ca4a724..956013f 100644
@@ -213,7 +213,7 @@ static int find_partition(BlockBackend *blk, int partition,
 
 static void termsig_handler(int signum)
 {
-    state = TERMINATE;
+    atomic_cmpxchg(&state, RUNNING, TERMINATE);
     qemu_notify_event();
 }
 
Signature · 1 line
2.1.4
#1 lore raw
Quoted 29 lines · Denis V. Lunev

From: Pavel Butsykin <[email protected]>

From time to time qemu-nbd is crashing on the following assert:

    assert(state == TERMINATING);
    nbd_export_closed
    nbd_export_put
    main

and the state at the moment of the crash is evaluated to TERMINATE.

During shutdown process of the client the nbd_client_thread thread sends SIGTERM signal and the main thread calls the nbd_client_closed callback. If the SIGTERM callback will be executed after change the state to TERMINATING, then the state will once again be TERMINATE.

To solve the issue, we must change the state to TERMINATE only if the state is RUNNING. In the other case we are shutting down already.

---
 qemu-nbd.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
qemu-nbd.c +0 -0
index ca4a724..956013f 100644

qemu-nbd.c static int find_partition(BlockBackend *blk, int partition, · line 216

Quoted 5 lines
qemu-nbd.c +1 -1
@@ -213,7 +213,7 @@ static int find_partition(BlockBackend *blk, int partition,
 
 static void termsig_handler(int signum)
 {
-    state = TERMINATE;
+    atomic_cmpxchg(&state, RUNNING, TERMINATE);

Just a simple "if" is enough (the rest of the file is not using any of atomic{mb_,}{read,set}.

I'm not able to send further pull requests for 2.6, can the block maintainers help?

Paolo

Quoted 4 lines
qemu-nbd.c +0 -0
     qemu_notify_event();
 }
 
#2 lore raw
Quoted 39 lines · Paolo Bonzini
Quoted 29 lines · Denis V. Lunev

From: Pavel Butsykin <[email protected]>

From time to time qemu-nbd is crashing on the following assert:

     assert(state == TERMINATING);
     nbd_export_closed
     nbd_export_put
     main

and the state at the moment of the crash is evaluated to TERMINATE.

During shutdown process of the client the nbd_client_thread thread sends SIGTERM signal and the main thread calls the nbd_client_closed callback. If the SIGTERM callback will be executed after change the state to TERMINATING, then the state will once again be TERMINATE.

To solve the issue, we must change the state to TERMINATE only if the state is RUNNING. In the other case we are shutting down already.

---
  qemu-nbd.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
qemu-nbd.c +0 -0
index ca4a724..956013f 100644

qemu-nbd.c static int find_partition(BlockBackend *blk, int partition, · line 216

Quoted 5 lines
qemu-nbd.c +1 -1
@@ -213,7 +213,7 @@ static int find_partition(BlockBackend *blk, int partition,
 
  static void termsig_handler(int signum)
  {
-    state = TERMINATE;
+    atomic_cmpxchg(&state, RUNNING, TERMINATE);

Just a simple "if" is enough (the rest of the file is not using any of atomic{mb_,}{read,set}.

Quoted 4 lines

I'm not able to send further pull requests for 2.6, can the block maintainers help?

Paolo

unfortunately, if () would be not enough. The race is with different thread which can run on the another CPU. Thus this OP should be atomic or some locking is required.

Den

P.S. Added more block guys...

#3 lore raw
Quoted 41 lines · Denis V. Lunev
Quoted 40 lines · Paolo Bonzini
Quoted 30 lines · Denis V. Lunev

From: Pavel Butsykin <[email protected]>

From time to time qemu-nbd is crashing on the following assert:

     assert(state == TERMINATING);
     nbd_export_closed
     nbd_export_put
     main

and the state at the moment of the crash is evaluated to TERMINATE.

During shutdown process of the client the nbd_client_thread thread sends SIGTERM signal and the main thread calls the nbd_client_closed callback. If the SIGTERM callback will be executed after change the state to TERMINATING, then the state will once again be TERMINATE.

To solve the issue, we must change the state to TERMINATE only if the state
is RUNNING. In the other case we are shutting down already.

---
  qemu-nbd.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
qemu-nbd.c +0 -0
index ca4a724..956013f 100644

qemu-nbd.c static int find_partition(BlockBackend *blk, int · line 216

Quoted 5 lines
qemu-nbd.c +1 -1
@@ -213,7 +213,7 @@ static int find_partition(BlockBackend *blk, int
 partition,
    static void termsig_handler(int signum)
  {
-    state = TERMINATE;
+    atomic_cmpxchg(&state, RUNNING, TERMINATE);

Just a simple "if" is enough (the rest of the file is not using any of atomic{mb_,}{read,set}.

Quoted 8 lines
Quoted 4 lines

I'm not able to send further pull requests for 2.6, can the block maintainers help?

Paolo

unfortunately, if () would be not enough. The race is with different thread which can run on the another CPU. Thus this OP should be atomic or some locking is required.

Oh, qemu-nbd doesn't use qemu_thread_create (qemu_thread_create ensures that the sigmask is all-blocked except in the main thread)!

Patch is okay then (though we certainly want to revisit it in 2.7).

Paolo

Quoted 6 lines

Den

P.S. Added more block guys...

#4 lore raw
In reply to Denis V. Lunev
Quoted 17 lines · Denis V. Lunev

From: Pavel Butsykin <[email protected]>

From time to time qemu-nbd is crashing on the following assert:

    assert(state == TERMINATING);
    nbd_export_closed
    nbd_export_put
    main

and the state at the moment of the crash is evaluated to TERMINATE.

During shutdown process of the client the nbd_client_thread thread sends SIGTERM signal and the main thread calls the nbd_client_closed callback. If the SIGTERM callback will be executed after change the state to TERMINATING, then the state will once again be TERMINATE.

To solve the issue, we must change the state to TERMINATE only if the state is RUNNING. In the other case we are shutting down already.

Quoted 6 lines
---
 qemu-nbd.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Thanks Pavel and Denis, I have applied the patch to my block tree:

https://github.com/XanClic/qemu/commits/block

Max