🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: linux.kernel.netdev
4 messages
4 total messages Started by Michal Luczaj Sat, 21 Nov 2026 18:32
Re: [PATCH net v2] vsock: only undo SS_CONNECTING when connect() fails
#20185
Author: Michal Luczaj
Date: Sat, 21 Nov 2026 18:32
78 lines
11550 bytes
On 11/18/26 02:34, Bobby Eshleman wrote:
> vsock_connect() sleeps with the socket unlocked. Meanwhile the transport
> can complete the handshake, setting sock->state to SS_CONNECTED, and then
> reset the connection, e.g. in vhost_vsock_reset_orphans(),
> virtio_vsock_reset_sock() or vmci_transport_handle_detach(). When
> connect() gets the lock back, it finds sk_err set and moves the socket
> back to SS_UNCONNECTED. Userspace can then connect() the socket again,
> and a connect() to a CID on another transport makes
> vsock_assign_transport() release the transport the socket was connected
> on.
> 
> Only move the socket back to SS_UNCONNECTED if it is still
> SS_CONNECTING. A socket that got connected stays SS_CONNECTED, and
> another connect() fails with -EISCONN.
> 
> Likewise, only move sk_state to TCP_CLOSE if it is still TCP_SYN_SENT.
> Otherwise a second thread blocked in connect() on the same socket can
> time out after the first one failed and called listen(), and overwrite
> TCP_LISTEN.

Ohh, this took me a while.

> +/* Avoid breaking the socket when another thread has changed its state while
> + * we were asleep.
> + */
> +static void vsock_connect_close(struct socket *sock)
> +{
> +	struct sock *sk = sock->sk;
> +
> +	if (sk->sk_state == TCP_SYN_SENT)
> +		sk->sk_state = TCP_CLOSE;
> +	if (sock->state == SS_CONNECTING)
> +		sock->state = SS_UNCONNECTED;
> +}

I was wondering why not

	if (sk->sk_state == TCP_SYN_SENT) {
		sk->sk_state = TCP_CLOSE;
		sock->state = SS_UNCONNECTED;
	}

and I think the only reasons we can't say "SYN_SENT iff CONNECTING" is
because shutdown() can flip SYN_SENT/CONNECTING to SYN_SENT/DISCONNECTING.
On connect's failure, the blocking path kinda-ignores such shutdown() and
resets the socket to UNCONNECTED anyway, while the non-blocking (i.e.
vsock_connect_timeout()) also consults sk_shutdown != SHUTDOWN_MASK.

So how about being consistent:

static void vsock_connect_close(struct socket *sock)
{
	struct sock *sk = sock->sk;

	// we follow vsock_connect_timeout():
	// TCP_SYN_SENT implies SS_{DIS,}CONNECTING;
	// and we do check sk_shutdown
	//
	// or am I missing something?

	if (sk->sk_state == TCP_SYN_SENT &&
	    sk->sk_shutdown != SHUTDOWN_MASK) {
		sk->sk_state = TCP_CLOSE;
		sock->state = SS_UNCONNECTED;
	}
}

What do you think?

I guess this approach also addresses "[Severity: Low] What happens if
another thread calls shutdown() while a blocking connect() is asleep?". As
for "[Severity: Medium] Can a connect() waiter that times out late still
abort a newer connect attempt on the same socket?", isn't this the current
situation?

thanks,
Michal
Re: [PATCH net v2] vsock: only undo SS_CONNECTING when connect() fails
#21937
Author: Michal Luczaj
Date: Tue, 24 Nov 2026 16:17
150 lines
15226 bytes
On 11/24/26 00:19, Bobby Eshleman wrote:
> On Sat, Nov 21, 2026 at 07:32:15PM +0100, Michal Luczaj wrote:
>> On 11/18/26 02:34, Bobby Eshleman wrote:
>>> vsock_connect() sleeps with the socket unlocked. Meanwhile the transport
>>> can complete the handshake, setting sock->state to SS_CONNECTED, and then
>>> reset the connection, e.g. in vhost_vsock_reset_orphans(),
>>> virtio_vsock_reset_sock() or vmci_transport_handle_detach(). When
>>> connect() gets the lock back, it finds sk_err set and moves the socket
>>> back to SS_UNCONNECTED. Userspace can then connect() the socket again,
>>> and a connect() to a CID on another transport makes
>>> vsock_assign_transport() release the transport the socket was connected
>>> on.
>>>
>>> Only move the socket back to SS_UNCONNECTED if it is still
>>> SS_CONNECTING. A socket that got connected stays SS_CONNECTED, and
>>> another connect() fails with -EISCONN.
>>>
>>> Likewise, only move sk_state to TCP_CLOSE if it is still TCP_SYN_SENT.
>>> Otherwise a second thread blocked in connect() on the same socket can
>>> time out after the first one failed and called listen(), and overwrite
>>> TCP_LISTEN.
>>
>> Ohh, this took me a while.
>>
>>> +/* Avoid breaking the socket when another thread has changed its state while
>>> + * we were asleep.
>>> + */
>>> +static void vsock_connect_close(struct socket *sock)
>>> +{
>>> +	struct sock *sk = sock->sk;
>>> +
>>> +	if (sk->sk_state == TCP_SYN_SENT)
>>> +		sk->sk_state = TCP_CLOSE;
>>> +	if (sock->state == SS_CONNECTING)
>>> +		sock->state = SS_UNCONNECTED;
>>> +}
>>
>> I was wondering why not
>>
>> 	if (sk->sk_state == TCP_SYN_SENT) {
>> 		sk->sk_state = TCP_CLOSE;
>> 		sock->state = SS_UNCONNECTED;
>> 	}
>>
>> and I think the only reasons we can't say "SYN_SENT iff CONNECTING" is
>> because shutdown() can flip SYN_SENT/CONNECTING to SYN_SENT/DISCONNECTING.
>> On connect's failure, the blocking path kinda-ignores such shutdown() and
>> resets the socket to UNCONNECTED anyway, while the non-blocking (i.e.
>> vsock_connect_timeout()) also consults sk_shutdown != SHUTDOWN_MASK.
>>
>> So how about being consistent:
>>
>> static void vsock_connect_close(struct socket *sock)
>> {
>> 	struct sock *sk = sock->sk;
>>
>> 	// we follow vsock_connect_timeout():
>> 	// TCP_SYN_SENT implies SS_{DIS,}CONNECTING;
>> 	// and we do check sk_shutdown
>> 	//
>> 	// or am I missing something?
>>
>> 	if (sk->sk_state == TCP_SYN_SENT &&
>> 	    sk->sk_shutdown != SHUTDOWN_MASK) {
>> 		sk->sk_state = TCP_CLOSE;
>> 		sock->state = SS_UNCONNECTED;
>> 	}
>> }
>>
>> What do you think?
> 
> I think we could say TCP_SYN_SENT implies SS_{DIS,}CONNECTING, but
> probably not the reverse... without a peer on the other end our connect
> request gets an RST reply, switching to TCP_CLOSE but leaving untouched
> SS_CONNECTING. Using only TCP_SYN_SENT then skips resetting
> SS_CONNECTING to SS_UNCONNECTED, and future connect() retries hit
> -EALREADY forever.

Ugh, true. And your double-if approach has it covered.

> It seems to work for vsock_connect_timeout() because that function only
> needs to test if the attempt to connect is still pending (if yes, then
> close everything up), but vsock_connect_close() needs to know more
> precisely if the socket still holds the state that this current connect
> attempt changed... so it separately checks "Did we set TCP_SYN_SENT? if
> so, set to TCP_CLOSE.  Did we set SS_CONNECTING?  If so, set to
> SS_UNCONNECTED."

Right, got it.

> I think it might even make more sense to consolidate in the other
> direction and make vsock_connect_timeout() a caller of the new
> vsock_connect_close(), since it appears there is a similar edge case
> there...
> 
> connect(), O_NONBLOCK
>   transport->connect()  /* OP_REQUEST */
>   mod_delayed_work(connect_work)
> 
> shutdown(SHUT_RDWR)
>   sock->state = SS_DISCONNECTING
>   sk_shutdown = SHUTDOWN_MASK
> 
> vsock_connect_timeout()
>   skips the socket because of SHUTDOWN_MASK
>   /* still TCP_SYN_SENT / SS_DISCONNECTING */
> 
> late OP_RESPONSE, virtio_transport_recv_connecting()
>   sk_state = TCP_ESTABLISHED
>   sock->state = SS_CONNECTED
>   vsock_insert_connected()
> 
> So the attempt never times out, and a late OP_RESPONSE connects a
> socket the user already shut down, overwriting SS_DISCONNECTING with
> SS_CONNECTED. I think that's a separate bug for a separate patch,
> though.

Great, I'm all in favour of consolidation. So in the scenario above,
invoking vsock_connect_close() on vsock_connect_timeout() (with sk_shutdown
check dropped) would result in CLOSE/DISCONNECTING, and no more late
virtio_transport_recv_connecting(), right?

>> I guess this approach also addresses "[Severity: Low] What happens if
>> another thread calls shutdown() while a blocking connect() is asleep?". As
>> for "[Severity: Medium] Can a connect() waiter that times out late still
>> abort a newer connect attempt on the same socket?", isn't this the current
>> situation?
>>
> Yeah, Sashiko is worried that a racing shutdown() breaks reusing the
> socket, but AFAIK vsock doesn't support reuse after shutdown anyway.
> Before this patch a second connect() could succeed, but the shutdown
> flags are never cleared, so sendmsg() still returns -EPIPE and recvmsg()
> returns 0. With this patch the socket stays SS_DISCONNECTING, so
> connect() fails with -EINVAL instead.

One more detail: currently a sock that went through a failed connect() can
still become a listener. A transport-bearing listener, which I don't think
is good. So, except for the connect+shutdown case, this does not change
with your patch, right?

> And agreed about the medium issue, the late connect() waiter race is no
> different than before this patch.

Paolo, is this the "multiple threads connecting/disconnecting the same
socket concurrently" you were referring to? Yeah, I think there's an
agreement it's not an issue.

thanks,
Michal
Re: [PATCH net v2] vsock: only undo SS_CONNECTING when connect() fails
#22612
Author: Michal Luczaj
Date: Wed, 25 Nov 2026 15:10
38 lines
10919 bytes
On 11/24/26 21:42, Bobby Eshleman wrote:
> On Tue, Nov 24, 2026 at 05:17:56PM +0100, Michal Luczaj wrote:
>> On 11/24/26 00:19, Bobby Eshleman wrote:
>>> On Sat, Nov 21, 2026 at 07:32:15PM +0100, Michal Luczaj wrote:
> 
> [...]
> 
>>> So the attempt never times out, and a late OP_RESPONSE connects a
>>> socket the user already shut down, overwriting SS_DISCONNECTING with
>>> SS_CONNECTED. I think that's a separate bug for a separate patch,
>>> though.
>>
>> Great, I'm all in favour of consolidation. So in the scenario above,
>> invoking vsock_connect_close() on vsock_connect_timeout() (with sk_shutdown
>> check dropped) would result in CLOSE/DISCONNECTING, and no more late
>> virtio_transport_recv_connecting(), right?
> 
> Right, that late response will fall through and get an RST back.  I'm
> not 100% sure yet about how to rework the sk_shutdown check yet
> though. I mention TCP's approach to shutdowns in my reply to Paolo and
> I'm thinking thats probably the right way to go about it, but not 100%
> sure yet...
> 
> Do we agree that touching vsock_connect_timeout() should not happen in
> this patch? Is there anything pending on this one from your perspective
> Michal?

I think we're good. Patch fixes what started the discussion.

And if I get this right, the only unintended side-effect is that
shutdown/connect inconsistency, but the sibling thread concludes shutdown
handling needs rework anyway.

Reviewed-by: Michal Luczaj <mhal@rbox.co>

thanks,
Michal
Re: [PATCH net v2] vsock: only undo SS_CONNECTING when connect() fails
#22733
Author: Michal Luczaj
Date: Wed, 25 Nov 2026 20:25
27 lines
10606 bytes
On 11/24/26 21:42, Bobby Eshleman wrote:
> On Tue, Nov 24, 2026 at 05:17:56PM +0100, Michal Luczaj wrote:
>> On 11/24/26 00:19, Bobby Eshleman wrote:
>>> On Sat, Nov 21, 2026 at 07:32:15PM +0100, Michal Luczaj wrote:
> 
> [...]
> 
>>> So the attempt never times out, and a late OP_RESPONSE connects a
>>> socket the user already shut down, overwriting SS_DISCONNECTING with
>>> SS_CONNECTED. I think that's a separate bug for a separate patch,
>>> though.
>>
>> Great, I'm all in favour of consolidation. So in the scenario above,
>> invoking vsock_connect_close() on vsock_connect_timeout() (with sk_shutdown
>> check dropped) would result in CLOSE/DISCONNECTING, and no more late
>> virtio_transport_recv_connecting(), right?
> 
> Right, that late response will fall through and get an RST back.  I'm
> not 100% sure yet about how to rework the sk_shutdown check yet
> though. I mention TCP's approach to shutdowns in my reply to Paolo and
> I'm thinking thats probably the right way to go about it, but not 100%
> sure yet...

I just wanted to admit that simply dropping the sk_shutdown check was a bad
idea: vsock_connect_timeout() may outlive vsock_release(), and that check
guards against bad things happening.
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads