tcp_metrics: four fixes
Subsystems (3)
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
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().
--- net/ipv4/tcp_metrics.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
net/ipv4/tcp_metrics.c +2 -2
@@ -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
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()orWRITE_ONCE().Fixes 04f721c671656 ("tcp_metrics: Rewrite tcp_metrics_flush_all")Signed-off-by Eric Dumazet <[email protected]>--- net/ipv4/tcp_metrics.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
Re: [PATCH net-next 1/4] tcp_metrics: add missing barriers on delete
In reply to Eric DumazetQuoted 7 lines · Eric Dumazet
When removing an item from RCU protected list, we must prevent store-tearing, using
rcu_assign_pointer()orWRITE_ONCE().Fixes 04f721c671656 ("tcp_metrics: Rewrite tcp_metrics_flush_all")Signed-off-by Eric Dumazet <[email protected]>---
neal
PATCH 2/4 tcp_metrics: properly set tp->snd_ssthresh in tcp_init_metrics()
In reply to Eric DumazetWe need to set tp->snd_ssthresh to TCP_INFINITE_SSTHRESH in the case tcp_get_metrics() fails for some reason.
--- net/ipv4/tcp_metrics.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-)
net/ipv4/tcp_metrics.c +4 -5
@@ -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
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_ssthreshtoTCP_INFINITE_SSTHRESHin the casetcp_get_metrics()fails for some reason.Fixes 9ad7c049f0f7 ("tcp: RFC2988bis + taking RTT sample from 3WHS for the passive open side")Signed-off-by Eric Dumazet <[email protected]>--- net/ipv4/tcp_metrics.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-)net/ipv4/tcp_metrics.c +0
-0
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
-0if (!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
-5if (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)
Re: [PATCH net-next 2/4] tcp_metrics: properly set tp->snd_ssthresh in tcp_init_metrics()
In reply to Eric DumazetQuoted 7 lines · Eric Dumazet
We need to set
tp->snd_ssthreshtoTCP_INFINITE_SSTHRESHin the casetcp_get_metrics()fails for some reason.Fixes 9ad7c049f0f7 ("tcp: RFC2988bis + taking RTT sample from 3WHS for the passive open side")Signed-off-by Eric Dumazet <[email protected]>---
neal
PATCH 3/4 tcp_metrics: do not create an entry from tcp_init_metrics()
In reply to Eric Dumazettcp_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.
--- net/ipv4/tcp_metrics.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
net/ipv4/tcp_metrics.c +1 -1
@@ -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
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 whentcp_no_metrics_saveis set.Fixes 51c5d0c4b169 ("tcp: Maintain dynamic metrics in local cache.")Signed-off-by Eric Dumazet <[email protected]>--- net/ipv4/tcp_metrics.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
Re: [PATCH net-next 3/4] tcp_metrics: do not create an entry from tcp_init_metrics()
In reply to Eric DumazetQuoted 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 whentcp_no_metrics_saveis set.Fixes 51c5d0c4b169 ("tcp: Maintain dynamic metrics in local cache.")Signed-off-by Eric Dumazet <[email protected]>---
neal
PATCH 4/4 tcp_metrics: optimize tcp_metrics_flush_all()
In reply to Eric DumazetThis is inspired by several syzbot reports wheretcp_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
@@ -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
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_lockfor empty buckets, and we should add onecond_resched()to break potential long loops.Signed-off-by Eric Dumazet <[email protected]>--- net/ipv4/tcp_metrics.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)net/ipv4/tcp_metrics.c +4
-2@@ -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)?
Paolo Abeni
maintainerRe: [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_lockfor empty buckets, and we should add onecond_resched()to break potential long loops.Signed-off-by Eric Dumazet <[email protected]>--- net/ipv4/tcp_metrics.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)net/ipv4/tcp_metrics.c +1
-1@@ -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 inif (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
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 inif (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.
Re: [PATCH net-next 4/4] tcp_metrics: optimize tcp_metrics_flush_all()
In reply to Eric DumazetQuoted 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_lockfor empty buckets, and we should add onecond_resched()to break potential long loops.Signed-off-by Eric Dumazet <[email protected]>---
neal
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_metricscache on my host was full of useless entries, even though I have/proc/sys/net/ipv4/tcp_no_metrics_saveset 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