When an application locks SO_RCVBUF, it disables TCP window auto-tuning.
However, the kernel still applies dynamic truesize penalties to the
scaling_ratio.

For small packets, this penalty drops the scaling_ratio to 1. This
reduces the advertised window and causes Silly Window Syndrome (SWS)
along with 504 Gateway Timeouts in applications like Tomcat.

This patch bypasses the truesize penalty if SOCK_RCVBUF_LOCK is set.
To prevent memory exhaustion from large aggregate payloads, the penalty
is still applied for GRO packets (skb->len > len).

Fixes: a2cbb1603943 ("tcp: Update window clamping condition")
Reported-by: Karen Badiryan <[email protected]>
Signed-off-by: Ankit Jain <[email protected]>
---
 net/ipv4/tcp_input.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/net/ipv4/tcp_input.c b/net/ipv4/tcp_input.c
index d5c9e65d9760..4b1832b3face 100644
--- a/net/ipv4/tcp_input.c
+++ b/net/ipv4/tcp_input.c
@@ -240,8 +240,13 @@ static void tcp_measure_rcv_mss(struct sock *sk, const 
struct sk_buff *skb)
                /* Note: divides are still a bit expensive.
                 * For the moment, only adjust scaling_ratio
                 * when we update icsk_ack.rcv_mss.
+                *
+                * Bypass truesize penalty for locked SO_RCVBUF to prevent
+                * window collapse. Still apply it to GRO packets.
                 */
-               if (unlikely(len != icsk->icsk_ack.rcv_mss)) {
+               if (unlikely(len != icsk->icsk_ack.rcv_mss &&
+                            (!(sk->sk_userlocks & SOCK_RCVBUF_LOCK) ||
+                             skb->len > len))) {
                        u64 val = (u64)skb->len << TCP_RMEM_TO_WIN_SCALE;
                        u8 old_ratio = tcp_sk(sk)->scaling_ratio;
 
-- 
2.53.0


Reply via email to