๐Ÿš€ go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: linux.kernel.netdev
Article #10793

Re: [PATCH bpf v2 08/15] net: Leave sk_stream_wait_memory() once the socket is dead

#10793
From: Eric Dumazet
Date: Wed, 04 Nov 2026 05:23
93 lines
12876 bytes
Le lun. 2 nov. 2026 ร  08:22, Alexei Starovoitov
<alexei.starovoitov@gmail.com> a รฉcrit :
>
> From: Alexei Starovoitov <ast@kernel.org>
>
> When an sk_msg prog redirects data to the egress of another socket, the
> sender calls tcp_sendmsg_locked() on that socket. The sender holds a
> reference to the sock, but not to its file. Thus __tcp_close() can call
> sock_orphan() while the sender sleeps in sk_stream_wait_memory().
> sock_orphan() sets SOCK_DEAD and sets sk_wq to NULL.
>
> After the sleep the loop continues and does not test SOCK_DEAD. The
> subsequent sk_set_bit() can dereference the NULL sk_wq. The tests of sk_err
> and sk_shutdown do not always stop the sender. sock_error() clears sk_err,
> and __tcp_close() can call tcp_disconnect(), which clears sk_shutdown. The
> sender then adds data to the write queue of the dead socket, and
> inet_sock_destruct() warns.
>
> Leave the loop with -EPIPE when the socket is dead after the sleep. The
> function already tests SOCK_DEAD before remove_wait_queue() for the same
> reason. A caller that has the socket open never sees SOCK_DEAD.
>
> Fixes: 4f738adba30a ("bpf: create tcp_bpf_ulp allowing BPF to monitor socket TX/RX data")
> Signed-off-by: Alexei Starovoitov <ast@kernel.org>
> ---
>  net/core/stream.c | 6 +++++-
>  1 file changed, 5 insertions(+), 1 deletion(-)
>
> diff --git a/net/core/stream.c b/net/core/stream.c
> index cb255e2a3a5d..334124d683a7 100644
> --- a/net/core/stream.c
> +++ b/net/core/stream.c
> @@ -150,7 +150,11 @@ int sk_stream_wait_memory(struct sock *sk, long *timeo_p)
>                                     (sk_stream_memory_free(sk) && !vm_wait),
>                                     &wait);
>                 sk->sk_write_pending--;
> -               if (ret < 0)
> +               /*
> +                * A sender which sockmap has redirected to sk does not keep it
> +                * open. A dead socket has no sk_wq.
> +                */
> +               if (ret < 0 || sock_flag(sk, SOCK_DEAD))
>                         goto do_error;

This is fine, but it only helps a sender that wakes up.

As you said in your reply to the bot on patch 07, nothing wakes it.
__tcp_close() sets sk_shutdown and calls sock_orphan() without a wakeup
on sk_sleep(sk), and once sk_wq is NULL, sk_stream_write_space() cannot
reach the sleeper either.

"A signal or SO_SNDTIMEO of the target ends it" does not look enough to
me. tcp_sendmsg_locked() takes the timeout from sock_sndtimeo() of the
target, a socket the sender does not own and cannot configure. With the
default MAX_SCHEDULE_TIMEOUT, a blocking sendmsg() on the sender's own
socket never returns, and the sender's sock_hold() pins the dead target
until then.

Since this patch makes SOCK_DEAD a reason to leave, one wakeup before
sock_orphan(), while the socket is still owned by the closer, should be
enough. The sender can only get the socket lock back after
sock_orphan(), so it sees SOCK_DEAD. Nothing walks the wait queue after
sock_orphan(), so skipping remove_wait_queue() stays safe.

Something like this (untested). sk_wq can be NULL here for MPTCP
subflows, hence skwq_has_sleeper():

diff --git a/net/ipv4/tcp.c b/net/ipv4/tcp.c
--- a/net/ipv4/tcp.c
+++ b/net/ipv4/tcp.c
@@ void __tcp_close(struct sock *sk, long timeout)
 adjudge_to_death:
  state = sk->sk_state;
  sock_hold(sk);
+ /* A sockmap egress redirect can sleep in sk_stream_wait_memory()
+ * without a reference on our file. Let it see SOCK_DEAD.
+ */
+ rcu_read_lock();
+ wq = rcu_dereference(sk->sk_wq);
+ if (skwq_has_sleeper(wq))
+ wake_up_interruptible_all(&wq->wait);
+ rcu_read_unlock();
  sock_orphan(sk);

This can be a follow-up and does not need to block the series.
Technical Details
Message-ID: <CAL4Wiiq+QX8quVBjEZK5qipki6Yt7vx4=tt2L4o+FwO9zD-5=g@mail.gmail.com>
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-356205-linux-kernel-netdev=dewy.ru
References: <20261102072112.4160195-1-alexei.starovoitov@gmail.com> <20261102072112.4160195-9-alexei.starovoitov@gmail.com>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-356205-linux-kernel-netdev=dewy.ru
From: edumazet@kernel.org (Eric Dumazet)
Newsgroups: linux.kernel.netdev
Subject: Re: [PATCH bpf v2 08/15] net: Leave sk_stream_wait_memory() once the
 socket is dead
Date: Wed, 4 Nov 2026 06:23:34 +0100
Organization: Usenet Network
Message-ID: <CAL4Wiiq+QX8quVBjEZK5qipki6Yt7vx4=tt2L4o+FwO9zD-5=g@mail.gmail.com>
References: <20261102072112.4160195-1-alexei.starovoitov@gmail.com> <20261102072112.4160195-9-alexei.starovoitov@gmail.com>
MIME-Version: 1.0
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
Injection-Info: usenet.network;
	logging-data="3331616"; mail-complaints-to="abuse@usenet.network"
Cancel-Lock: sha1:rw0zESmHjyq6luRLUXiHyqox8aY= sha256:HIsENO+alDLyuHDdxVcJ6uo6CM4DsPlSzHcgIKdToM8=
	sha1:NvilbV7nvLCEVAtyMH7pxG3wZtg= sha256:g6mQ8rEs2DqFfMCnQ1qS+FI1WfQyh39qh1GdvD8ByDI=
List-ID: <netdev.vger.kernel.org>
X-Mailpost-Empty-Hdrs: X-Spam-Level
X-Received: from localhost (localhost [127.0.0.1])
	by mail.dewy.ru (Postfix) with ESMTP id 4A803112AEB; Wed,  4 Nov 2026 08:24:10 +0300 (MSK)
X-Virus-Scanned: Debian amavisd-new at usenet.network
X-Spam-Flag: NO
X-Spam-Score: -3.401
X-Spam-Status: No, score=-3.401 tagged_above=-9999 required=6.31
	tests=[DCC_REPUT_70_89=0.1, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1,
	DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1,
	MAILING_LIST_MULTI=-1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001,
	SPF_PASS=-0.001] autolearn=ham autolearn_force=no
X-Authentication-Results: mail.dewy.ru (amavisd-new); dkim=pass (2048-bit key)
	header.d=kernel.org
X-Received: from mail.dewy.ru ([127.0.0.1])
	by localhost (mail.dewy.ru [127.0.0.1]) (amavisd-new, port 10024)
	with ESMTP id GTTtouvWD_Dp;
	Wed,  4 Nov 2026 08:24:07 +0300 (MSK)
X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.105.105.114; helo=tor.lore.kernel.org; envelope-from=netdev+bounces-356205-linux-kernel-netdev=dewy.ru@vger.kernel.org; receiver=<UNKNOWN> 
X-Authentication-Results: OpenDMARC; dmarc=pass (p=quarantine dis=none) header.from=kernel.org
X-Received: from tor.lore.kernel.org (tor.lore.kernel.org [172.105.105.114])
	by mail.dewy.ru (Postfix) with ESMTPS id E90E5112AEA; Wed,  4 Nov 2026 08:23:58 +0300 (MSK)
X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.77.114.92])
	by tor.lore.kernel.org (Postfix) with ESMTP id 1F2F629A6D; Wed,  4 Nov 2026 05:23:50 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1
X-ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass;
	t=1793769827;
	b=vb0w6eLSX3kq0Le10UeFCZkGQ09fusKrbJ9T2ajWA+ifIgXkZiCHtq9DMW4RrmtG0/B1
	 BVKJ1Ii/9LXHTVbShMd8rsz04QtliKINl6MbQ6wjku4SPGU458rXpFxPH1+HoyVxeGao7
	 YTibSdtgjSOyhbuTJv42jR8cMowenbRgJlR+5O817seeame/iqBQd9rdQC1c9NAJ2Icu6
	 i5c3r3c4REUb/pbMykJly5SNlzPYzBasx9A/XPvzS04RksMKA9NmnTQ2jPsP0acX6PwI9
	 TD63shdrr+svat3TxGC9Sa7M8bvA1W1hRI0UMhsMu52oupcI0GAOsLKOcaRnvh5PNuw==
X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1793769827;
	h=X-Original-To:Received:Received:DKIM-Signature:Received:
	 X-Forwarded-Encrypted:X-Gm-Message-State:X-Received:X-Mailing-List:
	 List-Id:List-Post:List-Help:List-Subscribe:List-Unsubscribe:
	 List-Owner:Precedence:MIME-Version:References:In-Reply-To:From:Date:
	 X-Gmail-Original-Message-ID:X-Gm-Features:Message-ID:Subject:To:Cc:
	 Content-Type:Content-Transfer-Encoding;
	bh=Qn8NAtZoDb7HH34IWyK2VCQA4luhbTedxXM4sKwZuRo=;
	b=A+zAooc/WcnfYS1aP5dNILYddyfugqA2P2XL14HxYT/7Kx+X/PM/msyg55KKVDhb1TG5
	 LWXo6wqYhqcQh4XM/mSAgT8o6ld4ifbUH64/oRj+p2XXJRP9CYkSjVqtpt8B5jY0LqTga
	 4Pyg8CyAWoJLcjRuYcEa64P83MfHEiMPiPvWkODvonbxDn2OnnCxUbhQaoMJSp5Bjo7Wm
	 YClv+lpJMQFe8F/MkBZLzOH2gjK3yHVeQ5mkJpceQ8Lc8P7qmNBmV+XqTy61kAnRixgoL
	 2/x5kSzCv0sXDEz+e3g+SA7v9a0DkeZ4rs/dFAAk6StkqP092DHJhGVcr/T3bZ8rOjA==
X-ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; 
	dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=oC27qxmL;
	arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1
X-Received: from localhost.localdomain (localhost [127.0.0.1])
	by smtp.subspace.kernel.org (Postfix) with ESMTP id A8215606A334;
	Wed,  4 Nov 2026 05:23:47 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org;
	dkim=pass (2048-bit key, unprotected) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=oC27qxmL
X-Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18
X-ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none;
	t=1793769826;
	b=JROlPKpGzMLeFFZJM8eX7I5rV8BytzkUNJ0ms6WkXGZQNoAyadfgTn0Myv4Uq4+BflFo
	 UsCUXHURe30OVDEd88+mqLizxG+S7L/anJnZbMNI963Wd2CToFsUJei0sRw0Y/Wx0OtrA
	 dZZS2bzjiu9KjAXh3T9NvbXUZiMfkUpp/Y33byBTXDzIDzcmpWxziSTTAawXqIJvnduCf
	 x10JZKXusL4MhQbmLE9Ym0uYtQJ15aVgvwPPmbkSG89s36NQmlsv65SBXaV428Ac9X9pK
	 115QpY/IrUHivO5rNzp0g1hZhViXfCbYszC8hCWyYSaw61Eo0qnfD6BounsU/hHJhjA==
X-ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1793769826;
	h=Received:DKIM-Signature:Received:X-Forwarded-Encrypted:
	 X-Gm-Message-State:X-Received:MIME-Version:References:In-Reply-To:
	 From:Date:X-Gmail-Original-Message-ID:X-Gm-Features:Message-ID:
	 Subject:To:Cc:Content-Type:Content-Transfer-Encoding;
	bh=Qn8NAtZoDb7HH34IWyK2VCQA4luhbTedxXM4sKwZuRo=;
	b=KmyVvwH7Vogy0deTX0kWr9wqzgLGJ1vp7qB9FWG/OOtkPdH+7EqXNLZ6q4bcxGIFp3x8
	 WvwtZbIKtVMHDdOVN6QNBXwfqf/BxzKAvcphDcWBxSabztUSTDdoXlXFWRm/RaIRa9qwb
	 GlgxBTxtcqSXARbCiiGFLt2+vn1t09+yB48HRhgYsWvrCCELmjZeLAiac0u9VjSnA8A7n
	 v8BEXluBY8+OF8HXuscOE/5beptYXBenXXh7D2BZsvM/Aa9qkPU8Ck0FnA002DtcbirPF
	 VG4WR82CHw6Kr8/zV6uHLd9oFKPJ6+ukVwffRSroKhGu++lj+FrImEvzT9KqorLaK0w==
X-ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; 
	dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=oC27qxmL;
	arc=none smtp.remote-ip=100.103.45.18
X-Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18])
	(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
	 key-exchange X25519 server-signature ECDSA (prime256v1))
	(No client certificate requested)
	by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0C33A606A328
	for <netdev@vger.kernel.org>; Wed,  4 Nov 2026 05:23:45 +0000 (UTC)
X-Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9D67D1F00A16
	for <netdev@vger.kernel.org>; Wed,  4 Nov 2026 05:23:45 +0000 (UTC)
X-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org;
	s=k20260515; t=1793769825;
	bh=Qn8NAtZoDb7HH34IWyK2VCQA4luhbTedxXM4sKwZuRo=;
	h=References:In-Reply-To:From:Date:Subject:To:Cc;
	b=oC27qxmLddM+EFhnLVgpy/VmEZXINf+xmHMuE4FdR6WRBLVsnWvH4KjVNHVFraJuO
	 /8MkqE97l3jpwOqC3pKUzDOHT9ZRJ1DXXVzgBzKn9ZKDZfJsxzGxxh2bMHJWSUoO4P
	 Iw9Rl2YyPuajJcU8OJRvCxTdBU9IleHw9W+6i52qbeVTRP/a4YmrOt01ifcx6sjGHh
	 ZJUmeQWpsSTW5zqXezbe306mtiwtYW/T/dYIHro4eFD8m5+H6WJxonqCYrmAb1qqy0
	 h4YEOzoU8ZjAqpOXrGwZ3Owi4CViRUiQdQ7ALC9gsPHBJny2GXKyd3FzwjFZ+u3zSd
	 pjJPF6FRZ9MwA==
X-Received: by mail-oa1-f54.google.com with SMTP id 586e51a60fabf-49429181ce9so295268fac.0
        for <netdev@vger.kernel.org>; Tue, 03 Nov 2026 21:23:45 -0800 (PST)
X-Forwarded-Encrypted: i=1; AKwUvByNE81onbqSiRLwS5roxAYLYRNbV0eaChJxRFG0PKTAztmb+Fp7ToipEM0KZhRQIGK9SnJdv7c=@vger.kernel.org
X-Gm-Message-State: APuU4Z+9+18H5dPgrn3eosKn8jh6xp9CtupxQxbxsfKnCdW7SWWE5CyA
	1zkmRdqZPmArh/kXzRSd4cUCBuXAMkzNCFjfYXSCwvcwjVc9IUG/4HlU3G6hTPFrGxY0D8L/ixo
	jx9Ko/uPlpzBWK817+stRNrl6E8u2zcE=
X-Received: by 2002:a05:6870:e988:b0:485:d1ab:793c with SMTP id
 586e51a60fabf-4a90769be04mr9501691fac.29.1793769824978; Tue, 03 Nov 2026
 21:23:44 -0800 (PST)
X-Mailing-List: netdev@vger.kernel.org
X-List-Post: <mailto:netdev@vger.kernel.org>
X-List-Help: <mailto:netdev+help@vger.kernel.org>
X-List-Subscribe: <mailto:netdev+subscribe@vger.kernel.org>
X-List-Unsubscribe: <mailto:netdev+unsubscribe@vger.kernel.org>
X-List-Owner: <mailto:netdev+owner@vger.kernel.org>
X-Precedence: list
X-In-Reply-To: <20261102072112.4160195-9-alexei.starovoitov@gmail.com>
X-Gmail-Original-Message-ID: <CAL4Wiiq+QX8quVBjEZK5qipki6Yt7vx4=tt2L4o+FwO9zD-5=g@mail.gmail.com>
X-Gm-Features: AclHuK_M_LLkqH56WlWzBGN8TVZ6lnrAKJmZVwCPt2OMDTAdOmG-v8JtUt35WrY
X-Cc: bpf@vger.kernel.org, netdev@vger.kernel.org, daniel@iogearbox.net, 
	andrii@kernel.org, eddyz87@gmail.com, memxor@gmail.com, 
	john.fastabend@gmail.com, jakub@cloudflare.com, jiayuan.chen@linux.dev, 
	davem@davemloft.net, kuba@kernel.org, pabeni@redhat.com, horms@kernel.org, 
	kuniyu@google.com, willemb@google.com, matttbe@kernel.org, 
	mptcp@lists.linux.dev, mason@kernel.org, josef@toxicpanda.com