diff mbox series

net: use READ_ONCE() to read in concurrent environment

Message ID tencent_F35C58B90E47D014455212BC7110EDBB2106@qq.com (mailing list archive)
State Changes Requested
Delegated to: Netdev Maintainers
Headers show
Series net: use READ_ONCE() to read in concurrent environment | expand

Checks

Context Check Description
netdev/series_format warning Single patches do not need cover letters; Target tree name not specified in the subject
netdev/tree_selection success Guessed tree name to be net-next
netdev/ynl success Generated files up to date; no warnings/errors; no diff in generated;
netdev/fixes_present success Fixes tag not required for -next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 1079 this patch: 1079
netdev/build_tools success No tools touched, skip
netdev/cc_maintainers success CCed 0 of 0 maintainers
netdev/build_clang success Errors and warnings before: 1096 this patch: 1096
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 1097 this patch: 1097
netdev/checkpatch warning WARNING: line length of 86 exceeds 80 columns WARNING: line length of 90 exceeds 80 columns
netdev/build_clang_rust success No Rust files in patch. Skipping build
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0

Commit Message

linke li Jan. 22, 2024, 8:24 p.m. UTC
In function sk_stream_wait_memory(), reads of sk->sk_err and sk->sk_shutdown
is protected using READ_ONCE() in line 145, 146.
145: 		ret = sk_wait_event(sk, &current_timeo, READ_ONCE(sk->sk_err) ||
146: 				    (READ_ONCE(sk->sk_shutdown) & SEND_SHUTDOWN) ||

But reads in line 133 are not protected. This may cause unexpected error
when other threads change sk->sk_err and sk->sk_shutdown. Function
sk_stream_wait_connect() has same problem.

There is patch similar to this. https://github.com/torvalds/linux/commit/c1c0ce31b2420d5c173228a2132a492ede03d81f
This patch find two read of same variable while one is protected, another
is not. And READ_ONCE() is added to protect.

Signed-off-by: linke li <lilinke99@qq.com>
---
 net/core/stream.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

Comments

Kuniyuki Iwashima Jan. 22, 2024, 10:15 p.m. UTC | #1
From: linke li <lilinke99@qq.com>
Date: Tue, 23 Jan 2024 04:24:46 +0800
> In function sk_stream_wait_memory(), reads of sk->sk_err and sk->sk_shutdown
> is protected using READ_ONCE() in line 145, 146.
> 145: 		ret = sk_wait_event(sk, &current_timeo, READ_ONCE(sk->sk_err) ||
> 146: 				    (READ_ONCE(sk->sk_shutdown) & SEND_SHUTDOWN) ||
> 
> But reads in line 133 are not protected. This may cause unexpected error
> when other threads change sk->sk_err and sk->sk_shutdown. Function
> sk_stream_wait_connect() has same problem.
> 
> There is patch similar to this. https://github.com/torvalds/linux/commit/c1c0ce31b2420d5c173228a2132a492ede03d81f
> This patch find two read of same variable while one is protected, another
> is not. And READ_ONCE() is added to protect.

This is not sufficient.
You need to add WRITE_ONCE() on the writer side as well.

Also, you can disambiguate Subject by mentioning sk_state or
sk_stream_wait_connect().

Thanks!


> 
> Signed-off-by: linke li <lilinke99@qq.com>
> ---
>  net/core/stream.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/net/core/stream.c b/net/core/stream.c
> index b16dfa568a2d..7e67a2bf4480 100644
> --- a/net/core/stream.c
> +++ b/net/core/stream.c
> @@ -63,7 +63,7 @@ int sk_stream_wait_connect(struct sock *sk, long *timeo_p)
>  		int err = sock_error(sk);
>  		if (err)
>  			return err;
> -		if ((1 << sk->sk_state) & ~(TCPF_SYN_SENT | TCPF_SYN_RECV))
> +		if ((1 << READ_ONCE(sk->sk_state)) & ~(TCPF_SYN_SENT | TCPF_SYN_RECV))
>  			return -EPIPE;
>  		if (!*timeo_p)
>  			return -EAGAIN;
> @@ -130,7 +130,7 @@ int sk_stream_wait_memory(struct sock *sk, long *timeo_p)
>  	while (1) {
>  		sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
>  
> -		if (sk->sk_err || (sk->sk_shutdown & SEND_SHUTDOWN))
> +		if (READ_ONCE(sk->sk_err) || (READ_ONCE(sk->sk_shutdown) & SEND_SHUTDOWN))
>  			goto do_error;
>  		if (!*timeo_p)
>  			goto do_eagain;
> -- 
> 2.39.3 (Apple Git-145)
Paolo Abeni Jan. 23, 2024, 8:15 a.m. UTC | #2
On Tue, 2024-01-23 at 04:24 +0800, linke li wrote:
> In function sk_stream_wait_memory(), reads of sk->sk_err and sk->sk_shutdown
> is protected using READ_ONCE() in line 145, 146.
> 145: 		ret = sk_wait_event(sk, &current_timeo, READ_ONCE(sk->sk_err) ||
> 146: 				    (READ_ONCE(sk->sk_shutdown) & SEND_SHUTDOWN) ||

The above read happens outside the sk socket lock, see the
sk_wait_event() macro definition...

> 
> But reads in line 133 are not protected. 

... while the above one happens under the sk socket lock. The _ONCE
annotation is not needed.


> This may cause unexpected error
> when other threads change sk->sk_err and sk->sk_shutdown. Function
> sk_stream_wait_connect() has same problem.

Same as above, the access with the _ONCE() annotation is outside the
socket lock and vice versa.


I think this patch is not needed.

Thanks,

Paolo
diff mbox series

Patch

diff --git a/net/core/stream.c b/net/core/stream.c
index b16dfa568a2d..7e67a2bf4480 100644
--- a/net/core/stream.c
+++ b/net/core/stream.c
@@ -63,7 +63,7 @@  int sk_stream_wait_connect(struct sock *sk, long *timeo_p)
 		int err = sock_error(sk);
 		if (err)
 			return err;
-		if ((1 << sk->sk_state) & ~(TCPF_SYN_SENT | TCPF_SYN_RECV))
+		if ((1 << READ_ONCE(sk->sk_state)) & ~(TCPF_SYN_SENT | TCPF_SYN_RECV))
 			return -EPIPE;
 		if (!*timeo_p)
 			return -EAGAIN;
@@ -130,7 +130,7 @@  int sk_stream_wait_memory(struct sock *sk, long *timeo_p)
 	while (1) {
 		sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
 
-		if (sk->sk_err || (sk->sk_shutdown & SEND_SHUTDOWN))
+		if (READ_ONCE(sk->sk_err) || (READ_ONCE(sk->sk_shutdown) & SEND_SHUTDOWN))
 			goto do_error;
 		if (!*timeo_p)
 			goto do_eagain;