Skip to contentSkip to conversation

chardev/char-socket: Properly make qio connections non blocking

PATCH Superseded Sai Pavan Boddu 4 participants 4 messages Apr 17, 2020

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 chardev/char-socket: Properly make qio connections non blocking

In tcp_chr_sync_read function, there is a possibility of socket disconnection during read, then tcp_chr_hup function would clean up the qio channel pointers(i.e ioc, sioc).

---
 chardev/char-socket.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
chardev/char-socket.c +3 -1
index 185fe38..30f2b2b 100644
@@ -549,11 +549,13 @@ static int tcp_chr_sync_read(Chardev *chr, const uint8_t *buf, int len)
 
     qio_channel_set_blocking(s->ioc, true, NULL);
     size = tcp_chr_recv(chr, (void *) buf, len);
-    qio_channel_set_blocking(s->ioc, false, NULL);
     if (size == 0) {
         /* connection closed */
         tcp_chr_disconnect(chr);
+        return 0;
     }
+    /* Connection is good */
+    qio_channel_set_blocking(s->ioc, false, NULL);
 
     return size;
 }
Signature · 1 line
2.7.4
#1 lore raw

Hi

Quoted 15 lines · Sai Pavan Boddu

In tcp_chr_sync_read function, there is a possibility of socket disconnection during read, then tcp_chr_hup function would clean up the qio channel pointers(i.e ioc, sioc).

---
 chardev/char-socket.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
chardev/char-socket.c +0 -0
index 185fe38..30f2b2b 100644

chardev/char-socket.c static int tcp_chr_sync_read(Chardev *chr, const uint8_t *buf, int len) · line 552

Quoted 4 lines
chardev/char-socket.c +0 -1
@@ -549,11 +549,13 @@ static int tcp_chr_sync_read(Chardev *chr, const uint8_t *buf, int len)
 
     qio_channel_set_blocking(s->ioc, true, NULL);
     size = tcp_chr_recv(chr, (void *) buf, len);
-    qio_channel_set_blocking(s->ioc, false, NULL);

But here it calls tcp_chr_recv(). And I can't find cleanup there. Nevertheless, I think this patch should be harmless.

I'd ask Daniel to have a second look.

Quoted 14 lines
chardev/char-socket.c +3 -1
     if (size == 0) {
         /* connection closed */
         tcp_chr_disconnect(chr);
+        return 0;
     }
+    /* Connection is good */
+    qio_channel_set_blocking(s->ioc, false, NULL);
 
     return size;
 }
--

2.7.4

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

Hi

Quoted 16 lines · Sai Pavan Boddu

In tcp_chr_sync_read function, there is a possibility of socket disconnection during read, then tcp_chr_hup function would clean up the qio channel pointers(i.e ioc, sioc).

---
 chardev/char-socket.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
chardev/char-socket.c +0 -0
index 185fe38..30f2b2b 100644
@@ -549,11 +549,13 @@ static int tcp_chr_sync_read(Chardev *chr, const uint8_t *buf, int len)
 
Quoted 8 lines

chardev/char-socket.c static int tcp_chr_sync_read(Chardev *chr, const uint8_t *buf, int len) · line 552

Quoted 3 lines
chardev/char-socket.c +0 -1
     qio_channel_set_blocking(s->ioc, true, NULL);
     size = tcp_chr_recv(chr, (void *) buf, len);
-    qio_channel_set_blocking(s->ioc, false, NULL);

But here it calls tcp_chr_recv(). And I can't find cleanup there. Nevertheless, I think this patch should be harmless.

I'd ask Daniel to have a second look.

I don't see any bug that needs fixing here, and I prefer the current code as it gives confidence that nothing tcp_chr_disconnect does will accidentally block.

Quoted 8 lines
Quoted 8 lines
chardev/char-socket.c +3 -0
     if (size == 0) {
         /* connection closed */
         tcp_chr_disconnect(chr);
+        return 0;
     }
+    /* Connection is good */
+    qio_channel_set_blocking(s->ioc, false, NULL);
 
Quoted 11 lines
Quoted 6 lines
chardev/char-socket.c +0 -1
     return size;
 }
--

2.7.4

Signature · 1 line
Marc-André Lureau

Regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
#3 lore raw

Hi Daniel,

Quoted 39 lines

-----Original Message-----
From: Daniel P. Berrangé <[email protected]>
Sent: Friday, April 17, 2020 6:44 PM
To: Marc-André Lureau <[email protected]>
Cc: Sai Pavan Boddu <[email protected]>; Paolo Bonzini <[email protected]>; Edgar Iglesias <[email protected]>; QEMU <qemu- [email protected]>
Subject: Re: [PATCH] chardev/char-socket: Properly make qio connections non blocking

Quoted 28 lines · Marc-André Lureau

Hi

Quoted 15 lines · Sai Pavan Boddu

In tcp_chr_sync_read function, there is a possibility of socket disconnection during read, then tcp_chr_hup function would clean up the qio channel pointers(i.e ioc, sioc).

---
 chardev/char-socket.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
chardev/char-socket.c index renamed from chardev/char-socket.c +0 -0

185fe38..30f2b2b 100644

chardev/char-socket.c static int tcp_chr_sync_read(Chardev *chr, · line 553

Quoted 5 lines
chardev/char-socket.c +0 -1
@@ -549,11 +549,13 @@ static int tcp_chr_sync_read(Chardev *chr,
 const uint8_t *buf, int len)
 
     qio_channel_set_blocking(s->ioc, true, NULL);
     size = tcp_chr_recv(chr, (void *) buf, len);
-    qio_channel_set_blocking(s->ioc, false, NULL);

But here it calls tcp_chr_recv(). And I can't find cleanup there. Nevertheless, I think this patch should be harmless.

Quoted 5 lines
Quoted 1 line

I'd ask Daniel to have a second look.

I don't see any bug that needs fixing here, and I prefer the current code as it gives confidence that nothing tcp_chr_disconnect does will accidentally block.

[Sai Pavan Boddu] The issue is triggered when 'tcp_chr_hup' callback, is dispatched when socket hung up during a blocking read. Which also calls tcp_chr_disconnect and cleans up ioc, sioc pointers. Later below line segfaults due to null pointers

	" qio_channel_set_blocking(s->ioc, false, NULL); "

Regards,
Sai Pavan

Quoted 30 lines
Quoted 18 lines
Quoted 11 lines
chardev/char-socket.c +3 -0
     if (size == 0) {
         /* connection closed */
         tcp_chr_disconnect(chr);
+        return 0;
     }
+    /* Connection is good */
+    qio_channel_set_blocking(s->ioc, false, NULL);
 
     return size;
Quoted 5 lines

}
--
2.7.4

Quoted 3 lines

--
Marc-André Lureau

Regards,
Daniel
--
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange
:|

|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange
:|