qemu-char: socket backend: disconnect on write error
PATCH v2 1/1 qemu-char: socket backend: disconnect on write error
From: Anton Nefedov <[email protected]>
Socket backend read handler should normally perform a disconnect, however the read handler may not get a chance to run if the frontend is not ready (qemu_chr_be_can_write() == 0).
This means that in virtio-serial frontend case if
- the host has disconnected (giving EPIPE on socket write)
- and the guest has disconnected (-> frontend not ready -> backend will not read)
- and there is still data (frontend->backend) to flush (has to be a really tricky timing but nevertheless, we have observed the case in production)
This results in virtio-serial trying to flush this data continiously forming a busy loop.
Solution: react on write error in the socket write handler. errno is not reliable after qio_channel_writev_full(), so we may not get the exact EPIPE, so disconnect on any error but QIO_CHANNEL_ERR_BLOCK which io_channel_send_full() converts to errno EAGAIN.
We must not disconnect right away though, there still may be data to read (see 4bf1cb0).
---
Changes from v1:
- we do not rely on EPIPE anynore. Socket should be closed on all errors except EAGAIN
chardev/char-socket.c | 10 ++++++++++ 1 file changed, 10 insertions(+)
chardev/char-socket.c +10 -0
@@ -97,6 +97,9 @@ static gboolean tcp_chr_accept(QIOChannel *chan,GIOCondition cond, void *opaque); +static int tcp_chr_read_poll (void *opaque); +static void tcp_chr_disconnect (CharDriverState *chr); +/* Called with chr_write_lock held. */ static int tcp_chr_write (Chardev *chr,const uint8_t *buf,int len) {
@@ -114,6 +117,13 @@ static int tcp_chr_write(Chardev *chr, const uint8_t *buf, int len) s->write_msgfds_num =0 ; } +if (ret <0 && errno !=EAGAIN ) { +if (tcp_chr_read_poll (chr) <=0 ) { +tcp_chr_disconnect (chr); +return len; + }/* else let the read handler finish it properly */ + } +return ret; }else {/* XXX: indicate an error ? */
Signature · 1 line
2.7.4
Quoted 54 lines · Denis V. Lunev
From: Anton Nefedov <[email protected]>
Socket backend read handler should normally perform a disconnect, however the read handler may not get a chance to run if the frontend is not ready (
qemu_chr_be_can_write()== 0).This means that in virtio-serial frontend case if
- the host has disconnected (giving EPIPE on socket write)
- and the guest has disconnected (-> frontend not ready -> backend will not read)
- and there is still data (frontend->backend) to flush (has to be a really tricky timing but nevertheless, we have observed the case in production)This results in virtio-serial trying to flush this data continiously forming a busy loop.
Solution: react on write error in the socket write handler. errno is not reliable after
qio_channel_writev_full(), so we may not get the exact EPIPE, so disconnect on any error butQIO_CHANNEL_ERR_BLOCKwhichio_channel_send_full()converts to errno EAGAIN.
We must not disconnect right away though, there still may be data to read (see 4bf1cb0).Signed-off-by Anton Nefedov <[email protected]>Signed-off-by Denis V. Lunev <[email protected]>CC Paolo Bonzini <[email protected]>CC Daniel P. Berrange <[email protected]>CC Marc-André Lureau <[email protected]>---
Changes from v1:
- we do not rely on EPIPE anynore. Socket should be closed on all errors except EAGAINchardev/char-socket.c | 10 ++++++++++ 1 file changed, 10 insertions(+)chardev/char-socket.c +3
-0@@ -97,6 +97,9 @@ static gboolean tcp_chr_accept(QIOChannel *chan,GIOCondition cond, void *opaque); +static int tcp_chr_read_poll (void *opaque); +static void tcp_chr_disconnect (CharDriverState *chr); +/* Called with chr_write_lock held. */ static int tcp_chr_write (Chardev *chr,const uint8_t *buf,int len) {@@ -114,6 +117,13 @@ static int tcp_chr_write(Chardev *chr, const uint8_t *buf, int len) s->write_msgfds_num =0 ; }
chardev/char-socket.c static int tcp_chr_write(Chardev *chr, const uint8_t *buf, int len) · line 129
Quoted 10 lines
chardev/char-socket.c +7
-0+if (ret <0 && errno !=EAGAIN ) { +if (tcp_chr_read_poll (chr) <=0 ) { +tcp_chr_disconnect (chr); +return len; + }/* else let the read handler finish it properly */ + } +return ret; }else {/* XXX: indicate an error ? */
pls disregard. I am very sorry. This patch was made against all version and will not compile.
Den
Hi
Quoted 52 lines · Denis V. Lunev
From: Anton Nefedov <[email protected]>
Socket backend read handler should normally perform a disconnect, however the read handler may not get a chance to run if the frontend is not ready (
qemu_chr_be_can_write()== 0).This means that in virtio-serial frontend case if
- the host has disconnected (giving EPIPE on socket write)
- and the guest has disconnected (-> frontend not ready -> backend will not read)
- and there is still data (frontend->backend) to flush (has to be a really tricky timing but nevertheless, we have observed the case in production)This results in virtio-serial trying to flush this data continiously forming
a busy loop.Solution: react on write error in the socket write handler. errno is not reliable after
qio_channel_writev_full(), so we may not get the exact EPIPE, so disconnect on any error butQIO_CHANNEL_ERR_BLOCKwhichio_channel_send_full()converts to errno EAGAIN.
We must not disconnect right away though, there still may be data to read (see 4bf1cb0).Signed-off-by Anton Nefedov <[email protected]>Signed-off-by Denis V. Lunev <[email protected]>CC Paolo Bonzini <[email protected]>CC Daniel P. Berrange <[email protected]>CC Marc-André Lureau <[email protected]>---
Changes from v1:
- we do not rely on EPIPE anynore. Socket should be closed on all errors except EAGAINchardev/char-socket.c | 10 ++++++++++ 1 file changed, 10 insertions(+)chardev/char-socket.c +3
-0@@ -97,6 +97,9 @@ static gboolean tcp_chr_accept(QIOChannel *chan,GIOCondition cond, void *opaque); +static int tcp_chr_read_poll (void *opaque); +static void tcp_chr_disconnect (CharDriverState *chr); +/* Called with chr_write_lock held. */ static int tcp_chr_write (Chardev *chr,const uint8_t *buf,int len) {
chardev/char-socket.c static int tcp_chr_write(Chardev *chr, const uint8_t · line 125
Quoted 9 lines
chardev/char-socket.c +4
-0@@ -114,6 +117,13 @@ static int tcp_chr_write(Chardev *chr, const uint8_t *buf, int len) s->write_msgfds_num =0 ; } +if (ret <0 && errno !=EAGAIN ) { +if (tcp_chr_read_poll (chr) <=0 ) { +tcp_chr_disconnect (chr); +return len;
This change breaks a number of assumption in vhost-user code. Until now, a vhost-user function assumed that dev->vhost_ops would remain as long as the function is running, so it may call vhost_ops callbacks several time, which may eventually fail to do io, but no crash would occur. The disconnection would be handled later with the HUP handler. Now, vhost_ops may be cleared during a write (chr_disconnect -> CHR_EVENT_CLOSED in net_vhost_user_event). This can be randomly reproduced with vhost-user-test -p /x86_64/vhost-user/flags-mismatch/subprocess (broken pipe, qemu crashes)
I am trying to fix this, but it isn't simple (races, many code paths etc), so I just wanted to inform you about it.
+ } /* else let the read handler finish it properly */
Quoted 10 lines
chardev/char-socket.c +2
-1+ } +return ret; }else {/* XXX: indicate an error ? */ --2.7.4
--
Marc-André Lureau
Quoted 60 lines · Marc-André Lureau
Hi
Quoted 56 lines · Denis V. Lunev
From: Anton Nefedov <[email protected]>
Socket backend read handler should normally perform a disconnect, however the read handler may not get a chance to run if the frontend is not ready (
qemu_chr_be_can_write()== 0).This means that in virtio-serial frontend case if
- the host has disconnected (giving EPIPE on socket write)
- and the guest has disconnected (-> frontend not ready -> backend will not read)
- and there is still data (frontend->backend) to flush (has to be a really tricky timing but nevertheless, we have observed the case in production)This results in virtio-serial trying to flush this data continiously forming
a busy loop.Solution: react on write error in the socket write handler. errno is not reliable after
qio_channel_writev_full(), so we may not get the exact EPIPE, so disconnect on any error butQIO_CHANNEL_ERR_BLOCKwhichio_channel_send_full()converts to errno EAGAIN.
We must not disconnect right away though, there still may be data to read (see 4bf1cb0).Signed-off-by Anton Nefedov <[email protected]>Signed-off-by Denis V. Lunev <[email protected]>CC Paolo Bonzini <[email protected]>CC Daniel P. Berrange <[email protected]>CC Marc-André Lureau <[email protected]>---
Changes from v1:
- we do not rely on EPIPE anynore. Socket should be closed on all errors except EAGAINchardev/char-socket.c | 10 ++++++++++ 1 file changed, 10 insertions(+)chardev/char-socket.c +3
-0@@ -97,6 +97,9 @@ static gboolean tcp_chr_accept(QIOChannel *chan,GIOCondition cond, void *opaque); +static int tcp_chr_read_poll (void *opaque); +static void tcp_chr_disconnect (CharDriverState *chr); +/* Called with chr_write_lock held. */ static int tcp_chr_write (Chardev *chr,const uint8_t *buf,int len) {@@ -114,6 +117,13 @@ static int tcp_chr_write(Chardev *chr, const uint8_t *buf, int len) s->write_msgfds_num =0 ; }
Quoted 14 lines
chardev/char-socket.c static int tcp_chr_write(Chardev *chr, const uint8_t *buf, int len)
Quoted 5 lines
chardev/char-socket.c +4
-0+if (ret <0 && errno !=EAGAIN ) { +if (tcp_chr_read_poll (chr) <=0 ) { +tcp_chr_disconnect (chr); +return len;This change breaks a number of assumption in vhost-user code. Until now, a vhost-user function assumed that
dev->vhost_opswould remain as long as the function is running, so it may callvhost_opscallbacks several time, which may eventually fail to do io, but no crash would occur. The disconnection would be handled later with the HUP handler. Now,vhost_opsmay be cleared during a write (chr_disconnect->CHR_EVENT_CLOSEDinnet_vhost_user_event). This can be randomly reproduced with vhost-user-test -p /x86_64/vhost-user/flags-mismatch/subprocess (broken pipe, qemu crashes)
It hangs for me with annoying frequency. I'm glad you found the culprit!
Quoted 2 lines
I am trying to fix this, but it isn't simple (races, many code paths etc), so I just wanted to inform you about it.
Can we revert this patch until we have a fix?
Hi
Quoted 83 lines · Markus Armbruster
Quoted 7 lines · Marc-André Lureau
Hi
Quoted 3 lines · Denis V. Lunev
From: Anton Nefedov <[email protected]>
Socket backend read handler should normally perform a disconnect,
however
Quoted 1 line
Quoted 1 line
the read handler may not get a chance to run if the frontend is not
ready
Quoted 7 lines
Quoted 7 lines
(
qemu_chr_be_can_write()== 0).This means that in virtio-serial frontend case if
- the host has disconnected (giving EPIPE on socket write)
- and the guest has disconnected (-> frontend not ready -> backend will not read)
- and there is still data (frontend->backend) to flush (has to be areally
Quoted 1 line
Quoted 1 line
tricky timing but nevertheless, we have observed the case in
production)
Quoted 8 lines
Quoted 8 lines
This results in virtio-serial trying to flush this data continiously forming
a busy loop.Solution: react on write error in the socket write handler. errno is not reliable after
qio_channel_writev_full(), so we may not get the exact EPIPE, so disconnect on any error butQIO_CHANNEL_ERR_BLOCKwhich
Quoted 2 lines
Quoted 2 lines
io_channel_send_full()converts to errno EAGAIN.
We must not disconnect right away though, there still may be data toread
Quoted 23 lines
Quoted 23 lines
(see 4bf1cb0).
Signed-off-by Anton Nefedov <[email protected]>Signed-off-by Denis V. Lunev <[email protected]>CC Paolo Bonzini <[email protected]>CC Daniel P. Berrange <[email protected]>CC Marc-André Lureau <[email protected]>---
Changes from v1:
- we do not rely on EPIPE anynore. Socket should be closed on all errors except EAGAINchardev/char-socket.c | 10 ++++++++++ 1 file changed, 10 insertions(+)chardev/char-socket.c +0
-0@@ -97,6 +97,9 @@ static gboolean tcp_chr_accept(QIOChannel *chan,GIOCondition cond, void *opaque);Quoted 7 lines
chardev/char-socket.c static int tcp_chr_write(Chardev *chr, const
Quoted 7 lines
chardev/char-socket.c +3
-0+static int tcp_chr_read_poll (void *opaque); +static void tcp_chr_disconnect (CharDriverState *chr); +/* Called with chr_write_lock held. */ static int tcp_chr_write (Chardev *chr,const uint8_t *buf,int len) {@@ -114,6 +117,13 @@ static int tcp_chr_write(Chardev *chr, const
uint8_tQuoted 11 lines
chardev/char-socket.c static int tcp_chr_write(Chardev *chr, const uint8_t *buf, int len)
Quoted 9 lines
chardev/char-socket.c +4
-0*buf, int len) s->write_msgfds_num =0 ; } +if (ret <0 && errno !=EAGAIN ) { +if (tcp_chr_read_poll (chr) <=0 ) { +tcp_chr_disconnect (chr); +return len;This change breaks a number of assumption in vhost-user code. Until now,
a
Quoted 1 line
vhost-user function assumed that
dev->vhost_opswould remain as long asthe
Quoted 1 line
function is running, so it may call
vhost_opscallbacks several time,which
Quoted 2 lines
may eventually fail to do io, but no crash would occur. The disconnection would be handled later with the HUP handler. Now,
vhost_opsmay be
Quoted 10 lines
cleared
Quoted 2 lines
during a write (
chr_disconnect->CHR_EVENT_CLOSEDinnet_vhost_user_event). This can be randomly reproduced withvhost-user-test
Quoted 1 line
-p /x86_64/vhost-user/flags-mismatch/subprocess (broken pipe, qemu
crashes)
It hangs for me with annoying frequency. I'm glad you found the culprit!
Yeah, when reverted, it seems it never hangs here. But the test exercices the code in interesting ways, with some unpredictable situations (device initialization while the backend disconnects/reconnects), so I wouldn't be surprised if we find more hidden issues. (vhost-user disconnect handling still isn't close to being safe)
Quoted 5 lines
I am trying to fix this, but it isn't simple (races, many code paths etc),
Quoted 1 line
so I just wanted to inform you about it.
Can we revert this patch until we have a fix?
That or disable the test for now.
But I wonder if this patch is a good solution to the problem. And I don't like the socket backend behaving differently than other backends.
Signature · 1 line
Marc-André Lureau
Quoted 7 lines · Marc-André Lureau
Quoted 7 lines
Quoted 6 lines
chardev/char-socket.c +4
-0+if (ret <0 && errno !=EAGAIN ) { +if (tcp_chr_read_poll (chr) <=0 ) { +tcp_chr_disconnect (chr); +return len;
Quoted 8 lines
Quoted 8 lines
This change breaks a number of assumption in vhost-user code. Until now, a vhost-user function assumed that
dev->vhost_opswould remain as long as the function is running, so it may callvhost_opscallbacks several time, which may eventually fail to do io, but no crash would occur. The disconnection would be handled later with the HUP handler. Now,vhost_opsmay be cleared during a write (chr_disconnect->CHR_EVENT_CLOSEDinnet_vhost_user_event). This can be randomly reproduced with vhost-user-test -p /x86_64/vhost-user/flags-mismatch/subprocess
Would it work to call tcp_chr_disconnect from an idle source (and destroy the source on the next connect)?
Paolo
Hi
Quoted 18 lines · Paolo Bonzini
Quoted 8 lines · Marc-André Lureau
Quoted 8 lines
chardev/char-socket.c static int tcp_chr_write(Chardev *chr, const uint8_t *buf, int len) · line 123
Quoted 6 lines
chardev/char-socket.c +4
-0+if (ret <0 && errno !=EAGAIN ) { +if (tcp_chr_read_poll (chr) <=0 ) { +tcp_chr_disconnect (chr); +return len;This change breaks a number of assumption in vhost-user code. Until
now, a
Quoted 1 line
Quoted 1 line
vhost-user function assumed that
dev->vhost_opswould remain as long asthe
Quoted 1 line
Quoted 1 line
function is running, so it may call
vhost_opscallbacks several time,which
Quoted 1 line
Quoted 1 line
may eventually fail to do io, but no crash would occur. The
disconnection
Quoted 9 lines
Quoted 1 line
Quoted 1 line
would be handled later with the HUP handler. Now,
vhost_opsmay becleared
Quoted 3 lines
Quoted 3 lines
during a write (
chr_disconnect->CHR_EVENT_CLOSEDinnet_vhost_user_event). This can be randomly reproduced with vhost-user-test -p /x86_64/vhost-user/flags-mismatch/subprocessWould it work to call
tcp_chr_disconnectfrom an idle source (and destroy the source on the next connect)?
Yes, I have a solution in this direction: https://da.gd/NUZf7
Note that vhost code could reach disconnect on read and segv in similar ways even if this commit was reverted. Delaying cleanup would solve that case too. Otherwise, significant work needs to be done to make vhost more robust on disconnect...
Signature · 1 line
Marc-André Lureau
Quoted 15 lines · Paolo Bonzini
Quoted 5 lines · Marc-André Lureau
Quoted 5 lines
Quoted 5 lines
chardev/char-socket.c +4
-0+if (ret <0 && errno !=EAGAIN ) { +if (tcp_chr_read_poll (chr) <=0 ) { +tcp_chr_disconnect (chr); +return len;Quoted 8 lines
Quoted 8 lines
This change breaks a number of assumption in vhost-user code. Until now, a vhost-user function assumed that
dev->vhost_opswould remain as long as the function is running, so it may callvhost_opscallbacks several time, which may eventually fail to do io, but no crash would occur. The disconnection would be handled later with the HUP handler. Now,vhost_opsmay be cleared during a write (chr_disconnect->CHR_EVENT_CLOSEDinnet_vhost_user_event). This can be randomly reproduced with vhost-user-test -p /x86_64/vhost-user/flags-mismatch/subprocess
Quoted 4 lines
Would it work to call
tcp_chr_disconnectfrom an idle source (and destroy the source on the next connect)?Paolo
I think no, but will think more on Monday. Unfortunately today is official holiday in Russia :(
Right now the code loops forever. May be we should notify the guest that the problem really happens.
Den