On Thu, Nov 19, 2026 at 01:55:35AM +0000, [email protected] 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 via email to