Skip to contentSkip to conversation

vsock: SOCK_LINGER rework

PATCH Superseded Michal Luczaj 2 participants 15 messages May 1, 2025 – May 20, 2025

Subsystems (6)

v4 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v4 cover vsock: SOCK_LINGER rework

Change vsock's lingerning to wait on close() until all data is sent, i.e. until workers picked all the packets for processing.

Changes in v4:
- While in virtio, stick to virtio_transport_unsent_bytes() [Stefano]
- Squash the indentation reduction [Stefano]
- Pull SOCK_LINGER check into vsock_linger() [Stefano]
- Don't explicitly pass sk->sk_lingertime [Stefano]
- Link to v3: https://lore.kernel.org/r/[email protected]

Changes in v3:
- Set "vsock/virtio" topic where appropriate
- Do not claim that Hyper-V and VMCI ever lingered [Stefano]
- Move lingering to af_vsock core [Stefano]
- Link to v2: https://lore.kernel.org/r/[email protected]

Changes in v2:
- Comment that some transports do not implement unsent_bytes [Stefano]
- Reduce the indentation of virtio_transport_wait_close() [Stefano]
- Do not linger on shutdown(), expand the commit messages [Paolo]
- Link to v1: https://lore.kernel.org/r/[email protected]

Changes in v1:
- Do not assume unsent_bytes() is implemented by all transports [Stefano]
- Link to v0: https://lore.kernel.org/netdev/[email protected]/

---
Michal Luczaj (3):
vsock/virtio: Linger on unsent data
vsock: Move lingering logic to af_vsock core vsock/test: Expand linger test to ensure close() does not misbehave

 include/net/af_vsock.h                  |  1 +
 net/vmw_vsock/af_vsock.c                | 30 ++++++++++++++++++++++++++++++
 net/vmw_vsock/virtio_transport_common.c | 21 ++-------------------
 tools/testing/vsock/vsock_test.c        | 30 +++++++++++++++++++++++++++---
 4 files changed, 60 insertions(+), 22 deletions(-)

---
base-commit: deeed351e982ac4d521598375b34b071304533b0
change-id: 20250304-vsock-linger-9026e5f9986c

Best regards,

Signature · 1 line
Michal Luczaj <[email protected]>
#1 lore raw

PATCH v4 1/3 vsock/virtio: Linger on unsent data

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.

---
 net/vmw_vsock/virtio_transport_common.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
net/vmw_vsock/virtio_transport_common.c +3 -1
index 7f7de6d8809655fe522749fbbc9025df71f071bd..045ac53f69735e1979162aea8c9ab5961407640c 100644
@@ -1196,12 +1196,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 = vsock_sk(sk);
 
 		add_wait_queue(sk_sleep(sk), &wait);
 
 		do {
 			if (sk_wait_event(sk, &timeout,
-					  sock_flag(sk, SOCK_DONE), &wait))
+					  virtio_transport_unsent_bytes(vsk) == 0,
+					  &wait))
 				break;
 		} while (!signal_pending(current) && timeout);
 
Signature · 1 line
2.49.0
maintainer Reviewed-by on v4 1/3
#2 lore raw

Re: [PATCH net-next v4 1/3] vsock/virtio: Linger on unsent data

Quoted 16 lines · Michal Luczaj

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.

Quoted 7 lines

The implementation does not adhere strictly to man page's interpretation of
SO_LINGER: shutdown() will not trigger the lingering. This follows AF_INET.

---
net/vmw_vsock/virtio_transport_common.c | 4 +++-

1 file changed, 3 insertions(+), 1 deletion(-)
Quoted 25 lines
net/vmw_vsock/virtio_transport_common.c +3 -1
index 7f7de6d8809655fe522749fbbc9025df71f071bd..045ac53f69735e1979162aea8c9ab5961407640c 100644
@@ -1196,12 +1196,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 = vsock_sk(sk);
 
 		add_wait_queue(sk_sleep(sk), &wait);
 
 		do {
 			if (sk_wait_event(sk, &timeout,
-					  sock_flag(sk, SOCK_DONE), &wait))
+					  virtio_transport_unsent_bytes(vsk) == 0,
+					  &wait))
 				break;
 		} while (!signal_pending(current) && timeout);
 
Signature · 2 lines
-- 
2.49.0
#3 lore raw

PATCH v4 2/3 vsock: Move lingering logic to af_vsock core

In reply to Michal Luczaj

Lingering should be transport-independent in the long run. In preparation for supporting other transports, as well 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.

---
 include/net/af_vsock.h                  |  1 +
 net/vmw_vsock/af_vsock.c                | 30 ++++++++++++++++++++++++++++++
 net/vmw_vsock/virtio_transport_common.c | 23 ++---------------------
 3 files changed, 33 insertions(+), 21 deletions(-)
include/net/af_vsock.h +1 -0
index 9e85424c834353d016a527070dd62e15ff3bfce1..d56e6e135158939087d060dfcf65d3fdaea53bf3 100644
@@ -221,6 +221,7 @@ void vsock_for_each_connected_socket(struct vsock_transport *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);
 
 /**** TAP ****/
 
net/vmw_vsock/af_vsock.c +30 -0
index fc6afbc8d6806a4d98c66abc3af4bd139c583b08..a31ad6b141cd38d1806df4b5d417924bb8607e32 100644
@@ -1013,6 +1013,36 @@ static int vsock_getname(struct socket *sock,
 	return err;
 }
 
+void vsock_linger(struct sock *sk)
+{
+	DEFINE_WAIT_FUNC(wait, woken_wake_function);
+	ssize_t (*unsent)(struct vsock_sock *vsk);
+	struct vsock_sock *vsk = vsock_sk(sk);
+	long timeout;
+
+	if (!sock_flag(sk, SOCK_LINGER))
+		return;
+
+	timeout = sk->sk_lingertime;
+	if (!timeout)
+		return;
+
+	/* unsent_bytes() may be unimplemented. */
+	unsent = vsk->transport->unsent_bytes;
+	if (!unsent)
+		return;
+
+	add_wait_queue(sk_sleep(sk), &wait);
+
+	do {
+		if (sk_wait_event(sk, &timeout, unsent(vsk) == 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;
net/vmw_vsock/virtio_transport_common.c +2 -21
index 045ac53f69735e1979162aea8c9ab5961407640c..aa308f285bf1bcf4c689407033de854c6f85a639 100644
@@ -1192,25 +1192,6 @@ static void virtio_transport_remove_sock(struct vsock_sock *vsk)
 	vsock_remove_sock(vsk);
 }
 
-static void virtio_transport_wait_close(struct sock *sk, long timeout)
-{
-	if (timeout) {
-		DEFINE_WAIT_FUNC(wait, woken_wake_function);
-		struct vsock_sock *vsk = vsock_sk(sk);
-
-		add_wait_queue(sk_sleep(sk), &wait);
-
-		do {
-			if (sk_wait_event(sk, &timeout,
-					  virtio_transport_unsent_bytes(vsk) == 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)
 {
@@ -1280,8 +1261,8 @@ static bool virtio_transport_close(struct vsock_sock *vsk)
 	if ((sk->sk_shutdown & SHUTDOWN_MASK) != SHUTDOWN_MASK)
 		(void)virtio_transport_shutdown(vsk, SHUTDOWN_MASK);
 
-	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);
 
 	if (sock_flag(sk, SOCK_DONE)) {
 		return true;
Signature · 1 line
2.49.0
#4 lore raw

Re: [PATCH net-next v4 2/3] vsock: Move lingering logic to af_vsock core

Quoted 44 lines · Michal Luczaj

Lingering should be transport-independent in the long run. In preparation for supporting other transports, as well 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.

---

include/net/af_vsock.h                  |  1 +
net/vmw_vsock/af_vsock.c                | 30 ++++++++++++++++++++++++++++++
net/vmw_vsock/virtio_transport_common.c | 23 ++---------------------
3 files changed, 33 insertions(+), 21 deletions(-)
include/net/af_vsock.h +1 -0
index 9e85424c834353d016a527070dd62e15ff3bfce1..d56e6e135158939087d060dfcf65d3fdaea53bf3 100644
@@ -221,6 +221,7 @@ void vsock_for_each_connected_socket(struct vsock_transport *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);
 
 /**** TAP ****/
 
net/vmw_vsock/af_vsock.c +5 -0
index fc6afbc8d6806a4d98c66abc3af4bd139c583b08..a31ad6b141cd38d1806df4b5d417924bb8607e32 100644
@@ -1013,6 +1013,36 @@ static int vsock_getname(struct socket *sock,
 	return err;
 }
 
+void vsock_linger(struct sock *sk)
+{
+	DEFINE_WAIT_FUNC(wait, woken_wake_function);
+	ssize_t (*unsent)(struct vsock_sock *vsk);
+	struct vsock_sock *vsk = vsock_sk(sk);

net/vmw_vsock/af_vsock.c static int vsock_getname(struct socket *sock, · line 1030

Quoted 10 lines
net/vmw_vsock/af_vsock.c +10 -0
+	long timeout;
+
+	if (!sock_flag(sk, SOCK_LINGER))
+		return;
+
+	timeout = sk->sk_lingertime;
+	if (!timeout)
+		return;
+
+	/* unsent_bytes() may be unimplemented. */

This comment IMO should be enriched, as it is now it doesn't add much to the code. I'm thinking on something like this:
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.

The rest LGTM!

Thanks,
Stefano

Quoted 63 lines
net/vmw_vsock/af_vsock.c +15 -0
+	unsent = vsk->transport->unsent_bytes;
+	if (!unsent)
+		return;
+
+	add_wait_queue(sk_sleep(sk), &wait);
+
+	do {
+		if (sk_wait_event(sk, &timeout, unsent(vsk) == 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;
net/vmw_vsock/virtio_transport_common.c +2 -21
index 045ac53f69735e1979162aea8c9ab5961407640c..aa308f285bf1bcf4c689407033de854c6f85a639 100644
@@ -1192,25 +1192,6 @@ static void virtio_transport_remove_sock(struct vsock_sock *vsk)
 	vsock_remove_sock(vsk);
 }
 
-static void virtio_transport_wait_close(struct sock *sk, long timeout)
-{
-	if (timeout) {
-		DEFINE_WAIT_FUNC(wait, woken_wake_function);
-		struct vsock_sock *vsk = vsock_sk(sk);
-
-		add_wait_queue(sk_sleep(sk), &wait);
-
-		do {
-			if (sk_wait_event(sk, &timeout,
-					  virtio_transport_unsent_bytes(vsk) == 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)
 {
@@ -1280,8 +1261,8 @@ static bool virtio_transport_close(struct vsock_sock *vsk)
 	if ((sk->sk_shutdown & SHUTDOWN_MASK) != SHUTDOWN_MASK)
 		(void)virtio_transport_shutdown(vsk, SHUTDOWN_MASK);
 
-	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);
 
 	if (sock_flag(sk, SOCK_DONE)) {
 		return true;
Signature · 2 lines
-- 
2.49.0
#5 lore raw

Re: [PATCH net-next v4 2/3] vsock: Move lingering logic to af_vsock core

Quoted 54 lines · Stefano Garzarella
Quoted 53 lines · Michal Luczaj

Lingering should be transport-independent in the long run. In preparation for supporting other transports, as well 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.

---

include/net/af_vsock.h                  |  1 +
net/vmw_vsock/af_vsock.c                | 30 ++++++++++++++++++++++++++++++
net/vmw_vsock/virtio_transport_common.c | 23 ++---------------------
3 files changed, 33 insertions(+), 21 deletions(-)
include/net/af_vsock.h +0 -0
index 9e85424c834353d016a527070dd62e15ff3bfce1..d56e6e135158939087d060dfcf65d3fdaea53bf3 100644
@@ -221,6 +221,7 @@ void vsock_for_each_connected_socket(struct vsock_transport *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);

/**** TAP ****/

net/vmw_vsock/af_vsock.c +13 -0
index fc6afbc8d6806a4d98c66abc3af4bd139c583b08..a31ad6b141cd38d1806df4b5d417924bb8607e32 100644
@@ -1013,6 +1013,36 @@ static int vsock_getname(struct socket *sock,
 	return err;
 }
 
+void vsock_linger(struct sock *sk)
+{
+	DEFINE_WAIT_FUNC(wait, woken_wake_function);
+	ssize_t (*unsent)(struct vsock_sock *vsk);
+	struct vsock_sock *vsk = vsock_sk(sk);
+	long timeout;
+
+	if (!sock_flag(sk, SOCK_LINGER))
+		return;
+
+	timeout = sk->sk_lingertime;
+	if (!timeout)
+		return;
Quoted 8 lines

net/vmw_vsock/af_vsock.c static int vsock_getname(struct socket *sock, · line 1030

Quoted 2 lines
net/vmw_vsock/af_vsock.c +2 -0
+
+	/* unsent_bytes() may be unimplemented. */

This comment IMO should be enriched, as it is now it doesn't add much to the code. I'm thinking on something like this:
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.

OK, will do.

Thanks,
Michal

#6 lore raw

PATCH v4 3/3 vsock/test: Expand linger test to ensure close() does not misbehave

In reply to Michal Luczaj

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 check to alert on close() lingering when it should not.

---
 tools/testing/vsock/vsock_test.c | 30 +++++++++++++++++++++++++++---
 1 file changed, 27 insertions(+), 3 deletions(-)
tools/testing/vsock/vsock_test.c +27 -3
index d0f6d253ac72d08a957cb81a3c38fcc72bec5a53..82d0bc20dfa75041f04eada1b4310be2f7c3a0c1 100644
@@ -1788,13 +1788,16 @@ static void test_stream_connect_retry_server(const struct test_opts *opts)
 	close(fd);
 }
 
+#define	LINGER_TIMEOUT	1	/* seconds */
+
 static void test_stream_linger_client(const struct test_opts *opts)
 {
 	struct linger optval = {
 		.l_onoff = 1,
-		.l_linger = 1
+		.l_linger = LINGER_TIMEOUT
 	};
-	int fd;
+	int bytes_unsent, fd;
+	time_t ts;
 
 	fd = vsock_stream_connect(opts->peer_cid, opts->peer_port);
 	if (fd < 0) {
@@ -1807,7 +1810,28 @@ static void test_stream_linger_client(const struct test_opts *opts)
 		exit(EXIT_FAILURE);
 	}
 
+	/* Byte left unread to expose any incorrect behaviour. */
+	send_byte(fd, 1, 0);
+
+	/* Reuse LINGER_TIMEOUT to wait for bytes_unsent == 0. */
+	timeout_begin(LINGER_TIMEOUT);
+	do {
+		if (ioctl(fd, SIOCOUTQ, &bytes_unsent) < 0) {
+			perror("ioctl(SIOCOUTQ)");
+			exit(EXIT_FAILURE);
+		}
+		timeout_check("ioctl(SIOCOUTQ) == 0");
+	} while (bytes_unsent != 0);
+	timeout_end();
+
+	ts = current_nsec();
 	close(fd);
+	if ((current_nsec() - ts) / NSEC_PER_SEC > 0) {
+		fprintf(stderr, "Unexpected lingering on close()\n");
+		exit(EXIT_FAILURE);
+	}
+
+	control_writeln("DONE");
 }
 
 static void test_stream_linger_server(const struct test_opts *opts)
@@ -1820,7 +1844,7 @@ static void test_stream_linger_server(const struct test_opts *opts)
 		exit(EXIT_FAILURE);
 	}
 
-	vsock_wait_remote_close(fd);
+	control_expectln("DONE");
 	close(fd);
 }
 
Signature · 1 line
2.49.0
#7 lore raw

Re: [PATCH net-next v4 3/3] vsock/test: Expand linger test to ensure close() does not misbehave

Quoted 4 lines · Michal Luczaj

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.

This is a new behaviour that only new kernels will follow, so I think it is better to add a new test instead of extending a pre-existing test that we described as "SOCK_STREAM SO_LINGER null-ptr-deref".

The old test should continue to check the null-ptr-deref also for old kernels, while the new test will check the new behaviour, so we can skip the new test while testing an old kernel.

Thanks,
Stefano

Quoted 73 lines

Add a check to alert on close() lingering when it should not.

---
tools/testing/vsock/vsock_test.c | 30 +++++++++++++++++++++++++++---

1 file changed, 27 insertions(+), 3 deletions(-)
tools/testing/vsock/vsock_test.c +27 -3
index d0f6d253ac72d08a957cb81a3c38fcc72bec5a53..82d0bc20dfa75041f04eada1b4310be2f7c3a0c1 100644
@@ -1788,13 +1788,16 @@ static void test_stream_connect_retry_server(const struct test_opts *opts)
 	close(fd);
 }
 
+#define	LINGER_TIMEOUT	1	/* seconds */
+
 static void test_stream_linger_client(const struct test_opts *opts)
 {
 	struct linger optval = {
 		.l_onoff = 1,
-		.l_linger = 1
+		.l_linger = LINGER_TIMEOUT
 	};
-	int fd;
+	int bytes_unsent, fd;
+	time_t ts;
 
 	fd = vsock_stream_connect(opts->peer_cid, opts->peer_port);
 	if (fd < 0) {
@@ -1807,7 +1810,28 @@ static void test_stream_linger_client(const struct test_opts *opts)
 		exit(EXIT_FAILURE);
 	}
 
+	/* Byte left unread to expose any incorrect behaviour. */
+	send_byte(fd, 1, 0);
+
+	/* Reuse LINGER_TIMEOUT to wait for bytes_unsent == 0. */
+	timeout_begin(LINGER_TIMEOUT);
+	do {
+		if (ioctl(fd, SIOCOUTQ, &bytes_unsent) < 0) {
+			perror("ioctl(SIOCOUTQ)");
+			exit(EXIT_FAILURE);
+		}
+		timeout_check("ioctl(SIOCOUTQ) == 0");
+	} while (bytes_unsent != 0);
+	timeout_end();
+
+	ts = current_nsec();
 	close(fd);
+	if ((current_nsec() - ts) / NSEC_PER_SEC > 0) {
+		fprintf(stderr, "Unexpected lingering on close()\n");
+		exit(EXIT_FAILURE);
+	}
+
+	control_writeln("DONE");
 }
 
 static void test_stream_linger_server(const struct test_opts *opts)
@@ -1820,7 +1844,7 @@ static void test_stream_linger_server(const struct test_opts *opts)
 		exit(EXIT_FAILURE);
 	}
 
-	vsock_wait_remote_close(fd);
+	control_expectln("DONE");
 	close(fd);
 }
 
Signature · 2 lines
-- 
2.49.0
#8 lore raw

Re: [PATCH net-next v4 3/3] vsock/test: Expand linger test to ensure close() does not misbehave

Quoted 7 lines · Stefano Garzarella
Quoted 4 lines · Michal Luczaj

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.

Quoted 7 lines

This is a new behaviour that only new kernels will follow, so I think it is better to add a new test instead of extending a pre-existing test that we described as "SOCK_STREAM SO_LINGER null-ptr-deref".

The old test should continue to check the null-ptr-deref also for old kernels, while the new test will check the new behaviour, so we can skip the new test while testing an old kernel.

I also saw that we don't have any test to verify that actually the lingering is working, should we add it since we are touching it?

Thanks,
Stefano

#9 lore raw

Re: [PATCH net-next v4 3/3] vsock/test: Expand linger test to ensure close() does not misbehave

Quoted 8 lines · Stefano Garzarella
Quoted 7 lines · Stefano Garzarella
Quoted 4 lines · Michal Luczaj

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.

Quoted 7 lines
Quoted 7 lines

This is a new behaviour that only new kernels will follow, so I think it is better to add a new test instead of extending a pre-existing test that we described as "SOCK_STREAM SO_LINGER null-ptr-deref".

The old test should continue to check the null-ptr-deref also for old kernels, while the new test will check the new behaviour, so we can skip the new test while testing an old kernel.

Right, I'll split it.

Quoted 2 lines

I also saw that we don't have any test to verify that actually the lingering is working, should we add it since we are touching it?

Yeah, I agree we should. Do you have any suggestion how this could be done reliably?

Thanks,
Michal

#10 lore raw

Re: [PATCH net-next v4 3/3] vsock/test: Expand linger test to ensure close() does not misbehave

Quoted 18 lines · Michal Luczaj
Quoted 8 lines · Stefano Garzarella
Quoted 7 lines · Stefano Garzarella
Quoted 4 lines · Michal Luczaj

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.

Quoted 7 lines
Quoted 7 lines

This is a new behaviour that only new kernels will follow, so I think it is better to add a new test instead of extending a pre-existing test that we described as "SOCK_STREAM SO_LINGER null-ptr-deref".

The old test should continue to check the null-ptr-deref also for old kernels, while the new test will check the new behaviour, so we can skip the new test while testing an old kernel.

Quoted 7 lines

Right, I'll split it.

Quoted 2 lines

I also saw that we don't have any test to verify that actually the lingering is working, should we add it since we are touching it?

Yeah, I agree we should. Do you have any suggestion how this could be done reliably?

Can we play with SO_VM_SOCKETS_BUFFER_SIZE like in credit-update tests?

One peer can set it (e.g. to 1k), accept the connection, but without read anything. The other peer can set the linger timeout, send more bytes than the buffer size set by the receiver.
At this point the extra bytes should stay on the sender socket buffer, so we can do the close() and it should time out, and we can check if it happens.

WDYT?

Thanks,
Stefano

#11 lore raw

Re: [PATCH net-next v4 3/3] vsock/test: Expand linger test to ensure close() does not misbehave

Quoted 29 lines · Stefano Garzarella
Quoted 18 lines · Michal Luczaj
Quoted 8 lines · Stefano Garzarella
Quoted 7 lines · Stefano Garzarella
Quoted 4 lines · Michal Luczaj

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.

Quoted 7 lines
Quoted 7 lines

This is a new behaviour that only new kernels will follow, so I think it is better to add a new test instead of extending a pre-existing test that we described as "SOCK_STREAM SO_LINGER null-ptr-deref".

The old test should continue to check the null-ptr-deref also for old kernels, while the new test will check the new behaviour, so we can skip the new test while testing an old kernel.

Quoted 7 lines

Right, I'll split it.

Quoted 2 lines

I also saw that we don't have any test to verify that actually the lingering is working, should we add it since we are touching it?

Yeah, I agree we should. Do you have any suggestion how this could be done reliably?

Can we play with SO_VM_SOCKETS_BUFFER_SIZE like in credit-update tests?

Quoted 8 lines

One peer can set it (e.g. to 1k), accept the connection, but without read anything. The other peer can set the linger timeout, send more bytes than the buffer size set by the receiver.
At this point the extra bytes should stay on the sender socket buffer, so we can do the close() and it should time out, and we can check if it happens.

WDYT?

Haven't we discussed this approach in [1]? I've reported that I can't make it work. But maybe I'm misunderstanding something, please see the code below.

[1]:
https://lore.kernel.org/netdev/[email protected]/

import termios, time
from socket import *

SIOCOUTQ = termios.TIOCOUTQ
VMADDR_CID_LOCAL = 1
SZ = 1024

def set_linger(s, timeout):

	optval = (timeout << 32) | 1
	s.setsockopt(SOL_SOCKET, SO_LINGER, optval)
	assert s.getsockopt(SOL_SOCKET, SO_LINGER) == optval

def set_bufsz(s, size):

	s.setsockopt(AF_VSOCK, SO_VM_SOCKETS_BUFFER_SIZE, size)
	assert s.getsockopt(AF_VSOCK, SO_VM_SOCKETS_BUFFER_SIZE) == size

def check_lingering(addr):

	lis = socket(AF_VSOCK, SOCK_STREAM)
	lis.bind(addr)
	lis.listen()
	set_bufsz(lis, SZ)

	s = socket(AF_VSOCK, SOCK_STREAM)
	set_linger(s, 5)
	s.connect(lis.getsockname())

	p, _ = lis.accept()

	s.send(b'x')
	p.recv(1)

	print("sending...")
	s.send(b'x' * (SZ+1)) # blocks
	print("sent")

	print("closing...")
	ts = time.time()
	s.close()
	print("done in %ds" % (time.time() - ts))

check_lingering((VMADDR_CID_LOCAL, 1234))

#12 lore raw

Re: [PATCH net-next v4 3/3] vsock/test: Expand linger test to ensure close() does not misbehave

Quoted 37 lines · Michal Luczaj
Quoted 36 lines · Stefano Garzarella
Quoted 18 lines · Michal Luczaj
Quoted 8 lines · Stefano Garzarella
Quoted 7 lines · Stefano Garzarella
Quoted 4 lines · Michal Luczaj

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.

Quoted 7 lines
Quoted 7 lines

This is a new behaviour that only new kernels will follow, so I think it is better to add a new test instead of extending a pre-existing test that we described as "SOCK_STREAM SO_LINGER null-ptr-deref".

The old test should continue to check the null-ptr-deref also for old kernels, while the new test will check the new behaviour, so we can skip the new test while testing an old kernel.

Quoted 7 lines

Right, I'll split it.

Quoted 2 lines

I also saw that we don't have any test to verify that actually the lingering is working, should we add it since we are touching it?

Yeah, I agree we should. Do you have any suggestion how this could be done reliably?

Can we play with SO_VM_SOCKETS_BUFFER_SIZE like in credit-update tests?

One peer can set it (e.g. to 1k), accept the connection, but without read anything. The other peer can set the linger timeout, send more bytes than the buffer size set by the receiver.
At this point the extra bytes should stay on the sender socket buffer, so we can do the close() and it should time out, and we can check if it happens.

Quoted 3 lines
Quoted 1 line

WDYT?

Haven't we discussed this approach in [1]? I've reported that I can't make

Sorry, I forgot. What was the conclusion? Why this can't work?

Quoted 1 line

it work. But maybe I'm misunderstanding something, please see the code below.

What I should check in the code below?

Thanks,
Stefano

Quoted 46 lines

[1]:
https://lore.kernel.org/netdev/[email protected]/

import termios, time
from socket import *

SIOCOUTQ = termios.TIOCOUTQ
VMADDR_CID_LOCAL = 1
SZ = 1024

def set_linger(s, timeout):

	optval = (timeout << 32) | 1
	s.setsockopt(SOL_SOCKET, SO_LINGER, optval)
	assert s.getsockopt(SOL_SOCKET, SO_LINGER) == optval

def set_bufsz(s, size):

	s.setsockopt(AF_VSOCK, SO_VM_SOCKETS_BUFFER_SIZE, size)
	assert s.getsockopt(AF_VSOCK, SO_VM_SOCKETS_BUFFER_SIZE) == size

def check_lingering(addr):

	lis = socket(AF_VSOCK, SOCK_STREAM)
	lis.bind(addr)
	lis.listen()
	set_bufsz(lis, SZ)

	s = socket(AF_VSOCK, SOCK_STREAM)
	set_linger(s, 5)
	s.connect(lis.getsockname())

	p, _ = lis.accept()

	s.send(b'x')
	p.recv(1)

	print("sending...")
	s.send(b'x' * (SZ+1)) # blocks
	print("sent")

	print("closing...")
	ts = time.time()
	s.close()
	print("done in %ds" % (time.time() - ts))

check_lingering((VMADDR_CID_LOCAL, 1234))

#13 lore raw

Re: [PATCH net-next v4 3/3] vsock/test: Expand linger test to ensure close() does not misbehave

Quoted 41 lines · Stefano Garzarella
Quoted 39 lines · Michal Luczaj
Quoted 37 lines · Stefano Garzarella
Quoted 18 lines · Michal Luczaj
Quoted 8 lines · Stefano Garzarella
Quoted 7 lines · Stefano Garzarella
Quoted 4 lines · Michal Luczaj

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.

Quoted 7 lines
Quoted 7 lines

This is a new behaviour that only new kernels will follow, so I think it is better to add a new test instead of extending a pre-existing test that we described as "SOCK_STREAM SO_LINGER null-ptr-deref".

The old test should continue to check the null-ptr-deref also for old kernels, while the new test will check the new behaviour, so we can skip the new test while testing an old kernel.

Quoted 7 lines

Right, I'll split it.

Quoted 2 lines

I also saw that we don't have any test to verify that actually the lingering is working, should we add it since we are touching it?

Yeah, I agree we should. Do you have any suggestion how this could be done reliably?

Can we play with SO_VM_SOCKETS_BUFFER_SIZE like in credit-update tests?

One peer can set it (e.g. to 1k), accept the connection, but without read anything. The other peer can set the linger timeout, send more bytes than the buffer size set by the receiver.
At this point the extra bytes should stay on the sender socket buffer, so we can do the close() and it should time out, and we can check if it happens.

WDYT?

Quoted 8 lines
Quoted 1 line

Haven't we discussed this approach in [1]? I've reported that I can't make

Sorry, I forgot. What was the conclusion? Why this can't work?

Quoted 2 lines

it work. But maybe I'm misunderstanding something, please see the code below.

What I should check in the code below?

Okay, I see the send() is blocking (please next time explain better the issue, etc.)

I don't want to block this series, so feel free to investigate that later if we have a way to test it. If I'll find some time, I'll try to check if we have a way.

Thanks,
Stefano

#14 lore raw

Re: [PATCH net-next v4 3/3] vsock/test: Expand linger test to ensure close() does not misbehave

Quoted 50 lines · Stefano Garzarella
Quoted 48 lines · Stefano Garzarella
Quoted 37 lines · Michal Luczaj
Quoted 36 lines · Stefano Garzarella
Quoted 18 lines · Michal Luczaj
Quoted 8 lines · Stefano Garzarella
Quoted 7 lines · Stefano Garzarella
Quoted 4 lines · Michal Luczaj

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.

Quoted 7 lines
Quoted 7 lines

This is a new behaviour that only new kernels will follow, so I think it is better to add a new test instead of extending a pre-existing test that we described as "SOCK_STREAM SO_LINGER null-ptr-deref".

The old test should continue to check the null-ptr-deref also for old kernels, while the new test will check the new behaviour, so we can skip the new test while testing an old kernel.

Quoted 7 lines

Right, I'll split it.

Quoted 2 lines

I also saw that we don't have any test to verify that actually the lingering is working, should we add it since we are touching it?

Yeah, I agree we should. Do you have any suggestion how this could be done reliably?

Can we play with SO_VM_SOCKETS_BUFFER_SIZE like in credit-update tests?

One peer can set it (e.g. to 1k), accept the connection, but without read anything. The other peer can set the linger timeout, send more bytes than the buffer size set by the receiver.
At this point the extra bytes should stay on the sender socket buffer, so we can do the close() and it should time out, and we can check if it happens.

Quoted 3 lines
Quoted 1 line

WDYT?

Haven't we discussed this approach in [1]? I've reported that I can't make

Sorry, I forgot. What was the conclusion? Why this can't work?

Quoted 2 lines

it work. But maybe I'm misunderstanding something, please see the code below.

Quoted 8 lines
Quoted 1 line

What I should check in the code below?

Okay, I see the send() is blocking (please next time explain better the issue, etc.)

I don't want to block this series, so feel free to investigate that later if we have a way to test it. If I'll find some time, I'll try to check if we have a way.

I've tried to take a look, and no, there's no easy way except to somehow slow down the receiver, but I don't think we have a reliable way to do that, so I can't think of anything for now. Let's skip it (I'll try to remember ;-)

Thanks,
Stefano