Netdev List
 help / color / mirror / Atom feed
From: netdev-bot+sashiko@kernel.org
To: zihanx@nebusec.ai
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
Subject: Re: [PATCH net 2/2] selftests: net: cover connected UDP reuseport rescore
Date: Thu, 19 Nov 2026 09:57:43 +0000	[thread overview]
Message-ID: <179508226333.3136.7134798566838618368@kernel.org> (raw)
In-Reply-To: <4302d941304941ea6059b3d1fa167236635686c6.1794759652.git.zihanx@nebusec.ai>

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

      reply	other threads:[~2026-11-19  9:57 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-16  3:55 [PATCH net 0/2] udp: fix connected reuseport rescore fallback Zihan Xi
2026-11-16  3:55 ` [PATCH net 1/2] udp: fix " Zihan Xi
2026-11-16  4:51   ` Alexei Starovoitov
2026-11-16  5:08     ` zihan xi
2026-11-19  9:57   ` netdev-bot+sashiko
2026-11-16  3:55 ` [PATCH net 2/2] selftests: net: cover connected UDP reuseport rescore Zihan Xi
2026-11-19  9:57   ` netdev-bot+sashiko [this message]

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=179508226333.3136.7134798566838618368@kernel.org \
    --to=netdev-bot+sashiko@kernel.org \
    --cc=benh@amazon.com \
    --cc=bpf@vger.kernel.org \
    --cc=davem@davemloft.net \
    --cc=edumazet@kernel.org \
    --cc=horms@kernel.org \
    --cc=kuba@kernel.org \
    --cc=kuniyu@google.com \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=shuah@kernel.org \
    --cc=vega@nebusec.ai \
    --cc=willemdebruijn.kernel@gmail.com \
    --cc=zihanx@nebusec.ai \
    /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