mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Michal Luczaj <mhal@rbox.co>
To: Bobby Eshleman <bobbyeshleman@meta.com>,
	Stefano Garzarella <sgarzare@redhat.com>,
	"David S. Miller" <davem@davemloft.net>,
	Eric Dumazet <edumazet@kernel.org>,
	Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
	Simon Horman <horms@kernel.org>,
	George Zhang <georgezhang@vmware.com>,
	Andy King <acking@vmware.com>, Dmitry Torokhov <dtor@vmware.com>
Cc: virtualization@lists.linux.dev, netdev@vger.kernel.org,
	linux-kernel@vger.kernel.org, Kyle Zeng <kylebot@openai.com>
Subject: Re: [PATCH net v2] vsock: only undo SS_CONNECTING when connect() fails
Date: Sat, 21 Nov 2026 19:32:15 +0100	[thread overview]
Message-ID: <ed28d36a-682e-4db5-abf4-1339734e7c57@rbox.co> (raw)
In-Reply-To: <20261117-vsock-connect-close-v2-1-db5388323770@meta.com>

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

      parent reply	other threads:[~2026-11-21 18:32 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-18  1:34 Bobby Eshleman
2026-11-18  1:38 ` netdev-bot+sinfo
2026-11-19  2:27 ` netdev-bot+sashiko
2026-11-21 18:32 ` Michal Luczaj [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ed28d36a-682e-4db5-abf4-1339734e7c57@rbox.co \
    --to=mhal@rbox.co \
    --cc=acking@vmware.com \
    --cc=bobbyeshleman@meta.com \
    --cc=davem@davemloft.net \
    --cc=dtor@vmware.com \
    --cc=edumazet@kernel.org \
    --cc=georgezhang@vmware.com \
    --cc=horms@kernel.org \
    --cc=kuba@kernel.org \
    --cc=kylebot@openai.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=sgarzare@redhat.com \
    --cc=virtualization@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®