Netdev List
 help / color / mirror / Atom feed
From: Michal Luczaj <mhal@rbox.co>
To: Stefano Garzarella <sgarzare@redhat.com>
Cc: "Stefan Hajnoczi" <stefanha@redhat.com>,
	"Michael S. Tsirkin" <mst@redhat.com>,
	"Jason Wang" <jasowangio@gmail.com>,
	"Eugenio Pérez" <eperezma@redhat.com>,
	"David S. Miller" <davem@davemloft.net>,
	"Xuan Zhuo" <xuanzhuo@linux.alibaba.com>,
	"Eric Dumazet" <edumazet@google.com>,
	"Jakub Kicinski" <kuba@kernel.org>,
	"Paolo Abeni" <pabeni@redhat.com>,
	"Simon Horman" <horms@kernel.org>, "Asias He" <asias@redhat.com>,
	kvm@vger.kernel.org, virtualization@lists.linux.dev,
	netdev@vger.kernel.org, linux-kernel@vger.kernel.org,
	"Hyunwoo Kim" <imv4bel@gmail.com>
Subject: Re: [PATCH net v2 4/5] vsock: Do not reset a TCP_CLOSING socket
Date: Tue, 22 Sep 2026 15:17:56 +0200	[thread overview]
Message-ID: <3b3bde52-1f7c-4a7a-b1a9-536e202a281a@rbox.co> (raw)
In-Reply-To: <aqqFkBYebKK1nZNJ@sgarzare-redhat>

On 9/16/26 14:30, Stefano Garzarella wrote:
>> @@ -1834,23 +1833,22 @@ static int vsock_connect(struct socket *sock, struct sockaddr_unsized *addr,
>> 		timeout = schedule_timeout(timeout);
>> 		lock_sock(sk);
>>
>> -		/* Connection established. Whatever happens to socket once we
>> -		 * release it, that's not connect()'s concern. No need to go
>> +		/* Connection was established. Whatever happens to socket once
>> +		 * we release it, that's not connect()'s concern. No need to go
>> 		 * into signal and timeout handling. Call it a day.
>> 		 *
>> 		 * Note that allowing to "reset" an already established socket
>> 		 * here is racy and insecure.
>> 		 */
>> -		if (sk->sk_state == TCP_ESTABLISHED)
>> -			break;
>> +		if (sk->sk_state == TCP_ESTABLISHED ||
>> +		    sk->sk_state == TCP_CLOSING) {
>> +			err = 0;
>> +			goto out_wait;
>> +		}
>>
>> 		/* If connection was _not_ established and a signal/timeout came
>> 		 * to be, we want the socket's state reset. User space may want
>> 		 * to retry.
>> -		 *
>> -		 * sk_state != TCP_ESTABLISHED implies that socket is not on
>> -		 * vsock_connected_table. We keep the binding and the transport
>> -		 * assigned.
> 
> Should we keep "We keep the binding and the transport assigned." or 
> maybe add on the other phrase, "User space may want to retry, so we keep 
> ..."

Right, I'll extend the first paragraph.

>> 		 */
>> 		if (signal_pending(current) || timeout == 0) {
>> 			err = timeout == 0 ? -ETIMEDOUT : sock_intr_errno(timeout);
>> @@ -1874,8 +1872,8 @@ static int vsock_connect(struct socket *sock, struct sockaddr_unsized *addr,
>> 		prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
>> 	}
>>
>> -	err = sock_error(sk);
>> -	if (err) {
> 
> Should we add a comment here explaining why we are doing this?

OK, I'll expand on this. With `if (sk->sk_state == TCP_ESTABLISHED) break`,
there's another bug here.

>> +	if (sk->sk_state != TCP_ESTABLISHED && sk->sk_state != TCP_CLOSING) {
>> +		err = sock_error(sk);
>> 		sk->sk_state = TCP_CLOSE;
>> 		sock->state = SS_UNCONNECTED;
>> 	}

We might exit the loop and get here on:
1. vsock_connect_timeout() timing out; setting sk_err, TCP_CLOSE,
   SS_UNCONNECTED
2. virtio_transport_recv_connecting() failing to establish connection;
   setting sk_err, TCP_CLOSE, and leaving SS_CONNECTING
And to be precise:
3. established connection getting torn by a transport event, locklessly
   setting sk_err, TCP_CLOSE, and leaving SS_CONNECTED
I sure hope I'm not missing something.

So the code above is taking a bit of a defensive approach, but I suppose it
could be simplified to an if()-less

  err = sock_error(sk);
  sock->state = SS_UNCONNECTED;

thanks,
Michal

  reply	other threads:[~2026-09-22 13:18 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-15 13:15 [PATCH net v2 0/5] vsock: Fix connect() races Michal Luczaj
2026-09-15 13:15 ` [PATCH net v2 1/5] vhost/vsock: Fix socket state constant Michal Luczaj
2026-09-16 12:28   ` Stefano Garzarella
2026-09-15 13:15 ` [PATCH net v2 2/5] vsock/virtio: Streamline socket reset on transport/PM event Michal Luczaj
2026-09-16 12:30   ` Stefano Garzarella
2026-09-22 13:17     ` Michal Luczaj
2026-09-24 10:06       ` Stefano Garzarella
2026-09-24 21:26         ` Michal Luczaj
2026-09-16 23:39   ` netdev-bot+sashiko
2026-09-15 13:15 ` [PATCH net v2 3/5] vsock: Enforce no-transport invariant for TCP_LISTEN sockets Michal Luczaj
2026-09-16 12:30   ` Stefano Garzarella
2026-09-22 13:18     ` Michal Luczaj
2026-09-24 10:17       ` Stefano Garzarella
2026-09-24 21:26         ` Michal Luczaj
2026-09-28 10:31           ` Stefano Garzarella
2026-10-05  7:58             ` Michal Luczaj
2026-09-16 23:39   ` netdev-bot+sashiko
2026-09-15 13:15 ` [PATCH net v2 4/5] vsock: Do not reset a TCP_CLOSING socket Michal Luczaj
2026-09-16 12:30   ` Stefano Garzarella
2026-09-22 13:17     ` Michal Luczaj [this message]
2026-09-16 23:39   ` netdev-bot+sashiko
2026-09-15 13:15 ` [PATCH net v2 5/5] vsock: Handle sudden TCP_CLOSE during connect Michal Luczaj
2026-09-16 12:31   ` Stefano Garzarella
2026-09-22 13:18     ` Michal Luczaj
2026-09-28 10:44       ` Stefano Garzarella
2026-09-16 23:39   ` netdev-bot+sashiko

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=3b3bde52-1f7c-4a7a-b1a9-536e202a281a@rbox.co \
    --to=mhal@rbox.co \
    --cc=asias@redhat.com \
    --cc=davem@davemloft.net \
    --cc=edumazet@google.com \
    --cc=eperezma@redhat.com \
    --cc=horms@kernel.org \
    --cc=imv4bel@gmail.com \
    --cc=jasowangio@gmail.com \
    --cc=kuba@kernel.org \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mst@redhat.com \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=sgarzare@redhat.com \
    --cc=stefanha@redhat.com \
    --cc=virtualization@lists.linux.dev \
    --cc=xuanzhuo@linux.alibaba.com \
    /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