More of a meta comment, but I'm always grateful for these sort of network-related debugging blog posts. As a software eng who has a good grasp of networking at a conceptual layer, but has struggled to translate concept into concrete implementation in linux, I get a lot of value from these. Thank you to the author(s) for putting in the effort to write this up.
Same here. Is there any good printed/printable resource on all of the state of the art protocol configurations? Like what can all be configured, how can stuff break? It seems there is a myriad of man pages, mailing lists with that arcane knowledge
Brendan Greggs Systems Performance: Enterprise and the Cloud[0] gives a nice overview on available diagnostic tools in linux (like kprobe mentioned in TFA), and BPF Performance Tools[1] is a great introduction to eBPF tools (like bpftrace mentioned in TFA). His page Linux Performance[2] should be in everyone's bookmarks.
The rest is reading through the well documented linux source code, with the help of a map[3] I guess.
that's exactly the comment I wanted to give. I love to see people feel encouraged to go into such rabbit holes and also dare to spend time to describe it and share with the community!
> Memory reclaim reports how many pages it scanned and how many it managed to free per cgroup. Once roughly two megabytes' worth of pages have been scanned, the kernel turns that into a ratio. If it freed less than about 40% of what it walked, that counts as memory pressure, and every socket in the cgroup gets throttled for that one second. What's NOT in the formula is how much free memory the machine has.
Maybe I'm missing something, but can someone explain why memory pressure is detected like this in the first place? The article goes on to say that there was a TODO comment in the source to scale the scanning window proportional to the memory size, but why is memory pressure even measured like this without considering the free memory at all? Am I missing something or does that indeed make no sense?
> The memory controller helps you set limits for the maximum memory consumption of a service, which we are actually not using. It's the well-known memory.high, memory.max, etc. But the memory controller has an undocumented feature, which activates when the controller is only loaded.
Why is there even memory pressure in the first place when the cgroup does not have a limit and there is lots of free memory?
TL;DR: TCP delayed acknowledgments should never have been in the IETF RFC in the first place (as per John Nagle himself) and Linux is known for stalling this issue. TCP_QUICKACK is trash. MacOS and FreeBSD (and IIRC even Windows) already have options to kill it globally. The few cases where this makes sense, seem to be covered by client-side a.k.a. sender-side TCP_NODELAY.
Linux still has no decent option for killing off TCP delayed acknowledgments. Socket option TCP_QUICKACK on the server needs to be set for every interaction with the socket, like read(2) and write(2). Try forcing that through all the library layers. Sigh.
Linux kernel mailing list complained that there was insufficient measurements backing up a proposed build time kernel config for eliminating delayed ACK. Anecdotally, MacOS and FreeBSD respond better on crappy Motel WiFi when their global sysctl values set ack delay to zero.
Still cannot shake the feeling that the statistics for TCP congestion control failed to correct for the statistical effects of delayed acknowledgements and that structurally every other acknowledgement is often dropped.
Oh, this was excellent! And such a "duh! everything falls into place" with the components that in the end contributed to it.
And how many impactful TODO's must have be in the kernel? I've run a naive `rg "TODO:" --stats -c` on the current mainline (7.4-rc5), and looks like there's opportunity!
cgroup memory controller in general is full of traps. For example, it does also count filesystem caches into memory usage, which means just using it as limit against application leaks can get into bad behavior, especially if app relies on that cached file access, any leak will just cause that performance to get worse and worse far before it leaks to the level causing app to be killed.
IIRC tmpfs is also charged to the cgroup that uses it.
It makes sense from memory system perspective (else there would be a ways for cgroup to leak memory usage) but it also leads to many unintuitive debug sessions, and under-utilization of free RAM for caching
We should have a target rate test on prod to ensure we always hit that and assert in logs when it goes slow (in this case its memory throttle tomorrow is something else) as to ensure consistent performance for our apps
> The two-megabyte window was a hardcoded constant in the kernel version we were debugging, with a TODO: in the kernel asking it to scale with the machine size.
That TODO was removed about a month ago but remains there in spirit.
> There has been a steady stream of patches that have been submitted by newcomers to core mm 'fixing' this TODO, with all but the original having very likely been generated by LLMs.
> It appears that TODOs to LLMs are like red rags to a bull.
So... big open source projects are now forced to remove important TODOs from their code, because of vibe coders spamming patches for them?
Every day, I discover a new way in which AI is killing open source.
From the context of this HN post it seems like it was a TODO that should be fixed
The structural issue is more that the threshold for getting the initial limit in when you first write the code is very low, the standard of evidence for changing it afterwards is very high because you don't want to make things worse for anyone
The issue is more in how to communicate to all potential contributors (of which there are a lot more, now that llms promise an easier path to the still prestigious mark of "kernel contributor"). Improving on the current approach surely isn't hard, proving that your version is better is the hard part, and that has to be communicated
I understand the annoyance but at least from my experiences TODOs do have tendency to rot and be ignored for sometimes years; even before LLMplosion we stopped doing those because there is rarely someone that goes some day "lets fix that TODO"
Roadmap or a ticketing system is far better place for it, I only use it in currently work in progress code to denote which part isn't implemented yet
This is why I prefer FIXMEs limited to things that need immediate improvement. I try to resolve them in new code before the first commit and I periodically sweep for them in places where they remain in the repo.
They don't need to... it's clear when they are.. not only the dash length but certain phrases like "what happened" and "why it mattered" kind of section summaries. It's annoying when I see my team mates share such material without editing at least a bit and then claimed they've done the work.
On a project that I'm involved with between our organization and another, I am cc'd on a number of the emails. Right now it seems like there is a LLM on our side responding to a LLM on their side. It's both dystopian and depressing.
But limiting TCP socket memory use on a cgroup that could have TB of memory available to it and is not under memory pressure - WTF? The global kernel algorithm for socket memory still applies, so why have this extra socket limit on a cgroup when the user hasn't constrained the cgroup's memory?
We regularly see posts from kernel devs sweating blood to squeeze an extra couple of % of performance under some rare circumstances, yet here we have a process being throttled by more than 80% for no good reason. This has got to be one of the most egregious kernel performance bugs I've seen in a long time. And it's been this way for 11 years.
One of the wrongest default parameters in Linux is the dirty page ratio. By default, if I recall correctly, it permits up to 20% of pages to be dirty. If you're doing some quick arithmetic you already see the problem. The whole "pressure stall" concept has been adopted to hide this fact, when the better option is to simply lower the dirty page limit (not ratio) to 1GiB or less.
The dirty ratio also behaves differently than I assumed at first. 20% does not mean 20% of all memory, it means 20% of "dirtyable" memory. Also, 1GB dirty threshold does not mean that writes only get slowed down at 1GB. There is a "freerun" threshold at the midpoint between the background dirty limit and the dirty limit, and the kernel starts progressively throttling writers at that midpoint.
depends, if you have servers that buffer large blobs in tempfiles you don't really want those flushed to storage unless there's a real memory shortage.
that would be true if /tmp and the destination happen to be on the same filesystem, but I don't think that's a safe assumption. Especially if you are using tmpfs which stores /tmp in ram or swap.
I'm not sure that changes anything about the analysis. (1) It's not like pages in tmpfs are dirty to be flushed anywhere. (2) Web browsers typically download to temporary files in ~/Downloads before renaming in place anyway, to avoid that exact cross-filesystem mv problem.
No, on a multi-tenant machine I really, really want that tempfile thing to be throttled on dirty page creation. If that process thinks it can run in constant space without externalizing the page cache then it knows what to do: write its temporary objects to anonymous memory, not files.
The volume of the writeout in itself would not be a problem if it weren't that the dirty page table is locked until writeout completes. That means that new page allocations are stalled while writeout is in progress. And you're right, the default dirty page ratio is way too high, especially in the much-ram-meets-spinning-rust scenario. I have had rsync backups stall for over a minute while the archive server digests its dirty page buffer, only to have the server then stop writing for long enough to allow the disks to go back to standby mode.
It would take major engineering effort in the VFS layer to avoid locking the page table during writeout, but that's what a proper solution would look like in my eyes.
More of a meta comment, but I'm always grateful for these sort of network-related debugging blog posts. As a software eng who has a good grasp of networking at a conceptual layer, but has struggled to translate concept into concrete implementation in linux, I get a lot of value from these. Thank you to the author(s) for putting in the effort to write this up.
Same here. Is there any good printed/printable resource on all of the state of the art protocol configurations? Like what can all be configured, how can stuff break? It seems there is a myriad of man pages, mailing lists with that arcane knowledge
Brendan Greggs Systems Performance: Enterprise and the Cloud[0] gives a nice overview on available diagnostic tools in linux (like kprobe mentioned in TFA), and BPF Performance Tools[1] is a great introduction to eBPF tools (like bpftrace mentioned in TFA). His page Linux Performance[2] should be in everyone's bookmarks.
The rest is reading through the well documented linux source code, with the help of a map[3] I guess.
[0]: https://www.brendangregg.com/systems-performance-2nd-edition...
[1]: https://www.brendangregg.com/bpf-performance-tools-book.html
[2]: https://www.brendangregg.com/linuxperf.html
[3]: https://makelinux.github.io/kernel/map/
Indeed this was such a pleasurable read (and very nicely written too). Absolutely agree, +1 :)
that's exactly the comment I wanted to give. I love to see people feel encouraged to go into such rabbit holes and also dare to spend time to describe it and share with the community!
I think the workaround for this at the moment is the boot parameter:
If you're not bothered about socket memory counting towards the cgroup's limit (it's usually negligible).It can be the largest factor in non-caching proxies
Lots of little gems in this article. e.g.
> We launch nginx as a systemd service, and systemd places each service into its own cgroup and sets default controllers
Thank you for sharing.
> Memory reclaim reports how many pages it scanned and how many it managed to free per cgroup. Once roughly two megabytes' worth of pages have been scanned, the kernel turns that into a ratio. If it freed less than about 40% of what it walked, that counts as memory pressure, and every socket in the cgroup gets throttled for that one second. What's NOT in the formula is how much free memory the machine has.
Maybe I'm missing something, but can someone explain why memory pressure is detected like this in the first place? The article goes on to say that there was a TODO comment in the source to scale the scanning window proportional to the memory size, but why is memory pressure even measured like this without considering the free memory at all? Am I missing something or does that indeed make no sense?
Afaiu memory pressure is a boolean. Either the cgroup is under pressure or not. What is the question?
Further up they say (emphasis mine)
> The memory controller helps you set limits for the maximum memory consumption of a service, which we are actually not using. It's the well-known memory.high, memory.max, etc. But the memory controller has an undocumented feature, which activates when the controller is only loaded.
Why is there even memory pressure in the first place when the cgroup does not have a limit and there is lots of free memory?
ok, this is more of cgroup question than tcp, not my area of expertise.
This condition as described should not be considered memory pressure true.
TL;DR: TCP delayed acknowledgments should never have been in the IETF RFC in the first place (as per John Nagle himself) and Linux is known for stalling this issue. TCP_QUICKACK is trash. MacOS and FreeBSD (and IIRC even Windows) already have options to kill it globally. The few cases where this makes sense, seem to be covered by client-side a.k.a. sender-side TCP_NODELAY.
Linux still has no decent option for killing off TCP delayed acknowledgments. Socket option TCP_QUICKACK on the server needs to be set for every interaction with the socket, like read(2) and write(2). Try forcing that through all the library layers. Sigh.
Linux kernel mailing list complained that there was insufficient measurements backing up a proposed build time kernel config for eliminating delayed ACK. Anecdotally, MacOS and FreeBSD respond better on crappy Motel WiFi when their global sysctl values set ack delay to zero.
Still cannot shake the feeling that the statistics for TCP congestion control failed to correct for the statistical effects of delayed acknowledgements and that structurally every other acknowledgement is often dropped.
John Nagle posted important information on how it got into the IETF RFC erroneously here: https://news.ycombinator.com/item?id=9048947
CETERAM CENSEO TCP DELAYED ACK DELENDAM ESSE.
Oh, this was excellent! And such a "duh! everything falls into place" with the components that in the end contributed to it.
And how many impactful TODO's must have be in the kernel? I've run a naive `rg "TODO:" --stats -c` on the current mainline (7.4-rc5), and looks like there's opportunity!
(a bit more than 2.6% of files have at least one todo entry). Cutting it down, naively to only code withL `rg "TODO:" --stats -c -g "*.c"` More than double, 5.5% of `.c` files have some todo...cgroup memory controller in general is full of traps. For example, it does also count filesystem caches into memory usage, which means just using it as limit against application leaks can get into bad behavior, especially if app relies on that cached file access, any leak will just cause that performance to get worse and worse far before it leaks to the level causing app to be killed.
IIRC tmpfs is also charged to the cgroup that uses it.
It makes sense from memory system perspective (else there would be a ways for cgroup to leak memory usage) but it also leads to many unintuitive debug sessions, and under-utilization of free RAM for caching
any book resources where i can learn more about networking like tcp?
We should have a target rate test on prod to ensure we always hit that and assert in logs when it goes slow (in this case its memory throttle tomorrow is something else) as to ensure consistent performance for our apps
Do you need systemd or cgroups for this? Seems like unnecessary configuration and complexity.
great effort debugging and isolating the root cause, but also an unnecessary issue.
> The two-megabyte window was a hardcoded constant in the kernel version we were debugging, with a TODO: in the kernel asking it to scale with the machine size.
That TODO was removed about a month ago but remains there in spirit.
https://github.com/torvalds/linux/commit/0041eef981cc55bb1f3...
> those who understand the code know what could be improved in future
my god, they never learn, do they?
it's ego and no true scotsman all the way down
if they can't do it for memory management, they do it somewhere else
> There has been a steady stream of patches that have been submitted by newcomers to core mm 'fixing' this TODO, with all but the original having very likely been generated by LLMs.
> It appears that TODOs to LLMs are like red rags to a bull.
So... big open source projects are now forced to remove important TODOs from their code, because of vibe coders spamming patches for them?
Every day, I discover a new way in which AI is killing open source.
From the context of this HN post it seems like it was a TODO that should be fixed
The structural issue is more that the threshold for getting the initial limit in when you first write the code is very low, the standard of evidence for changing it afterwards is very high because you don't want to make things worse for anyone
The issue is more in how to communicate to all potential contributors (of which there are a lot more, now that llms promise an easier path to the still prestigious mark of "kernel contributor"). Improving on the current approach surely isn't hard, proving that your version is better is the hard part, and that has to be communicated
I understand the annoyance but at least from my experiences TODOs do have tendency to rot and be ignored for sometimes years; even before LLMplosion we stopped doing those because there is rarely someone that goes some day "lets fix that TODO"
Roadmap or a ticketing system is far better place for it, I only use it in currently work in progress code to denote which part isn't implemented yet
This is why I prefer FIXMEs limited to things that need immediate improvement. I try to resolve them in new code before the first commit and I periodically sweep for them in places where they remain in the repo.
> Disclaimer: No AI was used for writing this blog. Enjoy a humanly engineered piece.
The disclaimer now belongs on articles that were human written?
I don't think that AI generated articles are going to specify that they were written by AI any time soon.
They don't need to... it's clear when they are.. not only the dash length but certain phrases like "what happened" and "why it mattered" kind of section summaries. It's annoying when I see my team mates share such material without editing at least a bit and then claimed they've done the work.
On a project that I'm involved with between our organization and another, I am cc'd on a number of the emails. Right now it seems like there is a LLM on our side responding to a LLM on their side. It's both dystopian and depressing.
Bravo to CDN77 for tracking this down.
But limiting TCP socket memory use on a cgroup that could have TB of memory available to it and is not under memory pressure - WTF? The global kernel algorithm for socket memory still applies, so why have this extra socket limit on a cgroup when the user hasn't constrained the cgroup's memory?
We regularly see posts from kernel devs sweating blood to squeeze an extra couple of % of performance under some rare circumstances, yet here we have a process being throttled by more than 80% for no good reason. This has got to be one of the most egregious kernel performance bugs I've seen in a long time. And it's been this way for 11 years.
One of the wrongest default parameters in Linux is the dirty page ratio. By default, if I recall correctly, it permits up to 20% of pages to be dirty. If you're doing some quick arithmetic you already see the problem. The whole "pressure stall" concept has been adopted to hide this fact, when the better option is to simply lower the dirty page limit (not ratio) to 1GiB or less.
The dirty ratio also behaves differently than I assumed at first. 20% does not mean 20% of all memory, it means 20% of "dirtyable" memory. Also, 1GB dirty threshold does not mean that writes only get slowed down at 1GB. There is a "freerun" threshold at the midpoint between the background dirty limit and the dirty limit, and the kernel starts progressively throttling writers at that midpoint.
depends, if you have servers that buffer large blobs in tempfiles you don't really want those flushed to storage unless there's a real memory shortage.
But is that a default usecase?
well YES, even on desktop
Many, many apps do "download to tmpfile then move to final location when finished", from browsers thru Steam to package managers.
That's a metadata action and has nothing to do with rewriting the blocks of the file.
that would be true if /tmp and the destination happen to be on the same filesystem, but I don't think that's a safe assumption. Especially if you are using tmpfs which stores /tmp in ram or swap.
I'm not sure that changes anything about the analysis. (1) It's not like pages in tmpfs are dirty to be flushed anywhere. (2) Web browsers typically download to temporary files in ~/Downloads before renaming in place anyway, to avoid that exact cross-filesystem mv problem.
No, on a multi-tenant machine I really, really want that tempfile thing to be throttled on dirty page creation. If that process thinks it can run in constant space without externalizing the page cache then it knows what to do: write its temporary objects to anonymous memory, not files.
And then you need to worry about swapping.
The volume of the writeout in itself would not be a problem if it weren't that the dirty page table is locked until writeout completes. That means that new page allocations are stalled while writeout is in progress. And you're right, the default dirty page ratio is way too high, especially in the much-ram-meets-spinning-rust scenario. I have had rsync backups stall for over a minute while the archive server digests its dirty page buffer, only to have the server then stop writing for long enough to allow the disks to go back to standby mode.
It would take major engineering effort in the VFS layer to avoid locking the page table during writeout, but that's what a proper solution would look like in my eyes.
I set vm.dirty_bytes to 256MiB.