Skip to content

Commit 867f816

Browse files
soheilhydavem330
authored andcommitted
tcp: limit sk_rcvlowat by the maximum receive buffer
The user-provided value to setsockopt(SO_RCVLOWAT) can be larger than the maximum possible receive buffer. Such values mute POLLIN signals on the socket which can stall progress on the socket. Limit the user-provided value to half of the maximum receive buffer, i.e., half of sk_rcvbuf when the receive buffer size is set by the user, or otherwise half of sysctl_tcp_rmem[2]. Fixes: d136184 ("tcp: fix SO_RCVLOWAT and RCVBUF autotuning") Signed-off-by: Soheil Hassas Yeganeh <[email protected]> Signed-off-by: Eric Dumazet <[email protected]> Reviewed-by: Neal Cardwell <[email protected]> Acked-by: Willem de Bruijn <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent b718e8c commit 867f816

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

net/ipv4/tcp.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1694,6 +1694,13 @@ EXPORT_SYMBOL(tcp_peek_len);
16941694
/* Make sure sk_rcvbuf is big enough to satisfy SO_RCVLOWAT hint */
16951695
int tcp_set_rcvlowat(struct sock *sk, int val)
16961696
{
1697+
int cap;
1698+
1699+
if (sk->sk_userlocks & SOCK_RCVBUF_LOCK)
1700+
cap = sk->sk_rcvbuf >> 1;
1701+
else
1702+
cap = sock_net(sk)->ipv4.sysctl_tcp_rmem[2] >> 1;
1703+
val = min(val, cap);
16971704
sk->sk_rcvlowat = val ? : 1;
16981705

16991706
/* Check if we need to signal EPOLLIN right now */
@@ -1702,12 +1709,7 @@ int tcp_set_rcvlowat(struct sock *sk, int val)
17021709
if (sk->sk_userlocks & SOCK_RCVBUF_LOCK)
17031710
return 0;
17041711

1705-
/* val comes from user space and might be close to INT_MAX */
17061712
val <<= 1;
1707-
if (val < 0)
1708-
val = INT_MAX;
1709-
1710-
val = min(val, sock_net(sk)->ipv4.sysctl_tcp_rmem[2]);
17111713
if (val > sk->sk_rcvbuf) {
17121714
sk->sk_rcvbuf = val;
17131715
tcp_sk(sk)->window_clamp = tcp_win_from_space(sk, val);

0 commit comments

Comments
 (0)