vhost-user race condition on shutdown
cover vhost-user race condition on shutdown
From: Dan Streetman <[email protected]>
This is a race condition between the normal shutdown of a guest and the handling of its vhost-user net being externally closed. It's explained in more detail at the bug link; the short version is that there are 2 problems, fixed by the 2 patches. The first patch fixes the race condition where multiple threads call vhost_net_stop(), and the second patch prevents vhost-user from calling vhost_net_cleanup() on CHR_EVENT_CLOSED, because it will be cleaned up later and its fields will be accessed when vhost_net_stop() is called later.
As explained in the bug report, this requires a rather complicated setup to reproduce, and I'm not able to create a setup to reproduce it myself. However this has been reported to me/Canonical, and the reporter is able to reproduce it consistently, so I've used them for debug and testing. This reproduction was done with the older 2.5 qemu, from Ubuntu Xenial; but the problem does still appear to exist in upstream qemu, based on review of the code, which is why I'm sending these patches.
Dan Streetman (2):
add VirtIONet vhost_stopped flag to prevent multiple stops do not call vhost_net_cleanup() on running net from char user event
hw/net/virtio-net.c | 3 ++- include/hw/virtio/virtio-net.h | 1 + net/vhost-user.c | 1 - 3 files changed, 3 insertions(+), 2 deletions(-)
Signature · 1 line
2.20.1
PATCH 1/2 add VirtIONet vhost_stopped flag to prevent multiple stops
From: Dan Streetman <[email protected]>
There is a race condition when using the vhost-user driver, between a guest shutdown and the vhost-user interface being closed. This is explained in more detail at the bug link above; the short explanation is the vhost-user device can be closed while the main thread is in the middle of stopping the vhost_net. In this case, the main thread handling shutdown will enter virtio_net_vhost_status() and move into the n->vhost_started (else) block, and call vhost_net_stop(); while it is running that function, another thread is notified that the vhost-user device has been closed, and (indirectly) calls into virtio_net_vhost_status() also. Since the vhost_net status hasn't yet changed, the second thread also enters the n->vhost_started block, and also calls vhost_net_stop(). This causes problems for the second thread when it tries to stop the network that's already been stopped.
This adds a flag to the struct that's atomically set to prevent more than one thread from calling vhost_net_stop(). The atomic_fetch_inc() is likely overkill and probably could be done with a simple check-and-set, but since it's a race condition there would still be a (very, very) small window without using an atomic to set it.
--- hw/net/virtio-net.c | 3 ++- include/hw/virtio/virtio-net.h | 1 + 2 files changed, 3 insertions(+), 1 deletion(-)
hw/net/virtio-net.c +2 -1
@@ -13,6 +13,7 @@#include "qemu/osdep.h" #include "qemu/iov.h" +#include "qemu/atomic.h" #include "hw/virtio/virtio.h" #include "net/net.h" #include "net/checksum.h"
@@ -240,7 +241,7 @@ static void virtio_net_vhost_status(VirtIONet *n, uint8_t status) "falling back on userspace virtio", -r); n->vhost_started =0 ; } - }else { + }else if (atomic_fetch_inc (&n->vhost_stopped) ==0 ) {vhost_net_stop (vdev, n->nic->ncs, queues); n->vhost_started =0 ; }
include/hw/virtio/virtio-net.h +1 -0
@@ -164,6 +164,7 @@ struct VirtIONet {uint8_t nouni;uint8_t nobcast;uint8_t vhost_started; +int vhost_stopped;struct {uint32_t in_use;uint32_t first_multi;
Signature · 1 line
2.20.1
Re: [Qemu-devel] [PATCH 1/2] add VirtIONet vhost_stopped flag to prevent multiple stops
Quoted 18 lines · Dan Streetman
From: Dan Streetman <[email protected]>
There is a race condition when using the vhost-user driver, between a guest shutdown and the vhost-user interface being closed. This is explained in more detail at the bug link above; the short explanation is the vhost-user device can be closed while the main thread is in the middle of stopping the
vhost_net. In this case, the main thread handling shutdown will entervirtio_net_vhost_status()and move into then->vhost_started(else) block, and callvhost_net_stop(); while it is running that function, another thread is notified that the vhost-user device has been closed, and (indirectly) calls intovirtio_net_vhost_status()also. Since thevhost_netstatus hasn't yet changed, the second thread also enters then->vhost_startedblock, and also callsvhost_net_stop(). This causes problems for the second thread when it tries to stop the network that's already been stopped.
Quoted 5 lines
This adds a flag to the struct that's atomically set to prevent more than one thread from calling
vhost_net_stop(). Theatomic_fetch_inc()is likely overkill and probably could be done with a simple check-and-set, but since it's a race condition there would still be a (very, very) small window without using an atomic to set it.
How? Isn't all this under the BQL?
Quoted 33 lines
Signed-off-by Dan Streetman <[email protected]>--- hw/net/virtio-net.c | 3 ++- include/hw/virtio/virtio-net.h | 1 + 2 files changed, 3 insertions(+), 1 deletion(-)hw/net/virtio-net.c +2
-1@@ -13,6 +13,7 @@#include "qemu/osdep.h" #include "qemu/iov.h" +#include "qemu/atomic.h" #include "hw/virtio/virtio.h" #include "net/net.h" #include "net/checksum.h" @@ -240,7 +241,7 @@ static void virtio_net_vhost_status(VirtIONet *n, uint8_t status) "falling back on userspace virtio", -r); n->vhost_started =0 ; } - }else { + }else if (atomic_fetch_inc (&n->vhost_stopped) ==0 ) {vhost_net_stop (vdev, n->nic->ncs, queues); n->vhost_started =0 ; }include/hw/virtio/virtio-net.h +0
-0
include/hw/virtio/virtio-net.h struct VirtIONet { · line 170
Quoted 7 lines
include/hw/virtio/virtio-net.h +1
-0@@ -164,6 +164,7 @@ struct VirtIONet {uint8_t nouni;uint8_t nobcast;uint8_t vhost_started; +int vhost_stopped;struct {uint32_t in_use;uint32_t first_multi;
OK questions same as any state:
- do we need to migrate this?
- reset it on device reset?
Quoted 2 lines
Signature · 1 line
2.20.1
Re: [Qemu-devel] [PATCH 1/2] add VirtIONet vhost_stopped flag to prevent multiple stops
Quoted 20 lines · Michael S. Tsirkin
Quoted 18 lines · Dan Streetman
From: Dan Streetman <[email protected]>
There is a race condition when using the vhost-user driver, between a guest shutdown and the vhost-user interface being closed. This is explained in more detail at the bug link above; the short explanation is the vhost-user device can be closed while the main thread is in the middle of stopping the
vhost_net. In this case, the main thread handling shutdown will entervirtio_net_vhost_status()and move into then->vhost_started(else) block, and callvhost_net_stop(); while it is running that function, another thread is notified that the vhost-user device has been closed, and (indirectly) calls intovirtio_net_vhost_status()also. Since thevhost_netstatus hasn't yet changed, the second thread also enters then->vhost_startedblock, and also callsvhost_net_stop(). This causes problems for the second thread when it tries to stop the network that's already been stopped.
Quoted 7 lines
Quoted 5 lines
This adds a flag to the struct that's atomically set to prevent more than one thread from calling
vhost_net_stop(). Theatomic_fetch_inc()is likely overkill and probably could be done with a simple check-and-set, but since it's a race condition there would still be a (very, very) small window without using an atomic to set it.How? Isn't all this under the BQL?
I don't think so, although I'm not deeply familiar with the code. Note the code path listed in my last email, run from aio_bh_schedule_oneshot() - does that hold the bql while running?
Quoted 49 lines
Quoted 34 lines
Signed-off-by Dan Streetman <[email protected]>--- hw/net/virtio-net.c | 3 ++- include/hw/virtio/virtio-net.h | 1 + 2 files changed, 3 insertions(+), 1 deletion(-)hw/net/virtio-net.c +2
-1@@ -13,6 +13,7 @@#include "qemu/osdep.h" #include "qemu/iov.h" +#include "qemu/atomic.h" #include "hw/virtio/virtio.h" #include "net/net.h" #include "net/checksum.h" @@ -240,7 +241,7 @@ static void virtio_net_vhost_status(VirtIONet *n, uint8_t status) "falling back on userspace virtio", -r); n->vhost_started =0 ; } - }else { + }else if (atomic_fetch_inc (&n->vhost_stopped) ==0 ) {vhost_net_stop (vdev, n->nic->ncs, queues); n->vhost_started =0 ; }include/hw/virtio/virtio-net.h +0
-0include/hw/virtio/virtio-net.h struct VirtIONet { · line 170
Quoted 7 lines
include/hw/virtio/virtio-net.h +1
-0@@ -164,6 +164,7 @@ struct VirtIONet {uint8_t nouni;uint8_t nobcast;uint8_t vhost_started; +int vhost_stopped;struct {uint32_t in_use;uint32_t first_multi;OK questions same as any state:
- do we need to migrate this?
- reset it on device reset?Quoted 2 lines
--
2.20.1
Re: [Qemu-devel] [PATCH 1/2] add VirtIONet vhost_stopped flag to prevent multiple stops
In reply to Dan StreetmanQuoted 4 lines · Dan Streetman
From: Dan Streetman <[email protected]>
Quoted 9 lines
There is a race condition when using the vhost-user driver, between a guest shutdown and the vhost-user interface being closed. This is explained in more detail at the bug link above; the short explanation is the vhost-user device can be closed while the main thread is in the middle of stopping the
vhost_net. In this case, the main thread handling shutdown will entervirtio_net_vhost_status()and move into then->vhost_started(else) block, and callvhost_net_stop(); while it is running that function, another thread is notified that the vhost-user device has been closed, and (indirectly) calls intovirtio_net_vhost_status()also.
I think we need figure out why there are multiple vhost_net_stop() calls simultaneously. E.g vhost-user register fd handlers like:
qemu_chr_fe_set_handlers(&s->chr, NULL, NULL, net_vhost_user_event, NULL, nc0->name, NULL, true);
which uses default main context, so it should only be called only in main thread.
Thanks
Quoted 51 lines
Since the
vhost_netstatus hasn't yet changed, the second thread also enters then->vhost_startedblock, and also callsvhost_net_stop(). This causes problems for the second thread when it tries to stop the network that's already been stopped.This adds a flag to the struct that's atomically set to prevent more than one thread from calling
vhost_net_stop(). Theatomic_fetch_inc()is likely overkill and probably could be done with a simple check-and-set, but since it's a race condition there would still be a (very, very) small window without using an atomic to set it.Signed-off-by Dan Streetman <[email protected]>--- hw/net/virtio-net.c | 3 ++- include/hw/virtio/virtio-net.h | 1 + 2 files changed, 3 insertions(+), 1 deletion(-)hw/net/virtio-net.c +2
-1@@ -13,6 +13,7 @@#include "qemu/osdep.h" #include "qemu/iov.h" +#include "qemu/atomic.h" #include "hw/virtio/virtio.h" #include "net/net.h" #include "net/checksum.h" @@ -240,7 +241,7 @@ static void virtio_net_vhost_status(VirtIONet *n, uint8_t status) "falling back on userspace virtio", -r); n->vhost_started =0 ; } - }else { + }else if (atomic_fetch_inc (&n->vhost_stopped) ==0 ) {vhost_net_stop (vdev, n->nic->ncs, queues); n->vhost_started =0 ; }include/hw/virtio/virtio-net.h +1
-0@@ -164,6 +164,7 @@ struct VirtIONet {uint8_t nouni;uint8_t nobcast;uint8_t vhost_started; +int vhost_stopped;struct {uint32_t in_use;uint32_t first_multi;
Re: [Qemu-devel] [PATCH 1/2] add VirtIONet vhost_stopped flag to prevent multiple stops
Quoted 21 lines · Jason Wang
Quoted 4 lines · Dan Streetman
From: Dan Streetman <[email protected]>
Quoted 9 lines
There is a race condition when using the vhost-user driver, between a guest shutdown and the vhost-user interface being closed. This is explained in more detail at the bug link above; the short explanation is the vhost-user device can be closed while the main thread is in the middle of stopping the
vhost_net. In this case, the main thread handling shutdown will entervirtio_net_vhost_status()and move into then->vhost_started(else) block, and callvhost_net_stop(); while it is running that function, another thread is notified that the vhost-user device has been closed, and (indirectly) calls intovirtio_net_vhost_status()also.I think we need figure out why there are multiple
vhost_net_stop()calls simultaneously. E.g vhost-user register fd handlers like:
Quoted 7 lines
qemu_chr_fe_set_handlers(&s->chr, NULL, NULL, net_vhost_user_event, NULL, nc0->name, NULL, true);which uses default main context, so it should only be called only in main thread.
net_vhost_user_event() schedules chr_closed_bh() to do its bottom half work; does aio_bh_schedule_oneshot() execute its events from the main thread?
For reference, the call chain is:
chr_closed_bh()
qmp_set_link()
nc->info->link_status_changed() -> virtio_net_set_link_status()
virtio_net_set_status()
virtio_net_vhost_status()Quoted 55 lines
Thanks
Quoted 51 lines
Since the
vhost_netstatus hasn't yet changed, the second thread also enters then->vhost_startedblock, and also callsvhost_net_stop(). This causes problems for the second thread when it tries to stop the network that's already been stopped.This adds a flag to the struct that's atomically set to prevent more than one thread from calling
vhost_net_stop(). Theatomic_fetch_inc()is likely overkill and probably could be done with a simple check-and-set, but since it's a race condition there would still be a (very, very) small window without using an atomic to set it.Signed-off-by Dan Streetman <[email protected]>--- hw/net/virtio-net.c | 3 ++- include/hw/virtio/virtio-net.h | 1 + 2 files changed, 3 insertions(+), 1 deletion(-)hw/net/virtio-net.c +2
-1@@ -13,6 +13,7 @@#include "qemu/osdep.h" #include "qemu/iov.h" +#include "qemu/atomic.h" #include "hw/virtio/virtio.h" #include "net/net.h" #include "net/checksum.h" @@ -240,7 +241,7 @@ static void virtio_net_vhost_status(VirtIONet *n, uint8_t status) "falling back on userspace virtio", -r); n->vhost_started =0 ; } - }else { + }else if (atomic_fetch_inc (&n->vhost_stopped) ==0 ) {vhost_net_stop (vdev, n->nic->ncs, queues); n->vhost_started =0 ; }include/hw/virtio/virtio-net.h +1
-0@@ -164,6 +164,7 @@ struct VirtIONet {uint8_t nouni;uint8_t nobcast;uint8_t vhost_started; +int vhost_stopped;struct {uint32_t in_use;uint32_t first_multi;
Re: [Qemu-devel] [PATCH 1/2] add VirtIONet vhost_stopped flag to prevent multiple stops
Quoted 25 lines · Dan Streetman
Quoted 24 lines · Jason Wang
Quoted 4 lines · Dan Streetman
From: Dan Streetman <[email protected]>
Quoted 9 lines
There is a race condition when using the vhost-user driver, between a guest shutdown and the vhost-user interface being closed. This is explained in more detail at the bug link above; the short explanation is the vhost-user device can be closed while the main thread is in the middle of stopping the
vhost_net. In this case, the main thread handling shutdown will entervirtio_net_vhost_status()and move into then->vhost_started(else) block, and callvhost_net_stop(); while it is running that function, another thread is notified that the vhost-user device has been closed, and (indirectly) calls intovirtio_net_vhost_status()also.I think we need figure out why there are multiple
vhost_net_stop()calls simultaneously. E.g vhost-user register fd handlers like:qemu_chr_fe_set_handlers(&s->chr, NULL, NULL, net_vhost_user_event, NULL, nc0->name, NULL, true);
Quoted 5 lines
Quoted 2 lines
which uses default main context, so it should only be called only in main thread.
net_vhost_user_event()scheduleschr_closed_bh()to do its bottom half work; doesaio_bh_schedule_oneshot()execute its events from the main thread?
I think so if net_vhost_user_event() was called in main thread (it calls qemu_get_current_aio_context()).
Quoted 8 lines
For reference, the call chain is:
chr_closed_bh()
qmp_set_link()nc->info->link_status_changed() -> virtio_net_set_link_status() virtio_net_set_status() virtio_net_vhost_status()
The code was added by Marc since:
commit e7c83a885f865128ae3cf1946f8cb538b63cbfba
Author: Marc-André Lureau <[email protected]> Date: Mon Feb 27 14:49:56 2017 +0400
vhost-user: delay vhost_user_stop
Cc him for more thoughts.
Thanks
Quoted 54 lines
Quoted 54 lines
Thanks
Quoted 51 lines
Since the
vhost_netstatus hasn't yet changed, the second thread also enters then->vhost_startedblock, and also callsvhost_net_stop(). This causes problems for the second thread when it tries to stop the network that's already been stopped.This adds a flag to the struct that's atomically set to prevent more than one thread from calling
vhost_net_stop(). Theatomic_fetch_inc()is likely overkill and probably could be done with a simple check-and-set, but since it's a race condition there would still be a (very, very) small window without using an atomic to set it.Signed-off-by Dan Streetman <[email protected]>--- hw/net/virtio-net.c | 3 ++- include/hw/virtio/virtio-net.h | 1 + 2 files changed, 3 insertions(+), 1 deletion(-)hw/net/virtio-net.c +2
-1@@ -13,6 +13,7 @@#include "qemu/osdep.h" #include "qemu/iov.h" +#include "qemu/atomic.h" #include "hw/virtio/virtio.h" #include "net/net.h" #include "net/checksum.h" @@ -240,7 +241,7 @@ static void virtio_net_vhost_status(VirtIONet *n, uint8_t status) "falling back on userspace virtio", -r); n->vhost_started =0 ; } - }else { + }else if (atomic_fetch_inc (&n->vhost_stopped) ==0 ) {vhost_net_stop (vdev, n->nic->ncs, queues); n->vhost_started =0 ; }include/hw/virtio/virtio-net.h +1
-0@@ -164,6 +164,7 @@ struct VirtIONet {uint8_t nouni;uint8_t nobcast;uint8_t vhost_started; +int vhost_stopped;struct {uint32_t in_use;uint32_t first_multi;
Re: [Qemu-devel] [PATCH 1/2] add VirtIONet vhost_stopped flag to prevent multiple stops
Quoted 30 lines · Jason Wang
Quoted 27 lines · Dan Streetman
Quoted 26 lines · Jason Wang
Quoted 4 lines · Dan Streetman
From: Dan Streetman <[email protected]>
Quoted 9 lines
There is a race condition when using the vhost-user driver, between a guest shutdown and the vhost-user interface being closed. This is explained in more detail at the bug link above; the short explanation is the vhost-user device can be closed while the main thread is in the middle of stopping the
vhost_net. In this case, the main thread handling shutdown will entervirtio_net_vhost_status()and move into then->vhost_started(else) block, and callvhost_net_stop(); while it is running that function, another thread is notified that the vhost-user device has been closed, and (indirectly) calls intovirtio_net_vhost_status()also.I think we need figure out why there are multiple
vhost_net_stop()calls simultaneously. E.g vhost-user register fd handlers like:qemu_chr_fe_set_handlers(&s->chr, NULL, NULL, net_vhost_user_event, NULL, nc0->name, NULL, true);which uses default main context, so it should only be called only in main thread.
Quoted 7 lines
Quoted 3 lines
net_vhost_user_event()scheduleschr_closed_bh()to do its bottom half work; doesaio_bh_schedule_oneshot()execute its events from the main thread?I think so if
net_vhost_user_event()was called in main thread (it callsqemu_get_current_aio_context()).
ok, I'll check that, thanks!
I think my other patch, to remove the vhost_user_stop() call completely from the net_vhost_user_event() handler for CHR_EVENT_CLOSED, is still relevant; do you have thoughts on that?
Quoted 79 lines
Quoted 10 lines
For reference, the call chain is:
chr_closed_bh()
qmp_set_link()nc->info->link_status_changed() -> virtio_net_set_link_status() virtio_net_set_status() virtio_net_vhost_status()The code was added by Marc since:
commit
e7c83a885f865128ae3cf1946f8cb538b63cbfbaAuthor: Marc-André Lureau <[email protected]> Date: Mon Feb 27 14:49:56 2017 +0400vhost-user: delay
vhost_user_stopCc him for more thoughts.
Thanks
Quoted 54 lines
Quoted 54 lines
Thanks
Quoted 51 lines
Since the
vhost_netstatus hasn't yet changed, the second thread also enters then->vhost_startedblock, and also callsvhost_net_stop(). This causes problems for the second thread when it tries to stop the network that's already been stopped.This adds a flag to the struct that's atomically set to prevent more than one thread from calling
vhost_net_stop(). Theatomic_fetch_inc()is likely overkill and probably could be done with a simple check-and-set, but since it's a race condition there would still be a (very, very) small window without using an atomic to set it.Signed-off-by Dan Streetman <[email protected]>--- hw/net/virtio-net.c | 3 ++- include/hw/virtio/virtio-net.h | 1 + 2 files changed, 3 insertions(+), 1 deletion(-)hw/net/virtio-net.c +2
-1@@ -13,6 +13,7 @@#include "qemu/osdep.h" #include "qemu/iov.h" +#include "qemu/atomic.h" #include "hw/virtio/virtio.h" #include "net/net.h" #include "net/checksum.h" @@ -240,7 +241,7 @@ static void virtio_net_vhost_status(VirtIONet *n, uint8_t status) "falling back on userspace virtio", -r); n->vhost_started =0 ; } - }else { + }else if (atomic_fetch_inc (&n->vhost_stopped) ==0 ) {vhost_net_stop (vdev, n->nic->ncs, queues); n->vhost_started =0 ; }include/hw/virtio/virtio-net.h +1
-0@@ -164,6 +164,7 @@ struct VirtIONet {uint8_t nouni;uint8_t nobcast;uint8_t vhost_started; +int vhost_stopped;struct {uint32_t in_use;uint32_t first_multi;
Re: [Qemu-devel] [PATCH 1/2] add VirtIONet vhost_stopped flag to prevent multiple stops
Quoted 8 lines · Dan Streetman
Quoted 2 lines
I think so if
net_vhost_user_event()was called in main thread (it callsqemu_get_current_aio_context()).ok, I'll check that, thanks!
I think my other patch, to remove the
vhost_user_stop()call completely from thenet_vhost_user_event()handler forCHR_EVENT_CLOSED, is still relevant; do you have thoughts on that?
I think that patch makes sense.
Thanks
PATCH 2/2 do not call vhost_net_cleanup() on running net from char user event
In reply to Dan StreetmanFrom: Dan Streetman <[email protected]>
Currently, a user CHR_EVENT_CLOSED event will cause net_vhost_user_event() to call vhost_user_cleanup(), which calls vhost_net_cleanup() for all its queues. However, vhost_net_cleanup() must never be called like this for fully-initialized nets; when other code later calls vhost_net_stop() - such as from virtio_net_vhost_status() - it will try to access the already-cleaned-up fields and fail with assertion errors or segfaults.
The vhost_net_cleanup() will eventually be called from qemu_cleanup_net_client().
--- net/vhost-user.c | 1 - 1 file changed, 1 deletion(-)
net/vhost-user.c +0 -1
@@ -236,7 +236,6 @@ static void chr_closed_bh(void *opaque) s =DO_UPCAST (NetVhostUserState, nc, ncs[0 ]);qmp_set_link (name, false, &err); -vhost_user_stop (queues, ncs);qemu_chr_fe_set_handlers (&s->chr,NULL ,NULL , net_vhost_user_event,NULL , opaque,NULL , true);
Signature · 1 line
2.20.1
Quoted 3 lines · Dan Streetman
From: Dan Streetman <[email protected]>
Cc Maxime.
Quoted 30 lines
This is a race condition between the normal shutdown of a guest and the handling of its vhost-user net being externally closed. It's explained in more detail at the bug link; the short version is that there are 2 problems, fixed by the 2 patches. The first patch fixes the race condition where multiple threads call
vhost_net_stop(), and the second patch prevents vhost-user from callingvhost_net_cleanup()onCHR_EVENT_CLOSED, because it will be cleaned up later and its fields will be accessed whenvhost_net_stop()is called later.As explained in the bug report, this requires a rather complicated setup to reproduce, and I'm not able to create a setup to reproduce it myself. However this has been reported to me/Canonical, and the reporter is able to reproduce it consistently, so I've used them for debug and testing. This reproduction was done with the older 2.5 qemu, from Ubuntu Xenial; but the problem does still appear to exist in upstream qemu, based on review of the code, which is why I'm sending these patches.
Dan Streetman (2):
add VirtIONetvhost_stoppedflag to prevent multiple stops do not callvhost_net_cleanup()on running net from char user eventhw/net/virtio-net.c | 3 ++- include/hw/virtio/virtio-net.h | 1 + net/vhost-user.c | 1 - 3 files changed, 3 insertions(+), 2 deletions(-)Signature · 1 line
2.20.1