diff mbox

[net] tcp: fix wraparound issue in tcp_lp

Message ID 1493677788.31837.25.camel@edumazet-glaptop3.roam.corp.google.com
State Accepted, archived
Delegated to: David Miller
Headers show

Commit Message

Eric Dumazet May 1, 2017, 10:29 p.m. UTC
From: Eric Dumazet <edumazet@google.com>

Be careful when comparing tcp_time_stamp to some u32 quantity,
otherwise result can be surprising.

Fixes: 7c106d7e782b ("[TCP]: TCP Low Priority congestion control")
Signed-off-by: Eric Dumazet <edumazet@google.com>
---
 net/ipv4/tcp_lp.c |    6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

Comments

Stephen Hemminger May 1, 2017, 11:56 p.m. UTC | #1
On Mon, 01 May 2017 15:29:48 -0700
Eric Dumazet <eric.dumazet@gmail.com> wrote:

> From: Eric Dumazet <edumazet@google.com>
> 
> Be careful when comparing tcp_time_stamp to some u32 quantity,
> otherwise result can be surprising.
> 
> Fixes: 7c106d7e782b ("[TCP]: TCP Low Priority congestion control")
> Signed-off-by: Eric Dumazet <edumazet@google.com>
> ---
>  net/ipv4/tcp_lp.c |    6 ++++--
>  1 file changed, 4 insertions(+), 2 deletions(-)
> 
> diff --git a/net/ipv4/tcp_lp.c b/net/ipv4/tcp_lp.c
> index 046fd3910873306d74207615d6997e1c847ea361..d6fb6c067af4641f232b94e7c590c212648e8173 100644
> --- a/net/ipv4/tcp_lp.c
> +++ b/net/ipv4/tcp_lp.c
> @@ -264,13 +264,15 @@ static void tcp_lp_pkts_acked(struct sock *sk, const struct ack_sample *sample)
>  {
>  	struct tcp_sock *tp = tcp_sk(sk);
>  	struct lp *lp = inet_csk_ca(sk);
> +	u32 delta;
>  
>  	if (sample->rtt_us > 0)
>  		tcp_lp_rtt_sample(sk, sample->rtt_us);
>  
>  	/* calc inference */
> -	if (tcp_time_stamp > tp->rx_opt.rcv_tsecr)
> -		lp->inference = 3 * (tcp_time_stamp - tp->rx_opt.rcv_tsecr);
> +	delta = tcp_time_stamp - tp->rx_opt.rcv_tsecr;
> +	if ((s32)delta > 0)
> +		lp->inference = 3 * delta;

Agreed time wraparound would cause problems.
But why not use existing time_after() macro here?
Neal Cardwell May 2, 2017, 12:31 a.m. UTC | #2
On Mon, May 1, 2017 at 7:56 PM, Stephen Hemminger
<stephen@networkplumber.org> wrote:
> On Mon, 01 May 2017 15:29:48 -0700
> Agreed time wraparound would cause problems.
> But why not use existing time_after() macro here?
>

I suspect this is because time_after() asserts that it is being used
on unsigned long (64 bits), and tcp_time_stamp is 32 bits.

I suppose for tcp_time_stamp comparisons we could re-use the u32 TCP
sequence macros for before() and after()? Even the comment for
before()/after() is already generic enough to apply to tcp_time_stamp:
"The next routines deal with comparing 32 bit unsigned ints and worry
about wraparound (automatic with unsigned arithmetic)." That might be
nice.

neal
Eric Dumazet May 2, 2017, 1:04 a.m. UTC | #3
On Mon, 2017-05-01 at 16:56 -0700, Stephen Hemminger wrote:
> On Mon, 01 May 2017 15:29:48 -0700
> Eric Dumazet <eric.dumazet@gmail.com> wrote:
> 
> > From: Eric Dumazet <edumazet@google.com>
> > 
> > Be careful when comparing tcp_time_stamp to some u32 quantity,
> > otherwise result can be surprising.
> > 
> > Fixes: 7c106d7e782b ("[TCP]: TCP Low Priority congestion control")
> > Signed-off-by: Eric Dumazet <edumazet@google.com>
> > ---
> >  net/ipv4/tcp_lp.c |    6 ++++--
> >  1 file changed, 4 insertions(+), 2 deletions(-)
> > 
> > diff --git a/net/ipv4/tcp_lp.c b/net/ipv4/tcp_lp.c
> > index 046fd3910873306d74207615d6997e1c847ea361..d6fb6c067af4641f232b94e7c590c212648e8173 100644
> > --- a/net/ipv4/tcp_lp.c
> > +++ b/net/ipv4/tcp_lp.c
> > @@ -264,13 +264,15 @@ static void tcp_lp_pkts_acked(struct sock *sk, const struct ack_sample *sample)
> >  {
> >  	struct tcp_sock *tp = tcp_sk(sk);
> >  	struct lp *lp = inet_csk_ca(sk);
> > +	u32 delta;
> >  
> >  	if (sample->rtt_us > 0)
> >  		tcp_lp_rtt_sample(sk, sample->rtt_us);
> >  
> >  	/* calc inference */
> > -	if (tcp_time_stamp > tp->rx_opt.rcv_tsecr)
> > -		lp->inference = 3 * (tcp_time_stamp - tp->rx_opt.rcv_tsecr);
> > +	delta = tcp_time_stamp - tp->rx_opt.rcv_tsecr;
> > +	if ((s32)delta > 0)
> > +		lp->inference = 3 * delta;
> 
> Agreed time wraparound would cause problems.
> But why not use existing time_after() macro here?
> 

Simply to not perform (tcp_time_stamp - tp->rx_opt.rcv_tsecr) twice.

jiffies being volatile, this can not be optimized by the compiler.

I have a patch series (for linux-4.13) that will switch TCP stack to 1ms
TS options, regardless of CONFIG_HZ value, and when cooking it I found
this bug.
Eric Dumazet May 2, 2017, 1:58 a.m. UTC | #4
On Mon, 2017-05-01 at 18:04 -0700, Eric Dumazet wrote:

> 
> Simply to not perform (tcp_time_stamp - tp->rx_opt.rcv_tsecr) twice.
> 
> jiffies being volatile, this can not be optimized by the compiler.
> 
> I have a patch series (for linux-4.13) that will switch TCP stack to 1ms
> TS options, regardless of CONFIG_HZ value, and when cooking it I found
> this bug.

I forgot to say that after this upcoming patch series, tcp_time_stamp
will become a more expensive function, no longer a plain (u32)jiffies.
David Miller May 2, 2017, 7:07 p.m. UTC | #5
From: Eric Dumazet <eric.dumazet@gmail.com>
Date: Mon, 01 May 2017 15:29:48 -0700

> From: Eric Dumazet <edumazet@google.com>
> 
> Be careful when comparing tcp_time_stamp to some u32 quantity,
> otherwise result can be surprising.
> 
> Fixes: 7c106d7e782b ("[TCP]: TCP Low Priority congestion control")
> Signed-off-by: Eric Dumazet <edumazet@google.com>

Applied and queued up for -stable, thanks.
diff mbox

Patch

diff --git a/net/ipv4/tcp_lp.c b/net/ipv4/tcp_lp.c
index 046fd3910873306d74207615d6997e1c847ea361..d6fb6c067af4641f232b94e7c590c212648e8173 100644
--- a/net/ipv4/tcp_lp.c
+++ b/net/ipv4/tcp_lp.c
@@ -264,13 +264,15 @@  static void tcp_lp_pkts_acked(struct sock *sk, const struct ack_sample *sample)
 {
 	struct tcp_sock *tp = tcp_sk(sk);
 	struct lp *lp = inet_csk_ca(sk);
+	u32 delta;
 
 	if (sample->rtt_us > 0)
 		tcp_lp_rtt_sample(sk, sample->rtt_us);
 
 	/* calc inference */
-	if (tcp_time_stamp > tp->rx_opt.rcv_tsecr)
-		lp->inference = 3 * (tcp_time_stamp - tp->rx_opt.rcv_tsecr);
+	delta = tcp_time_stamp - tp->rx_opt.rcv_tsecr;
+	if ((s32)delta > 0)
+		lp->inference = 3 * delta;
 
 	/* test if within inference */
 	if (lp->last_drop && (tcp_time_stamp - lp->last_drop < lp->inference))