🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: linux.kernel.netdev
Article #19083

Re: [PATCH net 2/2] selftests: net: cover connected UDP reuseport rescore

#19083
From: netdev-bot+sashiko
Date: Thu, 19 Nov 2026 09:57
194 lines
15425 bytes
Thank you for your contribution! Sashiko AI review found 3 potential
issue(s) to consider.

Critical: 0 · High: 1 · Medium: 1 · Low: 1

If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).

- [High] This patch's parent commit in the series, "udp: fix reuseport
  rescore fallback", changes the rescore branch in udp4_lib_lookup2()…
- [Medium] On the default CONFIG_BASE_SMALL=n kernel,
  test_connected_reuseport_rescore() never has a BPF program select the…
- [Low] The comment on the second assertion in
  test_connected_reuseport_rescore()…

--

selftests: net: cover connected UDP reuseport rescore

This adds a reuseport_bpf selftest in which a constant classic BPF
program selects a connected UDP socket in a reuseport group. A packet
from a different peer should go to the unconnected socket, and a packet
from the connected peer should still reach the connected socket. The
test runs for both IPv4 and IPv6.

> diff --git a/tools/testing/selftests/net/reuseport_bpf.c b/tools/testing/selftests/net/reuseport_bpf.c
> index 12e48b97b8625..fbfe73dce2111 100644
> --- a/tools/testing/selftests/net/reuseport_bpf.c
> +++ b/tools/testing/selftests/net/reuseport_bpf.c
> @@ -303,6 +303,154 @@ static void test_reuseport_cbpf(struct test_params p)

[ ... ]

> +static void test_connected_reuseport_rescore(int family)
> +{

[ ... ]

> +	good_addr = new_loopback_sockaddr(family, 0);
> +	if (bind(good_fd, good_addr, sockaddr_size()))
> +		error(1, errno, "failed to bind good reuseport socket");
> +	if (getsockname(good_fd, good_addr, (socklen_t []){ sockaddr_size() }))
> +		error(1, errno, "failed to get reuseport port");
> +	recv_port = get_sockaddr_port(good_addr);
> +	if (bind(connected_fd, good_addr, sockaddr_size()))
> +		error(1, errno, "failed to bind connected reuseport socket");
> +
> +	match_src = new_loopback_sockaddr(family, 0);

[ ... ]

> +	peer_addr = new_loopback_sockaddr(family, peer_port);
> +	if (connect(connected_fd, peer_addr, sockaddr_size()))
> +		error(1, errno, "failed to connect reuseport socket");
> +
> +	/* Bind order makes good_fd index 0 and connected_fd index 1. */
> +	attach_cbpf_index(good_fd, 1);

[Severity: Medium]
Is connected_fd still in the reuseport group at this point?

On a default CONFIG_BASE_SMALL=n kernel, connected_fd is bound to
127.0.0.1 or ::1. So udp_connect() calls udp4_hash4() (udpv6_connect()
calls udp6_hash4()), and udp_lib_hash4() detaches the socket:

net/ipv4/udp.c:udp_lib_hash4() {
    ...
	spin_lock_bh(&hslot->lock);
	if (rcu_access_pointer(sk->sk_reuseport_cb))
		reuseport_detach_sock(sk);
    ...
}

That leaves good_fd as the only member (num_socks == 1), while has_conns
stays set from __ip4_datagram_connect(). The cBPF return value of 1 is
then out of range, and run_bpf_filter() rejects it:

net/core/sock_reuseport.c:run_bpf_filter() {
    ...
	if (index >= socks)
		return NULL;
    ...
}

reuseport_select_sock_by_hash() then picks good_fd.

As a result, the mismatch packet reaches good_fd on both the old and the
new code. A good_fd rescored to an equal score is kept either way. The
match packet is resolved by udp4_lib_lookup4() or udp6_lib_lookup4() in
__udp4_lib_lookup() or __udp6_lib_lookup(), before udp*_lib_lookup2()
runs.

The case the test is named after, a BPF-selected connected socket kept as
a stale result, only seems to reproduce on CONFIG_BASE_SMALL=y kernels.
There udp_lib_hash4() is a stub and connected_fd stays at index 1.

Could the setup keep a connected socket in the group on hash4 kernels?
One option is to bind and connect connected_fd before good_fd binds, so
that reuseport_add_sock() rebuilds the group around it, and then set
has_conns. Failing that, could the test at least check the group
membership it assumes, rather than passing silently?

[ ... ]

> +	/* An unmatched BPF-selected socket must not replace the valid candidate. */
> +	send_udp_packet(other_send_fd, dst, mismatch_payload);
> +	expect_udp_socket(epfd, good_fd, connected_fd, good_fd,
> +			  mismatch_payload);

[Severity: High]
This is about the parent commit 33c2c891d6d7 ("udp: fix reuseport rescore
fallback"), but this test can't detect it. That commit changes the
rescore branch in udp4_lib_lookup2() and udp6_lib_lookup2() to:

		if (need_rescore) {
			if (score > badness)
				badness = score;
			else
				result = sk;
			continue;
		}

Suppose the socket picked by the reuseport program or hash scores the
same as sk, the socket that set badness. Won't the pick then be dropped
and sk returned instead?

Before that change, an equal score left result unchanged, so the pick
was kept. Only a lower score, the stale connected socket case, seems to
need the fallback.

Unconnected sockets have sk_incoming_cpu == -1 unless SO_INCOMING_CPU is
set, and only connected sockets update it. So equal scores are the usual
case.

reuseport_has_conns_set() sets has_conns in __ip4_datagram_connect() and
__ip6_datagram_connect() when a member bound to a specific address
connects. The flag is never cleared, and it stays set after
udp_lib_hash4() detaches the connected socket.

For example, take unconnected A and B plus connected C, all bound to
127.0.0.1:X, with the BPF program or hash picking B. B rescores equal to
A, the else branch sets result = A, and A gets every packet from
unconnected peers.

Does this override hash load balancing and cBPF/eBPF steering in every
group with has_conns set? One such setup is a QUIC-style server with
several unconnected workers and per-connection connected sockets on the
same address and port.

The parent is tagged Cc: stable. Should it keep the pick unless
score < badness?

This test uses only one unconnected and one connected socket, so it
can't catch this. Could it add a second unconnected socket and have the
BPF program select that one?

> +
> +	/* A matching selected socket must still win the positive rescore path. */
> +	send_udp_packet(match_send_fd, dst, match_payload);
> +	expect_udp_socket(epfd, good_fd, connected_fd, connected_fd,
> +			  match_payload);

[Severity: Low]
Is this comment accurate? On every config, the match packet seems to be
decided by something other than the positive rescore branch
(score > badness).

On default CONFIG_BASE_SMALL=n kernels, connected_fd is in the 4-tuple
hash. __udp4_lib_lookup() returns it from udp4_lib_lookup4() before
udp4_lib_lookup2() runs, and IPv6 does the same through
udp6_lib_lookup4().

On IPv4 with CONFIG_BASE_SMALL=y, connected_fd is bound second, and
udp_lib_get_port() adds it at the head of hslot2. It is scanned first and
taken by this shortcut, with no reuseport selection or rescore:

		if (sk->sk_state == TCP_ESTABLISHED) {
			result = sk;
			continue;
		}

On IPv6 with CONFIG_BASE_SMALL=y, the rescore does run. But connected_fd
outscores good_fd on its own merits. Even if the positive branch fell
back to result = sk, the next iteration would still pick connected_fd
through the TCP_ESTABLISHED shortcut.

So can this assertion catch a regression in the badness = score branch?

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/cover.1794759652.git.zihanx%40nebusec.ai
Technical Details
Message-ID: <179508226333.3136.7134798566838618368@kernel.org>
Path: mbox-mo1.pugleaf.net!news-lax.pugleaf.net!srl.newsdeef.eu!newsfeed.bofh.team!news.nntp4.net!nntp.xenet.de!news.neodome.net!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-366923-linux-kernel-netdev=dewy.ru
References: <4302d941304941ea6059b3d1fa167236635686c6.1794759652.git.zihanx@nebusec.ai>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-lax.pugleaf.net!srl.newsdeef.eu!newsfeed.bofh.team!news.nntp4.net!nntp.xenet.de!news.neodome.net!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-366923-linux-kernel-netdev=dewy.ru
From: netdev-bot+sashiko@kernel.org
Newsgroups: linux.kernel.netdev
Subject: Re: [PATCH net 2/2] selftests: net: cover connected UDP reuseport rescore
Date: Thu, 19 Nov 2026 09:57:43 +0000
Organization: Usenet Network
Message-ID: <179508226333.3136.7134798566838618368@kernel.org>
References: <4302d941304941ea6059b3d1fa167236635686c6.1794759652.git.zihanx@nebusec.ai>
MIME-Version: 1.0
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: 8bit
Injection-Info: usenet.network;
	logging-data="2842945"; mail-complaints-to="abuse@usenet.network"
Cancel-Lock: sha1:f2APWKncT0Hz0EvAan29dkkE8+o= sha256:KZTpR0tVPm46Oxb36e2QnxWJIXk3bgBWOaAKhr8Y2Ew=
	sha1:LaNdyUx+5RiPNcUuMgFN1ZKEicA= sha256:eM3LXOUQ5qAgmoyLeBHio+pshSsZ1ca9KPpi+gqnECY=
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 92407115597; Thu, 19 Nov 2026 12:58:05 +0300 (MSK)
X-Virus-Scanned: Debian amavisd-new at usenet.network
X-Spam-Flag: NO
X-Spam-Score: -3.5
X-Spam-Status: No, score=-3.5 tagged_above=-9999 required=6.31
	tests=[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,
	URIBL_BLOCKED=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 EsXI2ovxmVV8;
	Thu, 19 Nov 2026 12:58:01 +0300 (MSK)
X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.232.135.74; helo=sto.lore.kernel.org; envelope-from=netdev+bounces-366923-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 sto.lore.kernel.org (sto.lore.kernel.org [172.232.135.74])
	by mail.dewy.ru (Postfix) with ESMTPS id 558831155A9; Thu, 19 Nov 2026 12:57:52 +0300 (MSK)
X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.77.114.92])
	by sto.lore.kernel.org (Postfix) with ESMTP id 8898260E003; Thu, 19 Nov 2026 09:57:51 +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=1795082270;
	b=pijbVMVQYghszlWIQgnHAznbMf7yuK05K5Q3MpybiWSwiHodXDQdUXOsJqhfJKRjoKjk
	 laFxWRuhwUzsO97n18fKDhGXHC8QDSvZJ7M826+v05AGcTWlAQSrOjOGsuU3YJnJEp1i5
	 FGFfpMD5ofl9awo9/WFXtAYdl52B0mmPZNIcfFPO8qfC7jtCjQ+WG4+QLMevCCpDGeNXI
	 ccJI2f7hGWnMjL5TQV/4k42bPsrnk9+aiUOXPerE3/hYbgmBMDxckbToN8wDk3a/zFUg8
	 TxPQVhk3cdHXEonYRN2G5i146S0L6YTNhwR6N7+RTw1O34lBYCFs3WwABr+HUIArKLQ==
X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1795082270;
	h=X-Original-To:Received:Received:DKIM-Signature:Subject:From:To:Cc:
	 Date:Message-ID:In-Reply-To:References:X-sashiko-severity:
	 Content-Type:Content-Transfer-Encoding:X-Mailing-List:List-Id:
	 List-Post:List-Help:List-Subscribe:List-Unsubscribe:List-Owner:
	 Precedence:MIME-Version;
	bh=lOEEb3xkcDjySa+zz1fKZtGI0y7c9MFtdo/OrpAicu0=;
	b=Q46LojnXiG+2wYuzYuRc7v8fi8CycbCFvhjPp2usjQ8oXF2pLotd8b7Ud+vWdf0S+KoT
	 +7cNISE3+5GZjtM0Z47qhcSFdDYvFQh9nU6o4X5HFwNxQfgkLxi6l0MvVlLWY5B7EPkF3
	 nMBl8IZESncRd0/dhK75Zgxc+p6Zd0nW4xLRpeanDS+zfOVgG6gGi4h6NPihXkEnFwpRM
	 qMjGZherH57z8LXpIy73YrfyPwRhV00PM6PponoF8UMhTQs0HNTiGU2+/EK6Y21D1jS/U
	 1nkCkEqi0QwHkHhfKJIUahJIPDu5Zwe5wumM4ysJ1QBy/nCWifzvv2BFgOYrXopca1g==
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=B66W5Anj;
	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 7C6A8606A330;
	Thu, 19 Nov 2026 09:57:50 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org;
	dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=B66W5Anj
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=1795082264;
	b=Z9R9OGl4kj0rzagIvirEmB1y8VNiZT44mBtvB/Kp1oTXWzmNVeOrNefrRIrLmmRdp78q
	 SWUiDGTJ6uSN5qEaiQLS/Dp5FyL0XWU9HcSzJ3dQZQacthas/m6+ngjG9PZLaKO70V/4R
	 uETTxj8Xo7MrVKdKIx1YNkkaCgfA4+23+yIF1J5fua2AqBI6ltSP0SskmzFiAhdoSDwjn
	 7Cq97hJYzWRk+yPqhvlXxHYj6C8AcsaE+QtCm+2crCD1QkwK5HBGLthw7XH/mcrPDeVLM
	 CrmmQi43d0VmepLaYSmJUgl6VWhYY9CeDfaeB1An8IyHeSpzxpf76uUlgW9QgpB1/Jg==
X-ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1795082264;
	h=Received:DKIM-Signature:Subject:From:To:Cc:Date:Message-ID:
	 In-Reply-To:References:X-sashiko-severity:Content-Type:
	 Content-Transfer-Encoding:MIME-Version;
	bh=lOEEb3xkcDjySa+zz1fKZtGI0y7c9MFtdo/OrpAicu0=;
	b=e3758HdQasvmcS4etzbbMVHz5vBSD+nfPtyVlIe6I1xuUGCi4jBIgPNzRzGV2qekxNEz
	 5+CceS1QO70NZrQOds0o3S57KR184Jf0OxYsz3lCmrrkoRRwN/vivwYOzw13ad0haedQP
	 h0YAv7A7kyHG/zdxSFdR/8xdShxaHOgD1ppK8wyCgsFacTl3QynXt4yZ6e7om7lhXT/GT
	 IT+d6pCf0ORY5rbKRAK3Vg017oS/uNl+rKVIBiTIWHzc54svtdmEc9RKC7BQzf8g7Euka
	 VoPd8u4oPm/B9N8L6j029rnqZGqJdBmialkaaJiMCr7qL8iCbzeqiiNaBJcmORKLggw==
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=B66W5Anj;
	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 90E6C60A2607;
	Thu, 19 Nov 2026 09:57:44 +0000 (UTC)
X-Received: by smtp.kernel.org (Postfix) with ESMTPSA id BA56D1F000C4;
	Thu, 19 Nov 2026 09:57:43 +0000 (UTC)
X-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org;
	s=k20260515; t=1795082264;
	bh=lOEEb3xkcDjySa+zz1fKZtGI0y7c9MFtdo/OrpAicu0=;
	h=Subject:From:To:Cc:Date:In-Reply-To:References;
	b=B66W5AnjvREPognEqYec8dNFLWaVQhvqSRV8xcZpHLfbmA6o87wy5yoxNHtnD5M5c
	 xvmWDrjiy7S/SFDxFB7+nN4VQpcObAFiDZ93Lv5BAFZmfXuzNjTrnwWppgW+HNgLB8
	 xtI7dfaw4FT/oTvALrf3nmWiR4Ebi5Bm7i/RAaLA4/0zugLVN7uZEv2Pgf2o2e/OL0
	 R9xTvjG22tTVBMPV/qtb5E7za7OhOA43us9ZhaOyPiI3nh7uXYVq1Ou7d3WFHdrc6U
	 mFd7dHF7V13lMydBW+P6OzWtsfYvocnv+E/0+zaLQ/pyDGayTq2GkAmHIeeVLqLNYN
	 tY0qhBBUzXjbA==
X-Cc: netdev@vger.kernel.org, linux-kselftest@vger.kernel.org,
 bpf@vger.kernel.org, willemdebruijn.kernel@gmail.com, davem@davemloft.net,
 edumazet@kernel.org, pabeni@redhat.com, horms@kernel.org, benh@amazon.com,
 kuniyu@google.com, shuah@kernel.org, vega@nebusec.ai, kuba@kernel.org
X-In-Reply-To: <4302d941304941ea6059b3d1fa167236635686c6.1794759652.git.zihanx@nebusec.ai>
X-sashiko-severity: High
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