News for hackers. Stuff that matters.
Re:Once upon a time, memory allocators made sense by joosters on Monday January 18, 2016 @09:26AM (#10924529)
As other people are commenting, malloc can still return NULL, but the critical point here is that even if you handle the NULL cases, you will still miss the other situations where your process runs out of memory. In short, if the OS over-commits RAM, it is impossible for a program to safely handle out of memory situations.

One reason why overcommit is popular is because of fork(). Imagine a process that does malloc(lots of memory), then forks and execs a tiny program (/bin/true or something like it). If the fork() call succeeds, this means the OS has guaranteed that all the memory in the child process is available to be written over. i.e. it has had to allocate 'lots of memory' x 2 in total, even if only 'lots of memory' x 1 will actually be used.

Without overcommit, fork() can fail even if the system won't ever use anywhere close to the limit of RAM+swap space.

Re:Once upon a time, memory allocators made sense by JoeAltmaier on Monday January 18, 2016 @09:35AM (#10924574)
I with you on the no-point-checking-for-malloc-fail issue. It has to be done in every place, or it rarely is effective. My old colleage Mike Rowe used to call it "Putting an altimeter on your car. So if you drive over a cliff, you can see how far it is to the ground."
Re:Once upon a time, memory allocators made sense by joosters on Monday January 18, 2016 @11:32AM (#10925283)
Definitely. In practice, it's impossible to do correctly. Even if your own code gets every malloc() correct (and also properly checks the return code of every syscall and library call that might fail due to lack of memory), you still have to trust that every library you use is also perfectly written and handles its memory failures just as perfectly. It'll never happen.
Re:Once upon a time, memory allocators made sense by JoeAltmaier on Monday January 18, 2016 @11:59AM (#10925424)
There's just one place that it makes sense: for very large buffers. If they fail, it doesn't mean the system is doomed. So it doesn't hurt to check that video-frame buffer alloc, or that file-decompression buffer. But for any of the small change (anything within orders of magnitude of the mean allocation size) its pointless.
Re:Once upon a time, memory allocators made sense by zAy0LfpBZLC8mAC on Monday January 18, 2016 @10:58AM (#10925081)
> In short, if the OS over-commits RAM, it is impossible for a program to safely handle out of memory situations.

That's a non-sequitur.

If you don't check for NULL return, you have undefined behaviour--that is: your program might end up doing just about anything. If the OOM killer kills you, all visible effects of your program will still be perfectly consistent with the semantics of your program. And you have to be able to safely deal with that scenario anyhow, as much more fundamental resources such as electricity might run out at any time as well.

Re:Once upon a time, memory allocators made sense by joosters on Monday January 18, 2016 @11:27AM (#10925257)
You're splitting hairs.

Imagine your program is processing a folder of emails. If the program is halted part-way through (or if you yank the power cable out) then the mail folder will be left in an inconsistent state.

If the program runs in an OS that does not overcommit memory, it is possible to write code that checks every malloc() and, if it hits a memory limit, you could shut down gracefully, fixing the mail folder so that its state is correct.

If the OS overcommits, then even if you checked every malloc() call, your program might die at any time because of a SIGSEGV or the OOM killer nuking it. There's no way to tidy up an incomplete run.

It's nothing to do with undefined behaviour, or dereferencing NULL pointers.

Re:Once upon a time, memory allocators made sense by zAy0LfpBZLC8mAC on Monday January 18, 2016 @12:19PM (#10925514)
If you leave something in a corrupted state when execution is aborted at some random point, that means that your code (and possibly your on-disk data structure) is defective. It is perfectly possible to handle that case safely (using transactions, journaling, rollback, atomic replace, ...).

However, if you don't check for allocation failures, you get undefined behaviour, which you indeed cannot handle safely ... other than by avoiding the undefined behaviour in the first place by checking for allocation failures.

Also, if you check for allocation failures, you won't get a SIGSEGV. SIGSEGV is for invalid virtual addresses, not for lack of resources.

Re:Once upon a time, memory allocators made sense by joosters on Monday January 18, 2016 @01:20PM (#10925885)
You can get memory errors outside of malloc() on an OS that overcommits RAM. Whether that is a SIGSEGV or some other signal, or just the heavy boots of the OOM killer nuking your process, doesn't really matter. The key point is that your program cannot handle them. It is impossible to handle every case.

Allocation failures can and do occur outside of malloc() when overcommit is the memory policy. Malloc might return memory that it believes is valid, but when you try to write to it later on, the OS discovers that it has no free space in the swap and no spare pages to evict. Result: process death (SIGSEGV? SIGBUS? not sure, but it doesn't matter)

Re:Once upon a time, memory allocators made sense by zAy0LfpBZLC8mAC on Monday January 18, 2016 @01:44PM (#10926027)
> You can get memory errors outside of malloc() on an OS that overcommits RAM.

Did anyone claim otherwise?

> Whether that is a SIGSEGV or some other signal, or just the heavy boots of the OOM killer nuking your process, doesn't really matter.

It does, because you can catch SIGSEGV.

> The key point is that your program cannot handle them. It is impossible to handle every case.

It can, and it has to, if you don't want it to be defective. (Where "handling" does not mean "continue execution", but "don't corrupt persistent state"--you cannot continue execution with insufficient resources anyway, there is no way around that).

> Result: process death (SIGSEGV? SIGBUS? not sure, but it doesn't matter)

SIGKILL. See above.

Re:Once upon a time, memory allocators made sense by btilly on Monday January 18, 2016 @04:19PM (#10927038)
Here is the mechanism that I wish existed...

Have a system call that allocates memory to a process or process group, but doesn't assign it. This call will fail if overall memory usage is too high.

When the process tries to get memory, it will only tap into that reserved allocation if the system is out of memory.

The OOM killer will never kill processes that have reserved spare memory.

The result? Long-running daemons with stable memory usage can be relatively easily protected against the OOM killer. Ideally the OOM killer never runs amok. But in the real world, it would be nice to be able to limit the damage if it has, and guarantee a usable shell for a superuser while it is going on.

Re:Once upon a time, memory allocators made sense by zAy0LfpBZLC8mAC on Monday January 18, 2016 @08:33PM (#10928337)
The thing is: There isn't really any such thing as "the memory of a process". What about a page of a shared library that hasn't been loaded yet, but is mapped in more than one process, for example?

Also, if memory is guaranteed to be available at a later point, it cannot really be used for anything, as there is nowhere to put the contents of that memory the moment the process it's reserved for wants to use it.

But there are two APIs that can be used to implement the same goal: Linux has a procfs setting "oom_score_adj" that can be used to decrease the risk of a particular process being killed (commonly used for sshd for obvious reasons) and there are the mlock()/mlockall() syscalls that you can use to make sure some address space is backed by actual RAM rather than potentially swap.

Re:Once upon a time, memory allocators made sense by btilly on Wednesday January 20, 2016 @12:03AM (#10936177)
Nothing you said disagreed with what I said.

It is true that the reserved memory could not be used for anything. This is a feature. The memory has been reserved for emergencies, and will keep part of the system usable when everything else goes belly up.

The two alternate methods that you specify are not as useful.

I cannot with oom_score_adj have a shell that can be logged in to and be entirely usable during a fork bomb. Yes, you can ssh in. But good luck running arbitrary commands.

With mlock()/mlockall() I can guarantee that a process is responsive during heavy memory pressure, but good luck if it needs to allocate more memory.

And neither system makes it particularly easy to set things up so that the OOM killer will avoid killing all daemons whose memory usage remained stable during memory pressure. (And that is the biggest problem with the OOM killer, that it killed random things you'd have preferred stayed up.)

Re:Once upon a time, memory allocators made sense by zAy0LfpBZLC8mAC on Wednesday January 20, 2016 @10:03AM (#10938500)
> I cannot with oom_score_adj have a shell that can be logged in to and be entirely usable during a fork bomb. Yes, you can ssh in. But good luck running arbitrary commands.

But that's due to PID exhaustion, not due to memory exhaustion.

> With mlock()/mlockall() I can guarantee that a process is responsive during heavy memory pressure, but good luck if it needs to allocate more memory.

Well, good luck if the process needs more memory than it had reserved in your scheme. Obviously, you have to mlock() sufficient memory for peak need.

> And neither system makes it particularly easy to set things up so that the OOM killer will avoid killing all daemons whose memory usage remained stable during memory pressure. (And that is the biggest problem with the OOM killer, that it killed random things you'd have preferred stayed up.)

No, it doesn't kill random things, it kills things that are easy to reconstruct (little CPU use so far) and that have lots of memory allocated, and where the oom_score_adj doesn't tell it to spare the process for other reasons.

Re:Once upon a time, memory allocators made sense by btilly on Wednesday January 20, 2016 @03:32PM (#10941114)
I have definitely been on a system with plenty of pids available, that was completely unusable due to memory exhaustion.

As for the OOM killer, its logic has changed over time. All that I definitely know is that if a slow leak in application code causes Apache processes to grow too quickly over time, it is usually a good idea to reboot the machine because you never know what random daemon got killed before the actual culprit.

Of course you also need to fix application code...

The logic that is applied probably works well in the desktop/developer case where what went wrong probably went wrong recently and there is a person who can notice. That isn't the context where I've usually encountered it.

Re:Once upon a time, memory allocators made sense by nightski on Monday January 18, 2016 @01:07PM (#10925809)
You might be technically right, but I guarantee you that by your definition the vast majority of software out there is defective. The solutions you mention are non-trivial. It is really easy to make a mistake. While it may not be the operating systems fault at the end of the day, designing a system like this is setting up the overall experience for failure. A well designed system should make it easy to get right and not the other way around.
Re:Once upon a time, memory allocators made sense by zAy0LfpBZLC8mAC on Monday January 18, 2016 @01:48PM (#10926053)
> You might be technically right, but I guarantee you that by your definition the vast majority of software out there is defective.

Your point being? That it's untechnically correct?

> The solutions you mention are non-trivial. It is really easy to make a mistake. While it may not be the operating systems fault at the end of the day, designing a system like this is setting up the overall experience for failure. A well designed system should make it easy to get right and not the other way around.

Which is all kindof true, but doesn't change that those APIs are the way they are, for historical reasons. Just because some API is broken, doesn't mean your code will work correctly when you pretend it's not broken.

edit: just to be sure: yes, the special-casing of zero-sized allocations is somewhat of a bug, which we still have to live with. Other than that, the API is actuall perfectly fine--if you don't check for allocation failures and you can't handle it if your program gets interrupted at any point, that's just a bug in your code, there is no useful general way to abstract those problems away.

Re:Once upon a time, memory allocators made sense by jimrandomh on Monday January 18, 2016 @11:53AM (#10925405)
I think the point is that it's easier to use techniques that keep the mail folder always in a consistent or recoverable state, such as journaling or copy-modify-move, than it is to fully handle out-of-memory conditions. Since the former technique is both easier and can handle problems that the latter can't, it's always preferable.
Re:Once upon a time, memory allocators made sense by markps on Monday January 18, 2016 @03:47PM (#10926800)
> If the fork() call succeeds, this means the OS has guaranteed that all the memory in the child process is available to be written over. i.e. it has had to allocate 'lots of memory' x 2 in total, even if only 'lots of memory' x 1 will actually be used.

That isn't the case anymore, at least on Linux and BSD. fork() uses a copy-on-write scheme so the OS only allocates/copies the parent memory space if the child attempts to write to it.

Re:Once upon a time, memory allocators made sense by btilly on Monday January 18, 2016 @04:09PM (#10926966)
You jut repeated the parent's point while thinking you disagreed with it.

The point was that after you fork() in a copy-on-write scheme, the OS now has promised that more memory is available to write on than may actually exist. If the OS avoided overallocating, it would have to right there and then reserve lots of memory (without necessarily writing to it) just to be sure that you wouldn't run out at a later date.

Re:Once upon a time, memory allocators made sense by markps on Monday January 18, 2016 @04:24PM (#10927073)
You're correct. I do agree with the point, but I misinterpreted where he was going with that example.
Re:Once upon a time, memory allocators made sense by justincormack on Monday January 18, 2016 @01:58PM (#10926123)
Use posix_spawn not fork and exec, then at least the OS may not allocate twice the memory, eg the BSDs will not.
Re:Once upon a time, memory allocators made sense by joosters on Monday January 18, 2016 @02:36PM (#10926369)
Good luck getting any existing 3rd-party code to use posix_spawn ...