🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: linux.kernel.netdev
Article #25230

[PATCH bpf v2 08/10] bpf, sockmap: Fix apply_bytes when a redirect sends only a part

#25230
From: Jiayuan Chen
Date: Tue, 01 Dec 2026 12:18
104 lines
12987 bytes
An sk_msg prog can parse the header of an application message and call
bpf_msg_apply_bytes() with the message length. The verdict then covers
the whole message, and the prog runs again at the next header:

  +-----+-----------+-----+-------+-----+------
  | hdr |   body    | hdr | body  | hdr | ...
  +-----+-----------+-----+-------+-----+------
  ^                 ^             ^
  prog: apply(len1) prog: apply(len2)
                                  prog: apply(len3)

For SK_REDIRECT, tcp_bpf_send_verdict() takes tosend bytes off
apply_bytes before the redirect. But the redirect can send only a part:
the ingress target can't get more memory, or the egress target has no
send buffer left. The rest stays in msg, or goes back to the user who
sends it again. The verdict has already used up these bytes, so the prog
runs on the rest, in the middle of a message:

  +-----+-----------+-----+-------+------
  | hdr |   body    | hdr | body  | ...
  +-----+-----------+-----+-------+------
  |<- apply(len1) ->|
  |<-- sent -->|
               ^
               prog runs here and takes body as a header

From then on the message boundaries are wrong.

Give back the bytes that were not sent, and keep the verdict while
apply_bytes is not 0. The bytes of a cork that fails are freed and
reported as sent, so they stay used.

A cork that is sent only in part is not fixed here. The rest is corked
again and the prog runs on it later, in the middle of the message as
before. That needs more work on the cork and is left for later.

This is for a target that is busy. If the target is dead, the verdict
is kept too and sendmsg() keeps failing. A part of the message is
already at the target and the rest can't go anywhere else, so the
message is lost anyway. Return the error and let the user decide.

Fixes: 604326b41a6f ("bpf, sockmap: convert to generic sk_msg interface")
Signed-off-by: Jiayuan Chen <jiayuan.chen@linux.dev>
---
 net/ipv4/tcp_bpf.c | 22 +++++++++++++++++-----
 1 file changed, 17 insertions(+), 5 deletions(-)

diff --git a/net/ipv4/tcp_bpf.c b/net/ipv4/tcp_bpf.c
index 5b4444ee6d881..27e9981698787 100644
--- a/net/ipv4/tcp_bpf.c
+++ b/net/ipv4/tcp_bpf.c
@@ -465,7 +465,7 @@ static int tcp_bpf_send_verdict(struct sock *sk, struct sk_psock *psock,
 {
 	bool cork = false, enospc = sk_msg_full(msg), redir_ingress;
 	struct sock *sk_redir;
-	u32 tosend, origsize, sent, delta = 0;
+	u32 tosend, origsize, sent, apply, delta = 0;
 	u32 eval;
 	int ret;
 
@@ -534,6 +534,7 @@ static int tcp_bpf_send_verdict(struct sock *sk, struct sk_psock *psock,
 		redir_ingress = psock->redir_ingress;
 		sk_redir = psock->sk_redir;
 		sock_hold(sk_redir);
+		apply = psock->apply_bytes;
 		sk_msg_apply_bytes(psock, tosend);
 		if (!psock->apply_bytes) {
 			/* Clean up before releasing the sock lock. */
@@ -556,10 +557,6 @@ static int tcp_bpf_send_verdict(struct sock *sk, struct sk_psock *psock,
 					    msg, tosend, flags);
 		sent = origsize - msg->sg.size;
 
-		if (eval == __SK_REDIRECT)
-			sock_put(sk_redir);
-		sock_put(sk_redir);
-
 		lock_sock(sk);
 		sk_mem_uncharge(sk, sent);
 		if (unlikely(ret < 0)) {
@@ -582,6 +579,21 @@ static int tcp_bpf_send_verdict(struct sock *sk, struct sk_psock *psock,
 			msg = NULL;
 			ret = 0;
 		}
+		/*
+		 * Bytes that were not sent and come back, in msg or in the
+		 * next sendmsg(), are still covered by the verdict.
+		 */
+		if (apply && sent < tosend && !cork) {
+			psock->apply_bytes += tosend - sent;
+			if (eval == __SK_REDIRECT) {
+				psock->eval = eval;
+				psock->sk_redir = sk_redir;
+				eval = __SK_NONE;
+			}
+		}
+		if (eval == __SK_REDIRECT)
+			sock_put(sk_redir);
+		sock_put(sk_redir);
 		break;
 	case __SK_DROP:
 	default:
-- 
2.43.0
Technical Details
Message-ID: <20261201122055.269577-9-jiayuan.chen@linux.dev>
Path: mbox-mo1.pugleaf.net!srv.newsdeef.eu!news.mb-net.net!open-news-network.org!news.nntp4.net!nntp.xenet.de!news.neodome.net!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-375018-linux-kernel-netdev=dewy.ru
References: <20261201122055.269577-1-jiayuan.chen@linux.dev>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!srv.newsdeef.eu!news.mb-net.net!open-news-network.org!news.nntp4.net!nntp.xenet.de!news.neodome.net!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-375018-linux-kernel-netdev=dewy.ru
From: jiayuan.chen@linux.dev (Jiayuan Chen)
Newsgroups: linux.kernel.netdev
Subject: [PATCH bpf v2 08/10] bpf, sockmap: Fix apply_bytes when a redirect sends only a part
Date: Tue,  1 Dec 2026 20:18:56 +0800
Organization: Usenet Network
Message-ID: <20261201122055.269577-9-jiayuan.chen@linux.dev>
References: <20261201122055.269577-1-jiayuan.chen@linux.dev>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Injection-Info: usenet.network;
	logging-data="1708209"; mail-complaints-to="abuse@usenet.network"
Cancel-Lock: sha1:Du0cgPzM3/c4yOYBBb8FH3SQvcQ= sha256:QY/DMAUdZZd0fWrpdxgaC1VSGM6MupJh5YOE/T9OM/Q=
	sha1:Y5e3F4NF2w6+N33oym8bkcml2y0= sha256:kKrXYxdgvvYQESTfAhobtWZQZWnP0lQ+8dfuoj1dfeY=
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 29C6A11736C; Tue,  1 Dec 2026 15:25:05 +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 3Z36TD-VMIKr;
	Tue,  1 Dec 2026 15:25:01 +0300 (MSK)
X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=104.64.211.4; helo=sin.lore.kernel.org; envelope-from=netdev+bounces-375018-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 sin.lore.kernel.org (sin.lore.kernel.org [104.64.211.4])
	by mail.dewy.ru (Postfix) with ESMTPS id AE17811736A; Tue,  1 Dec 2026 15:24:50 +0300 (MSK)
X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.77.114.92])
	by sin.lore.kernel.org (Postfix) with ESMTP id B0F4C20818; Tue,  1 Dec 2026 12:24:45 +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=1796127884;
	b=qpZmuJ2TuM2zQgAGbknFjYiMox3BjgkZ9VcH4oYjQqvxJ4zY1OeTw5EUVWNpp84h5RAb
	 Z8nrS5wtQOSJWPzTta6hUaKd27727Zp8ygOHb36V0SZ0ZW0aNEnbWfzMX4tA9BxqRPzBw
	 OLRw+d73ZEKk7OXEiqUebtHiHceb83jwCbCS4IaAGkmZRn606kIvVxDMLcZ/1dbMZWzcD
	 TeBJoIas1JL2gAEZcM0F2KcovU4qh1ZvvYfSyl0WaUxSZVTOIe0yOde5NuFFACRwCj0DT
	 VC6/LsMKG06sbNLsr5SeM6EXWM1LckBl9maOQYlosUZZeclv4MGVJJ73AizF2Hcr27w==
X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1796127884;
	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=yTorJP+ZaGeGJFuKyJ/gs4i0eg8lEf7OiPoQyyUzyoo=;
	b=r8bGbYyfqo82KcdZOmDUEf8vJiwsRY9P5SwX3JIKqtVu1JsP87dY4IF1CigQdZl2A5Gz
	 9MPuSMPOG8FrAIjg7qn7tnLlmc8DtjwuAF7HtL0R9eR/7ZVpWh8uRFaGbzX61z5e9ZbId
	 lZGhXzdODv7JqqZoSZCyKJLvc99QYcOqASsJLIteGrV9z2cRmmx+GxMPBM8pGajuBZUms
	 prOmY+E+155iSb7dLmyBqpEd4QB0SshhoIAQVonu5h9Vvj4k6srhE3/L1yamsAeI2s7l6
	 wS0yHF9J/QYnE0Xx8WHAVT3/tTS5OJiZy4YNraqR9u5ZrEFTXMfedlmhGji0aVlXLlA==
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=fLWPiNHo;
	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 0866D606A332;
	Tue,  1 Dec 2026 12:24:44 +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=fLWPiNHo
X-Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=95.215.58.35
X-ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none;
	t=1796127883;
	b=VRqN+qqSOEBbSSFI5SwUHLagdpqrOItgiwBu+IZ9EbopQ3qOMV5f2LolKwjnLga/oi+h
	 WXFWBoN+DhgU/NPizdMtt/ojJghCWCuawrzZffVtEm3cxj47gXZer8mL6PGuDRgN+XaFn
	 LdYeElD2+LKSpqbyOHAruVa+9Ug6F5Jry+DaOA+uVgmEunHPnN9lK6FioaiwGoCcw7v+f
	 +rRmUGRwJbM2iNwjMEWCiQzj6Jo+AXwOt8yKY8ptlmCYZ80cwuFtKfBT0pOp7fj5A+M5Z
	 1vA87RIuzrirt3gPqZIYPDFoSm+mvbulHBFWVkGx5qySxpp6IGw8r5o9fUV1THUv5jQ==
X-ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1796127883;
	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=yTorJP+ZaGeGJFuKyJ/gs4i0eg8lEf7OiPoQyyUzyoo=;
	b=3lj75Kvx+00KPZTcxDVv6uhgfG3NlVsh9EVZSKzg+lXDIHwSmTJKVwaoRA2Kh4moFxOf
	 ZYqfULEGJScFo4avpcqjJlUwlsuhc+SCw8tlTpC5nhl02n3c3Rb3/xX8UfvT/Q7u650Fh
	 R8m//0C8IztsbAmNNiu8nZUB2oYI5DxOhShFKi42GNFLZvbNoU6M4viPJ8x1Vy5OyTGo+
	 2VK4Fi1EBScxwuj0boRuBj0GmfIoHpqYzkMKwjrwCczu/CxkiNa3p/QInS6FJX+dIgBh8
	 /eps6Li+OgkTgZjjFvcbibux8K+DSwfKAsEOq33FEX7dkkJwTVBG18cgvtnwFnN6Dqg==
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=fLWPiNHo;
	arc=none smtp.remote-ip=95.215.58.35
X-Received: from mta1.migadu.com (out-35.mta1.migadu.com [95.215.58.35])
	(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 D004C606A320
	for <netdev@vger.kernel.org>; Tue,  1 Dec 2026 12:24:41 +0000 (UTC)
X-DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org D004C606A320
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=1jwO1LRebAlQ++0gRiPVlYXiCpuC13utGM4jRf4SYm0=;
 c=simple/simple; d=linux.dev;
 h=from:to:subject:date:message-id:mime-version:content-type; s=key1;
 t=1796127880; v=1; x=1796732680;
 b=fLWPiNHo1bNaj+zQT2AtmlCgsYgCt6zJA0Ftn+aIioa5R+a/cIXpgUv+3PaZPlctCBuB5kAC
 jTpfxj/qxBuNaMj63p+ZX5D9lD/KQmmKUglCWBJNUHwJXAiI8AQZOy56V8RiSN4CtBAYpNyFE9D
 wHAziOAdm0gwfzhME9qySxJ0=
X-Envelope-To: netdev@vger.kernel.org
X-Received: by smtp.migadu.com with ESMTPS id fd96cb41223b62c6;
	Tue, 01 Dec 2026 12:24:39 +0000
X-Mizu-Trace-ID: fd96cb41223b62c6
X-Migadu-Flow: FLOW_OUT
X-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>,
	Mattia Meleleo <mattia.meleleo@coralogix.com>,
	Michal Luczaj <mhal@rbox.co>,
	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: <20261201122055.269577-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