From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=91.218.175.169 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796222274; b=fBNM2Ky0pyYAcX3CXtE9u4imwMLRH/sFRUFSYtVKmUFEJaaokms3Qb8ChOJj2Bm5kk1g mqdZ4253Hje5HXgHHcHNFsNkN87tj67/GHATmaWygAMzyvFyRLHlBU3j6W8Nn/EY0XA8/ 0teOKwMY0pZFSFDIttjdeP//hpRMpYWzRDLvmooo41V2olzKBCn9tDkdgtg4BhM3JBHE3 q9henYpqxRj1V6kocD6w64wkPdlcDqwWhSHE4mIZmLIWyhC6EOM8RuU78zJYl4gkkCYhf mRFnqMculnZp9BYvV7HKo68jg8YqLBOCk2tQXuBv3bmYQPe3T/TStT+CJ3bXqSe7B/g== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796222274; 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=d5kHmZrTze08ha/7XNcxRGh3sh8DNOqIRQNSZookS/Q=; b=08IqgmjUA674SVhZw0ZE/epTuEmwnH/NWx3rZHBxxP8Mp7iraD8nJwXjiQCRsdiOC57V Epex8n+6QcdG9632cqvSJr5hq/8UhOaZ8eLJFIjjPbOofm7CI4P/5ijvjic1jXC28/yCg 4CXXRFZC5lZhXMfjrZqh/Uu4vh8z7oXDMekevw5xOXa6YaXSVEqhXDlDhKJJvFppIlzCG oWtFDpWysa45DasrgaEiBdxbAl/qP1rlvkxAvnrb8+rDkqGNHMC9/jYPW/F3TSYqk4zOw cPt3t3aMVgBkftRglVd4DnuRox3dm/2B7HbKY1oktSjoSLpKJeNgifMEA6ZMBdujccw== 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=GuqpUwAY; arc=none smtp.remote-ip=91.218.175.169 Received: from mta0.migadu.com (out-169.mta0.migadu.com [91.218.175.169]) (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 AB0E4606A31A for ; Wed, 2 Dec 2026 14:37:53 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org AB0E4606A31A Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev 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=GuqpUwAY X-Envelope-To: bpf@vger.kernel.org DKIM-Signature: a=rsa-sha256; bh=xz100yB47WHXcpiXiXgmMpJVKUdWBej3gIFOzwnk9So=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1796222271; v=1; x=1796827071; b=GuqpUwAYrVXpitUbsqm3dMCWV/LuBTstvJskgrM9glppHNtWIWABs5IKlyw2i95JTmKiGQGc H19zl8MpwWgQ9QPUGpgoAT6vzDDg9FLbQ0QLzsVQYgAKkdiP8+YGTbpJpYlTr+OWW5sFuYpLfaC WKTAcnJN7vKQhmYcgm1EywYU= X-Envelope-To: bpf@vger.kernel.org Received: by smtp.migadu.com with ESMTPS id b9c644c832bab104; Wed, 02 Dec 2026 14:37:51 +0000 X-Mizu-Trace-ID: b9c644c832bab104 X-Migadu-Flow: FLOW_OUT From: Jiayuan Chen To: bpf@vger.kernel.org Cc: Jiayuan Chen , John Fastabend , Jakub Sitnicki , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Willem de Bruijn , Simon Horman , Kuniyuki Iwashima , Neal Cardwell , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Eduard Zingerman , Kumar Kartikeya Dwivedi , Martin KaFai Lau , Song Liu , Yonghong Song , Jiri Olsa , Emil Tsalapatis , Ihor Solodrai , Shuah Khan , Sechang Lim , Mattia Meleleo , Michal Luczaj , Zijian Zhang , linux-kernel@vger.kernel.org, netdev@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [PATCH bpf v3 7/9] bpf, sockmap: Fix apply_bytes when a redirect sends only a part Date: Wed, 2 Dec 2026 22:30:46 +0800 Message-ID: <20261202143337.400831-8-jiayuan.chen@linux.dev> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20261202143337.400831-1-jiayuan.chen@linux.dev> References: <20261202143337.400831-1-jiayuan.chen@linux.dev> X-Mailing-List: bpf@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 --- 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 5b4444ee6d88..27e998169878 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