tcp_nv: fix division by zero in tcpnv_acked()

Message ID 150954313588.153848.10192893966808917578.stgit@buzz
State Accepted
Delegated to: David Miller
Headers show
Series
  • tcp_nv: fix division by zero in tcpnv_acked()
Related show

Commit Message

Konstantin Khlebnikov Nov. 1, 2017, 1:32 p.m.
Average RTT could become zero. This happened in real life at least twice.
This patch treats zero as 1us.

Signed-off-by: Konstantin Khlebnikov <khlebnikov@yandex-team.ru>
---
 net/ipv4/tcp_nv.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Comments

Lawrence Brakmo Nov. 1, 2017, 1:47 p.m. | #1
Thank you for finding and fixing this.

On 11/1/17, 6:32 AM, "Konstantin Khlebnikov" <khlebnikov@yandex-team.ru> wrote:

    Average RTT could become zero. This happened in real life at least twice.
    This patch treats zero as 1us.
    
    Signed-off-by: Konstantin Khlebnikov khlebnikov@yandex-team.ru

Acked-by: Lawrence Brakmo <Brakmo@fb.com>

    ---
     net/ipv4/tcp_nv.c |    2 +-
     1 file changed, 1 insertion(+), 1 deletion(-)
    
    diff --git a/net/ipv4/tcp_nv.c b/net/ipv4/tcp_nv.c
    index 1ff73982e28c..125fc1450b01 100644
    --- a/net/ipv4/tcp_nv.c
    +++ b/net/ipv4/tcp_nv.c
    @@ -252,7 +252,7 @@ static void tcpnv_acked(struct sock *sk, const struct ack_sample *sample)
     
     	/* rate in 100's bits per second */
     	rate64 = ((u64)sample->in_flight) * 8000000;
    -	rate = (u32)div64_u64(rate64, (u64)(avg_rtt * 100));
    +	rate = (u32)div64_u64(rate64, (u64)(avg_rtt ?: 1) * 100);
     
     	/* Remember the maximum rate seen during this RTT
     	 * Note: It may be more than one RTT. This function should be
Stephen Hemminger Nov. 1, 2017, 2:13 p.m. | #2
On Wed, 1 Nov 2017 13:47:17 +0000
Lawrence Brakmo <brakmo@fb.com> wrote:

> Thank you for finding and fixing this.
> 
> On 11/1/17, 6:32 AM, "Konstantin Khlebnikov" <khlebnikov@yandex-team.ru> wrote:
> 
>     Average RTT could become zero. This happened in real life at least twice.
>     This patch treats zero as 1us.
>     
>     Signed-off-by: Konstantin Khlebnikov khlebnikov@yandex-team.ru
> Acked-by: Lawrence Brakmo <Brakmo@fb.com>
>     ---
>      net/ipv4/tcp_nv.c |    2 +-
>      1 file changed, 1 insertion(+), 1 deletion(-)
>     
>     diff --git a/net/ipv4/tcp_nv.c b/net/ipv4/tcp_nv.c
>     index 1ff73982e28c..125fc1450b01 100644
>     --- a/net/ipv4/tcp_nv.c
>     +++ b/net/ipv4/tcp_nv.c
>     @@ -252,7 +252,7 @@ static void tcpnv_acked(struct sock *sk, const struct ack_sample *sample)
>      
>      	/* rate in 100's bits per second */
>      	rate64 = ((u64)sample->in_flight) * 8000000;
>     -	rate = (u32)div64_u64(rate64, (u64)(avg_rtt * 100));
>     +	rate = (u32)div64_u64(rate64, (u64)(avg_rtt ?: 1) * 100);

Why is this code using expensive 64 bit by 64 bit divide when avg_rtt should never be bigger
than 32 bits?
Eric Dumazet Nov. 1, 2017, 2:17 p.m. | #3
On Wed, 2017-11-01 at 16:32 +0300, Konstantin Khlebnikov wrote:
> Average RTT could become zero. This happened in real life at least twice.
> This patch treats zero as 1us.
> 
> Signed-off-by: Konstantin Khlebnikov <khlebnikov@yandex-team.ru>
> ---
>  net/ipv4/tcp_nv.c |    2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/net/ipv4/tcp_nv.c b/net/ipv4/tcp_nv.c
> index 1ff73982e28c..125fc1450b01 100644
> --- a/net/ipv4/tcp_nv.c
> +++ b/net/ipv4/tcp_nv.c
> @@ -252,7 +252,7 @@ static void tcpnv_acked(struct sock *sk, const struct ack_sample *sample)
>  
>  	/* rate in 100's bits per second */
>  	rate64 = ((u64)sample->in_flight) * 8000000;
> -	rate = (u32)div64_u64(rate64, (u64)(avg_rtt * 100));
> +	rate = (u32)div64_u64(rate64, (u64)(avg_rtt ?: 1) * 100);
>  
>  	/* Remember the maximum rate seen during this RTT
>  	 * Note: It may be more than one RTT. This function should be
> 

BTW, this div64_u64() could be replaced by a u32 divide (aka do_div()),
slightly less expensive.


rate64 = ((u64)sample->in_flight) * 80000;
do_div(rate64, avg_rtt ?: 1);
rate = (u32)rate64;
David Miller Nov. 2, 2017, 7:17 a.m. | #4
From: Konstantin Khlebnikov <khlebnikov@yandex-team.ru>
Date: Wed, 01 Nov 2017 16:32:15 +0300

> Average RTT could become zero. This happened in real life at least twice.
> This patch treats zero as 1us.
> 
> Signed-off-by: Konstantin Khlebnikov <khlebnikov@yandex-team.ru>

Applied and queued up for -stable, thank you.

Indeed, as others have mentioned, a cheaper divide should be used
but that is a separate issue from this fix and therefore should be
done in a separate patch.

Thanks again.

Patch

diff --git a/net/ipv4/tcp_nv.c b/net/ipv4/tcp_nv.c
index 1ff73982e28c..125fc1450b01 100644
--- a/net/ipv4/tcp_nv.c
+++ b/net/ipv4/tcp_nv.c
@@ -252,7 +252,7 @@  static void tcpnv_acked(struct sock *sk, const struct ack_sample *sample)
 
 	/* rate in 100's bits per second */
 	rate64 = ((u64)sample->in_flight) * 8000000;
-	rate = (u32)div64_u64(rate64, (u64)(avg_rtt * 100));
+	rate = (u32)div64_u64(rate64, (u64)(avg_rtt ?: 1) * 100);
 
 	/* Remember the maximum rate seen during this RTT
 	 * Note: It may be more than one RTT. This function should be