Kernel KVM virtualization development
 help / color / mirror / Atom feed
From: Bobby Eshleman <bobbyeshleman@meta.com>
To: <sashiko-reviews@lists.linux.dev>
Cc: <virtualization@lists.linux.dev>, <kvm@vger.kernel.org>,
	"Michael S. Tsirkin" <mst@redhat.com>,
	Eugenio Perez <eperezma@redhat.com>
Subject: Re: [PATCH net-next v7 4/7] vsock: add a netlink command to assign the g2h device to a netns
Date: Fri, 20 Nov 2026 11:00:41 -0800	[thread overview]
Message-ID: <awCY2UrEthUvfihw@devvm29614.prn0.facebook.com> (raw)
In-Reply-To: <sashiko-outbox-222310@kernel.org>

On Thu, Nov 19, 2026 at 01:55:35AM +0000, sashiko-bot@kernel.org wrote:
> Thank you for your contribution! Sashiko AI review found 2 potential
> issues to consider:
> - [Medium] vsock: swept sockets lose seqpacket data and return EOF
>   instead of ECONNRESET
> - [Medium] vsock: vsock_shutdown() clears SOCK_DONE on swept sockets in
>   connected table
> --

I'm going to respin this and there will be some changes that I think
will resolve this back-and-forth with sashiko.

Replying below to explain what the next rev will do differently, to give
context for review when that comes out.

pw-bot: cr

[...]

> 
> [Severity: Medium]
> Can calling shutdown() on a swept socket clear SOCK_DONE and cause
> vsock_g2h_should_reset() to match it again?
> 
> When vsock_g2h_reset_unreachable() sweeps a socket that has unread data
> queued, it leaves the socket in vsock_connected_table with sk->sk_state set
> to TCP_ESTABLISHED and relies on SOCK_DONE to prevent
> vsock_g2h_should_reset() from matching it again.  However, vsock_shutdown()
> unconditionally clears SOCK_DONE whenever sock->state is not SS_UNCONNECTED:
> 
> net/vmw_vsock/af_vsock.c:vsock_shutdown() {
> 	...
> 	if (sock->state == SS_UNCONNECTED) {
> 		err = -ENOTCONN;
> 		if (sock_type_connectible(sk->sk_type))
> 			goto out;
> 	} else {
> 		sock->state = SS_DISCONNECTING;
> 		err = 0;
> 	}
> 
> 	/* Receive and send shutdowns are treated alike. */
> 	mode = mode & (RCV_SHUTDOWN | SEND_SHUTDOWN);
> 	if (mode) {
> 		...
> 		if (sock_type_connectible(sk->sk_type)) {
> 			sock_reset_flag(sk, SOCK_DONE);
> 			vsock_send_shutdown(sk, mode);
> 		}
> 	}
> }
> 
> Because vsock_next_connected_socket() restarts from the head of the current
> bucket on each iteration, a process calling shutdown() on its swept socket
> makes vsock_g2h_should_reset() true again and re-matches the same socket in a
> loop under vsock_g2h_assign_mutex.  On a swept socket with sk->sk_state set
> to TCP_CLOSING, clearing SOCK_DONE also causes subsequent recvmsg() calls to
> return -ENOTCONN, and on a TCP_ESTABLISHED swept socket it bypasses the
> sock_flag(sk, SOCK_DONE) check in virtio_transport_recv_pkt() if the device
> later moves back to a reachable namespace.

In this series I've been trying to make the socket losing access to the
device moving to another netns equal in behavior to the socket receiving
a peer RST, which keeps sockets in TCP_ESTABLISHED and uses SOCK_DONE to
distinguish between "peer alive" and "peer reset" instead of ->sk_state.

The last several versions have been trying to get this mental model
working race and edge-case free... but the last several revisions have
just been a game of a whack-a-mole.

Instead, I think a better and simpler model is to treat the move as if
the device had been removed, the way virtio_vsock_reset_sock() already
handles a device unplug, i.e., just set TCP_CLOSE and ECONNRESET. This
problem then goes away because we aren't messing with SOCK_DONE and
shutdown won't cause this re-matching in the iterator.

> 
> > +
> > +/* Close every connected socket of @t that can no longer reach the g2h device.
> > + */
> > +static void vsock_g2h_reset_unreachable(const struct vsock_transport *t)
> > +{
> > +	struct vsock_sock *vsk;
> > +	unsigned int i = 0;
> > +	struct sock *sk;
> > +	bool reset;
> > +
> > +	lockdep_assert_held(&vsock_g2h_assign_mutex);
> > +	lockdep_assert_not_held(&vsock_register_mutex);
> > +
> > +	/* lock_sock() cannot be taken under vsock_table_lock, so pick the
> > +	 * sockets one at a time, then recheck each under both locks.
> > +	 */
> > +	while ((sk = vsock_next_connected_socket(t, &i,
> > +						 vsock_g2h_should_reset))) {
> > +		vsk = vsock_sk(sk);
> > +
> > +		lock_sock(sk);
> > +
> > +		reset = false;
> > +		spin_lock_bh(&vsock_table_lock);
> > +		/* It may have been closed since we dropped the table lock. */
> > +		if (__vsock_in_connected_table(vsk) && vsk->transport == t &&
> > +		    vsock_g2h_should_reset(vsk))
> > +			reset = true;
> > +		spin_unlock_bh(&vsock_table_lock);
> > +
> > +		if (reset) {
> > +			sock_set_flag(sk, SOCK_DONE);
> > +			WRITE_ONCE(vsk->peer_shutdown, SHUTDOWN_MASK);
> > +			/* Same as a peer reset, stay TCP_ESTABLISHED while data
> > +			 * is queued so that recv() can still read it.
> > +			 */
> > +			if (vsock_connectible_has_data(vsk) <= 0)
> > +				sk->sk_state = TCP_CLOSING;
> > +			sk->sk_err = ECONNRESET;
> > +			sk_error_report(sk);
> > +			if (t->cancel_pkt)
> > +				t->cancel_pkt(vsk);
> > +		}
> 
> [Severity: Medium]
> Does setting sk->sk_err = ECONNRESET and sk->sk_state = TCP_CLOSING here
> cause SOCK_SEQPACKET sockets to drop queued messages and empty sockets to
> return 0 instead of -ECONNRESET?
> 
> When a SOCK_SEQPACKET socket has unread messages queued at sweep time,
> sk->sk_state remains TCP_ESTABLISHED and __vsock_seqpacket_recvmsg() dequeues
> the message, but then checks sk->sk_err and overwrites the return value with
> -ECONNRESET, discarding each queued message:

Yes, this will go away when loss of netns access behaves the same as
device removal... sk_state == TCP_CLOSE with !sock_flag(sk, SOCK_DONE)
will cause recvmsg to return -ENOTCONN. If a reader is already blocked
in recvmsg() then seqpacket may return 0 / EOF. I think this will be the
right move and overall much simpler.

I'm expecting Sashiko to complain again about some of the quirks that
were there when trying this approach before: -ENOTCONN instead of
-ECONNRESET, the lockless socket-state change, and a few others... but
all of them are tractable/tolerable issues or actually belong in a
different series.

Best,
Bobby

  reply	other threads:[~2026-11-20 19:00 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-18  1:54 [PATCH net-next v7 0/7] vsock: assign the guest vsock device to a network namespace Bobby Eshleman
2026-11-18  1:54 ` [PATCH net-next v7 1/7] vsock: constify the transport in vsock_for_each_connected_socket() Bobby Eshleman
2026-11-18  1:54 ` [PATCH net-next v7 2/7] vsock: rename the vsock pernet operations Bobby Eshleman
2026-11-18  1:54 ` [PATCH net-next v7 3/7] vsock: pass the netns to ->has_remote_cid() Bobby Eshleman
2026-11-18  1:54 ` [PATCH net-next v7 4/7] vsock: add a netlink command to assign the g2h device to a netns Bobby Eshleman
2026-11-19  1:55   ` sashiko-bot
2026-11-20 19:00     ` Bobby Eshleman [this message]
2026-11-18  1:54 ` [PATCH net-next v7 5/7] vsock/virtio: support guest device network namespace Bobby Eshleman
2026-11-19  1:55   ` sashiko-bot
2026-11-18  1:54 ` [PATCH net-next v7 6/7] selftests/vsock: test the guest vsock " Bobby Eshleman
2026-11-18  1:54 ` [PATCH net-next v7 7/7] selftests/vsock: test the netns assign privilege checks Bobby Eshleman
2026-11-19  2:28 ` [PATCH net-next v7 0/7] vsock: assign the guest vsock device to a network namespace Jakub Kicinski
2026-11-19 17:42   ` Bobby Eshleman
2026-11-19 21:53     ` Jakub Kicinski
2026-11-20 18:08       ` 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=awCY2UrEthUvfihw@devvm29614.prn0.facebook.com \
    --to=bobbyeshleman@meta.com \
    --cc=eperezma@redhat.com \
    --cc=kvm@vger.kernel.org \
    --cc=mst@redhat.com \
    --cc=sashiko-reviews@lists.linux.dev \
    --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