Re: malloc does not return null when out of memory

From: Matthew Whelan (muttley_at_gotadsl.co.uk)
Date: 07/26/03

  • Next message: Pete French: "Re: malloc does not return null when out of memory"
    Date: Sat, 26 Jul 2003 11:35:26 +0100
    To: Chuck Swiger <cswiger@mac.com>
    
    

    On Thu, 24 Jul 2003 19:18:06 -0400 Chuck Swiger <cswiger@mac.com> wrote:
    > Muttley wrote:
    > > Yes, I thought briefly about something like this.
    > >
    > > Then I thought 'there's a race condition'.
    >
    > Where? The FreeBSD implementation is wrapped in a THREAD_LOCK()...?

    Good point, well made

    > > Then I realised that other processes might not link against this malloc.
    >
    > Perhaps.

    Statically linked binaries, for example. Maybe Linux ones too?

    > > Then I realised the race condition doesn't even matter; processes will
    > > still be killed, as the kernel doesn't care that you're still in
    > > malloc() when the overcommitted memory is touched, it just knows you've
    > > touched it and there's no actual memory there. This will result in far
    > > more processes being killed. I believe that's a bad thing.
    >
    > Someone stated that it was a problem that malloc() returned pointers to
    > virtual address space that had been mapped but not allocated. This patch does
    > not guarantee that malloc() will return, but, if malloc() does returns a
    > pointer, using the memory being pointed to will refer to memory that is
    > allocated.

    Their main problem was that when memory ran out, processes got killed. The fact
    the process gets killed earlier doesn't alter the fact that it was killed.

    > As Barny Wolff said:
    > > Won't this merely die in malloc, not return 0?
    >
    > True. This isn't a perfect solution, but given the choice between:
    >
    > 1) malloc(LOTS) returning a pointer, and then sometime later the program dies
    > with a bus error when using that memory because no more VM is available, or
    >
    > 2) malloc(LOTS) causing an immediate failure in malloc(),
    >
    > ...choice #2 appears to be significantly better.
    >
    > Figuring out what went wrong from a coredump or backtrace for #2 when the
    > signal happens in malloc() should be obvious; determining why the program
    > crashed in the middle of referencing memory in some large buffer is
    > potentially misleading.

    If you re-read the original post in this thread, you will see that this appeared
    in the poster's syslog:

    Jul 23 01:37:57 m0n0wall /kernel: pid 80 (racoon), uid 0, was killed: out of swap space

    Finding out why the process died was never the big worry. Besides, see below...

    > Programs which take care to preallocate regions of memory they need before
    > they start doing a transaction or some other operation that needs to be atomic
    > would also prefer #2; the patch I proposed could have a beneficial impact on
    > data integrity for such programs.
    >

    Except that the process which cops the bullet in the head is the largest
    runnable non-system process. Check /usr/src/sys/vm/vm_pageout.c near the end of
    vm_pageout_scan(). Neither data integrity nor debugging from cores is helped by
    the patch.

    > --
    >
    > People who encounter programs crashing in malloc() are likely going to
    > continue to complain about malloc() not returning NULL when the system is out
    > of memory.
    >
    > If malloc() is referencing memory before returning the pointer, means that the
    > system is going to reserve VM resources with temporal locality towards memory
    > _allocation_ rather than memory _reference_. Having the program crash at
    > memory allocation time rather than usage helps identify when and where this
    > problem actually happens more clearly, if only by a little bit.

    See above, and above that.

    > I'm not sure whether allocating memory sooner that way will make it more
    > likely that brk()/sbrk() or mmap() will return ENOMEM to the libc malloc()
    > implementation, but if it does not help, perhaps that means something and
    > we've identified the location of problem more precisely.

    Other posts suggest these calls won't ever return ENOMEM based on total system
    usage, as the kernel doesn't even track it. Even if they went for something in
    the style of Brent's description of vswap, surely this patch would effectively
    prevent ENOMEM due forcing the overcommit to zero by killing something as soon
    as a process requests memory beyond the total physical+swap.

    I don't see how you can change the undesired behaviour in userland.

    Cheers,
    Matt

    -- 
    Matthew Whelan <muttley@gotadsl.co.uk>
    _______________________________________________
    freebsd-stable@freebsd.org mailing list
    http://lists.freebsd.org/mailman/listinfo/freebsd-stable
    To unsubscribe, send any mail to "freebsd-stable-unsubscribe@freebsd.org"
    

  • Next message: Pete French: "Re: malloc does not return null when out of memory"

    Relevant Pages

    • Re: sizeof(ptr) = ?
      ... The value returned by malloc() is of type 'void*', ... The memory is typeless until an object has been written ... Since 'void' is defined to be an incomplete ... an lvalue of a complete type, there must be a pointer conversion ...
      (comp.lang.c)
    • Re: Checking validity of a file pointer
      ... because the pointer looks valid. ... detected (preferably by having fclose return an error indication ... if the buffer of the file is returned from the malloc() function, ... with memory returned from mallocfunction in that buff ...
      (comp.lang.c)
    • Re: memory allocation wrapper
      ... I've written a wrapper for malloc and friends. ... The reason for doing writing this so that newbies ... How do I know how much memory a pointer points to? ...
      (comp.lang.c)
    • Re: 2D array of structures
      ... Don't cast the return value of malloc(), ... you allocate here memory for 7 such structures. ... a pointer to the start of this memory, which is of type 'STRUCTURE *' ...
      (comp.lang.c)
    • Re: Difference between Macro and Function...
      ... > | Look up the library documentation; malloc is used to request memory space ... using a pointer to freemakes sense. ... that share a single region of memory. ...
      (comp.lang.c)