Skip to contentSkip to conversation

char: restore read callback on a reattached (hotplug) chardev

PATCH Superseded Gal Hammer 2 participants 5 messages Dec 15, 2013 – Dec 18, 2013

v4 of 6 Superseded Latest is v6 →

lore
Share & tools
lore

Gal Hammer

author
#0 lore raw

PATCH v4 1/1 char: restore read callback on a reattached (hotplug) chardev

Fix a bug that was introduced in commit 386a5a1e. A removal of a device set the chr handlers to NULL. However when the device is plugged back, its read callback is not restored so data can't be transferred from the host to the guest (e.g. via the virtio-serial port).

https://bugzilla.redhat.com/show_bug.cgi?id=1027181

---
 qemu-char.c | 17 +++++++++++++++--
 1 file changed, 15 insertions(+), 2 deletions(-)

V4: - Same as V3, but this time done right.

V3: - fix a typo in comment.
- move the revision history after the "signed-off-by" tag.

V2: - do not call chr_update_read_handler on device removal.
- add asserts to verify chr_update_read_handler is not called with an assigned fd_in_tag to prevent fd leaks.
- update fd and udp backends' chr_update_read_handler function so it won't remove fd_in to prevent a double release.

qemu-char.c +15 -2
index e00f84c..69649f0 100644
@@ -213,7 +213,7 @@ void qemu_chr_add_handlers(CharDriverState *s,
     s->chr_read = fd_read;
     s->chr_event = fd_event;
     s->handler_opaque = opaque;
-    if (s->chr_update_read_handler)
+    if (fe_open && s->chr_update_read_handler)
         s->chr_update_read_handler(s);
 
     if (!s->explicit_fe_open) {
@@ -870,6 +870,7 @@ static void fd_chr_update_read_handler(CharDriverState *chr)
 {
     FDCharDriver *s = chr->opaque;
 
+    assert(!chr->fd_in_tag);
     remove_fd_in_watch(chr);
     if (s->fd_in) {
         chr->fd_in_tag = io_add_watch_poll(s->fd_in, fd_chr_read_poll,
@@ -2228,7 +2229,7 @@ static void udp_chr_update_read_handler(CharDriverState *chr)
 {
     NetCharDriver *s = chr->opaque;
 
-    remove_fd_in_watch(chr);
+    assert(!chr->fd_in_tag);
     if (s->chan) {
         chr->fd_in_tag = io_add_watch_poll(s->chan, udp_chr_read_poll,
                                            udp_chr_read, chr);
@@ -2510,6 +2511,17 @@ static void tcp_chr_connect(void *opaque)
     qemu_chr_be_generic_open(chr);
 }
 
+static void tcp_chr_update_read_handler(CharDriverState *chr)
+{
+    TCPCharDriver *s = chr->opaque;
+
+    assert(!chr->fd_in_tag);
+    if (s->chan && !chr->fd_in_tag) {
+        chr->fd_in_tag = io_add_watch_poll(s->chan, tcp_chr_read_poll,
+                                           tcp_chr_read, chr);
+    }
+}
+
 #define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
 static void tcp_chr_telnet_init(int fd)
 {
@@ -2665,6 +2677,7 @@ static CharDriverState *qemu_chr_open_socket_fd(int fd, bool do_nodelay,
     chr->get_msgfd = tcp_get_msgfd;
     chr->chr_add_client = tcp_chr_add_client;
     chr->chr_add_watch = tcp_chr_add_watch;
+    chr->chr_update_read_handler = tcp_chr_update_read_handler;
     /* be isn't opened until we get a connection */
     chr->explicit_be_open = true;
 
Signature · 1 line
1.8.1.4
#1 lore raw
Quoted 15 lines · Gal Hammer

Fix a bug that was introduced in commit 386a5a1e. A removal of a device set the chr handlers to NULL. However when the device is plugged back, its read callback is not restored so data can't be transferred from the host to the guest (e.g. via the virtio-serial port).

https://bugzilla.redhat.com/show_bug.cgi?id=1027181

---
 qemu-char.c | 17 +++++++++++++++--
 1 file changed, 15 insertions(+), 2 deletions(-)

V4: - Same as V3, but this time done right.

Quoted 8 lines

V3: - fix a typo in comment.
- move the revision history after the "signed-off-by" tag.

V2: - do not call chr_update_read_handler on device removal.
- add asserts to verify chr_update_read_handler is not called with an assigned fd_in_tag to prevent fd leaks.
- update fd and udp backends' chr_update_read_handler function so it won't remove fd_in to prevent a double release.

Looks like you missed the pty backend. Can't blame you -- this chardev stuff is really messy. pty is doing is own handling + polling
+ HUP detection, which really is equally applicable to tcp and udp as well.

More below.

Quoted 22 lines
qemu-char.c +2 -1
index e00f84c..69649f0 100644
@@ -213,7 +213,7 @@ void qemu_chr_add_handlers(CharDriverState *s,
     s->chr_read = fd_read;
     s->chr_event = fd_event;
     s->handler_opaque = opaque;
-    if (s->chr_update_read_handler)
+    if (fe_open && s->chr_update_read_handler)
         s->chr_update_read_handler(s);
 
     if (!s->explicit_fe_open) {
@@ -870,6 +870,7 @@ static void fd_chr_update_read_handler(CharDriverState *chr)
 {
     FDCharDriver *s = chr->opaque;
 
+    assert(!chr->fd_in_tag);
     remove_fd_in_watch(chr);
     if (s->fd_in) {
         chr->fd_in_tag = io_add_watch_poll(s->fd_in, fd_chr_read_poll,

qemu-char.c static void udp_chr_update_read_handler(CharDriverState *chr) · line 2232

Quoted 5 lines
qemu-char.c +1 -1
@@ -2228,7 +2229,7 @@ static void udp_chr_update_read_handler(CharDriverState *chr)
 {
     NetCharDriver *s = chr->opaque;
 
-    remove_fd_in_watch(chr);
+    assert(!chr->fd_in_tag);

You're removing remove_fd_in_watch() here but not in fd_chr_update_read_handler() above. Any particular reason?

Also, this assert would be a risky thing in pty, since it has its own HUP detection method. A common way to deal with all this would be nice, but chardevs are hacks upon hacks already, and this fixes a regression, so I'm fine with applying the patch in this form now.

Please update for pty.

Gerd, can you please look at this and pick it up? I'm going to be away till Jan.

Thanks,

Amit

author
#2 lore raw
Quoted 25 lines · Amit Shah
Quoted 15 lines · Gal Hammer

Fix a bug that was introduced in commit 386a5a1e. A removal of a device set the chr handlers to NULL. However when the device is plugged back, its read callback is not restored so data can't be transferred from the host to the guest (e.g. via the virtio-serial port).

https://bugzilla.redhat.com/show_bug.cgi?id=1027181

---
  qemu-char.c | 17 +++++++++++++++--
  1 file changed, 15 insertions(+), 2 deletions(-)

V4: - Same as V3, but this time done right.

Quoted 8 lines

V3: - fix a typo in comment.
- move the revision history after the "signed-off-by" tag.

V2: - do not call chr_update_read_handler on device removal.
- add asserts to verify chr_update_read_handler is not called with an assigned fd_in_tag to prevent fd leaks.
- update fd and udp backends' chr_update_read_handler function so it won't remove fd_in to prevent a double release.

Quoted 4 lines

Looks like you missed the pty backend. Can't blame you -- this chardev stuff is really messy. pty is doing is own handling + polling
+ HUP detection, which really is equally applicable to tcp and udp as well.

As far as I could tell the pty backend doesn't suffer from this issue. That's why I didn't change anything there.

Quoted 27 lines

More below.

Quoted 25 lines
qemu-char.c +2 -1
index e00f84c..69649f0 100644
@@ -213,7 +213,7 @@ void qemu_chr_add_handlers(CharDriverState *s,
      s->chr_read = fd_read;
      s->chr_event = fd_event;
      s->handler_opaque = opaque;
-    if (s->chr_update_read_handler)
+    if (fe_open && s->chr_update_read_handler)
          s->chr_update_read_handler(s);
 
      if (!s->explicit_fe_open) {
@@ -870,6 +870,7 @@ static void fd_chr_update_read_handler(CharDriverState *chr)
  {
      FDCharDriver *s = chr->opaque;
 
+    assert(!chr->fd_in_tag);
      remove_fd_in_watch(chr);
      if (s->fd_in) {
          chr->fd_in_tag = io_add_watch_poll(s->fd_in, fd_chr_read_poll,
@@ -2228,7 +2229,7 @@ static void udp_chr_update_read_handler(CharDriverState *chr)
  {
      NetCharDriver *s = chr->opaque;
 
Quoted 5 lines

qemu-char.c static void udp_chr_update_read_handler(CharDriverState *chr) · line 2232

Quoted 2 lines
qemu-char.c +1 -1
-    remove_fd_in_watch(chr);
+    assert(!chr->fd_in_tag);

You're removing remove_fd_in_watch() here but not in fd_chr_update_read_handler() above. Any particular reason?

I missed it while "commit -p" the changes. Fixed.

Quoted 5 lines

Also, this assert would be a risky thing in pty, since it has its own HUP detection method. A common way to deal with all this would be nice, but chardevs are hacks upon hacks already, and this fixes a regression, so I'm fine with applying the patch in this form now.

Quoted 9 lines

Please update for pty.

Gerd, can you please look at this and pick it up? I'm going to be away till Jan.

Thanks,

Amit

Gal.

#3 lore raw
Quoted 26 lines · Gal Hammer
Quoted 25 lines · Amit Shah
Quoted 23 lines · Gal Hammer

Fix a bug that was introduced in commit 386a5a1e. A removal of a device set the chr handlers to NULL. However when the device is plugged back, its read callback is not restored so data can't be transferred from the host to the guest (e.g. via the virtio-serial port).

https://bugzilla.redhat.com/show_bug.cgi?id=1027181

---
 qemu-char.c | 17 +++++++++++++++--
 1 file changed, 15 insertions(+), 2 deletions(-)

V4: - Same as V3, but this time done right.

V3: - fix a typo in comment.
- move the revision history after the "signed-off-by" tag.

V2: - do not call chr_update_read_handler on device removal.
- add asserts to verify chr_update_read_handler is not called with an assigned fd_in_tag to prevent fd leaks.
- update fd and udp backends' chr_update_read_handler function so it won't remove fd_in to prevent a double release.

Quoted 7 lines
Quoted 4 lines

Looks like you missed the pty backend. Can't blame you -- this chardev stuff is really messy. pty is doing is own handling + polling
+ HUP detection, which really is equally applicable to tcp and udp as well.

As far as I could tell the pty backend doesn't suffer from this issue. That's why I didn't change anything there.

pty_chr_update_read_handler() calls pty_chr_state(), which calls remove_fd_in_watch().

Amit

author
#4 lore raw
Quoted 5 lines · Amit Shah
Quoted 2 lines

As far as I could tell the pty backend doesn't suffer from this issue. That's why I didn't change anything there.

pty_chr_update_read_handler() calls pty_chr_state(), which calls remove_fd_in_watch().

Yes, but the pty_chr_update_read_handler() isn't called on device removal so remove_fd_in_watch() is not executed.

However I did find that there was a problem in the pty backend that I missed and I'll post an updated patch.

Thanks,

Gal.