mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Rich Felker <dalias@libc.org>
To: tugouxp <13824125580@163.com>
Cc: musl@lists.openwall.com
Subject: Re: [musl] Did the musl libc never decrease the brk pointer of Kernel? just increase ?
Date: Wed, 15 Dec 2021 10:10:21 -0500	[thread overview]
Message-ID: <20211215151020.GI7074@brightrain.aerifal.cx> (raw)
In-Reply-To: <367effaf.5eb0.17dbd6db592.Coremail.13824125580@163.com>

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

      reply	other threads:[~2021-12-15 15:10 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-12-15  9:30 tugouxp
2021-12-15 15:10 ` Rich Felker [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20211215151020.GI7074@brightrain.aerifal.cx \
    --to=dalias@libc.org \
    --cc=13824125580@163.com \
    --cc=musl@lists.openwall.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).