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>
This commit is contained in:
Willem de Bruijn 2016-04-05 12:41:14 -04:00 committed by David S. Miller
parent e43d15c8d3
commit b9bb53f383

View File

@ -459,26 +459,28 @@ struct sock {
static inline int sk_peek_offset(struct sock *sk, int flags) static inline int sk_peek_offset(struct sock *sk, int flags)
{ {
if ((flags & MSG_PEEK) && (sk->sk_peek_off >= 0)) if (unlikely(flags & MSG_PEEK)) {
return sk->sk_peek_off; s32 off = READ_ONCE(sk->sk_peek_off);
else if (off >= 0)
return off;
}
return 0; return 0;
} }
static inline void sk_peek_offset_bwd(struct sock *sk, int val) static inline void sk_peek_offset_bwd(struct sock *sk, int val)
{ {
if (sk->sk_peek_off >= 0) { s32 off = READ_ONCE(sk->sk_peek_off);
if (sk->sk_peek_off >= val)
sk->sk_peek_off -= val; if (unlikely(off >= 0)) {
else off = max_t(s32, off - val, 0);
sk->sk_peek_off = 0; WRITE_ONCE(sk->sk_peek_off, off);
} }
} }
static inline void sk_peek_offset_fwd(struct sock *sk, int val) static inline void sk_peek_offset_fwd(struct sock *sk, int val)
{ {
if (sk->sk_peek_off >= 0) sk_peek_offset_bwd(sk, -val);
sk->sk_peek_off += val;
} }
/* /*