[PATCH 02/10] char-socket: rework tcp_chr_recv()

Vladimir Sementsov-Ogievskiy posted 10 patches 5 months, 1 week ago
Maintainers: "Marc-André Lureau" <marcandre.lureau@redhat.com>, Paolo Bonzini <pbonzini@redhat.com>, "Michael S. Tsirkin" <mst@redhat.com>, Gerd Hoffmann <kraxel@redhat.com>, Gustavo Romero <gustavo.romero@linaro.org>, Elena Ufimtseva <elena.ufimtseva@oracle.com>, Jagannathan Raman <jag.raman@oracle.com>, John Levon <john.levon@nutanix.com>, Thanos Makatos <thanos.makatos@nutanix.com>, "Cédric Le Goater" <clg@redhat.com>, Stefano Garzarella <sgarzare@redhat.com>, "Daniel P. Berrangé" <berrange@redhat.com>, Peter Xu <peterx@redhat.com>, Fabiano Rosas <farosas@suse.de>, Jason Wang <jasowang@redhat.com>, Michael Roth <michael.roth@amd.com>, Kostiantyn Kostiuk <kkostiuk@redhat.com>, Fam Zheng <fam@euphon.net>, Alexander Bulekov <alxndr@bu.edu>, Bandan Das <bsd@redhat.com>, Stefan Hajnoczi <stefanha@redhat.com>, Darren Kenny <darren.kenny@oracle.com>, Qiuhao Li <Qiuhao.Li@outlook.com>, Laurent Vivier <lvivier@redhat.com>, Stefan Berger <stefanb@linux.vnet.ibm.com>, Stefan Weil <sw@weilnetz.de>, Coiby Xu <Coiby.Xu@gmail.com>
There is a newer version of this series
[PATCH 02/10] char-socket: rework tcp_chr_recv()
Posted by Vladimir Sementsov-Ogievskiy 5 months, 1 week ago
First, qio_channel_readv_full() already guarantees BLOCKING and
CLOEXEC states for incoming descriptors, no reason call extra
ioctls.

Second, current implementation calls _set_block() and _set_cloexec()
again on old descriptors on failure path - we fix this too.

Finally, handling errors exactly after qio_channel_readv_full() call
looks more readable.

Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@yandex-team.ru>
---
 chardev/char-socket.c | 37 +++++++++++++------------------------
 1 file changed, 13 insertions(+), 24 deletions(-)

diff --git a/chardev/char-socket.c b/chardev/char-socket.c
index 1e8313915b..5b9b19ba8b 100644
--- a/chardev/char-socket.c
+++ b/chardev/char-socket.c
@@ -293,6 +293,18 @@ static ssize_t tcp_chr_recv(Chardev *chr, char *buf, size_t len)
                                      0, &err);
     }
 
+    if (ret == QIO_CHANNEL_ERR_BLOCK) {
+        errno = EAGAIN;
+        return -1;
+    } else if (ret == -1) {
+        trace_chr_socket_recv_err(chr, chr->label, error_get_pretty(err));
+        error_free(err);
+        errno = EIO;
+        return -1;
+    }
+
+    assert(ret >= 0);
+
     if (msgfds_num) {
         /* close and clean read_msgfds */
         for (i = 0; i < s->read_msgfds_num; i++) {
@@ -307,30 +319,7 @@ static ssize_t tcp_chr_recv(Chardev *chr, char *buf, size_t len)
         s->read_msgfds_num = msgfds_num;
     }
 
-    for (i = 0; i < s->read_msgfds_num; i++) {
-        int fd = s->read_msgfds[i];
-        if (fd < 0) {
-            continue;
-        }
-
-        /* O_NONBLOCK is preserved across SCM_RIGHTS so reset it */
-        qemu_socket_set_block(fd);
-
-#ifndef MSG_CMSG_CLOEXEC
-        qemu_set_cloexec(fd);
-#endif
-    }
-
-    if (ret == QIO_CHANNEL_ERR_BLOCK) {
-        errno = EAGAIN;
-        ret = -1;
-    } else if (ret == -1) {
-        trace_chr_socket_recv_err(chr, chr->label, error_get_pretty(err));
-        error_free(err);
-        errno = EIO;
-    } else if (ret == 0) {
-        trace_chr_socket_recv_eof(chr, chr->label);
-    }
+    trace_chr_socket_recv_eof(chr, chr->label);
 
     return ret;
 }
-- 
2.48.1
Re: [PATCH 02/10] char-socket: rework tcp_chr_recv()
Posted by Daniel P. Berrangé 5 months ago
On Wed, Sep 03, 2025 at 12:44:02PM +0300, Vladimir Sementsov-Ogievskiy wrote:
> First, qio_channel_readv_full() already guarantees BLOCKING and
> CLOEXEC states for incoming descriptors, no reason call extra
> ioctls.
> 
> Second, current implementation calls _set_block() and _set_cloexec()
> again on old descriptors on failure path - we fix this too.
> 
> Finally, handling errors exactly after qio_channel_readv_full() call
> looks more readable.
> 
> Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@yandex-team.ru>
> ---
>  chardev/char-socket.c | 37 +++++++++++++------------------------
>  1 file changed, 13 insertions(+), 24 deletions(-)
> 
> diff --git a/chardev/char-socket.c b/chardev/char-socket.c
> index 1e8313915b..5b9b19ba8b 100644
> --- a/chardev/char-socket.c
> +++ b/chardev/char-socket.c
> @@ -293,6 +293,18 @@ static ssize_t tcp_chr_recv(Chardev *chr, char *buf, size_t len)
>                                       0, &err);
>      }
>  
> +    if (ret == QIO_CHANNEL_ERR_BLOCK) {
> +        errno = EAGAIN;
> +        return -1;
> +    } else if (ret == -1) {
> +        trace_chr_socket_recv_err(chr, chr->label, error_get_pretty(err));
> +        error_free(err);
> +        errno = EIO;
> +        return -1;
> +    }
> +
> +    assert(ret >= 0);

Moving this logic to here means that in the blocking I/O, and/or error
paths, we are not clearing out the previously read s->read_msgfds_num
These should be considered obsolete at the point we start a new read
call, regardless of its success, hence why we had the code ordered in
this way.


> +
>      if (msgfds_num) {
>          /* close and clean read_msgfds */
>          for (i = 0; i < s->read_msgfds_num; i++) {
> @@ -307,30 +319,7 @@ static ssize_t tcp_chr_recv(Chardev *chr, char *buf, size_t len)
>          s->read_msgfds_num = msgfds_num;
>      }
>  
> -    for (i = 0; i < s->read_msgfds_num; i++) {
> -        int fd = s->read_msgfds[i];
> -        if (fd < 0) {
> -            continue;
> -        }
> -
> -        /* O_NONBLOCK is preserved across SCM_RIGHTS so reset it */
> -        qemu_socket_set_block(fd);
> -
> -#ifndef MSG_CMSG_CLOEXEC
> -        qemu_set_cloexec(fd);
> -#endif
> -    }

This for(){} removal is fnie.

> -
> -    if (ret == QIO_CHANNEL_ERR_BLOCK) {
> -        errno = EAGAIN;
> -        ret = -1;
> -    } else if (ret == -1) {
> -        trace_chr_socket_recv_err(chr, chr->label, error_get_pretty(err));
> -        error_free(err);
> -        errno = EIO;
> -    } else if (ret == 0) {
> -        trace_chr_socket_recv_eof(chr, chr->label);
> -    }
> +    trace_chr_socket_recv_eof(chr, chr->label);
>  
>      return ret;
>  }
> -- 
> 2.48.1
> 

With 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 :|
Re: [PATCH 02/10] char-socket: rework tcp_chr_recv()
Posted by Vladimir Sementsov-Ogievskiy 5 months ago
On 09.09.25 11:38, Daniel P. Berrangé wrote:
> On Wed, Sep 03, 2025 at 12:44:02PM +0300, Vladimir Sementsov-Ogievskiy wrote:
>> First, qio_channel_readv_full() already guarantees BLOCKING and
>> CLOEXEC states for incoming descriptors, no reason call extra
>> ioctls.
>>
>> Second, current implementation calls _set_block() and _set_cloexec()
>> again on old descriptors on failure path - we fix this too.
>>
>> Finally, handling errors exactly after qio_channel_readv_full() call
>> looks more readable.
>>
>> Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@yandex-team.ru>
>> ---
>>   chardev/char-socket.c | 37 +++++++++++++------------------------
>>   1 file changed, 13 insertions(+), 24 deletions(-)
>>
>> diff --git a/chardev/char-socket.c b/chardev/char-socket.c
>> index 1e8313915b..5b9b19ba8b 100644
>> --- a/chardev/char-socket.c
>> +++ b/chardev/char-socket.c
>> @@ -293,6 +293,18 @@ static ssize_t tcp_chr_recv(Chardev *chr, char *buf, size_t len)
>>                                        0, &err);
>>       }
>>   
>> +    if (ret == QIO_CHANNEL_ERR_BLOCK) {
>> +        errno = EAGAIN;
>> +        return -1;
>> +    } else if (ret == -1) {
>> +        trace_chr_socket_recv_err(chr, chr->label, error_get_pretty(err));
>> +        error_free(err);
>> +        errno = EIO;
>> +        return -1;
>> +    }
>> +
>> +    assert(ret >= 0);
> 
> Moving this logic to here means that in the blocking I/O, and/or error
> paths, we are not clearing out the previously read s->read_msgfds_num
> These should be considered obsolete at the point we start a new read
> call, regardless of its success, hence why we had the code ordered in
> this way.

Oops, thanks! That's not obvious, I'll add a comment.

> 
> 
>> +
>>       if (msgfds_num) {
>>           /* close and clean read_msgfds */
>>           for (i = 0; i < s->read_msgfds_num; i++) {
>> @@ -307,30 +319,7 @@ static ssize_t tcp_chr_recv(Chardev *chr, char *buf, size_t len)
>>           s->read_msgfds_num = msgfds_num;
>>       }
>>   
>> -    for (i = 0; i < s->read_msgfds_num; i++) {
>> -        int fd = s->read_msgfds[i];
>> -        if (fd < 0) {
>> -            continue;
>> -        }
>> -
>> -        /* O_NONBLOCK is preserved across SCM_RIGHTS so reset it */
>> -        qemu_socket_set_block(fd);
>> -
>> -#ifndef MSG_CMSG_CLOEXEC
>> -        qemu_set_cloexec(fd);
>> -#endif
>> -    }
> 
> This for(){} removal is fnie.
> 
>> -
>> -    if (ret == QIO_CHANNEL_ERR_BLOCK) {
>> -        errno = EAGAIN;
>> -        ret = -1;
>> -    } else if (ret == -1) {
>> -        trace_chr_socket_recv_err(chr, chr->label, error_get_pretty(err));
>> -        error_free(err);
>> -        errno = EIO;
>> -    } else if (ret == 0) {
>> -        trace_chr_socket_recv_eof(chr, chr->label);
>> -    }
>> +    trace_chr_socket_recv_eof(chr, chr->label);
>>   
>>       return ret;
>>   }
>> -- 
>> 2.48.1
>>
> 
> With regards,
> Daniel


-- 
Best regards,
Vladimir

Re: [PATCH 02/10] char-socket: rework tcp_chr_recv()
Posted by Peter Xu 5 months ago
On Wed, Sep 03, 2025 at 12:44:02PM +0300, Vladimir Sementsov-Ogievskiy wrote:
> First, qio_channel_readv_full() already guarantees BLOCKING and
> CLOEXEC states for incoming descriptors, no reason call extra
> ioctls.
> 
> Second, current implementation calls _set_block() and _set_cloexec()
> again on old descriptors on failure path - we fix this too.
> 
> Finally, handling errors exactly after qio_channel_readv_full() call
> looks more readable.
> 
> Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@yandex-team.ru>
> ---
>  chardev/char-socket.c | 37 +++++++++++++------------------------
>  1 file changed, 13 insertions(+), 24 deletions(-)
> 
> diff --git a/chardev/char-socket.c b/chardev/char-socket.c
> index 1e8313915b..5b9b19ba8b 100644
> --- a/chardev/char-socket.c
> +++ b/chardev/char-socket.c
> @@ -293,6 +293,18 @@ static ssize_t tcp_chr_recv(Chardev *chr, char *buf, size_t len)
>                                       0, &err);
>      }
>  
> +    if (ret == QIO_CHANNEL_ERR_BLOCK) {
> +        errno = EAGAIN;
> +        return -1;
> +    } else if (ret == -1) {
> +        trace_chr_socket_recv_err(chr, chr->label, error_get_pretty(err));
> +        error_free(err);
> +        errno = EIO;
> +        return -1;
> +    }
> +
> +    assert(ret >= 0);
> +
>      if (msgfds_num) {
>          /* close and clean read_msgfds */
>          for (i = 0; i < s->read_msgfds_num; i++) {
> @@ -307,30 +319,7 @@ static ssize_t tcp_chr_recv(Chardev *chr, char *buf, size_t len)
>          s->read_msgfds_num = msgfds_num;
>      }
>  
> -    for (i = 0; i < s->read_msgfds_num; i++) {
> -        int fd = s->read_msgfds[i];
> -        if (fd < 0) {
> -            continue;
> -        }
> -
> -        /* O_NONBLOCK is preserved across SCM_RIGHTS so reset it */
> -        qemu_socket_set_block(fd);
> -
> -#ifndef MSG_CMSG_CLOEXEC
> -        qemu_set_cloexec(fd);
> -#endif
> -    }
> -
> -    if (ret == QIO_CHANNEL_ERR_BLOCK) {
> -        errno = EAGAIN;
> -        ret = -1;
> -    } else if (ret == -1) {
> -        trace_chr_socket_recv_err(chr, chr->label, error_get_pretty(err));
> -        error_free(err);
> -        errno = EIO;
> -    } else if (ret == 0) {
> -        trace_chr_socket_recv_eof(chr, chr->label);
> -    }
> +    trace_chr_socket_recv_eof(chr, chr->label);

This tracepoint may still need to be put into a ret==0 check.

Looks reasonable other than that..

>  
>      return ret;
>  }
> -- 
> 2.48.1
> 

-- 
Peter Xu
Re: [PATCH 02/10] char-socket: rework tcp_chr_recv()
Posted by Vladimir Sementsov-Ogievskiy 5 months ago
On 09.09.25 00:53, Peter Xu wrote:
> On Wed, Sep 03, 2025 at 12:44:02PM +0300, Vladimir Sementsov-Ogievskiy wrote:
>> First, qio_channel_readv_full() already guarantees BLOCKING and
>> CLOEXEC states for incoming descriptors, no reason call extra
>> ioctls.
>>
>> Second, current implementation calls _set_block() and _set_cloexec()
>> again on old descriptors on failure path - we fix this too.
>>
>> Finally, handling errors exactly after qio_channel_readv_full() call
>> looks more readable.
>>
>> Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@yandex-team.ru>
>> ---
>>   chardev/char-socket.c | 37 +++++++++++++------------------------
>>   1 file changed, 13 insertions(+), 24 deletions(-)
>>
>> diff --git a/chardev/char-socket.c b/chardev/char-socket.c
>> index 1e8313915b..5b9b19ba8b 100644
>> --- a/chardev/char-socket.c
>> +++ b/chardev/char-socket.c
>> @@ -293,6 +293,18 @@ static ssize_t tcp_chr_recv(Chardev *chr, char *buf, size_t len)
>>                                        0, &err);
>>       }
>>   
>> +    if (ret == QIO_CHANNEL_ERR_BLOCK) {
>> +        errno = EAGAIN;
>> +        return -1;
>> +    } else if (ret == -1) {
>> +        trace_chr_socket_recv_err(chr, chr->label, error_get_pretty(err));
>> +        error_free(err);
>> +        errno = EIO;
>> +        return -1;
>> +    }
>> +
>> +    assert(ret >= 0);
>> +
>>       if (msgfds_num) {
>>           /* close and clean read_msgfds */
>>           for (i = 0; i < s->read_msgfds_num; i++) {
>> @@ -307,30 +319,7 @@ static ssize_t tcp_chr_recv(Chardev *chr, char *buf, size_t len)
>>           s->read_msgfds_num = msgfds_num;
>>       }
>>   
>> -    for (i = 0; i < s->read_msgfds_num; i++) {
>> -        int fd = s->read_msgfds[i];
>> -        if (fd < 0) {
>> -            continue;
>> -        }
>> -
>> -        /* O_NONBLOCK is preserved across SCM_RIGHTS so reset it */
>> -        qemu_socket_set_block(fd);
>> -
>> -#ifndef MSG_CMSG_CLOEXEC
>> -        qemu_set_cloexec(fd);
>> -#endif
>> -    }
>> -
>> -    if (ret == QIO_CHANNEL_ERR_BLOCK) {
>> -        errno = EAGAIN;
>> -        ret = -1;
>> -    } else if (ret == -1) {
>> -        trace_chr_socket_recv_err(chr, chr->label, error_get_pretty(err));
>> -        error_free(err);
>> -        errno = EIO;
>> -    } else if (ret == 0) {
>> -        trace_chr_socket_recv_eof(chr, chr->label);
>> -    }
>> +    trace_chr_socket_recv_eof(chr, chr->label);
> 
> This tracepoint may still need to be put into a ret==0 check.
> 
> Looks reasonable other than that..
> 

Oh, right. Will fix.

>>   
>>       return ret;
>>   }
>> -- 
>> 2.48.1
>>
> 


-- 
Best regards,
Vladimir