Skip to content

Commit

Permalink
tcp: adjust sndbuf according to sk_reserved_mem
Browse files Browse the repository at this point in the history
If user sets SO_RESERVE_MEM socket option, in order to fully utilize the
reserved memory in memory pressure state on the tx path, we modify the
logic in sk_stream_moderate_sndbuf() to set sk_sndbuf according to
available reserved memory, instead of MIN_SOCK_SNDBUF, and adjust it
when new data is acked.

Signed-off-by: Wei Wang <weiwan@google.com>
Signed-off-by: Eric Dumazet <edumazet@google.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
  • Loading branch information
Wei Wang authored and David S. Miller committed Sep 30, 2021
1 parent 2bb2f5f commit ca05705
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
1 change: 1 addition & 0 deletions include/net/sock.h
Original file line number Diff line number Diff line change
Expand Up @@ -2378,6 +2378,7 @@ static inline void sk_stream_moderate_sndbuf(struct sock *sk)
return;

val = min(sk->sk_sndbuf, sk->sk_wmem_queued >> 1);
val = max_t(u32, val, sk_unused_reserved_mem(sk));

WRITE_ONCE(sk->sk_sndbuf, max_t(u32, val, SOCK_MIN_SNDBUF));
}
Expand Down
14 changes: 12 additions & 2 deletions net/ipv4/tcp_input.c
Original file line number Diff line number Diff line change
Expand Up @@ -5380,7 +5380,7 @@ static int tcp_prune_queue(struct sock *sk)
return -1;
}

static bool tcp_should_expand_sndbuf(const struct sock *sk)
static bool tcp_should_expand_sndbuf(struct sock *sk)
{
const struct tcp_sock *tp = tcp_sk(sk);

Expand All @@ -5391,8 +5391,18 @@ static bool tcp_should_expand_sndbuf(const struct sock *sk)
return false;

/* If we are under global TCP memory pressure, do not expand. */
if (tcp_under_memory_pressure(sk))
if (tcp_under_memory_pressure(sk)) {
int unused_mem = sk_unused_reserved_mem(sk);

/* Adjust sndbuf according to reserved mem. But make sure
* it never goes below SOCK_MIN_SNDBUF.
* See sk_stream_moderate_sndbuf() for more details.
*/
if (unused_mem > SOCK_MIN_SNDBUF)
WRITE_ONCE(sk->sk_sndbuf, unused_mem);

return false;
}

/* If we are under soft global TCP memory pressure, do not expand. */
if (sk_memory_allocated(sk) >= sk_prot_mem_limits(sk, 0))
Expand Down

0 comments on commit ca05705

Please sign in to comment.