diff mbox

net: tcp: Fix a PTO timing granularity issue

Message ID 20150527152337.GB558@WorkStation.home
State Superseded, archived
Delegated to: David Miller
Headers show

Commit Message

Ido Yariv May 27, 2015, 3:23 p.m. UTC
Hi Eric,

On Wed, May 27, 2015 at 07:56:25AM -0700, Eric Dumazet wrote:
> On Wed, 2015-05-27 at 10:40 -0400, Ido Yariv wrote:
> 
> > HZ=100 is used on some embedded platforms, so it's still something we
> > have to deal with unfortunately..
> > 
> > Since the '2' here is a lower bound, and msecs_to_jiffies(10) will
> > return values greater than 2 for HZ>100 anyway, always ensuring the
> > 2 jiffies lower bound shouldn't impact the behavior when HZ=1000.
> > 
> > However, as far as I can tell, comparing msecs_to_jiffies(10) to 2, or
> > comparing the whole timeout to 2 doesn't make much difference, since
> > msecs_to_jiffies isn't inlined.
> > 
> > In other words, keeping the #if shouldn't make much difference in behavior,
> > but will save the small comparison.
> 
> Yes, I guess David point is to have a macro in include/linux/tcp.h so
> that we can have a nice comment, and not having #if ... in a C file.
> 
> Maybe other timers in TCP need the same care (I am not asking you to
> find them, but having a macro would ease things perhaps)

Something along the lines of the patch below?

Thanks,
Ido.

From 562019884d1b2c7619ce3f49ecb595147d28bbdd Mon Sep 17 00:00:00 2001
From: Ido Yariv <ido@wizery.com>
Date: Thu, 21 May 2015 08:23:13 +0200
Subject: [PATCH v3] net: tcp: Fix a PTO timing granularity issue

The Tail Loss Probe RFC specifies that the PTO value should be set to
max(2 * SRTT, 10ms), where SRTT is the smoothed round-trip time.

The PTO value is converted to jiffies, so the timer may expire
prematurely.

This is especially problematic on systems in which HZ <= 100, so work
around this by setting the timeout to at least 2 jiffies on such
systems.

The 10ms figure was originally selected based on tests performed with
the current implementation and HZ = 1000. Thus, leave the behavior on
systems with HZ > 100 unchanged.

Signed-off-by: Ido Yariv <idox.yariv@intel.com>
---
 include/net/tcp.h     | 9 +++++++++
 net/ipv4/tcp_output.c | 2 +-
 2 files changed, 10 insertions(+), 1 deletion(-)

Comments

Eric Dumazet May 27, 2015, 4:23 p.m. UTC | #1
On Wed, 2015-05-27 at 11:23 -0400, Ido Yariv wrote:

> Signed-off-by: Ido Yariv <idox.yariv@intel.com>
> ---
>  include/net/tcp.h     | 9 +++++++++
>  net/ipv4/tcp_output.c | 2 +-
>  2 files changed, 10 insertions(+), 1 deletion(-)
> 
> diff --git a/include/net/tcp.h b/include/net/tcp.h
> index 2bb2bad..86090b6 100644
> --- a/include/net/tcp.h
> +++ b/include/net/tcp.h
> @@ -1751,4 +1751,13 @@ static inline void skb_set_tcp_pure_ack(struct sk_buff *skb)
>  	skb->truesize = 2;
>  }
>  
> +/* Convert msecs to jiffies, ensuring that the return value is always at least
> + * 2. This can be used when setting tick-based timers to guarantee that they
> + * won't expire right away.
> + */
> +static inline unsigned long tcp_safe_msecs_to_jiffies(const unsigned int m)
> +{
> +	return max_t(u32, 2, msecs_to_jiffies(m));
> +}

Note that you can do slightly better if m is a constant at compile
time ;)

if (__builtin_constant_p(m) && m * 1000 >= 2 * HZ)
	return msecs_to_jiffies(m);
return max_t(u32, 2, msecs_to_jiffies(m));

Or something like that ?


--
To unsubscribe from this list: send the line "unsubscribe netdev" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Ido Yariv May 27, 2015, 4:54 p.m. UTC | #2
Hi Eric,

On Wed, May 27, 2015 at 09:23:36AM -0700, Eric Dumazet wrote:
> On Wed, 2015-05-27 at 11:23 -0400, Ido Yariv wrote:
> 
> > Signed-off-by: Ido Yariv <idox.yariv@intel.com>
> > ---
> >  include/net/tcp.h     | 9 +++++++++
> >  net/ipv4/tcp_output.c | 2 +-
> >  2 files changed, 10 insertions(+), 1 deletion(-)
> > 
> > diff --git a/include/net/tcp.h b/include/net/tcp.h
> > index 2bb2bad..86090b6 100644
> > --- a/include/net/tcp.h
> > +++ b/include/net/tcp.h
> > @@ -1751,4 +1751,13 @@ static inline void skb_set_tcp_pure_ack(struct sk_buff *skb)
> >  	skb->truesize = 2;
> >  }
> >  
> > +/* Convert msecs to jiffies, ensuring that the return value is always at least
> > + * 2. This can be used when setting tick-based timers to guarantee that they
> > + * won't expire right away.
> > + */
> > +static inline unsigned long tcp_safe_msecs_to_jiffies(const unsigned int m)
> > +{
> > +	return max_t(u32, 2, msecs_to_jiffies(m));
> > +}
> 
> Note that you can do slightly better if m is a constant at compile
> time ;)
> 
> if (__builtin_constant_p(m) && m * 1000 >= 2 * HZ)
> 	return msecs_to_jiffies(m);
> return max_t(u32, 2, msecs_to_jiffies(m));
> 
> Or something like that ?

That's a nice optimization ;)

However, I think that with Nicholas Mc Guire's recent changes to
msecs_to_jiffies (http://marc.info/?l=linux-kernel&m=143195210010666),
we should get this for free, no?

Cheers,
Ido.
--
To unsubscribe from this list: send the line "unsubscribe netdev" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Eric Dumazet May 27, 2015, 5:24 p.m. UTC | #3
On Wed, 2015-05-27 at 12:54 -0400, Ido Yariv wrote:
> Hi Eric,

> That's a nice optimization ;)
> 
> However, I think that with Nicholas Mc Guire's recent changes to
> msecs_to_jiffies (http://marc.info/?l=linux-kernel&m=143195210010666),
> we should get this for free, no?

Well, on net and net-next tree we currently have :

$ grep msecs_to_jiffies include/linux/jiffies.h
extern unsigned long msecs_to_jiffies(const unsigned int m);

Given your patch is for stable, I would not mind having this done
anyway.



--
To unsubscribe from this list: send the line "unsubscribe netdev" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
diff mbox

Patch

diff --git a/include/net/tcp.h b/include/net/tcp.h
index 2bb2bad..86090b6 100644
--- a/include/net/tcp.h
+++ b/include/net/tcp.h
@@ -1751,4 +1751,13 @@  static inline void skb_set_tcp_pure_ack(struct sk_buff *skb)
 	skb->truesize = 2;
 }
 
+/* Convert msecs to jiffies, ensuring that the return value is always at least
+ * 2. This can be used when setting tick-based timers to guarantee that they
+ * won't expire right away.
+ */
+static inline unsigned long tcp_safe_msecs_to_jiffies(const unsigned int m)
+{
+	return max_t(u32, 2, msecs_to_jiffies(m));
+}
+
 #endif	/* _TCP_H */
diff --git a/net/ipv4/tcp_output.c b/net/ipv4/tcp_output.c
index 534e5fd..83021c5 100644
--- a/net/ipv4/tcp_output.c
+++ b/net/ipv4/tcp_output.c
@@ -2207,7 +2207,7 @@  bool tcp_schedule_loss_probe(struct sock *sk)
 	if (tp->packets_out == 1)
 		timeout = max_t(u32, timeout,
 				(rtt + (rtt >> 1) + TCP_DELACK_MAX));
-	timeout = max_t(u32, timeout, msecs_to_jiffies(10));
+	timeout = max_t(u32, timeout, tcp_safe_msecs_to_jiffies(10));
 
 	/* If RTO is shorter, just schedule TLP in its place. */
 	tlp_time_stamp = tcp_time_stamp + timeout;