All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v1 net 00/15] sysctl: Fix data-races around ipv4_net_table (Round 3).
@ 2022-07-15 17:17 Kuniyuki Iwashima
  2022-07-15 17:17 ` [PATCH v1 net 01/15] igmp: Fix data-races around sysctl_igmp_llm_reports Kuniyuki Iwashima
                   ` (15 more replies)
  0 siblings, 16 replies; 18+ messages in thread
From: Kuniyuki Iwashima @ 2022-07-15 17:17 UTC (permalink / raw)
  To: David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni, David Ahern
  Cc: Kuniyuki Iwashima, Kuniyuki Iwashima, netdev

This series fixes data-races around 21 knobs after
igmp_link_local_mcast_reports in ipv4_net_table.

These 4 knobs are skipped because they are safe.

  - tcp_congestion_control: Safe with RCU and xchg().
  - tcp_available_congestion_control: Read only.
  - tcp_allowed_congestion_control: Safe with RCU and spinlock().
  - tcp_fastopen_key: Safe with RCU and xchg()

So, round 4 will start with fib_multipath_use_neigh.


Kuniyuki Iwashima (15):
  igmp: Fix data-races around sysctl_igmp_llm_reports.
  igmp: Fix a data-race around sysctl_igmp_max_memberships.
  igmp: Fix data-races around sysctl_igmp_max_msf.
  igmp: Fix data-races around sysctl_igmp_qrv.
  tcp: Fix data-races around keepalive sysctl knobs.
  tcp: Fix data-races around sysctl_tcp_syn(ack)?_retries.
  tcp: Fix data-races around sysctl_tcp_syncookies.
  tcp: Fix data-races around sysctl_tcp_migrate_req.
  tcp: Fix data-races around sysctl_tcp_reordering.
  tcp: Fix data-races around some timeout sysctl knobs.
  tcp: Fix a data-race around sysctl_tcp_notsent_lowat.
  tcp: Fix a data-race around sysctl_tcp_tw_reuse.
  tcp: Fix data-races around sysctl_max_syn_backlog.
  tcp: Fix data-races around sysctl_tcp_fastopen.
  tcp: Fix data-races around sysctl_tcp_fastopen_blackhole_timeout.

 drivers/net/amt.c               |  4 +--
 include/net/tcp.h               | 14 ++++++----
 net/core/filter.c               |  4 +--
 net/core/sock_reuseport.c       |  4 +--
 net/ipv4/af_inet.c              |  2 +-
 net/ipv4/igmp.c                 | 49 +++++++++++++++++++--------------
 net/ipv4/inet_connection_sock.c |  3 +-
 net/ipv4/ip_sockglue.c          |  6 ++--
 net/ipv4/syncookies.c           |  3 +-
 net/ipv4/tcp.c                  | 13 +++++----
 net/ipv4/tcp_fastopen.c         |  9 +++---
 net/ipv4/tcp_input.c            | 36 +++++++++++++++---------
 net/ipv4/tcp_ipv4.c             |  2 +-
 net/ipv4/tcp_metrics.c          |  3 +-
 net/ipv4/tcp_output.c           |  2 +-
 net/ipv4/tcp_timer.c            | 20 ++++++++------
 net/ipv6/syncookies.c           |  3 +-
 net/smc/smc_llc.c               |  2 +-
 18 files changed, 106 insertions(+), 73 deletions(-)

-- 
2.30.2


^ permalink raw reply	[flat|nested] 18+ messages in thread

* [PATCH v1 net 01/15] igmp: Fix data-races around sysctl_igmp_llm_reports.
  2022-07-15 17:17 [PATCH v1 net 00/15] sysctl: Fix data-races around ipv4_net_table (Round 3) Kuniyuki Iwashima
@ 2022-07-15 17:17 ` Kuniyuki Iwashima
  2022-07-15 17:17 ` [PATCH v1 net 02/15] igmp: Fix a data-race around sysctl_igmp_max_memberships Kuniyuki Iwashima
                   ` (14 subsequent siblings)
  15 siblings, 0 replies; 18+ messages in thread
From: Kuniyuki Iwashima @ 2022-07-15 17:17 UTC (permalink / raw)
  To: David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni, David Ahern
  Cc: Kuniyuki Iwashima, Kuniyuki Iwashima, netdev, Philip Downey

While reading sysctl_igmp_llm_reports, it can be changed concurrently.
Thus, we need to add READ_ONCE() to its readers.

This test can be packed into a helper, so such changes will be in the
follow-up series after net is merged into net-next.

  if (ipv4_is_local_multicast(pmc->multiaddr) &&
      !READ_ONCE(net->ipv4.sysctl_igmp_llm_reports))

Fixes: df2cf4a78e48 ("IGMP: Inhibit reports for local multicast groups")
Signed-off-by: Kuniyuki Iwashima <kuniyu@amazon.com>
---
CC: Philip Downey <pdowney@brocade.com>
---
 net/ipv4/igmp.c | 21 +++++++++++++--------
 1 file changed, 13 insertions(+), 8 deletions(-)

diff --git a/net/ipv4/igmp.c b/net/ipv4/igmp.c
index b65d074d9620..cf75fff170e4 100644
--- a/net/ipv4/igmp.c
+++ b/net/ipv4/igmp.c
@@ -467,7 +467,8 @@ static struct sk_buff *add_grec(struct sk_buff *skb, struct ip_mc_list *pmc,
 
 	if (pmc->multiaddr == IGMP_ALL_HOSTS)
 		return skb;
-	if (ipv4_is_local_multicast(pmc->multiaddr) && !net->ipv4.sysctl_igmp_llm_reports)
+	if (ipv4_is_local_multicast(pmc->multiaddr) &&
+	    !READ_ONCE(net->ipv4.sysctl_igmp_llm_reports))
 		return skb;
 
 	mtu = READ_ONCE(dev->mtu);
@@ -593,7 +594,7 @@ static int igmpv3_send_report(struct in_device *in_dev, struct ip_mc_list *pmc)
 			if (pmc->multiaddr == IGMP_ALL_HOSTS)
 				continue;
 			if (ipv4_is_local_multicast(pmc->multiaddr) &&
-			     !net->ipv4.sysctl_igmp_llm_reports)
+			    !READ_ONCE(net->ipv4.sysctl_igmp_llm_reports))
 				continue;
 			spin_lock_bh(&pmc->lock);
 			if (pmc->sfcount[MCAST_EXCLUDE])
@@ -736,7 +737,8 @@ static int igmp_send_report(struct in_device *in_dev, struct ip_mc_list *pmc,
 	if (type == IGMPV3_HOST_MEMBERSHIP_REPORT)
 		return igmpv3_send_report(in_dev, pmc);
 
-	if (ipv4_is_local_multicast(group) && !net->ipv4.sysctl_igmp_llm_reports)
+	if (ipv4_is_local_multicast(group) &&
+	    !READ_ONCE(net->ipv4.sysctl_igmp_llm_reports))
 		return 0;
 
 	if (type == IGMP_HOST_LEAVE_MESSAGE)
@@ -920,7 +922,8 @@ static bool igmp_heard_report(struct in_device *in_dev, __be32 group)
 
 	if (group == IGMP_ALL_HOSTS)
 		return false;
-	if (ipv4_is_local_multicast(group) && !net->ipv4.sysctl_igmp_llm_reports)
+	if (ipv4_is_local_multicast(group) &&
+	    !READ_ONCE(net->ipv4.sysctl_igmp_llm_reports))
 		return false;
 
 	rcu_read_lock();
@@ -1045,7 +1048,7 @@ static bool igmp_heard_query(struct in_device *in_dev, struct sk_buff *skb,
 		if (im->multiaddr == IGMP_ALL_HOSTS)
 			continue;
 		if (ipv4_is_local_multicast(im->multiaddr) &&
-		    !net->ipv4.sysctl_igmp_llm_reports)
+		    !READ_ONCE(net->ipv4.sysctl_igmp_llm_reports))
 			continue;
 		spin_lock_bh(&im->lock);
 		if (im->tm_running)
@@ -1296,7 +1299,8 @@ static void __igmp_group_dropped(struct ip_mc_list *im, gfp_t gfp)
 #ifdef CONFIG_IP_MULTICAST
 	if (im->multiaddr == IGMP_ALL_HOSTS)
 		return;
-	if (ipv4_is_local_multicast(im->multiaddr) && !net->ipv4.sysctl_igmp_llm_reports)
+	if (ipv4_is_local_multicast(im->multiaddr) &&
+	    !READ_ONCE(net->ipv4.sysctl_igmp_llm_reports))
 		return;
 
 	reporter = im->reporter;
@@ -1338,7 +1342,8 @@ static void igmp_group_added(struct ip_mc_list *im)
 #ifdef CONFIG_IP_MULTICAST
 	if (im->multiaddr == IGMP_ALL_HOSTS)
 		return;
-	if (ipv4_is_local_multicast(im->multiaddr) && !net->ipv4.sysctl_igmp_llm_reports)
+	if (ipv4_is_local_multicast(im->multiaddr) &&
+	    !READ_ONCE(net->ipv4.sysctl_igmp_llm_reports))
 		return;
 
 	if (in_dev->dead)
@@ -1642,7 +1647,7 @@ static void ip_mc_rejoin_groups(struct in_device *in_dev)
 		if (im->multiaddr == IGMP_ALL_HOSTS)
 			continue;
 		if (ipv4_is_local_multicast(im->multiaddr) &&
-		    !net->ipv4.sysctl_igmp_llm_reports)
+		    !READ_ONCE(net->ipv4.sysctl_igmp_llm_reports))
 			continue;
 
 		/* a failover is happening and switches
-- 
2.30.2


^ permalink raw reply related	[flat|nested] 18+ messages in thread

* [PATCH v1 net 02/15] igmp: Fix a data-race around sysctl_igmp_max_memberships.
  2022-07-15 17:17 [PATCH v1 net 00/15] sysctl: Fix data-races around ipv4_net_table (Round 3) Kuniyuki Iwashima
  2022-07-15 17:17 ` [PATCH v1 net 01/15] igmp: Fix data-races around sysctl_igmp_llm_reports Kuniyuki Iwashima
@ 2022-07-15 17:17 ` Kuniyuki Iwashima
  2022-07-15 17:17 ` [PATCH v1 net 03/15] igmp: Fix data-races around sysctl_igmp_max_msf Kuniyuki Iwashima
                   ` (13 subsequent siblings)
  15 siblings, 0 replies; 18+ messages in thread
From: Kuniyuki Iwashima @ 2022-07-15 17:17 UTC (permalink / raw)
  To: David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni, David Ahern
  Cc: Kuniyuki Iwashima, Kuniyuki Iwashima, netdev

While reading sysctl_igmp_max_memberships, it can be changed concurrently.
Thus, we need to add READ_ONCE() to its reader.

Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
Signed-off-by: Kuniyuki Iwashima <kuniyu@amazon.com>
---
 net/ipv4/igmp.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/net/ipv4/igmp.c b/net/ipv4/igmp.c
index cf75fff170e4..792ea1b56b9e 100644
--- a/net/ipv4/igmp.c
+++ b/net/ipv4/igmp.c
@@ -2197,7 +2197,7 @@ static int __ip_mc_join_group(struct sock *sk, struct ip_mreqn *imr,
 		count++;
 	}
 	err = -ENOBUFS;
-	if (count >= net->ipv4.sysctl_igmp_max_memberships)
+	if (count >= READ_ONCE(net->ipv4.sysctl_igmp_max_memberships))
 		goto done;
 	iml = sock_kmalloc(sk, sizeof(*iml), GFP_KERNEL);
 	if (!iml)
-- 
2.30.2


^ permalink raw reply related	[flat|nested] 18+ messages in thread

* [PATCH v1 net 03/15] igmp: Fix data-races around sysctl_igmp_max_msf.
  2022-07-15 17:17 [PATCH v1 net 00/15] sysctl: Fix data-races around ipv4_net_table (Round 3) Kuniyuki Iwashima
  2022-07-15 17:17 ` [PATCH v1 net 01/15] igmp: Fix data-races around sysctl_igmp_llm_reports Kuniyuki Iwashima
  2022-07-15 17:17 ` [PATCH v1 net 02/15] igmp: Fix a data-race around sysctl_igmp_max_memberships Kuniyuki Iwashima
@ 2022-07-15 17:17 ` Kuniyuki Iwashima
  2022-07-15 17:17 ` [PATCH v1 net 04/15] igmp: Fix data-races around sysctl_igmp_qrv Kuniyuki Iwashima
                   ` (12 subsequent siblings)
  15 siblings, 0 replies; 18+ messages in thread
From: Kuniyuki Iwashima @ 2022-07-15 17:17 UTC (permalink / raw)
  To: David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni, David Ahern
  Cc: Kuniyuki Iwashima, Kuniyuki Iwashima, netdev

While reading sysctl_igmp_max_msf, it can be changed concurrently.
Thus, we need to add READ_ONCE() to its readers.

Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
Signed-off-by: Kuniyuki Iwashima <kuniyu@amazon.com>
---
 net/ipv4/igmp.c        | 2 +-
 net/ipv4/ip_sockglue.c | 6 +++---
 2 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/net/ipv4/igmp.c b/net/ipv4/igmp.c
index 792ea1b56b9e..cd7839db34da 100644
--- a/net/ipv4/igmp.c
+++ b/net/ipv4/igmp.c
@@ -2384,7 +2384,7 @@ int ip_mc_source(int add, int omode, struct sock *sk, struct
 	}
 	/* else, add a new source to the filter */
 
-	if (psl && psl->sl_count >= net->ipv4.sysctl_igmp_max_msf) {
+	if (psl && psl->sl_count >= READ_ONCE(net->ipv4.sysctl_igmp_max_msf)) {
 		err = -ENOBUFS;
 		goto done;
 	}
diff --git a/net/ipv4/ip_sockglue.c b/net/ipv4/ip_sockglue.c
index d497d525dea3..a8a323ecbb54 100644
--- a/net/ipv4/ip_sockglue.c
+++ b/net/ipv4/ip_sockglue.c
@@ -782,7 +782,7 @@ static int ip_set_mcast_msfilter(struct sock *sk, sockptr_t optval, int optlen)
 	/* numsrc >= (4G-140)/128 overflow in 32 bits */
 	err = -ENOBUFS;
 	if (gsf->gf_numsrc >= 0x1ffffff ||
-	    gsf->gf_numsrc > sock_net(sk)->ipv4.sysctl_igmp_max_msf)
+	    gsf->gf_numsrc > READ_ONCE(sock_net(sk)->ipv4.sysctl_igmp_max_msf))
 		goto out_free_gsf;
 
 	err = -EINVAL;
@@ -832,7 +832,7 @@ static int compat_ip_set_mcast_msfilter(struct sock *sk, sockptr_t optval,
 
 	/* numsrc >= (4G-140)/128 overflow in 32 bits */
 	err = -ENOBUFS;
-	if (n > sock_net(sk)->ipv4.sysctl_igmp_max_msf)
+	if (n > READ_ONCE(sock_net(sk)->ipv4.sysctl_igmp_max_msf))
 		goto out_free_gsf;
 	err = set_mcast_msfilter(sk, gf32->gf_interface, n, gf32->gf_fmode,
 				 &gf32->gf_group, gf32->gf_slist_flex);
@@ -1244,7 +1244,7 @@ static int do_ip_setsockopt(struct sock *sk, int level, int optname,
 		}
 		/* numsrc >= (1G-4) overflow in 32 bits */
 		if (msf->imsf_numsrc >= 0x3ffffffcU ||
-		    msf->imsf_numsrc > net->ipv4.sysctl_igmp_max_msf) {
+		    msf->imsf_numsrc > READ_ONCE(net->ipv4.sysctl_igmp_max_msf)) {
 			kfree(msf);
 			err = -ENOBUFS;
 			break;
-- 
2.30.2


^ permalink raw reply related	[flat|nested] 18+ messages in thread

* [PATCH v1 net 04/15] igmp: Fix data-races around sysctl_igmp_qrv.
  2022-07-15 17:17 [PATCH v1 net 00/15] sysctl: Fix data-races around ipv4_net_table (Round 3) Kuniyuki Iwashima
                   ` (2 preceding siblings ...)
  2022-07-15 17:17 ` [PATCH v1 net 03/15] igmp: Fix data-races around sysctl_igmp_max_msf Kuniyuki Iwashima
@ 2022-07-15 17:17 ` Kuniyuki Iwashima
  2022-07-15 17:17 ` [PATCH v1 net 05/15] tcp: Fix data-races around keepalive sysctl knobs Kuniyuki Iwashima
                   ` (11 subsequent siblings)
  15 siblings, 0 replies; 18+ messages in thread
From: Kuniyuki Iwashima @ 2022-07-15 17:17 UTC (permalink / raw)
  To: David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni, David Ahern
  Cc: Kuniyuki Iwashima, Kuniyuki Iwashima, netdev, Hannes Frederic Sowa

While reading sysctl_igmp_qrv, it can be changed concurrently.
Thus, we need to add READ_ONCE() to its readers.

This test can be packed into a helper, so such changes will be in the
follow-up series after net is merged into net-next.

  qrv ?: READ_ONCE(net->ipv4.sysctl_igmp_qrv);

Fixes: a9fe8e29945d ("ipv4: implement igmp_qrv sysctl to tune igmp robustness variable")
Signed-off-by: Kuniyuki Iwashima <kuniyu@amazon.com>
---
CC: Hannes Frederic Sowa <hannes@stressinduktion.org>
---
 drivers/net/amt.c |  4 ++--
 net/ipv4/igmp.c   | 24 +++++++++++++-----------
 2 files changed, 15 insertions(+), 13 deletions(-)

diff --git a/drivers/net/amt.c b/drivers/net/amt.c
index be2719a3ba70..89563d1b2a3b 100644
--- a/drivers/net/amt.c
+++ b/drivers/net/amt.c
@@ -563,7 +563,7 @@ static struct sk_buff *amt_build_igmp_gq(struct amt_dev *amt)
 	ihv3->nsrcs	= 0;
 	ihv3->resv	= 0;
 	ihv3->suppress	= false;
-	ihv3->qrv	= amt->net->ipv4.sysctl_igmp_qrv;
+	ihv3->qrv	= READ_ONCE(amt->net->ipv4.sysctl_igmp_qrv);
 	ihv3->csum	= 0;
 	csum		= &ihv3->csum;
 	csum_start	= (void *)ihv3;
@@ -3095,7 +3095,7 @@ static int amt_newlink(struct net *net, struct net_device *dev,
 		goto err;
 	}
 	if (amt->mode == AMT_MODE_RELAY) {
-		amt->qrv = amt->net->ipv4.sysctl_igmp_qrv;
+		amt->qrv = READ_ONCE(amt->net->ipv4.sysctl_igmp_qrv);
 		amt->qri = 10;
 		dev->needed_headroom = amt->stream_dev->needed_headroom +
 				       AMT_RELAY_HLEN;
diff --git a/net/ipv4/igmp.c b/net/ipv4/igmp.c
index cd7839db34da..e3ab0cb61624 100644
--- a/net/ipv4/igmp.c
+++ b/net/ipv4/igmp.c
@@ -827,7 +827,7 @@ static void igmp_ifc_event(struct in_device *in_dev)
 	struct net *net = dev_net(in_dev->dev);
 	if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev))
 		return;
-	WRITE_ONCE(in_dev->mr_ifc_count, in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv);
+	WRITE_ONCE(in_dev->mr_ifc_count, in_dev->mr_qrv ?: READ_ONCE(net->ipv4.sysctl_igmp_qrv));
 	igmp_ifc_start_timer(in_dev, 1);
 }
 
@@ -1009,7 +1009,7 @@ static bool igmp_heard_query(struct in_device *in_dev, struct sk_buff *skb,
 		 * received value was zero, use the default or statically
 		 * configured value.
 		 */
-		in_dev->mr_qrv = ih3->qrv ?: net->ipv4.sysctl_igmp_qrv;
+		in_dev->mr_qrv = ih3->qrv ?: READ_ONCE(net->ipv4.sysctl_igmp_qrv);
 		in_dev->mr_qi = IGMPV3_QQIC(ih3->qqic)*HZ ?: IGMP_QUERY_INTERVAL;
 
 		/* RFC3376, 8.3. Query Response Interval:
@@ -1189,7 +1189,7 @@ static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im,
 	pmc->interface = im->interface;
 	in_dev_hold(in_dev);
 	pmc->multiaddr = im->multiaddr;
-	pmc->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
+	pmc->crcount = in_dev->mr_qrv ?: READ_ONCE(net->ipv4.sysctl_igmp_qrv);
 	pmc->sfmode = im->sfmode;
 	if (pmc->sfmode == MCAST_INCLUDE) {
 		struct ip_sf_list *psf;
@@ -1240,9 +1240,11 @@ static void igmpv3_del_delrec(struct in_device *in_dev, struct ip_mc_list *im)
 			swap(im->tomb, pmc->tomb);
 			swap(im->sources, pmc->sources);
 			for (psf = im->sources; psf; psf = psf->sf_next)
-				psf->sf_crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
+				psf->sf_crcount = in_dev->mr_qrv ?:
+					READ_ONCE(net->ipv4.sysctl_igmp_qrv);
 		} else {
-			im->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
+			im->crcount = in_dev->mr_qrv ?:
+				READ_ONCE(net->ipv4.sysctl_igmp_qrv);
 		}
 		in_dev_put(pmc->interface);
 		kfree_pmc(pmc);
@@ -1349,7 +1351,7 @@ static void igmp_group_added(struct ip_mc_list *im)
 	if (in_dev->dead)
 		return;
 
-	im->unsolicit_count = net->ipv4.sysctl_igmp_qrv;
+	im->unsolicit_count = READ_ONCE(net->ipv4.sysctl_igmp_qrv);
 	if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev)) {
 		spin_lock_bh(&im->lock);
 		igmp_start_timer(im, IGMP_INITIAL_REPORT_DELAY);
@@ -1363,7 +1365,7 @@ static void igmp_group_added(struct ip_mc_list *im)
 	 * IN() to IN(A).
 	 */
 	if (im->sfmode == MCAST_EXCLUDE)
-		im->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
+		im->crcount = in_dev->mr_qrv ?: READ_ONCE(net->ipv4.sysctl_igmp_qrv);
 
 	igmp_ifc_event(in_dev);
 #endif
@@ -1754,7 +1756,7 @@ static void ip_mc_reset(struct in_device *in_dev)
 
 	in_dev->mr_qi = IGMP_QUERY_INTERVAL;
 	in_dev->mr_qri = IGMP_QUERY_RESPONSE_INTERVAL;
-	in_dev->mr_qrv = net->ipv4.sysctl_igmp_qrv;
+	in_dev->mr_qrv = READ_ONCE(net->ipv4.sysctl_igmp_qrv);
 }
 #else
 static void ip_mc_reset(struct in_device *in_dev)
@@ -1888,7 +1890,7 @@ static int ip_mc_del1_src(struct ip_mc_list *pmc, int sfmode,
 #ifdef CONFIG_IP_MULTICAST
 		if (psf->sf_oldin &&
 		    !IGMP_V1_SEEN(in_dev) && !IGMP_V2_SEEN(in_dev)) {
-			psf->sf_crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
+			psf->sf_crcount = in_dev->mr_qrv ?: READ_ONCE(net->ipv4.sysctl_igmp_qrv);
 			psf->sf_next = pmc->tomb;
 			pmc->tomb = psf;
 			rv = 1;
@@ -1952,7 +1954,7 @@ static int ip_mc_del_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
 		/* filter mode change */
 		pmc->sfmode = MCAST_INCLUDE;
 #ifdef CONFIG_IP_MULTICAST
-		pmc->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
+		pmc->crcount = in_dev->mr_qrv ?: READ_ONCE(net->ipv4.sysctl_igmp_qrv);
 		WRITE_ONCE(in_dev->mr_ifc_count, pmc->crcount);
 		for (psf = pmc->sources; psf; psf = psf->sf_next)
 			psf->sf_crcount = 0;
@@ -2131,7 +2133,7 @@ static int ip_mc_add_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
 #ifdef CONFIG_IP_MULTICAST
 		/* else no filters; keep old mode for reports */
 
-		pmc->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
+		pmc->crcount = in_dev->mr_qrv ?: READ_ONCE(net->ipv4.sysctl_igmp_qrv);
 		WRITE_ONCE(in_dev->mr_ifc_count, pmc->crcount);
 		for (psf = pmc->sources; psf; psf = psf->sf_next)
 			psf->sf_crcount = 0;
-- 
2.30.2


^ permalink raw reply related	[flat|nested] 18+ messages in thread

* [PATCH v1 net 05/15] tcp: Fix data-races around keepalive sysctl knobs.
  2022-07-15 17:17 [PATCH v1 net 00/15] sysctl: Fix data-races around ipv4_net_table (Round 3) Kuniyuki Iwashima
                   ` (3 preceding siblings ...)
  2022-07-15 17:17 ` [PATCH v1 net 04/15] igmp: Fix data-races around sysctl_igmp_qrv Kuniyuki Iwashima
@ 2022-07-15 17:17 ` Kuniyuki Iwashima
  2022-07-15 17:17 ` [PATCH v1 net 06/15] tcp: Fix data-races around sysctl_tcp_syn(ack)?_retries Kuniyuki Iwashima
                   ` (10 subsequent siblings)
  15 siblings, 0 replies; 18+ messages in thread
From: Kuniyuki Iwashima @ 2022-07-15 17:17 UTC (permalink / raw)
  To: David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni, David Ahern
  Cc: Kuniyuki Iwashima, Kuniyuki Iwashima, netdev

While reading sysctl_tcp_keepalive_(time|probes|intvl), they can be changed
concurrently.  Thus, we need to add READ_ONCE() to their readers.

Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
Signed-off-by: Kuniyuki Iwashima <kuniyu@amazon.com>
---
 include/net/tcp.h | 9 ++++++---
 net/smc/smc_llc.c | 2 +-
 2 files changed, 7 insertions(+), 4 deletions(-)

diff --git a/include/net/tcp.h b/include/net/tcp.h
index 1e99f5c61f84..7e5a3da4682e 100644
--- a/include/net/tcp.h
+++ b/include/net/tcp.h
@@ -1493,21 +1493,24 @@ static inline int keepalive_intvl_when(const struct tcp_sock *tp)
 {
 	struct net *net = sock_net((struct sock *)tp);
 
-	return tp->keepalive_intvl ? : net->ipv4.sysctl_tcp_keepalive_intvl;
+	return tp->keepalive_intvl ? :
+		READ_ONCE(net->ipv4.sysctl_tcp_keepalive_intvl);
 }
 
 static inline int keepalive_time_when(const struct tcp_sock *tp)
 {
 	struct net *net = sock_net((struct sock *)tp);
 
-	return tp->keepalive_time ? : net->ipv4.sysctl_tcp_keepalive_time;
+	return tp->keepalive_time ? :
+		READ_ONCE(net->ipv4.sysctl_tcp_keepalive_time);
 }
 
 static inline int keepalive_probes(const struct tcp_sock *tp)
 {
 	struct net *net = sock_net((struct sock *)tp);
 
-	return tp->keepalive_probes ? : net->ipv4.sysctl_tcp_keepalive_probes;
+	return tp->keepalive_probes ? :
+		READ_ONCE(net->ipv4.sysctl_tcp_keepalive_probes);
 }
 
 static inline u32 keepalive_time_elapsed(const struct tcp_sock *tp)
diff --git a/net/smc/smc_llc.c b/net/smc/smc_llc.c
index c4d057b2941d..0bde36b56472 100644
--- a/net/smc/smc_llc.c
+++ b/net/smc/smc_llc.c
@@ -2122,7 +2122,7 @@ void smc_llc_lgr_init(struct smc_link_group *lgr, struct smc_sock *smc)
 	init_waitqueue_head(&lgr->llc_flow_waiter);
 	init_waitqueue_head(&lgr->llc_msg_waiter);
 	mutex_init(&lgr->llc_conf_mutex);
-	lgr->llc_testlink_time = net->ipv4.sysctl_tcp_keepalive_time;
+	lgr->llc_testlink_time = READ_ONCE(net->ipv4.sysctl_tcp_keepalive_time);
 }
 
 /* called after lgr was removed from lgr_list */
-- 
2.30.2


^ permalink raw reply related	[flat|nested] 18+ messages in thread

* [PATCH v1 net 06/15] tcp: Fix data-races around sysctl_tcp_syn(ack)?_retries.
  2022-07-15 17:17 [PATCH v1 net 00/15] sysctl: Fix data-races around ipv4_net_table (Round 3) Kuniyuki Iwashima
                   ` (4 preceding siblings ...)
  2022-07-15 17:17 ` [PATCH v1 net 05/15] tcp: Fix data-races around keepalive sysctl knobs Kuniyuki Iwashima
@ 2022-07-15 17:17 ` Kuniyuki Iwashima
  2022-07-15 17:17 ` [PATCH v1 net 07/15] tcp: Fix data-races around sysctl_tcp_syncookies Kuniyuki Iwashima
                   ` (9 subsequent siblings)
  15 siblings, 0 replies; 18+ messages in thread
From: Kuniyuki Iwashima @ 2022-07-15 17:17 UTC (permalink / raw)
  To: David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni, David Ahern
  Cc: Kuniyuki Iwashima, Kuniyuki Iwashima, netdev

While reading sysctl_tcp_syn(ack)?_retries, they can be changed
concurrently.  Thus, we need to add READ_ONCE() to their readers.

Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
Signed-off-by: Kuniyuki Iwashima <kuniyu@amazon.com>
---
 net/ipv4/inet_connection_sock.c |  3 ++-
 net/ipv4/tcp.c                  |  3 ++-
 net/ipv4/tcp_timer.c            | 10 +++++++---
 3 files changed, 11 insertions(+), 5 deletions(-)

diff --git a/net/ipv4/inet_connection_sock.c b/net/ipv4/inet_connection_sock.c
index 2c44556af452..eb31c7158b39 100644
--- a/net/ipv4/inet_connection_sock.c
+++ b/net/ipv4/inet_connection_sock.c
@@ -833,7 +833,8 @@ static void reqsk_timer_handler(struct timer_list *t)
 
 	icsk = inet_csk(sk_listener);
 	net = sock_net(sk_listener);
-	max_syn_ack_retries = icsk->icsk_syn_retries ? : net->ipv4.sysctl_tcp_synack_retries;
+	max_syn_ack_retries = icsk->icsk_syn_retries ? :
+		READ_ONCE(net->ipv4.sysctl_tcp_synack_retries);
 	/* Normally all the openreqs are young and become mature
 	 * (i.e. converted to established socket) for first timeout.
 	 * If synack was not acknowledged for 1 second, it means
diff --git a/net/ipv4/tcp.c b/net/ipv4/tcp.c
index 2222dfdde316..19ce08c9fbdc 100644
--- a/net/ipv4/tcp.c
+++ b/net/ipv4/tcp.c
@@ -3967,7 +3967,8 @@ static int do_tcp_getsockopt(struct sock *sk, int level,
 		val = keepalive_probes(tp);
 		break;
 	case TCP_SYNCNT:
-		val = icsk->icsk_syn_retries ? : net->ipv4.sysctl_tcp_syn_retries;
+		val = icsk->icsk_syn_retries ? :
+			READ_ONCE(net->ipv4.sysctl_tcp_syn_retries);
 		break;
 	case TCP_LINGER2:
 		val = tp->linger2;
diff --git a/net/ipv4/tcp_timer.c b/net/ipv4/tcp_timer.c
index 4f3b9ab222b6..a234704e8163 100644
--- a/net/ipv4/tcp_timer.c
+++ b/net/ipv4/tcp_timer.c
@@ -239,7 +239,8 @@ static int tcp_write_timeout(struct sock *sk)
 	if ((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV)) {
 		if (icsk->icsk_retransmits)
 			__dst_negative_advice(sk);
-		retry_until = icsk->icsk_syn_retries ? : net->ipv4.sysctl_tcp_syn_retries;
+		retry_until = icsk->icsk_syn_retries ? :
+			READ_ONCE(net->ipv4.sysctl_tcp_syn_retries);
 		expired = icsk->icsk_retransmits >= retry_until;
 	} else {
 		if (retransmits_timed_out(sk, net->ipv4.sysctl_tcp_retries1, 0)) {
@@ -406,12 +407,15 @@ abort:		tcp_write_err(sk);
 static void tcp_fastopen_synack_timer(struct sock *sk, struct request_sock *req)
 {
 	struct inet_connection_sock *icsk = inet_csk(sk);
-	int max_retries = icsk->icsk_syn_retries ? :
-	    sock_net(sk)->ipv4.sysctl_tcp_synack_retries + 1; /* add one more retry for fastopen */
 	struct tcp_sock *tp = tcp_sk(sk);
+	int max_retries;
 
 	req->rsk_ops->syn_ack_timeout(req);
 
+	/* add one more retry for fastopen */
+	max_retries = icsk->icsk_syn_retries ? :
+		READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_synack_retries) + 1;
+
 	if (req->num_timeout >= max_retries) {
 		tcp_write_err(sk);
 		return;
-- 
2.30.2


^ permalink raw reply related	[flat|nested] 18+ messages in thread

* [PATCH v1 net 07/15] tcp: Fix data-races around sysctl_tcp_syncookies.
  2022-07-15 17:17 [PATCH v1 net 00/15] sysctl: Fix data-races around ipv4_net_table (Round 3) Kuniyuki Iwashima
                   ` (5 preceding siblings ...)
  2022-07-15 17:17 ` [PATCH v1 net 06/15] tcp: Fix data-races around sysctl_tcp_syn(ack)?_retries Kuniyuki Iwashima
@ 2022-07-15 17:17 ` Kuniyuki Iwashima
  2022-07-15 17:17 ` [PATCH v1 net 08/15] tcp: Fix data-races around sysctl_tcp_migrate_req Kuniyuki Iwashima
                   ` (8 subsequent siblings)
  15 siblings, 0 replies; 18+ messages in thread
From: Kuniyuki Iwashima @ 2022-07-15 17:17 UTC (permalink / raw)
  To: David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni, David Ahern
  Cc: Kuniyuki Iwashima, Kuniyuki Iwashima, netdev

While reading sysctl_tcp_syncookies, it can be changed concurrently.
Thus, we need to add READ_ONCE() to its readers.

Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
Signed-off-by: Kuniyuki Iwashima <kuniyu@amazon.com>
---
 net/core/filter.c     |  4 ++--
 net/ipv4/syncookies.c |  3 ++-
 net/ipv4/tcp_input.c  | 20 ++++++++++++--------
 net/ipv6/syncookies.c |  3 ++-
 4 files changed, 18 insertions(+), 12 deletions(-)

diff --git a/net/core/filter.c b/net/core/filter.c
index 2a6a0b0ce43e..7950f7520765 100644
--- a/net/core/filter.c
+++ b/net/core/filter.c
@@ -7041,7 +7041,7 @@ BPF_CALL_5(bpf_tcp_check_syncookie, struct sock *, sk, void *, iph, u32, iph_len
 	if (sk->sk_protocol != IPPROTO_TCP || sk->sk_state != TCP_LISTEN)
 		return -EINVAL;
 
-	if (!sock_net(sk)->ipv4.sysctl_tcp_syncookies)
+	if (!READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_syncookies))
 		return -EINVAL;
 
 	if (!th->ack || th->rst || th->syn)
@@ -7116,7 +7116,7 @@ BPF_CALL_5(bpf_tcp_gen_syncookie, struct sock *, sk, void *, iph, u32, iph_len,
 	if (sk->sk_protocol != IPPROTO_TCP || sk->sk_state != TCP_LISTEN)
 		return -EINVAL;
 
-	if (!sock_net(sk)->ipv4.sysctl_tcp_syncookies)
+	if (!READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_syncookies))
 		return -ENOENT;
 
 	if (!th->syn || th->ack || th->fin || th->rst)
diff --git a/net/ipv4/syncookies.c b/net/ipv4/syncookies.c
index b387c4835155..9b234b42021e 100644
--- a/net/ipv4/syncookies.c
+++ b/net/ipv4/syncookies.c
@@ -340,7 +340,8 @@ struct sock *cookie_v4_check(struct sock *sk, struct sk_buff *skb)
 	struct flowi4 fl4;
 	u32 tsoff = 0;
 
-	if (!sock_net(sk)->ipv4.sysctl_tcp_syncookies || !th->ack || th->rst)
+	if (!READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_syncookies) ||
+	    !th->ack || th->rst)
 		goto out;
 
 	if (tcp_synq_no_recent_overflow(sk))
diff --git a/net/ipv4/tcp_input.c b/net/ipv4/tcp_input.c
index 3ec4edc37313..8271eaad887b 100644
--- a/net/ipv4/tcp_input.c
+++ b/net/ipv4/tcp_input.c
@@ -6797,11 +6797,14 @@ static bool tcp_syn_flood_action(const struct sock *sk, const char *proto)
 {
 	struct request_sock_queue *queue = &inet_csk(sk)->icsk_accept_queue;
 	const char *msg = "Dropping request";
-	bool want_cookie = false;
 	struct net *net = sock_net(sk);
+	bool want_cookie = false;
+	u8 syncookies;
+
+	syncookies = READ_ONCE(net->ipv4.sysctl_tcp_syncookies);
 
 #ifdef CONFIG_SYN_COOKIES
-	if (net->ipv4.sysctl_tcp_syncookies) {
+	if (syncookies) {
 		msg = "Sending cookies";
 		want_cookie = true;
 		__NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPREQQFULLDOCOOKIES);
@@ -6809,8 +6812,7 @@ static bool tcp_syn_flood_action(const struct sock *sk, const char *proto)
 #endif
 		__NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPREQQFULLDROP);
 
-	if (!queue->synflood_warned &&
-	    net->ipv4.sysctl_tcp_syncookies != 2 &&
+	if (!queue->synflood_warned && syncookies != 2 &&
 	    xchg(&queue->synflood_warned, 1) == 0)
 		net_info_ratelimited("%s: Possible SYN flooding on port %d. %s.  Check SNMP counters.\n",
 				     proto, sk->sk_num, msg);
@@ -6859,7 +6861,7 @@ u16 tcp_get_syncookie_mss(struct request_sock_ops *rsk_ops,
 	struct tcp_sock *tp = tcp_sk(sk);
 	u16 mss;
 
-	if (sock_net(sk)->ipv4.sysctl_tcp_syncookies != 2 &&
+	if (READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_syncookies) != 2 &&
 	    !inet_csk_reqsk_queue_is_full(sk))
 		return 0;
 
@@ -6893,13 +6895,15 @@ int tcp_conn_request(struct request_sock_ops *rsk_ops,
 	bool want_cookie = false;
 	struct dst_entry *dst;
 	struct flowi fl;
+	u8 syncookies;
+
+	syncookies = READ_ONCE(net->ipv4.sysctl_tcp_syncookies);
 
 	/* TW buckets are converted to open requests without
 	 * limitations, they conserve resources and peer is
 	 * evidently real one.
 	 */
-	if ((net->ipv4.sysctl_tcp_syncookies == 2 ||
-	     inet_csk_reqsk_queue_is_full(sk)) && !isn) {
+	if ((syncookies == 2 || inet_csk_reqsk_queue_is_full(sk)) && !isn) {
 		want_cookie = tcp_syn_flood_action(sk, rsk_ops->slab_name);
 		if (!want_cookie)
 			goto drop;
@@ -6949,7 +6953,7 @@ int tcp_conn_request(struct request_sock_ops *rsk_ops,
 
 	if (!want_cookie && !isn) {
 		/* Kill the following clause, if you dislike this way. */
-		if (!net->ipv4.sysctl_tcp_syncookies &&
+		if (!syncookies &&
 		    (net->ipv4.sysctl_max_syn_backlog - inet_csk_reqsk_queue_len(sk) <
 		     (net->ipv4.sysctl_max_syn_backlog >> 2)) &&
 		    !tcp_peer_is_proven(req, dst)) {
diff --git a/net/ipv6/syncookies.c b/net/ipv6/syncookies.c
index 9cc123f000fb..5014aa663452 100644
--- a/net/ipv6/syncookies.c
+++ b/net/ipv6/syncookies.c
@@ -141,7 +141,8 @@ struct sock *cookie_v6_check(struct sock *sk, struct sk_buff *skb)
 	__u8 rcv_wscale;
 	u32 tsoff = 0;
 
-	if (!sock_net(sk)->ipv4.sysctl_tcp_syncookies || !th->ack || th->rst)
+	if (!READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_syncookies) ||
+	    !th->ack || th->rst)
 		goto out;
 
 	if (tcp_synq_no_recent_overflow(sk))
-- 
2.30.2


^ permalink raw reply related	[flat|nested] 18+ messages in thread

* [PATCH v1 net 08/15] tcp: Fix data-races around sysctl_tcp_migrate_req.
  2022-07-15 17:17 [PATCH v1 net 00/15] sysctl: Fix data-races around ipv4_net_table (Round 3) Kuniyuki Iwashima
                   ` (6 preceding siblings ...)
  2022-07-15 17:17 ` [PATCH v1 net 07/15] tcp: Fix data-races around sysctl_tcp_syncookies Kuniyuki Iwashima
@ 2022-07-15 17:17 ` Kuniyuki Iwashima
  2022-07-15 17:17 ` [PATCH v1 net 09/15] tcp: Fix data-races around sysctl_tcp_reordering Kuniyuki Iwashima
                   ` (7 subsequent siblings)
  15 siblings, 0 replies; 18+ messages in thread
From: Kuniyuki Iwashima @ 2022-07-15 17:17 UTC (permalink / raw)
  To: David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni, David Ahern
  Cc: Kuniyuki Iwashima, Kuniyuki Iwashima, netdev

While reading sysctl_tcp_migrate_req, it can be changed concurrently.
Thus, we need to add READ_ONCE() to its readers.

Fixes: f9ac779f881c ("net: Introduce net.ipv4.tcp_migrate_req.")
Signed-off-by: Kuniyuki Iwashima <kuniyu@amazon.com>
---
 net/core/sock_reuseport.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/net/core/sock_reuseport.c b/net/core/sock_reuseport.c
index 3f00a28fe762..5daa1fa54249 100644
--- a/net/core/sock_reuseport.c
+++ b/net/core/sock_reuseport.c
@@ -387,7 +387,7 @@ void reuseport_stop_listen_sock(struct sock *sk)
 		prog = rcu_dereference_protected(reuse->prog,
 						 lockdep_is_held(&reuseport_lock));
 
-		if (sock_net(sk)->ipv4.sysctl_tcp_migrate_req ||
+		if (READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_migrate_req) ||
 		    (prog && prog->expected_attach_type == BPF_SK_REUSEPORT_SELECT_OR_MIGRATE)) {
 			/* Migration capable, move sk from the listening section
 			 * to the closed section.
@@ -545,7 +545,7 @@ struct sock *reuseport_migrate_sock(struct sock *sk,
 	hash = migrating_sk->sk_hash;
 	prog = rcu_dereference(reuse->prog);
 	if (!prog || prog->expected_attach_type != BPF_SK_REUSEPORT_SELECT_OR_MIGRATE) {
-		if (sock_net(sk)->ipv4.sysctl_tcp_migrate_req)
+		if (READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_migrate_req))
 			goto select_by_hash;
 		goto failure;
 	}
-- 
2.30.2


^ permalink raw reply related	[flat|nested] 18+ messages in thread

* [PATCH v1 net 09/15] tcp: Fix data-races around sysctl_tcp_reordering.
  2022-07-15 17:17 [PATCH v1 net 00/15] sysctl: Fix data-races around ipv4_net_table (Round 3) Kuniyuki Iwashima
                   ` (7 preceding siblings ...)
  2022-07-15 17:17 ` [PATCH v1 net 08/15] tcp: Fix data-races around sysctl_tcp_migrate_req Kuniyuki Iwashima
@ 2022-07-15 17:17 ` Kuniyuki Iwashima
  2022-07-15 17:17 ` [PATCH v1 net 10/15] tcp: Fix data-races around some timeout sysctl knobs Kuniyuki Iwashima
                   ` (6 subsequent siblings)
  15 siblings, 0 replies; 18+ messages in thread
From: Kuniyuki Iwashima @ 2022-07-15 17:17 UTC (permalink / raw)
  To: David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni, David Ahern
  Cc: Kuniyuki Iwashima, Kuniyuki Iwashima, netdev

While reading sysctl_tcp_reordering, it can be changed concurrently.
Thus, we need to add READ_ONCE() to its readers.

Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
Signed-off-by: Kuniyuki Iwashima <kuniyu@amazon.com>
---
 net/ipv4/tcp.c         |  2 +-
 net/ipv4/tcp_input.c   | 10 +++++++---
 net/ipv4/tcp_metrics.c |  3 ++-
 3 files changed, 10 insertions(+), 5 deletions(-)

diff --git a/net/ipv4/tcp.c b/net/ipv4/tcp.c
index 19ce08c9fbdc..b3632fa5df07 100644
--- a/net/ipv4/tcp.c
+++ b/net/ipv4/tcp.c
@@ -441,7 +441,7 @@ void tcp_init_sock(struct sock *sk)
 	tp->snd_cwnd_clamp = ~0;
 	tp->mss_cache = TCP_MSS_DEFAULT;
 
-	tp->reordering = sock_net(sk)->ipv4.sysctl_tcp_reordering;
+	tp->reordering = READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_reordering);
 	tcp_assign_congestion_control(sk);
 
 	tp->tsoffset = 0;
diff --git a/net/ipv4/tcp_input.c b/net/ipv4/tcp_input.c
index 8271eaad887b..de4ccd173c7f 100644
--- a/net/ipv4/tcp_input.c
+++ b/net/ipv4/tcp_input.c
@@ -2139,6 +2139,7 @@ void tcp_enter_loss(struct sock *sk)
 	struct tcp_sock *tp = tcp_sk(sk);
 	struct net *net = sock_net(sk);
 	bool new_recovery = icsk->icsk_ca_state < TCP_CA_Recovery;
+	u8 reordering;
 
 	tcp_timeout_mark_lost(sk);
 
@@ -2159,10 +2160,12 @@ void tcp_enter_loss(struct sock *sk)
 	/* Timeout in disordered state after receiving substantial DUPACKs
 	 * suggests that the degree of reordering is over-estimated.
 	 */
+	reordering = READ_ONCE(net->ipv4.sysctl_tcp_reordering);
 	if (icsk->icsk_ca_state <= TCP_CA_Disorder &&
-	    tp->sacked_out >= net->ipv4.sysctl_tcp_reordering)
+	    tp->sacked_out >= reordering)
 		tp->reordering = min_t(unsigned int, tp->reordering,
-				       net->ipv4.sysctl_tcp_reordering);
+				       reordering);
+
 	tcp_set_ca_state(sk, TCP_CA_Loss);
 	tp->high_seq = tp->snd_nxt;
 	tcp_ecn_queue_cwr(tp);
@@ -3464,7 +3467,8 @@ static inline bool tcp_may_raise_cwnd(const struct sock *sk, const int flag)
 	 * new SACK or ECE mark may first advance cwnd here and later reduce
 	 * cwnd in tcp_fastretrans_alert() based on more states.
 	 */
-	if (tcp_sk(sk)->reordering > sock_net(sk)->ipv4.sysctl_tcp_reordering)
+	if (tcp_sk(sk)->reordering >
+	    READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_reordering))
 		return flag & FLAG_FORWARD_PROGRESS;
 
 	return flag & FLAG_DATA_ACKED;
diff --git a/net/ipv4/tcp_metrics.c b/net/ipv4/tcp_metrics.c
index 7029b0e98edb..a501150deaa3 100644
--- a/net/ipv4/tcp_metrics.c
+++ b/net/ipv4/tcp_metrics.c
@@ -428,7 +428,8 @@ void tcp_update_metrics(struct sock *sk)
 		if (!tcp_metric_locked(tm, TCP_METRIC_REORDERING)) {
 			val = tcp_metric_get(tm, TCP_METRIC_REORDERING);
 			if (val < tp->reordering &&
-			    tp->reordering != net->ipv4.sysctl_tcp_reordering)
+			    tp->reordering !=
+			    READ_ONCE(net->ipv4.sysctl_tcp_reordering))
 				tcp_metric_set(tm, TCP_METRIC_REORDERING,
 					       tp->reordering);
 		}
-- 
2.30.2


^ permalink raw reply related	[flat|nested] 18+ messages in thread

* [PATCH v1 net 10/15] tcp: Fix data-races around some timeout sysctl knobs.
  2022-07-15 17:17 [PATCH v1 net 00/15] sysctl: Fix data-races around ipv4_net_table (Round 3) Kuniyuki Iwashima
                   ` (8 preceding siblings ...)
  2022-07-15 17:17 ` [PATCH v1 net 09/15] tcp: Fix data-races around sysctl_tcp_reordering Kuniyuki Iwashima
@ 2022-07-15 17:17 ` Kuniyuki Iwashima
  2022-07-15 17:17 ` [PATCH v1 net 11/15] tcp: Fix a data-race around sysctl_tcp_notsent_lowat Kuniyuki Iwashima
                   ` (5 subsequent siblings)
  15 siblings, 0 replies; 18+ messages in thread
From: Kuniyuki Iwashima @ 2022-07-15 17:17 UTC (permalink / raw)
  To: David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni, David Ahern
  Cc: Kuniyuki Iwashima, Kuniyuki Iwashima, netdev

While reading these sysctl knobs, they can be changed concurrently.
Thus, we need to add READ_ONCE() to their readers.

  - tcp_retries1
  - tcp_retries2
  - tcp_orphan_retries
  - tcp_fin_timeout

Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
Signed-off-by: Kuniyuki Iwashima <kuniyu@amazon.com>
---
 include/net/tcp.h     |  3 ++-
 net/ipv4/tcp.c        |  2 +-
 net/ipv4/tcp_output.c |  2 +-
 net/ipv4/tcp_timer.c  | 10 +++++-----
 4 files changed, 9 insertions(+), 8 deletions(-)

diff --git a/include/net/tcp.h b/include/net/tcp.h
index 7e5a3da4682e..b78f1d60be33 100644
--- a/include/net/tcp.h
+++ b/include/net/tcp.h
@@ -1523,7 +1523,8 @@ static inline u32 keepalive_time_elapsed(const struct tcp_sock *tp)
 
 static inline int tcp_fin_time(const struct sock *sk)
 {
-	int fin_timeout = tcp_sk(sk)->linger2 ? : sock_net(sk)->ipv4.sysctl_tcp_fin_timeout;
+	int fin_timeout = tcp_sk(sk)->linger2 ? :
+		READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_fin_timeout);
 	const int rto = inet_csk(sk)->icsk_rto;
 
 	if (fin_timeout < (rto << 2) - (rto >> 1))
diff --git a/net/ipv4/tcp.c b/net/ipv4/tcp.c
index b3632fa5df07..b1b1bcbc4f60 100644
--- a/net/ipv4/tcp.c
+++ b/net/ipv4/tcp.c
@@ -3973,7 +3973,7 @@ static int do_tcp_getsockopt(struct sock *sk, int level,
 	case TCP_LINGER2:
 		val = tp->linger2;
 		if (val >= 0)
-			val = (val ? : net->ipv4.sysctl_tcp_fin_timeout) / HZ;
+			val = (val ? : READ_ONCE(net->ipv4.sysctl_tcp_fin_timeout)) / HZ;
 		break;
 	case TCP_DEFER_ACCEPT:
 		val = retrans_to_secs(icsk->icsk_accept_queue.rskq_defer_accept,
diff --git a/net/ipv4/tcp_output.c b/net/ipv4/tcp_output.c
index 02ab3a9c6657..3b3552d292a5 100644
--- a/net/ipv4/tcp_output.c
+++ b/net/ipv4/tcp_output.c
@@ -4090,7 +4090,7 @@ void tcp_send_probe0(struct sock *sk)
 
 	icsk->icsk_probes_out++;
 	if (err <= 0) {
-		if (icsk->icsk_backoff < net->ipv4.sysctl_tcp_retries2)
+		if (icsk->icsk_backoff < READ_ONCE(net->ipv4.sysctl_tcp_retries2))
 			icsk->icsk_backoff++;
 		timeout = tcp_probe0_when(sk, TCP_RTO_MAX);
 	} else {
diff --git a/net/ipv4/tcp_timer.c b/net/ipv4/tcp_timer.c
index a234704e8163..ec5277becc6a 100644
--- a/net/ipv4/tcp_timer.c
+++ b/net/ipv4/tcp_timer.c
@@ -143,7 +143,7 @@ static int tcp_out_of_resources(struct sock *sk, bool do_reset)
  */
 static int tcp_orphan_retries(struct sock *sk, bool alive)
 {
-	int retries = sock_net(sk)->ipv4.sysctl_tcp_orphan_retries; /* May be zero. */
+	int retries = READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_orphan_retries); /* May be zero. */
 
 	/* We know from an ICMP that something is wrong. */
 	if (sk->sk_err_soft && !alive)
@@ -243,14 +243,14 @@ static int tcp_write_timeout(struct sock *sk)
 			READ_ONCE(net->ipv4.sysctl_tcp_syn_retries);
 		expired = icsk->icsk_retransmits >= retry_until;
 	} else {
-		if (retransmits_timed_out(sk, net->ipv4.sysctl_tcp_retries1, 0)) {
+		if (retransmits_timed_out(sk, READ_ONCE(net->ipv4.sysctl_tcp_retries1), 0)) {
 			/* Black hole detection */
 			tcp_mtu_probing(icsk, sk);
 
 			__dst_negative_advice(sk);
 		}
 
-		retry_until = net->ipv4.sysctl_tcp_retries2;
+		retry_until = READ_ONCE(net->ipv4.sysctl_tcp_retries2);
 		if (sock_flag(sk, SOCK_DEAD)) {
 			const bool alive = icsk->icsk_rto < TCP_RTO_MAX;
 
@@ -381,7 +381,7 @@ static void tcp_probe_timer(struct sock *sk)
 		 msecs_to_jiffies(icsk->icsk_user_timeout))
 		goto abort;
 
-	max_probes = sock_net(sk)->ipv4.sysctl_tcp_retries2;
+	max_probes = READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_retries2);
 	if (sock_flag(sk, SOCK_DEAD)) {
 		const bool alive = inet_csk_rto_backoff(icsk, TCP_RTO_MAX) < TCP_RTO_MAX;
 
@@ -589,7 +589,7 @@ void tcp_retransmit_timer(struct sock *sk)
 	}
 	inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS,
 				  tcp_clamp_rto_to_user_timeout(sk), TCP_RTO_MAX);
-	if (retransmits_timed_out(sk, net->ipv4.sysctl_tcp_retries1 + 1, 0))
+	if (retransmits_timed_out(sk, READ_ONCE(net->ipv4.sysctl_tcp_retries1) + 1, 0))
 		__sk_dst_reset(sk);
 
 out:;
-- 
2.30.2


^ permalink raw reply related	[flat|nested] 18+ messages in thread

* [PATCH v1 net 11/15] tcp: Fix a data-race around sysctl_tcp_notsent_lowat.
  2022-07-15 17:17 [PATCH v1 net 00/15] sysctl: Fix data-races around ipv4_net_table (Round 3) Kuniyuki Iwashima
                   ` (9 preceding siblings ...)
  2022-07-15 17:17 ` [PATCH v1 net 10/15] tcp: Fix data-races around some timeout sysctl knobs Kuniyuki Iwashima
@ 2022-07-15 17:17 ` Kuniyuki Iwashima
  2022-07-15 17:17 ` [PATCH v1 net 12/15] tcp: Fix a data-race around sysctl_tcp_tw_reuse Kuniyuki Iwashima
                   ` (4 subsequent siblings)
  15 siblings, 0 replies; 18+ messages in thread
From: Kuniyuki Iwashima @ 2022-07-15 17:17 UTC (permalink / raw)
  To: David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni, David Ahern
  Cc: Kuniyuki Iwashima, Kuniyuki Iwashima, netdev

While reading sysctl_tcp_notsent_lowat, it can be changed concurrently.
Thus, we need to add READ_ONCE() to its reader.

Fixes: c9bee3b7fdec ("tcp: TCP_NOTSENT_LOWAT socket option")
Signed-off-by: Kuniyuki Iwashima <kuniyu@amazon.com>
---
 include/net/tcp.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/include/net/tcp.h b/include/net/tcp.h
index b78f1d60be33..504077ae5da9 100644
--- a/include/net/tcp.h
+++ b/include/net/tcp.h
@@ -2027,7 +2027,7 @@ void __tcp_v4_send_check(struct sk_buff *skb, __be32 saddr, __be32 daddr);
 static inline u32 tcp_notsent_lowat(const struct tcp_sock *tp)
 {
 	struct net *net = sock_net((struct sock *)tp);
-	return tp->notsent_lowat ?: net->ipv4.sysctl_tcp_notsent_lowat;
+	return tp->notsent_lowat ?: READ_ONCE(net->ipv4.sysctl_tcp_notsent_lowat);
 }
 
 bool tcp_stream_memory_free(const struct sock *sk, int wake);
-- 
2.30.2


^ permalink raw reply related	[flat|nested] 18+ messages in thread

* [PATCH v1 net 12/15] tcp: Fix a data-race around sysctl_tcp_tw_reuse.
  2022-07-15 17:17 [PATCH v1 net 00/15] sysctl: Fix data-races around ipv4_net_table (Round 3) Kuniyuki Iwashima
                   ` (10 preceding siblings ...)
  2022-07-15 17:17 ` [PATCH v1 net 11/15] tcp: Fix a data-race around sysctl_tcp_notsent_lowat Kuniyuki Iwashima
@ 2022-07-15 17:17 ` Kuniyuki Iwashima
  2022-07-15 17:17 ` [PATCH v1 net 13/15] tcp: Fix data-races around sysctl_max_syn_backlog Kuniyuki Iwashima
                   ` (3 subsequent siblings)
  15 siblings, 0 replies; 18+ messages in thread
From: Kuniyuki Iwashima @ 2022-07-15 17:17 UTC (permalink / raw)
  To: David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni, David Ahern
  Cc: Kuniyuki Iwashima, Kuniyuki Iwashima, netdev

While reading sysctl_tcp_tw_reuse, it can be changed concurrently.
Thus, we need to add READ_ONCE() to its reader.

Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
Signed-off-by: Kuniyuki Iwashima <kuniyu@amazon.com>
---
 net/ipv4/tcp_ipv4.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/net/ipv4/tcp_ipv4.c b/net/ipv4/tcp_ipv4.c
index da5a3c44c4fb..d16e6e40f47b 100644
--- a/net/ipv4/tcp_ipv4.c
+++ b/net/ipv4/tcp_ipv4.c
@@ -108,10 +108,10 @@ static u32 tcp_v4_init_ts_off(const struct net *net, const struct sk_buff *skb)
 
 int tcp_twsk_unique(struct sock *sk, struct sock *sktw, void *twp)
 {
+	int reuse = READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_tw_reuse);
 	const struct inet_timewait_sock *tw = inet_twsk(sktw);
 	const struct tcp_timewait_sock *tcptw = tcp_twsk(sktw);
 	struct tcp_sock *tp = tcp_sk(sk);
-	int reuse = sock_net(sk)->ipv4.sysctl_tcp_tw_reuse;
 
 	if (reuse == 2) {
 		/* Still does not detect *everything* that goes through
-- 
2.30.2


^ permalink raw reply related	[flat|nested] 18+ messages in thread

* [PATCH v1 net 13/15] tcp: Fix data-races around sysctl_max_syn_backlog.
  2022-07-15 17:17 [PATCH v1 net 00/15] sysctl: Fix data-races around ipv4_net_table (Round 3) Kuniyuki Iwashima
                   ` (11 preceding siblings ...)
  2022-07-15 17:17 ` [PATCH v1 net 12/15] tcp: Fix a data-race around sysctl_tcp_tw_reuse Kuniyuki Iwashima
@ 2022-07-15 17:17 ` Kuniyuki Iwashima
  2022-07-15 17:17 ` [PATCH v1 net 14/15] tcp: Fix data-races around sysctl_tcp_fastopen Kuniyuki Iwashima
                   ` (2 subsequent siblings)
  15 siblings, 0 replies; 18+ messages in thread
From: Kuniyuki Iwashima @ 2022-07-15 17:17 UTC (permalink / raw)
  To: David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni, David Ahern
  Cc: Kuniyuki Iwashima, Kuniyuki Iwashima, netdev

While reading sysctl_max_syn_backlog, it can be changed concurrently.
Thus, we need to add READ_ONCE() to its readers.

Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
Signed-off-by: Kuniyuki Iwashima <kuniyu@amazon.com>
---
 net/ipv4/tcp_input.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/net/ipv4/tcp_input.c b/net/ipv4/tcp_input.c
index de4ccd173c7f..d451248bebec 100644
--- a/net/ipv4/tcp_input.c
+++ b/net/ipv4/tcp_input.c
@@ -6956,10 +6956,12 @@ int tcp_conn_request(struct request_sock_ops *rsk_ops,
 		tcp_rsk(req)->ts_off = af_ops->init_ts_off(net, skb);
 
 	if (!want_cookie && !isn) {
+		int max_syn_backlog = READ_ONCE(net->ipv4.sysctl_max_syn_backlog);
+
 		/* Kill the following clause, if you dislike this way. */
 		if (!syncookies &&
-		    (net->ipv4.sysctl_max_syn_backlog - inet_csk_reqsk_queue_len(sk) <
-		     (net->ipv4.sysctl_max_syn_backlog >> 2)) &&
+		    (max_syn_backlog - inet_csk_reqsk_queue_len(sk) <
+		     (max_syn_backlog >> 2)) &&
 		    !tcp_peer_is_proven(req, dst)) {
 			/* Without syncookies last quarter of
 			 * backlog is filled with destinations,
-- 
2.30.2


^ permalink raw reply related	[flat|nested] 18+ messages in thread

* [PATCH v1 net 14/15] tcp: Fix data-races around sysctl_tcp_fastopen.
  2022-07-15 17:17 [PATCH v1 net 00/15] sysctl: Fix data-races around ipv4_net_table (Round 3) Kuniyuki Iwashima
                   ` (12 preceding siblings ...)
  2022-07-15 17:17 ` [PATCH v1 net 13/15] tcp: Fix data-races around sysctl_max_syn_backlog Kuniyuki Iwashima
@ 2022-07-15 17:17 ` Kuniyuki Iwashima
  2022-07-17 15:20   ` Yuchung Cheng
  2022-07-15 17:17 ` [PATCH v1 net 15/15] tcp: Fix data-races around sysctl_tcp_fastopen_blackhole_timeout Kuniyuki Iwashima
  2022-07-18 11:50 ` [PATCH v1 net 00/15] sysctl: Fix data-races around ipv4_net_table (Round 3) patchwork-bot+netdevbpf
  15 siblings, 1 reply; 18+ messages in thread
From: Kuniyuki Iwashima @ 2022-07-15 17:17 UTC (permalink / raw)
  To: David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni, David Ahern
  Cc: Kuniyuki Iwashima, Kuniyuki Iwashima, netdev, Yuchung Cheng

While reading sysctl_tcp_fastopen, it can be changed concurrently.
Thus, we need to add READ_ONCE() to its readers.

Fixes: 2100c8d2d9db ("net-tcp: Fast Open base")
Signed-off-by: Kuniyuki Iwashima <kuniyu@amazon.com>
---
CC: Yuchung Cheng <ycheng@google.com>
---
 net/ipv4/af_inet.c      | 2 +-
 net/ipv4/tcp.c          | 6 ++++--
 net/ipv4/tcp_fastopen.c | 4 ++--
 3 files changed, 7 insertions(+), 5 deletions(-)

diff --git a/net/ipv4/af_inet.c b/net/ipv4/af_inet.c
index 4bc24f9e38b3..59a0c5406fc1 100644
--- a/net/ipv4/af_inet.c
+++ b/net/ipv4/af_inet.c
@@ -217,7 +217,7 @@ int inet_listen(struct socket *sock, int backlog)
 		 * because the socket was in TCP_LISTEN state previously but
 		 * was shutdown() rather than close().
 		 */
-		tcp_fastopen = sock_net(sk)->ipv4.sysctl_tcp_fastopen;
+		tcp_fastopen = READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_fastopen);
 		if ((tcp_fastopen & TFO_SERVER_WO_SOCKOPT1) &&
 		    (tcp_fastopen & TFO_SERVER_ENABLE) &&
 		    !inet_csk(sk)->icsk_accept_queue.fastopenq.max_qlen) {
diff --git a/net/ipv4/tcp.c b/net/ipv4/tcp.c
index b1b1bcbc4f60..2faaaaf540ac 100644
--- a/net/ipv4/tcp.c
+++ b/net/ipv4/tcp.c
@@ -1150,7 +1150,8 @@ static int tcp_sendmsg_fastopen(struct sock *sk, struct msghdr *msg,
 	struct sockaddr *uaddr = msg->msg_name;
 	int err, flags;
 
-	if (!(sock_net(sk)->ipv4.sysctl_tcp_fastopen & TFO_CLIENT_ENABLE) ||
+	if (!(READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_fastopen) &
+	      TFO_CLIENT_ENABLE) ||
 	    (uaddr && msg->msg_namelen >= sizeof(uaddr->sa_family) &&
 	     uaddr->sa_family == AF_UNSPEC))
 		return -EOPNOTSUPP;
@@ -3617,7 +3618,8 @@ static int do_tcp_setsockopt(struct sock *sk, int level, int optname,
 	case TCP_FASTOPEN_CONNECT:
 		if (val > 1 || val < 0) {
 			err = -EINVAL;
-		} else if (net->ipv4.sysctl_tcp_fastopen & TFO_CLIENT_ENABLE) {
+		} else if (READ_ONCE(net->ipv4.sysctl_tcp_fastopen) &
+			   TFO_CLIENT_ENABLE) {
 			if (sk->sk_state == TCP_CLOSE)
 				tp->fastopen_connect = val;
 			else
diff --git a/net/ipv4/tcp_fastopen.c b/net/ipv4/tcp_fastopen.c
index fdbcf2a6d08e..0acdb5473850 100644
--- a/net/ipv4/tcp_fastopen.c
+++ b/net/ipv4/tcp_fastopen.c
@@ -332,7 +332,7 @@ static bool tcp_fastopen_no_cookie(const struct sock *sk,
 				   const struct dst_entry *dst,
 				   int flag)
 {
-	return (sock_net(sk)->ipv4.sysctl_tcp_fastopen & flag) ||
+	return (READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_fastopen) & flag) ||
 	       tcp_sk(sk)->fastopen_no_cookie ||
 	       (dst && dst_metric(dst, RTAX_FASTOPEN_NO_COOKIE));
 }
@@ -347,7 +347,7 @@ struct sock *tcp_try_fastopen(struct sock *sk, struct sk_buff *skb,
 			      const struct dst_entry *dst)
 {
 	bool syn_data = TCP_SKB_CB(skb)->end_seq != TCP_SKB_CB(skb)->seq + 1;
-	int tcp_fastopen = sock_net(sk)->ipv4.sysctl_tcp_fastopen;
+	int tcp_fastopen = READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_fastopen);
 	struct tcp_fastopen_cookie valid_foc = { .len = -1 };
 	struct sock *child;
 	int ret = 0;
-- 
2.30.2


^ permalink raw reply related	[flat|nested] 18+ messages in thread

* [PATCH v1 net 15/15] tcp: Fix data-races around sysctl_tcp_fastopen_blackhole_timeout.
  2022-07-15 17:17 [PATCH v1 net 00/15] sysctl: Fix data-races around ipv4_net_table (Round 3) Kuniyuki Iwashima
                   ` (13 preceding siblings ...)
  2022-07-15 17:17 ` [PATCH v1 net 14/15] tcp: Fix data-races around sysctl_tcp_fastopen Kuniyuki Iwashima
@ 2022-07-15 17:17 ` Kuniyuki Iwashima
  2022-07-18 11:50 ` [PATCH v1 net 00/15] sysctl: Fix data-races around ipv4_net_table (Round 3) patchwork-bot+netdevbpf
  15 siblings, 0 replies; 18+ messages in thread
From: Kuniyuki Iwashima @ 2022-07-15 17:17 UTC (permalink / raw)
  To: David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni, David Ahern
  Cc: Kuniyuki Iwashima, Kuniyuki Iwashima, netdev, Wei Wang

While reading sysctl_tcp_fastopen_blackhole_timeout, it can be changed
concurrently.  Thus, we need to add READ_ONCE() to its readers.

Fixes: cf1ef3f0719b ("net/tcp_fastopen: Disable active side TFO in certain scenarios")
Signed-off-by: Kuniyuki Iwashima <kuniyu@amazon.com>
---
CC: Wei Wang <weiwan@google.com>
---
 net/ipv4/tcp_fastopen.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/net/ipv4/tcp_fastopen.c b/net/ipv4/tcp_fastopen.c
index 0acdb5473850..825b216d11f5 100644
--- a/net/ipv4/tcp_fastopen.c
+++ b/net/ipv4/tcp_fastopen.c
@@ -489,7 +489,7 @@ void tcp_fastopen_active_disable(struct sock *sk)
 {
 	struct net *net = sock_net(sk);
 
-	if (!sock_net(sk)->ipv4.sysctl_tcp_fastopen_blackhole_timeout)
+	if (!READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_fastopen_blackhole_timeout))
 		return;
 
 	/* Paired with READ_ONCE() in tcp_fastopen_active_should_disable() */
@@ -510,7 +510,8 @@ void tcp_fastopen_active_disable(struct sock *sk)
  */
 bool tcp_fastopen_active_should_disable(struct sock *sk)
 {
-	unsigned int tfo_bh_timeout = sock_net(sk)->ipv4.sysctl_tcp_fastopen_blackhole_timeout;
+	unsigned int tfo_bh_timeout =
+		READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_fastopen_blackhole_timeout);
 	unsigned long timeout;
 	int tfo_da_times;
 	int multiplier;
-- 
2.30.2


^ permalink raw reply related	[flat|nested] 18+ messages in thread

* Re: [PATCH v1 net 14/15] tcp: Fix data-races around sysctl_tcp_fastopen.
  2022-07-15 17:17 ` [PATCH v1 net 14/15] tcp: Fix data-races around sysctl_tcp_fastopen Kuniyuki Iwashima
@ 2022-07-17 15:20   ` Yuchung Cheng
  0 siblings, 0 replies; 18+ messages in thread
From: Yuchung Cheng @ 2022-07-17 15:20 UTC (permalink / raw)
  To: Kuniyuki Iwashima
  Cc: David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
	David Ahern, Kuniyuki Iwashima, netdev

On Fri, Jul 15, 2022 at 10:21 AM Kuniyuki Iwashima <kuniyu@amazon.com> wrote:
>
> While reading sysctl_tcp_fastopen, it can be changed concurrently.
> Thus, we need to add READ_ONCE() to its readers.
>
> Fixes: 2100c8d2d9db ("net-tcp: Fast Open base")
> Signed-off-by: Kuniyuki Iwashima <kuniyu@amazon.com>
> ---
> CC: Yuchung Cheng <ycheng@google.com>
Thanks for the improvement

Acked-by: Yuchung Cheng <ycheng@google.com>

> ---
>  net/ipv4/af_inet.c      | 2 +-
>  net/ipv4/tcp.c          | 6 ++++--
>  net/ipv4/tcp_fastopen.c | 4 ++--
>  3 files changed, 7 insertions(+), 5 deletions(-)
>
> diff --git a/net/ipv4/af_inet.c b/net/ipv4/af_inet.c
> index 4bc24f9e38b3..59a0c5406fc1 100644
> --- a/net/ipv4/af_inet.c
> +++ b/net/ipv4/af_inet.c
> @@ -217,7 +217,7 @@ int inet_listen(struct socket *sock, int backlog)
>                  * because the socket was in TCP_LISTEN state previously but
>                  * was shutdown() rather than close().
>                  */
> -               tcp_fastopen = sock_net(sk)->ipv4.sysctl_tcp_fastopen;
> +               tcp_fastopen = READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_fastopen);
>                 if ((tcp_fastopen & TFO_SERVER_WO_SOCKOPT1) &&
>                     (tcp_fastopen & TFO_SERVER_ENABLE) &&
>                     !inet_csk(sk)->icsk_accept_queue.fastopenq.max_qlen) {
> diff --git a/net/ipv4/tcp.c b/net/ipv4/tcp.c
> index b1b1bcbc4f60..2faaaaf540ac 100644
> --- a/net/ipv4/tcp.c
> +++ b/net/ipv4/tcp.c
> @@ -1150,7 +1150,8 @@ static int tcp_sendmsg_fastopen(struct sock *sk, struct msghdr *msg,
>         struct sockaddr *uaddr = msg->msg_name;
>         int err, flags;
>
> -       if (!(sock_net(sk)->ipv4.sysctl_tcp_fastopen & TFO_CLIENT_ENABLE) ||
> +       if (!(READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_fastopen) &
> +             TFO_CLIENT_ENABLE) ||
>             (uaddr && msg->msg_namelen >= sizeof(uaddr->sa_family) &&
>              uaddr->sa_family == AF_UNSPEC))
>                 return -EOPNOTSUPP;
> @@ -3617,7 +3618,8 @@ static int do_tcp_setsockopt(struct sock *sk, int level, int optname,
>         case TCP_FASTOPEN_CONNECT:
>                 if (val > 1 || val < 0) {
>                         err = -EINVAL;
> -               } else if (net->ipv4.sysctl_tcp_fastopen & TFO_CLIENT_ENABLE) {
> +               } else if (READ_ONCE(net->ipv4.sysctl_tcp_fastopen) &
> +                          TFO_CLIENT_ENABLE) {
>                         if (sk->sk_state == TCP_CLOSE)
>                                 tp->fastopen_connect = val;
>                         else
> diff --git a/net/ipv4/tcp_fastopen.c b/net/ipv4/tcp_fastopen.c
> index fdbcf2a6d08e..0acdb5473850 100644
> --- a/net/ipv4/tcp_fastopen.c
> +++ b/net/ipv4/tcp_fastopen.c
> @@ -332,7 +332,7 @@ static bool tcp_fastopen_no_cookie(const struct sock *sk,
>                                    const struct dst_entry *dst,
>                                    int flag)
>  {
> -       return (sock_net(sk)->ipv4.sysctl_tcp_fastopen & flag) ||
> +       return (READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_fastopen) & flag) ||
>                tcp_sk(sk)->fastopen_no_cookie ||
>                (dst && dst_metric(dst, RTAX_FASTOPEN_NO_COOKIE));
>  }
> @@ -347,7 +347,7 @@ struct sock *tcp_try_fastopen(struct sock *sk, struct sk_buff *skb,
>                               const struct dst_entry *dst)
>  {
>         bool syn_data = TCP_SKB_CB(skb)->end_seq != TCP_SKB_CB(skb)->seq + 1;
> -       int tcp_fastopen = sock_net(sk)->ipv4.sysctl_tcp_fastopen;
> +       int tcp_fastopen = READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_fastopen);
>         struct tcp_fastopen_cookie valid_foc = { .len = -1 };
>         struct sock *child;
>         int ret = 0;
> --
> 2.30.2
>

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH v1 net 00/15] sysctl: Fix data-races around ipv4_net_table (Round 3).
  2022-07-15 17:17 [PATCH v1 net 00/15] sysctl: Fix data-races around ipv4_net_table (Round 3) Kuniyuki Iwashima
                   ` (14 preceding siblings ...)
  2022-07-15 17:17 ` [PATCH v1 net 15/15] tcp: Fix data-races around sysctl_tcp_fastopen_blackhole_timeout Kuniyuki Iwashima
@ 2022-07-18 11:50 ` patchwork-bot+netdevbpf
  15 siblings, 0 replies; 18+ messages in thread
From: patchwork-bot+netdevbpf @ 2022-07-18 11:50 UTC (permalink / raw)
  To: Kuniyuki Iwashima
  Cc: davem, edumazet, kuba, pabeni, dsahern, kuni1840, netdev

Hello:

This series was applied to netdev/net.git (master)
by David S. Miller <davem@davemloft.net>:

On Fri, 15 Jul 2022 10:17:40 -0700 you wrote:
> This series fixes data-races around 21 knobs after
> igmp_link_local_mcast_reports in ipv4_net_table.
> 
> These 4 knobs are skipped because they are safe.
> 
>   - tcp_congestion_control: Safe with RCU and xchg().
>   - tcp_available_congestion_control: Read only.
>   - tcp_allowed_congestion_control: Safe with RCU and spinlock().
>   - tcp_fastopen_key: Safe with RCU and xchg()
> 
> [...]

Here is the summary with links:
  - [v1,net,01/15] igmp: Fix data-races around sysctl_igmp_llm_reports.
    https://git.kernel.org/netdev/net/c/f6da2267e711
  - [v1,net,02/15] igmp: Fix a data-race around sysctl_igmp_max_memberships.
    https://git.kernel.org/netdev/net/c/6305d821e3b9
  - [v1,net,03/15] igmp: Fix data-races around sysctl_igmp_max_msf.
    https://git.kernel.org/netdev/net/c/6ae0f2e55373
  - [v1,net,04/15] igmp: Fix data-races around sysctl_igmp_qrv.
    https://git.kernel.org/netdev/net/c/8ebcc62c738f
  - [v1,net,05/15] tcp: Fix data-races around keepalive sysctl knobs.
    https://git.kernel.org/netdev/net/c/f2f316e287e6
  - [v1,net,06/15] tcp: Fix data-races around sysctl_tcp_syn(ack)?_retries.
    https://git.kernel.org/netdev/net/c/20a3b1c0f603
  - [v1,net,07/15] tcp: Fix data-races around sysctl_tcp_syncookies.
    https://git.kernel.org/netdev/net/c/f2e383b5bb6b
  - [v1,net,08/15] tcp: Fix data-races around sysctl_tcp_migrate_req.
    https://git.kernel.org/netdev/net/c/4177f545895b
  - [v1,net,09/15] tcp: Fix data-races around sysctl_tcp_reordering.
    https://git.kernel.org/netdev/net/c/46778cd16e6a
  - [v1,net,10/15] tcp: Fix data-races around some timeout sysctl knobs.
    https://git.kernel.org/netdev/net/c/39e24435a776
  - [v1,net,11/15] tcp: Fix a data-race around sysctl_tcp_notsent_lowat.
    https://git.kernel.org/netdev/net/c/55be873695ed
  - [v1,net,12/15] tcp: Fix a data-race around sysctl_tcp_tw_reuse.
    https://git.kernel.org/netdev/net/c/cbfc6495586a
  - [v1,net,13/15] tcp: Fix data-races around sysctl_max_syn_backlog.
    https://git.kernel.org/netdev/net/c/79539f34743d
  - [v1,net,14/15] tcp: Fix data-races around sysctl_tcp_fastopen.
    https://git.kernel.org/netdev/net/c/5a54213318c4
  - [v1,net,15/15] tcp: Fix data-races around sysctl_tcp_fastopen_blackhole_timeout.
    https://git.kernel.org/netdev/net/c/021266ec640c

You are awesome, thank you!
-- 
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html



^ permalink raw reply	[flat|nested] 18+ messages in thread

end of thread, other threads:[~2022-07-18 11:50 UTC | newest]

Thread overview: 18+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-07-15 17:17 [PATCH v1 net 00/15] sysctl: Fix data-races around ipv4_net_table (Round 3) Kuniyuki Iwashima
2022-07-15 17:17 ` [PATCH v1 net 01/15] igmp: Fix data-races around sysctl_igmp_llm_reports Kuniyuki Iwashima
2022-07-15 17:17 ` [PATCH v1 net 02/15] igmp: Fix a data-race around sysctl_igmp_max_memberships Kuniyuki Iwashima
2022-07-15 17:17 ` [PATCH v1 net 03/15] igmp: Fix data-races around sysctl_igmp_max_msf Kuniyuki Iwashima
2022-07-15 17:17 ` [PATCH v1 net 04/15] igmp: Fix data-races around sysctl_igmp_qrv Kuniyuki Iwashima
2022-07-15 17:17 ` [PATCH v1 net 05/15] tcp: Fix data-races around keepalive sysctl knobs Kuniyuki Iwashima
2022-07-15 17:17 ` [PATCH v1 net 06/15] tcp: Fix data-races around sysctl_tcp_syn(ack)?_retries Kuniyuki Iwashima
2022-07-15 17:17 ` [PATCH v1 net 07/15] tcp: Fix data-races around sysctl_tcp_syncookies Kuniyuki Iwashima
2022-07-15 17:17 ` [PATCH v1 net 08/15] tcp: Fix data-races around sysctl_tcp_migrate_req Kuniyuki Iwashima
2022-07-15 17:17 ` [PATCH v1 net 09/15] tcp: Fix data-races around sysctl_tcp_reordering Kuniyuki Iwashima
2022-07-15 17:17 ` [PATCH v1 net 10/15] tcp: Fix data-races around some timeout sysctl knobs Kuniyuki Iwashima
2022-07-15 17:17 ` [PATCH v1 net 11/15] tcp: Fix a data-race around sysctl_tcp_notsent_lowat Kuniyuki Iwashima
2022-07-15 17:17 ` [PATCH v1 net 12/15] tcp: Fix a data-race around sysctl_tcp_tw_reuse Kuniyuki Iwashima
2022-07-15 17:17 ` [PATCH v1 net 13/15] tcp: Fix data-races around sysctl_max_syn_backlog Kuniyuki Iwashima
2022-07-15 17:17 ` [PATCH v1 net 14/15] tcp: Fix data-races around sysctl_tcp_fastopen Kuniyuki Iwashima
2022-07-17 15:20   ` Yuchung Cheng
2022-07-15 17:17 ` [PATCH v1 net 15/15] tcp: Fix data-races around sysctl_tcp_fastopen_blackhole_timeout Kuniyuki Iwashima
2022-07-18 11:50 ` [PATCH v1 net 00/15] sysctl: Fix data-races around ipv4_net_table (Round 3) patchwork-bot+netdevbpf

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.