Skip to content

Commit b9bb53f

Browse files
wdebruijdavem330
authored andcommitted
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 <[email protected]> Signed-off-by: Willem de Bruijn <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent e43d15c commit b9bb53f

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

include/net/sock.h

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -459,26 +459,28 @@ struct sock {
459459

460460
static inline int sk_peek_offset(struct sock *sk, int flags)
461461
{
462-
if ((flags & MSG_PEEK) && (sk->sk_peek_off >= 0))
463-
return sk->sk_peek_off;
464-
else
465-
return 0;
462+
if (unlikely(flags & MSG_PEEK)) {
463+
s32 off = READ_ONCE(sk->sk_peek_off);
464+
if (off >= 0)
465+
return off;
466+
}
467+
468+
return 0;
466469
}
467470

468471
static inline void sk_peek_offset_bwd(struct sock *sk, int val)
469472
{
470-
if (sk->sk_peek_off >= 0) {
471-
if (sk->sk_peek_off >= val)
472-
sk->sk_peek_off -= val;
473-
else
474-
sk->sk_peek_off = 0;
473+
s32 off = READ_ONCE(sk->sk_peek_off);
474+
475+
if (unlikely(off >= 0)) {
476+
off = max_t(s32, off - val, 0);
477+
WRITE_ONCE(sk->sk_peek_off, off);
475478
}
476479
}
477480

478481
static inline void sk_peek_offset_fwd(struct sock *sk, int val)
479482
{
480-
if (sk->sk_peek_off >= 0)
481-
sk->sk_peek_off += val;
483+
sk_peek_offset_bwd(sk, -val);
482484
}
483485

484486
/*

0 commit comments

Comments
 (0)