From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796387010; b=krV+yLxDEWXKvL9GGveI1LIzmW9P5d3LBOrcETuwCXhVMQHpOCvGw1LCH0klaBLpBXqb HRCL2JFJJA+fMsjX9/SeZsH3wSfVVTvwDSYYBhiDzVtDxL7gBL04OoWaIopiXo0b/ADZm qJSR0uve2JYOHHnXsR8B+FPrvOctik5PqZ8WOHyskSvHwR3arfwHtXIB4Xl2P7hlxvLbo JUGrykGpNzhgPkkfr0upPYwXLuPDL4Aex/IiZNDdJz0p0Oq8KI0tXH2A1H/ySmJDuN9rj SMbNi8Zybn+Bf4X01l/YWAWGHqh6KJoGZQBL7XlcfrTlopVOlaiw51FKzeOzfzmi0QQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796387010; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:User-Agent:X-stable:X-Patchwork-Hint: MIME-Version:Content-Transfer-Encoding; bh=JuuAFeFkTzuClT66CSMKEib/M+ldb2grjEsDaotGXN8=; b=w+ffCfdfeK6L/PSxzhQAnlXhKtzWInDNE71I6tfMVsDshzWt+pAuj5FlbeRSnCLv09TK OXom3TP+gFwbAPU3zJua5KK0GtTc/BDrOlXS6xVnC7/8XXTfnM1G8436FoZWKEfvMhIQK S3QafbeEJPbyXrahYhAl5VDQywov577gLHnZf6lNCrimUYP/2Ges4qS7cUKODK5lvnC/K SWTkJjfJ98rDXtW+/wMbPTanOdsXmmFRz/UXY3936yj+TOff2MYgMCvx61peCpEGwOomN mNP2CKO3UUKeorRI+BjIITjAs9Kv0X+2a1sN+lrKqN26W75jz0pfzV/NklC0fq1v+5A== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=uJvaNqLS; arc=none smtp.remote-ip=100.103.45.18 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 2449D606A322; Fri, 4 Dec 2026 12:23:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=uJvaNqLS Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7B2181F000CE; Fri, 4 Dec 2026 12:23:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796387009; bh=JuuAFeFkTzuClT66CSMKEib/M+ldb2grjEsDaotGXN8=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=uJvaNqLS1ddFZa0n46CKFsPuWovjLih4j97IgFDz4dNN0nq32V2pkTRpSqozG1229 1h7QBcKf027IWS0kyA5ib2zgqqlFeKCzUXQEc1GeXGiKiHJ7hZOqYVEH1C4XQTx0p7 qKRuDErxVxFCM20RE4xD9eTcdoVBWZX+l+nlrul4= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Kuniyuki Iwashima , Eric Dumazet , Jiayuan Chen , Paolo Abeni , Sasha Levin Subject: [PATCH 6.1 0094/1059] soreuseport: annotate data-races around reuse->socks[] and num_socks Date: Fri, 4 Dec 2026 18:53:45 +0900 Message-ID: <20261204100646.550675944@linuxfoundation.org> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261204100643.980430235@linuxfoundation.org> References: <20261204100643.980430235@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore X-Mailing-List: patches@lists.linux.dev List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 6.1-stable review patch. If anyone has any objections, please let me know. ------------------ From: Eric Dumazet [ Upstream commit 213d491308b99d026514c93980cf8dd572226b1b ] reuseport_select_sock() and reuseport_migrate_sock() read reuse->num_socks and reuse->socks[] locklessly under RCU, while writers holding reuseport_lock can change them. Readers snapshot num_socks once, then index socks[] with it. Meanwhile, writers can perform several operations, storing into slots below the reader's stale count: - __reuseport_detach_sock() moves the last socket into slot i. - __reuseport_add_sock() then writes the new socket at the (now smaller) num_socks index. - reuseport_stop_listen_sock() calls __reuseport_detach_sock() then __reuseport_add_closed_sock(), which writes slot max_socks - num_closed_socks - 1. This can be the slot a reader still considers part of the listener region when the array is full. - __reuseport_detach_closed_sock() can then rewrite that slot. All pointers stored in socks[] remain valid for the RCU grace period, but without annotations the compiler is allowed to tear or reload these accesses. For instance, reuseport_select_sock_by_hash() could reload reuse->socks[i] between the sk_state/sk_incoming_cpu checks and the return, returning a socket that did not pass them. Use WRITE_ONCE() for all socks[] stores done while the array is visible to readers, and for num_socks updates (readers already use READ_ONCE()). Use READ_ONCE() in the two lockless readers. Fixes: ef456144da8e ("soreuseport: define reuseport groups") Link: https://lore.kernel.org/netdev/20260902071900.395567-1-ruanjinjie@huawei.com/ Cc: Kuniyuki Iwashima Signed-off-by: Eric Dumazet Reviewed-by: Kuniyuki Iwashima Reviewed-by: Jiayuan Chen Link: https://patch.msgid.link/20261030120956.3026324-1-edumazet@kernel.org Signed-off-by: Paolo Abeni Signed-off-by: Sasha Levin --- net/core/sock_reuseport.c | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/net/core/sock_reuseport.c b/net/core/sock_reuseport.c index 5a165286e4d8e..e7685956b09d7 100644 --- a/net/core/sock_reuseport.c +++ b/net/core/sock_reuseport.c @@ -124,10 +124,10 @@ static int reuseport_sock_index(struct sock *sk, static void __reuseport_add_sock(struct sock *sk, struct sock_reuseport *reuse) { - reuse->socks[reuse->num_socks] = sk; + WRITE_ONCE(reuse->socks[reuse->num_socks], sk); /* paired with smp_rmb() in reuseport_(select|migrate)_sock() */ smp_wmb(); - reuse->num_socks++; + WRITE_ONCE(reuse->num_socks, reuse->num_socks + 1); reuseport_get_incoming_cpu(sk, reuse); } @@ -139,8 +139,8 @@ static bool __reuseport_detach_sock(struct sock *sk, if (i == -1) return false; - reuse->socks[i] = reuse->socks[reuse->num_socks - 1]; - reuse->num_socks--; + WRITE_ONCE(reuse->socks[i], reuse->socks[reuse->num_socks - 1]); + WRITE_ONCE(reuse->num_socks, reuse->num_socks - 1); reuseport_put_incoming_cpu(sk, reuse); return true; @@ -149,7 +149,7 @@ static bool __reuseport_detach_sock(struct sock *sk, static void __reuseport_add_closed_sock(struct sock *sk, struct sock_reuseport *reuse) { - reuse->socks[reuse->max_socks - reuse->num_closed_socks - 1] = sk; + WRITE_ONCE(reuse->socks[reuse->max_socks - reuse->num_closed_socks - 1], sk); /* paired with READ_ONCE() in inet_csk_bind_conflict() */ WRITE_ONCE(reuse->num_closed_socks, reuse->num_closed_socks + 1); reuseport_get_incoming_cpu(sk, reuse); @@ -163,7 +163,8 @@ static bool __reuseport_detach_closed_sock(struct sock *sk, if (i == -1) return false; - reuse->socks[i] = reuse->socks[reuse->max_socks - reuse->num_closed_socks]; + WRITE_ONCE(reuse->socks[i], + reuse->socks[reuse->max_socks - reuse->num_closed_socks]); /* paired with READ_ONCE() in inet_csk_bind_conflict() */ WRITE_ONCE(reuse->num_closed_socks, reuse->num_closed_socks - 1); reuseport_put_incoming_cpu(sk, reuse); @@ -522,7 +523,7 @@ static struct sock *run_bpf_filter(struct sock_reuseport *reuse, u16 socks, if (index >= socks) return NULL; - return reuse->socks[index]; + return READ_ONCE(reuse->socks[index]); } static struct sock *reuseport_select_sock_by_hash(struct sock_reuseport *reuse, @@ -533,7 +534,7 @@ static struct sock *reuseport_select_sock_by_hash(struct sock_reuseport *reuse, i = j = reciprocal_scale(hash, num_socks); do { - struct sock *sk = reuse->socks[i]; + struct sock *sk = READ_ONCE(reuse->socks[i]); if (sk->sk_state != TCP_ESTABLISHED) { /* Paired with WRITE_ONCE() in __reuseport_(get|put)_incoming_cpu(). */ -- 2.53.0