Skip to contentSkip to conversation

hw/char/serial: Only retry if qemu_chr_fe_write returns 0

PATCH Paolo Bonzini 4 participants 11 messages Jun 5, 2018 – Jul 16, 2018 1 message not in the archive

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

Paolo Bonzini

author Applied
#1 lore raw
Quoted 23 lines · Sergio Lopez

Only retry on serial_xmit if qemu_chr_fe_write returns 0, as this is the only recoverable error.

Retrying with any other scenario, in addition to being a waste of CPU cycles, can compromise the Guest stability if by the vCPU issuing the write and the main loop thread are, by chance or explicit pinning, running on the same pCPU.

Previous discussion:

https://lists.nongnu.org/archive/html/qemu-devel/2018-05/msg06998.html

---
 hw/char/serial.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/char/serial.c +0 -0
index 605b0d0..6de6c29 100644

hw/char/serial.c static void serial_xmit(SerialState *s) · line 266

Quoted 8 lines
hw/char/serial.c +1 -1
@@ -260,7 +260,7 @@ static void serial_xmit(SerialState *s)
         if (s->mcr & UART_MCR_LOOP) {
             /* in loopback mode, say that we just received a char */
             serial_receive1(s, &s->tsr, 1);
-        } else if (qemu_chr_fe_write(&s->chr, &s->tsr, 1) != 1 &&
+        } else if (qemu_chr_fe_write(&s->chr, &s->tsr, 1) == 0 &&
                    s->tsr_retry < MAX_XMIT_RETRY) {
             assert(s->watch_tag == 0);
             s->watch_tag =

Queued, thanks to you and all those who participated in the discussion.

Paolo

#2 lore raw

Hi

Quoted 18 lines · Paolo Bonzini
Quoted 17 lines · Sergio Lopez

Only retry on serial_xmit if qemu_chr_fe_write returns 0, as this is the only recoverable error.

Retrying with any other scenario, in addition to being a waste of CPU cycles, can compromise the Guest stability if by the vCPU issuing the write and the main loop thread are, by chance or explicit pinning, running on the same pCPU.

Previous discussion:

https://lists.nongnu.org/archive/html/qemu-devel/2018-05/msg06998.html

---
 hw/char/serial.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 16 lines
Quoted 6 lines
hw/char/serial.c +0 -0
index 605b0d0..6de6c29 100644

hw/char/serial.c static void serial_xmit(SerialState *s) · line 266

Quoted 8 lines
hw/char/serial.c +1 -1
@@ -260,7 +260,7 @@ static void serial_xmit(SerialState *s)
         if (s->mcr & UART_MCR_LOOP) {
             /* in loopback mode, say that we just received a char */
             serial_receive1(s, &s->tsr, 1);
-        } else if (qemu_chr_fe_write(&s->chr, &s->tsr, 1) != 1 &&
+        } else if (qemu_chr_fe_write(&s->chr, &s->tsr, 1) == 0 &&
                    s->tsr_retry < MAX_XMIT_RETRY) {
             assert(s->watch_tag == 0);
             s->watch_tag =

Queued, thanks to you and all those who participated in the discussion.

Shouldn't it rety if it returns -1 and the errno == EAGAIN? I am not sure when it would return 0 and a retry would make sense.

Signature · 1 line
Marc-André Lureau
#3 lore raw
Quoted 2 lines · Marc-André Lureau

Shouldn't it rety if it returns -1 and the errno == EAGAIN? I am not sure when it would return 0 and a retry would make sense.

This came up in previous discussion on an earlier version of this:
https://lists.gnu.org/archive/html/qemu-devel/2018-06/msg00171.html

qemu_chr_fe_write() should never return -1/EAGAIN, because then all of its callers need to deal with it. It should handle EAGAIN internally and report "try again later" by returning 0.

The logic is that (a) EAGAIN is a pain to handle and we should avoid expanding the set of places in QEMU that has to care about it where we can and (b) this function already has a way to say 'try again later' (ie, returning 0), so it is confusing and complicates callsites to give it a second way to say the same thing (ie, returning -1/EINTR). Better to always report "try again" in the same way.

thanks
-- PMM

#4 lore raw

Hi

Quoted 7 lines · Peter Maydell
Quoted 2 lines · Marc-André Lureau

Shouldn't it rety if it returns -1 and the errno == EAGAIN? I am not sure when it would return 0 and a retry would make sense.

This came up in previous discussion on an earlier version of this:
https://lists.gnu.org/archive/html/qemu-devel/2018-06/msg00171.html

Thanks, I lost that thread somehow

Quoted 3 lines

qemu_chr_fe_write() should never return -1/EAGAIN, because then all of its callers need to deal with it. It should handle EAGAIN internally and report "try again later" by returning 0.

But that's not the case, unless qemu_chr_write() 'write_all' arg is true.

qemu_chr_fe_write() uses false.

I suppose the patch should also change qemu_chr_fe_write() to qemu_chr_fe_write_all() to be complete and let the chardev handle EAGAIN.

Quoted 11 lines

The logic is that (a) EAGAIN is a pain to handle and we should avoid expanding the set of places in QEMU that has to care about it where we can and (b) this function already has a way to say 'try again later' (ie, returning 0), so it is confusing and complicates callsites to give it a second way to say the same thing (ie, returning -1/EINTR). Better to always report "try again" in the same way.

thanks
-- PMM

Signature · 1 line
Marc-André Lureau
#5 lore raw
Quoted 3 lines · Marc-André Lureau

I suppose the patch should also change qemu_chr_fe_write() to qemu_chr_fe_write_all() to be complete and let the chardev handle EAGAIN.

No, using qemu_chr_fe_write_all() is even more wrong, because hw/char devices should never block (and hw/char/serial.c handles flow control properly).

However, indeed it seems to me that the logic of the patch is backwards:

- -1/EAGAIN should retry

- 0 should *not* retry, because it means the other side has hung up

- everything else should not retry.

Paolo

#6 lore raw
Quoted 9 lines · Paolo Bonzini
Quoted 3 lines · Marc-André Lureau

I suppose the patch should also change qemu_chr_fe_write() to qemu_chr_fe_write_all() to be complete and let the chardev handle EAGAIN.

No, using qemu_chr_fe_write_all() is even more wrong, because hw/char devices should never block (and hw/char/serial.c handles flow control properly).

Quoted 5 lines

However, indeed it seems to me that the logic of the patch is backwards:

- -1/EAGAIN should retry

- 0 should *not* retry, because it means the other side has hung up

This seems weird, because it doesn't follow the usual pattern for non-blocking functions, where 0 just means "nothing was written" and actual error conditions like the other side having gone away are reported via -1 and some errno.

Quoted 1 line

- everything else should not retry.

thanks
-- PMM

#7 lore raw
Quoted 11 lines · Peter Maydell
Quoted 6 lines

However, indeed it seems to me that the logic of the patch is backwards:

- -1/EAGAIN should retry

- 0 should *not* retry, because it means the other side has hung up

This seems weird, because it doesn't follow the usual pattern for non-blocking functions, where 0 just means "nothing was written" and actual error conditions like the other side having gone away are reported via -1 and some errno.

Ah no, that's read(2). write(2) is as you say, so the patch should check for EAGAIN.

Paolo

#8 lore raw
In reply to Paolo Bonzini
Quoted 18 lines · On Tue, 5 Jun 2018 11:18:35 +0200 Paolo Bonzini <[email protected]> wrote:
Quoted 17 lines · Sergio Lopez

Only retry on serial_xmit if qemu_chr_fe_write returns 0, as this is the only recoverable error.

Retrying with any other scenario, in addition to being a waste of CPU cycles, can compromise the Guest stability if by the vCPU issuing the write and the main loop thread are, by chance or explicit pinning, running on the same pCPU.

Previous discussion:

https://lists.nongnu.org/archive/html/qemu-devel/2018-05/msg06998.html

---
 hw/char/serial.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 14 lines
Quoted 6 lines
hw/char/serial.c +0 -0
index 605b0d0..6de6c29 100644

hw/char/serial.c static void serial_xmit(SerialState *s) · line 266

Quoted 8 lines
hw/char/serial.c +1 -1
@@ -260,7 +260,7 @@ static void serial_xmit(SerialState *s)
         if (s->mcr & UART_MCR_LOOP) {
             /* in loopback mode, say that we just received a char */
             serial_receive1(s, &s->tsr, 1);
-        } else if (qemu_chr_fe_write(&s->chr, &s->tsr, 1) != 1 &&
+        } else if (qemu_chr_fe_write(&s->chr, &s->tsr, 1) == 0 &&
                    s->tsr_retry < MAX_XMIT_RETRY) {
             assert(s->watch_tag == 0);
             s->watch_tag =

Hi Sergio, Paolo,

it looks like commit
commit 019288bf137183bf3407c9824655b753bfafc99f

Author: Sergio Lopez <[email protected]>
Date:   Tue Jun 5 03:54:55 2018 -0400

hw/char/serial: Only retry if qemu_chr_fe_write returns 0

introduced regression wrt 2.12 and broke windows guest remote kernel debug over serial, where windbg can't connect to target and target hangs when windbg tries to connect to it.

Reverting the commit fixes issue

#9 lore raw

Hi

Quoted 42 lines · Igor Mammedov
Quoted 18 lines · On Tue, 5 Jun 2018 11:18:35 +0200 Paolo Bonzini <[email protected]> wrote:
Quoted 17 lines · Sergio Lopez

Only retry on serial_xmit if qemu_chr_fe_write returns 0, as this is the only recoverable error.

Retrying with any other scenario, in addition to being a waste of CPU cycles, can compromise the Guest stability if by the vCPU issuing the write and the main loop thread are, by chance or explicit pinning, running on the same pCPU.

Previous discussion:

https://lists.nongnu.org/archive/html/qemu-devel/2018-05/msg06998.html

---
 hw/char/serial.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 14 lines
Quoted 6 lines
hw/char/serial.c +0 -0
index 605b0d0..6de6c29 100644

hw/char/serial.c static void serial_xmit(SerialState *s) · line 266

Quoted 8 lines
hw/char/serial.c +1 -1
@@ -260,7 +260,7 @@ static void serial_xmit(SerialState *s)
         if (s->mcr & UART_MCR_LOOP) {
             /* in loopback mode, say that we just received a char */
             serial_receive1(s, &s->tsr, 1);
-        } else if (qemu_chr_fe_write(&s->chr, &s->tsr, 1) != 1 &&
+        } else if (qemu_chr_fe_write(&s->chr, &s->tsr, 1) == 0 &&
                    s->tsr_retry < MAX_XMIT_RETRY) {
             assert(s->watch_tag == 0);
             s->watch_tag =

Hi Sergio, Paolo,

it looks like commit
commit 019288bf137183bf3407c9824655b753bfafc99f

Author: Sergio Lopez <[email protected]>
Date:   Tue Jun 5 03:54:55 2018 -0400
Quoted 8 lines

hw/char/serial: Only retry if qemu_chr_fe_write returns 0

introduced regression wrt 2.12 and broke windows guest remote kernel debug over serial, where windbg can't connect to target and target hangs when windbg tries to connect to it.

Reverting the commit fixes issue

is this a possible solution?

hw/char/serial.c +14 -9
index cd7d747c68..046c4684ff 100644
@@ -261,15 +261,20 @@ static void serial_xmit(SerialState *s)
         if (s->mcr & UART_MCR_LOOP) {
             /* in loopback mode, say that we just received a char */
             serial_receive1(s, &s->tsr, 1);
-        } else if (qemu_chr_fe_write(&s->chr, &s->tsr, 1) == 0 &&
-                   s->tsr_retry < MAX_XMIT_RETRY) {
-            assert(s->watch_tag == 0);
-            s->watch_tag =
-                qemu_chr_fe_add_watch(&s->chr, G_IO_OUT | G_IO_HUP,
-                                      serial_watch_cb, s);
-            if (s->watch_tag > 0) {
-                s->tsr_retry++;
-                return;
+        } else {
+            int rc = qemu_chr_fe_write(&s->chr, &s->tsr, 1);
+
+            if ((rc == 0 ||
+                 (rc == -1 && (errno == EAGAIN || errno == EINTR))) &&
+                s->tsr_retry < MAX_XMIT_RETRY) {
+                assert(s->watch_tag == 0);
+                s->watch_tag =
+                    qemu_chr_fe_add_watch(&s->chr, G_IO_OUT | G_IO_HUP,
+                                          serial_watch_cb, s);
+                if (s->watch_tag > 0) {
+                    s->tsr_retry++;
+                    return;
+                }
             }
         }
         s->tsr_retry = 0;
Signature · 1 line
Marc-André Lureau
#10 lore raw
Quoted 5 lines · Marc-André Lureau
hw/char/serial.c +4 -1
-                return;
+        } else {
+            int rc = qemu_chr_fe_write(&s->chr, &s->tsr, 1);
+
+            if ((rc == 0 ||

hw/char/serial.c static void serial_xmit(SerialState *s) · line 277

Quoted 10 lines
hw/char/serial.c +10 -0
+                 (rc == -1 && (errno == EAGAIN || errno == EINTR))) &&
+                s->tsr_retry < MAX_XMIT_RETRY) {
+                assert(s->watch_tag == 0);
+                s->watch_tag =
+                    qemu_chr_fe_add_watch(&s->chr, G_IO_OUT | G_IO_HUP,
+                                          serial_watch_cb, s);
+                if (s->watch_tag > 0) {
+                    s->tsr_retry++;
+                    return;
+                }

I think EINTR should be handled by chardev, otherwise yes.

Paolo

#11 lore raw
In reply to Marc-André Lureau
Quoted 47 lines · On Fri, 13 Jul 2018 16:55:15 +0200 Marc-André Lureau…

Hi

Quoted 44 lines · Igor Mammedov
Quoted 18 lines · On Tue, 5 Jun 2018 11:18:35 +0200 Paolo Bonzini <[email protected]> wrote:
Quoted 17 lines · Sergio Lopez

Only retry on serial_xmit if qemu_chr_fe_write returns 0, as this is the only recoverable error.

Retrying with any other scenario, in addition to being a waste of CPU cycles, can compromise the Guest stability if by the vCPU issuing the write and the main loop thread are, by chance or explicit pinning, running on the same pCPU.

Previous discussion:

https://lists.nongnu.org/archive/html/qemu-devel/2018-05/msg06998.html

---
 hw/char/serial.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 14 lines
Quoted 6 lines
hw/char/serial.c +0 -0
index 605b0d0..6de6c29 100644

hw/char/serial.c static void serial_xmit(SerialState *s) · line 266

Quoted 8 lines
hw/char/serial.c +1 -1
@@ -260,7 +260,7 @@ static void serial_xmit(SerialState *s)
         if (s->mcr & UART_MCR_LOOP) {
             /* in loopback mode, say that we just received a char */
             serial_receive1(s, &s->tsr, 1);
-        } else if (qemu_chr_fe_write(&s->chr, &s->tsr, 1) != 1 &&
+        } else if (qemu_chr_fe_write(&s->chr, &s->tsr, 1) == 0 &&
                    s->tsr_retry < MAX_XMIT_RETRY) {
             assert(s->watch_tag == 0);
             s->watch_tag =

Hi Sergio, Paolo,

it looks like commit
commit 019288bf137183bf3407c9824655b753bfafc99f

Author: Sergio Lopez <[email protected]>
Date:   Tue Jun 5 03:54:55 2018 -0400

hw/char/serial: Only retry if qemu_chr_fe_write returns 0

Quoted 8 lines
Quoted 6 lines

introduced regression wrt 2.12 and broke windows guest remote kernel debug over serial, where windbg can't connect to target and target hangs when windbg tries to connect to it.

Reverting the commit fixes issue

is this a possible solution?

it fixes windbg over serial (with and without EINTR)

Quoted 36 lines
hw/char/serial.c +14 -9
index cd7d747c68..046c4684ff 100644
@@ -261,15 +261,20 @@ static void serial_xmit(SerialState *s)
         if (s->mcr & UART_MCR_LOOP) {
             /* in loopback mode, say that we just received a char */
             serial_receive1(s, &s->tsr, 1);
-        } else if (qemu_chr_fe_write(&s->chr, &s->tsr, 1) == 0 &&
-                   s->tsr_retry < MAX_XMIT_RETRY) {
-            assert(s->watch_tag == 0);
-            s->watch_tag =
-                qemu_chr_fe_add_watch(&s->chr, G_IO_OUT | G_IO_HUP,
-                                      serial_watch_cb, s);
-            if (s->watch_tag > 0) {
-                s->tsr_retry++;
-                return;
+        } else {
+            int rc = qemu_chr_fe_write(&s->chr, &s->tsr, 1);
+
+            if ((rc == 0 ||
+                 (rc == -1 && (errno == EAGAIN || errno == EINTR))) &&
+                s->tsr_retry < MAX_XMIT_RETRY) {
+                assert(s->watch_tag == 0);
+                s->watch_tag =
+                    qemu_chr_fe_add_watch(&s->chr, G_IO_OUT | G_IO_HUP,
+                                          serial_watch_cb, s);
+                if (s->watch_tag > 0) {
+                    s->tsr_retry++;
+                    return;
+                }
             }
         }
         s->tsr_retry = 0;