Skip to contentSkip to conversation

tcp_metrics: four fixes

PATCH Applied Eric Dumazet 5 participants 16 messages Sep 22, 2023 – Oct 3, 2023

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

cover tcp_metrics: four fixes

Looking at an inconclusive syzbot report, I was surprised to see that tcp_metrics cache on my host was full of useless entries, even though I have /proc/sys/net/ipv4/tcp_no_metrics_save set to 1.

While looking more closely I found a total of four issues.

Eric Dumazet (4):
tcp_metrics: add missing barriers on delete
tcp_metrics: properly set tp->snd_ssthresh in tcp_init_metrics()
tcp_metrics: do not create an entry from tcp_init_metrics()
tcp_metrics: optimize tcp_metrics_flush_all()

 net/ipv4/tcp_metrics.c | 22 ++++++++++++----------
 1 file changed, 12 insertions(+), 10 deletions(-)
Signature · 1 line
2.42.0.515.g380fc7ccd1-goog
#1 lore raw

PATCH 1/4 tcp_metrics: add missing barriers on delete

When removing an item from RCU protected list, we must prevent store-tearing, using rcu_assign_pointer() or WRITE_ONCE().

Fixes 04f721c671656 ("tcp_metrics: Rewrite tcp_metrics_flush_all")
---
 net/ipv4/tcp_metrics.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
net/ipv4/tcp_metrics.c +2 -2
index c196759f1d3bd8cb16479522e936bd95091f89b2..4bfa2fb27de5481ca3d1300d7e7b2c80d1577a31 100644
@@ -908,7 +908,7 @@ static void tcp_metrics_flush_all(struct net *net)
 			match = net ? net_eq(tm_net(tm), net) :
 				!refcount_read(&tm_net(tm)->ns.count);
 			if (match) {
-				*pp = tm->tcpm_next;
+				rcu_assign_pointer(*pp, tm->tcpm_next);
 				kfree_rcu(tm, rcu_head);
 			} else {
 				pp = &tm->tcpm_next;
@@ -949,7 +949,7 @@ static int tcp_metrics_nl_cmd_del(struct sk_buff *skb, struct genl_info *info)
 		if (addr_same(&tm->tcpm_daddr, &daddr) &&
 		    (!src || addr_same(&tm->tcpm_saddr, &saddr)) &&
 		    net_eq(tm_net(tm), net)) {
-			*pp = tm->tcpm_next;
+			rcu_assign_pointer(*pp, tm->tcpm_next);
 			kfree_rcu(tm, rcu_head);
 			found = true;
 		} else {
Signature · 1 line
2.42.0.515.g380fc7ccd1-goog
Reviewed-by on v1 1/4
#2 lore raw

Re: [PATCH net-next 1/4] tcp_metrics: add missing barriers on delete

Quoted 9 lines · Eric Dumazet

When removing an item from RCU protected list, we must prevent store-tearing, using rcu_assign_pointer() or WRITE_ONCE().

Fixes 04f721c671656 ("tcp_metrics: Rewrite tcp_metrics_flush_all")
---
 net/ipv4/tcp_metrics.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
#4 lore raw

PATCH 2/4 tcp_metrics: properly set tp->snd_ssthresh in tcp_init_metrics()

In reply to Eric Dumazet

We need to set tp->snd_ssthresh to TCP_INFINITE_SSTHRESH in the case tcp_get_metrics() fails for some reason.

Fixes 9ad7c049f0f7 ("tcp: RFC2988bis + taking RTT sample from 3WHS for the passive open side")
---
 net/ipv4/tcp_metrics.c | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)
net/ipv4/tcp_metrics.c +4 -5
index 4bfa2fb27de5481ca3d1300d7e7b2c80d1577a31..0c03f564878ff0a0dbefd9b631f54697346c8fa9 100644
@@ -470,6 +470,10 @@ void tcp_init_metrics(struct sock *sk)
 	u32 val, crtt = 0; /* cached RTT scaled by 8 */
 
 	sk_dst_confirm(sk);
+	/* ssthresh may have been reduced unnecessarily during.
+	 * 3WHS. Restore it back to its initial default.
+	 */
+	tp->snd_ssthresh = TCP_INFINITE_SSTHRESH;
 	if (!dst)
 		goto reset;
 
@@ -489,11 +493,6 @@ void tcp_init_metrics(struct sock *sk)
 		tp->snd_ssthresh = val;
 		if (tp->snd_ssthresh > tp->snd_cwnd_clamp)
 			tp->snd_ssthresh = tp->snd_cwnd_clamp;
-	} else {
-		/* ssthresh may have been reduced unnecessarily during.
-		 * 3WHS. Restore it back to its initial default.
-		 */
-		tp->snd_ssthresh = TCP_INFINITE_SSTHRESH;
 	}
 	val = tcp_metric_get(tm, TCP_METRIC_REORDERING);
 	if (val && tp->reordering != val)
Signature · 1 line
2.42.0.515.g380fc7ccd1-goog
Reviewed-by on v1 2/4
#5 lore raw

Re: [PATCH net-next 2/4] tcp_metrics: properly set tp->snd_ssthresh in tcp_init_metrics()

Quoted 14 lines · Eric Dumazet

We need to set tp->snd_ssthresh to TCP_INFINITE_SSTHRESH in the case tcp_get_metrics() fails for some reason.

Fixes 9ad7c049f0f7 ("tcp: RFC2988bis + taking RTT sample from 3WHS for the passive open side")
---
 net/ipv4/tcp_metrics.c | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)
net/ipv4/tcp_metrics.c +0 -0
index 4bfa2fb27de5481ca3d1300d7e7b2c80d1577a31..0c03f564878ff0a0dbefd9b631f54697346c8fa9 100644

net/ipv4/tcp_metrics.c void tcp_init_metrics(struct sock *sk) · line 476

Quoted 7 lines
net/ipv4/tcp_metrics.c +4 -0
@@ -470,6 +470,10 @@ void tcp_init_metrics(struct sock *sk)
 	u32 val, crtt = 0; /* cached RTT scaled by 8 */
 
 	sk_dst_confirm(sk);
+	/* ssthresh may have been reduced unnecessarily during.
+	 * 3WHS. Restore it back to its initial default.
+	 */
+	tp->snd_ssthresh = TCP_INFINITE_SSTHRESH;

This would read easier with newlines after sk_dst_confirm and again here.

Quoted 5 lines
net/ipv4/tcp_metrics.c +0 -0
 	if (!dst)
 		goto reset;
 
@@ -489,11 +493,6 @@ void tcp_init_metrics(struct sock *sk)
 		tp->snd_ssthresh = val;

net/ipv4/tcp_metrics.c void tcp_init_metrics(struct sock *sk) · line 498

Quoted 10 lines
net/ipv4/tcp_metrics.c +0 -5
 		if (tp->snd_ssthresh > tp->snd_cwnd_clamp)
 			tp->snd_ssthresh = tp->snd_cwnd_clamp;
-	} else {
-		/* ssthresh may have been reduced unnecessarily during.
-		 * 3WHS. Restore it back to its initial default.
-		 */
-		tp->snd_ssthresh = TCP_INFINITE_SSTHRESH;
 	}
 	val = tcp_metric_get(tm, TCP_METRIC_REORDERING);
 	if (val && tp->reordering != val)
maintainer Acked-by on v1 2/4
#6 lore raw

Re: [PATCH net-next 2/4] tcp_metrics: properly set tp->snd_ssthresh in tcp_init_metrics()

In reply to Eric Dumazet
Quoted 7 lines · Eric Dumazet

We need to set tp->snd_ssthresh to TCP_INFINITE_SSTHRESH in the case tcp_get_metrics() fails for some reason.

Fixes 9ad7c049f0f7 ("tcp: RFC2988bis + taking RTT sample from 3WHS for the passive open side")

---

neal

#7 lore raw

PATCH 3/4 tcp_metrics: do not create an entry from tcp_init_metrics()

In reply to Eric Dumazet

tcp_init_metrics() only wants to get metrics if they were previously stored in the cache. Creating an entry is adding useless costs, especially when tcp_no_metrics_save is set.

Fixes 51c5d0c4b169 ("tcp: Maintain dynamic metrics in local cache.")
---
 net/ipv4/tcp_metrics.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/ipv4/tcp_metrics.c +1 -1
index 0c03f564878ff0a0dbefd9b631f54697346c8fa9..7aca12c59c18483f42276d01252ed0fac326e5d8 100644
@@ -478,7 +478,7 @@ void tcp_init_metrics(struct sock *sk)
 		goto reset;
 
 	rcu_read_lock();
-	tm = tcp_get_metrics(sk, dst, true);
+	tm = tcp_get_metrics(sk, dst, false);
 	if (!tm) {
 		rcu_read_unlock();
 		goto reset;
Signature · 1 line
2.42.0.515.g380fc7ccd1-goog
Reviewed-by on v1 3/4
#8 lore raw

Re: [PATCH net-next 3/4] tcp_metrics: do not create an entry from tcp_init_metrics()

Quoted 10 lines · Eric Dumazet

tcp_init_metrics() only wants to get metrics if they were previously stored in the cache. Creating an entry is adding useless costs, especially when tcp_no_metrics_save is set.

Fixes 51c5d0c4b169 ("tcp: Maintain dynamic metrics in local cache.")
---
 net/ipv4/tcp_metrics.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
maintainer Acked-by on v1 3/4
#9 lore raw

Re: [PATCH net-next 3/4] tcp_metrics: do not create an entry from tcp_init_metrics()

In reply to Eric Dumazet
Quoted 8 lines · Eric Dumazet

tcp_init_metrics() only wants to get metrics if they were previously stored in the cache. Creating an entry is adding useless costs, especially when tcp_no_metrics_save is set.

Fixes 51c5d0c4b169 ("tcp: Maintain dynamic metrics in local cache.")

---

neal

#10 lore raw

PATCH 4/4 tcp_metrics: optimize tcp_metrics_flush_all()

In reply to Eric Dumazet

This is inspired by several syzbot reports where
tcp_metrics_flush_all() was seen in the traces.

We can avoid acquiring tcp_metrics_lock for empty buckets, and we should add one cond_resched() to break potential long loops.

---
 net/ipv4/tcp_metrics.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
net/ipv4/tcp_metrics.c +5 -2
index 7aca12c59c18483f42276d01252ed0fac326e5d8..c2a925538542b5d787596b7d76705dda86cf48d8 100644
@@ -898,11 +898,13 @@ static void tcp_metrics_flush_all(struct net *net)
 	unsigned int row;
 
 	for (row = 0; row < max_rows; row++, hb++) {
-		struct tcp_metrics_block __rcu **pp;
+		struct tcp_metrics_block __rcu **pp = &hb->chain;
 		bool match;
 
+		if (!rcu_access_pointer(*pp))
+			continue;
+
 		spin_lock_bh(&tcp_metrics_lock);
-		pp = &hb->chain;
 		for (tm = deref_locked(*pp); tm; tm = deref_locked(*pp)) {
 			match = net ? net_eq(tm_net(tm), net) :
 				!refcount_read(&tm_net(tm)->ns.count);
@@ -914,6 +916,7 @@ static void tcp_metrics_flush_all(struct net *net)
 			}
 		}
 		spin_unlock_bh(&tcp_metrics_lock);
+		cond_resched();
 	}
 }
 
Signature · 1 line
2.42.0.515.g380fc7ccd1-goog
Reviewed-by on v1 4/4
#11 lore raw

Re: [PATCH net-next 4/4] tcp_metrics: optimize tcp_metrics_flush_all()

Quoted 32 lines · Eric Dumazet

This is inspired by several syzbot reports where
tcp_metrics_flush_all() was seen in the traces.

We can avoid acquiring tcp_metrics_lock for empty buckets, and we should add one cond_resched() to break potential long loops.

---
 net/ipv4/tcp_metrics.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
net/ipv4/tcp_metrics.c +4 -2
index 7aca12c59c18483f42276d01252ed0fac326e5d8..c2a925538542b5d787596b7d76705dda86cf48d8 100644
@@ -898,11 +898,13 @@ static void tcp_metrics_flush_all(struct net *net)
 	unsigned int row;
 
 	for (row = 0; row < max_rows; row++, hb++) {
-		struct tcp_metrics_block __rcu **pp;
+		struct tcp_metrics_block __rcu **pp = &hb->chain;
 		bool match;
 
+		if (!rcu_access_pointer(*pp))
+			continue;
+
 		spin_lock_bh(&tcp_metrics_lock);
-		pp = &hb->chain;
 		for (tm = deref_locked(*pp); tm; tm = deref_locked(*pp)) {
 			match = net ? net_eq(tm_net(tm), net) :
 				!refcount_read(&tm_net(tm)->ns.count);

net/ipv4/tcp_metrics.c static void tcp_metrics_flush_all(struct net *net) · line 919

Quoted 4 lines
net/ipv4/tcp_metrics.c +1 -0
@@ -914,6 +916,7 @@ static void tcp_metrics_flush_all(struct net *net)
 			}
 		}
 		spin_unlock_bh(&tcp_metrics_lock);
+		cond_resched();

I have found cond_resched() can occur some unnecessary overhead if called too often. Wrap in if (need_resched)?

maintainer
#12 lore raw

Re: [PATCH net-next 4/4] tcp_metrics: optimize tcp_metrics_flush_all()

Quoted 24 lines · David Ahern
Quoted 23 lines · Eric Dumazet

This is inspired by several syzbot reports where
tcp_metrics_flush_all() was seen in the traces.

We can avoid acquiring tcp_metrics_lock for empty buckets, and we should add one cond_resched() to break potential long loops.

---
 net/ipv4/tcp_metrics.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
net/ipv4/tcp_metrics.c +1 -1
index 7aca12c59c18483f42276d01252ed0fac326e5d8..c2a925538542b5d787596b7d76705dda86cf48d8 100644
@@ -898,11 +898,13 @@ static void tcp_metrics_flush_all(struct net *net)
 	unsigned int row;
 
 	for (row = 0; row < max_rows; row++, hb++) {
-		struct tcp_metrics_block __rcu **pp;
+		struct tcp_metrics_block __rcu **pp = &hb->chain;
 		bool match;
 
Quoted 16 lines

net/ipv4/tcp_metrics.c static void tcp_metrics_flush_all(struct net *net) · line 919

Quoted 13 lines
net/ipv4/tcp_metrics.c +4 -1
+		if (!rcu_access_pointer(*pp))
+			continue;
+
 		spin_lock_bh(&tcp_metrics_lock);
-		pp = &hb->chain;
 		for (tm = deref_locked(*pp); tm; tm = deref_locked(*pp)) {
 			match = net ? net_eq(tm_net(tm), net) :
 				!refcount_read(&tm_net(tm)->ns.count);
@@ -914,6 +916,7 @@ static void tcp_metrics_flush_all(struct net *net)
 			}
 		}
 		spin_unlock_bh(&tcp_metrics_lock);
+		cond_resched();

I have found cond_resched() can occur some unnecessary overhead if called too often. Wrap in if (need_resched)?

Interesting. I could not find any significant overhead with code inspection - it should be a matter of 2 conditionals instead of one - Any idea why?

In any case I think we can follow-up with that if needed - e.g. no changes required here.

Cheers,

Paolo

#13 lore raw

Re: [PATCH net-next 4/4] tcp_metrics: optimize tcp_metrics_flush_all()

Quoted 6 lines · Paolo Abeni
Quoted 2 lines

I have found cond_resched() can occur some unnecessary overhead if called too often. Wrap in if (need_resched)?

Interesting. I could not find any significant overhead with code inspection - it should be a matter of 2 conditionals instead of one - Any idea why?

I would need to go through the ftrace output. I think it is really use case dependent and for this one and the way tcp_metrics_nl_cmd_del is called just cond_resched is ok.

maintainer Acked-by on v1 4/4
#14 lore raw

Re: [PATCH net-next 4/4] tcp_metrics: optimize tcp_metrics_flush_all()

In reply to Eric Dumazet
Quoted 9 lines · Eric Dumazet

This is inspired by several syzbot reports where
tcp_metrics_flush_all() was seen in the traces.

We can avoid acquiring tcp_metrics_lock for empty buckets, and we should add one cond_resched() to break potential long loops.

---

neal

#15 lore raw
In reply to Eric Dumazet
This series was applied to netdev/net-next.git (main) by Paolo Abeni <[email protected]>:

Hello:

This series was applied to netdev/net-next.git (main) by Paolo Abeni <[email protected]>:

Quoted 8 lines · On Fri, 22 Sep 2023 22:03:52 +0000 you wrote:

Looking at an inconclusive syzbot report, I was surprised to see that tcp_metrics cache on my host was full of useless entries, even though I have /proc/sys/net/ipv4/tcp_no_metrics_save set to 1.

While looking more closely I found a total of four issues.

[...]

Here is the summary with links:
- [net-next,1/4] tcp_metrics: add missing barriers on delete

    https://git.kernel.org/netdev/net-next/c/cbc3a1532228
  - [net-next,2/4] tcp_metrics: properly set tp->snd_ssthresh in tcp_init_metrics()
    https://git.kernel.org/netdev/net-next/c/081480014a64
  - [net-next,3/4] tcp_metrics: do not create an entry from tcp_init_metrics()
    https://git.kernel.org/netdev/net-next/c/a135798e6e20
  - [net-next,4/4] tcp_metrics: optimize tcp_metrics_flush_all()
    https://git.kernel.org/netdev/net-next/c/6532e257aa73

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html