Skip to content

Commit

Permalink
tcp: adjust rcv_ssthresh according to sk_reserved_mem
Browse files Browse the repository at this point in the history
When user sets SO_RESERVE_MEM socket option, in order to utilize the
reserved memory when in memory pressure state, we adjust rcv_ssthresh
according to the available reserved memory for the socket, instead of
using 4 * advmss always.

Signed-off-by: Wei Wang <[email protected]>
Signed-off-by: Eric Dumazet <[email protected]>
Signed-off-by: David S. Miller <[email protected]>
tracywwnj authored and davem330 committed Sep 30, 2021
1 parent ca05705 commit 053f368
Showing 3 changed files with 22 additions and 4 deletions.
11 changes: 11 additions & 0 deletions include/net/tcp.h
Original file line number Diff line number Diff line change
@@ -1421,6 +1421,17 @@ static inline int tcp_full_space(const struct sock *sk)
return tcp_win_from_space(sk, READ_ONCE(sk->sk_rcvbuf));
}

static inline void tcp_adjust_rcv_ssthresh(struct sock *sk)
{
int unused_mem = sk_unused_reserved_mem(sk);
struct tcp_sock *tp = tcp_sk(sk);

tp->rcv_ssthresh = min(tp->rcv_ssthresh, 4U * tp->advmss);
if (unused_mem)
tp->rcv_ssthresh = max_t(u32, tp->rcv_ssthresh,
tcp_win_from_space(sk, unused_mem));
}

void tcp_cleanup_rbuf(struct sock *sk, int copied);

/* We provision sk_rcvbuf around 200% of sk_rcvlowat.
12 changes: 10 additions & 2 deletions net/ipv4/tcp_input.c
Original file line number Diff line number Diff line change
@@ -500,8 +500,11 @@ static void tcp_grow_window(struct sock *sk, const struct sk_buff *skb,

room = min_t(int, tp->window_clamp, tcp_space(sk)) - tp->rcv_ssthresh;

if (room <= 0)
return;

/* Check #1 */
if (room > 0 && !tcp_under_memory_pressure(sk)) {
if (!tcp_under_memory_pressure(sk)) {
unsigned int truesize = truesize_adjust(adjust, skb);
int incr;

@@ -518,6 +521,11 @@ static void tcp_grow_window(struct sock *sk, const struct sk_buff *skb,
tp->rcv_ssthresh += min(room, incr);
inet_csk(sk)->icsk_ack.quick |= 1;
}
} else {
/* Under pressure:
* Adjust rcv_ssthresh according to reserved mem
*/
tcp_adjust_rcv_ssthresh(sk);
}
}

@@ -5345,7 +5353,7 @@ static int tcp_prune_queue(struct sock *sk)
if (atomic_read(&sk->sk_rmem_alloc) >= sk->sk_rcvbuf)
tcp_clamp_window(sk);
else if (tcp_under_memory_pressure(sk))
tp->rcv_ssthresh = min(tp->rcv_ssthresh, 4U * tp->advmss);
tcp_adjust_rcv_ssthresh(sk);

if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf)
return 0;
3 changes: 1 addition & 2 deletions net/ipv4/tcp_output.c
Original file line number Diff line number Diff line change
@@ -2967,8 +2967,7 @@ u32 __tcp_select_window(struct sock *sk)
icsk->icsk_ack.quick = 0;

if (tcp_under_memory_pressure(sk))
tp->rcv_ssthresh = min(tp->rcv_ssthresh,
4U * tp->advmss);
tcp_adjust_rcv_ssthresh(sk);

/* free_space might become our new window, make sure we don't
* increase it due to wscale.

0 comments on commit 053f368

Please sign in to comment.