Skip to contentSkip to conversation

net: annotate sk->sk_rcvlowat lockless reads

PATCH Applied Eric Dumazet 2 participants 2 messages Oct 9, 2019 – Oct 10, 2019

Subsystems (7)
Networking Network drivers BPF TCP +3 more subsystems
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 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.

---
 include/net/sock.h  | 4 +++-
 net/core/filter.c   | 2 +-
 net/core/sock.c     | 2 +-
 net/ipv4/tcp.c      | 2 +-
 net/sched/em_meta.c | 2 +-
 5 files changed, 7 insertions(+), 5 deletions(-)
include/net/sock.h +3 -1
index 2c53f1a1d905409247b1bdafdfaf99d86e430cd0..79f54e1f88277dc7cc64ca0f35fd5ba869a2f96d 100644
@@ -2271,7 +2271,9 @@ static inline long sock_sndtimeo(const struct sock *sk, bool noblock)
 
 static inline int sock_rcvlowat(const struct sock *sk, int waitall, int len)
 {
-	return (waitall ? len : min_t(int, sk->sk_rcvlowat, len)) ? : 1;
+	int v = waitall ? len : min_t(int, READ_ONCE(sk->sk_rcvlowat), len);
+
+	return v ?: 1;
 }
 
 /* Alas, with timeout socket operations are not restartable.
net/core/filter.c +1 -1
index ed6563622ce31dcced4e6ba622770e26f1f7756a..a50c0b6846f29006268b2fb18303d692533bc081 100644
@@ -4274,7 +4274,7 @@ BPF_CALL_5(bpf_setsockopt, struct bpf_sock_ops_kern *, bpf_sock,
 		case SO_RCVLOWAT:
 			if (val < 0)
 				val = INT_MAX;
-			sk->sk_rcvlowat = val ? : 1;
+			WRITE_ONCE(sk->sk_rcvlowat, val ? : 1);
 			break;
 		case SO_MARK:
 			if (sk->sk_mark != val) {
net/core/sock.c +1 -1
index 1cf06934da50b98fccc849d396680cee46badb7d..b7c5c6ea51baf88548e73abd85c8f77cf29a2249 100644
@@ -974,7 +974,7 @@ int sock_setsockopt(struct socket *sock, int level, int optname,
 		if (sock->ops->set_rcvlowat)
 			ret = sock->ops->set_rcvlowat(sk, val);
 		else
-			sk->sk_rcvlowat = val ? : 1;
+			WRITE_ONCE(sk->sk_rcvlowat, val ? : 1);
 		break;
 
 	case SO_RCVTIMEO_OLD:
net/ipv4/tcp.c +1 -1
index 888c92b63f5a6dc4b935cca7c979c1e559126d44..8781a92ea4b6e4ee9ceeb763dae01970e7f4438a 100644
@@ -1699,7 +1699,7 @@ int tcp_set_rcvlowat(struct sock *sk, int val)
 	else
 		cap = sock_net(sk)->ipv4.sysctl_tcp_rmem[2] >> 1;
 	val = min(val, cap);
-	sk->sk_rcvlowat = val ? : 1;
+	WRITE_ONCE(sk->sk_rcvlowat, val ? : 1);
 
 	/* Check if we need to signal EPOLLIN right now */
 	tcp_data_ready(sk);
net/sched/em_meta.c +1 -1
index 82bd14e7ac93dc709483b3437cdc1779b34d0888..4c9122fc35c9d5f86ed60bc03427da1cde57b636 100644
@@ -554,7 +554,7 @@ META_COLLECTOR(int_sk_rcvlowat)
 		*err = -1;
 		return;
 	}
-	dst->value = sk->sk_rcvlowat;
+	dst->value = READ_ONCE(sk->sk_rcvlowat);
 }
 
 META_COLLECTOR(int_sk_rcvtimeo)
Signature · 1 line
2.23.0.581.g78d2f28ef7-goog
#1 lore raw
Quoted 9 lines · Eric Dumazet

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.

Applied, thanks!