netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH net-next 1/1] tcp: sysctl: Fix a race to avoid unexpected 0 window from space
@ 2017-03-23 23:05 gfree.wind
  2017-03-24 20:29 ` David Miller
  2017-03-24 20:31 ` Stephen Hemminger
  0 siblings, 2 replies; 4+ messages in thread
From: gfree.wind @ 2017-03-23 23:05 UTC (permalink / raw)
  To: davem, netdev, gfree.wind; +Cc: Gao Feng

From: Gao Feng <fgao@ikuai8.com>

Because sysctl_tcp_adv_win_scale could be changed any time, so there
is one race in tcp_win_from_space.
For example,
1.sysctl_tcp_adv_win_scale<=0 (sysctl_tcp_adv_win_scale is negative now)
2.space>>(-sysctl_tcp_adv_win_scale) (sysctl_tcp_adv_win_scale is postive now)

As a result, tcp_win_from_space returns 0. It is unexpected.

Certainly if the compiler put the sysctl_tcp_adv_win_scale into one
register firstly, then use the register directly, it would be ok.
But we could not depend on the compiler behavior.

Signed-off-by: Gao Feng <fgao@ikuai8.com>
---
 include/net/tcp.h | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/include/net/tcp.h b/include/net/tcp.h
index 6ec4ea6..119b592 100644
--- a/include/net/tcp.h
+++ b/include/net/tcp.h
@@ -1252,9 +1252,11 @@ void tcp_select_initial_window(int __space, __u32 mss, __u32 *rcv_wnd,
 
 static inline int tcp_win_from_space(int space)
 {
-	return sysctl_tcp_adv_win_scale<=0 ?
-		(space>>(-sysctl_tcp_adv_win_scale)) :
-		space - (space>>sysctl_tcp_adv_win_scale);
+	int tcp_adv_win_scale = sysctl_tcp_adv_win_scale;
+
+	return tcp_adv_win_scale <= 0 ?
+		(space>>(-tcp_adv_win_scale)) :
+		space - (space>>tcp_adv_win_scale);
 }
 
 /* Note: caller must be prepared to deal with negative returns */
-- 
1.9.1

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH net-next 1/1] tcp: sysctl: Fix a race to avoid unexpected 0 window from space
  2017-03-23 23:05 [PATCH net-next 1/1] tcp: sysctl: Fix a race to avoid unexpected 0 window from space gfree.wind
@ 2017-03-24 20:29 ` David Miller
  2017-03-24 20:31 ` Stephen Hemminger
  1 sibling, 0 replies; 4+ messages in thread
From: David Miller @ 2017-03-24 20:29 UTC (permalink / raw)
  To: gfree.wind; +Cc: netdev, fgao

From: gfree.wind@foxmail.com
Date: Fri, 24 Mar 2017 07:05:12 +0800

> From: Gao Feng <fgao@ikuai8.com>
> 
> Because sysctl_tcp_adv_win_scale could be changed any time, so there
> is one race in tcp_win_from_space.
> For example,
> 1.sysctl_tcp_adv_win_scale<=0 (sysctl_tcp_adv_win_scale is negative now)
> 2.space>>(-sysctl_tcp_adv_win_scale) (sysctl_tcp_adv_win_scale is postive now)
> 
> As a result, tcp_win_from_space returns 0. It is unexpected.
> 
> Certainly if the compiler put the sysctl_tcp_adv_win_scale into one
> register firstly, then use the register directly, it would be ok.
> But we could not depend on the compiler behavior.
> 
> Signed-off-by: Gao Feng <fgao@ikuai8.com>

Applied.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH net-next 1/1] tcp: sysctl: Fix a race to avoid unexpected 0 window from space
  2017-03-23 23:05 [PATCH net-next 1/1] tcp: sysctl: Fix a race to avoid unexpected 0 window from space gfree.wind
  2017-03-24 20:29 ` David Miller
@ 2017-03-24 20:31 ` Stephen Hemminger
  2017-03-25  3:32   ` Gao Feng
  1 sibling, 1 reply; 4+ messages in thread
From: Stephen Hemminger @ 2017-03-24 20:31 UTC (permalink / raw)
  To: gfree.wind; +Cc: davem, netdev, Gao Feng

On Fri, 24 Mar 2017 07:05:12 +0800
gfree.wind@foxmail.com wrote:

> From: Gao Feng <fgao@ikuai8.com>
> 
> Because sysctl_tcp_adv_win_scale could be changed any time, so there
> is one race in tcp_win_from_space.
> For example,
> 1.sysctl_tcp_adv_win_scale<=0 (sysctl_tcp_adv_win_scale is negative now)
> 2.space>>(-sysctl_tcp_adv_win_scale) (sysctl_tcp_adv_win_scale is postive now)  
> 
> As a result, tcp_win_from_space returns 0. It is unexpected.
> 
> Certainly if the compiler put the sysctl_tcp_adv_win_scale into one
> register firstly, then use the register directly, it would be ok.
> But we could not depend on the compiler behavior.
> 
> Signed-off-by: Gao Feng <fgao@ikuai8.com>
> ---
>  include/net/tcp.h | 8 +++++---
>  1 file changed, 5 insertions(+), 3 deletions(-)
> 
> diff --git a/include/net/tcp.h b/include/net/tcp.h
> index 6ec4ea6..119b592 100644
> --- a/include/net/tcp.h
> +++ b/include/net/tcp.h
> @@ -1252,9 +1252,11 @@ void tcp_select_initial_window(int __space, __u32 mss, __u32 *rcv_wnd,
>  
>  static inline int tcp_win_from_space(int space)
>  {
> -	return sysctl_tcp_adv_win_scale<=0 ?
> -		(space>>(-sysctl_tcp_adv_win_scale)) :  
> -		space - (space>>sysctl_tcp_adv_win_scale);
> +	int tcp_adv_win_scale = sysctl_tcp_adv_win_scale;
> +
> +	return tcp_adv_win_scale <= 0 ?
> +		(space>>(-tcp_adv_win_scale)) :
> +		space - (space>>tcp_adv_win_scale);
>  }
>  
>  /* Note: caller must be prepared to deal with negative returns */

You need to use READ_ONCE() to be safe. The compiler is free to optimized the code back
to the original unless READ_ONCE() is used.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* RE: [PATCH net-next 1/1] tcp: sysctl: Fix a race to avoid unexpected 0 window from space
  2017-03-24 20:31 ` Stephen Hemminger
@ 2017-03-25  3:32   ` Gao Feng
  0 siblings, 0 replies; 4+ messages in thread
From: Gao Feng @ 2017-03-25  3:32 UTC (permalink / raw)
  To: 'Stephen Hemminger'; +Cc: davem, netdev, 'Gao Feng'

> -----Original Message-----
> From: netdev-owner@vger.kernel.org [mailto:netdev-owner@vger.kernel.org]
> On Behalf Of Stephen Hemminger
> Sent: Saturday, March 25, 2017 4:32 AM
> To: gfree.wind@foxmail.com
> Cc: davem@davemloft.net; netdev@vger.kernel.org; Gao Feng
> <fgao@ikuai8.com>
> Subject: Re: [PATCH net-next 1/1] tcp: sysctl: Fix a race to avoid
unexpected 0
> window from space
> 
> On Fri, 24 Mar 2017 07:05:12 +0800
> gfree.wind@foxmail.com wrote:
> 
> > From: Gao Feng <fgao@ikuai8.com>
> >
> > Because sysctl_tcp_adv_win_scale could be changed any time, so there
> > is one race in tcp_win_from_space.
> > For example,
> > 1.sysctl_tcp_adv_win_scale<=0 (sysctl_tcp_adv_win_scale is negative
> > now)
> > 2.space>>(-sysctl_tcp_adv_win_scale) (sysctl_tcp_adv_win_scale is
> > postive now)
> >
> > As a result, tcp_win_from_space returns 0. It is unexpected.
> >
> > Certainly if the compiler put the sysctl_tcp_adv_win_scale into one
> > register firstly, then use the register directly, it would be ok.
> > But we could not depend on the compiler behavior.
> >
> > Signed-off-by: Gao Feng <fgao@ikuai8.com>
> > ---
> >  include/net/tcp.h | 8 +++++---
> >  1 file changed, 5 insertions(+), 3 deletions(-)
> >
> > diff --git a/include/net/tcp.h b/include/net/tcp.h index
> > 6ec4ea6..119b592 100644
> > --- a/include/net/tcp.h
> > +++ b/include/net/tcp.h
> > @@ -1252,9 +1252,11 @@ void tcp_select_initial_window(int __space,
> > __u32 mss, __u32 *rcv_wnd,
> >
> >  static inline int tcp_win_from_space(int space)  {
> > -	return sysctl_tcp_adv_win_scale<=0 ?
> > -		(space>>(-sysctl_tcp_adv_win_scale)) :
> > -		space - (space>>sysctl_tcp_adv_win_scale);
> > +	int tcp_adv_win_scale = sysctl_tcp_adv_win_scale;
> > +
> > +	return tcp_adv_win_scale <= 0 ?
> > +		(space>>(-tcp_adv_win_scale)) :
> > +		space - (space>>tcp_adv_win_scale);
> >  }
> >
> >  /* Note: caller must be prepared to deal with negative returns */
> 
> You need to use READ_ONCE() to be safe. The compiler is free to optimized
the
> code back to the original unless READ_ONCE() is used.

Thanks your reminder.
I could not figure out why compiler would do the optimization, rollback the
global variable instead of local tmp variable.
It breaks the original logic.

BTW, I think the READ_ONCE is used to avoid reorder. 
Could you give some guides please ?

Best Regards
Feng

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2017-03-25  3:32 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-03-23 23:05 [PATCH net-next 1/1] tcp: sysctl: Fix a race to avoid unexpected 0 window from space gfree.wind
2017-03-24 20:29 ` David Miller
2017-03-24 20:31 ` Stephen Hemminger
2017-03-25  3:32   ` Gao Feng

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).