net: prevent load/store tearing on sk->sk_stamp
Subsystems (3)
PATCH 1/1 net: prevent load/store tearing on sk->sk_stamp
Add a couple of READ_ONCE() and WRITE_ONCE() to prevent load-tearing and store-tearing in sock_read_timestamp() and sock_write_timestamp()
This might prevent another KCSAN report.
--- include/net/sock.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
include/net/sock.h +2 -2
@@ -2342,7 +2342,7 @@ static inline ktime_t sock_read_timestamp(struct sock *sk)return kt;#else -return sk->sk_stamp; +return READ_ONCE (sk->sk_stamp);#endif }
@@ -2353,7 +2353,7 @@ static inline void sock_write_timestamp(struct sock *sk, ktime_t kt) sk->sk_stamp = kt;write_sequnlock (&sk->sk_stamp_seq);#else - sk->sk_stamp = kt; +WRITE_ONCE (sk->sk_stamp, kt);#endif }
Signature · 1 line
2.24.0.rc1.363.gb1bccd3e3d-goog
Quoted 37 lines · Eric Dumazet
Add a couple of
READ_ONCE()andWRITE_ONCE()to prevent load-tearing and store-tearing insock_read_timestamp()andsock_write_timestamp()This might prevent another KCSAN report.
Fixes 3a0ed3e96197 ("sock: Make sock->sk_stamp thread-safe")Signed-off-by Eric Dumazet <[email protected]>Cc Deepa Dinamani <[email protected]>--- include/net/sock.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)include/net/sock.h +2
-3@@ -2342,7 +2342,7 @@ static inline ktime_t sock_read_timestamp(struct sock *sk)return kt;#else -return sk->sk_stamp; +return READ_ONCE (sk->sk_stamp);#endif }@@ -2353,7 +2353,7 @@ static inline void sock_write_timestamp(struct sock *sk, ktime_t kt) sk->sk_stamp = kt;write_sequnlock (&sk->sk_stamp_seq);#else - sk->sk_stamp = kt; +WRITE_ONCE (sk->sk_stamp, kt);#endif } --
Quoted 1 line
2.24.0.rc1.363.gb1bccd3e3d-goog
I do not see any harm with this. Does it cause performance degradation?
-Deepa
Quoted 2 lines · Deepa Dinamani
I do not see any harm with this. Does it cause performance degradation?
It does not change compiler output, at least for common GCC versions.
It would change things if a hypothetical caller would do for example :
ktime_t res;
res = sock_read_timestamp(sk);
res = sock_read_timestamp(sk);
Since without the READ_ONCE(), compiler could remove the first instance.
Quoted 3 lines
Acked-by Deepa Dinamani <[email protected]>-Deepa
David Miller
maintainerFrom: Eric Dumazet <[email protected]>
Date: Mon, 4 Nov 2019 21:38:43 -0800
Quoted 8 lines
Add a couple of
READ_ONCE()andWRITE_ONCE()to prevent load-tearing and store-tearing insock_read_timestamp()andsock_write_timestamp()This might prevent another KCSAN report.
Fixes 3a0ed3e96197 ("sock: Make sock->sk_stamp thread-safe")Signed-off-by Eric Dumazet <[email protected]>
Applied and queued up for -stable.