From nobody Fri Dec 19 11:30:24 2025 Received: from mailtransmit04.runbox.com (mailtransmit04.runbox.com [185.226.149.37]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0E9AC256C7D; Tue, 20 May 2025 22:56:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.226.149.37 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747781791; cv=none; b=fJaO+K1kycKv6TYaItTCqfI7huiFqCFeVZf+tqI6IfFP4+vsYOKzpvuBolEWx4mVnb4Q3X6iTkZuBsfnW2egyOhdAmOG4k4rNgqUjpY/vSt3TWAgFBYcV/CarK5p6fpJsVl+I0/M5ifKup+lv2i/SnTNbfIZPIw0Gcyfv3F0NXo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747781791; c=relaxed/simple; bh=Bisvp9WXtx0uaSzm4IId2G0NuCg21iZTRlMs7KZj3Zs=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=i4kyjRzdjiTgGkzg94cBC1VEuzOrZ5bYnPTiL3mCT6tyR4aD6yF/+qoRxS1OcfN8/bnVPVWmGPKKcCLnAu85T7B47JCOb5qIJJZKz+xP3eF8qz/w9tf8wmk69045+Uvu8t5Sj3MoAQ1bZ4GPEUyqLGPbxV0qZqtTM4BY8Kb3iPM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=rbox.co; spf=pass smtp.mailfrom=rbox.co; dkim=pass (2048-bit key) header.d=rbox.co header.i=@rbox.co header.b=EbqaShv5; arc=none smtp.client-ip=185.226.149.37 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=rbox.co Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=rbox.co Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=rbox.co header.i=@rbox.co header.b="EbqaShv5" Received: from mailtransmit02.runbox ([10.9.9.162] helo=aibo.runbox.com) by mailtransmit04.runbox.com with esmtps (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.93) (envelope-from ) id 1uHVsg-001X4u-TC; Wed, 21 May 2025 00:56:18 +0200 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=rbox.co; s=selector2; h=Cc:To:In-Reply-To:References:Message-Id: Content-Transfer-Encoding:Content-Type:MIME-Version:Subject:Date:From; bh=bJE5agG6suW5EMhVSS9xIoPl9te7wqkSo/E38GSN3wQ=; b=EbqaShv58TyWcztpNBCjVoxyeK mLkk6jGtgmElvKvh5rmBeLXsR/cHk+9iT9ZSDpYanrpEEG1v3/eIz0ZM/19k41HArDYPBTEuCkwM2 mxk25tr5bAtCy+OtlHRUI8tFXZuhHH7WeElIkKGpqdMsNliC7f+cS+cbp9wmokN6blQ7HJFXgnjSI g/ZV2kuUlWsZ6kK96Tpre3NE0BwP6xxjc77kAZLRBLMfrscYbAtl/w8SaoFhTcEhNIh208qbR1gQl qQOP8AZi7KdB4GowGiX5P8uyz2YTXzA9ICkDSBDUpLPUkLc02XS3W1D2x54AOOMenqBhPVrGGZlty J9qx78BQ==; Received: from [10.9.9.73] (helo=submission02.runbox) by mailtransmit02.runbox with esmtp (Exim 4.86_2) (envelope-from ) id 1uHVsf-0007mr-Ni; Wed, 21 May 2025 00:56:17 +0200 Received: by submission02.runbox with esmtpsa [Authenticated ID (604044)] (TLS1.2:ECDHE_SECP256R1__RSA_PSS_RSAE_SHA256__AES_256_GCM:256) (Exim 4.93) id 1uHVsW-00CxGf-QH; Wed, 21 May 2025 00:56:08 +0200 From: Michal Luczaj Date: Wed, 21 May 2025 00:55:19 +0200 Subject: [PATCH net-next v5 1/5] vsock/virtio: Linger on unsent data Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250521-vsock-linger-v5-1-94827860d1d6@rbox.co> References: <20250521-vsock-linger-v5-0-94827860d1d6@rbox.co> In-Reply-To: <20250521-vsock-linger-v5-0-94827860d1d6@rbox.co> To: Stefano Garzarella , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Simon Horman , "Michael S. Tsirkin" , Jason Wang , Xuan Zhuo , =?utf-8?q?Eugenio_P=C3=A9rez?= , Stefan Hajnoczi Cc: virtualization@lists.linux.dev, netdev@vger.kernel.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Michal Luczaj X-Mailer: b4 0.14.2 Currently vsock's lingering effectively boils down to waiting (or timing out) until packets are consumed or dropped by the peer; be it by receiving the data, closing or shutting down the connection. To align with the semantics described in the SO_LINGER section of man socket(7) and to mimic AF_INET's behaviour more closely, change the logic of a lingering close(): instead of waiting for all data to be handled, block until data is considered sent from the vsock's transport point of view. That is until worker picks the packets for processing and decrements virtio_vsock_sock::bytes_unsent down to 0. Note that (some interpretation of) lingering was always limited to transports that called virtio_transport_wait_close() on transport release. This does not change, i.e. under Hyper-V and VMCI no lingering would be observed. The implementation does not adhere strictly to man page's interpretation of SO_LINGER: shutdown() will not trigger the lingering. This follows AF_INET. Reviewed-by: Stefano Garzarella Signed-off-by: Michal Luczaj --- net/vmw_vsock/virtio_transport_common.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/net/vmw_vsock/virtio_transport_common.c b/net/vmw_vsock/virtio= _transport_common.c index 6e7b727c781c88674c147b7b75f49f4f1c670d38..f2f1b166731b1bf2baa3db2854d= e19aa331128ea 100644 --- a/net/vmw_vsock/virtio_transport_common.c +++ b/net/vmw_vsock/virtio_transport_common.c @@ -1195,12 +1195,14 @@ static void virtio_transport_wait_close(struct sock= *sk, long timeout) { if (timeout) { DEFINE_WAIT_FUNC(wait, woken_wake_function); + struct vsock_sock *vsk =3D vsock_sk(sk); =20 add_wait_queue(sk_sleep(sk), &wait); =20 do { if (sk_wait_event(sk, &timeout, - sock_flag(sk, SOCK_DONE), &wait)) + virtio_transport_unsent_bytes(vsk) =3D=3D 0, + &wait)) break; } while (!signal_pending(current) && timeout); =20 --=20 2.49.0 From nobody Fri Dec 19 11:30:24 2025 Received: from mailtransmit05.runbox.com (mailtransmit05.runbox.com [185.226.149.38]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CC4A8256C84 for ; Tue, 20 May 2025 22:56:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.226.149.38 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747781791; cv=none; b=AxEnKdxs9K+TvVDvSEFA2nn6F8b2VFVV3hEXFGjFwHGsB1ATgZ6cP0366F0aNKGemNuM+cDsEOGKMtwmkycBrfNxInv5pprYOYuR1Z8zHv7NoJnlW3y/l9r+YZqTjzEMcZaPFXHUir6v2OAA0a7vOsB5umEOoT+JVHLxsYkgnRM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747781791; c=relaxed/simple; bh=7/4DagCvVLly4gImCZNK1PedcDgABasf/ogGGGrZizY=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=gdiCDlZWwEFc6sPe9ukDESkCrUu3YMoajX0ZP2FpnJdSd93UqxgGl0gVmW6WCJMLdjb/konLFoeIeiJN6ulNEW/Kd/INGVHsiM9WQd4sFNYCZP7C5loO8vtYTv02MgWf5LSYJRKoJh2omb9uLFdeIZTvlGSyHCN5FK/rA5RLJa0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=rbox.co; spf=pass smtp.mailfrom=rbox.co; dkim=pass (2048-bit key) header.d=rbox.co header.i=@rbox.co header.b=O2iEYA2G; arc=none smtp.client-ip=185.226.149.38 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=rbox.co Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=rbox.co Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=rbox.co header.i=@rbox.co header.b="O2iEYA2G" Received: from mailtransmit02.runbox ([10.9.9.162] helo=aibo.runbox.com) by mailtransmit05.runbox.com with esmtps (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.93) (envelope-from ) id 1uHVsn-001Mhn-C2; Wed, 21 May 2025 00:56:25 +0200 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=rbox.co; s=selector2; h=Cc:To:In-Reply-To:References:Message-Id: Content-Transfer-Encoding:Content-Type:MIME-Version:Subject:Date:From; bh=1RCUs8dYsV0Y4s60XlR52eOROlRK4tgJbTFvoxC73qY=; b=O2iEYA2GxZA9vCJZO/KyxhWWhF i9XbHPsnIjDMRhUV+mltKqa/k+tOy0NKqxn5u4aRmHX5+y520qG98ePC65Q/RkXmXpK5QlCphMYUQ 09U7yFGBUV0VEntX2fSBXGPyc2S6XRfk4fyriaPTzbrSJIzsewlzBXzWuvDCoNla1nTPqDBuaFkfb 07VgHWKkMnmoLnm1GdHxGhay7pdjP2nmYQghShZpYyk4bFWwlM75YDEGNNG0wcKg4TdxcykBA4TVx IDJN1J8QCv+MxUhbStUIX/6XS0KkwPM82rtdNIrvMnwaiD2TGdMXsskrK8G08VtjMd3l+GYFdeusl mv4Oo4ig==; Received: from [10.9.9.73] (helo=submission02.runbox) by mailtransmit02.runbox with esmtp (Exim 4.86_2) (envelope-from ) id 1uHVsn-0007nU-45; Wed, 21 May 2025 00:56:25 +0200 Received: by submission02.runbox with esmtpsa [Authenticated ID (604044)] (TLS1.2:ECDHE_SECP256R1__RSA_PSS_RSAE_SHA256__AES_256_GCM:256) (Exim 4.93) id 1uHVsX-00CxGf-G9; Wed, 21 May 2025 00:56:09 +0200 From: Michal Luczaj Date: Wed, 21 May 2025 00:55:20 +0200 Subject: [PATCH net-next v5 2/5] vsock: Move lingering logic to af_vsock core Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250521-vsock-linger-v5-2-94827860d1d6@rbox.co> References: <20250521-vsock-linger-v5-0-94827860d1d6@rbox.co> In-Reply-To: <20250521-vsock-linger-v5-0-94827860d1d6@rbox.co> To: Stefano Garzarella , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Simon Horman , "Michael S. Tsirkin" , Jason Wang , Xuan Zhuo , =?utf-8?q?Eugenio_P=C3=A9rez?= , Stefan Hajnoczi Cc: virtualization@lists.linux.dev, netdev@vger.kernel.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Michal Luczaj X-Mailer: b4 0.14.2 Lingering should be transport-independent in the long run. In preparation for supporting other transports, as well as the linger on shutdown(), move code to core. Generalize by querying vsock_transport::unsent_bytes(), guard against the callback being unimplemented. Do not pass sk_lingertime explicitly. Pull SOCK_LINGER check into vsock_linger(). Flatten the function. Remove the nested block by inverting the condition: return early on !timeout. Suggested-by: Stefano Garzarella Signed-off-by: Michal Luczaj Reviewed-by: Stefano Garzarella --- include/net/af_vsock.h | 1 + net/vmw_vsock/af_vsock.c | 33 +++++++++++++++++++++++++++++= ++++ net/vmw_vsock/virtio_transport_common.c | 23 ++--------------------- 3 files changed, 36 insertions(+), 21 deletions(-) diff --git a/include/net/af_vsock.h b/include/net/af_vsock.h index 9e85424c834353d016a527070dd62e15ff3bfce1..d56e6e135158939087d060dfcf6= 5d3fdaea53bf3 100644 --- a/include/net/af_vsock.h +++ b/include/net/af_vsock.h @@ -221,6 +221,7 @@ void vsock_for_each_connected_socket(struct vsock_trans= port *transport, void (*fn)(struct sock *sk)); int vsock_assign_transport(struct vsock_sock *vsk, struct vsock_sock *psk); bool vsock_find_cid(unsigned int cid); +void vsock_linger(struct sock *sk); =20 /**** TAP ****/ =20 diff --git a/net/vmw_vsock/af_vsock.c b/net/vmw_vsock/af_vsock.c index fc6afbc8d6806a4d98c66abc3af4bd139c583b08..2e7a3034e965db30b6ee295370d= 866e6d8b1c341 100644 --- a/net/vmw_vsock/af_vsock.c +++ b/net/vmw_vsock/af_vsock.c @@ -1013,6 +1013,39 @@ static int vsock_getname(struct socket *sock, return err; } =20 +void vsock_linger(struct sock *sk) +{ + DEFINE_WAIT_FUNC(wait, woken_wake_function); + ssize_t (*unsent)(struct vsock_sock *vsk); + struct vsock_sock *vsk =3D vsock_sk(sk); + long timeout; + + if (!sock_flag(sk, SOCK_LINGER)) + return; + + timeout =3D sk->sk_lingertime; + if (!timeout) + return; + + /* Transports must implement `unsent_bytes` if they want to support + * SOCK_LINGER through `vsock_linger()` since we use it to check when + * the socket can be closed. + */ + unsent =3D vsk->transport->unsent_bytes; + if (!unsent) + return; + + add_wait_queue(sk_sleep(sk), &wait); + + do { + if (sk_wait_event(sk, &timeout, unsent(vsk) =3D=3D 0, &wait)) + break; + } while (!signal_pending(current) && timeout); + + remove_wait_queue(sk_sleep(sk), &wait); +} +EXPORT_SYMBOL_GPL(vsock_linger); + static int vsock_shutdown(struct socket *sock, int mode) { int err; diff --git a/net/vmw_vsock/virtio_transport_common.c b/net/vmw_vsock/virtio= _transport_common.c index f2f1b166731b1bf2baa3db2854de19aa331128ea..7897fd970dd867bd2c97a2147e3= a5c853fb514af 100644 --- a/net/vmw_vsock/virtio_transport_common.c +++ b/net/vmw_vsock/virtio_transport_common.c @@ -1191,25 +1191,6 @@ static void virtio_transport_remove_sock(struct vsoc= k_sock *vsk) vsock_remove_sock(vsk); } =20 -static void virtio_transport_wait_close(struct sock *sk, long timeout) -{ - if (timeout) { - DEFINE_WAIT_FUNC(wait, woken_wake_function); - struct vsock_sock *vsk =3D vsock_sk(sk); - - add_wait_queue(sk_sleep(sk), &wait); - - do { - if (sk_wait_event(sk, &timeout, - virtio_transport_unsent_bytes(vsk) =3D=3D 0, - &wait)) - break; - } while (!signal_pending(current) && timeout); - - remove_wait_queue(sk_sleep(sk), &wait); - } -} - static void virtio_transport_cancel_close_work(struct vsock_sock *vsk, bool cancel_timeout) { @@ -1279,8 +1260,8 @@ static bool virtio_transport_close(struct vsock_sock = *vsk) if ((sk->sk_shutdown & SHUTDOWN_MASK) !=3D SHUTDOWN_MASK) (void)virtio_transport_shutdown(vsk, SHUTDOWN_MASK); =20 - if (sock_flag(sk, SOCK_LINGER) && !(current->flags & PF_EXITING)) - virtio_transport_wait_close(sk, sk->sk_lingertime); + if (!(current->flags & PF_EXITING)) + vsock_linger(sk); =20 if (sock_flag(sk, SOCK_DONE)) { return true; --=20 2.49.0 From nobody Fri Dec 19 11:30:24 2025 Received: from mailtransmit04.runbox.com (mailtransmit04.runbox.com [185.226.149.37]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 71AC625FA11 for ; Tue, 20 May 2025 22:56:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.226.149.37 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747781794; cv=none; b=gGZ0ygDvPqa0Vn7GbPF2NrvbRmzsnZNce2TBe+JIf+HIwnYvXr38nIAvTSNFQ4V52lWDIQdyH4iFYjKfr/C23BBkMYmBF+HseY0VvDToKF6C7P+muOVv36EQotvq+DdXObX6bIfqV+jnaHMy5INKgASkXHvaaPMEXPiWrGERGW0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747781794; c=relaxed/simple; bh=LSPBwF/h8fdoRSP5rra5dxI9Jgsh64MRZKZAeajtuFE=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=FNYnWm6E7n09ICLwsCl1iYcvbkR84IWKo2/aEM6p812gNiQUUztE0Vp7vpabcQBqP+SRznx+9QNhDEdk+TpPYJyLr16+gljRxLbz+AtJ09SBGaV//uXvhHhjAO6lq8XCuCSJ7soIK6ign37p59jcLV8NbkqpSewAWF7FYfWiZX0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=rbox.co; spf=pass smtp.mailfrom=rbox.co; dkim=pass (2048-bit key) header.d=rbox.co header.i=@rbox.co header.b=mqyLc1BR; arc=none smtp.client-ip=185.226.149.37 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=rbox.co Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=rbox.co Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=rbox.co header.i=@rbox.co header.b="mqyLc1BR" Received: from mailtransmit03.runbox ([10.9.9.163] helo=aibo.runbox.com) by mailtransmit04.runbox.com with esmtps (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.93) (envelope-from ) id 1uHVsl-001X6G-K1; Wed, 21 May 2025 00:56:23 +0200 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=rbox.co; s=selector2; h=Cc:To:In-Reply-To:References:Message-Id: Content-Transfer-Encoding:Content-Type:MIME-Version:Subject:Date:From; bh=2cwdhH2ahVirQ/fffeWGbWy0yyPkZHiMvGUr/k2S5C4=; b=mqyLc1BRJ+yhwRKHYqH/xUHQr5 EQjpejWv3vjT7gtS1x6749TiMg8CMLyYHsvVs4FygzdfKbanvR80H5azoInwDtOfxeozLYAw1KeOP QQgpl0ROjK0reKxr5ntogy+ueMR70yQCYgdvLaLFLR3nQpxZk5x9y4JOVTxu0lZxAtqHbjT2mSeWD p6gkDjWAaffRAJrNVBAVxqtYyYuzV09ERPCOz8S0/ag+GYvgG/jdg1cEHBlSIX0lxOAhAZRWepNeU gwm1JKNEMTErzpaPa0ReK+5sGxRDVIpOz4nwPx/2ZhtaG+UNl5HhBHNZvAZ/8ODlqXEPhlQ/8uX6c +omqtesg==; Received: from [10.9.9.73] (helo=submission02.runbox) by mailtransmit03.runbox with esmtp (Exim 4.86_2) (envelope-from ) id 1uHVsl-00083t-AZ; Wed, 21 May 2025 00:56:23 +0200 Received: by submission02.runbox with esmtpsa [Authenticated ID (604044)] (TLS1.2:ECDHE_SECP256R1__RSA_PSS_RSAE_SHA256__AES_256_GCM:256) (Exim 4.93) id 1uHVsY-00CxGf-6E; Wed, 21 May 2025 00:56:10 +0200 From: Michal Luczaj Date: Wed, 21 May 2025 00:55:21 +0200 Subject: [PATCH net-next v5 3/5] vsock/test: Introduce vsock_wait_sent() helper Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250521-vsock-linger-v5-3-94827860d1d6@rbox.co> References: <20250521-vsock-linger-v5-0-94827860d1d6@rbox.co> In-Reply-To: <20250521-vsock-linger-v5-0-94827860d1d6@rbox.co> To: Stefano Garzarella , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Simon Horman , "Michael S. Tsirkin" , Jason Wang , Xuan Zhuo , =?utf-8?q?Eugenio_P=C3=A9rez?= , Stefan Hajnoczi Cc: virtualization@lists.linux.dev, netdev@vger.kernel.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Michal Luczaj X-Mailer: b4 0.14.2 Distill the virtio_vsock_sock::bytes_unsent checking loop (ioctl SIOCOUTQ) and move it to utils. Tweak the comment. Signed-off-by: Michal Luczaj --- tools/testing/vsock/util.c | 25 +++++++++++++++++++++++++ tools/testing/vsock/util.h | 1 + tools/testing/vsock/vsock_test.c | 23 ++++++----------------- 3 files changed, 32 insertions(+), 17 deletions(-) diff --git a/tools/testing/vsock/util.c b/tools/testing/vsock/util.c index de25892f865f07672da0886be8bd1a429ade8b05..120277be14ab2f58e0350adcdd5= 6fc18861399c9 100644 --- a/tools/testing/vsock/util.c +++ b/tools/testing/vsock/util.c @@ -17,6 +17,7 @@ #include #include #include +#include =20 #include "timeout.h" #include "control.h" @@ -96,6 +97,30 @@ void vsock_wait_remote_close(int fd) close(epollfd); } =20 +/* Wait until transport reports no data left to be sent. + * Return non-zero if transport does not implement the unsent_bytes() call= back. + */ +int vsock_wait_sent(int fd) +{ + int ret, sock_bytes_unsent; + + timeout_begin(TIMEOUT); + do { + ret =3D ioctl(fd, SIOCOUTQ, &sock_bytes_unsent); + if (ret < 0) { + if (errno =3D=3D EOPNOTSUPP) + break; + + perror("ioctl(SIOCOUTQ)"); + exit(EXIT_FAILURE); + } + timeout_check("SIOCOUTQ"); + } while (sock_bytes_unsent !=3D 0); + timeout_end(); + + return ret; +} + /* Create socket , bind to and return the file descripto= r. */ int vsock_bind(unsigned int cid, unsigned int port, int type) { diff --git a/tools/testing/vsock/util.h b/tools/testing/vsock/util.h index d1f765ce3eeeed8f738630846bb47c4f3f6f946f..e307f0d4f6940e984b84a95fd0d= 57598e7c4e35f 100644 --- a/tools/testing/vsock/util.h +++ b/tools/testing/vsock/util.h @@ -54,6 +54,7 @@ int vsock_stream_listen(unsigned int cid, unsigned int po= rt); int vsock_seqpacket_accept(unsigned int cid, unsigned int port, struct sockaddr_vm *clientaddrp); void vsock_wait_remote_close(int fd); +int vsock_wait_sent(int fd); void send_buf(int fd, const void *buf, size_t len, int flags, ssize_t expected_ret); void recv_buf(int fd, void *buf, size_t len, int flags, ssize_t expected_r= et); diff --git a/tools/testing/vsock/vsock_test.c b/tools/testing/vsock/vsock_t= est.c index 9ea33b78b9fcb532f4f9616b38b4d2b627b04d31..4c2c94151070d54d1ed6e6af5a6= de0b262a0206e 100644 --- a/tools/testing/vsock/vsock_test.c +++ b/tools/testing/vsock/vsock_test.c @@ -21,7 +21,6 @@ #include #include #include -#include #include =20 #include "vsock_test_zerocopy.h" @@ -1280,7 +1279,7 @@ static void test_unsent_bytes_server(const struct tes= t_opts *opts, int type) static void test_unsent_bytes_client(const struct test_opts *opts, int typ= e) { unsigned char buf[MSG_BUF_IOCTL_LEN]; - int ret, fd, sock_bytes_unsent; + int fd; =20 fd =3D vsock_connect(opts->peer_cid, opts->peer_port, type); if (fd < 0) { @@ -1297,22 +1296,12 @@ static void test_unsent_bytes_client(const struct t= est_opts *opts, int type) /* SIOCOUTQ isn't guaranteed to instantly track sent data. Even though * the "RECEIVED" message means that the other side has received the * data, there can be a delay in our kernel before updating the "unsent - * bytes" counter. Repeat SIOCOUTQ until it returns 0. + * bytes" counter. vsock_wait_sent() will repeat SIOCOUTQ until it + * returns 0. */ - timeout_begin(TIMEOUT); - do { - ret =3D ioctl(fd, SIOCOUTQ, &sock_bytes_unsent); - if (ret < 0) { - if (errno =3D=3D EOPNOTSUPP) { - fprintf(stderr, "Test skipped, SIOCOUTQ not supported.\n"); - break; - } - perror("ioctl"); - exit(EXIT_FAILURE); - } - timeout_check("SIOCOUTQ"); - } while (sock_bytes_unsent !=3D 0); - timeout_end(); + if (vsock_wait_sent(fd)) + fprintf(stderr, "Test skipped, SIOCOUTQ not supported.\n"); + close(fd); } =20 --=20 2.49.0 From nobody Fri Dec 19 11:30:24 2025 Received: from mailtransmit05.runbox.com (mailtransmit05.runbox.com [185.226.149.38]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6EC0425FA0E; Tue, 20 May 2025 22:56:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.226.149.38 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747781794; cv=none; b=sfgnA5teRmA7iOWbwVxl1j9PVwobwg3+zWrgWb1+Q2VQUzr9SSB5vR0hoYzgCOQZdeXt7DuN6bIILAYlAcvF92e6WZQmVnc48tHzDiIyXdER6eH8kj1ECJn6MzyBw4pzAPRJZZBe/W2t3uMpqeQA85JSFn9H4BH64/pfzKC7dyo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747781794; c=relaxed/simple; bh=GObFbfN2wVKimWgzJ9n0XeVJJnRB7nn/JUvS5QfYToA=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=DlPdyhh3tD88XPCjhzHKK584iIS5WadKjom258EDfRBUOT1GlnkaE9ZIspwQ90YHJ3uCKpmsfONEgwKFi9OJgy5aOq+s/syLmsO3Zu508i4rNt/hUcT5wa6vprbzMQvs5N+pX9MJ30eErtiCl6DjlfGGZQCs0sD8yR0FlkkFla4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=rbox.co; spf=pass smtp.mailfrom=rbox.co; dkim=pass (2048-bit key) header.d=rbox.co header.i=@rbox.co header.b=bZpY3pKB; arc=none smtp.client-ip=185.226.149.38 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=rbox.co Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=rbox.co Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=rbox.co header.i=@rbox.co header.b="bZpY3pKB" Received: from mailtransmit03.runbox ([10.9.9.163] helo=aibo.runbox.com) by mailtransmit05.runbox.com with esmtps (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.93) (envelope-from ) id 1uHVsk-001MhP-KB; Wed, 21 May 2025 00:56:22 +0200 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=rbox.co; s=selector2; h=Cc:To:In-Reply-To:References:Message-Id: Content-Transfer-Encoding:Content-Type:MIME-Version:Subject:Date:From; bh=Ddvvjug14RF0dGT1UP+nZlt4T3wLWAXlbRy9MG84HCs=; b=bZpY3pKBQC8gEzbquxd94ltjNS QuZWRKqhGby4mM7Ie09oGwO2X23GQ5s2QasdSusi0jj7r1Xbsk7Z+g2vgiB46IpXSaWJn/5GYIXAY hNwynsVWH4gjM0ipRe1bWen9f7hNMgGBcOrAODx47ixVBCJUnTEsbvOFUBromrHLli7VvUlV99grx AYyV5zfDjga4j8kn7MPGqgiqDv/dk6uJaGmBzi1vB8yrWu980nNoEl2O0RPaPnemIa7TWNbmk5GIo wRg0URn0V/bjpMAgaWXBvcJb040LjDlNXgHn5A07rILCqijurvm6tsHUoVAmeMNtrnU9ggtL0py1m //9v2HaA==; Received: from [10.9.9.73] (helo=submission02.runbox) by mailtransmit03.runbox with esmtp (Exim 4.86_2) (envelope-from ) id 1uHVsk-00083n-Ah; Wed, 21 May 2025 00:56:22 +0200 Received: by submission02.runbox with esmtpsa [Authenticated ID (604044)] (TLS1.2:ECDHE_SECP256R1__RSA_PSS_RSAE_SHA256__AES_256_GCM:256) (Exim 4.93) id 1uHVsY-00CxGf-S9; Wed, 21 May 2025 00:56:10 +0200 From: Michal Luczaj Date: Wed, 21 May 2025 00:55:22 +0200 Subject: [PATCH net-next v5 4/5] vsock/test: Introduce enable_so_linger() helper Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250521-vsock-linger-v5-4-94827860d1d6@rbox.co> References: <20250521-vsock-linger-v5-0-94827860d1d6@rbox.co> In-Reply-To: <20250521-vsock-linger-v5-0-94827860d1d6@rbox.co> To: Stefano Garzarella , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Simon Horman , "Michael S. Tsirkin" , Jason Wang , Xuan Zhuo , =?utf-8?q?Eugenio_P=C3=A9rez?= , Stefan Hajnoczi Cc: virtualization@lists.linux.dev, netdev@vger.kernel.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Michal Luczaj X-Mailer: b4 0.14.2 Add a helper function that sets SO_LINGER. Adapt the caller. Signed-off-by: Michal Luczaj --- tools/testing/vsock/util.c | 13 +++++++++++++ tools/testing/vsock/util.h | 4 ++++ tools/testing/vsock/vsock_test.c | 10 +--------- 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/tools/testing/vsock/util.c b/tools/testing/vsock/util.c index 120277be14ab2f58e0350adcdd56fc18861399c9..41b47f7deadcda68fddc2b22a6d= 9bb7847cc0a14 100644 --- a/tools/testing/vsock/util.c +++ b/tools/testing/vsock/util.c @@ -823,3 +823,16 @@ void enable_so_zerocopy_check(int fd) setsockopt_int_check(fd, SOL_SOCKET, SO_ZEROCOPY, 1, "setsockopt SO_ZEROCOPY"); } + +void enable_so_linger(int fd) +{ + struct linger optval =3D { + .l_onoff =3D 1, + .l_linger =3D LINGER_TIMEOUT + }; + + if (setsockopt(fd, SOL_SOCKET, SO_LINGER, &optval, sizeof(optval))) { + perror("setsockopt(SO_LINGER)"); + exit(EXIT_FAILURE); + } +} diff --git a/tools/testing/vsock/util.h b/tools/testing/vsock/util.h index e307f0d4f6940e984b84a95fd0d57598e7c4e35f..1b3d8eb2c4b3c41c90075841774= 55c4fa442334c 100644 --- a/tools/testing/vsock/util.h +++ b/tools/testing/vsock/util.h @@ -14,6 +14,9 @@ enum test_mode { =20 #define DEFAULT_PEER_PORT 1234 =20 +/* Half of the default to not risk timing out the control channel */ +#define LINGER_TIMEOUT (TIMEOUT / 2) + /* Test runner options */ struct test_opts { enum test_mode mode; @@ -80,4 +83,5 @@ void setsockopt_int_check(int fd, int level, int optname,= int val, void setsockopt_timeval_check(int fd, int level, int optname, struct timeval val, char const *errmsg); void enable_so_zerocopy_check(int fd); +void enable_so_linger(int fd); #endif /* UTIL_H */ diff --git a/tools/testing/vsock/vsock_test.c b/tools/testing/vsock/vsock_t= est.c index 4c2c94151070d54d1ed6e6af5a6de0b262a0206e..f401c6a79495bc7fda97012e5bf= eabec7dbfb60a 100644 --- a/tools/testing/vsock/vsock_test.c +++ b/tools/testing/vsock/vsock_test.c @@ -1813,10 +1813,6 @@ static void test_stream_connect_retry_server(const s= truct test_opts *opts) =20 static void test_stream_linger_client(const struct test_opts *opts) { - struct linger optval =3D { - .l_onoff =3D 1, - .l_linger =3D 1 - }; int fd; =20 fd =3D vsock_stream_connect(opts->peer_cid, opts->peer_port); @@ -1825,11 +1821,7 @@ static void test_stream_linger_client(const struct t= est_opts *opts) exit(EXIT_FAILURE); } =20 - if (setsockopt(fd, SOL_SOCKET, SO_LINGER, &optval, sizeof(optval))) { - perror("setsockopt(SO_LINGER)"); - exit(EXIT_FAILURE); - } - + enable_so_linger(fd); close(fd); } =20 --=20 2.49.0 From nobody Fri Dec 19 11:30:24 2025 Received: from mailtransmit04.runbox.com (mailtransmit04.runbox.com [185.226.149.37]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9FEA725A322 for ; Tue, 20 May 2025 22:56:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.226.149.37 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747781793; cv=none; b=QZstDMt8TLxv6v741k+PzDh/TVjlENT9fFmFG8vGI633MltGApTUU6Quhl5L9aLkFzti7a136R+Qh1O7mnmti0GoGntOkJ00pprc/0HPCVKxLoKJ1VsngHPW6fhsoqz1ODtkP4mDncibDN0o6q8Wp+kF05PDlL4DU8/GyOpNjMM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747781793; c=relaxed/simple; bh=wQQubEBib3byNdfTnd7ko0vhCud06Uio7n09JluN5A0=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=oS0LH9w9gLNeOzmHePQiPGL0uZGfJTTegEiJFE9UNsXvFbG86W91fNl/pXsPebofrmplkUcujYswY67tOghqUfjkyOIGFXpYaC5sE240PW9hIuYQIPKe2yhfIVh0XxbFp6dLbhIrezYlav6IpVHX2q34ZiyOL+fqGTmEK+cMyVE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=rbox.co; spf=pass smtp.mailfrom=rbox.co; dkim=pass (2048-bit key) header.d=rbox.co header.i=@rbox.co header.b=a1zwU4lC; arc=none smtp.client-ip=185.226.149.37 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=rbox.co Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=rbox.co Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=rbox.co header.i=@rbox.co header.b="a1zwU4lC" Received: from mailtransmit02.runbox ([10.9.9.162] helo=aibo.runbox.com) by mailtransmit04.runbox.com with esmtps (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.93) (envelope-from ) id 1uHVsi-001X5G-Em; Wed, 21 May 2025 00:56:20 +0200 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=rbox.co; s=selector2; h=Cc:To:In-Reply-To:References:Message-Id: Content-Transfer-Encoding:Content-Type:MIME-Version:Subject:Date:From; bh=J8ECBmbAYxlbH2+qPMrZpA1tVV7W3mma7W+7fWFHowY=; b=a1zwU4lCjIw4rl0eCGmePhdG70 Kl5MZpMWS3dDQKCbhrvJuYTJICmXOGRkgD6tSjAQ8IiGd6MX/2NVB3J/JEe3jmdNkaYOXfkBX1HOF titg1QEnk4LOFk22l1ttevdjoS2E559+ym1/Zr00rZO5NdIEoVjphwk1Hp3p0R8gUlnrzJLBuROj+ pppmsVvoNZRMHuK8sqZagfc63xgF3JBR7NtfRNnTW8LiXj7cX8GoI+d5HEpzwTzvSyIYrPX1mf6N7 DnETfPryVxqiiBBRXqyfJ9Mx6vMFCvgEmhtHpV6ve+b6EDEpej9FLZ0Z8g+vSHgijXUKU4ri7JPAj 6F2jVpdA==; Received: from [10.9.9.73] (helo=submission02.runbox) by mailtransmit02.runbox with esmtp (Exim 4.86_2) (envelope-from ) id 1uHVsi-0007nJ-6I; Wed, 21 May 2025 00:56:20 +0200 Received: by submission02.runbox with esmtpsa [Authenticated ID (604044)] (TLS1.2:ECDHE_SECP256R1__RSA_PSS_RSAE_SHA256__AES_256_GCM:256) (Exim 4.93) id 1uHVsZ-00CxGf-Hx; Wed, 21 May 2025 00:56:11 +0200 From: Michal Luczaj Date: Wed, 21 May 2025 00:55:23 +0200 Subject: [PATCH net-next v5 5/5] vsock/test: Add test for an unexpectedly lingering close() Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250521-vsock-linger-v5-5-94827860d1d6@rbox.co> References: <20250521-vsock-linger-v5-0-94827860d1d6@rbox.co> In-Reply-To: <20250521-vsock-linger-v5-0-94827860d1d6@rbox.co> To: Stefano Garzarella , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Simon Horman , "Michael S. Tsirkin" , Jason Wang , Xuan Zhuo , =?utf-8?q?Eugenio_P=C3=A9rez?= , Stefan Hajnoczi Cc: virtualization@lists.linux.dev, netdev@vger.kernel.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Michal Luczaj X-Mailer: b4 0.14.2 There was an issue with SO_LINGER: instead of blocking until all queued messages for the socket have been successfully sent (or the linger timeout has been reached), close() would block until packets were handled by the peer. Add a test to alert on close() lingering when it should not. Signed-off-by: Michal Luczaj --- tools/testing/vsock/vsock_test.c | 49 ++++++++++++++++++++++++++++++++++++= ++++ 1 file changed, 49 insertions(+) diff --git a/tools/testing/vsock/vsock_test.c b/tools/testing/vsock/vsock_t= est.c index f401c6a79495bc7fda97012e5bfeabec7dbfb60a..1040503333cf315e52592c876f2= c1809b36fdfdb 100644 --- a/tools/testing/vsock/vsock_test.c +++ b/tools/testing/vsock/vsock_test.c @@ -1839,6 +1839,50 @@ static void test_stream_linger_server(const struct t= est_opts *opts) close(fd); } =20 +static void test_stream_nolinger_client(const struct test_opts *opts) +{ + bool nowait; + time_t ns; + int fd; + + fd =3D vsock_stream_connect(opts->peer_cid, opts->peer_port); + if (fd < 0) { + perror("connect"); + exit(EXIT_FAILURE); + } + + enable_so_linger(fd); + send_byte(fd, 1, 0); /* Left unread to expose incorrect behaviour. */ + nowait =3D vsock_wait_sent(fd); + + ns =3D current_nsec(); + close(fd); + ns =3D current_nsec() - ns; + + if (nowait) { + fprintf(stderr, "Test skipped, SIOCOUTQ not supported.\n"); + } else if ((ns + NSEC_PER_SEC - 1) / NSEC_PER_SEC >=3D LINGER_TIMEOUT) { + fprintf(stderr, "Unexpected lingering\n"); + exit(EXIT_FAILURE); + } + + control_writeln("DONE"); +} + +static void test_stream_nolinger_server(const struct test_opts *opts) +{ + int fd; + + fd =3D vsock_stream_accept(VMADDR_CID_ANY, opts->peer_port, NULL); + if (fd < 0) { + perror("accept"); + exit(EXIT_FAILURE); + } + + control_expectln("DONE"); + close(fd); +} + static struct test_case test_cases[] =3D { { .name =3D "SOCK_STREAM connection reset", @@ -1999,6 +2043,11 @@ static struct test_case test_cases[] =3D { .run_client =3D test_stream_linger_client, .run_server =3D test_stream_linger_server, }, + { + .name =3D "SOCK_STREAM SO_LINGER close() on unread", + .run_client =3D test_stream_nolinger_client, + .run_server =3D test_stream_nolinger_server, + }, {}, }; =20 --=20 2.49.0