News for hackers. Stuff that matters.
Re:Systemd 248 RC3: systemd-oomd is now considered fully sup... by bscphil on Saturday March 13, 2021 @11:08PM (#26452725)
For reference, [1] describes the SSD near-OOM thrash problem, where the speed of refaulting file pages is so high that the kernel detects enough activity and doesn't trigger OOM.

It's a heck of a problem to deal with, and a OOM killer will go part of the way to fixing it, but I still have two complaints about the thrash problem.

1. In similar situations on other operating systems, I don't usually see the GUI freeze to an unusable state. In Linux hitting a thrash situation will freeze the computer to the point where Ctrl-Alt-F2 will not switch the vterm even when waiting several minutes, and it has to be rebooted. I suppose it's possible that I just haven't used other OSes enough in a long time and they have this too, but this seems like a solvable problem. I assume it's not a CPU issue, since the scheduler should handle that just fine. So why not have a mechanism by which the user's desktop environment and init / login system can reserve itself enough memory to always be responsive under any memory condition?

2. Browsers. The state of browser memory management is atrocious. Just about every time I've run out of memory (other than compiling some complex software with -O3) it's been because my browser is hogging a huge chunk of it (even if the OOM killer blames something else). Now, I understand that in theory unused memory is wasted memory. So a browser using a ton of memory can be a good thing. But this is only true when memory the browser is holding but not using can be reclaimed to be used by another program, and this seems to basically never happen. If I close and reopen the browser (with the tabs automatically restored) the memory usage drops by 80% or more; the issue is that the browser simply never suspends tabs I haven't used in days or weeks. Modern browsers are not good "desktop citizens", if you will. They hog memory to the point where opening anything new will create a pressure stall.

I suspect solving these two issues would largely fix thrashing for most desktop users, without the need to ever kill anything, which is obviously undesirable.

Feel free to tell me if one or both of these is incorrect or impossible. These are just my observations, I'm not a kernel dev or anything close to one.

Re:Systemd 248 RC3: systemd-oomd is now considered fully sup... by garaetjjte on Sunday March 14, 2021 @10:13AM (#26455611)
>In Linux hitting a thrash situation will freeze the computer to the point where Ctrl-Alt-F2 will not switch the vterm even when waiting several minutes, and it has to be rebooted.

You can recover system from this state by manually triggering OOM killer using Magic SysRq keys. (Alt+SysRq+F, provided you have SysRq enabled in /proc/sys/kernel/sysrq)

Re:Systemd 248 RC3: systemd-oomd is now considered fully sup... by bscphil on Sunday March 14, 2021 @05:54PM (#26459688)
Is there a version of this that works on typical laptop keyboards that don't have SysRq? I could map an unused key to it with xmodmap of course, but if my X server isn't responding I don't think that would help very much!
Re:Systemd 248 RC3: systemd-oomd is now considered fully sup... by garaetjjte on Sunday March 14, 2021 @06:23PM (#26459993)
SysRq is PrintScreen, I think it should be present even on laptops?
Re:Systemd 248 RC3: systemd-oomd is now considered fully sup... by bscphil on Sunday March 14, 2021 @06:36PM (#26460148)
Oh! News to me, I definitely have it. Thanks!
Re:Systemd 248 RC3: systemd-oomd is now considered fully sup... by lolc on Sunday March 14, 2021 @11:01AM (#26455959)
This is very useful! I have to look up this combination every other month when one of my systems starts thrashing.
Re:Systemd 248 RC3: systemd-oomd is now considered fully sup... by raffraffraff on Sunday March 14, 2021 @04:07AM (#26453570)
> So why not have a mechanism by which the user's desktop environment and init / login system can reserve itself enough memory to always be responsive under any memory condition?

Because it's not the memory shortage that's nailing the UI. It's disk contention. When pathological swap activity causes disk thrashing, every process that needs to use the disk is now waiting in a very long queue with all the swap page in/out requests and every other process that is now backed up.

If you have a metrics agent that emits a handful of metrics on a per second granularity, you can see a bunch of things happen:

* Memory rises

* Some pages get swapped out

* Even if the system has no other processes writing to disk, you'll see the swap-out correlating with disk writes

* Memory rises some more

* You see more pages get swapped out

* (...and see more disk writes)

* Memory rises some more

* You see pages get swapped out and in, because now we're shuffling out pages that are imminently required by another running process.

* Disk queue lengths rise

* One by one, all processes are waiting (driving up the CPU "wa" number, and causing load averages to shoot up)

This is why, if you're oncall and you're trying to remotely troubleshoot "high load", you likely can't log in because the system needs to access /etc/passwd, /etc/nsswitch.conf, /etc/ldap/ldap.conf, /bin/bash... Your login gets handed a ticket with "∞" written on it, and is told to stand in the line for the disk.

But aren't these files already in the disk cache? Possibly. But another thing that happens when the system runs out of memory is that disk caches get purged. So you have to go back to the disk again. Also, when disk caches get dropped, your disk read performance is dogshit.

So what can you do? Try zswap? Use a different disk devices for swap? Try to pin important processes so the can't be swapped out?

NOTE: I might not be right about this stuff - I've never worked on the Linux kernel, and I rarely even compile it these days. But I've worked as SRE / *Ops for years and saw this problem a lot in badly configured systems. Same patterns all the time.

Re:Systemd 248 RC3: systemd-oomd is now considered fully sup... by bscphil on Sunday March 14, 2021 @05:15AM (#26453815)
Thanks for the comment, that's very interesting. I hadn't considered the possibility of the desktop environment needing to do disk reads, I was thinking everything was in memory already.

I suppose the solution is to figure out how to reduce or eliminate the number of disk reads after the DE initially loads, or cache everything needed in memory with a "do not evict" flag on it. Especially for the TTY, that's basically a crucial system function and keeping everything needed for it always available in memory wouldn't take much space relative to a typical desktop system.

I think at least some of the problem is the program code pages getting evicted to disk, so they have to be read back to memory too, which goes to the back of the disk queue. When I'm experiencing thrashing, even basic stuff like alt-tab to switch windows doesn't work. And I assume the window manager doesn't need to do disk reads to switch windows. Even the damn X cursor freezes!

Re:Systemd 248 RC3: systemd-oomd is now considered fully sup... by nitrogen on Sunday March 14, 2021 @12:29PM (#26456576)
Desktops could be improved quite a bit by tweaking default nice and ionice priorities for apps, instead of relying on one scheduling algorithm to be able to just "figure it out" on desktops, servers, phones, etc. A memory eviction priority, if niceness isn't treated as memory priority, could also help, but the existing tools can already go a long way and they just aren't being used.

I was able to get glitch-free audio by tweaking software and soft-irq realtime and non-realtime priorities, when the default Ububtu (with low-latency kernel) was nearly unusable for audio.

Re:Systemd 248 RC3: systemd-oomd is now considered fully sup... by thraway123412 on Sunday March 14, 2021 @07:36AM (#26454498)
This problem is there even if you have no swap at all, so I don't see different devices or zswap helping.
Re:Systemd 248 RC3: systemd-oomd is now considered fully sup... by raffraffraff on Sunday March 14, 2021 @08:52AM (#26454952)
> This problem

Which problem? Yes, there's always disk contention. But if you don't have swap enabled then you won't have pathological swap activity dominating the disk.

There are several bad things that happen when you run out of memory. One of them is pathological swapping. Another is loss of disk caches. Another is (eventual) oom. The one that has the most detrimental effect, and typically results in the system having to be reset, is swap activity saturating disk io. Even when you've got fast SSDs, because they're still orders of magnitude slower than memory.

Re:Systemd 248 RC3: systemd-oomd is now considered fully sup... by mrob on Sunday March 14, 2021 @09:35AM (#26455275)
You still have pathological disk io even with swap disabled because Linux will evict file-backed pages. More aggressive OOM killing is currently the only solution, which is not ideal, but losing one process beats losing all of them. I use earlyoom, and I assume systemd-oomd is something similar.
Re:Systemd 248 RC3: systemd-oomd is now considered fully sup... by thraway123412 on Sunday March 14, 2021 @09:44AM (#26455343)
> Which problem?

Thrashing and becoming unusable for a long time.

Re:Systemd 248 RC3: systemd-oomd is now considered fully sup... by pedrocr on Sunday March 14, 2021 @08:14AM (#26454680)
For 2) last time I looked I couldn't find a way to have a memory contract with the kernel that allowed for a given allocation to be evicted at will by the kernel while also telling the application that has happened. This would be ideal for apps that can grow their cache unbounded (e.g., photo processing apps with each photo, browsers with each tab) but can also regenerate that cache at will. You end up having tunables for how much cache to hold in RAM which is never correct as depending on what else the user is doing there may be a lot or very little free RAM.
Re:Systemd 248 RC3: systemd-oomd is now considered fully sup... by the8472 on Sunday March 14, 2021 @08:25AM (#26454737)
You can use PSI for that. And it actually makes more sense when all applications listen on the global PSI indicators so that if one application would start thrashing another can free up memory cooperatively. But if needed it can also be done per cgroup.
Re:Systemd 248 RC3: systemd-oomd is now considered fully sup... by nitrogen on Sunday March 14, 2021 @12:22PM (#26456532)
Something like madvise(MADV_FREE) sounds closer than MADV_DONTNEED -- the app can check for zero where it wrote nonzero -- but doesn't guarantee the freeing will be delayed until there is memory pressure.
Re:Systemd 248 RC3: systemd-oomd is now considered fully sup... by bombcar on Sunday March 14, 2021 @08:18AM (#26454701)
Would it be possible to run the cache in a separate process/thread and then somehow mark that process/thread as sacrificial?
Re:Systemd 248 RC3: systemd-oomd is now considered fully sup... by marcodiego on Saturday March 13, 2021 @11:41PM (#26452854)
> So why not have a mechanism by which the user's desktop environment and init / login system can reserve itself enough memory to always be responsive under any memory condition?

This! Always wanted a /proc/$PID flag to flag process' pages never to go to swap and reserve a number of cache pages specifically for the process. Something similar to mlock.

Re:Systemd 248 RC3: systemd-oomd is now considered fully sup... by cipherboy on Sunday March 14, 2021 @12:04PM (#26456413)
How much memory is enough for 1? Back on Fedora 32, on a system with close to a year of uptime (it was attached to my TV), gnome-shell consumed nearly 20GB of RAM on a 32GB system! And I rarely interacted with it (outside of watching the occasional concert on it). Most of the time was spent on the lockscreen, with the TV on a different input.

Perhaps this falls outside the category of "any memory condition"... :)

Though, it was responsive. :D

Needless to say, I think 1 is harder than expected. Fedora has made some recent changes on the OOM killer front though, perhaps those are closer to what you're interested in.

Re:Systemd 248 RC3: systemd-oomd is now considered fully sup... by notyourday on Sunday March 14, 2021 @11:19AM (#26456095)
Control groups are your friends. Run your browser in a separate control group with a constrained memory. It took me discovering memory leaks from qemu running X over virgl machines to actually spend a weekend figuring it out, but it is doable.

* I run separate cgroups for admin SSH with (1G min, 4G max ) - worst case scenario I can ssh into the desktop and kill offending processes.

* Separate cgroup for two browsers ( 8G each max ).

* Separate cgroup per qemu VM ( VM max ram + 512M each max ).

After implementing that I went from periodic lock ups to a quick browser crash and a restart maybe once a week.

Re:Systemd 248 RC3: systemd-oomd is now considered fully sup... by awwaiid on Sunday March 14, 2021 @12:33PM (#26456603)
Yesterday I ran into this nice article on using cgroups to contain firefox memory usage, https://dev.to/msugakov/taking-firefox-memory-usage-under-co...
Re:Systemd 248 RC3: systemd-oomd is now considered fully sup... by SAI_Peregrinus on Sunday March 14, 2021 @01:14PM (#26456917)
> the issue is that the browser simply never suspends tabs I haven't used in days or weeks. Modern browsers are not good "desktop citizens", if you will. They hog memory to the point where opening anything new will create a pressure stall.

The "Auto Tab Discard" extension[1] for Firefox unloads tab contents, keeping only the title and favicon. Tabs can be put in an allowlist to prevent discarding. I don't know if Chrome or Safari have something similar.

[1] https://addons.mozilla.org/en-US/firefox/addon/auto-tab-disc...

Re:Systemd 248 RC3: systemd-oomd is now considered fully sup... by drran on Sunday March 14, 2021 @09:36AM (#26455284)
For browser, reduce the number of parallel processes in preferences and try to use Auto Tab Discard plugin: https://www.youtube.com/watch?v=S0rHU38OnTE
Re:Systemd 248 RC3: systemd-oomd is now considered fully sup... by TacticalCoder on Sunday March 14, 2021 @11:05AM (#26455991)
> 2. Browsers. The state of browser memory management is atrocious. Just about every time I've run out of memory (other than compiling some complex software with -O3) it's been because my browser is hogging a huge chunk of it

> Modern browsers are not good "desktop citizens", if you will. They hog memory to the point where opening anything new will create a pressure stall.

I went the ballistic route: most of my browsers run inside local Docker containers, on which I put CPU and RAM quota.

It's my machine: I decide how "citizens" behave : )

Re:Systemd 248 RC3: systemd-oomd is now considered fully sup... by scaladev on Sunday March 14, 2021 @11:28AM (#26456171)
If you're using systemd, drop docker and start your browser through something like this:

  #!/bin/sh

  systemd-run \
    --user \
    --scope \
    --property=MemoryHigh=4G \
    --slice=background \
    /usr/bin/firefox "$@"
Theoretically this does not prevent Firefox from using more than 4 GBs of memory (you need MemoryMax for that), but practically it does.

  MemoryHigh=bytes
    Specify the throttling limit on memory usage of the executed processes in this unit. Memory usage may go above the limit if unavoidable, but the processes are heavily slowed down and memory is taken away aggressively in such cases. This is the main mechanism to control memory usage of a unit.

  MemoryMax=bytes
    Specify the absolute limit on memory usage of the executed processes in this unit. If memory usage cannot be contained under the limit, out-of-memory killer is invoked inside the unit. It is recommended to use MemoryHigh= as the main control mechanism and use MemoryMax= as the last line of defense.
I use a similar script to prevent my torrent client from pushing more useful data out of the page cache.

You can put resource limits on basically anything (see systemd.resource-control(5)).

Re:Systemd 248 RC3: systemd-oomd is now considered fully sup... by arp242 on Sunday March 14, 2021 @11:58AM (#26456377)
Docker is pretty overkill for this; you can just create a new cgroup with a maximum memory, CPU, etc.
Re:Systemd 248 RC3: systemd-oomd is now considered fully sup... by anthk on Sunday March 14, 2021 @12:41PM (#26456663)
Re:Systemd 248 RC3: systemd-oomd is now considered fully sup... by upofadown on Sunday March 14, 2021 @01:22PM (#26456964)
Speaking of which, Firefox behaviour has improved on OpenBSD significantly recently. It used to either crash or act weird when it hit the memory limit in login.conf (which it would eventually do). Now it just works. No idea what changed.
Re:Systemd 248 RC3: systemd-oomd is now considered fully sup... by xorcist on Sunday March 14, 2021 @02:01PM (#26457276)
This is what ulimit is for.
Re:Systemd 248 RC3: systemd-oomd is now considered fully sup... by The_rationalist on Sunday March 14, 2021 @11:48AM (#26456320)
Regarding the file pages this is the solution https://github.com/hakavlad/le9-patch