All of lore.kernel.org
 help / color / mirror / Atom feed
From: Eric Dumazet <eric.dumazet@gmail.com>
To: "David S . Miller" <davem@davemloft.net>,
	Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>
Cc: netdev <netdev@vger.kernel.org>,
	Eric Dumazet <edumazet@google.com>,
	Eric Dumazet <eric.dumazet@gmail.com>
Subject: [PATCH net-next 10/10] inet: add READ_ONCE(sk->sk_bound_dev_if) in INET_MATCH()
Date: Wed, 11 May 2022 16:37:57 -0700	[thread overview]
Message-ID: <20220511233757.2001218-11-eric.dumazet@gmail.com> (raw)
In-Reply-To: <20220511233757.2001218-1-eric.dumazet@gmail.com>

From: Eric Dumazet <edumazet@google.com>

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. This also allows us
to add some __always_unused qualifiers.

Signed-off-by: Eric Dumazet <edumazet@google.com>
---
 include/net/inet_hashtables.h | 52 +++++++++++++++++++++++++----------
 1 file changed, 38 insertions(+), 14 deletions(-)

diff --git a/include/net/inet_hashtables.h b/include/net/inet_hashtables.h
index 98e1ec1a14f0382d1f4f8e85fe5ac2a056d2d6bc..5d3fa071d754601149c9ad0dd559f074ac58deaa 100644
--- a/include/net/inet_hashtables.h
+++ b/include/net/inet_hashtables.h
@@ -307,23 +307,47 @@ static inline struct sock *inet_lookup_listener(struct 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 == (__ports))			&&	\
-	 ((__sk)->sk_addrpair == (__cookie))			&&	\
-	 (((__sk)->sk_bound_dev_if == (__dif))			||	\
-	  ((__sk)->sk_bound_dev_if == (__sdif)))		&&	\
-	 net_eq(sock_net(__sk), (__net)))
+static inline bool INET_MATCH(const struct sock *sk, struct net *net,
+			      const __addrpair cookie,
+			      const __be32 __always_unused saddr,
+			      const __be32 __always_unused daddr,
+			      const __portpair ports,
+			      const int dif,
+			      const int sdif)
+{
+	int bound_dev_if;
+
+	if (!net_eq(sock_net(sk), net) ||
+	    sk->sk_portpair != ports ||
+	    sk->sk_addrpair != cookie)
+		return false;
+
+	bound_dev_if = READ_ONCE(sk->sk_bound_dev_if);
+	return bound_dev_if == dif || bound_dev_if == sdif;
+}
 #else /* 32-bit arch */
 #define INET_ADDR_COOKIE(__name, __saddr, __daddr) \
-	const int __name __deprecated __attribute__((unused))
+	const int __name __deprecated __always_unused
 
-#define INET_MATCH(__sk, __net, __cookie, __saddr, __daddr, __ports, __dif, __sdif) \
-	(((__sk)->sk_portpair == (__ports))		&&		\
-	 ((__sk)->sk_daddr	== (__saddr))		&&		\
-	 ((__sk)->sk_rcv_saddr	== (__daddr))		&&		\
-	 (((__sk)->sk_bound_dev_if == (__dif))		||		\
-	  ((__sk)->sk_bound_dev_if == (__sdif)))	&&		\
-	 net_eq(sock_net(__sk), (__net)))
+static inline bool INET_MATCH(const struct sock *sk, struct net *net,
+			      const __addrpair __always_unused cookie,
+			      const __be32 saddr,
+			      const __be32 daddr,
+			      const __portpair ports,
+			      const int dif,
+			      const int sdif)
+{
+	int bound_dev_if;
+
+	if (!net_eq(sock_net(sk), net) ||
+	    sk->sk_portpair != ports ||
+	    sk->sk_daddr != saddr ||
+	    sk->sk_rcv_saddr != daddr)
+		return false;
+
+	bound_dev_if = READ_ONCE(sk->sk_bound_dev_if);
+	return bound_dev_if == dif || bound_dev_if == sdif;
+}
 #endif /* 64-bit arch */
 
 /* Sockets in TCP_CLOSE state are _always_ taken out of the hash, so we need
-- 
2.36.0.512.ge40c2bad7a-goog


  parent reply	other threads:[~2022-05-11 23:38 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-05-11 23:37 [PATCH net-next 00/10] net: add annotations for sk->sk_bound_dev_if Eric Dumazet
2022-05-11 23:37 ` [PATCH net-next 01/10] net: annotate races around sk->sk_bound_dev_if Eric Dumazet
2022-05-11 23:37 ` [PATCH net-next 02/10] sctp: read sk->sk_bound_dev_if once in sctp_rcv() Eric Dumazet
2022-05-11 23:37 ` [PATCH net-next 03/10] tcp: sk->sk_bound_dev_if once in inet_request_bound_dev_if() Eric Dumazet
2022-05-11 23:37 ` [PATCH net-next 04/10] net: core: add READ_ONCE/WRITE_ONCE annotations for sk->sk_bound_dev_if Eric Dumazet
2022-05-11 23:37 ` [PATCH net-next 05/10] dccp: use READ_ONCE() to read sk->sk_bound_dev_if Eric Dumazet
2022-05-11 23:37 ` [PATCH net-next 06/10] inet: add READ_ONCE(sk->sk_bound_dev_if) in inet_csk_bind_conflict() Eric Dumazet
2022-05-11 23:37 ` [PATCH net-next 07/10] net_sched: em_meta: add READ_ONCE() in var_sk_bound_if() Eric Dumazet
2022-05-11 23:37 ` [PATCH net-next 08/10] l2tp: use add READ_ONCE() to fetch sk->sk_bound_dev_if Eric Dumazet
2022-05-11 23:37 ` [PATCH net-next 09/10] ipv6: add READ_ONCE(sk->sk_bound_dev_if) in INET6_MATCH() Eric Dumazet
2022-05-12 15:48   ` kernel test robot
2022-05-13  1:00   ` kernel test robot
2022-05-11 23:37 ` Eric Dumazet [this message]
2022-05-12 13:15   ` [PATCH net-next 10/10] inet: add READ_ONCE(sk->sk_bound_dev_if) in INET_MATCH() kernel test robot
2022-05-12 16:13     ` Eric Dumazet
2022-05-12 16:13       ` Eric Dumazet
2022-05-20  8:40       ` Chen, Rong A
2022-05-20 13:17         ` Eric Dumazet
2022-05-20 13:17           ` Eric Dumazet
2022-05-13 21:30 kernel test robot

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=20220511233757.2001218-11-eric.dumazet@gmail.com \
    --to=eric.dumazet@gmail.com \
    --cc=davem@davemloft.net \
    --cc=edumazet@google.com \
    --cc=kuba@kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.