From nobody Sun Dec 14 08:05:50 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id AAD54C00140 for ; Mon, 15 Aug 2022 19:04:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S245318AbiHOTEM (ORCPT ); Mon, 15 Aug 2022 15:04:12 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49132 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244486AbiHOS5q (ORCPT ); Mon, 15 Aug 2022 14:57:46 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5C55333377; Mon, 15 Aug 2022 11:32:28 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 3521E61029; Mon, 15 Aug 2022 18:32:27 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 22645C433C1; Mon, 15 Aug 2022 18:32:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1660588346; bh=xFwjR9b18S4XtkLD52WBdoOw1GgnqbnoSM0RUQFkw24=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=z4Ae2GORnukJYeoTVZuI/tUyOQwufp96kH2It5Rx+UR09B5TgQg69A6PCRKS5/faU OPIfdQIO1oQtJq6fr31LVNiEipzz9SozJCePJkosmMim/MbGrLOXNgvKV2gH8kOL6h INeXnpPNNeRbpyyXGoqNpvDctYKrPKgub3e0shqk= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Eric Dumazet , "David S. Miller" , Sasha Levin Subject: [PATCH 5.15 374/779] inet: add READ_ONCE(sk->sk_bound_dev_if) in INET_MATCH() Date: Mon, 15 Aug 2022 20:00:18 +0200 Message-Id: <20220815180353.259445390@linuxfoundation.org> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20220815180337.130757997@linuxfoundation.org> References: <20220815180337.130757997@linuxfoundation.org> User-Agent: quilt/0.67 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" From: Eric Dumazet [ Upstream commit 4915d50e300e96929d2462041d6f6c6f061167fd ] INET_MATCH() runs without holding a lock on the socket. We probably need to annotate most reads. This patch makes INET_MATCH() an inline function to ease our changes. v2: We remove the 32bit version of it, as modern compilers should generate the same code really, no need to try to be smarter. Also make 'struct net *net' the first argument. Signed-off-by: Eric Dumazet Signed-off-by: David S. Miller Signed-off-by: Sasha Levin --- include/net/inet_hashtables.h | 35 ++++++++++++++++------------------- include/net/sock.h | 3 --- net/ipv4/inet_hashtables.c | 15 +++++---------- net/ipv4/udp.c | 3 +-- 4 files changed, 22 insertions(+), 34 deletions(-) diff --git a/include/net/inet_hashtables.h b/include/net/inet_hashtables.h index 749bb1e46087..825ad1d06d05 100644 --- a/include/net/inet_hashtables.h +++ b/include/net/inet_hashtables.h @@ -295,7 +295,6 @@ static inline struct sock *inet_lookup_listener(struct = net *net, ((__force __portpair)(((__u32)(__dport) << 16) | (__force __u32)(__be16)(= __sport))) #endif =20 -#if (BITS_PER_LONG =3D=3D 64) #ifdef __BIG_ENDIAN #define INET_ADDR_COOKIE(__name, __saddr, __daddr) \ const __addrpair __name =3D (__force __addrpair) ( \ @@ -307,24 +306,22 @@ static inline struct sock *inet_lookup_listener(struc= t net *net, (((__force __u64)(__be32)(__daddr)) << 32) | \ ((__force __u64)(__be32)(__saddr))) #endif /* __BIG_ENDIAN */ -#define INET_MATCH(__sk, __net, __cookie, __saddr, __daddr, __ports, __dif= , __sdif) \ - (((__sk)->sk_portpair =3D=3D (__ports)) && \ - ((__sk)->sk_addrpair =3D=3D (__cookie)) && \ - (((__sk)->sk_bound_dev_if =3D=3D (__dif)) || \ - ((__sk)->sk_bound_dev_if =3D=3D (__sdif))) && \ - net_eq(sock_net(__sk), (__net))) -#else /* 32-bit arch */ -#define INET_ADDR_COOKIE(__name, __saddr, __daddr) \ - const int __name __deprecated __attribute__((unused)) - -#define INET_MATCH(__sk, __net, __cookie, __saddr, __daddr, __ports, __dif= , __sdif) \ - (((__sk)->sk_portpair =3D=3D (__ports)) && \ - ((__sk)->sk_daddr =3D=3D (__saddr)) && \ - ((__sk)->sk_rcv_saddr =3D=3D (__daddr)) && \ - (((__sk)->sk_bound_dev_if =3D=3D (__dif)) || \ - ((__sk)->sk_bound_dev_if =3D=3D (__sdif))) && \ - net_eq(sock_net(__sk), (__net))) -#endif /* 64-bit arch */ + +static inline bool INET_MATCH(struct net *net, const struct sock *sk, + const __addrpair cookie, const __portpair ports, + int dif, int sdif) +{ + int bound_dev_if; + + if (!net_eq(sock_net(sk), net) || + sk->sk_portpair !=3D ports || + sk->sk_addrpair !=3D cookie) + return false; + + /* Paired with WRITE_ONCE() from sock_bindtoindex_locked() */ + bound_dev_if =3D READ_ONCE(sk->sk_bound_dev_if); + return bound_dev_if =3D=3D dif || bound_dev_if =3D=3D sdif; +} =20 /* Sockets in TCP_CLOSE state are _always_ taken out of the hash, so we ne= ed * not check it for lookups anymore, thanks Alexey. -DaveM diff --git a/include/net/sock.h b/include/net/sock.h index e0a88bb0a58c..49a6315d521f 100644 --- a/include/net/sock.h +++ b/include/net/sock.h @@ -161,9 +161,6 @@ typedef __u64 __bitwise __addrpair; * for struct sock and struct inet_timewait_sock. */ struct sock_common { - /* skc_daddr and skc_rcv_saddr must be grouped on a 8 bytes aligned - * address on 64bit arches : cf INET_MATCH() - */ union { __addrpair skc_addrpair; struct { diff --git a/net/ipv4/inet_hashtables.c b/net/ipv4/inet_hashtables.c index 342f3df77835..7c502c4a160b 100644 --- a/net/ipv4/inet_hashtables.c +++ b/net/ipv4/inet_hashtables.c @@ -410,13 +410,11 @@ struct sock *__inet_lookup_established(struct net *ne= t, sk_nulls_for_each_rcu(sk, node, &head->chain) { if (sk->sk_hash !=3D hash) continue; - if (likely(INET_MATCH(sk, net, acookie, - saddr, daddr, ports, dif, sdif))) { + if (likely(INET_MATCH(net, sk, acookie, ports, dif, sdif))) { if (unlikely(!refcount_inc_not_zero(&sk->sk_refcnt))) goto out; - if (unlikely(!INET_MATCH(sk, net, acookie, - saddr, daddr, ports, - dif, sdif))) { + if (unlikely(!INET_MATCH(net, sk, acookie, + ports, dif, sdif))) { sock_gen_put(sk); goto begin; } @@ -465,8 +463,7 @@ static int __inet_check_established(struct inet_timewai= t_death_row *death_row, if (sk2->sk_hash !=3D hash) continue; =20 - if (likely(INET_MATCH(sk2, net, acookie, - saddr, daddr, ports, dif, sdif))) { + if (likely(INET_MATCH(net, sk2, acookie, ports, dif, sdif))) { if (sk2->sk_state =3D=3D TCP_TIME_WAIT) { tw =3D inet_twsk(sk2); if (twsk_unique(sk, sk2, twp)) @@ -532,9 +529,7 @@ static bool inet_ehash_lookup_by_sk(struct sock *sk, if (esk->sk_hash !=3D sk->sk_hash) continue; if (sk->sk_family =3D=3D AF_INET) { - if (unlikely(INET_MATCH(esk, net, acookie, - sk->sk_daddr, - sk->sk_rcv_saddr, + if (unlikely(INET_MATCH(net, esk, acookie, ports, dif, sdif))) { return true; } diff --git a/net/ipv4/udp.c b/net/ipv4/udp.c index 4ad4daa16cce..efef7ba44e1d 100644 --- a/net/ipv4/udp.c +++ b/net/ipv4/udp.c @@ -2554,8 +2554,7 @@ static struct sock *__udp4_lib_demux_lookup(struct ne= t *net, struct sock *sk; =20 udp_portaddr_for_each_entry_rcu(sk, &hslot2->head) { - if (INET_MATCH(sk, net, acookie, rmt_addr, - loc_addr, ports, dif, sdif)) + if (INET_MATCH(net, sk, acookie, ports, dif, sdif)) return sk; /* Only check first socket in chain */ break; --=20 2.35.1