From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=67.231.153.30 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796054984; b=RpNIpDn3RSHvagDgR2U9yPisipwkjKu4HKlfwemf/w/+8L88QpBDdLrApFSCNHlmznjm 4Jox0FQELcG5hpi5qJLtV1oD1EgjDhPhR1XcYONXpL45yuulKskQaYqG1j1K0CKoohV4d Hr/r8XYTElkmcevo4U47Dl/mI1M1vaEpDs3PkzHDwEb8wma2OJ2D24x6Fik4MXrRDtCeV nmyREjBbu7GNN8LZpprW2nPnmio42svg45k9tddHWGomx6baVfq948fgKmQHufsxWap6p 2FRKMapkFNYv/MYhA+BKei1rZsG9NpKK+2jtwGEsJLW7BU4HnSKbDoG8qBKYz7IyjBg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796054984; h=DMARC-Filter:Received:DKIM-Signature:Received:Received:Date:From:To: CC:Subject:Message-ID:References:MIME-Version:Content-Type: Content-Disposition:In-Reply-To:X-Originating-IP:X-Proofpoint-GUID: X-Proofpoint-Spam-Details-Enc:X-Authority-Analysis: X-Proofpoint-ORIG-GUID:X-Proofpoint-Spam-Info: X-Proofpoint-Virus-Version; bh=UmFbkQVAZY5MX1puxVlZGyhJXmSVcgYM3/QPNFqehpM=; b=k302SF/pphSJyDW+A7S3VIG8x6lUNv7hTdLmtTV8zAD5lFlsnPnZx95LuRTjoNhHrzJ8 74Gd1q4Pj4dsnhs5HUXYvwKZ7FUPObMMJCibG/obU+QlQEhCq1pBebWa3lBQYmDjIDUyL f9ihj69Pq4gJnKkq/rK0Y9uixdhMeA9QySU5mCe2jdH0uik5bQuU86ffwD4269PH3/XbC iTwJ3RwcIv6z16Fpof6VEnD2fdnylByVB4+RZQRrfkTnFv80SriufWGefgZEIO7f4otlS bPk1RZ1uUMzwJgSmvK4jc6zPC6BdohwJ/lIuSyOdEhxgFCYT0cs88H5m0EVvgpam0aQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=meta.com; spf=pass smtp.mailfrom=meta.com; dkim=pass header.d=meta.com header.i=@meta.com header.a=rsa-sha256 header.s=pps82601-s2048-2026-q3 header.b=hrqGw4FQ; arc=none smtp.remote-ip=67.231.153.30 Received: from mx0b-00082601.pphosted.com (mx0b-00082601.pphosted.com [67.231.153.30]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0458F606A325; Mon, 30 Nov 2026 16:09:43 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 0458F606A325 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=meta.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=meta.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=meta.com header.i=@meta.com header.a=rsa-sha256 header.s=pps82601-s2048-2026-q3 header.b=hrqGw4FQ Received: from pps.filterd (m0528006.ppops.net [127.0.0.1]) by mx0a-00082601.pphosted.com (8.18.1.11/8.18.1.11) with ESMTP id 6AUG1W264005080; Mon, 30 Nov 2026 08:09:16 -0800 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=meta.com; h=cc :content-type:date:from:in-reply-to:message-id:mime-version :references:subject:to; s=pps82601-s2048-2026-q3; bh=UmFbkQVAZY5 MX1puxVlZGyhJXmSVcgYM3/QPNFqehpM=; b=hrqGw4FQoR0YRhsAqbygxy479z2 noJQDhD6EQkXlS4VnDopNHlPIh3LOVFa+PEpNp8XPBmnr0PdhmvTK8NrGnMlZ/5n QFdo3WvRksTqp2cgrI1YMW/D+LxVyf+zdwUvMtk3DhJs0fCbREgHyKej20YqnYz5 JWav0+tuak9Bw4GgYZsxj96SWrsWzvSqbhgWfySiY/h8/j3miEUY0HmWDej0yIMM /1uh7xRR9fnBHnRyDsJDD1B4ULrRNOdgDI5FKeKKBRc0RaY/Qfk2qxeATU7m3wz2 wHpOE2ChJU93urIHFatQ3VxJUVgCon4GEDtom7+igphBfUOv7o/nR1zrLZA== Received: from mail.thefacebook.com ([163.114.134.16]) by mx0a-00082601.pphosted.com (PPS) with ESMTPS id 4j8gfnxsxh-2 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NOT); Mon, 30 Nov 2026 08:09:15 -0800 (PST) Received: from localhost (2620:10d:c085:108::150d) by mail.thefacebook.com (2620:10d:c08b:78::2ac9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.2.2562.56; Mon, 30 Nov 2026 16:09:15 +0000 Date: Mon, 30 Nov 2026 08:09:14 -0800 From: Bobby Eshleman To: Paolo Abeni CC: Michal Luczaj , Stefano Garzarella , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Simon Horman , George Zhang , Andy King , Dmitry Torokhov , , , , Kyle Zeng Subject: Re: [PATCH net v2] vsock: only undo SS_CONNECTING when connect() fails Message-ID: References: <20261117-vsock-connect-close-v2-1-db5388323770@meta.com> <05ca76ec-3a6d-4ab4-ad88-86f95a793369@redhat.com> X-Mailing-List: netdev@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Disposition: inline In-Reply-To: <05ca76ec-3a6d-4ab4-ad88-86f95a793369@redhat.com> X-Proofpoint-GUID: jIqxN9EbbUkAdQIahxrJFfiJM-z3OW5F X-Proofpoint-Spam-Details-Enc: AW1haW4tMjYxMTMwMDA2NCBTYWx0ZWRfXwsmy3z4la4fy Px9emNYxEr6kkr+JG8LJCtlTaQNah6F6ojBYA8NmmgsOrVazeSMAAN26lVZlARtz3Xag95RxclC UdauHTHK10569kRyTSzJt67ZVCoXjjJYmPqLABQLo/uHJnLjawYhPjlNu1ZhzO8QsBl3X6mUlnG 43cLlf4PM+4Vm6wOyvbJy8PXhTQaWvHQWtDvwOAJ4qgol1LFsOfDoGmmSuLCE03PUDZd1VRh8j1 2rJriB5FthDmi8Valp+asxjDYeUZB6IDSnHXz79nzXhPmUtix4Lk5XcSjhyPgAucB/IwTG8thOy FTDMVif5p+AM5MdTqhDBgBQYuUxYDOQfMeL6J+GRWk7Vvre0VIk/Z7dvvvKIp5Yty0xRARTCJlU MocQQoWltqceKl7gJBM5r1DUwFr0K1kijGka7MtDvh0h3ecM63SyAl43Gks0mQDAj+lmNXE4/Jj vNtdJ5ONHzUfEnuJU/w== X-Authority-Analysis: v=2.4 cv=MsPHeGae c=1 sm=1 tr=0 ts=6b0d9fab cx=c_pps a=CB4LiSf2rd0gKozIdrpkBw==:117 a=CB4LiSf2rd0gKozIdrpkBw==:17 a=kj9zAlcOel0A:10 a=zJRp6_cAMcgA:10 a=VkNPw1HP01LnGYTKEx00:22 a=7x6HtfJdh03M6CCDgxCd:22 a=kkcUborcUVj0H7zxAXTl:22 a=vr3Pj5-kkdiXXtAFwsYA:9 a=CjuIK1q_8ugA:10 X-Proofpoint-ORIG-GUID: jIqxN9EbbUkAdQIahxrJFfiJM-z3OW5F X-Proofpoint-Spam-Info: AW1haW4tMjYxMTMwMDA2NCBTYWx0ZWRfX6jisqxNVdd5N JymFrfw3OMIji0BYWBTsDuQNdShGz2tcXMlFAkomUfbU4YZaVDQeUY6T3dwZ+bvyjT73wFWSPfv C6hdQOHuW3eg0f4co1yTO5mvLYucG9Q= X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1176,Hydra:6.1.138,FMLib:17.12.100.49 definitions=2026-11-30_04,2026-11-30_01,2025-10-01_01 On Wed, Nov 25, 2026 at 10:54:10AM +0100, Paolo Abeni wrote: > On 11/24/26 21:30, Bobby Eshleman wrote: > > 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. > Ok, it looks worth addressing. But possibly for net-next? since 'broken from > the beginning', not critical and possibly some functional change implied. > > /P > Yep, totally makes sense. Thanks, Bobby