Kernel KVM virtualization development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Bobby Eshleman" <bobbyeshleman@meta.com>
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: Thu, 19 Nov 2026 01:55:35 +0000	[thread overview]
Message-ID: <sashiko-outbox-222310@kernel.org> (raw)
In-Reply-To: <20261117-vsock-guest-ns-v7-4-7e522ea9dd9b@meta.com>

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
--

commit d661163d1e6afc774ff2a10040756b2e6a13f480
Author: Bobby Eshleman <bobbyeshleman@meta.com>

vsock: add a netlink command to assign the g2h device to a netns

This commit adds a vsock generic netlink family with VSOCK_CMD_DEV_NETNS_SET
and VSOCK_CMD_DEV_NETNS_GET commands to move the guest-to-host vsock device
between network namespaces and close sockets that lose access to it.

Link: https://lore.kernel.org/all/20200427142518.uwssa6dtasrp3bfc@steredhat/

> diff --git a/net/vmw_vsock/af_vsock.c b/net/vmw_vsock/af_vsock.c
> index 7d58e7396b75f..dcd39ad43eec3 100644
> --- a/net/vmw_vsock/af_vsock.c
> +++ b/net/vmw_vsock/af_vsock.c
[ ... ]
> @@ -502,6 +544,37 @@ void vsock_for_each_connected_socket(const struct vsock_transport *transport,
>  }
>  EXPORT_SYMBOL_GPL(vsock_for_each_connected_socket);
>  
> +/* Return the next connected socket of @transport that @match accepts, with a
> + * reference held. The scan starts at bucket *@bucket and leaves it where it
> + * stopped, so calling again resumes there. Unlike
> + * vsock_for_each_connected_socket(), the caller acts on the socket after
> + * vsock_table_lock is dropped, so it can take lock_sock().
> + */
> +static struct sock *
> +vsock_next_connected_socket(const struct vsock_transport *transport,
> +			    unsigned int *bucket,
> +			    bool (*match)(struct vsock_sock *vsk))
> +{
> +	struct vsock_sock *vsk;
> +
> +	spin_lock_bh(&vsock_table_lock);
> +
> +	for (; *bucket < ARRAY_SIZE(vsock_connected_table); (*bucket)++) {
> +		list_for_each_entry(vsk, &vsock_connected_table[*bucket],
> +				    connected_table) {
> +			if (vsk->transport == transport && match(vsk)) {
> +				sock_hold(sk_vsock(vsk));
> +				spin_unlock_bh(&vsock_table_lock);
> +				return sk_vsock(vsk);
> +			}
> +		}
> +	}
> +
> +	spin_unlock_bh(&vsock_table_lock);
> +
> +	return NULL;
> +}
[ ... ]
> +static bool vsock_g2h_should_reset(struct vsock_sock *vsk)
> +{
> +	return READ_ONCE(sk_vsock(vsk)->sk_state) == TCP_ESTABLISHED &&
> +	       !sock_flag(sk_vsock(vsk), SOCK_DONE) &&
> +	       !READ_ONCE(vsk->close_work_scheduled) &&
> +	       !vsock_g2h_reachable_sk(vsk);
> +}

[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.

> +
> +/* 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:

net/vmw_vsock/af_vsock.c:__vsock_seqpacket_recvmsg() {
	...
	msg_len = transport->seqpacket_dequeue(vsk, msg, flags);

	if (msg_len < 0) {
		err = msg_len;
		goto out;
	}

	if (sk->sk_err) {
		err = -sk->sk_err;
	} else if (sk->sk_shutdown & RCV_SHUTDOWN) {
		err = 0;
	} else {
		/* User sets MSG_TRUNC, so return real length of
		 * packet.
		 */
		if (flags & MSG_TRUNC)
			err = msg_len;
		else
			err = msg_len - (msg_len - len);
	}
}

Conversely, when a socket has no queued data at sweep time, setting both
sk->sk_state = TCP_CLOSING and SOCK_DONE causes __vsock_connectible_recvmsg()
to return 0 (EOF) before ever inspecting sk->sk_err:

net/vmw_vsock/af_vsock.c:__vsock_connectible_recvmsg() {
	...
	if (!transport || sk->sk_state != TCP_ESTABLISHED) {
		if (sock_flag(sk, SOCK_DONE))
			err = 0;
		else
			err = -ENOTCONN;

		goto out;
	}
}

Meanwhile, a SOCK_STREAM socket that had queued data at sweep time never
transitions to TCP_CLOSING after draining, so subsequent recvmsg() calls keep
returning -ECONNRESET instead of 0.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261117-vsock-guest-ns-v7-0-7e522ea9dd9b@meta.com?part=4

  reply	other threads:[~2026-11-19  1:55 UTC|newest]

Thread overview: 10+ 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 [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

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=sashiko-outbox-222310@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=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