Article View: linux.kernel.netdev
Article #25834[PATCH bpf v3 1/9] bpf, sockmap: Fix copied_seq updates with strparser
From: Jiayuan Chen
Date: Wed, 02 Dec 2026 14:30
Date: Wed, 02 Dec 2026 14:30
221 lines
18133 bytes
18133 bytes
With strparser, read_sock advances tp->copied_seq when it pulls skbs
off sk_receive_queue, and recvmsg() advances it again when the user
reads from ingress_msg. For a skb that the verdict passes back to the
same socket, this counted the bytes twice, so commit 36b62df5683c
("bpf: Fix wrong copied_seq calculation") added psock->ingress_bytes to
record how many bytes went to self and take them out of the read_sock
side.
That works for RPC style traffic, where each reply is read before the
next one arrives. But psock->copied_seq is advanced on every read_sock
call while ingress_bytes is reset on every call, so tp->copied_seq
moves forward whenever a new skb arrives, even if the user never calls
recvmsg(). Once the parser runs twice before a read, the SK_PASS bytes
of the first run are no longer subtracted and tp->copied_seq already
covers them. recvmsg() then adds them again and copied_seq passes
rcv_nxt. The socket now looks empty to TCP, the parser is not woken for
new data anymore and recvmsg() keeps returning -EAGAIN while the skbs
sit on sk_receive_queue.
tcp_bpf_recvmsg_parser() has the same problem from the other side. It
snapshots tp->copied_seq at entry and writes the snapshot plus
copied_from_self back at exit. In between, tcp_msg_wait_data() releases
the socket lock and tcp_data_ready() runs the verdict of read_skb under
it. Both can advance copied_seq after the snapshot. Writing the snapshot
back rewinds that progress and leaves copied_seq behind the queued data.
tp->copied_seq should only cover bytes that have left the socket: what
the verdict redirected or dropped, plus what the user has read. SK_PASS
bytes still queued in ingress_msg must not count until recvmsg() hands
them out. So let both sides add only their own delta to the current
tp->copied_seq instead of recomputing it from a saved value. The
read_sock side advances by what this call consumed minus what it passed
to self, taking the delta from psock->copied_seq rather than the return
value so that the FIN byte is still counted. The recvmsg() side adds
copied_from_self, and counts the FIN there as well. Either change alone
moves the mismatch to the other side, so they go together.
ingress_bytes is also bumped from sk_psock_skb_ingress_enqueue(), which
the backlog work reaches without the socket lock for skbs redirected
from other sockets. Those bytes are not part of this socket's stream
and recvmsg() never adds them back, so subtracting them leaves
copied_seq behind. Count only in sk_psock_skb_ingress_self(), where
msg->sk is set, which is what recvmsg() checks.
A strparser skb has no owner when the verdict runs.
sk_psock_skb_ingress_self() sets one with skb_set_owner_r(), and when
the enqueue fails after that, the skb is left to the backlog with
skb->sk still set. It is not in ingress_bytes, so read_sock has already
counted it as consumed. But the backlog picks the path by skb->sk:
sk_psock_backlog
sk_psock_handle_skb
sk_psock_skb_ingress
if (skb->sk == sk)
sk_psock_skb_ingress_self <- sets msg->sk
and recvmsg() counts the bytes again. So once a strparser skb is left
to the backlog, it must not take the skb->sk == sk path. Drop the owner
with skb_orphan() when the enqueue fails, in sk_psock_skb_ingress() as
well, where the backlog sets the owner before the enqueue. This also
gives the rmem charge back, and the backlog charges the skb again like
any other strparser skb queued there. Picking the path by skb->sk is
nasty and will be reworked later.
The drop path clears the strparser bit before tcp_eat_skb(), so
tcp_eat_skb() no longer skips the skb and adds skb->len to
tp->copied_seq. The old code overwrote that at the end of read_sock.
With the delta it would stay, so call tcp_eat_skb() first.
Fixes: 36b62df5683c ("bpf: Fix wrong copied_seq calculation")
Fixes: e5c6de5fa025 ("bpf, sockmap: Incorrectly handling copied_seq")
Cc: stable@vger.kernel.org
Reported-by: Vega <vega@nebusec.ai>
Suggested-by: Alexei Starovoitov <alexei.starovoitov@gmail.com>
Co-developed-by: Zhiling Zou <zhilinz@nebusec.ai>
Signed-off-by: Zhiling Zou <zhilinz@nebusec.ai>
Signed-off-by: Jiayuan Chen <jiayuan.chen@linux.dev>
---
The BUG_ON(offset < 0) in tcp_collapse() when copied_seq trails the head
of sk_receive_queue is not addressed here.
A message that spans several segments moves copied_seq back when it is
passed. That is fine, the old code did the same and recvmsg() adds the
bytes back.
---
net/core/skmsg.c | 26 ++++++++++++++++++++------
net/ipv4/tcp_bpf.c | 12 ++++++------
2 files changed, 26 insertions(+), 12 deletions(-)
diff --git a/net/core/skmsg.c b/net/core/skmsg.c
index 4089901a9581..5c730eb56bc0 100644
--- a/net/core/skmsg.c
+++ b/net/core/skmsg.c
@@ -574,9 +574,6 @@ static int sk_psock_skb_ingress_enqueue(struct sk_buff *skb,
return num_sge;
}
-#if IS_ENABLED(CONFIG_BPF_STREAM_PARSER)
- psock->ingress_bytes += len;
-#endif
copied = len;
msg->sg.start = 0;
msg->sg.size = copied;
@@ -616,8 +613,11 @@ static int sk_psock_skb_ingress(struct sk_psock *psock, struct sk_buff *skb,
*/
skb_set_owner_r(skb, sk);
err = sk_psock_skb_ingress_enqueue(skb, off, len, psock, sk, msg, true);
- if (err < 0)
+ if (err < 0) {
kfree(msg);
+ /* Same as in sk_psock_skb_ingress_self() */
+ skb_orphan(skb);
+ }
return err;
}
@@ -642,8 +642,21 @@ static int sk_psock_skb_ingress_self(struct sk_psock *psock, struct sk_buff *skb
*/
msg->sk = sk;
err = sk_psock_skb_ingress_enqueue(skb, off, len, psock, sk, msg, take_ref);
- if (err < 0)
+ if (err < 0) {
kfree(msg);
+ /*
+ * A strparser skb comes in without an owner and read_sock counts
+ * it as consumed once it is left to the backlog. Drop the owner
+ * again, or sk_psock_skb_ingress() takes the skb->sk == sk path
+ * and recvmsg() counts it a second time.
+ */
+ if (skb_bpf_strparser(skb))
+ skb_orphan(skb);
+ }
+#if IS_ENABLED(CONFIG_BPF_STREAM_PARSER)
+ else
+ psock->ingress_bytes += len;
+#endif
return err;
}
@@ -1061,8 +1074,9 @@ static int sk_psock_verdict_apply(struct sk_psock *psock, struct sk_buff *skb,
case __SK_DROP:
default:
out_free:
- skb_bpf_redirect_clear(skb);
+ /* tcp_eat_skb() checks the strparser bit, clear it after */
tcp_eat_skb(psock->sk, skb);
+ skb_bpf_redirect_clear(skb);
sock_drop(psock->sk, skb);
}
diff --git a/net/ipv4/tcp_bpf.c b/net/ipv4/tcp_bpf.c
index 788ce08eef4d..e59103e77aa5 100644
--- a/net/ipv4/tcp_bpf.c
+++ b/net/ipv4/tcp_bpf.c
@@ -249,7 +249,6 @@ static int tcp_bpf_recvmsg_parser(struct sock *sk,
struct tcp_sock *tcp;
int copied_from_self = 0;
int copied = 0;
- u32 seq;
if (unlikely(flags & MSG_ERRQUEUE))
return inet_recv_error(sk, msg, len);
@@ -263,7 +262,6 @@ static int tcp_bpf_recvmsg_parser(struct sock *sk,
lock_sock(sk);
tcp = tcp_sk(sk);
- seq = tcp->copied_seq;
/* We may have received data on the sk_receive_queue pre-accept and
* then we can not use read_skb in this context because we haven't
* assigned a sk_socket yet so have no link to the ops. The work-around
@@ -295,11 +293,10 @@ static int tcp_bpf_recvmsg_parser(struct sock *sk,
if (is_fin) {
copied = 0;
- seq++;
+ copied_from_self++;
goto out;
}
}
- seq += copied_from_self;
if (!copied) {
long timeo;
int data;
@@ -341,7 +338,7 @@ static int tcp_bpf_recvmsg_parser(struct sock *sk,
}
out:
if (!peek)
- WRITE_ONCE(tcp->copied_seq, seq);
+ WRITE_ONCE(tcp->copied_seq, tcp->copied_seq + copied_from_self);
tcp_rcv_space_adjust(sk);
/* Data on the psock outlives the connection that it came in on */
if (copied > 0 && tcp_can_send_ack(sk))
@@ -756,6 +753,7 @@ int tcp_bpf_strp_read_sock(struct strparser *strp, read_descriptor_t *desc,
struct sk_psock *psock;
struct tcp_sock *tp;
int copied = 0;
+ u32 seq;
tp = tcp_sk(sk);
rcu_read_lock();
@@ -765,6 +763,7 @@ int tcp_bpf_strp_read_sock(struct strparser *strp, read_descriptor_t *desc,
goto out;
}
+ seq = psock->copied_seq;
psock->ingress_bytes = 0;
copied = tcp_read_sock_noack(sk, desc, recv_actor, true,
&psock->copied_seq);
@@ -775,7 +774,8 @@ int tcp_bpf_strp_read_sock(struct strparser *strp, read_descriptor_t *desc,
* For SK_REDIRECT, we need to ack the frame immediately but for
* SK_PASS, we want to delay the ack until tcp_bpf_recvmsg_parser().
*/
- tp->copied_seq = psock->copied_seq - psock->ingress_bytes;
+ WRITE_ONCE(tp->copied_seq, tp->copied_seq + psock->copied_seq - seq -
+ psock->ingress_bytes);
tcp_rcv_space_adjust(sk);
__tcp_cleanup_rbuf(sk, copied - psock->ingress_bytes);
out:
--
2.43.0
Technical Details
Message-ID:
<20261202143337.400831-2-jiayuan.chen@linux.dev>Path:
mbox-mo1.pugleaf.net!news-can.pugleaf.net!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-375851-linux-kernel-netdev=dewy.ruReferences:
<20261202143337.400831-1-jiayuan.chen@linux.dev>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-can.pugleaf.net!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-375851-linux-kernel-netdev=dewy.ru From: jiayuan.chen@linux.dev (Jiayuan Chen) Newsgroups: linux.kernel.netdev Subject: [PATCH bpf v3 1/9] bpf, sockmap: Fix copied_seq updates with strparser Date: Wed, 2 Dec 2026 22:30:40 +0800 Organization: Usenet Network Message-ID: <20261202143337.400831-2-jiayuan.chen@linux.dev> References: <20261202143337.400831-1-jiayuan.chen@linux.dev> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Injection-Info: usenet.network; logging-data="1990615"; mail-complaints-to="abuse@usenet.network" Cancel-Lock: sha1:fBdJJgKR1WLhVxM5qF3bJpeObGc= sha256:buPVEdf9n5mV0p4otIryG8jwevArwpR3P4fBTXhG6H8= sha1:gQgHQsAlL3uwqN7dCnoHLIAuIUQ= sha256:TLhkNI+Utr1g+p2Bzyg77Rw6xYXouxmUGdgZ3wpAchI= 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 255CD117670; Wed, 2 Dec 2026 17:34:50 +0300 (MSK) X-Virus-Scanned: Debian amavisd-new at usenet.network X-Spam-Flag: NO X-Spam-Score: -2.05 X-Spam-Status: No, score=-2.05 tagged_above=-9999 required=6.31 tests=[DCC_REPUT_70_89=0.1, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, HEADER_FROM_DIFFERENT_DOMAINS=0.249, HK_RANDOM_FROM=1, MAILING_LIST_MULTI=-1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, URIBL_BLOCKED=0.001] autolearn=ham autolearn_force=no X-Authentication-Results: mail.dewy.ru (amavisd-new); dkim=pass (1024-bit key) header.d=linux.dev 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 c-r2B-erOgfc; Wed, 2 Dec 2026 17:34:45 +0300 (MSK) X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.234.253.10; helo=sea.lore.kernel.org; envelope-from=netdev+bounces-375851-linux-kernel-netdev=dewy.ru@vger.kernel.org; receiver=<UNKNOWN> X-Authentication-Results: OpenDMARC; dmarc=fail (p=none dis=none) header.from=linux.dev X-Received: from sea.lore.kernel.org (sea.lore.kernel.org [172.234.253.10]) by mail.dewy.ru (Postfix) with ESMTPS id 9343011766A; Wed, 2 Dec 2026 17:34:36 +0300 (MSK) X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.77.114.92]) by sea.lore.kernel.org (Postfix) with ESMTP id BE0612C635; Wed, 2 Dec 2026 14:34:32 +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=1796222070; b=Z0gXYZMG0YvYEAXmAT5qQKqJQ507j6oEOK5rpPpX4a5zVJkEHjx7Kez57U583Sc5pOtA uYq+dwOtgXTlsKm0RZFRZx32xo8wtEi62rHJlkxCP5KqLLqM0OBUdclCAVHCChDJM0Fst TyDb5mpCGvRHv/cXN3g/h0kg46EucGSQf9YSxRPo7sW1kP3cYF70MIYuToFHW4IZRiN9T HIffHIeTnMBWyJRKGxSOw4m6jvXnuHCujO994VhI1HbEiJ/ndp9XyaXboAuK13uSAYg7P yMrWXzuOl91tps3eCKJuE+3+SgbSUjhzXp+U5fbhQMBSPulhvJx6yQKUgjzX5CutCdg== X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796222070; h=X-Original-To:Received:DMARC-Filter:X-Envelope-To:DKIM-Signature: X-Envelope-To:Received:X-Mizu-Trace-ID:X-Migadu-Flow:From:To:Cc: Subject:Date:Message-ID:X-Mailer:In-Reply-To:References: X-Mailing-List:List-Id:List-Post:List-Help:List-Subscribe: List-Unsubscribe:List-Owner:Precedence:MIME-Version: Content-Transfer-Encoding; bh=izLcMdCbq0e5ve85xkId5uXLE85cP6fGztsMwC08QkA=; b=PKoczfwf/wOZE2VyTpH7i4DTy9afvdxjF/tKHtylngdgguJT8QXfyKPMkY3H4QeWslEw cWh/xmczBEC+I6tyRAuWDzoibzaJvxFGhF6Gs0b3I/tibm8yQchq+FDLJgWxqMOpWjcn/ bYPyKg0uvW55SyAsRBPjGI3ZWDwml37l8/cKKPMPaIxkNKVn7E12Y1SmXNqR8OhZPHZp7 tXcV1kUMRVasgVG2FKb2IqBuxZ7sRN31IxEYvpsaFY8EWqli2/4+RnPh6jqdmWIQnPiyG sG7MNdvEUte0mD0pKa+CTz9xWlbW58ltzY+1lVV9YQYMEr2lnqXhssuqyR/DjvTNIIA== X-ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dkim=pass header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b="C69+ARS/"; dmarc=pass header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; 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 A7881606A33F; Wed, 2 Dec 2026 14:34:30 +0000 (UTC) X-Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key, unprotected) header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=C69+ARS/ X-Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=91.218.175.39 X-ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796222069; b=yYOF7gm5jHCiABwFVQklh+0uxroSZsucmcBcfamrYImOmS1vzXfV5cr8uT3RB2XYdS4w 63dzpaWS1buq7AtAnf7yY0/PD+u6cNl79DQU1OEfqoGkUWLYCFaidcYMqmSPH5rzYHqAg HnKey23CmJ/lUDyMlYtFQ9H/DrNH/oV0V7Yg8ynMHLTq4zxnF0WpYYajL7PFPpc7yYeLY x7WwQFBPn9iMPTC/H920BT1lZ03DIaH/kXAffH3hRRyF7IVLUIj5EwoPpVR8LDgt5md0V V+ibOVD94JlxX7qnSok64GvlIO+ljsdvlB8/hl3Wr1EjmCNoWmfXN5quAM4uWaiJ43Q== X-ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796222069; h=DMARC-Filter:X-Envelope-To:DKIM-Signature:X-Envelope-To:Received: X-Mizu-Trace-ID:X-Migadu-Flow:From:To:Cc:Subject:Date:Message-ID: X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=izLcMdCbq0e5ve85xkId5uXLE85cP6fGztsMwC08QkA=; b=2iszqrUOhaq5oK+yeKZwGfPz0r10MKm11NzllNN3npsAWMq8NXnhPD980Jo1jbl+AdMZ JvqVtJwvae8Nk65ELa3Ahsd4x56dZvGXNM5YW2YMxjiby0jm1AwOL9S5Xh2tlBkXYPMOo EuqB5dEtxeSYd9ijz2HK+na22pbkd/gIFNTyXjgA7IAs9WiX8j36ht/cvgwn0bVjRLIjE jmvf+uccOdHeS3Pp2JlJfr8kYQEG9C2oxHJIkwCQSwyUp6jgSzL0QAvqW19V6R8OKCImk 0p9I8AwRZuKH28/nxIz6qX4veQkIxwQlaI2bt/HMdtfckSSxowHdJCDC7nqRrUC38Ig== X-ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b="C69+ARS/"; arc=none smtp.remote-ip=91.218.175.39 X-Received: from mta0.migadu.com (out-39.mta0.migadu.com [91.218.175.39]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8278A606A323 for <netdev@vger.kernel.org>; Wed, 2 Dec 2026 14:34:28 +0000 (UTC) X-DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 8278A606A323 X-Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev X-Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev X-Envelope-To: netdev@vger.kernel.org X-DKIM-Signature: a=rsa-sha256; bh=RaaYZDsE3Lvdc+L4N6WmwzWcd1GdT+GklYXM/T5ad5Y=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1796222066; v=1; x=1796826866; b=C69+ARS/J84OsjJI32lsMLz/m1zWqAcQndV94Vro0Wdr7crsUgQNLECds1H4pRDwTtYVnCW0 NsPW4dyeAwusd0/zIs351x7Nv9W2OnQFuMVe1oCiql1WV9dOvT0HxBmgNVRKOC6sKDo7L/UgTj7 fqyf1P7fCmclMnQ4pcHgtdkM= X-Envelope-To: netdev@vger.kernel.org X-Received: by smtp.migadu.com with ESMTPS id 3524bf191c43311c; Wed, 02 Dec 2026 14:34:26 +0000 X-Mizu-Trace-ID: 3524bf191c43311c X-Migadu-Flow: FLOW_OUT X-Cc: Jiayuan Chen <jiayuan.chen@linux.dev>, stable@vger.kernel.org, Vega <vega@nebusec.ai>, Alexei Starovoitov <alexei.starovoitov@gmail.com>, Zhiling Zou <zhilinz@nebusec.ai>, 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>, Willem de Bruijn <willemb@google.com>, Simon Horman <horms@kernel.org>, Kuniyuki Iwashima <kuniyu@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 X-Mailer: git-send-email 2.43.0 X-In-Reply-To: <20261202143337.400831-1-jiayuan.chen@linux.dev> 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