diff options
author | Willem de Bruijn <willemb@google.com> | 2016-04-05 12:41:14 -0400 |
---|---|---|
committer | David S. Miller <davem@davemloft.net> | 2016-04-05 16:29:36 -0400 |
commit | b9bb53f3836f4eb2bdeb3447be11042bd29c2408 (patch) | |
tree | f1052dadfe3e5694c235c77c7447a75a7ec353c1 /include/net/sock.h | |
parent | e43d15c8d3c8680fbf142360e5958f2ddd437047 (diff) | |
download | linux-stable-b9bb53f3836f4eb2bdeb3447be11042bd29c2408.tar.gz linux-stable-b9bb53f3836f4eb2bdeb3447be11042bd29c2408.tar.bz2 linux-stable-b9bb53f3836f4eb2bdeb3447be11042bd29c2408.zip |
sock: convert sk_peek_offset functions to WRITE_ONCE
Make the peek offset interface safe to use in lockless environments.
Use READ_ONCE and WRITE_ONCE to avoid race conditions between testing
and updating the peek offset.
Suggested-by: Eric Dumazet <edumazet@google.com>
Signed-off-by: Willem de Bruijn <willemb@google.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
Diffstat (limited to 'include/net/sock.h')
-rw-r--r-- | include/net/sock.h | 24 |
1 files changed, 13 insertions, 11 deletions
diff --git a/include/net/sock.h b/include/net/sock.h index 310c4367ea83..09aec75eb184 100644 --- a/include/net/sock.h +++ b/include/net/sock.h @@ -459,26 +459,28 @@ struct sock { static inline int sk_peek_offset(struct sock *sk, int flags) { - if ((flags & MSG_PEEK) && (sk->sk_peek_off >= 0)) - return sk->sk_peek_off; - else - return 0; + if (unlikely(flags & MSG_PEEK)) { + s32 off = READ_ONCE(sk->sk_peek_off); + if (off >= 0) + return off; + } + + return 0; } static inline void sk_peek_offset_bwd(struct sock *sk, int val) { - if (sk->sk_peek_off >= 0) { - if (sk->sk_peek_off >= val) - sk->sk_peek_off -= val; - else - sk->sk_peek_off = 0; + s32 off = READ_ONCE(sk->sk_peek_off); + + if (unlikely(off >= 0)) { + off = max_t(s32, off - val, 0); + WRITE_ONCE(sk->sk_peek_off, off); } } static inline void sk_peek_offset_fwd(struct sock *sk, int val) { - if (sk->sk_peek_off >= 0) - sk->sk_peek_off += val; + sk_peek_offset_bwd(sk, -val); } /* |