Slow reclaim in a cgroup clamps its TCP windows
CDN77 published a debugging writeup on November 25 that reached the Hacker News front page this week. Downloads from one of their edge servers would collapse for some connections, stay low for hours, and recover with nobody touching anything. In one case the speed dropped just before 3 a.m. and came back around 8 a.m. Only nginx was slow. A curl on the same server downloaded at full speed, and a reboot fixed things for a while. The sysctls, kernel and nginx versions, CPU and memory all matched the healthy servers next to it.
They found it with a bpftrace probe on tcp_rcv_established. On the slow sockets, rcv_ssthresh sat at exactly four times the advertised MSS. In their kernel only tcp_adjust_rcv_ssthresh() writes that value, and it runs when tcp_under_memory_pressure() returns true. nstat reported TcpExtTCPMemoryPressures 0, because the TCP version of the check also asks whether the socket’s memory cgroup is under pressure. systemd had put nginx in its own cgroup with the memory controller turned on and no limits set.
Almost everything I run sits in a Docker container or a systemd unit, so nearly every socket I own belongs to a non-root memory cgroup. I wanted to watch this happen on my own box, and to find a way to spot it on kernels that don’t have the new counter yet.
The code path
Here’s the check in include/net/tcp.h on Linux 6.12. The cgroup test comes first, and the global TCP flag that nstat reports only gets looked at after it:
static inline bool tcp_under_memory_pressure(const struct sock *sk)
{
if (mem_cgroup_sockets_enabled && sk->sk_memcg &&
mem_cgroup_under_socket_pressure(sk->sk_memcg))
return true;
return READ_ONCE(tcp_memory_pressure);
}
The cgroup flag gets set in mm/vmpressure.c. Each time reclaim runs for a cgroup, vmpressure() adds up how many pages it scanned and how many it freed. Once 512 pages have been scanned (vmpressure_win = SWAP_CLUSTER_MAX * 16, 2 MB with 4 KB pages), it scores the window. If 40% or less of the scanned pages were freed, the score is “medium” or worse and this runs:
/* For now, no users for root-level efficiency */
if (!memcg || mem_cgroup_is_root(memcg))
return;
...
if (level > VMPRESSURE_LOW) {
WRITE_ONCE(memcg->socket_pressure, jiffies + HZ);
}
That’s one second of pressure, and the flag lives on the cgroup. Every TCP socket charged to that cgroup sees it, and every bad 512-page window pushes the deadline out again. CDN77 make the point that free memory isn’t part of the formula at all. A cache-heavy server with hundreds of gigabytes free can still have reclaim walking pages it can’t free, and each of those windows sets the flag.
The path from a bad reclaim window to a small receive window. Source: Linux v6.12 vmpressure.c, tcp.h and tcp_input.c, plus CDN77’s writeup.
In tcp_grow_window(), the pressure branch calls tcp_adjust_rcv_ssthresh(sk), which is __tcp_adjust_rcv_ssthresh(sk, 4U * tcp_sk(sk)->advmss). With a 1500-byte MTU, advmss is 1448 and the receive window gets held at 5,792 bytes.
Reproducing it on a 6.12 box
My box runs kernel 6.12.113 with cgroup v2, 8 vCPUs, 16 GB of RAM and no swap. This kernel has no netem and no veth, so I couldn’t add latency or build a two-interface setup. Instead I used a network namespace, which gets its own loopback device, and set that loopback to MTU 1500 so advmss comes out at 1448 like on a normal Ethernet link. The iperf3 server runs inside a cgroup capped at 200 MB:
sudo ip netns add sockp
sudo ip -n sockp link set lo up mtu 1500
sudo mkdir /sys/fs/cgroup/sockp-test
echo 200M | sudo tee /sys/fs/cgroup/sockp-test/memory.max
# move this shell into the cgroup, then exec iperf3 inside the namespace
sudo sh -c 'echo $$ > /sys/fs/cgroup/sockp-test/cgroup.procs;
exec ip netns exec sockp iperf3 -s -D -p 5201'
One difference from CDN77’s servers matters here. They set no memory limits, and their reclaim came from the machine as a whole. I used memory.max because it gets reclaim going inside one cgroup within seconds. Everything after vmpressure() is the same code, but how often a real server hits the flag depends on its workload.
To make reclaim inefficient, I wanted a process that keeps touching more page cache than the cgroup can hold. This script random-reads a 600 MB file through mmap, so reclaim keeps finding pages that were just used:
import mmap, random, sys, time
f = open(sys.argv[1], "rb")
m = mmap.mmap(f.fileno(), 0, prot=mmap.PROT_READ)
pages = len(m) // 4096
end = time.time() + float(sys.argv[2])
while time.time() < end:
m[random.randrange(pages) * 4096]
My first run showed nothing, and memory.stat for the cgroup said file 20480. Page cache is charged to the cgroup that first brings a page in, and I had created the 600 MB file with dd from my own shell, so all of its cached pages belonged to my shell’s cgroup. Calling os.posix_fadvise(fd, 0, 0, os.POSIX_FADV_DONTNEED) on the file before each run dropped those pages, and after that the churn script’s reads were charged to the test cgroup.
To watch the window, I adapted CDN77’s probe. It counts incoming segments on the iperf3 socket and how many of them arrived while rcv_ssthresh was exactly 4 * advmss:
kprobe:tcp_rcv_established
{
$tp = (struct tcp_sock *)arg0;
if ($tp->inet_conn.icsk_inet.sk.__sk_common.skc_num == 5201) {
@total = count();
if ($tp->rcv_ssthresh == 4 * $tp->advmss) { @clamped = count(); }
@min_ssthresh = min($tp->rcv_ssthresh);
}
}
interval:s:1 { print(@total); print(@clamped); print(@min_ssthresh);
clear(@total); clear(@clamped); clear(@min_ssthresh); }
Each run is a 15-second iperf3 upload to the server, which stayed in sockp-test every time. I ran three conditions three times each: no churn, churn in sockp-test next to the server, and churn in a second cgroup, sockp-churn, with the same 200 MB cap. The last one uses the same CPU and disk as the second without sharing the receiver’s memory cgroup.
What came out
| Condition | Throughput per run (Gbit/s) | Segments with rcv_ssthresh = 4 × advmss | Lowest rcv_ssthresh |
|---|---|---|---|
| No churn | 62.4, 62.5, 65.1 | 0%, 0%, 0% | 64,088 B |
| Churn in another cgroup | 63.8, 63.1, 62.6 | 0%, 0%, 0% | 64,088 B |
| Churn in the receiver’s cgroup | 27.8, 21.9, 43.1 | 89%, 94%, 68% | 5,792 B |
Same server, same churn script. Only the cgroup the churn runs in changes. Source: my runs on 2026-12-01, kernel 6.12.113.
The control is the middle row. The same churn running next door, using the same CPU and disk, didn’t move the receiver at all. The window only collapsed when the churn and the receiver shared a memory cgroup, and the lowest rcv_ssthresh in those runs was 5,792 bytes every time. That’s 4 × 1448. In the other rows the lowest value is 64,088, which is where rcv_ssthresh starts on these sockets before it grows.
Loopback round trips take microseconds, so even a 5,792-byte window moved 20 Gbit/s or more. On a real network path the same window costs a lot more. A connection can’t move more than one window per round trip, and at 40 ms RTT, 5,792 bytes per trip works out to about 145 KB/s, a little over 1 Mbit/s. That’s arithmetic and not something I measured, but it’s the size of collapse CDN77 describe.
Two more things I noticed. During these runs the cgroup’s memory.events showed max 5022369 and nothing else that mentions sockets, so on this kernel the only sign in cgroupfs is a reclaim counter that a busy, capped cgroup would show anyway. I also ran three uploads in the other direction (iperf3 -R), so the server in the cgroup was the sender. That went from 61.4 to 62.6 Gbit/s without churn to 56.0 to 56.3 with it. That’s a small drop that CPU contention alone could explain, and I didn’t look further.
Spotting it on real machines
CDN77 reported the problem upstream, and Shakeel Butt wrote a patch, memcg: net: track network throttling due to memcg memory pressure, which Daniel Sedlak of CDN77 reviewed. It merged as commit d929525, and comparing tags on GitHub shows it in v6.19 and not in v6.18. It adds a sock_throttled line to memory.events, which the cgroup v2 docs describe as “The number of times network sockets associated with this cgroup are throttled.” On 6.19 or newer, checking every systemd service is one command:
grep -H sock_throttled /sys/fs/cgroup/system.slice/*/memory.events
On a host where Docker uses the systemd cgroup driver, containers show up in the same folder as docker-<id>.scope, so that glob covers them too.
Older kernels need the probe. This version drops the port filter and groups clamped segments by the socket’s memory cgroup, using sk->sk_memcg, so it works for any process:
kprobe:tcp_rcv_established
{
$sk = (struct sock *)arg0;
$tp = (struct tcp_sock *)arg0;
if ($sk->sk_memcg != 0 && $tp->rcv_ssthresh == 4 * $tp->advmss) {
@clamped[str($sk->sk_memcg->css.cgroup->kn->name)] = count();
}
}
interval:s:5 { exit(); }
During a churn run it printed @clamped[sockp-test]: 1279477 for five seconds of traffic, and with no churn it printed nothing. It’s a heuristic. tcp_adjust_rcv_ssthresh() also runs under global TCP memory pressure, so if nstat -z | grep TCPMemoryPressures isn’t zero, some of the counts come from that instead. On my box the idle runs never hit the value once.
Ways around it
CDN77’s temporary fix was to move nginx into the root cgroup, since vmpressure() returns before setting the flag there. That also takes the process out of whatever memory accounting and limits its unit had, which was fine for them since they weren’t using limits.
The other switch I found is the boot parameter cgroup.memory=nosocket, which the kernel parameter docs describe as “Disable socket memory accounting.” In 6.12’s mm/memcontrol.c, the static branch behind mem_cgroup_sockets_enabled only gets turned on when that flag is unset, so with it set tcp_under_memory_pressure() never reaches the cgroup check. The cost is that socket buffers stop counting against any cgroup’s memory.max, so a container’s limit no longer covers its TCP memory. I haven’t tried this one, because it needs a reboot and my box is shared. CDN77 say a proper fix is on the way.
For my own servers, I’m adding sock_throttled to the host metrics once they’re on 6.19, and running the bpftrace script before then on any host where one container both reads a lot from disk and serves traffic. The first one I’ll check is Postgres in a container with a memory limit, since Postgres reads its tables through the OS page cache on top of shared_buffers. I haven’t tested that case, so for now it’s only the first place I’d look.