The Linux Kernel Mailing List
 help / color / mirror / Atom feed
From: Jiayuan Chen <jiayuan.chen@linux.dev>
To: bpf@vger.kernel.org
Cc: Jiayuan Chen <jiayuan.chen@linux.dev>,
	John Fastabend <john.fastabend@gmail.com>,
	Jakub Sitnicki <jakub@cloudflare.com>,
	"David S. Miller" <davem@davemloft.net>,
	Eric Dumazet <edumazet@kernel.org>,
	Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
	Simon Horman <horms@kernel.org>,
	Kuniyuki Iwashima <kuniyu@google.com>,
	Willem de Bruijn <willemb@google.com>,
	Neal Cardwell <ncardwell@google.com>,
	Alexei Starovoitov <ast@kernel.org>,
	Daniel Borkmann <daniel@iogearbox.net>,
	Andrii Nakryiko <andrii@kernel.org>,
	Eduard Zingerman <eddyz87@gmail.com>,
	Kumar Kartikeya Dwivedi <memxor@gmail.com>,
	Martin KaFai Lau <martin.lau@linux.dev>,
	Song Liu <song@kernel.org>,
	Yonghong Song <yonghong.song@linux.dev>,
	Jiri Olsa <jolsa@kernel.org>,
	Emil Tsalapatis <emil@etsalapatis.com>,
	Ihor Solodrai <ihor.solodrai@linux.dev>,
	Shuah Khan <shuah@kernel.org>, Sechang Lim <rhkrqnwk98@gmail.com>,
	Michal Luczaj <mhal@rbox.co>,
	Mattia Meleleo <mattia.meleleo@coralogix.com>,
	Zijian Zhang <zijianzhang@bytedance.com>,
	linux-kernel@vger.kernel.org, netdev@vger.kernel.org,
	linux-kselftest@vger.kernel.org
Subject: [PATCH bpf v2 04/10] bpf, sockmap: Fix concurrent senders on one psock
Date: Tue,  1 Dec 2026 20:18:52 +0800	[thread overview]
Message-ID: <20261201122055.269577-5-jiayuan.chen@linux.dev> (raw)
In-Reply-To: <20261201122055.269577-1-jiayuan.chen@linux.dev>

tcp_bpf_sendmsg() keeps the state of the current send in the psock:
the verdict in eval, the byte budget in apply_bytes, the redirect
target in sk_redir and the message being built in cork. The socket lock
does not protect that state all the way through. tcp_bpf_push() drops
it in sk_stream_wait_memory(), and the SK_REDIRECT branch of
tcp_bpf_send_verdict() drops it around tcp_bpf_sendmsg_redir(). A
second sendmsg() on the same socket then runs with the state of the
first one.

The cork is the worst case. The SK_PASS branch pushes psock->cork while
it is still attached. When the push sleeps, the second sender picks the
same cork up through psock->cork, appends to it, runs the prog on it,
and on SK_REDIRECT frees it. The first sender wakes up in
tcp_bpf_push() and writes to a freed sk_msg. The other fields are not
freed, but the second sender sends without a verdict, or with the
verdict and the budget of the first one.

Plain TCP has no state of this kind, and two senders on one stream
socket only get their bytes interleaved. Add a mutex to the psock and
hold it for the whole tcp_bpf_sendmsg(). A second sender waits for it.

tls_sw_sendmsg() does the same with tx_lock, and the wait is the same
as there. The mutex is taken with MSG_DONTWAIT as well, and SO_SNDTIMEO
does not apply to it. A trylock that returns -EAGAIN has nothing to wake
the sender up later, since the send buffer is not full.

sk_psock_backlog() is a sender too when an skb is redirected to the
egress of a socket with an sk_msg prog. So a bit in psock->state that
fails the second sender with -EBUSY is smaller, but it breaks the
backlog and a single user sender that runs into it.

Redirects to another socket go through tcp_bpf_push_locked() and
bpf_tcp_ingress() and do not touch the target's send state, so they are
not affected.

tcp_bpf_send_verdict() already has a few checks for a second sender,
added by earlier fixes. They do no harm with the mutex and are left
as they are, comments included, to keep this patch small.

Fixes: 4f738adba30a ("bpf: create tcp_bpf_ulp allowing BPF to monitor socket TX/RX data")
Signed-off-by: Jiayuan Chen <jiayuan.chen@linux.dev>
---
 include/linux/skmsg.h |  1 +
 net/core/skmsg.c      |  2 ++
 net/ipv4/tcp_bpf.c    | 14 +++++++++++++-
 3 files changed, 16 insertions(+), 1 deletion(-)

diff --git a/include/linux/skmsg.h b/include/linux/skmsg.h
index f0e27b4b929f1..a86d58088f2ad 100644
--- a/include/linux/skmsg.h
+++ b/include/linux/skmsg.h
@@ -118,6 +118,7 @@ struct sk_psock {
 				     bool restore);
 	struct proto			*sk_proto;
 	struct mutex			work_mutex;
+	struct mutex			tx_mutex;
 	struct sk_psock_work_state	work_state;
 	struct delayed_work		work;
 	struct sock			*sk_pair;
diff --git a/net/core/skmsg.c b/net/core/skmsg.c
index b81abb3da379a..dcae3cd968edc 100644
--- a/net/core/skmsg.c
+++ b/net/core/skmsg.c
@@ -801,6 +801,7 @@ struct sk_psock *sk_psock_init(struct sock *sk, int node)
 
 	INIT_DELAYED_WORK(&psock->work, sk_psock_backlog);
 	mutex_init(&psock->work_mutex);
+	mutex_init(&psock->tx_mutex);
 	INIT_LIST_HEAD(&psock->ingress_msg);
 	spin_lock_init(&psock->ingress_lock);
 	skb_queue_head_init(&psock->ingress_skb);
@@ -889,6 +890,7 @@ static void sk_psock_destroy(struct work_struct *work)
 	cancel_delayed_work_sync(&psock->work);
 	__sk_psock_zap_ingress(psock);
 	mutex_destroy(&psock->work_mutex);
+	mutex_destroy(&psock->tx_mutex);
 
 	psock_progs_drop(&psock->progs);
 
diff --git a/net/ipv4/tcp_bpf.c b/net/ipv4/tcp_bpf.c
index 7191b95c7b1c7..49b34941bf13c 100644
--- a/net/ipv4/tcp_bpf.c
+++ b/net/ipv4/tcp_bpf.c
@@ -602,8 +602,19 @@ static int tcp_bpf_sendmsg(struct sock *sk, struct msghdr *msg, size_t size)
 	if (unlikely(!psock))
 		return tcp_sendmsg(sk, msg, size);
 
-	lock_sock(sk);
 	timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
+	/*
+	 * The verdict, apply_bytes, sk_redir and the cork live in the psock
+	 * and span the places where the socket lock is dropped below. They
+	 * can only serve one sender at a time.
+	 */
+	err = mutex_lock_interruptible(&psock->tx_mutex);
+	if (err) {
+		sk_psock_put(sk, psock);
+		return sock_intr_errno(timeo);
+	}
+
+	lock_sock(sk);
 	while (msg_data_left(msg)) {
 		bool enospc = false;
 		u32 copy, osize;
@@ -677,6 +688,7 @@ static int tcp_bpf_sendmsg(struct sock *sk, struct msghdr *msg, size_t size)
 	if (err < 0)
 		err = sk_stream_error(sk, msg->msg_flags, err);
 	release_sock(sk);
+	mutex_unlock(&psock->tx_mutex);
 	sk_psock_put(sk, psock);
 	return copied > 0 ? copied : err;
 }
-- 
2.43.0


  parent reply	other threads:[~2026-12-01 12:23 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-01 12:18 [PATCH bpf v2 00/10] fix misc sockmap issues Jiayuan Chen
2026-12-01 12:18 ` [PATCH bpf v2 01/10] bpf, sockmap: Fix copied_seq updates with strparser Jiayuan Chen
2026-12-01 12:18 ` [PATCH bpf v2 02/10] selftests/bpf: Add test for unread SK_PASS data " Jiayuan Chen
2026-12-01 12:18 ` [PATCH bpf v2 03/10] bpf, sockmap: Fix empty sk_msg queued by ingress redirect Jiayuan Chen
2026-12-01 12:18 ` Jiayuan Chen [this message]
2026-12-01 12:18 ` [PATCH bpf v2 05/10] bpf, sockmap: Fix reconnect with a psock by rejecting connect Jiayuan Chen
2026-12-01 12:18 ` [PATCH bpf v2 06/10] bpf, sockmap: Fix psock refcnt taken in sk_psock_backlog() Jiayuan Chen
2026-12-01 12:18 ` [PATCH bpf v2 07/10] bpf, sockmap: Fix socket lock taken on a child before accept() Jiayuan Chen
2026-12-01 12:18 ` [PATCH bpf v2 08/10] bpf, sockmap: Fix apply_bytes when a redirect sends only a part Jiayuan Chen
2026-12-01 12:18 ` [PATCH bpf v2 09/10] selftests/bpf: Fix errno expected by the wait send mem test of test_sockmap Jiayuan Chen
2026-12-01 12:18 ` [PATCH bpf v2 10/10] selftests/bpf: Add test for apply_bytes when a redirect sends a part Jiayuan Chen

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=20261201122055.269577-5-jiayuan.chen@linux.dev \
    --to=jiayuan.chen@linux.dev \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=davem@davemloft.net \
    --cc=eddyz87@gmail.com \
    --cc=edumazet@kernel.org \
    --cc=emil@etsalapatis.com \
    --cc=horms@kernel.org \
    --cc=ihor.solodrai@linux.dev \
    --cc=jakub@cloudflare.com \
    --cc=john.fastabend@gmail.com \
    --cc=jolsa@kernel.org \
    --cc=kuba@kernel.org \
    --cc=kuniyu@google.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=martin.lau@linux.dev \
    --cc=mattia.meleleo@coralogix.com \
    --cc=memxor@gmail.com \
    --cc=mhal@rbox.co \
    --cc=ncardwell@google.com \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=rhkrqnwk98@gmail.com \
    --cc=shuah@kernel.org \
    --cc=song@kernel.org \
    --cc=willemb@google.com \
    --cc=yonghong.song@linux.dev \
    --cc=zijianzhang@bytedance.com \
    /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