Netdev List
 help / color / mirror / Atom feed
From: Bobby Eshleman <bobbyeshleman@meta.com>
To: Paolo Abeni <pabeni@redhat.com>
Cc: Michal Luczaj <mhal@rbox.co>,
	Stefano Garzarella <sgarzare@redhat.com>,
	"David S. Miller" <davem@davemloft.net>,
	Eric Dumazet <edumazet@kernel.org>,
	Jakub Kicinski <kuba@kernel.org>, Simon Horman <horms@kernel.org>,
	George Zhang <georgezhang@vmware.com>,
	Andy King <acking@vmware.com>, Dmitry Torokhov <dtor@vmware.com>,
	<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: Tue, 24 Nov 2026 12:30:35 -0800	[thread overview]
Message-ID: <awXz6yo12ZOQPE8E@devvm29614.prn0.facebook.com> (raw)
In-Reply-To: <ebb9c249-85bf-4737-841f-ab6ddcaab5a9@redhat.com>

On Tue, Nov 24, 2026 at 03:39:17PM +0100, Paolo Abeni wrote:
> 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.
> > 
> > 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."
> > 
> > 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.
> > 
> > Or am I off track / missing something?
> I have slightly different question: is the 'multiple threads
> connecting/disconnecting the same socket concurrently' a
> relevant scenario? Otherwise if final state is self-consistent
> (i.e. _NOT_ TCP_SYN_SENT + SS_DISCONNECTING) I think there is
> nothing to fix here.
> 
> /P

To your point, the only user-facing issue I can see with
vsock_connect_timeout() would be seen in pretty rare cases.... Since it
skips the socket when sk_shutdown == SHUTDOWN_MASK, if the peer never
responds with RST the connect attempt never times out, the socket sits
in TCP_SYN_SENT + SS_DISCONNECTING (connect() returning -EINVAL) until
close(). But I'm pretty sure this could only happen on the vhost side if
the guest was unresponsive/crashed or on the virtio side if maybe
vhost's RST dropped during VM migration when vhost's send queue is
purged. IMHO this should probably be fixed, but I don't think its worth
turning the code upside down for.

Looking at this connect()/shutdown() sequence again today... with
O_NONBLOCK, there is some inconsistent behavior that although is not
caused by vsock_connect_timeout(), I'd like to mention here since its
come up. It exists even for a single thread making both calls. Playing
w/ vsock_loopback and bpftrace, it does end up with SS_DISCONNECTING +
TCP_SYN_SENT, but I think the most unexpected thing from the user's
perspective is that it temporarily flips to SS_CONNECTED even after
shutdown() and a number of syscalls present the socket as connected:

  repro    connect()     -> -EINPROGRESS  SS_CONNECTING     TCP_SYN_SENT
  repro    shutdown()    -> 0             SS_DISCONNECTING  TCP_SYN_SENT
  repro    getpeername() -> -ENOTCONN     SS_DISCONNECTING  TCP_SYN_SENT
  repro    connect()     -> -EINVAL       SS_DISCONNECTING  TCP_SYN_SENT
  repro    recv()        -> -ENOTCONN     SS_DISCONNECTING  TCP_SYN_SENT
  repro    SO_ERROR      =  0
  repro    poll()        -> IN|HUP|RDHUP  SS_DISCONNECTING  TCP_SYN_SENT
  kworker  recv_pkt(OP_RESPONSE)          SS_CONNECTED      TCP_ESTABLISHED
  ^ the switch to connected is here

  repro    getpeername() -> 0             SS_CONNECTED      TCP_ESTABLISHED
  repro    connect()     -> -EISCONN      SS_CONNECTED      TCP_ESTABLISHED
  repro    recv()        -> 0             SS_CONNECTED      TCP_ESTABLISHED
  repro    SO_ERROR      =  0
  ^ the socket starts partially acting like it's connected/established

  repro    poll()        -> IN|HUP|RDHUP  SS_CONNECTED      TCP_ESTABLISHED
  kworker  recv_pkt(OP_RST)               SS_CONNECTED      TCP_CLOSING
  ^ eventually RST shows up and shuts things down

TCP handles this better... shutdown() on SYN_SENT socket aborts the
connect attempt w/ tcp_disconnect() resulting in TCP_CLOSE,
SS_UNCONNECTED, sk_err = ECONNRESET, and the late SYN-ACK doesn't push
it into established, and users see POLLERR/SO_ERROR.

I think the TCP approach also fixes that edge case with
vsock_connect_timeout(), since the user would at least see the errors.

Best,
Bobby

  reply	other threads:[~2026-11-24 20:31 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-18  1:34 [PATCH net v2] vsock: only undo SS_CONNECTING when connect() fails 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
2026-11-23 23:19   ` Bobby Eshleman
2026-11-24 14:39     ` Paolo Abeni
2026-11-24 20:30       ` Bobby Eshleman [this message]
2026-11-24 16:17     ` Michal Luczaj
2026-11-24 20:42       ` Bobby Eshleman

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=awXz6yo12ZOQPE8E@devvm29614.prn0.facebook.com \
    --to=bobbyeshleman@meta.com \
    --cc=acking@vmware.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=mhal@rbox.co \
    --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