From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=-3.3 required=5.0 tests=MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL autolearn=ham autolearn_force=no version=3.4.4 Received: (qmail 12820 invoked from network); 15 Dec 2021 15:10:37 -0000 Received: from mother.openwall.net (195.42.179.200) by inbox.vuxu.org with ESMTPUTF8; 15 Dec 2021 15:10:37 -0000 Received: (qmail 30462 invoked by uid 550); 15 Dec 2021 15:10:34 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-ID: Reply-To: musl@lists.openwall.com Received: (qmail 30427 invoked from network); 15 Dec 2021 15:10:34 -0000 Date: Wed, 15 Dec 2021 10:10:21 -0500 From: Rich Felker To: tugouxp <13824125580@163.com> Cc: musl@lists.openwall.com Message-ID: <20211215151020.GI7074@brightrain.aerifal.cx> References: <367effaf.5eb0.17dbd6db592.Coremail.13824125580@163.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <367effaf.5eb0.17dbd6db592.Coremail.13824125580@163.com> User-Agent: Mutt/1.5.21 (2010-09-15) Subject: Re: [musl] Did the musl libc never decrease the brk pointer of Kernel? just increase ? 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