News for hackers. Stuff that matters.
Re:The Linux kernel's inability to gracefully handle low mem... by cperciva on Monday August 05, 2019 @09:56PM (#20621346)
Further to the comments about the pager hammering the disk to read clean pages (mainly but not exclusively binaries) even if swapping is disabled: In many cases adding swap space will reduce the amount of paging which occurs.

Many long-lived processes are completely idle (when was the last time that `getty ttyv6` woke up?) or at a minimum have pages of memory which are never used (e.g. the bottom page of main's stack). Evicting these "theoretically accessible but in practice never accessed" pages for memory frees up more memory for the things which matter.

Re:The Linux kernel's inability to gracefully handle low mem... by quazeekotl on Monday August 05, 2019 @10:16PM (#20621462)
Unfortunately enabling swap in linux has a very annoying side effect, linux will preferentially push out pages of running programs that have been untouched for X time for more disk cache, pretty much no matter how much ram you have.

This comes into play when you copy or access huge files that are going to be read exactly once, they will start pushing out untouched program pages to disk, in exchange for disk cache that is completely 100% useless, even to the tune of hundreds of gigabytes of it.

Programs can reduce the problem with madvise(MADV_DONT_NEED), but that only applies to files you are mmap()ing, and every single program under the sun needs to be patched to issue these calls.

You can adjust vm.swapiness systctl to make X larger, but no matter what, programs will start to get pushed out to disk eventually, and cause unresponsiveness when activated. You can reduce vm.swapiness to 1, but if you do, the system only starts swapping in an absolute critical low ram situation and you encounter anywhere from 5 minutes, to 1+ hour of total, complete unresponsiveness in a low ram situation.

There _NEEDS_ to be a setting where program pages don't get pushed out for disk cache, peroid, unless approaching a low ram situation, but BEFORE it causes long periods of total crushing unresponsiveness.

Re:The Linux kernel's inability to gracefully handle low mem... by caf on Tuesday August 06, 2019 @12:52AM (#20622244)
There _NEEDS_ to be a setting where program pages don't get pushed out for disk cache, peroid, unless approaching a low ram situation, but BEFORE it causes long periods of total crushing unresponsiveness.

Here's the thing: a mapped program page is just another page in the page cache. Now, you could maybe say that "any page cache page that is mapped into at least one process will be pinned", but the problem there is that means that any unprivileged process can then pin an unlimited amount of memory, which is an obvious non-starter.

A workable alternative might be to add an extended file attribute like 'privileged.pinned_mapping', which if set indicates that any pages of the file that have active shared mappings are pinned. That means the superuser can go along and mark all the normal executables in this way, and the worst-case memory consumption a user can cause is limited by the total size of all the executables marked in this way that the user has access to.

Re:The Linux kernel's inability to gracefully handle low mem... by pferde on Tuesday August 06, 2019 @06:33AM (#20623927)
SuSE solves this in their SuSE Linux Enterprise Server (SLES) with a new sysctl tunable, which soft-limits the size of the page cache.

https://www.suse.com/documentation/sles-for-sap-12/book_s4s/...

It is quite effective, although historically there have been issues with bugs causing server lockups in the kernel code around this tunable. It seems to be quite stable in SLES 15, however.

While the tunable is available in their regular SLES product, it is only supported in the "SLES for SAP". The two share the same kernel, that is probably why.

Re:The Linux kernel's inability to gracefully handle low mem... by jdsully on Tuesday August 06, 2019 @01:13AM (#20622333)
Theres no reason extra data cannot be added to entries in the page cache to make smarter decisions. That’s how Windows and OS X do it in their equivalent subsystems.

Nobody is suggesting these pages be pinned which is an extreme measure.

Re:The Linux kernel's inability to gracefully handle low mem... by caf on Tuesday August 06, 2019 @01:58AM (#20622551)
The problem I'm trying to point out here is that if the extra metadata in the page cache is entirely under user control (like for example "is mapped shared" and/or "is mapped executable") then it amounts to a user-specified QOS flag.

That might be OK on a single-user system but it doesn't fly on a multi-user one. That's why I suggested you could gate that kind of thing behind some kind of superuser control.

Re:The Linux kernel's inability to gracefully handle low mem... by jdsully on Tuesday August 06, 2019 @03:45AM (#20623083)
Why can’t a user make QoS decisions for their own pages? Root controlled pages should obviously have higher priority.

The kernel could still “fairly” evict pages across users - just letting them choose which N pages they prefer to go first.

Re:The Linux kernel's inability to gracefully handle low mem... by caf on Tuesday August 06, 2019 @04:03AM (#20623184)
Why can’t a user make QoS decisions for their own pages?

Because then you just get everyone asking for maximum QOS / don't-page-me-out on everything they can.

The pages in the page cache are not owned by a particular user, they're shared. If there's three users running /usr/bin/firefox, they'll all have have shared read-only executable mappings of the same page cache pages. If you do a buffered read of a file immediately after I do the same, we both get our data copied from the same page cache page. So it's not at all clear how you'd do the accounting on this to implement that user-based fairness criterion.

Re:The Linux kernel's inability to gracefully handle low mem... by the8472 on Monday August 05, 2019 @10:19PM (#20621483)
> but that only applies to files you are mmap()ing

fadvise provides the same for file descriptors. some tools such as rsync make use of it to prevent clobbering the page cache when streaming files.

Re:The Linux kernel's inability to gracefully handle low mem... by quazeekotl on Monday August 05, 2019 @10:20PM (#20621502)
nice! was not aware of that syscall, however, patching the entire world remains...
Re:The Linux kernel's inability to gracefully handle low mem... by compsciphd on Tuesday August 06, 2019 @12:01AM (#20622012)
it might be possible to create an LD_PRELOAD'd library that wraps open type syscalls (i.e. those that return an fd, might just be open, haven't kept up with all of linux syscalls) and that based on a config file calls fadvise on those fd's that correspond to specific files/paths on disk). Won't help for statically linked binaries or those that call syscalls directly without glibc's shims, but that should be a small number of programs.

heck, if I were still a phd student, I'd want to run performance numbers on this in many different scenarios and see how performance behaves. feel like there could be a paper here.

Re:The Linux kernel's inability to gracefully handle low mem... by the8472 on Tuesday August 06, 2019 @12:08AM (#20622036)
Re:The Linux kernel's inability to gracefully handle low mem... by compsciphd on Tuesday August 06, 2019 @12:09AM (#20622046)
@the8472, hah, so someone thought of the same thing. I'd try to leverage /etc/ld.so.preload with a config file as a more transparent solution, but your link proves the point that its possible.
Re:The Linux kernel's inability to gracefully handle low mem... by the8472 on Tuesday August 06, 2019 @02:30PM (#20628026)
You probably don't want it in ld preload globally because it would also clobber the page cache in processes that do benefit from it.

And if you only do it in a container you can also limit the page cache size of the container to avoid impacting the other workloads.

Re:The Linux kernel's inability to gracefully handle low mem... by compsciphd on Tuesday August 06, 2019 @03:35PM (#20628670)
hence why I said a config file based (i.e. include the path that stores one's media, won't matter what program you use to play it), but yes, page cache does play a role (but hence why I also said it be interesting to explore how different applications behave with and without it and how that impacts other system performance)

i.e. I really wonder for desktop workloads if one only caches "executable" data, how would that negatively impact perceived performance. I'd imagine it have some impact, but I'd be interested in seeing it quantified.

Re:The Linux kernel's inability to gracefully handle low mem... by magicalhippo on Monday August 05, 2019 @11:05PM (#20621741)
Can't the file cache detect streaming loads and skip caching it? ZFS does this for its L2ARC[1].

[1]: https://wiki.freebsd.org/ZFSTuningGuide#L2ARC_discussion

Re:The Linux kernel's inability to gracefully handle low mem... by wolfgang42 on Monday August 05, 2019 @10:48PM (#20621652)
Is there a way to enable such an option for an entire process, in the same way as e.g. ionice(1)?

Whenever I take a backup of my computer it winds up swapping everything else out to disk. Normally I'm perfectly happy letting unused pages get evicted in favor of more cache, but for this specific program this behavior is very much not ideal. I'm asking here since I've done some searching in the past and not found anything, but I'm not sure if I was using the right keywords.

Re:The Linux kernel's inability to gracefully handle low mem... by wolfgang42 on Monday August 12, 2019 @06:21PM (#20680690)
Follow-up, for people who encounter this thread in the future: I did some more hunting and found `nocache` (https://github.com/Feh/nocache , though I installed it via the Ubuntu repositories) which does this by intercepting the open() system call and calling fadvise() immediately afterwards.
Re:The Linux kernel's inability to gracefully handle low mem... by gwd on Tuesday August 06, 2019 @06:04AM (#20623798)
> Unfortunately enabling swap in linux has a very annoying side effect, linux will preferentially push out pages of running programs that have been untouched for X time for more disk cache, pretty much no matter how much ram you have.

THIS. I ended up disabling swap because my kernel insisted on essentially reserving 50% of RAM for disk buffers; meaning even with 16GiB of RAM, I'd have processes getting swapped out and taking forever to run, because everything was stuck in 8GiB of RAM, and Firefox was taking 6GiB of that. I couldn't for the life of me figure out a way to get Linux to make that something more reasonable, like 20%. (And yes, I tried playing with `vm.swapiness`.)

Re:The Linux kernel's inability to gracefully handle low mem... by paulsutter on Monday August 05, 2019 @11:37PM (#20621910)
Programs should really use unbuffered i/o for large files read only once (yes i know Linus doesn’t like unbuffered i/o but he’s wrong)

> This comes into play when you copy or access huge files that are going to be read exactly once

Re:The Linux kernel's inability to gracefully handle low mem... by caf on Tuesday August 06, 2019 @12:43AM (#20622198)
Readahead is still useful for large files read sequentially once, and that needs to be buffered. Such programs should use posix_fadvise().
Re:The Linux kernel's inability to gracefully handle low mem... by paulsutter on Tuesday August 06, 2019 @12:54AM (#20622254)
you can readahead as far as you like with unbuffered io
Re:The Linux kernel's inability to gracefully handle low mem... by caf on Tuesday August 06, 2019 @02:25AM (#20622690)
If you are reading unbuffered (ie. O_DIRECT) then you are reading directly into the memory block the user supplied, so you cannot read ahead - there's nowhere to put the extra data.
Re:The Linux kernel's inability to gracefully handle low mem... by paulsutter on Tuesday August 06, 2019 @07:32AM (#20624201)
Of course you can read ahead using multiple buffers, you can issue as many reads as you want concurrently
Re:The Linux kernel's inability to gracefully handle low mem... by caf on Tuesday August 06, 2019 @10:28AM (#20625450)
I think it is pretty clear I was referring to kernel-mediated readahead. Sure, you can achieve the same thing in userspace using async IO or threads.
Re:The Linux kernel's inability to gracefully handle low mem... by paulsutter on Tuesday August 06, 2019 @11:25AM (#20625960)
I think it’s pretty clear that I was referring to reading large files using direct io, and your comment was totally irrelevant
Re:The Linux kernel's inability to gracefully handle low mem... by caf on Tuesday August 06, 2019 @08:21PM (#20630953)
Reading large files using direct IO defeats kernel read-ahead, which means you have to take on the complexity of reimplementing it in userspace, or the performance hit of not having it.

This is a good reason for programs to use buffered IO even when they are reading a large file once, so yes my comment was entirely on point.

Re:The Linux kernel's inability to gracefully handle low mem... by paulsutter on Wednesday August 07, 2019 @11:52AM (#20635830)
Your comments were misleading, even if you want to argue they were on point. I know that you're knowledgable about the kernel, I ready your other comments. The following comments are just not true, except in bizarro world where all code must be in the kernel:

- "Readahead... needs to be buffered"

- "reading unbuffered (ie. O_DIRECT)... you cannot read ahead"

Good application code needs to work around kernel limitations all the time. Your suggestion of fadvise is reasonable, except in practice fadvise rarely makes a measurable difference. The only way to get what you really want is to code it the way you want it, and fortunately, there are multiple ways to do that.

Re:The Linux kernel's inability to gracefully handle low mem... by kasabali on Tuesday August 06, 2019 @07:01PM (#20630524)
> There _NEEDS_ to be a setting where program pages don't get pushed out for disk cache, peroid, unless approaching a low ram situation, but BEFORE it causes long periods of total crushing unresponsiveness.

Did you try different vm.vfs_cache_pressure values?

Re:The Linux kernel's inability to gracefully handle low mem... by ailideex on Tuesday August 06, 2019 @12:49AM (#20622229)
Probably the best solution to this is something like memlockd where you explicitly tell the kernel what memory must always be in resident set.
Re:The Linux kernel's inability to gracefully handle low mem... by throwaway3627 on Monday August 05, 2019 @10:02PM (#20621371)
Linux resource scheduling and prioritization and is pretty awful compared to its popularity.

TBH, there are very few OSes that get high-pressure resource scheduling and prioritization right under nearly all normal circumstances.

The hackaround for decades on Linux is always adding a tiny swap device, say 64-256 MiB on a fast device in order to 0) detect average high memory pressure with monitoring tools 1) prevent oddities under load without swap (as in OP example).

Re:The Linux kernel's inability to gracefully handle low mem... by ddingus on Tuesday August 06, 2019 @01:36AM (#20622449)
Sgi IRIX nailed this, FWIW.

I would have thought some of the IRIX scheduler made it into Linux by now.

Re:The Linux kernel's inability to gracefully handle low mem... by Annatar on Tuesday August 06, 2019 @02:17AM (#20622640)
No way, any sufficiently advanced technology is indistinguishable from magic and IRIX was so very, very advanced. IRIX hasn't been in development for almost two decades now and it's still more advanced in aspects like guaranteed I/O and software management (inst(1M))... What does that say about it and what does it say about the engineers who worked on it?
Re:The Linux kernel's inability to gracefully handle low mem... by ddingus on Tuesday August 06, 2019 @03:04PM (#20628385)
And you are right on all counts. Inst was magic. The things I did, often on live systems...
Re:The Linux kernel's inability to gracefully handle low mem... by twic on Tuesday August 06, 2019 @03:57AM (#20623157)
XFS is still better than any version of that ext dreck!
Re:The Linux kernel's inability to gracefully handle low mem... by vthriller on Tuesday August 06, 2019 @08:31AM (#20624520)
That's debatable. Better at losing file contents after hard reset? Maybe.
Re:The Linux kernel's inability to gracefully handle low mem... by ddingus on Tuesday August 06, 2019 @02:04PM (#20627784)
Makes me want to gather them in a room and continue the process. Irix was so very good.
Re:The Linux kernel's inability to gracefully handle low mem... by IcePic on Friday August 09, 2019 @12:57PM (#20656167)
Irix even had "virtual swap" which had no (or very insufficient sized) backing store for it, just to handle all the superlarge allocations from which it only uses a tiny amount.
Re:The Linux kernel's inability to gracefully handle low mem... by nathell on Tuesday August 06, 2019 @01:59AM (#20622561)
As crazy as it sounds, can that fast device be a ramdisk?
Re:The Linux kernel's inability to gracefully handle low mem... by LargoLasskhyfv on Tuesday August 06, 2019 @03:56AM (#20623152)
Not crazy at all, very useful.

https://en.wikipedia.org/wiki/Zram

https://wiki.gentoo.org/wiki/Zram

https://wiki.debian.org/ZRam

Or Armbian, a Debian derivative for many RaspberryPi-like ARM boards, where it helps avoiding trashing the usual sd-cards, and holds the logs, with different compression algorithms for /var/log, /tmp and swap.

Their script is here:

https://github.com/armbian/build/blob/master/packages/bsp/co...

I really like how this enables a tiny NanoPi Neo2 with one GB Ram booting from a 64GB SD-Card in an aluminum case with SATA-adapter and a 2,5" HDD mostly idling to draw only about 1W from the wall socket, while clocking up from about 400Mhz to 1,3Ghz if it needs to. It's not the fastest, but sufficient.

Re:The Linux kernel's inability to gracefully handle low mem... by GuB-42 on Tuesday August 06, 2019 @03:25AM (#20622968)
Exactly what I thought. Losing <5% of your RAM as swap space probably won't do much difference.

But if it solves problems, why doesn't the kernel automatically assign part of RAM as a virtual swap device, if the user allows it. It can help monitoring tools, and because the kernel knows it is not a real swap device, it can optimize its use.

Re:The Linux kernel's inability to gracefully handle low mem... by pantalaimon on Tuesday August 06, 2019 @04:15AM (#20623247)
Re:The Linux kernel's inability to gracefully handle low mem... by snvzz on Tuesday August 06, 2019 @01:44PM (#20627561)
Swap has a side effect that's not very nice: It makes memory non-deterministic, as disk is non-deterministic.

Linux does unfortunately have serious issues to do with peaks of latency which make it behave horribly with realtime tasks such as pro audio. It's so bad that it's often perceiveable in desktop usage.

linux-rt does mitigate this considerably, but it's still not very good.

I'm hopeful for genode (with seL4 specifically), haiku, helenos and fuchsia.

Re:The Linux kernel's inability to gracefully handle low mem... by mnw21cam on Tuesday August 06, 2019 @05:12AM (#20623547)
Yes, this is exactly the cause. The pager hammers the disc to read clean pages, because they don't count as swap.

And I agree that a small amount of swap can actually reduce the paging that occurs, if you assume that the amount of RAM required is independent of the amount of RAM available. However, as we all know, stuff grows to fill the available space, and if you do configure swap you just delay the inevitable, not prevent it.

Having said that, having swap available means that when memory pressure occurs, you have a more graceful degradation in service, because the first you know about it is when the kernel starts swapping out an idle process to free memory for the new memory hog that you are interacting with. This slows down your interactive session, but not as much as if you have no swap available - in that case, the system suddenly and drastically reduces in performance because it is trying to swap in your interactive process. The more graceful degradation of having some swap available gives you a chance to realise that you are doing more than your computer can cope with, and stop.

As far as I see it, there are three solutions:

1. Disable overcommit. This tends to not play very nicely with things that allocate loads of virtual memory and not use it, like Java. And if you do have a load of processes that actually use all the memory they allocated, then you can still have the same problem occur. The solution to that one is to get the kernel to say no early, before the system actually runs out of RAM.

2. Get the OOM killer to kill things much earlier, before the system starts throwing away clean pages that are actually being used. On my system with 384GB RAM, I have installed earlyoom, and instructed it to trigger the OOM killer if the free RAM falls below 10% (and remember that stuff you are actually using, but happens to be clean, counts as free RAM). This is the easiest and quickest solution right now. If your main objection to this is that you are inviting the system to kill things that might be important, remember that the kernel already does this, and if you don't like it you should use option 1 above (and really hope that all your software handles malloc failure correctly).

3. Introduce a new system in the kernel to mark pages that are actually being regularly used but are clean as "important", and no longer count as free RAM for the purposes of calculating memory pressure. This could either be as a new madvise (but it would be impractical to get all software developers to start using this), or by marking all binary text by default (which would neglect the large read-only databases that some programs hammer), or by some heuristics. This would then trigger the OOM killer (or the allocator to say no, depending on overcommit) when actual free RAM is low.