[PATCH v3] malloc: Mark pages with MADV_DONTNEED to shrink and grow rather than mremap()

Carlos O'Donell carlos@redhat.com
Mon Aug 25 13:34:22 GMT 2025


On 8/19/25 7:57 AM, William Hunt wrote:
> When reallocating mmap()ed chunks, use madvise() if shrinking to mark unused
> pages as MADV_DONTNEED, only making a call to mremap() on failure. Allow
> growing within MADV_DONTNEED pages for later calls to realloc under the
> original size. This improves the efficiency of shrinking large mmap()ed
> chunks, as madvise() is significantly faster than mremap(). It also provides
> robustness, as if mremap() fails when shrinking, another madvise() without the
> threshold check is attempted, and if this fails the pointer is returned as to
> avoid a potential malloc+memcpy+free failure. Since mremap() fragments the VAS
> by usually shrinking in-place, using madvise() will keep the VAS intact while
> freeing the physical frames backing the unused pages, such that they will be
> zero-filled before the first access. To avoid the process' VAS from being
> exhausted, create a threshold for the maximum relative size of an mmap()ed
> chunk that can be marked MADV_DONTNEED.
> 
> Place the logic for reallocating mmap()ed chunks into an _int_realloc_mmapped
> function to increase modularity.
> 
> Create a tst-realloc-madvise.c test to verify that the relative threshold
> works as intended.
> 
> Create a bench-realloc-shrink.c benchtest to show a 210% increase in
> reallocs/sec when shrinking up until an arbitrary limit for the process,
> verifying that realloc does handle shrinking large mmap()ed chunks more
> efficiently when using madvise() rather than mremap().
> 
> Update malloc-check.c to call _int_realloc_mmapped directly when testing
> realloc if the chunk is mmapped.

Thank you again for more work in the area of the allocator.

Like your other patches this needs either DCO or assignment.

Please see:
https://sourceware.org/glibc/wiki/Contribution%20checklist

Thank you!

-- 
Cheers,
Carlos.



More information about the Libc-alpha mailing list