Skip to content

Commit eac6640

Browse files
edumazetJakub Kicinski
authored andcommitted
net: annotate sk->sk_rcvlowat lockless reads
sock_rcvlowat() or int_sk_rcvlowat() might be called without the socket lock for example from tcp_poll(). Use READ_ONCE() to document the fact that other cpus might change sk->sk_rcvlowat under us and avoid KCSAN splats. Use WRITE_ONCE() on write sides too. Signed-off-by: Eric Dumazet <[email protected]> Signed-off-by: Jakub Kicinski <[email protected]>
1 parent 8265792 commit eac6640

File tree

5 files changed

+7
-5
lines changed

5 files changed

+7
-5
lines changed

include/net/sock.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2271,7 +2271,9 @@ static inline long sock_sndtimeo(const struct sock *sk, bool noblock)
22712271

22722272
static inline int sock_rcvlowat(const struct sock *sk, int waitall, int len)
22732273
{
2274-
return (waitall ? len : min_t(int, sk->sk_rcvlowat, len)) ? : 1;
2274+
int v = waitall ? len : min_t(int, READ_ONCE(sk->sk_rcvlowat), len);
2275+
2276+
return v ?: 1;
22752277
}
22762278

22772279
/* Alas, with timeout socket operations are not restartable.

net/core/filter.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4274,7 +4274,7 @@ BPF_CALL_5(bpf_setsockopt, struct bpf_sock_ops_kern *, bpf_sock,
42744274
case SO_RCVLOWAT:
42754275
if (val < 0)
42764276
val = INT_MAX;
4277-
sk->sk_rcvlowat = val ? : 1;
4277+
WRITE_ONCE(sk->sk_rcvlowat, val ? : 1);
42784278
break;
42794279
case SO_MARK:
42804280
if (sk->sk_mark != val) {

net/core/sock.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -974,7 +974,7 @@ int sock_setsockopt(struct socket *sock, int level, int optname,
974974
if (sock->ops->set_rcvlowat)
975975
ret = sock->ops->set_rcvlowat(sk, val);
976976
else
977-
sk->sk_rcvlowat = val ? : 1;
977+
WRITE_ONCE(sk->sk_rcvlowat, val ? : 1);
978978
break;
979979

980980
case SO_RCVTIMEO_OLD:

net/ipv4/tcp.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1699,7 +1699,7 @@ int tcp_set_rcvlowat(struct sock *sk, int val)
16991699
else
17001700
cap = sock_net(sk)->ipv4.sysctl_tcp_rmem[2] >> 1;
17011701
val = min(val, cap);
1702-
sk->sk_rcvlowat = val ? : 1;
1702+
WRITE_ONCE(sk->sk_rcvlowat, val ? : 1);
17031703

17041704
/* Check if we need to signal EPOLLIN right now */
17051705
tcp_data_ready(sk);

net/sched/em_meta.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,7 @@ META_COLLECTOR(int_sk_rcvlowat)
554554
*err = -1;
555555
return;
556556
}
557-
dst->value = sk->sk_rcvlowat;
557+
dst->value = READ_ONCE(sk->sk_rcvlowat);
558558
}
559559

560560
META_COLLECTOR(int_sk_rcvtimeo)

0 commit comments

Comments
 (0)