From nobody Sun Sep 28 17:12:44 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=yandex-team.ru ARC-Seal: i=1; a=rsa-sha256; t=1757942673; cv=none; d=zohomail.com; s=zohoarc; b=WA5dZ0yL165YjoMYFexxv1otL4PvqEouxo6YWIlmGnjb+i1alJVF8ZitTZOd9JF5P2Ls2QXKugg58f7qH2Ul1gmO4cpuAWX5Zz8Zv26jGHvbsIoAnr0F2l9ZfDmuHQSpE3Zy5qA9M9iDYa2sbqFfUxsgCgFZKUzSfrCIY+5/0es= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1757942673; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=eHSbhZivSSL6A70/eVcP41ZLHRUQcIsarokD4DDvIFI=; b=ati2toVQXzIXZRYJhe1eVenvQW+ASGMcz6DjzSsfxa2pbRKbDsadVIMOLoyg2NTDsMo9ObVUJoIMpUQxo35vieIHg1cOSE3OmNbZdlxhcUGXma6xCpqC4jZvNpCchenRPQdIdkfie2QHMHGStEKkhhM4mROE/sPA5YL1Xwj6O1o= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1757942673084626.9698747306247; Mon, 15 Sep 2025 06:24:33 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uy9Ah-0001pb-6B; Mon, 15 Sep 2025 09:23:09 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1uy9AQ-0001jM-BG; Mon, 15 Sep 2025 09:22:50 -0400 Received: from forwardcorp1d.mail.yandex.net ([178.154.239.200]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1uy9A9-0006z6-22; Mon, 15 Sep 2025 09:22:48 -0400 Received: from mail-nwsmtp-smtp-corp-main-56.klg.yp-c.yandex.net (mail-nwsmtp-smtp-corp-main-56.klg.yp-c.yandex.net [IPv6:2a02:6b8:c42:cf2d:0:640:140f:0]) by forwardcorp1d.mail.yandex.net (Yandex) with ESMTPS id 97F7D807EF; Mon, 15 Sep 2025 16:22:24 +0300 (MSK) Received: from vsementsov-lin.. (unknown [2a02:6bf:8080:130::1:7]) by mail-nwsmtp-smtp-corp-main-56.klg.yp-c.yandex.net (smtpcorp/Yandex) with ESMTPSA id GMPC4o1FuGk0-r8Q0a859; Mon, 15 Sep 2025 16:22:24 +0300 X-Yandex-Fwd: 1 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yandex-team.ru; s=default; t=1757942544; bh=eHSbhZivSSL6A70/eVcP41ZLHRUQcIsarokD4DDvIFI=; h=Message-ID:Date:In-Reply-To:Cc:Subject:References:To:From; b=JjX82Pd9YwiWgqudJIEQX18BCae+blTeJK1jkaP3weyduluPFPcfVV3RMouHbWm72 tFioycz4TvNLHj3vcLjq6TbxPYPrVGmo+vHnS79wdh8GaF8QzZdWqQt+qp5dY02abp HsPEYNsD+V/KLFOEBGh9DG6b7v0M8FPE/Vf84qVs= Authentication-Results: mail-nwsmtp-smtp-corp-main-56.klg.yp-c.yandex.net; dkim=pass header.i=@yandex-team.ru From: Vladimir Sementsov-Ogievskiy To: berrange@redhat.com Cc: qemu-devel@nongnu.org, peterx@redhat.com, qemu-block@nongnu.org, vsementsov@yandex-team.ru, leiyang@redhat.com, marcandre.lureau@redhat.com, "Michael S. Tsirkin" , Stefano Garzarella , Jason Wang , Michael Roth , Kostiantyn Kostiuk , Paolo Bonzini , Stefan Weil , Coiby Xu Subject: [PATCH v3 07/13] util: drop qemu_socket_set_nonblock() Date: Mon, 15 Sep 2025 16:22:04 +0300 Message-ID: <20250915132211.135095-8-vsementsov@yandex-team.ru> X-Mailer: git-send-email 2.48.1 In-Reply-To: <20250915132211.135095-1-vsementsov@yandex-team.ru> References: <20250915132211.135095-1-vsementsov@yandex-team.ru> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=178.154.239.200; envelope-from=vsementsov@yandex-team.ru; helo=forwardcorp1d.mail.yandex.net X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @yandex-team.ru) X-ZM-MESSAGEID: 1757942675832116600 Content-Type: text/plain; charset="utf-8" Use common qemu_set_blocking() instead. Note that pre-patch the behavior of Win32 and Linux realizations are inconsistent: we ignore failure for Win32, and assert success for Linux. How do we convert the callers? 1. Most of callers call qemu_socket_set_nonblock() on a freshly created socket fd, in conditions when we may simply report an error. Seems correct switching to error handling both for Windows (pre-patch error is ignored) and Linux (pre-patch we assert success). Anyway, we normally don't expect errors in these cases. Still in tests let's use &error_abort for simplicity. What are exclusions? 2. hw/virtio/vhost-user.c - we are inside #ifdef CONFIG_LINUX, so no damage in switching to error handling from assertion. 3. io/channel-socket.c: here we convert both old calls to qemu_socket_set_nonblock() and qemu_socket_set_block() to one new call. Pre-patch we assert success for Linux in qemu_socket_set_nonblock(), and ignore all other errors here. So, for Windows switch is a bit dangerous: we may get new errors or crashes(when error_abort is passed) in cases where we have silently ignored the error before (was it correct in all such cases, if they were?) Still, there is no other way to stricter API than take this risk. 4. util/vhost-user-server - compiled only for Linux (see util/meson.build), so we are safe, switching from assertion to &error_abort. Note: In qga/channel-posix.c we use g_warning(), where g_printerr() would actually be a better choice. Still let's for now follow common style of qga, where g_warning() is commonly used to print such messages, and no call to g_printerr(). Converting everything to use g_printerr() should better be another series. Signed-off-by: Vladimir Sementsov-Ogievskiy --- contrib/ivshmem-server/ivshmem-server.c | 7 ++++++- hw/hyperv/syndbg.c | 4 +++- hw/virtio/vhost-user.c | 5 ++++- include/qemu/sockets.h | 1 - io/channel-socket.c | 7 +++---- net/dgram.c | 16 +++++++++++++--- net/l2tpv3.c | 5 +++-- net/socket.c | 20 ++++++++++++++++---- qga/channel-posix.c | 7 ++++++- tests/unit/socket-helpers.c | 4 +++- tests/unit/test-crypto-tlssession.c | 8 ++++---- util/oslib-posix.c | 7 ------- util/oslib-win32.c | 5 ----- util/vhost-user-server.c | 4 ++-- 14 files changed, 63 insertions(+), 37 deletions(-) diff --git a/contrib/ivshmem-server/ivshmem-server.c b/contrib/ivshmem-serv= er/ivshmem-server.c index 2f3c7320a6..ff1c74a744 100644 --- a/contrib/ivshmem-server/ivshmem-server.c +++ b/contrib/ivshmem-server/ivshmem-server.c @@ -6,6 +6,7 @@ * top-level directory. */ #include "qemu/osdep.h" +#include "qapi/error.h" #include "qemu/host-utils.h" #include "qemu/sockets.h" =20 @@ -146,9 +147,13 @@ ivshmem_server_handle_new_conn(IvshmemServer *server) return -1; } =20 - qemu_socket_set_nonblock(newfd); IVSHMEM_SERVER_DEBUG(server, "accept()=3D%d\n", newfd); =20 + if (!qemu_set_blocking(newfd, false, &error_reporter)) { + close(newfd); + return -1; + } + /* allocate new structure for this peer */ peer =3D g_malloc0(sizeof(*peer)); peer->sock_fd =3D newfd; diff --git a/hw/hyperv/syndbg.c b/hw/hyperv/syndbg.c index ac7e15f6f1..bcdfdf6af7 100644 --- a/hw/hyperv/syndbg.c +++ b/hw/hyperv/syndbg.c @@ -338,7 +338,9 @@ static void hv_syndbg_realize(DeviceState *dev, Error *= *errp) return; } =20 - qemu_socket_set_nonblock(syndbg->socket); + if (!qemu_set_blocking(syndbg->socket, false, errp)) { + return; + } =20 syndbg->servaddr.sin_port =3D htons(syndbg->host_port); syndbg->servaddr.sin_family =3D AF_INET; diff --git a/hw/virtio/vhost-user.c b/hw/virtio/vhost-user.c index 1e1d6b0d6e..36c9c2e04d 100644 --- a/hw/virtio/vhost-user.c +++ b/hw/virtio/vhost-user.c @@ -2039,7 +2039,10 @@ static int vhost_user_postcopy_advise(struct vhost_d= ev *dev, Error **errp) error_setg(errp, "%s: Failed to get ufd", __func__); return -EIO; } - qemu_socket_set_nonblock(ufd); + if (!qemu_set_blocking(ufd, false, errp)) { + close(ufd); + return -EINVAL; + } =20 /* register ufd with userfault thread */ u->postcopy_fd.fd =3D ufd; diff --git a/include/qemu/sockets.h b/include/qemu/sockets.h index c562690d89..6477f90b9e 100644 --- a/include/qemu/sockets.h +++ b/include/qemu/sockets.h @@ -48,7 +48,6 @@ int socket_set_cork(int fd, int v); int socket_set_nodelay(int fd); void qemu_socket_set_block(int fd); int qemu_socket_try_set_nonblock(int fd); -void qemu_socket_set_nonblock(int fd); int socket_set_fast_reuse(int fd); =20 #ifdef WIN32 diff --git a/io/channel-socket.c b/io/channel-socket.c index 21f8f2e0c5..f7e3cb9742 100644 --- a/io/channel-socket.c +++ b/io/channel-socket.c @@ -825,11 +825,10 @@ qio_channel_socket_set_blocking(QIOChannel *ioc, { QIOChannelSocket *sioc =3D QIO_CHANNEL_SOCKET(ioc); =20 - if (enabled) { - qemu_socket_set_block(sioc->fd); - } else { - qemu_socket_set_nonblock(sioc->fd); + if (!qemu_set_blocking(sioc->fd, enabled, errp)) { + return -1; } + return 0; } =20 diff --git a/net/dgram.c b/net/dgram.c index 48f653bceb..fb9ded30df 100644 --- a/net/dgram.c +++ b/net/dgram.c @@ -226,7 +226,10 @@ static int net_dgram_mcast_create(struct sockaddr_in *= mcastaddr, } } =20 - qemu_socket_set_nonblock(fd); + if (!qemu_set_blocking(fd, false, errp)) { + goto fail; + } + return fd; fail: if (fd >=3D 0) { @@ -504,7 +507,11 @@ int net_init_dgram(const Netdev *netdev, const char *n= ame, close(fd); return -1; } - qemu_socket_set_nonblock(fd); + + if (!qemu_set_blocking(fd, false, errp)) { + close(fd); + return -1; + } =20 dest_len =3D sizeof(raddr_in); dest_addr =3D g_malloc(dest_len); @@ -551,7 +558,10 @@ int net_init_dgram(const Netdev *netdev, const char *n= ame, close(fd); return -1; } - qemu_socket_set_nonblock(fd); + if (!qemu_set_blocking(fd, false, errp)) { + close(fd); + return -1; + } =20 dest_len =3D sizeof(raddr_un); dest_addr =3D g_malloc(dest_len); diff --git a/net/l2tpv3.c b/net/l2tpv3.c index b5547cb917..cdfc641aa6 100644 --- a/net/l2tpv3.c +++ b/net/l2tpv3.c @@ -648,6 +648,9 @@ int net_init_l2tpv3(const Netdev *netdev, error_setg(errp, "could not bind socket err=3D%i", errno); goto outerr; } + if (!qemu_set_blocking(fd, false, errp)) { + goto outerr; + } =20 freeaddrinfo(result); =20 @@ -709,8 +712,6 @@ int net_init_l2tpv3(const Netdev *netdev, s->vec =3D g_new(struct iovec, MAX_L2TPV3_IOVCNT); s->header_buf =3D g_malloc(s->header_size); =20 - qemu_socket_set_nonblock(fd); - s->fd =3D fd; s->counter =3D 0; =20 diff --git a/net/socket.c b/net/socket.c index 784dda686f..db25e3d9ae 100644 --- a/net/socket.c +++ b/net/socket.c @@ -295,7 +295,10 @@ static int net_socket_mcast_create(struct sockaddr_in = *mcastaddr, } } =20 - qemu_socket_set_nonblock(fd); + if (!qemu_set_blocking(fd, false, errp)) { + goto fail; + } + return fd; fail: if (fd >=3D 0) @@ -508,7 +511,10 @@ static int net_socket_listen_init(NetClientState *peer, error_setg_errno(errp, errno, "can't create stream socket"); return -1; } - qemu_socket_set_nonblock(fd); + if (!qemu_set_blocking(fd, false, errp)) { + close(fd); + return -1; + } =20 socket_set_fast_reuse(fd); =20 @@ -556,7 +562,10 @@ static int net_socket_connect_init(NetClientState *pee= r, error_setg_errno(errp, errno, "can't create stream socket"); return -1; } - qemu_socket_set_nonblock(fd); + if (!qemu_set_blocking(fd, false, errp)) { + close(fd); + return -1; + } =20 connected =3D 0; for(;;) { @@ -671,7 +680,10 @@ static int net_socket_udp_init(NetClientState *peer, close(fd); return -1; } - qemu_socket_set_nonblock(fd); + if (!qemu_set_blocking(fd, false, errp)) { + close(fd); + return -1; + } =20 s =3D net_socket_fd_init_dgram(peer, model, name, fd, 0, NULL, errp); if (!s) { diff --git a/qga/channel-posix.c b/qga/channel-posix.c index 465d688ecb..9ccc8b7bd1 100644 --- a/qga/channel-posix.c +++ b/qga/channel-posix.c @@ -28,6 +28,7 @@ static gboolean ga_channel_listen_accept(GIOChannel *chan= nel, GAChannel *c =3D data; int ret, client_fd; bool accepted =3D false; + Error *err =3D NULL; =20 g_assert(channel !=3D NULL); =20 @@ -36,7 +37,11 @@ static gboolean ga_channel_listen_accept(GIOChannel *cha= nnel, g_warning("error converting fd to gsocket: %s", strerror(errno)); goto out; } - qemu_socket_set_nonblock(client_fd); + if (!qemu_set_blocking(client_fd, false, &err)) { + g_warning("%s", error_get_pretty(err)); + error_free(err); + goto out; + } ret =3D ga_channel_client_add(c, client_fd); if (ret) { g_warning("error setting up connection"); diff --git a/tests/unit/socket-helpers.c b/tests/unit/socket-helpers.c index 37db24f72a..46d2ff101c 100644 --- a/tests/unit/socket-helpers.c +++ b/tests/unit/socket-helpers.c @@ -19,6 +19,7 @@ */ =20 #include "qemu/osdep.h" +#include "qapi/error.h" #include "qemu/sockets.h" #include "socket-helpers.h" =20 @@ -88,7 +89,8 @@ static int socket_can_bind_connect(const char *hostname, = int family) goto cleanup; } =20 - qemu_socket_set_nonblock(cfd); + qemu_set_blocking(cfd, false, &error_abort); + if (connect(cfd, (struct sockaddr *)&ss, sslen) < 0) { if (errno =3D=3D EINPROGRESS) { check_soerr =3D true; diff --git a/tests/unit/test-crypto-tlssession.c b/tests/unit/test-crypto-t= lssession.c index 554054e934..61311cbe6e 100644 --- a/tests/unit/test-crypto-tlssession.c +++ b/tests/unit/test-crypto-tlssession.c @@ -112,8 +112,8 @@ static void test_crypto_tls_session_psk(void) * thread, so we need these non-blocking to avoid deadlock * of ourselves */ - qemu_socket_set_nonblock(channel[0]); - qemu_socket_set_nonblock(channel[1]); + qemu_set_blocking(channel[0], false, &error_abort); + qemu_set_blocking(channel[1], false, &error_abort); =20 clientCreds =3D test_tls_creds_psk_create( QCRYPTO_TLS_CREDS_ENDPOINT_CLIENT, @@ -264,8 +264,8 @@ static void test_crypto_tls_session_x509(const void *op= aque) * thread, so we need these non-blocking to avoid deadlock * of ourselves */ - qemu_socket_set_nonblock(channel[0]); - qemu_socket_set_nonblock(channel[1]); + qemu_set_blocking(channel[0], false, &error_abort); + qemu_set_blocking(channel[1], false, &error_abort); =20 #define CLIENT_CERT_DIR "tests/test-crypto-tlssession-client/" #define SERVER_CERT_DIR "tests/test-crypto-tlssession-server/" diff --git a/util/oslib-posix.c b/util/oslib-posix.c index c737701075..599993d40d 100644 --- a/util/oslib-posix.c +++ b/util/oslib-posix.c @@ -275,13 +275,6 @@ int qemu_socket_try_set_nonblock(int fd) return g_unix_set_fd_nonblocking(fd, true, NULL) ? 0 : -errno; } =20 -void qemu_socket_set_nonblock(int fd) -{ - int f; - f =3D qemu_socket_try_set_nonblock(fd); - assert(f =3D=3D 0); -} - int socket_set_fast_reuse(int fd) { int val =3D 1, ret; diff --git a/util/oslib-win32.c b/util/oslib-win32.c index 03044f5b59..1566eb57e7 100644 --- a/util/oslib-win32.c +++ b/util/oslib-win32.c @@ -211,11 +211,6 @@ int qemu_socket_try_set_nonblock(int fd) return 0; } =20 -void qemu_socket_set_nonblock(int fd) -{ - (void)qemu_socket_try_set_nonblock(fd); -} - int socket_set_fast_reuse(int fd) { /* Enabling the reuse of an endpoint that was used by a socket still in diff --git a/util/vhost-user-server.c b/util/vhost-user-server.c index fd59cd253e..127fdaf779 100644 --- a/util/vhost-user-server.c +++ b/util/vhost-user-server.c @@ -78,7 +78,7 @@ static void vmsg_unblock_fds(VhostUserMsg *vmsg) } =20 for (i =3D 0; i < vmsg->fd_num; i++) { - qemu_socket_set_nonblock(vmsg->fds[i]); + qemu_set_blocking(vmsg->fds[i], false, &error_abort); } } =20 @@ -303,7 +303,7 @@ set_watch(VuDev *vu_dev, int fd, int vu_evt, =20 vu_fd_watch->fd =3D fd; vu_fd_watch->cb =3D cb; - qemu_socket_set_nonblock(fd); + qemu_set_blocking(fd, false, &error_abort); aio_set_fd_handler(server->ctx, fd, kick_handler, NULL, NULL, NULL, vu_fd_watch); vu_fd_watch->vu_dev =3D vu_dev; --=20 2.48.1