mailing list of musl libc
 help / color / mirror / code / Atom feed
* [musl] Did the musl libc never decrease the brk pointer of Kernel? just increase ?
@ 2021-12-15  9:30 tugouxp
  2021-12-15 15:10 ` Rich Felker
  0 siblings, 1 reply; 2+ messages in thread
From: tugouxp @ 2021-12-15  9:30 UTC (permalink / raw)
  To: musl

[-- Attachment #1: Type: text/plain, Size: 2806 bytes --]

Hi guys:


   i found i intresting things when i fix a memory leak issue on may platform which based on musl c library.  the issue has been fixed but a puzzle leave it to me.
in the file of malloc/mallocng/malloc.c,  a function called "alloc_meta" says that as belows,so you can see the brk pointer of brk system call parmeter never decrease the brk, is not it ? did gilibc also does like this way? 
why design like this, thank you !
struct meta *alloc_meta(void)
{
        struct meta *m;
        unsigned char *p;
        if (!ctx.init_done) {
#ifndef PAGESIZE
                ctx.pagesize = get_page_size();
#endif
                ctx.secret = get_random_secret();
                ctx.init_done = 1;
        }
        size_t pagesize = PGSZ;
        if (pagesize < 4096) pagesize = 4096;
        if ((m = dequeue_head(&ctx.free_meta_head))) return m;
        if (!ctx.avail_meta_count) {
                int need_unprotect = 1;
                if (!ctx.avail_meta_area_count && ctx.brk!=-1) {
                        uintptr_t new = ctx.brk + pagesize;
                        int need_guard = 0;
                        if (!ctx.brk) {
                                need_guard = 1;
                                ctx.brk = brk(0);
                                // some ancient kernels returned _ebss
                                // instead of next page as initial brk.
                                ctx.brk += -ctx.brk & (pagesize-1);
                                new = ctx.brk + 2*pagesize;
                        }
                        if (brk(new) != new) {
                                ctx.brk = -1;
                        } else {
                                if (need_guard) mmap((void *)ctx.brk, pagesize,
                                        PROT_NONE, MAP_ANON|MAP_PRIVATE|MAP_FIXED, -1, 0);
                                ctx.brk = new;
                                ctx.avail_meta_areas = (void *)(new - pagesize);
                                ctx.avail_meta_area_count = pagesize>>12;
                                need_unprotect = 0;
                        }
                }
                if (!ctx.avail_meta_area_count) {
                        size_t n = 2UL << ctx.meta_alloc_shift;
                        p = mmap(0, n*pagesize, PROT_NONE,
                                MAP_PRIVATE|MAP_ANON, -1, 0);
                        if (p==MAP_FAILED) return 0;
                        ctx.avail_meta_areas = p + pagesize;
                        ctx.avail_meta_area_count = (n-1)*(pagesize>>12);
                        ctx.meta_alloc_shift++;
/\<brk\>                                                                                                                                                                                  63,15-43      10%


[-- Attachment #2: Type: text/html, Size: 7339 bytes --]

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [musl] Did the musl libc never decrease the brk pointer of Kernel? just increase ?
  2021-12-15  9:30 [musl] Did the musl libc never decrease the brk pointer of Kernel? just increase ? tugouxp
@ 2021-12-15 15:10 ` Rich Felker
  0 siblings, 0 replies; 2+ messages in thread
From: Rich Felker @ 2021-12-15 15:10 UTC (permalink / raw)
  To: tugouxp; +Cc: musl

On Wed, Dec 15, 2021 at 05:30:23PM +0800, tugouxp wrote:
> Hi guys:
> 
> 
>    i found i intresting things when i fix a memory leak issue on may
> platform which based on musl c library. the issue has been fixed but
> a puzzle leave it to me. in the file of malloc/mallocng/malloc.c, a
> function called "alloc_meta" says that as belows,so you can see the
> brk pointer of brk system call parmeter never decrease the brk, is
> not it ? did gilibc also does like this way? why design like this,
> thank you !

mallocng only uses the brk area for the (out of band) metadata
structures, not actual storage space provided to the application. The
amount of storage here is a small fraction of the peak memory usage
during the process lifetime. Accounting for the unlikely possibility
that the entire most-recently-added block of metadata storage becomes
unneeded would add a lot more code complexity and additional data
complexity, and somewhat de-harden the allocator, for basically no
benefit.

In general, "brk" is not a good system for allocating and returning
memory because it can only return memory "at the end". mallocng
allocates all application memory via mmap, which isn't subject to that
limitation but allows abitrary page-granular units to be freed. The
only reason we use brk at all is that it provides an independent ASLR
zone whose location relative to both code and mmap-allocated memory is
somewhat unpredictable.

I'm not up to date on what current glibc malloc does, but historically
it has used the brk area in the traditional way, to actually get
storage to return to the application, and it can "trim" (reduce) it.
But this is a very different usage.

Rich

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2021-12-15 15:10 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-12-15  9:30 [musl] Did the musl libc never decrease the brk pointer of Kernel? just increase ? tugouxp
2021-12-15 15:10 ` Rich Felker

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).